Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / profiler / SectionProfiler.php
blob245022df02e11a253b5ef5ad20c2a7a036017dbe
1 <?php
2 /**
3 * Arbitrary section name based PHP profiling.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Profiler
22 * @author Aaron Schulz
25 /**
26 * Custom PHP profiler for parser/DB type section names that xhprof/xdebug can't handle
28 * @since 1.25
30 class SectionProfiler {
31 /** @var array Map of (mem,real,cpu) */
32 protected $start;
33 /** @var array Map of (mem,real,cpu) */
34 protected $end;
35 /** @var array List of resolved profile calls with start/end data */
36 protected $stack = array();
37 /** @var array Queue of open profile calls with start data */
38 protected $workStack = array();
40 /** @var array Map of (function name => aggregate data array) */
41 protected $collated = array();
42 /** @var bool */
43 protected $collateDone = false;
45 /** @var bool Whether to collect the full stack trace or just aggregates */
46 protected $collateOnly = true;
47 /** @var array Cache of a standard broken collation entry */
48 protected $errorEntry;
49 /** @var callable Cache of a profile out callback */
50 protected $profileOutCallback;
52 /**
53 * @param array $params
55 public function __construct( array $params = array() ) {
56 $this->errorEntry = $this->getErrorEntry();
57 $this->collateOnly = empty( $params['trace'] );
58 $this->profileOutCallback = function ( $profiler, $section ) {
59 $profiler->profileOutInternal( $section );
63 /**
64 * @param string $section
65 * @return ScopedCallback
67 public function scopedProfileIn( $section ) {
68 $this->profileInInternal( $section );
70 return new SectionProfileCallback( $this, $section );
73 /**
74 * @param ScopedCallback $section
76 public function scopedProfileOut( ScopedCallback &$section ) {
77 $section = null;
80 /**
81 * Get the aggregated inclusive profiling data for each method
83 * The percent time for each time is based on the current "total" time
84 * used is based on all methods so far. This method can therefore be
85 * called several times in between several profiling calls without the
86 * delays in usage of the profiler skewing the results. A "-total" entry
87 * is always included in the results.
89 * @return array List of method entries arrays, each having:
90 * - name : method name
91 * - calls : the number of invoking calls
92 * - real : real time ellapsed (ms)
93 * - %real : percent real time
94 * - cpu : real time ellapsed (ms)
95 * - %cpu : percent real time
96 * - memory : memory used (bytes)
97 * - %memory : percent memory used
98 * - min_real : min real time in a call (ms)
99 * - max_real : max real time in a call (ms)
101 public function getFunctionStats() {
102 $this->collateData();
104 $totalCpu = max( $this->end['cpu'] - $this->start['cpu'], 0 );
105 $totalReal = max( $this->end['real'] - $this->start['real'], 0 );
106 $totalMem = max( $this->end['memory'] - $this->start['memory'], 0 );
108 $profile = array();
109 foreach ( $this->collated as $fname => $data ) {
110 $profile[] = array(
111 'name' => $fname,
112 'calls' => $data['count'],
113 'real' => $data['real'] * 1000,
114 '%real' => $totalReal ? 100 * $data['real'] / $totalReal : 0,
115 'cpu' => $data['cpu'] * 1000,
116 '%cpu' => $totalCpu ? 100 * $data['cpu'] / $totalCpu : 0,
117 'memory' => $data['memory'],
118 '%memory' => $totalMem ? 100 * $data['memory'] / $totalMem : 0,
119 'min_real' => 1000 * $data['min_real'],
120 'max_real' => 1000 * $data['max_real']
124 $profile[] = array(
125 'name' => '-total',
126 'calls' => 1,
127 'real' => 1000 * $totalReal,
128 '%real' => 100,
129 'cpu' => 1000 * $totalCpu,
130 '%cpu' => 100,
131 'memory' => $totalMem,
132 '%memory' => 100,
133 'min_real' => 1000 * $totalReal,
134 'max_real' => 1000 * $totalReal
137 return $profile;
141 * Clear all of the profiling data for another run
143 public function reset() {
144 $this->start = null;
145 $this->end = null;
146 $this->stack = array();
147 $this->workStack = array();
148 $this->collated = array();
149 $this->collateDone = false;
153 * @return array Initial collation entry
155 protected function getZeroEntry() {
156 return array(
157 'cpu' => 0.0,
158 'real' => 0.0,
159 'memory' => 0,
160 'count' => 0,
161 'min_real' => 0.0,
162 'max_real' => 0.0
167 * @return array Initial collation entry for errors
169 protected function getErrorEntry() {
170 $entry = $this->getZeroEntry();
171 $entry['count'] = 1;
172 return $entry;
176 * Update the collation entry for a given method name
178 * @param string $name
179 * @param float $elapsedCpu
180 * @param float $elapsedReal
181 * @param int $memChange
183 protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) {
184 $entry =& $this->collated[$name];
185 if ( !is_array( $entry ) ) {
186 $entry = $this->getZeroEntry();
187 $this->collated[$name] =& $entry;
189 $entry['cpu'] += $elapsedCpu;
190 $entry['real'] += $elapsedReal;
191 $entry['memory'] += $memChange > 0 ? $memChange : 0;
192 $entry['count']++;
193 $entry['min_real'] = min( $entry['min_real'], $elapsedReal );
194 $entry['max_real'] = max( $entry['max_real'], $elapsedReal );
198 * This method should not be called outside SectionProfiler
200 * @param string $functionname
202 public function profileInInternal( $functionname ) {
203 // Once the data is collated for reports, any future calls
204 // should clear the collation cache so the next report will
205 // reflect them. This matters when trace mode is used.
206 $this->collateDone = false;
208 $cpu = $this->getTime( 'cpu' );
209 $real = $this->getTime( 'wall' );
210 $memory = memory_get_usage();
212 if ( $this->start === null ) {
213 $this->start = array( 'cpu' => $cpu, 'real' => $real, 'memory' => $memory );
216 $this->workStack[] = array(
217 $functionname,
218 count( $this->workStack ),
219 $real,
220 $cpu,
221 $memory
226 * This method should not be called outside SectionProfiler
228 * @param string $functionname
230 public function profileOutInternal( $functionname ) {
231 $item = array_pop( $this->workStack );
232 if ( $item === null ) {
233 $this->debugGroup( 'profileerror', "Profiling error: $functionname" );
234 return;
236 list( $ofname, /* $ocount */, $ortime, $octime, $omem ) = $item;
238 if ( $functionname === 'close' ) {
239 $message = "Profile section ended by close(): {$ofname}";
240 $this->debugGroup( 'profileerror', $message );
241 if ( $this->collateOnly ) {
242 $this->collated[$message] = $this->errorEntry;
243 } else {
244 $this->stack[] = array( $message, 0, 0.0, 0.0, 0, 0.0, 0.0, 0 );
246 $functionname = $ofname;
247 } elseif ( $ofname !== $functionname ) {
248 $message = "Profiling error: in({$ofname}), out($functionname)";
249 $this->debugGroup( 'profileerror', $message );
250 if ( $this->collateOnly ) {
251 $this->collated[$message] = $this->errorEntry;
252 } else {
253 $this->stack[] = array( $message, 0, 0.0, 0.0, 0, 0.0, 0.0, 0 );
257 $realTime = $this->getTime( 'wall' );
258 $cpuTime = $this->getTime( 'cpu' );
259 $memUsage = memory_get_usage();
261 if ( $this->collateOnly ) {
262 $elapsedcpu = $cpuTime - $octime;
263 $elapsedreal = $realTime - $ortime;
264 $memchange = $memUsage - $omem;
265 $this->updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange );
266 } else {
267 $this->stack[] = array_merge( $item, array( $realTime, $cpuTime, $memUsage ) );
270 $this->end = array(
271 'cpu' => $cpuTime,
272 'real' => $realTime,
273 'memory' => $memUsage
278 * Returns a tree of function calls with their real times
279 * @return string
280 * @throws Exception
282 public function getCallTreeReport() {
283 if ( $this->collateOnly ) {
284 throw new Exception( "Tree is only available for trace profiling." );
286 return implode( '', array_map(
287 array( $this, 'getCallTreeLine' ), $this->remapCallTree( $this->stack )
288 ) );
292 * Recursive function the format the current profiling array into a tree
294 * @param array $stack Profiling array
295 * @return array
297 protected function remapCallTree( array $stack ) {
298 if ( count( $stack ) < 2 ) {
299 return $stack;
301 $outputs = array();
302 for ( $max = count( $stack ) - 1; $max > 0; ) {
303 /* Find all items under this entry */
304 $level = $stack[$max][1];
305 $working = array();
306 for ( $i = $max -1; $i >= 0; $i-- ) {
307 if ( $stack[$i][1] > $level ) {
308 $working[] = $stack[$i];
309 } else {
310 break;
313 $working = $this->remapCallTree( array_reverse( $working ) );
314 $output = array();
315 foreach ( $working as $item ) {
316 array_push( $output, $item );
318 array_unshift( $output, $stack[$max] );
319 $max = $i;
321 array_unshift( $outputs, $output );
323 $final = array();
324 foreach ( $outputs as $output ) {
325 foreach ( $output as $item ) {
326 $final[] = $item;
329 return $final;
333 * Callback to get a formatted line for the call tree
334 * @param array $entry
335 * @return string
337 protected function getCallTreeLine( $entry ) {
338 // $entry has (name, level, stime, scpu, smem, etime, ecpu, emem)
339 list( $fname, $level, $startreal, , , $endreal ) = $entry;
340 $delta = $endreal - $startreal;
341 $space = str_repeat( ' ', $level );
342 # The ugly double sprintf is to work around a PHP bug,
343 # which has been fixed in recent releases.
344 return sprintf( "%10s %s %s\n",
345 trim( sprintf( "%7.3f", $delta * 1000.0 ) ), $space, $fname );
349 * Populate collated data
351 protected function collateData() {
352 if ( $this->collateDone ) {
353 return;
355 $this->collateDone = true;
356 // Close opened profiling sections
357 while ( count( $this->workStack ) ) {
358 $this->profileOutInternal( 'close' );
361 if ( $this->collateOnly ) {
362 return; // already collated as methods exited
365 $this->collated = array();
367 # Estimate profiling overhead
368 $oldEnd = $this->end;
369 $profileCount = count( $this->stack );
370 $this->calculateOverhead( $profileCount );
372 # First, subtract the overhead!
373 $overheadTotal = $overheadMemory = $overheadInternal = array();
374 foreach ( $this->stack as $entry ) {
375 // $entry is (name,pos,rtime0,cputime0,mem0,rtime1,cputime1,mem1)
376 $fname = $entry[0];
377 $elapsed = $entry[5] - $entry[2];
378 $memchange = $entry[7] - $entry[4];
380 if ( $fname === '-overhead-total' ) {
381 $overheadTotal[] = $elapsed;
382 $overheadMemory[] = max( 0, $memchange );
383 } elseif ( $fname === '-overhead-internal' ) {
384 $overheadInternal[] = $elapsed;
387 $overheadTotal = $overheadTotal ?
388 array_sum( $overheadTotal ) / count( $overheadInternal ) : 0;
389 $overheadMemory = $overheadMemory ?
390 array_sum( $overheadMemory ) / count( $overheadInternal ) : 0;
391 $overheadInternal = $overheadInternal ?
392 array_sum( $overheadInternal ) / count( $overheadInternal ) : 0;
394 # Collate
395 foreach ( $this->stack as $index => $entry ) {
396 // $entry is (name,pos,rtime0,cputime0,mem0,rtime1,cputime1,mem1)
397 $fname = $entry[0];
398 $elapsedCpu = $entry[6] - $entry[3];
399 $elapsedReal = $entry[5] - $entry[2];
400 $memchange = $entry[7] - $entry[4];
401 $subcalls = $this->calltreeCount( $this->stack, $index );
403 if ( substr( $fname, 0, 9 ) !== '-overhead' ) {
404 # Adjust for profiling overhead (except special values with elapsed=0)
405 if ( $elapsed ) {
406 $elapsed -= $overheadInternal;
407 $elapsed -= ( $subcalls * $overheadTotal );
408 $memchange -= ( $subcalls * $overheadMemory );
412 $this->updateEntry( $fname, $elapsedCpu, $elapsedReal, $memchange );
415 $this->collated['-overhead-total']['count'] = $profileCount;
416 arsort( $this->collated, SORT_NUMERIC );
418 // Unclobber the end info map (the overhead checking alters it)
419 $this->end = $oldEnd;
423 * Dummy calls to calculate profiling overhead
425 * @param int $profileCount
427 protected function calculateOverhead( $profileCount ) {
428 $this->profileInInternal( '-overhead-total' );
429 for ( $i = 0; $i < $profileCount; $i++ ) {
430 $this->profileInInternal( '-overhead-internal' );
431 $this->profileOutInternal( '-overhead-internal' );
433 $this->profileOutInternal( '-overhead-total' );
437 * Counts the number of profiled function calls sitting under
438 * the given point in the call graph. Not the most efficient algo.
440 * @param array $stack
441 * @param int $start
442 * @return int
444 protected function calltreeCount( $stack, $start ) {
445 $level = $stack[$start][1];
446 $count = 0;
447 for ( $i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i-- ) {
448 $count ++;
450 return $count;
454 * Get the initial time of the request, based either on $wgRequestTime or
455 * $wgRUstart. Will return null if not able to find data.
457 * @param string|bool $metric Metric to use, with the following possibilities:
458 * - user: User CPU time (without system calls)
459 * - cpu: Total CPU time (user and system calls)
460 * - wall (or any other string): elapsed time
461 * - false (default): will fall back to default metric
462 * @return float|null
464 protected function getTime( $metric = 'wall' ) {
465 if ( $metric === 'cpu' || $metric === 'user' ) {
466 $ru = wfGetRusage();
467 if ( !$ru ) {
468 return 0;
470 $time = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
471 if ( $metric === 'cpu' ) {
472 # This is the time of system calls, added to the user time
473 # it gives the total CPU time
474 $time += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
476 return $time;
477 } else {
478 return microtime( true );
483 * Add an entry in the debug log file
485 * @param string $s String to output
487 protected function debug( $s ) {
488 if ( function_exists( 'wfDebug' ) ) {
489 wfDebug( $s );
494 * Add an entry in the debug log group
496 * @param string $group Group to send the message to
497 * @param string $s String to output
499 protected function debugGroup( $group, $s ) {
500 if ( function_exists( 'wfDebugLog' ) ) {
501 wfDebugLog( $group, $s );
507 * Subclass ScopedCallback to avoid call_user_func_array(), which is slow
509 * This class should not be used outside of SectionProfiler
511 class SectionProfileCallback extends ScopedCallback {
512 /** @var SectionProfiler */
513 protected $profiler;
514 /** @var string */
515 protected $section;
518 * @param SectionProfiler $profiler
519 * @param string $section
521 public function __construct( SectionProfiler $profiler, $section ) {
522 parent::__construct( null );
523 $this->profiler = $profiler;
524 $this->section = $section;
527 function __destruct() {
528 $this->profiler->profileOutInternal( $this->section );