-
Notifications
You must be signed in to change notification settings - Fork 2
/
aware_request.c
155 lines (125 loc) · 5.18 KB
/
aware_request.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
+----------------------------------------------------------------------+
| PHP Version 5 / aware |
+----------------------------------------------------------------------+
| Copyright (c) 2009 Mikko Koppanen |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Mikko Kopppanen <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_aware_private.h"
/* gettimeofday */
#ifdef PHP_WIN32
# include "win32/php_aware_win32.h"
#endif
#ifdef HAVE_GETTIMEOFDAY
# ifndef timersub
#define timersub(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
if ((vvp)->tv_usec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_usec += 1000000; \
} \
} while (0);
# endif /* ifndef timersub */
#define timeval_to_msec(_my_tv) ((_my_tv.tv_sec * 1000) + (_my_tv.tv_usec / 1000))
#define timeval_to_double(_my_tv) (double)(_my_tv).tv_sec + ((double)(_my_tv).tv_usec / 1000000.0)
/* {{{ static void php_aware_capture_slow_request(long elapsed, long threshold, double u_time, double s_time, const char *format, ...)
*/
static void php_aware_capture_slow_request(long elapsed, long threshold, double u_time, double s_time, const char *format, ...)
{
va_list args;
zval *event, *slow_request;
ALLOC_INIT_ZVAL(event);
array_init(event);
ALLOC_INIT_ZVAL(slow_request);
array_init(slow_request);
add_assoc_long(slow_request, "time_elapsed", elapsed);
add_assoc_long(slow_request, "slow_request_threshold", threshold);
/* Info about where the time is spent, TODO: check WIN32 */
add_assoc_double(slow_request, "rusage_user_time", u_time);
add_assoc_double(slow_request, "rusage_system_time", s_time);
add_assoc_bool(event, "slow_request", 1);
add_assoc_zval(event, "_AWARE_REQUEST", slow_request);
va_start(args, format);
php_aware_capture_error_ex(event, E_CORE_WARNING, "aware internal report", 0, 1, format, args);
va_end(args);
}
/* }}} */
/* {{{ zend_bool php_aware_init_slow_request_monitor(struct timeval *request_start, struct rusage *request_start_rusage)
*/
zend_bool php_aware_init_slow_request_monitor(struct timeval *request_start, struct rusage *request_start_rusage)
{
if (gettimeofday(request_start, NULL) == 0) {
if (getrusage(RUSAGE_SELF, request_start_rusage) == 0) {
return 1;
}
}
return 0;
}
/* }}} */
/* {{{ void php_aware_monitor_slow_request(struct timeval *request_start, struct rusage *request_start_rusage, long threshold)
*/
void php_aware_monitor_slow_request(struct timeval *request_start, struct rusage *request_start_rusage, long threshold)
{
struct timeval request_end;
if (gettimeofday(&request_end, NULL) == 0) {
struct timeval request_diff;
long elapsed;
timersub(&request_end, request_start, &request_diff);
elapsed = timeval_to_msec(request_diff);
if (elapsed > threshold) {
double u_time = 0.0, s_time = 0.0;
struct timeval tv_utime, tv_stime;
struct rusage request_end_rusage;
if (getrusage(RUSAGE_SELF, &request_end_rusage) == 0) {
timersub(&request_end_rusage.ru_utime, &(request_start_rusage->ru_utime), &tv_utime);
timersub(&request_end_rusage.ru_stime, &(request_start_rusage->ru_stime), &tv_stime);
u_time = timeval_to_double(tv_utime);
s_time = timeval_to_double(tv_stime);
}
php_aware_capture_slow_request(elapsed, threshold, u_time, s_time, "Slow request detected");
}
}
}
/* }}} */
#endif /* ifdef HAVE_GETTIMEOFDAY */
/* {{{ static void php_aware_capture_memory_usage(long peak, long threshold, const char *format, ...)
*/
static void php_aware_capture_memory_usage(long peak, long threshold, const char *format, ...)
{
va_list args;
zval *event, *peak_usage;
ALLOC_INIT_ZVAL(event);
array_init(event);
ALLOC_INIT_ZVAL(peak_usage);
array_init(peak_usage);
add_assoc_long(peak_usage, "memory_peak_usage", peak);
add_assoc_long(peak_usage, "memory_usage_threshold", threshold);
add_assoc_zval(event, "_AWARE_MEMORY", peak_usage);
add_assoc_bool(event, "excessive_memory_usage", 1);
va_start(args, format);
php_aware_capture_error_ex(event, E_CORE_WARNING, "aware internal report", 0, 1, format, args);
va_end(args);
}
/* }}} */
/* {{{ void php_aware_monitor_memory_usage(long threshold TSRMLS_DC)
*/
void php_aware_monitor_memory_usage(long threshold TSRMLS_DC)
{
long peak = zend_memory_peak_usage(1 TSRMLS_CC);
if (peak > threshold) {
php_aware_capture_memory_usage(peak, threshold, "Excessive memory usage detected");
}
}
/* }}} */