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 while( count( $this->mWorkStack ) ){
00132 $this->profileOut( 'close' );
00133 }
00134 }
00135
00139 function getOutput() {
00140 global $wgDebugFunctionEntry, $wgProfileCallTree;
00141 $wgDebugFunctionEntry = false;
00142
00143 if( !count( $this->mStack ) && !count( $this->mCollated ) ){
00144 return "No profiling output\n";
00145 }
00146 $this->close();
00147
00148 if( $wgProfileCallTree ) {
00149 global $wgProfileToDatabase;
00150 # XXX: We must call $this->getFunctionReport() to log to the DB
00151 if( $wgProfileToDatabase ) {
00152 $this->getFunctionReport();
00153 }
00154 return $this->getCallTree();
00155 } else {
00156 return $this->getFunctionReport();
00157 }
00158 }
00159
00163 function getCallTree() {
00164 return implode( '', array_map( array( &$this, 'getCallTreeLine' ), $this->remapCallTree( $this->mStack ) ) );
00165 }
00166
00172 function remapCallTree( $stack ) {
00173 if( count( $stack ) < 2 ){
00174 return $stack;
00175 }
00176 $outputs = array ();
00177 for( $max = count( $stack ) - 1; $max > 0; ){
00178
00179 $level = $stack[$max][1];
00180 $working = array ();
00181 for( $i = $max -1; $i >= 0; $i-- ){
00182 if( $stack[$i][1] > $level ){
00183 $working[] = $stack[$i];
00184 } else {
00185 break;
00186 }
00187 }
00188 $working = $this->remapCallTree( array_reverse( $working ) );
00189 $output = array();
00190 foreach( $working as $item ){
00191 array_push( $output, $item );
00192 }
00193 array_unshift( $output, $stack[$max] );
00194 $max = $i;
00195
00196 array_unshift( $outputs, $output );
00197 }
00198 $final = array();
00199 foreach( $outputs as $output ){
00200 foreach( $output as $item ){
00201 $final[] = $item;
00202 }
00203 }
00204 return $final;
00205 }
00206
00210 function getCallTreeLine( $entry ) {
00211 list( $fname, $level, $start, , $end) = $entry;
00212 $delta = $end - $start;
00213 $space = str_repeat(' ', $level);
00214 # The ugly double sprintf is to work around a PHP bug,
00215 # which has been fixed in recent releases.
00216 return sprintf( "%10s %s %s\n", trim( sprintf( "%7.3f", $delta * 1000.0 ) ), $space, $fname );
00217 }
00218
00219 function getTime() {
00220 return microtime(true);
00221 #return $this->getUserTime();
00222 }
00223
00224 function getUserTime() {
00225 $ru = getrusage();
00226 return $ru['ru_utime.tv_sec'].' '.$ru['ru_utime.tv_usec'] / 1e6;
00227 }
00228
00233 function getFunctionReport() {
00234 global $wgProfileToDatabase;
00235
00236 $width = 140;
00237 $nameWidth = $width - 65;
00238 $format = "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d (%13.3f -%13.3f) [%d]\n";
00239 $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s\n";
00240 $prof = "\nProfiling data\n";
00241 $prof .= sprintf( $titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem' );
00242 $this->mCollated = array ();
00243 $this->mCalls = array ();
00244 $this->mMemory = array ();
00245
00246 # Estimate profiling overhead
00247 $profileCount = count($this->mStack);
00248 wfProfileIn( '-overhead-total' );
00249 for( $i = 0; $i < $profileCount; $i ++ ){
00250 wfProfileIn( '-overhead-internal' );
00251 wfProfileOut( '-overhead-internal' );
00252 }
00253 wfProfileOut( '-overhead-total' );
00254
00255 # First, subtract the overhead!
00256 foreach( $this->mStack as $entry ){
00257 $fname = $entry[0];
00258 $start = $entry[2];
00259 $end = $entry[4];
00260 $elapsed = $end - $start;
00261 $memory = $entry[5] - $entry[3];
00262
00263 if( $fname == '-overhead-total' ){
00264 $overheadTotal[] = $elapsed;
00265 $overheadMemory[] = $memory;
00266 }
00267 elseif( $fname == '-overhead-internal' ){
00268 $overheadInternal[] = $elapsed;
00269 }
00270 }
00271 $overheadTotal = array_sum( $overheadTotal ) / count( $overheadInternal );
00272 $overheadMemory = array_sum( $overheadMemory ) / count( $overheadInternal );
00273 $overheadInternal = array_sum( $overheadInternal ) / count( $overheadInternal );
00274
00275 # Collate
00276 foreach( $this->mStack as $index => $entry ){
00277 $fname = $entry[0];
00278 $start = $entry[2];
00279 $end = $entry[4];
00280 $elapsed = $end - $start;
00281
00282 $memory = $entry[5] - $entry[3];
00283 $subcalls = $this->calltreeCount( $this->mStack, $index );
00284
00285 if( !preg_match( '/^-overhead/', $fname ) ){
00286 # Adjust for profiling overhead (except special values with elapsed=0
00287 if( $elapsed ) {
00288 $elapsed -= $overheadInternal;
00289 $elapsed -= ($subcalls * $overheadTotal);
00290 $memory -= ($subcalls * $overheadMemory);
00291 }
00292 }
00293
00294 if( !array_key_exists( $fname, $this->mCollated ) ){
00295 $this->mCollated[$fname] = 0;
00296 $this->mCalls[$fname] = 0;
00297 $this->mMemory[$fname] = 0;
00298 $this->mMin[$fname] = 1 << 24;
00299 $this->mMax[$fname] = 0;
00300 $this->mOverhead[$fname] = 0;
00301 }
00302
00303 $this->mCollated[$fname] += $elapsed;
00304 $this->mCalls[$fname]++;
00305 $this->mMemory[$fname] += $memory;
00306 $this->mMin[$fname] = min($this->mMin[$fname], $elapsed);
00307 $this->mMax[$fname] = max($this->mMax[$fname], $elapsed);
00308 $this->mOverhead[$fname] += $subcalls;
00309 }
00310
00311 $total = @$this->mCollated['-total'];
00312 $this->mCalls['-overhead-total'] = $profileCount;
00313
00314 # Output
00315 arsort( $this->mCollated, SORT_NUMERIC );
00316 foreach( $this->mCollated as $fname => $elapsed ){
00317 $calls = $this->mCalls[$fname];
00318 $percent = $total ? 100. * $elapsed / $total : 0;
00319 $memory = $this->mMemory[$fname];
00320 $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]);
00321 # Log to the DB
00322 if( $wgProfileToDatabase ) {
00323 self::logToDB($fname, (float) ($elapsed * 1000), $calls, (float) ($memory) );
00324 }
00325 }
00326 $prof .= "\nTotal: $total\n\n";
00327
00328 return $prof;
00329 }
00330
00340 function calltreeCount($stack, $start) {
00341 $level = $stack[$start][1];
00342 $count = 0;
00343 for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) {
00344 $count ++;
00345 }
00346 return $count;
00347 }
00348
00356 static function logToDB( $name, $timeSum, $eventCount, $memorySum ){
00357 # Do not log anything if database is readonly (bug 5375)
00358 if( wfReadOnly() ) { return; }
00359
00360 global $wgProfilePerHost;
00361
00362 $dbw = wfGetDB( DB_MASTER );
00363 if( !is_object( $dbw ) )
00364 return false;
00365 $errorState = $dbw->ignoreErrors( true );
00366
00367 $name = substr($name, 0, 255);
00368
00369 if( $wgProfilePerHost ){
00370 $pfhost = wfHostname();
00371 } else {
00372 $pfhost = '';
00373 }
00374
00375
00376 $timeSum = ($timeSum >= 0) ? $timeSum : 0;
00377 $memorySum = ($memorySum >= 0) ? $memorySum : 0;
00378
00379 $dbw->update( 'profiling',
00380 array(
00381 "pf_count=pf_count+{$eventCount}",
00382 "pf_time=pf_time+{$timeSum}",
00383 "pf_memory=pf_memory+{$memorySum}",
00384 ),
00385 array(
00386 'pf_name' => $name,
00387 'pf_server' => $pfhost,
00388 ),
00389 __METHOD__ );
00390
00391
00392 $rc = $dbw->affectedRows();
00393 if ($rc == 0) {
00394 $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount,
00395 'pf_time' => $timeSum, 'pf_memory' => $memorySum, 'pf_server' => $pfhost ),
00396 __METHOD__, array ('IGNORE'));
00397 }
00398
00399
00400
00401
00402
00403 $dbw->ignoreErrors( $errorState );
00404 }
00405
00409 function getCurrentSection() {
00410 $elt = end( $this->mWorkStack );
00411 return $elt[0];
00412 }
00413
00418 static function getCaller( $level ) {
00419 $backtrace = wfDebugBacktrace();
00420 if ( isset( $backtrace[$level] ) ) {
00421 if ( isset( $backtrace[$level]['class'] ) ) {
00422 $caller = $backtrace[$level]['class'] . '::' . $backtrace[$level]['function'];
00423 } else {
00424 $caller = $backtrace[$level]['function'];
00425 }
00426 } else {
00427 $caller = 'unknown';
00428 }
00429 return $caller;
00430 }
00431
00436 function debug( $s ) {
00437 if( function_exists( 'wfDebug' ) ) {
00438 wfDebug( $s );
00439 }
00440 }
00441 }