class Profiler {
var $mStack = array (), $mWorkStack = array (), $mCollated = array ();
var $mCalls = array (), $mTotals = array ();
- /*
+
function Profiler()
{
- $this->mProfileStack = array();
- $this->mWorkStack = array();
- $this->mCollated = array();
+ // Push an entry for the pre-profile setup time onto the stack
+ global $wgRequestTime;
+ if ( !empty( $wgRequestTime ) ) {
+ $this->mWorkStack[] = array( '-total', 0, $wgRequestTime, 0 );
+ $this->mStack[] = array( '-setup', 1, $wgRequestTime, 0, microtime(), 0 );
+ } else {
+ $this->profileIn( '-total' );
+ }
+
}
- */
function profileIn($functionname) {
global $wgDebugFunctionEntry;
$memory = memory_get_usage();
$time = $this->getTime();
- global $wgDebugProfiling, $wgDebugFunctionEntry;
+ global $wgDebugFunctionEntry;
if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
wfDebug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
} else {
//if ($wgDebugProfiling) {
if ($functionname == 'close') {
- $message = "Profile section ended by close(): {$bit[0]}\n";
- wfDebug( $message );
+ $message = "Profile section ended by close(): {$bit[0]}";
+ wfDebug( "$message\n" );
$this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
}
elseif ($bit[0] != $functionname) {
- $message = "Profiling error: in({$bit[0]}), out($functionname)\n";
- wfDebug( $message );
+ $message = "Profiling error: in({$bit[0]}), out($functionname)";
+ wfDebug( "$message\n" );
$this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
}
//}
$subcalls = $this->calltreeCount($this->mStack, $index);
if (!preg_match('/^-overhead/', $fname)) {
- # Adjust for profiling overhead
- $elapsed -= $overheadInternal;
- $elapsed -= ($subcalls * $overheadTotal);
- $memory -= ($subcalls * $overheadMemory);
+ # Adjust for profiling overhead (except special values with elapsed=0
+ if ( $elapsed ) {
+ $elapsed -= $overheadInternal;
+ $elapsed -= ($subcalls * $overheadTotal);
+ $memory -= ($subcalls * $overheadMemory);
+ }
}
if (!array_key_exists($fname, $this->mCollated)) {
$calls = $this->mCalls[$fname];
$percent = $total ? 100. * $elapsed / $total : 0;
$memory = $this->mMemory[$fname];
- $prof .= sprintf($format, $fname, $calls, (float) ($elapsed * 1000), (float) ($elapsed * 1000) / $calls, $percent, $memory, ($this->mMin[$fname] * 1000.0), ($this->mMax[$fname] * 1000.0), $this->mOverhead[$fname]);
+ $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]);
global $wgProfileToDatabase;
if ($wgProfileToDatabase) {
* @static
*/
function logToDB($name, $timeSum, $eventCount) {
+ # Warning: $wguname is a live patch, it should be moved to Setup.php
+ global $wguname;
+
$fname = 'Profiler::logToDB';
$dbw = & wfGetDB(DB_MASTER);
$profiling = $dbw->tableName('profiling');
$name = substr($name, 0, 255);
$encname = $dbw->strencode($name);
- $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} "."WHERE pf_name='{$encname}'";
+ $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} ".
+ "WHERE pf_name='{$encname}' AND pf_server='{$wguname['nodename']}'";
$dbw->query($sql);
$rc = $dbw->affectedRows();
if ($rc == 0) {
- $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount, 'pf_time' => $timeSum), $fname, array ('IGNORE'));
+ $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount,
+ 'pf_time' => $timeSum, 'pf_server' => $wguname['nodename'] ), $fname, array ('IGNORE'));
}
// When we upgrade to mysql 4.1, the insert+update
// can be merged into just a insert with this construct added:
// "pf_time=pf_time + VALUES(pf_time)";
}
+ /**
+ * Get the function name of the current profiling section
+ */
+ function getCurrentSection() {
+ $elt = end($this->mWorkStack);
+ return $elt[0];
+ }
+
}
$wgProfiler = new Profiler();
-$wgProfiler->profileIn('-total');
+
?>