forked from xdebug/xdebug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xdebug_gc_stats.c
282 lines (240 loc) · 7.49 KB
/
xdebug_gc_stats.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
273
274
275
276
277
278
279
280
281
282
/*
+----------------------------------------------------------------------+
| Xdebug |
+----------------------------------------------------------------------+
| Copyright (c) 2002-2018 Derick Rethans |
+----------------------------------------------------------------------+
| This source file is subject to version 1.01 of the Xdebug license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| https://xdebug.org/license.php |
| If you did not receive a copy of the Xdebug 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. |
+----------------------------------------------------------------------+
| Authors: Benjamin Eberlei <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_xdebug.h"
#include "xdebug_gc_stats.h"
#include "xdebug_stack.h"
#include "zend_builtin_functions.h"
#include "SAPI.h"
ZEND_EXTERN_MODULE_GLOBALS(xdebug)
static void xdebug_gc_stats_print_run(xdebug_gc_run *run);
static void xdebug_gc_stats_run_free(xdebug_gc_run *run);
int (*xdebug_old_gc_collect_cycles)(void);
int xdebug_gc_collect_cycles(void)
{
int ret;
uint32_t collected;
xdebug_gc_run *run;
zend_execute_data *execute_data;
long int memory;
double start;
xdebug_func tmp;
#if PHP_VERSION_ID >= 70300
zend_gc_status status;
#endif
if (!XG(gc_stats_enabled)) {
return xdebug_old_gc_collect_cycles();
}
execute_data = EG(current_execute_data);
#if PHP_VERSION_ID >= 70300
zend_gc_get_status(&status);
collected = status.collected;
#else
collected = GC_G(collected);
#endif
start = xdebug_get_utime();
memory = zend_memory_usage(0);
ret = xdebug_old_gc_collect_cycles();
run = xdmalloc(sizeof(xdebug_gc_run));
run->function_name = NULL;
run->class_name = NULL;
#if PHP_VERSION_ID >= 70300
zend_gc_get_status(&status);
run->collected = status.collected - collected;
#else
run->collected = GC_G(collected) - collected;
#endif
run->duration = xdebug_get_utime() - start;
run->memory_before = memory;
run->memory_after = zend_memory_usage(0);
xdebug_build_fname(&tmp, execute_data);
run->function_name = tmp.function ? xdstrdup(tmp.function) : NULL;
run->class_name = tmp.class ? xdstrdup(tmp.class) : NULL;
xdebug_gc_stats_print_run(run);
xdebug_gc_stats_run_free(run);
return ret;
}
static void xdebug_gc_stats_run_free(xdebug_gc_run *run)
{
if (run) {
if (run->function_name) {
xdfree(run->function_name);
}
if (run->class_name) {
xdfree(run->class_name);
}
xdfree(run);
}
}
int xdebug_gc_stats_init(char *fname, char *script_name)
{
char *filename = NULL;
if (fname && strlen(fname)) {
filename = xdstrdup(fname);
} else {
if (!strlen(XG(gc_stats_output_name)) ||
xdebug_format_output_filename(&fname, XG(gc_stats_output_name), script_name) <= 0)
{
return FAILURE;
}
if (IS_SLASH(XG(gc_stats_output_dir)[strlen(XG(gc_stats_output_dir)) - 1])) {
filename = xdebug_sprintf("%s%s", XG(gc_stats_output_dir), fname);
} else {
filename = xdebug_sprintf("%s%c%s", XG(gc_stats_output_dir), DEFAULT_SLASH, fname);
}
xdfree(fname);
}
XG(gc_stats_file) = xdebug_fopen(filename, "w", NULL, &XG(gc_stats_filename));
xdfree(filename);
if (!XG(gc_stats_file)) {
return FAILURE;
}
fprintf(XG(gc_stats_file), "Garbage Collection Report\n");
fprintf(XG(gc_stats_file), "version: 1\ncreator: xdebug %s (PHP %s)\n\n", XDEBUG_VERSION, PHP_VERSION);
fprintf(XG(gc_stats_file), "Collected | Efficiency%% | Duration | Memory Before | Memory After | Reduction%% | Function\n");
fprintf(XG(gc_stats_file), "----------+-------------+----------+---------------+--------------+------------+---------\n");
fflush(XG(gc_stats_file));
return SUCCESS;
}
void xdebug_gc_stats_stop()
{
XG(gc_stats_enabled) = 0;
if (XG(gc_stats_file)) {
fclose(XG(gc_stats_file));
XG(gc_stats_file) = NULL;
}
}
static void xdebug_gc_stats_print_run(xdebug_gc_run *run)
{
double reduction = (1 - (float)run->memory_after / (float)run->memory_before) * 100.0;
if (!XG(gc_stats_file)) {
return;
}
if (!run->function_name) {
fprintf(XG(gc_stats_file),
"%9lu | %9.2f %% | %5.2f ms | %13lu | %12lu | %8.2f %% | -\n",
run->collected,
(run->collected / 10000.0) * 100.0,
run->duration / 1000.0,
run->memory_before,
run->memory_after,
reduction
);
} else if (!run->class_name && run->function_name) {
fprintf(XG(gc_stats_file),
"%9lu | %9.2f %% | %5.2f ms | %13lu | %12lu | %8.2f %% | %s\n",
run->collected,
(run->collected / 10000.0) * 100.0,
run->duration / 1000.0,
run->memory_before,
run->memory_after,
reduction,
run->function_name
);
} else if (run->class_name && run->function_name) {
fprintf(XG(gc_stats_file),
"%9lu | %9.2f %% | %5.2f ms | %13lu | %12lu | %8.2f %% | %s::%s\n",
run->collected,
(run->collected / 10000.0) * 100.0,
run->duration / 1000.0,
run->memory_before,
run->memory_after,
reduction,
run->class_name,
run->function_name
);
}
fflush(XG(gc_stats_file));
}
/* {{{ proto void xdebug_get_gcstats_filename()
Returns the name of the current garbage collection statistics report file */
PHP_FUNCTION(xdebug_get_gcstats_filename)
{
if (XG(gc_stats_filename)) {
RETURN_STRING(XG(gc_stats_filename));
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto void xdebug_start_gcstats([string $fname])
Start collecting garbage collection statistics */
PHP_FUNCTION(xdebug_start_gcstats)
{
char *fname = NULL;
size_t fname_len = 0;
function_stack_entry *fse;
if (XG(gc_stats_enabled) == 0) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &fname, &fname_len) == FAILURE) {
return;
}
fse = xdebug_get_stack_frame(0 TSRMLS_CC);
if (xdebug_gc_stats_init(fname, fse->filename) == SUCCESS) {
XG(gc_stats_enabled) = 1;
RETVAL_STRING(XG(gc_stats_filename));
return;
} else {
php_error(E_NOTICE, "Garbage Collection statistics could not be started");
}
XG(gc_stats_enabled) = 0;
RETURN_FALSE;
} else {
php_error(E_NOTICE, "Garbage Collection statistics are already being collected.");
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto void xdebug_stop_gcstats()
Stop collecting garbage collection statistics */
PHP_FUNCTION(xdebug_stop_gcstats)
{
if (XG(gc_stats_enabled) == 1) {
RETVAL_STRING(XG(gc_stats_filename));
xdebug_gc_stats_stop();
} else {
RETVAL_FALSE;
php_error(E_NOTICE, "Garbage Collection statistics was not started");
}
}
/* {{{ proto void xdebug_get_gc_run_count()
Return number of times garbage collection was triggered. */
PHP_FUNCTION(xdebug_get_gc_run_count)
{
#if PHP_VERSION_ID >= 70300
zend_gc_status status;
#endif
#if PHP_VERSION_ID >= 70300
zend_gc_get_status(&status);
RETURN_LONG(status.runs);
#else
RETURN_LONG(GC_G(gc_runs));
#endif
}
/* {{{ proto void xdebug_get_gc_total_collected_roots()
Return total number of collected root variables during garbage collection. */
PHP_FUNCTION(xdebug_get_gc_total_collected_roots)
{
#if PHP_VERSION_ID >= 70300
zend_gc_status status;
#endif
#if PHP_VERSION_ID >= 70300
zend_gc_get_status(&status);
RETURN_LONG(status.collected);
#else
RETURN_LONG(GC_G(collected));
#endif
}