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());
64 function profileOut($functionname) {
65 $memory = memory_get_usage();
67 global $wgDebugProfiling, $wgDebugFunctionEntry;
69 if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
70 wfDebug(str_repeat(' ', count($this->mWorkStack
)).'Exiting '.$functionname."\n");
73 $bit = array_pop($this->mWorkStack
);
76 wfDebug("Profiling error, !\$bit: $functionname\n");
78 if ($wgDebugProfiling) {
79 if ($functionname == 'close') {
80 wfDebug("Profile section ended by close(): {$bit[0]}\n");
82 elseif ($bit[0] != $functionname) {
83 wfDebug("Profiling error: in({$bit[0]}), out($functionname)\n");
88 $this->mStack
[] = $bit;
93 while (count($this->mWorkStack
)) {
94 $this->profileOut('close');
98 function getOutput() {
99 global $wgDebugFunctionEntry;
100 $wgDebugFunctionEntry = false;
102 if (!count($this->mStack
)) {
103 return "No profiling output\n";
107 global $wgProfileCallTree;
108 if ($wgProfileCallTree) {
109 return $this->getCallTree();
111 return $this->getFunctionReport();
115 function getCallTree($start = 0) {
116 return implode('', array_map(array (& $this, 'getCallTreeLine'), $this->remapCallTree($this->mStack
)));
119 function remapCallTree($stack) {
120 if (count($stack) < 2) {
124 for ($max = count($stack) - 1; $max > 0;) {
125 /* Find all items under this entry */
126 $level = $stack[$max][1];
128 for ($i = $max -1; $i >= 0; $i --) {
129 if ($stack[$i][1] > $level) {
130 $working[] = $stack[$i];
135 $working = $this->remapCallTree(array_reverse($working));
137 foreach ($working as $item) {
138 array_push($output, $item);
140 array_unshift($output, $stack[$max]);
143 array_unshift($outputs, $output);
146 foreach ($outputs as $output) {
147 foreach ($output as $item) {
154 function getCallTreeLine($entry) {
155 list ($fname, $level, $start, $x, $end) = $entry;
156 $delta = $this->microDelta($start, $end);
157 $space = str_repeat(' ', $level);
159 # The ugly double sprintf is to work around a PHP bug,
160 # which has been fixed in recent releases.
161 return sprintf("%10s %s %s\n", trim(sprintf("%7.3f", $delta * 1000.0)), $space, $fname);
164 function micro2Float($micro) {
165 list ($whole, $fractional) = explode(' ', $micro);
166 return (float) $whole +
(float) $fractional;
169 function microDelta($start, $end) {
170 return $this->micro2Float($end) - $this->micro2Float($start);
173 function getFunctionReport() {
175 $format = "%-". ($width -34)."s %6d %6.3f %6.3f %7.3f%% %6d (%6.3f-%6.3f) [%d]\n";
176 $titleFormat = "%-". ($width -34)."s %9s %9s %9s %9s %6s\n";
177 $prof = "\nProfiling data\n";
178 $prof .= sprintf($titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem');
179 $this->mCollated
= array ();
180 $this->mCalls
= array ();
181 $this->mMemory
= array ();
183 # Estimate profiling overhead
184 $profileCount = count($this->mStack
);
185 wfProfileIn('-overhead-total');
186 for ($i = 0; $i < $profileCount; $i ++
) {
187 wfProfileIn('-overhead-internal');
188 wfProfileOut('-overhead-internal');
190 wfProfileOut('-overhead-total');
192 # First, subtract the overhead!
193 foreach ($this->mStack
as $entry) {
195 $thislevel = $entry[1];
196 $start = explode(' ', $entry[2]);
197 $start = (float) $start[0] +
(float) $start[1];
198 $end = explode(' ', $entry[4]);
199 $end = (float) $end[0] +
(float) $end[1];
200 $elapsed = $end - $start;
201 $memory = $entry[5] - $entry[3];
203 if ($fname == '-overhead-total') {
204 $overheadTotal[] = $elapsed;
205 $overheadMemory[] = $memory;
207 elseif ($fname == '-overhead-internal') {
208 $overheadInternal[] = $elapsed;
211 $overheadTotal = array_sum($overheadTotal) / count($overheadInternal);
212 $overheadMemory = array_sum($overheadMemory) / count($overheadInternal);
213 $overheadInternal = array_sum($overheadInternal) / count($overheadInternal);
216 foreach ($this->mStack
as $index => $entry) {
218 $thislevel = $entry[1];
219 $start = explode(' ', $entry[2]);
220 $start = (float) $start[0] +
(float) $start[1];
221 $end = explode(' ', $entry[4]);
222 $end = (float) $end[0] +
(float) $end[1];
223 $elapsed = $end - $start;
225 $memory = $entry[5] - $entry[3];
226 $subcalls = $this->calltreeCount($this->mStack
, $index);
228 if (!preg_match('/^-overhead/', $fname)) {
229 # Adjust for profiling overhead
230 $elapsed -= $overheadInternal;
231 $elapsed -= ($subcalls * $overheadTotal);
232 $memory -= ($subcalls * $overheadMemory);
235 if (!array_key_exists($fname, $this->mCollated
)) {
236 $this->mCollated
[$fname] = 0;
237 $this->mCalls
[$fname] = 0;
238 $this->mMemory
[$fname] = 0;
239 $this->mMin
[$fname] = 1 << 24;
240 $this->mMax
[$fname] = 0;
241 $this->mOverhead
[$fname] = 0;
244 $this->mCollated
[$fname] +
= $elapsed;
245 $this->mCalls
[$fname]++
;
246 $this->mMemory
[$fname] +
= $memory;
247 $this->mMin
[$fname] = min($this->mMin
[$fname], $elapsed);
248 $this->mMax
[$fname] = max($this->mMax
[$fname], $elapsed);
249 $this->mOverhead
[$fname] +
= $subcalls;
252 $total = @ $this->mCollated
['-total'];
253 $this->mCalls
['-overhead-total'] = $profileCount;
256 asort($this->mCollated
, SORT_NUMERIC
);
257 foreach ($this->mCollated
as $fname => $elapsed) {
258 $calls = $this->mCalls
[$fname];
259 $percent = $total ?
100. * $elapsed / $total : 0;
260 $memory = $this->mMemory
[$fname];
261 $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]);
263 global $wgProfileToDatabase;
264 if ($wgProfileToDatabase) {
265 Profiler
:: logToDB($fname, (float) ($elapsed * 1000), $calls);
268 $prof .= "\nTotal: $total\n\n";
274 * Counts the number of profiled function calls sitting under
275 * the given point in the call graph. Not the most efficient algo.
277 * @param array $stack
282 function calltreeCount(& $stack, $start) {
283 $level = $stack[$start][1];
285 for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) {
294 function logToDB($name, $timeSum, $eventCount) {
295 $fname = 'Profiler::logToDB';
296 $dbw = & wfGetDB(DB_MASTER
);
297 $profiling = $dbw->tableName('profiling');
299 $name = substr($name, 0, 255);
300 $encname = $dbw->strencode($name);
301 $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} "."WHERE pf_name='{$encname}'";
304 $rc = $dbw->affectedRows();
306 $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount, 'pf_time' => $timeSum), $fname, array ('IGNORE'));
308 // When we upgrade to mysql 4.1, the insert+update
309 // can be merged into just a insert with this construct added:
310 // "ON DUPLICATE KEY UPDATE ".
311 // "pf_count=pf_count + VALUES(pf_count), ".
312 // "pf_time=pf_time + VALUES(pf_time)";
317 $wgProfiler = new Profiler();
318 $wgProfiler->profileIn('-total');