3 * Base class and functions for 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 * This file is only included if profiling is enabled
26 * @defgroup Profiler Profiler
30 * Begin profiling of a function
31 * @param $functionname String: name of the function we will profile
33 function wfProfileIn( $functionname ) {
35 if ( $wgProfiler instanceof Profiler ||
isset( $wgProfiler['class'] ) ) {
36 Profiler
::instance()->profileIn( $functionname );
41 * Stop profiling of a function
42 * @param $functionname String: name of the function we have profiled
44 function wfProfileOut( $functionname = 'missing' ) {
46 if ( $wgProfiler instanceof Profiler ||
isset( $wgProfiler['class'] ) ) {
47 Profiler
::instance()->profileOut( $functionname );
56 protected $mStack = array(), $mWorkStack = array (), $mCollated = array (),
57 $mCalls = array (), $mTotals = array ();
58 protected $mTimeMetric = 'wall';
59 protected $mProfileID = false, $mCollateDone = false, $mTemplated = false;
60 private static $__instance = null;
62 function __construct( $params ) {
63 if ( isset( $params['timeMetric'] ) ) {
64 $this->mTimeMetric
= $params['timeMetric'];
66 if ( isset( $params['profileID'] ) ) {
67 $this->mProfileID
= $params['profileID'];
70 $this->addInitialStack();
77 public static function instance() {
78 if( is_null( self
::$__instance ) ) {
80 if( is_array( $wgProfiler ) ) {
81 if( !isset( $wgProfiler['class'] ) ) {
82 wfDebug( __METHOD__
. " called without \$wgProfiler['class']"
83 . " set, falling back to ProfilerStub for safety\n" );
84 $class = 'ProfilerStub';
86 $class = $wgProfiler['class'];
88 self
::$__instance = new $class( $wgProfiler );
89 } elseif( $wgProfiler instanceof Profiler
) {
90 self
::$__instance = $wgProfiler; // back-compat
92 wfDebug( __METHOD__
. ' called with bogus $wgProfiler setting,'
93 . " falling back to ProfilerStub for safety\n" );
94 self
::$__instance = new ProfilerStub( $wgProfiler );
97 return self
::$__instance;
101 * Set the profiler to a specific profiler instance. Mostly for dumpHTML
102 * @param $p Profiler object
104 public static function setInstance( Profiler
$p ) {
105 self
::$__instance = $p;
109 * Return whether this a stub profiler
113 public function isStub() {
117 public function setProfileID( $id ) {
118 $this->mProfileID
= $id;
121 public function getProfileID() {
122 if ( $this->mProfileID
=== false ) {
125 return $this->mProfileID
;
130 * Add the inital item in the stack.
132 protected function addInitialStack() {
133 // Push an entry for the pre-profile setup time onto the stack
134 $initial = $this->getInitialTime();
135 if ( $initial !== null ) {
136 $this->mWorkStack
[] = array( '-total', 0, $initial, 0 );
137 $this->mStack
[] = array( '-setup', 1, $initial, 0, $this->getTime(), 0 );
139 $this->profileIn( '-total' );
144 * Called by wfProfieIn()
146 * @param $functionname String
148 public function profileIn( $functionname ) {
149 global $wgDebugFunctionEntry;
150 if( $wgDebugFunctionEntry ){
151 $this->debug( str_repeat( ' ', count( $this->mWorkStack
) ) . 'Entering ' . $functionname . "\n" );
154 $this->mWorkStack
[] = array( $functionname, count( $this->mWorkStack
), $this->getTime(), memory_get_usage() );
158 * Called by wfProfieOut()
160 * @param $functionname String
162 public function profileOut( $functionname ) {
163 global $wgDebugFunctionEntry;
164 $memory = memory_get_usage();
165 $time = $this->getTime();
167 if( $wgDebugFunctionEntry ){
168 $this->debug( str_repeat( ' ', count( $this->mWorkStack
) - 1 ) . 'Exiting ' . $functionname . "\n" );
171 $bit = array_pop($this->mWorkStack
);
174 $this->debug("Profiling error, !\$bit: $functionname\n");
176 //if( $wgDebugProfiling ){
177 if( $functionname == 'close' ){
178 $message = "Profile section ended by close(): {$bit[0]}";
179 $this->debug( "$message\n" );
180 $this->mStack
[] = array( $message, 0, 0.0, 0, 0.0, 0 );
182 elseif( $bit[0] != $functionname ){
183 $message = "Profiling error: in({$bit[0]}), out($functionname)";
184 $this->debug( "$message\n" );
185 $this->mStack
[] = array( $message, 0, 0.0, 0, 0.0, 0 );
190 $this->mStack
[] = $bit;
195 * Close opened profiling sections
197 public function close() {
198 while( count( $this->mWorkStack
) ){
199 $this->profileOut( 'close' );
204 * Mark this call as templated or not
208 function setTemplated( $t ) {
209 $this->mTemplated
= $t;
213 * Returns a profiling output to be stored in debug file
217 public function getOutput() {
218 global $wgDebugFunctionEntry, $wgProfileCallTree;
219 $wgDebugFunctionEntry = false;
221 if( !count( $this->mStack
) && !count( $this->mCollated
) ){
222 return "No profiling output\n";
225 if( $wgProfileCallTree ) {
226 return $this->getCallTree();
228 return $this->getFunctionReport();
233 * Returns a tree of function call instead of a list of functions
236 function getCallTree() {
237 return implode( '', array_map( array( &$this, 'getCallTreeLine' ), $this->remapCallTree( $this->mStack
) ) );
241 * Recursive function the format the current profiling array into a tree
243 * @param $stack array profiling array
246 function remapCallTree( $stack ) {
247 if( count( $stack ) < 2 ){
251 for( $max = count( $stack ) - 1; $max > 0; ){
252 /* Find all items under this entry */
253 $level = $stack[$max][1];
255 for( $i = $max -1; $i >= 0; $i-- ){
256 if( $stack[$i][1] > $level ){
257 $working[] = $stack[$i];
262 $working = $this->remapCallTree( array_reverse( $working ) );
264 foreach( $working as $item ){
265 array_push( $output, $item );
267 array_unshift( $output, $stack[$max] );
270 array_unshift( $outputs, $output );
273 foreach( $outputs as $output ){
274 foreach( $output as $item ){
282 * Callback to get a formatted line for the call tree
285 function getCallTreeLine( $entry ) {
286 list( $fname, $level, $start, /* $x */, $end) = $entry;
287 $delta = $end - $start;
288 $space = str_repeat(' ', $level);
289 # The ugly double sprintf is to work around a PHP bug,
290 # which has been fixed in recent releases.
291 return sprintf( "%10s %s %s\n", trim( sprintf( "%7.3f", $delta * 1000.0 ) ), $space, $fname );
295 * Get the initial time of the request, based either on $wgRequestTime or
296 * $wgRUstart. Will return null if not able to find data.
298 * @param $metric string|false: metric to use, with the following possibilities:
299 * - user: User CPU time (without system calls)
300 * - cpu: Total CPU time (user and system calls)
301 * - wall (or any other string): elapsed time
302 * - false (default): will fall back to default metric
305 function getTime( $metric = false ) {
306 if ( $metric === false ) {
307 $metric = $this->mTimeMetric
;
310 if ( $metric === 'cpu' ||
$this->mTimeMetric
=== 'user' ) {
311 if ( !function_exists( 'getrusage' ) ) {
315 $time = $ru['ru_utime.tv_sec'] +
$ru['ru_utime.tv_usec'] / 1e6
;
316 if ( $metric === 'cpu' ) {
317 # This is the time of system calls, added to the user time
318 # it gives the total CPU time
319 $time +
= $ru['ru_stime.tv_sec'] +
$ru['ru_stime.tv_usec'] / 1e6
;
323 return microtime( true );
328 * Get the initial time of the request, based either on $wgRequestTime or
329 * $wgRUstart. Will return null if not able to find data.
331 * @param $metric string|false: metric to use, with the following possibilities:
332 * - user: User CPU time (without system calls)
333 * - cpu: Total CPU time (user and system calls)
334 * - wall (or any other string): elapsed time
335 * - false (default): will fall back to default metric
338 protected function getInitialTime( $metric = false ) {
339 global $wgRequestTime, $wgRUstart;
341 if ( $metric === false ) {
342 $metric = $this->mTimeMetric
;
345 if ( $metric === 'cpu' ||
$this->mTimeMetric
=== 'user' ) {
346 if ( !count( $wgRUstart ) ) {
350 $time = $wgRUstart['ru_utime.tv_sec'] +
$wgRUstart['ru_utime.tv_usec'] / 1e6
;
351 if ( $metric === 'cpu' ) {
352 # This is the time of system calls, added to the user time
353 # it gives the total CPU time
354 $time +
= $wgRUstart['ru_stime.tv_sec'] +
$wgRUstart['ru_stime.tv_usec'] / 1e6
;
358 if ( empty( $wgRequestTime ) ) {
361 return $wgRequestTime;
366 protected function collateData() {
367 if ( $this->mCollateDone
) {
370 $this->mCollateDone
= true;
374 $this->mCollated
= array();
375 $this->mCalls
= array();
376 $this->mMemory
= array();
378 # Estimate profiling overhead
379 $profileCount = count($this->mStack
);
380 self
::calculateOverhead( $profileCount );
382 # First, subtract the overhead!
383 $overheadTotal = $overheadMemory = $overheadInternal = array();
384 foreach( $this->mStack
as $entry ){
388 $elapsed = $end - $start;
389 $memory = $entry[5] - $entry[3];
391 if( $fname == '-overhead-total' ){
392 $overheadTotal[] = $elapsed;
393 $overheadMemory[] = $memory;
395 elseif( $fname == '-overhead-internal' ){
396 $overheadInternal[] = $elapsed;
399 $overheadTotal = $overheadTotal ?
array_sum( $overheadTotal ) / count( $overheadInternal ) : 0;
400 $overheadMemory = $overheadMemory ?
array_sum( $overheadMemory ) / count( $overheadInternal ) : 0;
401 $overheadInternal = $overheadInternal ?
array_sum( $overheadInternal ) / count( $overheadInternal ) : 0;
404 foreach( $this->mStack
as $index => $entry ){
408 $elapsed = $end - $start;
410 $memory = $entry[5] - $entry[3];
411 $subcalls = $this->calltreeCount( $this->mStack
, $index );
413 if( !preg_match( '/^-overhead/', $fname ) ){
414 # Adjust for profiling overhead (except special values with elapsed=0
416 $elapsed -= $overheadInternal;
417 $elapsed -= ($subcalls * $overheadTotal);
418 $memory -= ($subcalls * $overheadMemory);
422 if( !array_key_exists( $fname, $this->mCollated
) ){
423 $this->mCollated
[$fname] = 0;
424 $this->mCalls
[$fname] = 0;
425 $this->mMemory
[$fname] = 0;
426 $this->mMin
[$fname] = 1 << 24;
427 $this->mMax
[$fname] = 0;
428 $this->mOverhead
[$fname] = 0;
431 $this->mCollated
[$fname] +
= $elapsed;
432 $this->mCalls
[$fname]++
;
433 $this->mMemory
[$fname] +
= $memory;
434 $this->mMin
[$fname] = min($this->mMin
[$fname], $elapsed);
435 $this->mMax
[$fname] = max($this->mMax
[$fname], $elapsed);
436 $this->mOverhead
[$fname] +
= $subcalls;
439 $this->mCalls
['-overhead-total'] = $profileCount;
440 arsort( $this->mCollated
, SORT_NUMERIC
);
444 * Returns a list of profiled functions.
448 function getFunctionReport() {
449 $this->collateData();
452 $nameWidth = $width - 65;
453 $format = "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d (%13.3f -%13.3f) [%d]\n";
454 $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s\n";
455 $prof = "\nProfiling data\n";
456 $prof .= sprintf( $titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem' );
458 $total = isset( $this->mCollated
['-total'] ) ?
$this->mCollated
['-total'] : 0;
460 foreach( $this->mCollated
as $fname => $elapsed ){
461 $calls = $this->mCalls
[$fname];
462 $percent = $total ?
100. * $elapsed / $total : 0;
463 $memory = $this->mMemory
[$fname];
464 $prof .= sprintf($format, substr($fname, 0, $nameWidth), $calls, (float) ($elapsed * 1000), (float) ($elapsed * 1000) / $calls, $percent, $memory, ($this->mMin
[$fname] * 1000.0), ($this->mMax
[$fname] * 1000.0), $this->mOverhead
[$fname]);
466 $prof .= "\nTotal: $total\n\n";
472 * Dummy calls to wfProfileIn/wfProfileOut to calculate its overhead
474 protected static function calculateOverhead( $profileCount ) {
475 wfProfileIn( '-overhead-total' );
476 for( $i = 0; $i < $profileCount; $i++
){
477 wfProfileIn( '-overhead-internal' );
478 wfProfileOut( '-overhead-internal' );
480 wfProfileOut( '-overhead-total' );
484 * Counts the number of profiled function calls sitting under
485 * the given point in the call graph. Not the most efficient algo.
487 * @param $stack Array:
488 * @param $start Integer:
492 function calltreeCount($stack, $start) {
493 $level = $stack[$start][1];
495 for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) {
502 * Log the whole profiling data into the database.
504 public function logData(){
505 global $wgProfilePerHost, $wgProfileToDatabase;
507 # Do not log anything if database is readonly (bug 5375)
508 if( wfReadOnly() ||
!$wgProfileToDatabase ) {
512 $dbw = wfGetDB( DB_MASTER
);
513 if( !is_object( $dbw ) ) {
517 $errorState = $dbw->ignoreErrors( true );
519 if( $wgProfilePerHost ){
520 $pfhost = wfHostname();
525 $this->collateData();
527 foreach( $this->mCollated
as $name => $elapsed ){
528 $eventCount = $this->mCalls
[$name];
529 $timeSum = (float) ($elapsed * 1000);
530 $memorySum = (float)$this->mMemory
[$name];
531 $name = substr($name, 0, 255);
534 $timeSum = ($timeSum >= 0) ?
$timeSum : 0;
535 $memorySum = ($memorySum >= 0) ?
$memorySum : 0;
537 $dbw->update( 'profiling',
539 "pf_count=pf_count+{$eventCount}",
540 "pf_time=pf_time+{$timeSum}",
541 "pf_memory=pf_memory+{$memorySum}",
545 'pf_server' => $pfhost,
549 $rc = $dbw->affectedRows();
551 $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount,
552 'pf_time' => $timeSum, 'pf_memory' => $memorySum, 'pf_server' => $pfhost ),
553 __METHOD__
, array ('IGNORE'));
555 // When we upgrade to mysql 4.1, the insert+update
556 // can be merged into just a insert with this construct added:
557 // "ON DUPLICATE KEY UPDATE ".
558 // "pf_count=pf_count + VALUES(pf_count), ".
559 // "pf_time=pf_time + VALUES(pf_time)";
562 $dbw->ignoreErrors( $errorState );
566 * Get the function name of the current profiling section
569 function getCurrentSection() {
570 $elt = end( $this->mWorkStack
);
575 * Add an entry in the debug log file
577 * @param $s String to output
579 function debug( $s ) {
580 if( defined( 'MW_COMPILED' ) ||
function_exists( 'wfDebug' ) ) {