-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf.c
272 lines (210 loc) · 6.46 KB
/
perf.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
+----------------------------------------------------------------------+
| perf |
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: krakjoe |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <time.h>
#include <signal.h>
#include <pthread.h>
#include "php.h"
#include "ext/standard/info.h"
#include "src/monitor.h"
#include "php_perf.h"
typedef struct _php_perf_el_t {
zend_function function;
zend_op opline;
} php_perf_el_t;
typedef struct _php_perf_globals_t {
pthread_t thread;
timer_t timer;
php_perf_monitor_t *monitor;
zend_execute_data *frame;
} php_perf_globals_t;
ZEND_TLS php_perf_globals_t php_perf_globals;
#define PEG(e) php_perf_globals.e
static zend_bool php_perf_enabled = 0;
static zend_long php_perf_interval = -1;
void (*zend_interrupt_callback) (zend_execute_data *) = NULL;
static void php_perf_interrupt(zend_execute_data *frame) {
PEG(frame) = frame;
php_perf_monitor_set(
PEG(monitor), PHP_PERF_COLLECT, 1);
php_perf_monitor_wait(PEG(monitor), PHP_PERF_DONE);
if (zend_interrupt_callback) {
zend_interrupt_callback(frame);
}
}
static void* php_perf_routine(void *pg) {
php_perf_globals_t *peg =
(php_perf_globals_t*) pg;
uint32_t state;
HashTable *perf =
(HashTable*) calloc(1, sizeof(HashTable));
zend_hash_init(perf, 10000, NULL, NULL, 1);
php_perf_monitor_set(peg->monitor, PHP_PERF_WAITING, 1);
while ((state = php_perf_monitor_wait(peg->monitor, PHP_PERF_COLLECT|PHP_PERF_CLOSE))) {
php_perf_el_t el;
if ((state & PHP_PERF_CLOSE)) {
break;
}
el.function = *peg->frame->func;
el.opline = *peg->frame->opline;
php_perf_monitor_set(peg->monitor, PHP_PERF_DONE, 1);
zend_hash_next_index_insert_mem(
perf, &el, sizeof(php_perf_el_t));
}
php_perf_monitor_set(peg->monitor, PHP_PERF_DONE, 1);
pthread_exit((void*) perf);
return NULL;
}
/* {{{ */
PHP_MINIT_FUNCTION(perf)
{
char *interval = getenv("PHP_PERF_INTERVAL");
if (interval) {
php_perf_interval =
zend_atoi(interval, strlen(interval));
if (php_perf_interval > 0) {
php_perf_enabled = 1;
zend_interrupt_callback = zend_interrupt_function;
zend_interrupt_function = php_perf_interrupt;
}
}
return SUCCESS;
} /* }}} */
/* {{{ */
static void php_perf_notify(union sigval sv) {
zend_bool *interrupt =
(zend_bool*) sv.sival_ptr;
*interrupt = 1;
} /* }}} */
/* {{{ */
static int php_perf_startup() {
struct sigevent ev;
struct itimerspec its;
struct timespec ts;
memset(&ev, 0, sizeof(ev));
ev.sigev_notify = SIGEV_THREAD;
ev.sigev_notify_function = php_perf_notify;
ev.sigev_value.sival_ptr = &EG(vm_interrupt);
if (timer_create(CLOCK_MONOTONIC, &ev, &PEG(timer)) != SUCCESS) {
return FAILURE;
}
ts.tv_sec = 0;
ts.tv_nsec = php_perf_interval * 1000;
its.it_interval = ts;
its.it_value = ts;
if (timer_settime(PEG(timer), 0, &its, NULL) != SUCCESS) {
return FAILURE;
}
return SUCCESS;
} /* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(perf)
{
#if defined(ZTS) && defined(COMPILE_DL_PERF)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
if (!php_perf_enabled) {
return SUCCESS;
}
PEG(monitor) = php_perf_monitor_create();
if (pthread_create(&PEG(thread), NULL, php_perf_routine, &php_perf_globals) != SUCCESS) {
php_perf_monitor_destroy(PEG(monitor));
return FAILURE;
}
if (php_perf_startup() != SUCCESS) {
return FAILURE;
}
php_perf_monitor_wait(PEG(monitor), PHP_PERF_WAITING);
return SUCCESS;
}
/* }}} */
/* {{{ */
static void php_perf_process(HashTable *perf) {
uint32_t idx;
php_perf_el_t *el;
php_printf("got %d frames\n", zend_hash_num_elements(perf));
ZEND_HASH_FOREACH_NUM_KEY_PTR(perf, idx, el) {
php_printf("%s:%d %s\n",
el->function.common.function_name ?
ZSTR_VAL(el->function.common.function_name) :
"main()",
el->opline.lineno,
zend_get_opcode_name(el->opline.opcode));
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(perf);
free(perf);
} /* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(perf)
{
HashTable *perf;
if (!php_perf_enabled) {
return SUCCESS;
}
timer_delete(PEG(timer));
php_perf_monitor_set(
PEG(monitor), PHP_PERF_CLOSE, 1);
php_perf_monitor_wait(
PEG(monitor), PHP_PERF_DONE);
pthread_join(
PEG(thread), (void*) &perf);
php_perf_monitor_destroy(PEG(monitor));
php_perf_process(perf);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(perf)
{
php_info_print_table_start();
php_info_print_table_header(2, "perf support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ perf_functions[]
*/
static const zend_function_entry perf_functions[] = {
PHP_FE_END
};
/* }}} */
/* {{{ perf_module_entry
*/
zend_module_entry perf_module_entry = {
STANDARD_MODULE_HEADER,
"perf",
perf_functions,
PHP_MINIT(perf),
NULL,
PHP_RINIT(perf),
PHP_RSHUTDOWN(perf),
PHP_MINFO(perf),
PHP_PERF_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_PERF
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(perf)
#endif