-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxhprof.module
294 lines (245 loc) · 7.85 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
<?php
// $Id$
/**
* @file
*/
/**
* XHProf calls this both 'source' and 'run_type'.
* It's basically a way to namespace XHProf runs.
*/
define('XHPROF_SOURCE', 'drupal_xhprof');
/**
* Set to true when XHProf profiling has started.
*/
$xhprof_profiling_started = false;
/**
* Implementation of hook_menu().
*/
function xhprof_menu() {
$items = array();
$items['admin/settings/xhprof'] = array(
'title' => 'XHProf Settings',
'description' => 'XHProf Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('xhprof_admin_settings'),
'access arguments' => array('administer XHProf'),
'type' => MENU_NORMAL_ITEM
);
$items['admin/xhprof/runs'] = array(
'title' => 'XHProf Runs',
'description' => 'XHProf Runs',
'page callback' => 'xhprof_runs_view',
'access arguments' => array('view XHProf data'),
'type' => MENU_NORMAL_ITEM
);
return $items;
}
/**
* Implementation of hook_perm().
*/
function xhprof_perm() {
return array('administer XHProf', 'view XHProf data');
}
/**
* Implementation of hook_boot().
*/
function xhprof_boot() {
if (!extension_loaded('xhprof') or !function_exists("xhprof_enable")) {
return;
}
// So we don't waste time checking again on xhprof_exit()
global $xhprof_profiling_started;
if (xhprof_profiling_enabled()) {
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
$xhprof_profiling_started = true;
}
}
/**
* Implementation of hook_exit().
*/
function xhprof_exit() {
global $xhprof_profiling_started;
if (!$xhprof_profiling_started) {
return;
}
// Stop profiling and retrieve profile data
$xhprof_data = xhprof_disable();
// Include xhprof utils
$mod_path = drupal_get_path('module', 'xhprof');
include_once $mod_path . '/xhprof_lib/utils/xhprof_lib.php';
include_once $mod_path . '/xhprof_lib/utils/xhprof_runs.php';
// Generate xhprof run file
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, XHPROF_SOURCE);
// Calculate run totals
$total_calls = array_reduce($xhprof_data, 'xhprof_sum_calls');
$total_memory = array_reduce($xhprof_data, 'xhprof_sum_memory');
$total_cpu = array_reduce($xhprof_data, 'xhprof_sum_cpu');
// Record this run to the database
$query = "
INSERT INTO {xhprof_runs}
(run_id, url, data, time, calls, memory, cpu, created)
VALUES
('%s', '%s', '%s', %d, %d, %d, %d, %d)";
$args = array(
$run_id,
$_GET['q'],
serialize($xhprof_data),
$xhprof_data['main()']['wt'],
$total_calls,
$total_memory,
$total_cpu,
time()
);
db_query($query, $args);
}
/**
* XHProf admin settings
*/
function xhprof_admin_settings() {
$form = array();
$form['xhprof_include_paths'] = array(
'#type' => 'textarea',
'#title' => t('Paths to include in profiling'),
'#default_value' => variable_get('xhprof_include_paths', NULL),
'#description' => t('XHProf will profile this list of paths.'),
);
$form['xhprof_exclude_paths'] = array(
'#type' => 'textarea',
'#title' => t('Paths to exclude in profiling'),
'#default_value' => variable_get('xhprof_exclude_paths', NULL),
'#description' => t('XHProf will NOT profile this list of paths.'),
);
return system_settings_form($form);
}
/**
* XHProf runs table form
*/
function xhprof_runs_view_form() {
$form = array();
$form['clear'] = array(
'#type' => 'submit',
'#value' => t('Clear All Runs'),
'#id' => 'xhprof-clear-runs'
);
$form['refresh'] = array(
'#type' => 'submit',
'#value' => t('Refresh'),
'#id' => 'xhprof-refresh-runs'
);
return $form;
}
/**
* XHProf runs table form submit handler
*/
function xhprof_runs_view_form_submit($form, &$form_state) {
$op = $form_state['clicked_button']['#value'];
if ($op == t('Clear All Runs')) {
if (!user_access('administer XHProf')) {
drupal_set_message(t("You do not have permission to clear XHProf run data."), 'warning');
return;
}
$result = db_query("SELECT * FROM {xhprof_runs}");
while ($run = db_fetch_object($result)) {
xhprof_delete_run_file($run->run_id, XHPROF_SOURCE);
db_query("DELETE FROM {xhprof_runs} WHERE run_id = '%s'", $run->run_id);
}
drupal_set_message('Cleared all XHProf runs.');
}
}
/**
* XHProf runs table
*/
function xhprof_runs_view() {
drupal_add_js(drupal_get_path("module", "xhprof") . "/xhprof.js");
drupal_add_css(drupal_get_path("module", "xhprof") . "/xhprof.css");
// Table attributes
$attributes = array('id' => 'xhprof-runs-table');
// Table header
$header = array();
$header[] = array('data' => 'View');
$header[] = array('data' => 'Url', 'field' => 'url');
$header[] = array('data' => 'Time', 'field' => 'time');
$header[] = array('data' => 'Function Calls', 'field' => 'calls');
$header[] = array('data' => 'Memory', 'field' => 'memory');
$header[] = array('data' => 'CPU Cycles', 'field' => 'cpu');
$header[] = array('data' => 'Created', 'field' => 'created', 'sort' => 'desc');
$query = 'SELECT run_id, url, time, calls, memory, cpu, created FROM {xhprof_runs} '.tablesort_sql($header);
$result = pager_query($query, 25);
// Table rows
$rows = array();
while ($data = db_fetch_object($result)) {
$row = array();
$row[] = array('data' => l(t('details'), xhprof_url('details', $data->run_id)).' '.l(t('graph'), xhprof_url('graph', $data->run_id)));
$row[] = array('data' => l($data->url, $data->url));
$row[] = array('data' => number_format($data->time / 1000) . ' ms', 'class' => xhprof_class_from_time($data->time));
$row[] = array('data' => number_format($data->calls));
$row[] = array('data' => number_format($data->memory / 1024) . ' KB');
$row[] = array('data' => number_format($data->cpu));
$row[] = array('data' => format_date($data->created, 'small'));
$rows[] = $row;
}
$content = array();
$content[] = drupal_get_form('xhprof_runs_view_form');
$content[] = theme_table($header, $rows, $attributes);
$content[] = theme_pager(NULL, 25);
return implode("\n", $content);
}
/**
* Determines whether or not to profile the current request.
*/
function xhprof_profiling_enabled() {
require_once 'includes/path.inc';
if (isset($_GET['XHPROF_ENABLED'])) {
return true;
}
if (isset($_GET['XHPROF_DISABLED'])) {
return false;
}
drupal_init_language();
drupal_init_path();
$path = drupal_get_path_alias($_GET['q']);
return (bool) (drupal_match_path($path, variable_get("xhprof_include_paths", ''))
&& !drupal_match_path($path, variable_get("xhprof_exclude_paths", '')));
}
/**
* Calculate totals from an xhprof run.
*/
function xhprof_sum_calls($total, $data) { return $total + $data['ct']; }
function xhprof_sum_memory($total, $data) { return $total + $data['mu']; }
function xhprof_sum_cpu($total, $data) { return $total + $data['cpu']; }
/**
* Return a CSS class indicating the relative speed of $time.
*/
function xhprof_class_from_time($time) {
$classes = array(
'xhprof-green' => 300000, // < 300ms
'xhprof-yellow' => 1000000, // < 1s
'xhprof-red' => PHP_MAX_INT // > 1s
);
foreach ($classes as $class => $threshold) {
if ($time <= $threshold) {
return $class;
}
}
}
/**
* Delete a run file.
*/
function xhprof_delete_run_file($id, $source) {
$dir = ini_get("xhprof.output_dir");
$file = "$dir/$id.$source";
return (bool) (is_dir($dir) and is_file($file) and unlink($file));
}
function xhprof_url($type, $run_id) {
global $base_url;
$module_path = drupal_get_path('module', 'xhprof');
switch($type) {
case 'details':
return "$base_url/$module_path/xhprof_html/index.php?run=$run_id&source=".XHPROF_SOURCE;
case 'graph':
return "$base_url/$module_path/xhprof_html/callgraph.php?run=$run_id&source=".XHPROF_SOURCE;
default:
return '';
}
}