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
22 * @author Aaron Schulz
24 use Wikimedia\ScopedCallback
;
27 * Custom PHP profiler for parser/DB type section names that xhprof/xdebug can't handle
31 class SectionProfiler
{
32 /** @var array Map of (mem,real,cpu) */
34 /** @var array Map of (mem,real,cpu) */
36 /** @var array List of resolved profile calls with start/end data */
37 protected $stack = [];
38 /** @var array Queue of open profile calls with start data */
39 protected $workStack = [];
41 /** @var array Map of (function name => aggregate data array) */
42 protected $collated = [];
44 protected $collateDone = false;
46 /** @var bool Whether to collect the full stack trace or just aggregates */
47 protected $collateOnly = true;
48 /** @var array Cache of a standard broken collation entry */
49 protected $errorEntry;
52 * @param array $params
54 public function __construct( array $params = [] ) {
55 $this->errorEntry
= $this->getErrorEntry();
56 $this->collateOnly
= empty( $params['trace'] );
60 * @param string $section
61 * @return SectionProfileCallback
63 public function scopedProfileIn( $section ) {
64 $this->profileInInternal( $section );
66 return new SectionProfileCallback( $this, $section );
70 * @param ScopedCallback $section
72 public function scopedProfileOut( ScopedCallback
&$section ) {
77 * Get the aggregated inclusive profiling data for each method
79 * The percent time for each time is based on the current "total" time
80 * used is based on all methods so far. This method can therefore be
81 * called several times in between several profiling calls without the
82 * delays in usage of the profiler skewing the results. A "-total" entry
83 * is always included in the results.
85 * @return array List of method entries arrays, each having:
86 * - name : method name
87 * - calls : the number of invoking calls
88 * - real : real time elapsed (ms)
89 * - %real : percent real time
90 * - cpu : real time elapsed (ms)
91 * - %cpu : percent real time
92 * - memory : memory used (bytes)
93 * - %memory : percent memory used
94 * - min_real : min real time in a call (ms)
95 * - max_real : max real time in a call (ms)
97 public function getFunctionStats() {
100 $totalCpu = max( $this->end
['cpu'] - $this->start
['cpu'], 0 );
101 $totalReal = max( $this->end
['real'] - $this->start
['real'], 0 );
102 $totalMem = max( $this->end
['memory'] - $this->start
['memory'], 0 );
105 foreach ( $this->collated
as $fname => $data ) {
108 'calls' => $data['count'],
109 'real' => $data['real'] * 1000,
110 '%real' => $totalReal ?
100 * $data['real'] / $totalReal : 0,
111 'cpu' => $data['cpu'] * 1000,
112 '%cpu' => $totalCpu ?
100 * $data['cpu'] / $totalCpu : 0,
113 'memory' => $data['memory'],
114 '%memory' => $totalMem ?
100 * $data['memory'] / $totalMem : 0,
115 'min_real' => 1000 * $data['min_real'],
116 'max_real' => 1000 * $data['max_real']
123 'real' => 1000 * $totalReal,
125 'cpu' => 1000 * $totalCpu,
127 'memory' => $totalMem,
129 'min_real' => 1000 * $totalReal,
130 'max_real' => 1000 * $totalReal
137 * Clear all of the profiling data for another run
139 public function reset() {
143 $this->workStack
= [];
144 $this->collated
= [];
145 $this->collateDone
= false;
149 * @return array Initial collation entry
151 protected function getZeroEntry() {
163 * @return array Initial collation entry for errors
165 protected function getErrorEntry() {
166 $entry = $this->getZeroEntry();
172 * Update the collation entry for a given method name
174 * @param string $name
175 * @param float $elapsedCpu
176 * @param float $elapsedReal
177 * @param int $memChange
179 protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) {
180 $entry =& $this->collated
[$name];
181 if ( !is_array( $entry ) ) {
182 $entry = $this->getZeroEntry();
183 $this->collated
[$name] =& $entry;
185 $entry['cpu'] +
= $elapsedCpu;
186 $entry['real'] +
= $elapsedReal;
187 $entry['memory'] +
= $memChange > 0 ?
$memChange : 0;
189 $entry['min_real'] = min( $entry['min_real'], $elapsedReal );
190 $entry['max_real'] = max( $entry['max_real'], $elapsedReal );
194 * This method should not be called outside SectionProfiler
196 * @param string $functionname
198 public function profileInInternal( $functionname ) {
199 // Once the data is collated for reports, any future calls
200 // should clear the collation cache so the next report will
201 // reflect them. This matters when trace mode is used.
202 $this->collateDone
= false;
204 $cpu = $this->getTime( 'cpu' );
205 $real = $this->getTime( 'wall' );
206 $memory = memory_get_usage();
208 if ( $this->start
=== null ) {
209 $this->start
= [ 'cpu' => $cpu, 'real' => $real, 'memory' => $memory ];
212 $this->workStack
[] = [
214 count( $this->workStack
),
222 * This method should not be called outside SectionProfiler
224 * @param string $functionname
226 public function profileOutInternal( $functionname ) {
227 $item = array_pop( $this->workStack
);
228 if ( $item === null ) {
229 $this->debugGroup( 'profileerror', "Profiling error: $functionname" );
232 list( $ofname, /* $ocount */, $ortime, $octime, $omem ) = $item;
234 if ( $functionname === 'close' ) {
235 $message = "Profile section ended by close(): {$ofname}";
236 $this->debugGroup( 'profileerror', $message );
237 if ( $this->collateOnly
) {
238 $this->collated
[$message] = $this->errorEntry
;
240 $this->stack
[] = [ $message, 0, 0.0, 0.0, 0, 0.0, 0.0, 0 ];
242 $functionname = $ofname;
243 } elseif ( $ofname !== $functionname ) {
244 $message = "Profiling error: in({$ofname}), out($functionname)";
245 $this->debugGroup( 'profileerror', $message );
246 if ( $this->collateOnly
) {
247 $this->collated
[$message] = $this->errorEntry
;
249 $this->stack
[] = [ $message, 0, 0.0, 0.0, 0, 0.0, 0.0, 0 ];
253 $realTime = $this->getTime( 'wall' );
254 $cpuTime = $this->getTime( 'cpu' );
255 $memUsage = memory_get_usage();
257 if ( $this->collateOnly
) {
258 $elapsedcpu = $cpuTime - $octime;
259 $elapsedreal = $realTime - $ortime;
260 $memchange = $memUsage - $omem;
261 $this->updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange );
263 $this->stack
[] = array_merge( $item, [ $realTime, $cpuTime, $memUsage ] );
269 'memory' => $memUsage
274 * Returns a tree of function calls with their real times
278 public function getCallTreeReport() {
279 if ( $this->collateOnly
) {
280 throw new Exception( "Tree is only available for trace profiling." );
282 return implode( '', array_map(
283 [ $this, 'getCallTreeLine' ], $this->remapCallTree( $this->stack
)
288 * Recursive function the format the current profiling array into a tree
290 * @param array $stack Profiling array
293 protected function remapCallTree( array $stack ) {
294 if ( count( $stack ) < 2 ) {
298 for ( $max = count( $stack ) - 1; $max > 0; ) {
299 /* Find all items under this entry */
300 $level = $stack[$max][1];
302 for ( $i = $max -1; $i >= 0; $i-- ) {
303 if ( $stack[$i][1] > $level ) {
304 $working[] = $stack[$i];
309 $working = $this->remapCallTree( array_reverse( $working ) );
311 foreach ( $working as $item ) {
312 array_push( $output, $item );
314 array_unshift( $output, $stack[$max] );
317 array_unshift( $outputs, $output );
320 foreach ( $outputs as $output ) {
321 foreach ( $output as $item ) {
329 * Callback to get a formatted line for the call tree
330 * @param array $entry
333 protected function getCallTreeLine( $entry ) {
334 // $entry has (name, level, stime, scpu, smem, etime, ecpu, emem)
335 list( $fname, $level, $startreal, , , $endreal ) = $entry;
336 $delta = $endreal - $startreal;
337 $space = str_repeat( ' ', $level );
338 # The ugly double sprintf is to work around a PHP bug,
339 # which has been fixed in recent releases.
340 return sprintf( "%10s %s %s\n",
341 trim( sprintf( "%7.3f", $delta * 1000.0 ) ), $space, $fname );
345 * Populate collated data
347 protected function collateData() {
348 if ( $this->collateDone
) {
351 $this->collateDone
= true;
352 // Close opened profiling sections
353 while ( count( $this->workStack
) ) {
354 $this->profileOutInternal( 'close' );
357 if ( $this->collateOnly
) {
358 return; // already collated as methods exited
361 $this->collated
= [];
363 # Estimate profiling overhead
364 $oldEnd = $this->end
;
365 $profileCount = count( $this->stack
);
366 $this->calculateOverhead( $profileCount );
368 # First, subtract the overhead!
369 $overheadTotal = $overheadMemory = $overheadInternal = [];
370 foreach ( $this->stack
as $entry ) {
371 // $entry is (name,pos,rtime0,cputime0,mem0,rtime1,cputime1,mem1)
373 $elapsed = $entry[5] - $entry[2];
374 $memchange = $entry[7] - $entry[4];
376 if ( $fname === '-overhead-total' ) {
377 $overheadTotal[] = $elapsed;
378 $overheadMemory[] = max( 0, $memchange );
379 } elseif ( $fname === '-overhead-internal' ) {
380 $overheadInternal[] = $elapsed;
383 $overheadTotal = $overheadTotal ?
384 array_sum( $overheadTotal ) / count( $overheadInternal ) : 0;
385 $overheadMemory = $overheadMemory ?
386 array_sum( $overheadMemory ) / count( $overheadInternal ) : 0;
387 $overheadInternal = $overheadInternal ?
388 array_sum( $overheadInternal ) / count( $overheadInternal ) : 0;
391 foreach ( $this->stack
as $index => $entry ) {
392 // $entry is (name,pos,rtime0,cputime0,mem0,rtime1,cputime1,mem1)
394 $elapsedCpu = $entry[6] - $entry[3];
395 $elapsedReal = $entry[5] - $entry[2];
396 $memchange = $entry[7] - $entry[4];
397 $subcalls = $this->calltreeCount( $this->stack
, $index );
399 if ( substr( $fname, 0, 9 ) !== '-overhead' ) {
400 # Adjust for profiling overhead (except special values with elapsed=0)
402 $elapsed -= $overheadInternal;
403 $elapsed -= ( $subcalls * $overheadTotal );
404 $memchange -= ( $subcalls * $overheadMemory );
408 $this->updateEntry( $fname, $elapsedCpu, $elapsedReal, $memchange );
411 $this->collated
['-overhead-total']['count'] = $profileCount;
412 arsort( $this->collated
, SORT_NUMERIC
);
414 // Unclobber the end info map (the overhead checking alters it)
415 $this->end
= $oldEnd;
419 * Dummy calls to calculate profiling overhead
421 * @param int $profileCount
423 protected function calculateOverhead( $profileCount ) {
424 $this->profileInInternal( '-overhead-total' );
425 for ( $i = 0; $i < $profileCount; $i++
) {
426 $this->profileInInternal( '-overhead-internal' );
427 $this->profileOutInternal( '-overhead-internal' );
429 $this->profileOutInternal( '-overhead-total' );
433 * Counts the number of profiled function calls sitting under
434 * the given point in the call graph. Not the most efficient algo.
436 * @param array $stack
440 protected function calltreeCount( $stack, $start ) {
441 $level = $stack[$start][1];
443 for ( $i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i-- ) {
450 * Get the initial time of the request, based on getrusage()
452 * @param string|bool $metric Metric to use, with the following possibilities:
453 * - user: User CPU time (without system calls)
454 * - cpu: Total CPU time (user and system calls)
455 * - wall (or any other string): elapsed time
456 * - false (default): will fall back to default metric
459 protected function getTime( $metric = 'wall' ) {
460 if ( $metric === 'cpu' ||
$metric === 'user' ) {
465 $time = $ru['ru_utime.tv_sec'] +
$ru['ru_utime.tv_usec'] / 1e6
;
466 if ( $metric === 'cpu' ) {
467 # This is the time of system calls, added to the user time
468 # it gives the total CPU time
469 $time +
= $ru['ru_stime.tv_sec'] +
$ru['ru_stime.tv_usec'] / 1e6
;
473 return microtime( true );
478 * Add an entry in the debug log file
480 * @param string $s String to output
482 protected function debug( $s ) {
483 if ( function_exists( 'wfDebug' ) ) {
489 * Add an entry in the debug log group
491 * @param string $group Group to send the message to
492 * @param string $s String to output
494 protected function debugGroup( $group, $s ) {
495 if ( function_exists( 'wfDebugLog' ) ) {
496 wfDebugLog( $group, $s );
502 * Subclass ScopedCallback to avoid call_user_func_array(), which is slow
504 * This class should not be used outside of SectionProfiler
506 class SectionProfileCallback
extends ScopedCallback
{
507 /** @var SectionProfiler */
513 * @param SectionProfiler $profiler
514 * @param string $section
516 public function __construct( SectionProfiler
$profiler, $section ) {
517 parent
::__construct( null );
518 $this->profiler
= $profiler;
519 $this->section
= $section;
522 function __destruct() {
523 $this->profiler
->profileOutInternal( $this->section
);