-
Notifications
You must be signed in to change notification settings - Fork 1
/
xhprof.module
517 lines (467 loc) · 16.5 KB
/
xhprof.module
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
<?php
require_once(dirname(__FILE__) . '/xhprof.inc');
define('XHPROF_PATH', 'admin/reports/xhprof');
/**
* Implementation of hook_menu().
*/
function xhprof_menu() {
$items = array();
$items[XHPROF_PATH] = array(
'title' => 'XHProf runs',
'page callback' => 'xhprof_run_list',
'access arguments' => array('access xhprof data'),
'description' => 'View XHProf profiling data.',
);
$items[XHPROF_PATH . '/%'] = array(
'title' => 'XHProf view',
'page callback' => 'xhprof_display_page',
'page arguments' => array(3),
'access arguments' => array('access xhprof data'),
);
$items[XHPROF_PATH . '/%/callgraph'] = array(
'title' => 'XHProf callgraph',
'page callback' => 'xhprof_display_callgraph',
'page arguments' => array(3),
'access arguments' => array('access xhprof data'),
);
$items[XHPROF_PATH . '/diff/%/%'] = array(
'title' => 'XHProf view',
'page callback' => 'xhprof_display_diff_page',
'page arguments' => array(4, 5),
'access arguments' => array('access xhprof data'),
);
$items[XHPROF_PATH . '/%/symbol/%'] = array(
'title' => 'XHProf view',
'page callback' => 'xhprof_display_page',
'page arguments' => array(3, 5),
'access arguments' => array('access xhprof data'),
);
$items['admin/config/development/xhprof'] = array(
'title' => 'XHProf settings',
'description' => 'Configure XHProf profiler settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('xhprof_admin_settings'),
'file' => 'xhprof.admin.inc',
'access arguments' => array('administer site configuration'),
);
return $items;
}
/**
* Implementation of hook_permission().
*/
function xhprof_permission() {
return array(
'access xhprof data' => array(
'title' => t('Access XHProf data'),
),
);
}
/**
* Implementation of hook_theme()
*/
function xhprof_theme() {
return array(
'xhprof_overall_summary' => array(
'variables' => array('totals' => NULL, 'possible_metrics' => NULL, 'metrics' => NULL, 'display_calls' => NULL),
),
'xhprof_run_table' => array(
'variables' => array('stats' => NULL, 'totals' => NULL, 'url_params' => NULL, 'title' => NULL, 'flat_data' => NULL, 'limit' => NULL),
),
);
}
/**
* Implementation of hook_views_api().
*/
function xhprof_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'xhprof'),
);
}
/**
* Conditionally enable XHProf profiling.
*/
function xhprof_xhprof_enable() {
if (xhprof_is_enabled()) {
// @todo: consider a variable per-flag instead.
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
return TRUE;
}
else {
return FALSE;
}
}
/**
* Check whether XHProf should be enabled for the current request.
*/
function xhprof_is_enabled() {
$enabled = FALSE;
if (extension_loaded('xhprof') && variable_get('xhprof_enabled', FALSE)) {
$enabled = TRUE;
if (arg(0) == 'admin' && variable_get('xhprof_disable_admin_paths', TRUE)) {
$enabled = FALSE;
}
$interval = variable_get('xhprof_interval');
if (isset($interval) && mt_rand(1, $interval) % $interval != 0) {
$enabled = FALSE;
}
}
return $enabled;
}
/**
* Implementation of hook_boot(). Runs even for cached pages.
*/
function xhprof_boot() {
// Initialize XHProf.
if (xhprof_xhprof_enable()) {
drupal_register_shutdown_function('xhprof_shutdown');
}
}
/**
* See xhprof_start() which registers this function as a shutdown function.
*/
function xhprof_shutdown() {
// Register the real shutdown function so it runs later than other shutdown functions.
drupal_register_shutdown_function('xhprof_shutdown_real');
global $xhprof_run_id;
$xhprof_run_id = xhprof_is_enabled() ? xhprof_shutdown_xhprof(): NULL;
if ($xhprof_run_id && function_exists('drush_log')) {
drush_log('xhprof link: ' . xhprof_link($xhprof_run_id, 'url'), 'notice');
}
}
/**
* See xhprof_shutdown() which registers this function as a shutdown function. Displays xhprof link in the footer.
*/
function xhprof_shutdown_real() {
global $user;
// Try not to break non html pages.
if (function_exists('drupal_get_http_header')) {
$header = drupal_get_http_header('content-type');
if ($header) {
$formats = array('xml', 'javascript', 'json', 'plain', 'image', 'application', 'csv', 'x-comma-separated-values');
foreach ($formats as $format) {
if (strstr($header, $format)) {
return;
}
}
}
}
$output = $txt = '';
if (isset($user) && user_access('access xhprof data')) {
$output .= '<div class="xhprof-ui">' . xhprof_link($GLOBALS['xhprof_run_id']) . '</div>';
}
if ($output) {
print $output;
}
}
function xhprof_shutdown_xhprof() {
$namespace = variable_get('site_name', ''); // namespace for your application
$xhprof_data = xhprof_disable();
$class = variable_get('xhprof_default_class', 'XHProfRunsFile');
$xhprof_runs = new $class();
return $xhprof_runs->save_run($xhprof_data, $namespace);
}
function xhprof_link($run_id, $type = 'link') {
$namespace = variable_get('site_name', ''); // namespace for your application
$url = base_path() . XHPROF_PATH . '/' . $run_id;
return $type == 'url' ? $url : t('<a href="@xhprof">XHProf output</a>.', array('@xhprof' => $url)) . ' ';
}
/**
* Helper. Make sure expensive module_load_include() does not run needlessly.
*/
function xhprof_include() {
static $included = FALSE;
if (!$included) {
module_load_include('inc', 'xhprof');
module_invoke_all('xhprof_load_classes');
module_load_include('inc', 'xhprof', 'XHProfRunsInterface');
module_load_include('inc', 'xhprof', 'XHProfRunsFile');
$included = TRUE;
}
}
function _xhprof_get_object() {
static $xhprof_object = NULL;
if (empty($xhprof_object)) {
$class = variable_get('xhprof_default_class', 'XHProfRunsFile');
if (class_exists($class)) {
$xhprof_object = new $class();
}
else {
watchdog('xhprof', 'Unable to load default class %class!', array('%class' => $class), WATCHDOG_CRITICAL);
}
}
return $xhprof_object;
}
/**
* List all available XHProf storage backends.
*/
function xhprof_get_classes() {
xhprof_include();
$classes = array('XHProfRunsFile');
drupal_alter('xhprof_classes', $classes);
return $classes;
}
/**
* Display list of saved XHProf runs.
*/
function xhprof_run_list() {
global $pager_page_array, $pager_total, $pager_total_items;
xhprof_include();
$page = isset($_GET['page']) ? $_GET['page'] : '';
$element = 0;
$limit = 50;
$class = variable_get('xhprof_default_class', 'XHProfRunsFile');
$xhprof_runs_impl = new $class();
$pager_page_array = array($page);
$pager_total_items[$element] = $xhprof_runs_impl->getCount();
$pager_total[$element] = ceil($pager_total_items[$element] / $limit);
$pager_start = $page * 50;
$pager_end = $pager_start + 50;
$runs = $xhprof_runs_impl->getRuns(array(), $limit);
// Set the pager info in these globals since we need to fake them for
// theme_pager.
// Table attributes
$attributes = array('id' => 'xhprof-runs-table');
// Table header
$header = array();
$header[] = array('data' => t('View'));
$header[] = array('data' => t('Path'), 'field' => 'path');
$header[] = array('data' => t('Date'), 'field' => 'date', 'sort' => 'desc');
// Table rows
$rows = array();
foreach ($runs as $run) {
$row = array();
$link = XHPROF_PATH . '/' . $run['run_id'];
$row[] = array('data' => l($run['run_id'], $link));
$row[] = array('data' => isset($run['path']) ? $run['path'] : '');
$row[] = array('data' => format_date($run['date'], 'small'));
$rows[] = $row;
}
$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes));
$output .= theme('pager');
return $output;
}
/**
* Get default URL parameters for XHProf.
*/
function xhprof_param_defaults() {
// param name, its type, and default value
return array(
'run' => '',
'wts' => '',
'symbol' => '',
'sort' => 'wt', // wall time
'run1' => '',
'run2' => '',
'source' => 'xhprof',
'all' => 0,
);
}
/**
* Page callback to display a XHProf run.
*/
function xhprof_display_page($run_id, $symbol = NULL) {
drupal_add_css(drupal_get_path('module', 'xhprof') . '/xhprof.css');
return xhprof_display_run(array($run_id), $symbol);
}
/**
* Page callback to display a difference of two XHProf runs.
*/
function xhprof_display_diff_page($run1, $run2, $symbol = NULL) {
drupal_add_css(drupal_get_path('module', 'xhprof') . '/xhprof.css');
return xhprof_display_run(array($run1, $run2), $symbol = NULL);
}
/**
* Display XHProf run report.
*/
function xhprof_display_run($run_ids, $symbol = NULL) {
if (count($run_ids) === 1) {
$_GET['run'] = $run_ids[0];
$run_id = $run_ids[0];
}
else {
$_GET['run1'] = $run_ids[0];
$run1 = $run_ids[0];
$_GET['run2'] = $run_ids[1];
$run2 = $run_ids[1];
}
$source = variable_get('site_name', '');
$_GET['source'] = $source;
$url_params = xhprof_param_defaults();
$required_params = array('sort');
foreach ($url_params as $param => &$value) {
if (isset($_GET[$param])) {
$value = $_GET[$param];
}
elseif (!in_array($param, $required_params)) {
unset($url_params[$param]);
}
}
// Extract params here instead of making them globals. Gross, I know, but
// less gross than this was originally. Should make this less dumb in the
// future.
extract($url_params);
$class = variable_get('xhprof_default_class', 'XHProfRunsFile');
$xhprof_runs_impl = new $class();
$output = '';
if (isset($run_id)) {
// run may be a single run or a comma separate list of runs
// that'll be aggregated. If "wts" (a comma separated list
// of integral weights is specified), the runs will be
// aggregated in that ratio.
$runs_array = explode(",", $run_id);
if (isset($_GET['order'])) {
$sort = xhprof_stat_description($_GET['order'], TRUE);
}
if (count($runs_array) == 1) {
$xhprof_data = $xhprof_runs_impl->get_run($runs_array[0], $source, $description, $sort);
}
else {
if (!empty($wts)) {
$wts_array = explode(",", $wts);
}
else {
$wts_array = NULL;
}
$data = xhprof_aggregate_runs($xhprof_runs_impl, $runs_array, $wts_array, $source, FALSE);
$xhprof_data = $data['raw'];
$description = $data['description'];
}
xhprof_init_metrics($xhprof_data, $symbol, $sort, FALSE);
$output .= xhprof_profiler_report($url_params, $symbol, $sort, $run_id, $description, $xhprof_data);
}
elseif ($run1 && $run2) {
// Diff report for two runs.
$xhprof_data1 = $xhprof_runs_impl->get_run($run1, $source, $description1);
$xhprof_data2 = $xhprof_runs_impl->get_run($run2, $source, $description2);
// Initialize what metrics we'll display based on data in Run2
$output .= xhprof_init_metrics($xhprof_data2, $symbol, $sort, TRUE);
$output .= xhprof_profiler_report($url_params, $symbol, $sort, $run1, $description1, $xhprof_data1, $run2, $description2, $xhprof_data2);
}
else {
$output .= "No XHProf runs specified in the URL.";
}
return $output;
}
/**
* Display a call graph for the specified run
* @param type $id
* @param type $source
*/
function xhprof_display_callgraph($id, $depth = 0.1, $function = '') {
// The source is always the sitename
$source = variable_get('site_name', '');
// by default assume that xhprof_html & xhprof_lib directories
// are at the same level.
$GLOBALS['XHPROF_LIB_ROOT'] = dirname(__FILE__) . '/xhprof_lib';
include_once $GLOBALS['XHPROF_LIB_ROOT'].'/utils/callgraph_utils.php';
include_once $GLOBALS['XHPROF_LIB_ROOT'].'/utils/xhprof_runs.php';
ini_set('max_execution_time', 100);
$xhprof_runs_impl = new XHProfRuns_Default();
xhprof_render_image($xhprof_runs_impl, $id, 'jpg', $depth, $function, $source, FALSE);
}
function xhprof_scandir($dir, $source) {
if (is_dir($dir)) {
$runs = array();
foreach (glob("$dir/*.$source") as $file) {
list($run, $source) = explode('.', basename($file));
$runs[] = array(
'run_id' => $run,
'source' => $source,
'basename' => htmlentities(basename($file)),
'date' => date("Y-m-d H:i:s", filemtime($file)),
);
}
}
return array_reverse($runs);
}
/**
* Theme function to display XHProf run summary.
*/
function theme_xhprof_overall_summary($variables) {
$output = '';
// Extract variables: $totals, $possible_metrics, $metrics, $display_calls;
extract($variables);
$rows = array();
foreach ($metrics as $metric) {
$rows[] = array('<strong>Total ' . xhprof_stat_description($metric) . ':</strong>', number_format($totals[$metric]) . " " . $possible_metrics[$metric][1]);
}
if ($display_calls) {
$rows[] = array("<strong>Number of function xhprof_Calls:</strong>", number_format($totals['ct']));
}
$header = array(array('data' => 'Overall Summary', 'colspan' => 2));
$output .= theme('table', array('header' => $header, 'rows' => $rows));
return $output;
}
/**
* Theme function to display list of XHProf function calls.
*/
function theme_xhprof_run_table($variables) {
// Extract variables: $stats, $totals, $url_params, $title, $flat_data, $limit.
extract($variables);
global $base_path;
// Table attributes
$attributes = array('id' => 'xhprof-run-table');
$output = '';
// Headers.
$header = array();
foreach ($stats as $stat) {
$desc = xhprof_stat_description($stat);
if (array_key_exists($stat, xhprof_sortable_columns($stat))) {
if (isset($_GET['sort']) && $stat == $_GET['sort']) {
$header_desc = l(t($desc), $_GET['q'], array('query' => array('sort' => $stat), t($desc)));
$header[] = array('data' => t($header_desc) . theme('tablesort_indicator', 'desc'));
}
else {
$header_desc = l(t($desc), $_GET['q'], array('query' => array('sort' => $stat), t($desc)));
$header[] = array('data' => t($header_desc));
}
}
else {
$header[] = array('data' => t($desc));
}
}
$path = '/reports/xhprof/' . $run1 . '/symbol/';
// Table rows
$rows = array();
$trail = menu_get_active_trail();
$i = 0;
foreach ($flat_data as $data) {
$row = array(
array('data' => l($data["fn"], $trail[1]["href"] . $path . $data["fn"])),
array('data' => xhprof_print_num($data['ct'], NULL, TRUE), 'class' => 'xhprof_micro'),
array('data' => xhprof_print_pct($data['ct'], $totals['ct'], TRUE), 'class' => 'xhprof_percent'),
array('data' => xhprof_print_num($data['wt'], NULL, TRUE), 'class' => 'xhprof_micro'),
array('data' => xhprof_print_pct($data['wt'], $totals['wt'], TRUE), 'class' => 'xhprof_percent'),
array('data' => xhprof_print_num($data['excl_wt'], NULL, TRUE), 'class' => 'xhprof_micro'),
array('data' => xhprof_print_pct($data['excl_wt'], $totals['wt'], TRUE), 'class' => 'xhprof_percent'),
array('data' => xhprof_print_num($data['cpu'], NULL, TRUE), 'class' => 'xhprof_micro'),
array('data' => xhprof_print_pct($data['cpu'], $totals['cpu'], TRUE), 'class' => 'xhprof_percent'),
array('data' => xhprof_print_num($data['excl_cpu'], NULL, TRUE), 'class' => 'xhprof_micro'),
array('data' => xhprof_print_pct($data['excl_cpu'], $totals['cpu'], TRUE), 'class' => 'xhprof_percent'),
array('data' => xhprof_print_num($data['mu'], NULL, TRUE), 'class' => 'xhprof_micro'),
array('data' => xhprof_print_pct($data['mu'], $totals['mu'], TRUE), 'class' => 'xhprof_percent'),
array('data' => xhprof_print_num($data['excl_mu'], NULL, TRUE), 'class' => 'xhprof_micro'),
array('data' => xhprof_print_pct($data['excl_mu'], $totals['mu'], TRUE), 'class' => 'xhprof_percent'),
array('data' => xhprof_print_num($data['pmu'], NULL, TRUE), 'class' => 'xhprof_micro'),
array('data' => xhprof_print_pct($data['pmu'], $totals['pmu'], TRUE), 'class' => 'xhprof_percent'),
array('data' => xhprof_print_num($data['excl_pmu'], NULL, TRUE), 'class' => 'xhprof_micro'),
array('data' => xhprof_print_pct($data['excl_pmu'], $totals['pmu'], TRUE), 'class' => 'xhprof_percent'),
);
$rows[] = $row;
$i++;
if ($limit && $i >= $limit) break;
}
$size = count($flat_data);
if (!$limit) {
// no limit
$limit = $size;
$display_link = "";
}
else {
$display_link = l(" [ <strong class=bubble>display all </strong>]", $_GET['q'], array('query' => xhprof_array_set($url_params, 'all', 1), 'html' => TRUE));
}
$output .= "<h3 align=center>$title $display_link</h3><br>";
$output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes));
return $output;
}