2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* This file is only included if profiling is enabled
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $functioname name of the function we will profile
|
|
|
|
|
*/
|
|
|
|
|
function wfProfileIn( $functionname ) {
|
2003-10-16 13:30:45 +00:00
|
|
|
global $wgProfiler;
|
|
|
|
|
$wgProfiler->profileIn( $functionname );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* @param $functioname name of the function we have profiled
|
|
|
|
|
*/
|
|
|
|
|
function wfProfileOut( $functionname = 'missing' ) {
|
2003-10-16 13:30:45 +00:00
|
|
|
global $wgProfiler;
|
|
|
|
|
$wgProfiler->profileOut( $functionname );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function wfGetProfilingOutput( $start, $elapsed ) {
|
|
|
|
|
global $wgProfiler;
|
|
|
|
|
return $wgProfiler->getOutput( $start, $elapsed );
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
function wfProfileClose() {
|
2003-10-16 13:30:45 +00:00
|
|
|
global $wgProfiler;
|
|
|
|
|
$wgProfiler->close();
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-21 08:27:03 +00:00
|
|
|
if( !function_exists( 'memory_get_usage' ) ) {
|
|
|
|
|
# Old PHP or --enable-memory-limit not compiled in
|
|
|
|
|
function memory_get_usage() {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* @todo document
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2003-10-16 13:30:45 +00:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
function profileIn( $functionname ) {
|
2003-11-09 11:45:12 +00:00
|
|
|
global $wgDebugFunctionEntry;
|
2004-08-22 17:24:50 +00:00
|
|
|
if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
|
|
|
|
|
wfDebug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering '.$functionname."\n" );
|
2003-11-09 11:45:12 +00:00
|
|
|
}
|
2004-08-21 08:27:03 +00:00
|
|
|
array_push( $this->mWorkStack, array($functionname, count( $this->mWorkStack ), microtime(), memory_get_usage() ) );
|
2003-10-16 13:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
function profileOut( $functionname ) {
|
2004-08-21 08:27:03 +00:00
|
|
|
$memory = memory_get_usage();
|
2003-12-01 00:09:05 +00:00
|
|
|
global $wgDebugProfiling, $wgDebugFunctionEntry;
|
2003-11-18 23:52:09 +00:00
|
|
|
|
2004-08-22 17:24:50 +00:00
|
|
|
if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
|
2004-08-27 13:42:09 +00:00
|
|
|
wfDebug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Exiting '.$functionname."\n" );
|
2003-11-09 11:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
2003-10-16 13:30:45 +00:00
|
|
|
$bit = array_pop( $this->mWorkStack );
|
|
|
|
|
|
2003-11-02 13:57:24 +00:00
|
|
|
if ( !$bit ) {
|
|
|
|
|
wfDebug( "Profiling error, !\$bit: $functionname\n" );
|
|
|
|
|
} else {
|
|
|
|
|
if ( $wgDebugProfiling ) {
|
2004-08-22 17:24:50 +00:00
|
|
|
if ( $functionname == 'close' ) {
|
2003-11-02 13:57:24 +00:00
|
|
|
wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
|
|
|
|
|
} elseif ( $bit[0] != $functionname ) {
|
|
|
|
|
wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
|
|
|
|
|
}
|
2003-10-16 13:30:45 +00:00
|
|
|
}
|
2003-11-02 13:57:24 +00:00
|
|
|
array_push( $bit, microtime() );
|
2004-08-21 08:27:03 +00:00
|
|
|
array_push( $bit, $memory );
|
2003-11-02 13:57:24 +00:00
|
|
|
array_push( $this->mStack, $bit );
|
2003-10-16 13:30:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
function close() {
|
2003-10-16 13:30:45 +00:00
|
|
|
while ( count( $this->mWorkStack ) ) {
|
2004-08-22 17:24:50 +00:00
|
|
|
$this->profileOut( 'close' );
|
2003-10-16 13:30:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
function getOutput() {
|
2004-07-10 03:09:26 +00:00
|
|
|
global $wgDebugFunctionEntry;
|
|
|
|
|
$wgDebugFunctionEntry = false;
|
|
|
|
|
|
2003-10-16 13:30:45 +00:00
|
|
|
if( !count( $this->mStack ) ) {
|
|
|
|
|
return "No profiling output\n";
|
|
|
|
|
}
|
2004-05-26 12:30:36 +00:00
|
|
|
$this->close();
|
2003-11-09 11:45:12 +00:00
|
|
|
$width = 125;
|
2004-11-23 07:25:22 +00:00
|
|
|
$format = "%-" . ($width - 34) . "s %6d %6.3f %6.3f %7.3f%% %6d (%6.3f-%6.3f)\n";
|
2004-08-21 08:27:03 +00:00
|
|
|
$titleFormat = "%-" . ($width - 34) . "s %9s %9s %9s %9s %6s\n";
|
2003-10-20 13:39:40 +00:00
|
|
|
$prof = "\nProfiling data\n";
|
2004-08-22 17:24:50 +00:00
|
|
|
$prof .= sprintf( $titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem' );
|
2003-10-16 13:30:45 +00:00
|
|
|
$this->mCollated = array();
|
2003-10-20 13:39:40 +00:00
|
|
|
$this->mCalls = array();
|
2004-08-21 09:25:24 +00:00
|
|
|
$this->mMemory = array();
|
2003-10-20 13:39:40 +00:00
|
|
|
|
|
|
|
|
# Estimate profiling overhead
|
|
|
|
|
$profileCount = count( $this->mStack );
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileIn( '-overhead-total' );
|
2003-10-20 13:39:40 +00:00
|
|
|
for ($i=0; $i<$profileCount ; $i++) {
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileIn( '-overhead-internal' );
|
|
|
|
|
wfProfileOut( '-overhead-internal' );
|
2003-10-16 13:30:45 +00:00
|
|
|
}
|
2004-08-22 17:24:50 +00:00
|
|
|
wfProfileOut( '-overhead-total' );
|
2003-10-16 13:30:45 +00:00
|
|
|
|
2003-10-20 13:39:40 +00:00
|
|
|
# Collate
|
|
|
|
|
foreach ( $this->mStack as $entry ) {
|
|
|
|
|
$fname = $entry[0];
|
|
|
|
|
$thislevel = $entry[1];
|
2004-08-22 17:24:50 +00:00
|
|
|
$start = explode( ' ', $entry[2]);
|
2003-10-20 13:39:40 +00:00
|
|
|
$start = (float)$start[0] + (float)$start[1];
|
2004-08-22 17:24:50 +00:00
|
|
|
$end = explode( ' ', $entry[4]);
|
2003-10-20 13:39:40 +00:00
|
|
|
$end = (float)$end[0] + (float)$end[1];
|
2003-10-16 13:30:45 +00:00
|
|
|
$elapsed = $end - $start;
|
2004-01-17 11:31:48 +00:00
|
|
|
|
2004-08-21 08:27:03 +00:00
|
|
|
$memory = $entry[5] - $entry[3];
|
|
|
|
|
|
2004-01-17 11:31:48 +00:00
|
|
|
if ( !array_key_exists( $fname, $this->mCollated ) ) {
|
|
|
|
|
$this->mCollated[$fname] = 0;
|
|
|
|
|
$this->mCalls[$fname] = 0;
|
2004-08-21 09:25:24 +00:00
|
|
|
$this->mMemory[$fname] = 0;
|
2004-11-23 07:25:22 +00:00
|
|
|
$this->mMin[$fname] = 1 << 24;
|
|
|
|
|
$this->mMax[$fname] = 0;
|
2004-01-17 11:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
2003-10-20 13:39:40 +00:00
|
|
|
$this->mCollated[$fname] += $elapsed;
|
|
|
|
|
$this->mCalls[$fname] ++;
|
2004-08-21 08:27:03 +00:00
|
|
|
$this->mMemory[$fname] += $memory;
|
2004-11-23 07:25:22 +00:00
|
|
|
$this->mMin[$fname] = min( $this->mMin[$fname], $elapsed );
|
|
|
|
|
$this->mMax[$fname] = max( $this->mMax[$fname], $elapsed );
|
2003-10-20 13:39:40 +00:00
|
|
|
}
|
2003-10-25 08:01:33 +00:00
|
|
|
|
2004-08-22 17:24:50 +00:00
|
|
|
$total = @$this->mCollated['-total'];
|
|
|
|
|
$overhead = $this->mCollated['-overhead-internal'] / $profileCount;
|
|
|
|
|
$this->mCalls['-overhead-total'] = $profileCount;
|
2003-10-25 08:01:33 +00:00
|
|
|
|
2003-10-20 13:39:40 +00:00
|
|
|
# Output
|
2004-11-23 07:25:22 +00:00
|
|
|
asort( $this->mCollated, SORT_NUMERIC );
|
2003-10-20 13:39:40 +00:00
|
|
|
foreach ( $this->mCollated as $fname => $elapsed ) {
|
|
|
|
|
$calls = $this->mCalls[$fname];
|
|
|
|
|
# Adjust for overhead
|
2004-08-22 17:24:50 +00:00
|
|
|
if ( $fname[0] != '-' ) {
|
2003-10-20 13:39:40 +00:00
|
|
|
$elapsed -= $overhead * $calls;
|
2003-10-16 13:30:45 +00:00
|
|
|
}
|
2003-11-19 00:48:40 +00:00
|
|
|
|
2003-10-20 13:39:40 +00:00
|
|
|
$percent = $total ? 100. * $elapsed / $total : 0;
|
2004-08-21 08:27:03 +00:00
|
|
|
$memory = $this->mMemory[$fname];
|
2003-10-20 13:39:40 +00:00
|
|
|
$prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
|
2004-11-23 07:25:22 +00:00
|
|
|
(float)($elapsed * 1000) / $calls, $percent, $memory,
|
|
|
|
|
($this->mMin[$fname] * 1000.0),
|
|
|
|
|
($this->mMax[$fname] * 1000.0));
|
2003-11-18 23:52:09 +00:00
|
|
|
|
2003-12-01 00:09:05 +00:00
|
|
|
global $wgProfileToDatabase;
|
2003-11-18 23:52:09 +00:00
|
|
|
if( $wgProfileToDatabase ) {
|
2003-11-19 00:48:40 +00:00
|
|
|
Profiler::logToDB( $fname, (float)($elapsed * 1000), $calls );
|
2003-11-18 23:52:09 +00:00
|
|
|
}
|
2003-10-16 13:30:45 +00:00
|
|
|
}
|
2003-10-20 13:39:40 +00:00
|
|
|
$prof .= "\nTotal: $total\n\n";
|
|
|
|
|
|
|
|
|
|
return $prof;
|
2003-10-16 13:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
2003-11-19 00:48:40 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* @static
|
|
|
|
|
*/
|
|
|
|
|
function logToDB($name, $timeSum, $eventCount) {
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbw =& wfGetDB( DB_MASTER );
|
2004-07-10 03:09:26 +00:00
|
|
|
$profiling = $dbw->tableName( 'profiling' );
|
|
|
|
|
|
2004-09-30 14:40:06 +00:00
|
|
|
$name = substr($name,0,255);
|
|
|
|
|
$encname = $dbw->strencode($name);
|
2004-07-10 03:09:26 +00:00
|
|
|
$sql = "UPDATE $profiling ".
|
2003-11-19 00:48:40 +00:00
|
|
|
"SET pf_count=pf_count+{$eventCount}, ".
|
|
|
|
|
"pf_time=pf_time + {$timeSum} ".
|
2004-09-30 14:40:06 +00:00
|
|
|
"WHERE pf_name='{$encname}'";
|
2004-07-10 03:09:26 +00:00
|
|
|
$dbw->query($sql);
|
2003-11-19 00:48:40 +00:00
|
|
|
|
2004-07-10 03:09:26 +00:00
|
|
|
$rc = $dbw->affectedRows();
|
2003-11-19 00:48:40 +00:00
|
|
|
if( $rc == 0) {
|
2004-10-24 07:10:33 +00:00
|
|
|
$dbw->insert('profiling',array(
|
2004-09-01 12:29:07 +00:00
|
|
|
'pf_name'=>$name,
|
|
|
|
|
'pf_count'=>$eventCount,
|
|
|
|
|
'pf_time'=>$timeSum),
|
|
|
|
|
$fname,array('IGNORE'));
|
2003-11-19 00:48:40 +00:00
|
|
|
}
|
|
|
|
|
// When we upgrade to mysql 4.1, the insert+update
|
|
|
|
|
// can be merged into just a insert with this construct added:
|
|
|
|
|
// "ON DUPLICATE KEY UPDATE ".
|
|
|
|
|
// "pf_count=pf_count + VALUES(pf_count), ".
|
|
|
|
|
// "pf_time=pf_time + VALUES(pf_time)";
|
2003-11-18 23:52:09 +00:00
|
|
|
}
|
2003-11-19 00:48:40 +00:00
|
|
|
|
2003-11-18 23:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
2003-11-19 00:48:40 +00:00
|
|
|
|
2003-10-16 13:30:45 +00:00
|
|
|
$wgProfiler = new Profiler();
|
2004-08-22 17:24:50 +00:00
|
|
|
$wgProfiler->profileIn( '-total' );
|
2003-10-16 13:30:45 +00:00
|
|
|
?>
|