forked from krakjoe/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_profiler.c
More file actions
262 lines (222 loc) · 7.2 KB
/
php_profiler.c
File metadata and controls
262 lines (222 loc) · 7.2 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
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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 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: Joe Watkins <joe.watkins@live.co.uk> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "php_streams.h"
#include "ext/standard/info.h"
#include "php_profiler.h"
ZEND_DECLARE_MODULE_GLOBALS(profiler)
#define PROFILE_NORMAL 1
#define PROFILE_INTERN 2
#ifndef _WIN32
static __inline__ ticks_t ticks(void) {
unsigned x, y;
asm volatile ("rdtsc" : "=a" (x), "=d" (y));
return ((((ticks_t)x) | ((ticks_t)y) << 32));
}
#else
# include <intrin.h>
static inline ticks_t ticks(void) {
return __rdtsc();
}
#endif
static void profiler_execute(zend_op_array *ops TSRMLS_DC);
static void profiler_execute_internal(zend_execute_data *data, int return_value_used TSRMLS_DC);
const zend_function_entry profiler_functions[] = {
PHP_FE(profiler_enable, NULL)
PHP_FE(profiler_output, NULL)
PHP_FE(profiler_disable, NULL)
PHP_FE_END
};
zend_module_entry profiler_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"profiler",
profiler_functions,
PHP_MINIT(profiler),
PHP_MSHUTDOWN(profiler),
PHP_RINIT(profiler),
PHP_RSHUTDOWN(profiler),
PHP_MINFO(profiler),
#if ZEND_MODULE_API_NO >= 20010901
"0.4",
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_PROFILER
ZEND_GET_MODULE(profiler)
#endif
PHP_INI_BEGIN()
PHP_INI_ENTRY("profiler.enabled", "0", PHP_INI_SYSTEM, NULL)
PHP_INI_ENTRY("profiler.memory", "1", PHP_INI_SYSTEM, NULL)
PHP_INI_ENTRY("profiler.output", "/tmp/profile.callgrind", PHP_INI_SYSTEM, NULL)
PHP_INI_END()
static inline void profiler_globals_ctor(zend_profiler_globals *pg TSRMLS_DC) {
pg->enabled = 0;
pg->memory = 1;
pg->output = NULL;
pg->reset = 0;
pg->frame = &pg->frames[0];
pg->limit = &pg->frames[PROFILER_MAX_FRAMES];
}
static inline void profiler_globals_dtor(zend_profiler_globals *pg TSRMLS_DC) {}
PHP_MINIT_FUNCTION(profiler)
{
zend_execute = profiler_execute;
zend_execute_internal = profiler_execute_internal;
REGISTER_LONG_CONSTANT("PROFILE_NORMAL", PROFILE_NORMAL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PROFILE_INTERN", PROFILE_NORMAL, CONST_CS | CONST_PERSISTENT);
REGISTER_INI_ENTRIES();
ZEND_INIT_MODULE_GLOBALS(profiler, profiler_globals_ctor, profiler_globals_dtor);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(profiler)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
PHP_RINIT_FUNCTION(profiler)
{
PROF_G(enabled) = INI_BOOL("profiler.enabled");
PROF_G(memory) = INI_BOOL("profiler.memory");
PROF_G(output) = INI_STR("profiler.output");
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(profiler)
{
if (PROF_G(output)) {
FILE *stream = fopen(PROF_G(output), "w");
if (stream) {
profile_t profile, end;
fprintf(stream, "version: 1\n");
fprintf(stream, "creator: profiler\n");
fprintf(stream, "pid: %d\n", getpid());
if (PROF_G(memory)) {
fprintf(stream, "events: memory cpu\n");
} else fprintf(stream, "events: cpu\n");
profile = &PROF_G(frames)[0];
end = PROF_G(frame);
do {
if (profile) {
fprintf(stream, "fl=%s\n", profile->location.file);
if (profile->call.scope && strlen(profile->call.scope)) {
fprintf(stream, "fn=%s::%s\n", profile->call.scope, profile->call.function);
} else fprintf(stream, "fn=%s\n", profile->call.function);
if (PROF_G(memory)) {
fprintf(
stream,
"%d %ld %lld\n",
profile->location.line, (profile->call.memory > 0L) ? profile->call.memory : 0L, profile->call.cpu
);
} else fprintf(
stream,
"%d %lld\n",
profile->location.line, profile->call.cpu
);
fprintf(stream, "\n");
} else break;
} while (++profile < end);
fclose(stream);
} else zend_error(E_WARNING, "the profiler has failed to open %s for writing", PROF_G(output));
if (PROF_G(reset))
free(PROF_G(output));
}
return SUCCESS;
}
PHP_MINFO_FUNCTION(profiler)
{
php_info_print_table_start();
php_info_print_table_header(2, "profiler support", "enabled");
php_info_print_table_end();
}
PHP_FUNCTION(profiler_enable)
{
if (!PROF_G(enabled)) {
PROF_G(enabled)=1;
} else zend_error(E_WARNING, "the profiler is already enabled");
}
PHP_FUNCTION(profiler_output)
{
uint flength;
char *fpath;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &fpath, &flength)==SUCCESS) {
if (PROF_G(reset))
free(PROF_G(output));
PROF_G(output) = malloc(flength+1);
if (PROF_G(output)) {
if (strncpy(PROF_G(output), fpath, flength) != PROF_G(output)) {
PROF_G(output)=INI_STR("profiler.output");
PROF_G(reset)=0;
free(PROF_G(output));
RETURN_NULL();
}
}
PROF_G(reset)=1;
}
}
PHP_FUNCTION(profiler_disable)
{
if (PROF_G(enabled)) {
PROF_G(enabled)=0;
} else zend_error(E_WARNING, "the profiler is already disabled");
}
static inline void _profile(void *_input, uint type, int uret TSRMLS_DC) {
ulong line = 0L;
if (PROF_G(enabled) &&
(PROF_G(frame) < PROF_G(limit)) &&
(line = zend_get_executed_lineno(TSRMLS_C))) {
profile_t profile = PROF_G(frame)++;
profile->type = type;
profile->location.file = zend_get_executed_filename(TSRMLS_C);
profile->location.line = line;
profile->call.function = get_active_function_name(TSRMLS_C);
profile->call.scope = (EG(called_scope)) ? EG(called_scope)->name : "";
if (PROF_G(memory))
profile->call.memory = zend_memory_usage(0 TSRMLS_CC);
profile->call.cpu = ticks();
switch(type) {
case PROFILE_INTERN: {
execute_internal((zend_execute_data *) _input, uret TSRMLS_CC);
} break;
case PROFILE_NORMAL: {
execute((zend_op_array*) _input TSRMLS_CC);
} break;
}
profile->call.cpu = ticks() - profile->call.cpu;
if (PROF_G(memory))
profile->call.memory = (zend_memory_usage(0 TSRMLS_CC) - profile->call.memory);
} else switch(type) {
case PROFILE_INTERN: {
execute_internal((zend_execute_data *) _input, uret TSRMLS_CC);
} break;
case PROFILE_NORMAL: {
execute((zend_op_array*) _input TSRMLS_CC);
} break;
}
}
static void profiler_execute(zend_op_array *ops TSRMLS_DC) {
_profile(ops, PROFILE_NORMAL, 0 TSRMLS_CC);
}
static void profiler_execute_internal(zend_execute_data *data, int return_value_used TSRMLS_DC) {
_profile(data, PROFILE_INTERN, return_value_used TSRMLS_CC);
}