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
26 * Custom PHP profiler for parser/DB type section names that xhprof/xdebug can't handle
30 class SectionProfiler
{
31 /** @var array List of resolved profile calls with start/end data */
32 protected $stack = array();
33 /** @var array Queue of open profile calls with start data */
34 protected $workStack = array();
36 /** @var array Map of (function name => aggregate data array) */
37 protected $collated = array();
39 protected $collateDone = false;
40 /** @var bool Whether to collect the full stack trace or just aggregates */
41 protected $collateOnly = true;
43 /** @var array Cache of a standard broken collation entry */
44 protected $errorEntry;
47 * @param array $params
49 public function __construct( array $params = array() ) {
50 $this->errorEntry
= $this->getErrorEntry();
51 $this->collateOnly
= empty( $params['trace'] );
55 * @param string $section
56 * @return ScopedCallback
58 public function scopedProfileIn( $section ) {
59 $this->profileInInternal( $section );
62 return new ScopedCallback( function() use ( $that, $section ) {
63 $that->profileOutInternal( $section );
68 * @param ScopedCallback $section
70 public function scopedProfileOut( ScopedCallback
&$section ) {
75 * Get the aggregated inclusive profiling data for each method
77 * The percent time for each time is based on the current "total" time
78 * used is based on all methods so far. This method can therefore be
79 * called several times in between several profiling calls without the
80 * delays in usage of the profiler skewing the results. A "-total" entry
81 * is always included in the results.
83 * @return array List of method entries arrays, each having:
84 * - name : method name
85 * - calls : the number of invoking calls
86 * - real : real time ellapsed (ms)
87 * - %real : percent real time
88 * - cpu : real time ellapsed (ms)
89 * - %cpu : percent real time
90 * - memory : memory used (bytes)
91 * - %memory : percent memory used
93 public function getFunctionStats() {
99 foreach ( $this->collated
as $fname => $data ) {
100 $totalCpu +
= $data['cpu'];
101 $totalReal +
= $data['real'];
102 $totalMem +
= $data['memory'];
106 foreach ( $this->collated
as $fname => $data ) {
109 'calls' => $data['count'],
110 'real' => $data['real'] * 1000,
111 '%real' => $totalReal ?
100 * $data['real'] / $totalReal : 0,
112 'cpu' => $data['cpu'] * 1000,
113 '%cpu' => $totalCpu ?
100 * $data['cpu'] / $totalCpu : 0,
114 'memory' => $data['memory'],
115 '%memory' => $totalMem ?
100 * $data['memory'] / $totalMem : 0,
122 'real' => 1000 * $totalReal,
124 'cpu' => 1000 * $totalCpu,
126 'memory' => $totalMem,
134 * @return array Initial collation entry
136 protected function getZeroEntry() {
146 * @return array Initial collation entry for errors
148 protected function getErrorEntry() {
149 $entry = $this->getZeroEntry();
155 * Update the collation entry for a given method name
157 * @param string $name
158 * @param float $elapsedCpu
159 * @param float $elapsedReal
160 * @param int $memChange
162 protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) {
163 $entry =& $this->collated
[$name];
164 if ( !is_array( $entry ) ) {
165 $entry = $this->getZeroEntry();
166 $this->collated
[$name] =& $entry;
168 $entry['cpu'] +
= $elapsedCpu;
169 $entry['real'] +
= $elapsedReal;
170 $entry['memory'] +
= $memChange > 0 ?
$memChange : 0;
175 * This method should not be called outside SectionProfiler
177 * @param string $functionname
179 public function profileInInternal( $functionname ) {
180 $this->workStack
[] = array(
182 count( $this->workStack
),
183 $this->getTime( 'time' ),
184 $this->getTime( 'cpu' ),
190 * This method should not be called outside SectionProfiler
192 * @param string $functionname
194 public function profileOutInternal( $functionname ) {
195 $item = array_pop( $this->workStack
);
196 if ( $item === null ) {
197 $this->debugGroup( 'profileerror', "Profiling error: $functionname" );
200 list( $ofname, /* $ocount */, $ortime, $octime, $omem ) = $item;
202 if ( $functionname === 'close' ) {
203 $message = "Profile section ended by close(): {$ofname}";
204 $this->debugGroup( 'profileerror', $message );
205 if ( $this->collateOnly
) {
206 $this->collated
[$message] = $this->errorEntry
;
208 $this->stack
[] = array( $message, 0, 0.0, 0.0, 0, 0.0, 0.0, 0 );
210 $functionname = $ofname;
211 } elseif ( $ofname !== $functionname ) {
212 $message = "Profiling error: in({$ofname}), out($functionname)";
213 $this->debugGroup( 'profileerror', $message );
214 if ( $this->collateOnly
) {
215 $this->collated
[$message] = $this->errorEntry
;
217 $this->stack
[] = array( $message, 0, 0.0, 0.0, 0, 0.0, 0.0, 0 );
220 $realTime = $this->getTime( 'wall' );
221 $cpuTime = $this->getTime( 'cpu' );
222 if ( $this->collateOnly
) {
223 $elapsedcpu = $cpuTime - $octime;
224 $elapsedreal = $realTime - $ortime;
225 $memchange = memory_get_usage() - $omem;
226 $this->updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange );
228 $this->stack
[] = array_merge( $item,
229 array( $realTime, $cpuTime, memory_get_usage() ) );
234 * Returns a tree of function calls with their real times
237 public function getCallTreeReport() {
238 if ( $this->collateOnly
) {
239 throw new Exception( "Tree is only available for trace profiling." );
241 return implode( '', array_map(
242 array( $this, 'getCallTreeLine' ), $this->remapCallTree( $this->stack
)
247 * Recursive function the format the current profiling array into a tree
249 * @param array $stack Profiling array
252 protected function remapCallTree( array $stack ) {
253 if ( count( $stack ) < 2 ) {
257 for ( $max = count( $stack ) - 1; $max > 0; ) {
258 /* Find all items under this entry */
259 $level = $stack[$max][1];
261 for ( $i = $max -1; $i >= 0; $i-- ) {
262 if ( $stack[$i][1] > $level ) {
263 $working[] = $stack[$i];
268 $working = $this->remapCallTree( array_reverse( $working ) );
270 foreach ( $working as $item ) {
271 array_push( $output, $item );
273 array_unshift( $output, $stack[$max] );
276 array_unshift( $outputs, $output );
279 foreach ( $outputs as $output ) {
280 foreach ( $output as $item ) {
288 * Callback to get a formatted line for the call tree
289 * @param array $entry
292 protected function getCallTreeLine( $entry ) {
293 // $entry has (name, level, stime, scpu, smem, etime, ecpu, emem)
294 list( $fname, $level, $startreal, , , $endreal ) = $entry;
295 $delta = $endreal - $startreal;
296 $space = str_repeat( ' ', $level );
297 # The ugly double sprintf is to work around a PHP bug,
298 # which has been fixed in recent releases.
299 return sprintf( "%10s %s %s\n",
300 trim( sprintf( "%7.3f", $delta * 1000.0 ) ), $space, $fname );
304 * Populate collated data
306 protected function collateData() {
307 if ( $this->collateDone
) {
310 $this->collateDone
= true;
311 // Close opened profiling sections
312 while ( count( $this->workStack
) ) {
313 $this->profileOutInternal( 'close' );
316 if ( $this->collateOnly
) {
317 return; // already collated as methods exited
320 $this->collated
= array();
322 # Estimate profiling overhead
323 $profileCount = count( $this->stack
);
324 $this->calculateOverhead( $profileCount );
326 # First, subtract the overhead!
327 $overheadTotal = $overheadMemory = $overheadInternal = array();
328 foreach ( $this->stack
as $entry ) {
329 // $entry is (name,pos,rtime0,cputime0,mem0,rtime1,cputime1,mem1)
331 $elapsed = $entry[5] - $entry[2];
332 $memchange = $entry[7] - $entry[4];
334 if ( $fname === '-overhead-total' ) {
335 $overheadTotal[] = $elapsed;
336 $overheadMemory[] = max( 0, $memchange );
337 } elseif ( $fname === '-overhead-internal' ) {
338 $overheadInternal[] = $elapsed;
341 $overheadTotal = $overheadTotal ?
342 array_sum( $overheadTotal ) / count( $overheadInternal ) : 0;
343 $overheadMemory = $overheadMemory ?
344 array_sum( $overheadMemory ) / count( $overheadInternal ) : 0;
345 $overheadInternal = $overheadInternal ?
346 array_sum( $overheadInternal ) / count( $overheadInternal ) : 0;
349 foreach ( $this->stack
as $index => $entry ) {
350 // $entry is (name,pos,rtime0,cputime0,mem0,rtime1,cputime1,mem1)
352 $elapsedCpu = $entry[6] - $entry[3];
353 $elapsedReal = $entry[5] - $entry[2];
354 $memchange = $entry[7] - $entry[4];
355 $subcalls = $this->calltreeCount( $this->stack
, $index );
357 if ( substr( $fname, 0, 9 ) !== '-overhead' ) {
358 # Adjust for profiling overhead (except special values with elapsed=0
360 $elapsed -= $overheadInternal;
361 $elapsed -= ( $subcalls * $overheadTotal );
362 $memchange -= ( $subcalls * $overheadMemory );
366 $this->updateEntry( $fname, $elapsedCpu, $elapsedReal, $memchange );
369 $this->collated
['-overhead-total']['count'] = $profileCount;
370 arsort( $this->collated
, SORT_NUMERIC
);
374 * Dummy calls to calculate profiling overhead
376 * @param int $profileCount
378 protected function calculateOverhead( $profileCount ) {
379 $this->profileInInternal( '-overhead-total' );
380 for ( $i = 0; $i < $profileCount; $i++
) {
381 $this->profileInInternal( '-overhead-internal' );
382 $this->profileOutInternal( '-overhead-internal' );
384 $this->profileOutInternal( '-overhead-total' );
388 * Counts the number of profiled function calls sitting under
389 * the given point in the call graph. Not the most efficient algo.
391 * @param array $stack
395 protected function calltreeCount( $stack, $start ) {
396 $level = $stack[$start][1];
398 for ( $i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i-- ) {
405 * Get the initial time of the request, based either on $wgRequestTime or
406 * $wgRUstart. Will return null if not able to find data.
408 * @param string|bool $metric Metric to use, with the following possibilities:
409 * - user: User CPU time (without system calls)
410 * - cpu: Total CPU time (user and system calls)
411 * - wall (or any other string): elapsed time
412 * - false (default): will fall back to default metric
415 protected function getTime( $metric = 'wall' ) {
416 if ( $metric === 'cpu' ||
$metric === 'user' ) {
421 $time = $ru['ru_utime.tv_sec'] +
$ru['ru_utime.tv_usec'] / 1e6
;
422 if ( $metric === 'cpu' ) {
423 # This is the time of system calls, added to the user time
424 # it gives the total CPU time
425 $time +
= $ru['ru_stime.tv_sec'] +
$ru['ru_stime.tv_usec'] / 1e6
;
429 return microtime( true );
434 * Add an entry in the debug log file
436 * @param string $s String to output
438 protected function debug( $s ) {
439 if ( function_exists( 'wfDebug' ) ) {
445 * Add an entry in the debug log group
447 * @param string $group Group to send the message to
448 * @param string $s String to output
450 protected function debugGroup( $group, $s ) {
451 if ( function_exists( 'wfDebugLog' ) ) {
452 wfDebugLog( $group, $s );