3 * This file is only included if profiling is enabled
8 * @param $functioname name of the function we will profile
10 function wfProfileIn($functionname) {
12 $wgProfiler->profileIn($functionname);
16 * @param $functioname name of the function we have profiled
18 function wfProfileOut($functionname = 'missing') {
20 $wgProfiler->profileOut($functionname);
23 function wfGetProfilingOutput($start, $elapsed) {
25 return $wgProfiler->getOutput($start, $elapsed);
28 function wfProfileClose() {
33 if (!function_exists('memory_get_usage')) {
34 # Old PHP or --enable-memory-limit not compiled in
35 function memory_get_usage() {
45 var $mStack = array (), $mWorkStack = array (), $mCollated = array ();
46 var $mCalls = array (), $mTotals = array ();
50 $this->mProfileStack = array();
51 $this->mWorkStack = array();
52 $this->mCollated = array();
56 function profileIn($functionname) {
57 global $wgDebugFunctionEntry;
58 if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
59 wfDebug(str_repeat(' ', count($this->mWorkStack
)).'Entering '.$functionname."\n");
61 $this->mWorkStack
[] = array($functionname, count($this->mWorkStack
), microtime(), memory_get_usage());
62 #$this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), $this->getUserTime(), memory_get_usage());
65 function profileOut($functionname) {
66 $memory = memory_get_usage();
68 #$time = $this->getUserTime();
70 global $wgDebugProfiling, $wgDebugFunctionEntry;
72 if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
73 wfDebug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
76 $bit = array_pop($this->mWorkStack
);
79 wfDebug("Profiling error, !\$bit: $functionname\n");
81 if ($wgDebugProfiling) {
82 if ($functionname == 'close') {
83 wfDebug("Profile section ended by close(): {$bit[0]}\n");
85 elseif ($bit[0] != $functionname) {
86 wfDebug("Profiling error: in({$bit[0]}), out($functionname)\n");
91 $this->mStack
[] = $bit;
96 while (count($this->mWorkStack
)) {
97 $this->profileOut('close');
101 function getOutput() {
102 global $wgDebugFunctionEntry;
103 $wgDebugFunctionEntry = false;
105 if (!count($this->mStack
)) {
106 return "No profiling output\n";
110 global $wgProfileCallTree;
111 if ($wgProfileCallTree) {
112 return $this->getCallTree();
114 return $this->getFunctionReport();
118 function getCallTree($start = 0) {
119 return implode('', array_map(array (& $this, 'getCallTreeLine'), $this->remapCallTree($this->mStack
)));
122 function remapCallTree($stack) {
123 if (count($stack) < 2) {
127 for ($max = count($stack) - 1; $max > 0;) {
128 /* Find all items under this entry */
129 $level = $stack[$max][1];
131 for ($i = $max -1; $i >= 0; $i --) {
132 if ($stack[$i][1] > $level) {
133 $working[] = $stack[$i];
138 $working = $this->remapCallTree(array_reverse($working));
140 foreach ($working as $item) {
141 array_push($output, $item);
143 array_unshift($output, $stack[$max]);
146 array_unshift($outputs, $output);
149 foreach ($outputs as $output) {
150 foreach ($output as $item) {
157 function getCallTreeLine($entry) {
158 list ($fname, $level, $start, $x, $end) = $entry;
159 $delta = $this->microDelta($start, $end);
160 $space = str_repeat(' ', $level);
162 # The ugly double sprintf is to work around a PHP bug,
163 # which has been fixed in recent releases.
164 <<<<<<< Profiling
.php
165 return sprintf("%10s %s %s\n", trim(sprintf("%7.3f", $delta * 1000.0)), $space, $fname);
168 function micro2Float($micro) {
169 list ($whole, $fractional) = explode(' ', $micro);
170 return (float) $whole +
(float) $fractional;
173 function microDelta($start, $end) {
174 return $this->micro2Float($end) - $this->micro2Float($start);
177 function getFunctionReport() {
179 $format = "%-". ($width -34)."s %6d %6.3f %6.3f %7.3f%% %6d (%6.3f-%6.3f) [%d]\n";
180 $titleFormat = "%-". ($width -34)."s %9s %9s %9s %9s %6s\n";
182 return sprintf( "%10s %s %s\n",
183 trim( sprintf( "%7.3f", $delta * 1000.0 ) ),
187 function micro2Float( $micro ) {
188 list( $whole, $fractional ) = explode( ' ', $micro );
189 return (float)$whole +
(float)$fractional;
192 function microDelta( $start, $end ) {
193 return $this->micro2Float( $end ) -
194 $this->micro2Float( $start );
197 function getUserTime() {
199 return $ru['ru_utime.tv_sec'].' '.$ru['ru_utime.tv_usec'] / 1e6
;
202 function getFunctionReport() {
204 $nameWidth = $width - 65;
205 $format = "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d (%13.3f -%13.3f) [%d]\n";
206 $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s %14s %14s %9s\n";
208 $prof = "\nProfiling data\n";
209 $prof .= sprintf($titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem');
210 $this->mCollated
= array ();
211 $this->mCalls
= array ();
212 $this->mMemory
= array ();
214 # Estimate profiling overhead
215 $profileCount = count($this->mStack
);
216 wfProfileIn('-overhead-total');
217 for ($i = 0; $i < $profileCount; $i ++
) {
218 wfProfileIn('-overhead-internal');
219 wfProfileOut('-overhead-internal');
221 wfProfileOut('-overhead-total');
223 # First, subtract the overhead!
224 foreach ($this->mStack
as $entry) {
226 $thislevel = $entry[1];
227 $start = explode(' ', $entry[2]);
228 $start = (float) $start[0] +
(float) $start[1];
229 $end = explode(' ', $entry[4]);
230 $end = (float) $end[0] +
(float) $end[1];
231 $elapsed = $end - $start;
232 $memory = $entry[5] - $entry[3];
234 if ($fname == '-overhead-total') {
235 $overheadTotal[] = $elapsed;
236 $overheadMemory[] = $memory;
238 elseif ($fname == '-overhead-internal') {
239 $overheadInternal[] = $elapsed;
242 $overheadTotal = array_sum($overheadTotal) / count($overheadInternal);
243 $overheadMemory = array_sum($overheadMemory) / count($overheadInternal);
244 $overheadInternal = array_sum($overheadInternal) / count($overheadInternal);
247 foreach ($this->mStack
as $index => $entry) {
249 $thislevel = $entry[1];
250 $start = explode(' ', $entry[2]);
251 $start = (float) $start[0] +
(float) $start[1];
252 $end = explode(' ', $entry[4]);
253 $end = (float) $end[0] +
(float) $end[1];
254 $elapsed = $end - $start;
256 $memory = $entry[5] - $entry[3];
257 $subcalls = $this->calltreeCount($this->mStack
, $index);
259 if (!preg_match('/^-overhead/', $fname)) {
260 # Adjust for profiling overhead
261 $elapsed -= $overheadInternal;
262 $elapsed -= ($subcalls * $overheadTotal);
263 $memory -= ($subcalls * $overheadMemory);
266 if (!array_key_exists($fname, $this->mCollated
)) {
267 $this->mCollated
[$fname] = 0;
268 $this->mCalls
[$fname] = 0;
269 $this->mMemory
[$fname] = 0;
270 $this->mMin
[$fname] = 1 << 24;
271 $this->mMax
[$fname] = 0;
272 $this->mOverhead
[$fname] = 0;
275 $this->mCollated
[$fname] +
= $elapsed;
276 $this->mCalls
[$fname]++
;
277 $this->mMemory
[$fname] +
= $memory;
278 $this->mMin
[$fname] = min($this->mMin
[$fname], $elapsed);
279 $this->mMax
[$fname] = max($this->mMax
[$fname], $elapsed);
280 $this->mOverhead
[$fname] +
= $subcalls;
283 $total = @ $this->mCollated
['-total'];
284 $this->mCalls
['-overhead-total'] = $profileCount;
287 asort($this->mCollated
, SORT_NUMERIC
);
288 foreach ($this->mCollated
as $fname => $elapsed) {
289 $calls = $this->mCalls
[$fname];
290 $percent = $total ?
100. * $elapsed / $total : 0;
291 $memory = $this->mMemory
[$fname];
292 $prof .= sprintf($format, $fname, $calls, (float) ($elapsed * 1000), (float) ($elapsed * 1000) / $calls, $percent, $memory, ($this->mMin
[$fname] * 1000.0), ($this->mMax
[$fname] * 1000.0), $this->mOverhead
[$fname]);
294 global $wgProfileToDatabase;
295 if ($wgProfileToDatabase) {
296 Profiler
:: logToDB($fname, (float) ($elapsed * 1000), $calls);
299 $prof .= "\nTotal: $total\n\n";
305 * Counts the number of profiled function calls sitting under
306 * the given point in the call graph. Not the most efficient algo.
308 * @param array $stack
313 function calltreeCount(& $stack, $start) {
314 $level = $stack[$start][1];
316 for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) {
325 function logToDB($name, $timeSum, $eventCount) {
326 $fname = 'Profiler::logToDB';
327 $dbw = & wfGetDB(DB_MASTER
);
328 $profiling = $dbw->tableName('profiling');
330 $name = substr($name, 0, 255);
331 $encname = $dbw->strencode($name);
332 $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} "."WHERE pf_name='{$encname}'";
335 $rc = $dbw->affectedRows();
337 $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount, 'pf_time' => $timeSum), $fname, array ('IGNORE'));
339 // When we upgrade to mysql 4.1, the insert+update
340 // can be merged into just a insert with this construct added:
341 // "ON DUPLICATE KEY UPDATE ".
342 // "pf_count=pf_count + VALUES(pf_count), ".
343 // "pf_time=pf_time + VALUES(pf_time)";
348 $wgProfiler = new Profiler();
349 $wgProfiler->profileIn('-total');