-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_apm.c
More file actions
229 lines (188 loc) · 5.89 KB
/
Copy pathphp_apm.c
File metadata and controls
229 lines (188 loc) · 5.89 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "php.h"
#include "php_apm.h"
#include "Zend/zend_extensions.h"
#include "Zend/zend_exceptions.h"
#include "Zend/zend_execute.h"
#include "Zend/zend_observer.h"
#include "Zend/zend_smart_str.h"
#include "ext/standard/info.h"
#include <inttypes.h>
#include <stdarg.h>
#include <sys/resource.h>
#include <time.h>
ZEND_DECLARE_MODULE_GLOBALS(apm)
static void php_apm_init_globals(zend_apm_globals *apm_globals)
{
apm_globals->enabled = 1;
apm_globals->log_file = NULL;
apm_globals->log_fp = NULL;
apm_globals->request_start = 0;
apm_globals->cpu_user_start = 0;
apm_globals->cpu_sys_start = 0;
apm_globals->original_error_cb = NULL;
}
static void apm_log(const char *format, ...)
{
va_list args;
char *buffer = NULL;
if (!APM_G(enabled)) {
return;
}
va_start(args, format);
if (APM_G(log_fp)) {
vfprintf(APM_G(log_fp), format, args);
fprintf(APM_G(log_fp), "\n");
fflush(APM_G(log_fp));
} else {
vspprintf(&buffer, 0, format, args);
if (buffer) {
php_error_docref(NULL, E_NOTICE, "[apm] %s", buffer);
efree(buffer);
}
}
va_end(args);
}
static uint64_t apm_hrtime_now(void)
{
return zend_hrtime();
}
static uint64_t apm_cpu_time_usec(void)
{
struct rusage usage;
if (getrusage(RUSAGE_SELF, &usage) != 0) {
return 0;
}
return (uint64_t)usage.ru_utime.tv_sec * 1000000ULL + (uint64_t)usage.ru_utime.tv_usec
+ (uint64_t)usage.ru_stime.tv_sec * 1000000ULL + (uint64_t)usage.ru_stime.tv_usec;
}
static void apm_error_cb(int type, const char *error_filename, const uint32_t error_lineno, zend_string *message)
{
if (APM_G(enabled)) {
apm_log("error type=%d file=%s line=%u message=%s",
type,
error_filename ? error_filename : "unknown",
error_lineno,
message ? ZSTR_VAL(message) : "unknown");
}
if (APM_G(original_error_cb)) {
APM_G(original_error_cb)(type, error_filename, error_lineno, message);
}
}
static void apm_observer_fcall_begin(zend_execute_data *execute_data)
{
uint64_t start = apm_hrtime_now();
zend_hash_index_update_mem(&APM_G(call_times), (zend_ulong)execute_data, &start, sizeof(start));
}
static void apm_observer_fcall_end(zend_execute_data *execute_data, zval *return_value)
{
uint64_t *start_ptr = zend_hash_index_find_ptr(&APM_G(call_times), (zend_ulong)execute_data);
uint64_t duration = 0;
if (start_ptr != NULL) {
duration = apm_hrtime_now() - *start_ptr;
zend_hash_index_del(&APM_G(call_times), (zend_ulong)execute_data);
}
if (APM_G(enabled)) {
const char *func_name = "{main}";
if (execute_data && execute_data->func && execute_data->func->common.function_name) {
func_name = ZSTR_VAL(execute_data->func->common.function_name);
}
apm_log("trace function=%s duration_ns=%" PRIu64, func_name, duration);
}
(void)return_value;
}
static zend_observer_fcall_handlers apm_observer_fcall_register(zend_execute_data *execute_data)
{
zend_observer_fcall_handlers handlers = {0};
if (!APM_G(enabled)) {
return handlers;
}
handlers.begin = apm_observer_fcall_begin;
handlers.end = apm_observer_fcall_end;
return handlers;
}
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("apm.enabled", "1", PHP_INI_ALL, OnUpdateBool, enabled, zend_apm_globals, apm_globals)
STD_PHP_INI_ENTRY("apm.log_file", "", PHP_INI_ALL, OnUpdateString, log_file, zend_apm_globals, apm_globals)
PHP_INI_END()
PHP_MINIT_FUNCTION(apm)
{
REGISTER_INI_ENTRIES();
APM_G(original_error_cb) = zend_error_cb;
zend_error_cb = apm_error_cb;
zend_observer_fcall_register = apm_observer_fcall_register;
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(apm)
{
zend_error_cb = APM_G(original_error_cb);
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_RINIT_FUNCTION(apm)
{
#if defined(ZTS) && defined(COMPILE_DL_APM)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
if (!APM_G(enabled)) {
return SUCCESS;
}
zend_hash_init(&APM_G(call_times), 16, NULL, NULL, 0);
APM_G(request_start) = apm_hrtime_now();
APM_G(cpu_user_start) = apm_cpu_time_usec();
APM_G(cpu_sys_start) = 0;
if (APM_G(log_file) && ZSTR_LEN(APM_G(log_file)) > 0) {
APM_G(log_fp) = fopen(ZSTR_VAL(APM_G(log_file)), "a");
if (!APM_G(log_fp)) {
php_error_docref(NULL, E_WARNING, "Unable to open apm.log_file: %s", ZSTR_VAL(APM_G(log_file)));
}
}
if (SG(request_info).request_method && SG(request_info).request_uri) {
apm_log("request method=%s uri=%s", SG(request_info).request_method, SG(request_info).request_uri);
} else {
apm_log("request method=cli uri=cli");
}
apm_log("zend_status version=%s", PHP_VERSION);
apm_log("opcache_status note=use_opcache_get_status_in_userland");
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(apm)
{
if (!APM_G(enabled)) {
return SUCCESS;
}
uint64_t duration = apm_hrtime_now() - APM_G(request_start);
uint64_t cpu_total = apm_cpu_time_usec();
apm_log("request duration_ns=%" PRIu64 " cpu_total_usec=%" PRIu64, duration, cpu_total);
if (APM_G(log_fp)) {
fclose(APM_G(log_fp));
APM_G(log_fp) = NULL;
}
zend_hash_destroy(&APM_G(call_times));
return SUCCESS;
}
PHP_MINFO_FUNCTION(apm)
{
php_info_print_table_start();
php_info_print_table_header(2, "apm support", "enabled");
php_info_print_table_row(2, "Version", PHP_APM_VERSION);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
zend_module_entry apm_module_entry = {
STANDARD_MODULE_HEADER,
"apm",
NULL,
PHP_MINIT(apm),
PHP_MSHUTDOWN(apm),
PHP_RINIT(apm),
PHP_RSHUTDOWN(apm),
PHP_MINFO(apm),
PHP_APM_VERSION,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_APM
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE();
#endif
ZEND_GET_MODULE(apm)
#endif