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
), $this->getTime(), memory_get_usage());
64 function profileOut($functionname) {
65 $memory = memory_get_usage();
66 $time = $this->getTime();
68 global $wgDebugProfiling, $wgDebugFunctionEntry;
70 if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
71 wfDebug(str_repeat(' ', count($this->mWorkStack
) - 1).'Exiting '.$functionname."\n");
74 $bit = array_pop($this->mWorkStack
);
77 wfDebug("Profiling error, !\$bit: $functionname\n");
79 //if ($wgDebugProfiling) {
80 if ($functionname == 'close') {
81 $message = "Profile section ended by close(): {$bit[0]}\n";
83 $this->mStack
[] = array( $message, 0, '0 0', 0, '0 0', 0 );
85 elseif ($bit[0] != $functionname) {
86 $message = "Profiling error: in({$bit[0]}), out($functionname)\n";
88 $this->mStack
[] = array( $message, 0, '0 0', 0, '0 0', 0 );
93 $this->mStack
[] = $bit;
98 while (count($this->mWorkStack
)) {
99 $this->profileOut('close');
103 function getOutput() {
104 global $wgDebugFunctionEntry;
105 $wgDebugFunctionEntry = false;
107 if (!count($this->mStack
)) {
108 return "No profiling output\n";
112 global $wgProfileCallTree;
113 if ($wgProfileCallTree) {
114 return $this->getCallTree();
116 return $this->getFunctionReport();
120 function getCallTree($start = 0) {
121 return implode('', array_map(array (& $this, 'getCallTreeLine'), $this->remapCallTree($this->mStack
)));
124 function remapCallTree($stack) {
125 if (count($stack) < 2) {
129 for ($max = count($stack) - 1; $max > 0;) {
130 /* Find all items under this entry */
131 $level = $stack[$max][1];
133 for ($i = $max -1; $i >= 0; $i --) {
134 if ($stack[$i][1] > $level) {
135 $working[] = $stack[$i];
140 $working = $this->remapCallTree(array_reverse($working));
142 foreach ($working as $item) {
143 array_push($output, $item);
145 array_unshift($output, $stack[$max]);
148 array_unshift($outputs, $output);
151 foreach ($outputs as $output) {
152 foreach ($output as $item) {
159 function getCallTreeLine($entry) {
160 list ($fname, $level, $start, $x, $end) = $entry;
161 $delta = $this->microDelta($start, $end);
162 $space = str_repeat(' ', $level);
164 # The ugly double sprintf is to work around a PHP bug,
165 # which has been fixed in recent releases.
166 return sprintf( "%10s %s %s\n",
167 trim( sprintf( "%7.3f", $delta * 1000.0 ) ),
171 function micro2Float( $micro ) {
172 list( $whole, $fractional ) = explode( ' ', $micro );
173 return (float)$whole +
(float)$fractional;
176 function microDelta( $start, $end ) {
177 return $this->micro2Float( $end ) -
178 $this->micro2Float( $start );
183 #return $this->getUserTime();
186 function getUserTime() {
188 return $ru['ru_utime.tv_sec'].' '.$ru['ru_utime.tv_usec'] / 1e6
;
191 function getFunctionReport() {
193 $nameWidth = $width - 65;
194 $format = "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d (%13.3f -%13.3f) [%d]\n";
195 $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s\n";
196 $prof = "\nProfiling data\n";
197 $prof .= sprintf($titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem');
198 $this->mCollated
= array ();
199 $this->mCalls
= array ();
200 $this->mMemory
= array ();
202 # Estimate profiling overhead
203 $profileCount = count($this->mStack
);
204 wfProfileIn('-overhead-total');
205 for ($i = 0; $i < $profileCount; $i ++
) {
206 wfProfileIn('-overhead-internal');
207 wfProfileOut('-overhead-internal');
209 wfProfileOut('-overhead-total');
211 # First, subtract the overhead!
212 foreach ($this->mStack
as $entry) {
214 $thislevel = $entry[1];
215 $start = explode(' ', $entry[2]);
216 $start = (float) $start[0] +
(float) $start[1];
217 $end = explode(' ', $entry[4]);
218 $end = (float) $end[0] +
(float) $end[1];
219 $elapsed = $end - $start;
220 $memory = $entry[5] - $entry[3];
222 if ($fname == '-overhead-total') {
223 $overheadTotal[] = $elapsed;
224 $overheadMemory[] = $memory;
226 elseif ($fname == '-overhead-internal') {
227 $overheadInternal[] = $elapsed;
230 $overheadTotal = array_sum($overheadTotal) / count($overheadInternal);
231 $overheadMemory = array_sum($overheadMemory) / count($overheadInternal);
232 $overheadInternal = array_sum($overheadInternal) / count($overheadInternal);
235 foreach ($this->mStack
as $index => $entry) {
237 $thislevel = $entry[1];
238 $start = explode(' ', $entry[2]);
239 $start = (float) $start[0] +
(float) $start[1];
240 $end = explode(' ', $entry[4]);
241 $end = (float) $end[0] +
(float) $end[1];
242 $elapsed = $end - $start;
244 $memory = $entry[5] - $entry[3];
245 $subcalls = $this->calltreeCount($this->mStack
, $index);
247 if (!preg_match('/^-overhead/', $fname)) {
248 # Adjust for profiling overhead
249 $elapsed -= $overheadInternal;
250 $elapsed -= ($subcalls * $overheadTotal);
251 $memory -= ($subcalls * $overheadMemory);
254 if (!array_key_exists($fname, $this->mCollated
)) {
255 $this->mCollated
[$fname] = 0;
256 $this->mCalls
[$fname] = 0;
257 $this->mMemory
[$fname] = 0;
258 $this->mMin
[$fname] = 1 << 24;
259 $this->mMax
[$fname] = 0;
260 $this->mOverhead
[$fname] = 0;
263 $this->mCollated
[$fname] +
= $elapsed;
264 $this->mCalls
[$fname]++
;
265 $this->mMemory
[$fname] +
= $memory;
266 $this->mMin
[$fname] = min($this->mMin
[$fname], $elapsed);
267 $this->mMax
[$fname] = max($this->mMax
[$fname], $elapsed);
268 $this->mOverhead
[$fname] +
= $subcalls;
271 $total = @ $this->mCollated
['-total'];
272 $this->mCalls
['-overhead-total'] = $profileCount;
275 asort($this->mCollated
, SORT_NUMERIC
);
276 foreach ($this->mCollated
as $fname => $elapsed) {
277 $calls = $this->mCalls
[$fname];
278 $percent = $total ?
100. * $elapsed / $total : 0;
279 $memory = $this->mMemory
[$fname];
280 $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]);
282 global $wgProfileToDatabase;
283 if ($wgProfileToDatabase) {
284 Profiler
:: logToDB($fname, (float) ($elapsed * 1000), $calls);
287 $prof .= "\nTotal: $total\n\n";
293 * Counts the number of profiled function calls sitting under
294 * the given point in the call graph. Not the most efficient algo.
296 * @param array $stack
301 function calltreeCount(& $stack, $start) {
302 $level = $stack[$start][1];
304 for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) {
313 function logToDB($name, $timeSum, $eventCount) {
314 $fname = 'Profiler::logToDB';
315 $dbw = & wfGetDB(DB_MASTER
);
316 $profiling = $dbw->tableName('profiling');
318 $name = substr($name, 0, 255);
319 $encname = $dbw->strencode($name);
320 $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} "."WHERE pf_name='{$encname}'";
323 $rc = $dbw->affectedRows();
325 $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount, 'pf_time' => $timeSum), $fname, array ('IGNORE'));
327 // When we upgrade to mysql 4.1, the insert+update
328 // can be merged into just a insert with this construct added:
329 // "ON DUPLICATE KEY UPDATE ".
330 // "pf_count=pf_count + VALUES(pf_count), ".
331 // "pf_time=pf_time + VALUES(pf_time)";
336 $wgProfiler = new Profiler();
337 $wgProfiler->profileIn('-total');