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 return sprintf( "%10s %s %s\n",
165 trim( sprintf( "%7.3f", $delta * 1000.0 ) ),
169 function micro2Float( $micro ) {
170 list( $whole, $fractional ) = explode( ' ', $micro );
171 return (float)$whole +
(float)$fractional;
174 function microDelta( $start, $end ) {
175 return $this->micro2Float( $end ) -
176 $this->micro2Float( $start );
179 function getUserTime() {
181 return $ru['ru_utime.tv_sec'].' '.$ru['ru_utime.tv_usec'] / 1e6
;
184 function getFunctionReport() {
186 $nameWidth = $width - 65;
187 $format = "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d (%13.3f -%13.3f) [%d]\n";
188 $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s\n";
189 $prof = "\nProfiling data\n";
190 $prof .= sprintf($titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem');
191 $this->mCollated
= array ();
192 $this->mCalls
= array ();
193 $this->mMemory
= array ();
195 # Estimate profiling overhead
196 $profileCount = count($this->mStack
);
197 wfProfileIn('-overhead-total');
198 for ($i = 0; $i < $profileCount; $i ++
) {
199 wfProfileIn('-overhead-internal');
200 wfProfileOut('-overhead-internal');
202 wfProfileOut('-overhead-total');
204 # First, subtract the overhead!
205 foreach ($this->mStack
as $entry) {
207 $thislevel = $entry[1];
208 $start = explode(' ', $entry[2]);
209 $start = (float) $start[0] +
(float) $start[1];
210 $end = explode(' ', $entry[4]);
211 $end = (float) $end[0] +
(float) $end[1];
212 $elapsed = $end - $start;
213 $memory = $entry[5] - $entry[3];
215 if ($fname == '-overhead-total') {
216 $overheadTotal[] = $elapsed;
217 $overheadMemory[] = $memory;
219 elseif ($fname == '-overhead-internal') {
220 $overheadInternal[] = $elapsed;
223 $overheadTotal = array_sum($overheadTotal) / count($overheadInternal);
224 $overheadMemory = array_sum($overheadMemory) / count($overheadInternal);
225 $overheadInternal = array_sum($overheadInternal) / count($overheadInternal);
228 foreach ($this->mStack
as $index => $entry) {
230 $thislevel = $entry[1];
231 $start = explode(' ', $entry[2]);
232 $start = (float) $start[0] +
(float) $start[1];
233 $end = explode(' ', $entry[4]);
234 $end = (float) $end[0] +
(float) $end[1];
235 $elapsed = $end - $start;
237 $memory = $entry[5] - $entry[3];
238 $subcalls = $this->calltreeCount($this->mStack
, $index);
240 if (!preg_match('/^-overhead/', $fname)) {
241 # Adjust for profiling overhead
242 $elapsed -= $overheadInternal;
243 $elapsed -= ($subcalls * $overheadTotal);
244 $memory -= ($subcalls * $overheadMemory);
247 if (!array_key_exists($fname, $this->mCollated
)) {
248 $this->mCollated
[$fname] = 0;
249 $this->mCalls
[$fname] = 0;
250 $this->mMemory
[$fname] = 0;
251 $this->mMin
[$fname] = 1 << 24;
252 $this->mMax
[$fname] = 0;
253 $this->mOverhead
[$fname] = 0;
256 $this->mCollated
[$fname] +
= $elapsed;
257 $this->mCalls
[$fname]++
;
258 $this->mMemory
[$fname] +
= $memory;
259 $this->mMin
[$fname] = min($this->mMin
[$fname], $elapsed);
260 $this->mMax
[$fname] = max($this->mMax
[$fname], $elapsed);
261 $this->mOverhead
[$fname] +
= $subcalls;
264 $total = @ $this->mCollated
['-total'];
265 $this->mCalls
['-overhead-total'] = $profileCount;
268 asort($this->mCollated
, SORT_NUMERIC
);
269 foreach ($this->mCollated
as $fname => $elapsed) {
270 $calls = $this->mCalls
[$fname];
271 $percent = $total ?
100. * $elapsed / $total : 0;
272 $memory = $this->mMemory
[$fname];
273 $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]);
275 global $wgProfileToDatabase;
276 if ($wgProfileToDatabase) {
277 Profiler
:: logToDB($fname, (float) ($elapsed * 1000), $calls);
280 $prof .= "\nTotal: $total\n\n";
286 * Counts the number of profiled function calls sitting under
287 * the given point in the call graph. Not the most efficient algo.
289 * @param array $stack
294 function calltreeCount(& $stack, $start) {
295 $level = $stack[$start][1];
297 for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) {
306 function logToDB($name, $timeSum, $eventCount) {
307 $fname = 'Profiler::logToDB';
308 $dbw = & wfGetDB(DB_MASTER
);
309 $profiling = $dbw->tableName('profiling');
311 $name = substr($name, 0, 255);
312 $encname = $dbw->strencode($name);
313 $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} "."WHERE pf_name='{$encname}'";
316 $rc = $dbw->affectedRows();
318 $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount, 'pf_time' => $timeSum), $fname, array ('IGNORE'));
320 // When we upgrade to mysql 4.1, the insert+update
321 // can be merged into just a insert with this construct added:
322 // "ON DUPLICATE KEY UPDATE ".
323 // "pf_count=pf_count + VALUES(pf_count), ".
324 // "pf_time=pf_time + VALUES(pf_time)";
329 $wgProfiler = new Profiler();
330 $wgProfiler->profileIn('-total');