00001 <?php
00011 $wgProfiling = true;
00012
00017 function wfProfileIn( $functionname ) {
00018 global $wgProfiler;
00019 $wgProfiler->profileIn( $functionname );
00020 }
00021
00026 function wfProfileOut( $functionname = 'missing' ) {
00027 global $wgProfiler;
00028 $wgProfiler->profileOut( $functionname );
00029 }
00030
00037 function wfGetProfilingOutput( $start, $elapsed ) {
00038 global $wgProfiler;
00039 return $wgProfiler->getOutput( $start, $elapsed );
00040 }
00041
00045 function wfProfileClose() {
00046 global $wgProfiler;
00047 $wgProfiler->close();
00048 }
00049
00050 if (!function_exists('memory_get_usage')) {
00051 # Old PHP or --enable-memory-limit not compiled in
00052 function memory_get_usage() {
00053 return 0;
00054 }
00055 }
00056
00061 class Profiler {
00062 var $mStack = array (), $mWorkStack = array (), $mCollated = array ();
00063 var $mCalls = array (), $mTotals = array ();
00064
00065 function __construct() {
00066
00067 global $wgRequestTime;
00068 if ( !empty( $wgRequestTime ) ) {
00069 $this->mWorkStack[] = array( '-total', 0, $wgRequestTime, 0 );
00070 $this->mStack[] = array( '-setup', 1, $wgRequestTime, 0, microtime(true), 0 );
00071 } else {
00072 $this->profileIn( '-total' );
00073 }
00074 }
00075
00080 function profileIn( $functionname ) {
00081 global $wgDebugFunctionEntry, $wgProfiling;
00082 if( !$wgProfiling ) return;
00083 if( $wgDebugFunctionEntry ){
00084 $this->debug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering ' . $functionname . "\n" );
00085 }
00086
00087 $this->mWorkStack[] = array( $functionname, count( $this->mWorkStack ), $this->getTime(), memory_get_usage() );
00088 }
00089
00094 function profileOut($functionname) {
00095 global $wgDebugFunctionEntry, $wgProfiling;
00096 if( !$wgProfiling ) return;
00097 $memory = memory_get_usage();
00098 $time = $this->getTime();
00099
00100 if( $wgDebugFunctionEntry ){
00101 $this->debug( str_repeat( ' ', count( $this->mWorkStack ) - 1 ) . 'Exiting ' . $functionname . "\n" );
00102 }
00103
00104 $bit = array_pop($this->mWorkStack);
00105
00106 if (!$bit) {
00107 $this->debug("Profiling error, !\$bit: $functionname\n");
00108 } else {
00109
00110 if( $functionname == 'close' ){
00111 $message = "Profile section ended by close(): {$bit[0]}";
00112 $this->debug( "$message\n" );
00113 $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
00114 }
00115 elseif( $bit[0] != $functionname ){
00116 $message = "Profiling error: in({$bit[0]}), out($functionname)";
00117 $this->debug( "$message\n" );
00118 $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
00119 }
00120
00121 $bit[] = $time;
00122 $bit[] = $memory;
00123 $this->mStack[] = $bit;
00124 }
00125 }
00126
00130 function close() {
00131 global $wgProfiling;
00132
00133 # Avoid infinite loop
00134 if( !$wgProfiling )
00135 return;
00136
00137 while( count( $this->mWorkStack ) ){
00138 $this->profileOut( 'close' );
00139 }
00140 }
00141
00145 function getOutput() {
00146 global $wgDebugFunctionEntry, $wgProfileCallTree;
00147 $wgDebugFunctionEntry = false;
00148
00149 if( !count( $this->mStack ) && !count( $this->mCollated ) ){
00150 return "No profiling output\n";
00151 }
00152 $this->close();
00153
00154 if( $wgProfileCallTree ) {
00155 global $wgProfileToDatabase;
00156 # XXX: We must call $this->getFunctionReport() to log to the DB
00157 if( $wgProfileToDatabase ) {
00158 $this->getFunctionReport();
00159 }
00160 return $this->getCallTree();
00161 } else {
00162 return $this->getFunctionReport();
00163 }
00164 }
00165
00169 function getCallTree() {
00170 return implode( '', array_map( array( &$this, 'getCallTreeLine' ), $this->remapCallTree( $this->mStack ) ) );
00171 }
00172
00178 function remapCallTree( $stack ) {
00179 if( count( $stack ) < 2 ){
00180 return $stack;
00181 }
00182 $outputs = array ();
00183 for( $max = count( $stack ) - 1; $max > 0; ){
00184
00185 $level = $stack[$max][1];
00186 $working = array ();
00187 for( $i = $max -1; $i >= 0; $i-- ){
00188 if( $stack[$i][1] > $level ){
00189 $working[] = $stack[$i];
00190 } else {
00191 break;
00192 }
00193 }
00194 $working = $this->remapCallTree( array_reverse( $working ) );
00195 $output = array();
00196 foreach( $working as $item ){
00197 array_push( $output, $item );
00198 }
00199 array_unshift( $output, $stack[$max] );
00200 $max = $i;
00201
00202 array_unshift( $outputs, $output );
00203 }
00204 $final = array();
00205 foreach( $outputs as $output ){
00206 foreach( $output as $item ){
00207 $final[] = $item;
00208 }
00209 }
00210 return $final;
00211 }
00212
00216 function getCallTreeLine( $entry ) {
00217 list( $fname, $level, $start, , $end) = $entry;
00218 $delta = $end - $start;
00219 $space = str_repeat(' ', $level);
00220 # The ugly double sprintf is to work around a PHP bug,
00221 # which has been fixed in recent releases.
00222 return sprintf( "%10s %s %s\n", trim( sprintf( "%7.3f", $delta * 1000.0 ) ), $space, $fname );
00223 }
00224
00225 function getTime() {
00226 return microtime(true);
00227 #return $this->getUserTime();
00228 }
00229
00230 function getUserTime() {
00231 $ru = getrusage();
00232 return $ru['ru_utime.tv_sec'].' '.$ru['ru_utime.tv_usec'] / 1e6;
00233 }
00234
00239 function getFunctionReport() {
00240 global $wgProfileToDatabase;
00241
00242 $width = 140;
00243 $nameWidth = $width - 65;
00244 $format = "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d (%13.3f -%13.3f) [%d]\n";
00245 $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s\n";
00246 $prof = "\nProfiling data\n";
00247 $prof .= sprintf( $titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem' );
00248 $this->mCollated = array ();
00249 $this->mCalls = array ();
00250 $this->mMemory = array ();
00251
00252 # Estimate profiling overhead
00253 $profileCount = count($this->mStack);
00254 wfProfileIn( '-overhead-total' );
00255 for( $i = 0; $i < $profileCount; $i ++ ){
00256 wfProfileIn( '-overhead-internal' );
00257 wfProfileOut( '-overhead-internal' );
00258 }
00259 wfProfileOut( '-overhead-total' );
00260
00261 # First, subtract the overhead!
00262 $overheadTotal = $overheadMemory = $overheadInternal = array();
00263 foreach( $this->mStack as $entry ){
00264 $fname = $entry[0];
00265 $start = $entry[2];
00266 $end = $entry[4];
00267 $elapsed = $end - $start;
00268 $memory = $entry[5] - $entry[3];
00269
00270 if( $fname == '-overhead-total' ){
00271 $overheadTotal[] = $elapsed;
00272 $overheadMemory[] = $memory;
00273 }
00274 elseif( $fname == '-overhead-internal' ){
00275 $overheadInternal[] = $elapsed;
00276 }
00277 }
00278 $overheadTotal = array_sum( $overheadTotal ) / count( $overheadInternal );
00279 $overheadMemory = array_sum( $overheadMemory ) / count( $overheadInternal );
00280 $overheadInternal = array_sum( $overheadInternal ) / count( $overheadInternal );
00281
00282 # Collate
00283 foreach( $this->mStack as $index => $entry ){
00284 $fname = $entry[0];
00285 $start = $entry[2];
00286 $end = $entry[4];
00287 $elapsed = $end - $start;
00288
00289 $memory = $entry[5] - $entry[3];
00290 $subcalls = $this->calltreeCount( $this->mStack, $index );
00291
00292 if( !preg_match( '/^-overhead/', $fname ) ){
00293 # Adjust for profiling overhead (except special values with elapsed=0
00294 if( $elapsed ) {
00295 $elapsed -= $overheadInternal;
00296 $elapsed -= ($subcalls * $overheadTotal);
00297 $memory -= ($subcalls * $overheadMemory);
00298 }
00299 }
00300
00301 if( !array_key_exists( $fname, $this->mCollated ) ){
00302 $this->mCollated[$fname] = 0;
00303 $this->mCalls[$fname] = 0;
00304 $this->mMemory[$fname] = 0;
00305 $this->mMin[$fname] = 1 << 24;
00306 $this->mMax[$fname] = 0;
00307 $this->mOverhead[$fname] = 0;
00308 }
00309
00310 $this->mCollated[$fname] += $elapsed;
00311 $this->mCalls[$fname]++;
00312 $this->mMemory[$fname] += $memory;
00313 $this->mMin[$fname] = min($this->mMin[$fname], $elapsed);
00314 $this->mMax[$fname] = max($this->mMax[$fname], $elapsed);
00315 $this->mOverhead[$fname] += $subcalls;
00316 }
00317
00318 $total = @$this->mCollated['-total'];
00319 $this->mCalls['-overhead-total'] = $profileCount;
00320
00321 # Output
00322 arsort( $this->mCollated, SORT_NUMERIC );
00323 foreach( $this->mCollated as $fname => $elapsed ){
00324 $calls = $this->mCalls[$fname];
00325 $percent = $total ? 100. * $elapsed / $total : 0;
00326 $memory = $this->mMemory[$fname];
00327 $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]);
00328 # Log to the DB
00329 if( $wgProfileToDatabase ) {
00330 self::logToDB($fname, (float) ($elapsed * 1000), $calls, (float) ($memory) );
00331 }
00332 }
00333 $prof .= "\nTotal: $total\n\n";
00334
00335 return $prof;
00336 }
00337
00347 function calltreeCount($stack, $start) {
00348 $level = $stack[$start][1];
00349 $count = 0;
00350 for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) {
00351 $count ++;
00352 }
00353 return $count;
00354 }
00355
00363 static function logToDB( $name, $timeSum, $eventCount, $memorySum ){
00364 # Do not log anything if database is readonly (bug 5375)
00365 if( wfReadOnly() ) { return; }
00366
00367 global $wgProfilePerHost;
00368
00369 $dbw = wfGetDB( DB_MASTER );
00370 if( !is_object( $dbw ) )
00371 return false;
00372 $errorState = $dbw->ignoreErrors( true );
00373
00374 $name = substr($name, 0, 255);
00375
00376 if( $wgProfilePerHost ){
00377 $pfhost = wfHostname();
00378 } else {
00379 $pfhost = '';
00380 }
00381
00382
00383 $timeSum = ($timeSum >= 0) ? $timeSum : 0;
00384 $memorySum = ($memorySum >= 0) ? $memorySum : 0;
00385
00386 $dbw->update( 'profiling',
00387 array(
00388 "pf_count=pf_count+{$eventCount}",
00389 "pf_time=pf_time+{$timeSum}",
00390 "pf_memory=pf_memory+{$memorySum}",
00391 ),
00392 array(
00393 'pf_name' => $name,
00394 'pf_server' => $pfhost,
00395 ),
00396 __METHOD__ );
00397
00398
00399 $rc = $dbw->affectedRows();
00400 if ($rc == 0) {
00401 $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount,
00402 'pf_time' => $timeSum, 'pf_memory' => $memorySum, 'pf_server' => $pfhost ),
00403 __METHOD__, array ('IGNORE'));
00404 }
00405
00406
00407
00408
00409
00410 $dbw->ignoreErrors( $errorState );
00411 }
00412
00416 function getCurrentSection() {
00417 $elt = end( $this->mWorkStack );
00418 return $elt[0];
00419 }
00420
00425 static function getCaller( $level ) {
00426 $backtrace = wfDebugBacktrace();
00427 if ( isset( $backtrace[$level] ) ) {
00428 if ( isset( $backtrace[$level]['class'] ) ) {
00429 $caller = $backtrace[$level]['class'] . '::' . $backtrace[$level]['function'];
00430 } else {
00431 $caller = $backtrace[$level]['function'];
00432 }
00433 } else {
00434 $caller = 'unknown';
00435 }
00436 return $caller;
00437 }
00438
00443 function debug( $s ) {
00444 if( function_exists( 'wfDebug' ) ) {
00445 wfDebug( $s );
00446 }
00447 }
00448 }