From: Tim Starling Date: Fri, 14 Jul 2006 05:35:31 +0000 (+0000) Subject: Consolidated web initialisation code into includes/WebStart.php. Moved profiling... X-Git-Tag: 1.31.0-rc.0~56260 X-Git-Url: http://git.cyclocoop.org/%7B%24admin_url%7Dmembres/cotisations/gestion/rappel_supprimer.php?a=commitdiff_plain;h=3ea576aa259b9f990e18aa3c58c62036d80c3ce3;p=lhc%2Fweb%2Fwiklou.git Consolidated web initialisation code into includes/WebStart.php. Moved profiling setup to a hook file "StartProfiler.php", following Brion's suggestion to merge Wikimedia's early profiling patch into subversion. Renamed Profiling.php and logProfilingData(), removed unnecessary wfProfileClose() calls. --- diff --git a/StartProfiler.php b/StartProfiler.php new file mode 100644 index 0000000000..ff51fbf13c --- /dev/null +++ b/StartProfiler.php @@ -0,0 +1,22 @@ + diff --git a/img_auth.php b/img_auth.php index fb58ba2823..111fbdf747 100644 --- a/img_auth.php +++ b/img_auth.php @@ -7,18 +7,9 @@ * to an array of pages you want everyone to be able to access. Your server must * support PATH_INFO, CGI-based configurations generally don't. */ -# Valid web server entry point, enable includes -define( 'MEDIAWIKI', true ); - -if ( isset( $_REQUEST['GLOBALS'] ) ) { - echo '$GLOBALS overwrite vulnerability'; - die( -1 ); -} - -require_once( 'includes/Defines.php' ); -require_once( './LocalSettings.php' ); -require_once( 'includes/Setup.php' ); -require_once( 'includes/StreamFile.php' ); +require_once( './includes/WebStart.php' ); +wfProfileIn( 'img_auth.php' ); +require_once( './includes/StreamFile.php' ); if( !isset( $_SERVER['PATH_INFO'] ) ) { wfForbidden(); @@ -47,6 +38,7 @@ if( is_dir( $filename ) ) { # Write file wfStreamFile( $filename ); +wfLogProfilingData(); function wfForbidden() { header( 'HTTP/1.0 403 Forbidden' ); @@ -55,6 +47,7 @@ function wfForbidden() {

Access denied

You need to log in to access files on this server

"; + wfLogProfilingData(); exit; } diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 783570f9d4..f2082f82cd 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -122,7 +122,7 @@ function __autoload($className) { 'ParserCache' => 'includes/ParserCache.php', 'ProfilerSimple' => 'includes/ProfilerSimple.php', 'ProfilerSimpleUDP' => 'includes/ProfilerSimpleUDP.php', - 'Profiler' => 'includes/Profiling.php', + 'Profiler' => 'includes/Profiler.php', 'ProxyTools' => 'includes/ProxyTools.php', 'ProtectionForm' => 'includes/ProtectionForm.php', 'QueryPage' => 'includes/QueryPage.php', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 7148555ec9..9e89efdcb5 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -1153,21 +1153,17 @@ $wgTexvc = './math/texvc'; # # You have to create a 'profiling' table in your database before using # profiling see maintenance/archives/patch-profiling.sql . +# +# To enable profiling, edit StartProfiler.php -/** Enable for more detailed by-function times in debug log */ -$wgProfiling = false; /** Only record profiling info for pages that took longer than this */ $wgProfileLimit = 0.0; /** Don't put non-profiling info into log file */ $wgProfileOnly = false; /** Log sums from profiling into "profiling" table in db. */ $wgProfileToDatabase = false; -/** Only profile every n requests when profiling is turned on */ -$wgProfileSampleRate = 1; /** If true, print a raw call tree instead of per-function report */ $wgProfileCallTree = false; -/** If not empty, specifies profiler type to load */ -$wgProfilerType = ''; /** Should application server host be put into profiling table */ $wgProfilePerHost = false; diff --git a/includes/Exception.php b/includes/Exception.php index eeb45594a0..9b046117f9 100644 --- a/includes/Exception.php +++ b/includes/Exception.php @@ -181,8 +181,7 @@ function wfExceptionHandler( $e ) { // Final cleanup, similar to wfErrorExit() if ( $wgFullyInitialised ) { try { - wfProfileClose(); - logProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition + wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition } catch ( Exception $e ) {} } diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index c8a1a605a5..a50e3e1da5 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -243,13 +243,12 @@ function wfLogDBError( $text ) { /** * @todo document */ -function logProfilingData() { +function wfLogProfilingData() { global $wgRequestTime, $wgDebugLogFile, $wgDebugRawPage, $wgRequest; global $wgProfiling, $wgUser; - $now = wfTime(); - - $elapsed = $now - $wgRequestTime; if ( $wgProfiling ) { + $now = wfTime(); + $elapsed = $now - $wgRequestTime; $prof = wfGetProfilingOutput( $wgRequestTime, $elapsed ); $forward = ''; if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) @@ -621,8 +620,7 @@ function wfAbruptExit( $error = false ){ wfDebug('WARNING: Abrupt exit\n'); } - wfProfileClose(); - logProfilingData(); + wfLogProfilingData(); if ( !$error ) { $wgLoadBalancer->closeAll(); diff --git a/includes/Profiler.php b/includes/Profiler.php new file mode 100644 index 0000000000..9fa26e20b2 --- /dev/null +++ b/includes/Profiler.php @@ -0,0 +1,355 @@ +profileIn($functionname); +} + +/** + * @param $functioname name of the function we have profiled + */ +function wfProfileOut($functionname = 'missing') { + global $wgProfiler; + $wgProfiler->profileOut($functionname); +} + +function wfGetProfilingOutput($start, $elapsed) { + global $wgProfiler; + return $wgProfiler->getOutput($start, $elapsed); +} + +function wfProfileClose() { + global $wgProfiler; + $wgProfiler->close(); +} + +if (!function_exists('memory_get_usage')) { + # Old PHP or --enable-memory-limit not compiled in + function memory_get_usage() { + return 0; + } +} + +/** + * @todo document + * @package MediaWiki + */ +class Profiler { + var $mStack = array (), $mWorkStack = array (), $mCollated = array (); + var $mCalls = array (), $mTotals = array (); + + function Profiler() + { + // 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(true), 0 ); + } else { + $this->profileIn( '-total' ); + } + + } + + function profileIn($functionname) { + global $wgDebugFunctionEntry; + if ($wgDebugFunctionEntry && function_exists('wfDebug')) { + wfDebug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n"); + } + $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), $this->getTime(), memory_get_usage()); + } + + function profileOut($functionname) { + $memory = memory_get_usage(); + $time = $this->getTime(); + + global $wgDebugFunctionEntry; + + if ($wgDebugFunctionEntry && function_exists('wfDebug')) { + wfDebug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n"); + } + + $bit = array_pop($this->mWorkStack); + + if (!$bit) { + wfDebug("Profiling error, !\$bit: $functionname\n"); + } else { + //if ($wgDebugProfiling) { + if ($functionname == 'close') { + $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)"; + wfDebug( "$message\n" ); + $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 ); + } + //} + $bit[] = $time; + $bit[] = $memory; + $this->mStack[] = $bit; + } + } + + function close() { + while (count($this->mWorkStack)) { + $this->profileOut('close'); + } + } + + function getOutput() { + global $wgDebugFunctionEntry; + $wgDebugFunctionEntry = false; + + if (!count($this->mStack) && !count($this->mCollated)) { + return "No profiling output\n"; + } + $this->close(); + + global $wgProfileCallTree; + if ($wgProfileCallTree) { + return $this->getCallTree(); + } else { + return $this->getFunctionReport(); + } + } + + function getCallTree($start = 0) { + return implode('', array_map(array (& $this, 'getCallTreeLine'), $this->remapCallTree($this->mStack))); + } + + function remapCallTree($stack) { + if (count($stack) < 2) { + return $stack; + } + $outputs = array (); + for ($max = count($stack) - 1; $max > 0;) { + /* Find all items under this entry */ + $level = $stack[$max][1]; + $working = array (); + for ($i = $max -1; $i >= 0; $i --) { + if ($stack[$i][1] > $level) { + $working[] = $stack[$i]; + } else { + break; + } + } + $working = $this->remapCallTree(array_reverse($working)); + $output = array (); + foreach ($working as $item) { + array_push($output, $item); + } + array_unshift($output, $stack[$max]); + $max = $i; + + array_unshift($outputs, $output); + } + $final = array (); + foreach ($outputs as $output) { + foreach ($output as $item) { + $final[] = $item; + } + } + return $final; + } + + function getCallTreeLine($entry) { + list ($fname, $level, $start, $x, $end) = $entry; + $delta = $end - $start; + $space = str_repeat(' ', $level); + + # The ugly double sprintf is to work around a PHP bug, + # which has been fixed in recent releases. + return sprintf( "%10s %s %s\n", + trim( sprintf( "%7.3f", $delta * 1000.0 ) ), + $space, $fname ); + } + + function getTime() { + return microtime(true); + #return $this->getUserTime(); + } + + function getUserTime() { + $ru = getrusage(); + return $ru['ru_utime.tv_sec'].' '.$ru['ru_utime.tv_usec'] / 1e6; + } + + function getFunctionReport() { + $width = 140; + $nameWidth = $width - 65; + $format = "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d (%13.3f -%13.3f) [%d]\n"; + $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s\n"; + $prof = "\nProfiling data\n"; + $prof .= sprintf($titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem'); + $this->mCollated = array (); + $this->mCalls = array (); + $this->mMemory = array (); + + # Estimate profiling overhead + $profileCount = count($this->mStack); + wfProfileIn('-overhead-total'); + for ($i = 0; $i < $profileCount; $i ++) { + wfProfileIn('-overhead-internal'); + wfProfileOut('-overhead-internal'); + } + wfProfileOut('-overhead-total'); + + # First, subtract the overhead! + foreach ($this->mStack as $entry) { + $fname = $entry[0]; + $thislevel = $entry[1]; + $start = $entry[2]; + $end = $entry[4]; + $elapsed = $end - $start; + $memory = $entry[5] - $entry[3]; + + if ($fname == '-overhead-total') { + $overheadTotal[] = $elapsed; + $overheadMemory[] = $memory; + } + elseif ($fname == '-overhead-internal') { + $overheadInternal[] = $elapsed; + } + } + $overheadTotal = array_sum($overheadTotal) / count($overheadInternal); + $overheadMemory = array_sum($overheadMemory) / count($overheadInternal); + $overheadInternal = array_sum($overheadInternal) / count($overheadInternal); + + # Collate + foreach ($this->mStack as $index => $entry) { + $fname = $entry[0]; + $thislevel = $entry[1]; + $start = $entry[2]; + $end = $entry[4]; + $elapsed = $end - $start; + + $memory = $entry[5] - $entry[3]; + $subcalls = $this->calltreeCount($this->mStack, $index); + + if (!preg_match('/^-overhead/', $fname)) { + # 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)) { + $this->mCollated[$fname] = 0; + $this->mCalls[$fname] = 0; + $this->mMemory[$fname] = 0; + $this->mMin[$fname] = 1 << 24; + $this->mMax[$fname] = 0; + $this->mOverhead[$fname] = 0; + } + + $this->mCollated[$fname] += $elapsed; + $this->mCalls[$fname]++; + $this->mMemory[$fname] += $memory; + $this->mMin[$fname] = min($this->mMin[$fname], $elapsed); + $this->mMax[$fname] = max($this->mMax[$fname], $elapsed); + $this->mOverhead[$fname] += $subcalls; + } + + $total = @ $this->mCollated['-total']; + $this->mCalls['-overhead-total'] = $profileCount; + + # Output + arsort($this->mCollated, SORT_NUMERIC); + foreach ($this->mCollated as $fname => $elapsed) { + $calls = $this->mCalls[$fname]; + $percent = $total ? 100. * $elapsed / $total : 0; + $memory = $this->mMemory[$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) { + Profiler :: logToDB($fname, (float) ($elapsed * 1000), $calls); + } + } + $prof .= "\nTotal: $total\n\n"; + + return $prof; + } + + /** + * Counts the number of profiled function calls sitting under + * the given point in the call graph. Not the most efficient algo. + * + * @param $stack Array: + * @param $start Integer: + * @return Integer + * @private + */ + function calltreeCount(& $stack, $start) { + $level = $stack[$start][1]; + $count = 0; + for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) { + $count ++; + } + return $count; + } + + /** + * @static + */ + function logToDB($name, $timeSum, $eventCount) { + # Warning: $wguname is a live patch, it should be moved to Setup.php + global $wguname, $wgProfilePerHost; + + $fname = 'Profiler::logToDB'; + $dbw = & wfGetDB(DB_MASTER); + if (!is_object($dbw)) + return false; + $errorState = $dbw->ignoreErrors( true ); + $profiling = $dbw->tableName('profiling'); + + $name = substr($name, 0, 255); + $encname = $dbw->strencode($name); + + if ($wgProfilePerHost) { + $pfhost = $wguname['nodename']; + } else { + $pfhost = ''; + } + + $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} ". + "WHERE pf_name='{$encname}' AND pf_server='{$pfhost}'"; + $dbw->query($sql); + + $rc = $dbw->affectedRows(); + if ($rc == 0) { + $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount, + 'pf_time' => $timeSum, 'pf_server' => $pfhost ), $fname, array ('IGNORE')); + } + // 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)"; + $dbw->ignoreErrors( $errorState ); + } + + /** + * Get the function name of the current profiling section + */ + function getCurrentSection() { + $elt = end($this->mWorkStack); + return $elt[0]; + } + +} + +?> diff --git a/includes/ProfilerSimple.php b/includes/ProfilerSimple.php index ed058c65cd..777a10fd67 100644 --- a/includes/ProfilerSimple.php +++ b/includes/ProfilerSimple.php @@ -8,7 +8,7 @@ * @todo document * @package MediaWiki */ -require_once(dirname(__FILE__).'/Profiling.php'); +require_once(dirname(__FILE__).'/Profiler.php'); class ProfilerSimple extends Profiler { function ProfilerSimple() { @@ -86,9 +86,14 @@ class ProfilerSimple extends Profiler { } function getCpuTime($ru=null) { - if ($ru==null) - $ru=getrusage(); - return ($ru['ru_utime.tv_sec']+$ru['ru_stime.tv_sec']+($ru['ru_utime.tv_usec']+$ru['ru_stime.tv_usec'])*1e-6); + if ( function_exists( getrusage() ) { + if ( $ru == null ) + $ru = getrusage(); + return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] + + $ru['ru_stime.tv_usec']) * 1e-6); + } else { + return 0; + } } /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */ diff --git a/includes/ProfilerSimpleUDP.php b/includes/ProfilerSimpleUDP.php index c395228b08..5c8da1362b 100644 --- a/includes/ProfilerSimpleUDP.php +++ b/includes/ProfilerSimpleUDP.php @@ -3,7 +3,7 @@ (the one from wikipedia/udpprofile CVS ) */ -require_once(dirname(__FILE__).'/Profiling.php'); +require_once(dirname(__FILE__).'/Profiler.php'); require_once(dirname(__FILE__).'/ProfilerSimple.php'); class ProfilerSimpleUDP extends ProfilerSimple { diff --git a/includes/ProfilerStub.php b/includes/ProfilerStub.php index 3bcdaab2af..4cf0aa4494 100644 --- a/includes/ProfilerStub.php +++ b/includes/ProfilerStub.php @@ -21,6 +21,6 @@ function wfProfileOut( $fn = '' ) { } function wfGetProfilingOutput( $s, $e ) {} function wfProfileClose() {} -function wfLogProfilingData() {} +$wgProfiling = false; ?> diff --git a/includes/Profiling.php b/includes/Profiling.php deleted file mode 100644 index edecc4f32f..0000000000 --- a/includes/Profiling.php +++ /dev/null @@ -1,353 +0,0 @@ -profileIn($functionname); -} - -/** - * @param $functioname name of the function we have profiled - */ -function wfProfileOut($functionname = 'missing') { - global $wgProfiler; - $wgProfiler->profileOut($functionname); -} - -function wfGetProfilingOutput($start, $elapsed) { - global $wgProfiler; - return $wgProfiler->getOutput($start, $elapsed); -} - -function wfProfileClose() { - global $wgProfiler; - $wgProfiler->close(); -} - -if (!function_exists('memory_get_usage')) { - # Old PHP or --enable-memory-limit not compiled in - function memory_get_usage() { - return 0; - } -} - -/** - * @todo document - * @package MediaWiki - */ -class Profiler { - var $mStack = array (), $mWorkStack = array (), $mCollated = array (); - var $mCalls = array (), $mTotals = array (); - - function Profiler() - { - // 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(true), 0 ); - } else { - $this->profileIn( '-total' ); - } - - } - - function profileIn($functionname) { - global $wgDebugFunctionEntry; - if ($wgDebugFunctionEntry && function_exists('wfDebug')) { - wfDebug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n"); - } - $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), $this->getTime(), memory_get_usage()); - } - - function profileOut($functionname) { - $memory = memory_get_usage(); - $time = $this->getTime(); - - global $wgDebugFunctionEntry; - - if ($wgDebugFunctionEntry && function_exists('wfDebug')) { - wfDebug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n"); - } - - $bit = array_pop($this->mWorkStack); - - if (!$bit) { - wfDebug("Profiling error, !\$bit: $functionname\n"); - } else { - //if ($wgDebugProfiling) { - if ($functionname == 'close') { - $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)"; - wfDebug( "$message\n" ); - $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 ); - } - //} - $bit[] = $time; - $bit[] = $memory; - $this->mStack[] = $bit; - } - } - - function close() { - while (count($this->mWorkStack)) { - $this->profileOut('close'); - } - } - - function getOutput() { - global $wgDebugFunctionEntry; - $wgDebugFunctionEntry = false; - - if (!count($this->mStack) && !count($this->mCollated)) { - return "No profiling output\n"; - } - $this->close(); - - global $wgProfileCallTree; - if ($wgProfileCallTree) { - return $this->getCallTree(); - } else { - return $this->getFunctionReport(); - } - } - - function getCallTree($start = 0) { - return implode('', array_map(array (& $this, 'getCallTreeLine'), $this->remapCallTree($this->mStack))); - } - - function remapCallTree($stack) { - if (count($stack) < 2) { - return $stack; - } - $outputs = array (); - for ($max = count($stack) - 1; $max > 0;) { - /* Find all items under this entry */ - $level = $stack[$max][1]; - $working = array (); - for ($i = $max -1; $i >= 0; $i --) { - if ($stack[$i][1] > $level) { - $working[] = $stack[$i]; - } else { - break; - } - } - $working = $this->remapCallTree(array_reverse($working)); - $output = array (); - foreach ($working as $item) { - array_push($output, $item); - } - array_unshift($output, $stack[$max]); - $max = $i; - - array_unshift($outputs, $output); - } - $final = array (); - foreach ($outputs as $output) { - foreach ($output as $item) { - $final[] = $item; - } - } - return $final; - } - - function getCallTreeLine($entry) { - list ($fname, $level, $start, $x, $end) = $entry; - $delta = $end - $start; - $space = str_repeat(' ', $level); - - # The ugly double sprintf is to work around a PHP bug, - # which has been fixed in recent releases. - return sprintf( "%10s %s %s\n", - trim( sprintf( "%7.3f", $delta * 1000.0 ) ), - $space, $fname ); - } - - function getTime() { - return microtime(true); - #return $this->getUserTime(); - } - - function getUserTime() { - $ru = getrusage(); - return $ru['ru_utime.tv_sec'].' '.$ru['ru_utime.tv_usec'] / 1e6; - } - - function getFunctionReport() { - $width = 140; - $nameWidth = $width - 65; - $format = "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d (%13.3f -%13.3f) [%d]\n"; - $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s\n"; - $prof = "\nProfiling data\n"; - $prof .= sprintf($titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem'); - $this->mCollated = array (); - $this->mCalls = array (); - $this->mMemory = array (); - - # Estimate profiling overhead - $profileCount = count($this->mStack); - wfProfileIn('-overhead-total'); - for ($i = 0; $i < $profileCount; $i ++) { - wfProfileIn('-overhead-internal'); - wfProfileOut('-overhead-internal'); - } - wfProfileOut('-overhead-total'); - - # First, subtract the overhead! - foreach ($this->mStack as $entry) { - $fname = $entry[0]; - $thislevel = $entry[1]; - $start = $entry[2]; - $end = $entry[4]; - $elapsed = $end - $start; - $memory = $entry[5] - $entry[3]; - - if ($fname == '-overhead-total') { - $overheadTotal[] = $elapsed; - $overheadMemory[] = $memory; - } - elseif ($fname == '-overhead-internal') { - $overheadInternal[] = $elapsed; - } - } - $overheadTotal = array_sum($overheadTotal) / count($overheadInternal); - $overheadMemory = array_sum($overheadMemory) / count($overheadInternal); - $overheadInternal = array_sum($overheadInternal) / count($overheadInternal); - - # Collate - foreach ($this->mStack as $index => $entry) { - $fname = $entry[0]; - $thislevel = $entry[1]; - $start = $entry[2]; - $end = $entry[4]; - $elapsed = $end - $start; - - $memory = $entry[5] - $entry[3]; - $subcalls = $this->calltreeCount($this->mStack, $index); - - if (!preg_match('/^-overhead/', $fname)) { - # 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)) { - $this->mCollated[$fname] = 0; - $this->mCalls[$fname] = 0; - $this->mMemory[$fname] = 0; - $this->mMin[$fname] = 1 << 24; - $this->mMax[$fname] = 0; - $this->mOverhead[$fname] = 0; - } - - $this->mCollated[$fname] += $elapsed; - $this->mCalls[$fname]++; - $this->mMemory[$fname] += $memory; - $this->mMin[$fname] = min($this->mMin[$fname], $elapsed); - $this->mMax[$fname] = max($this->mMax[$fname], $elapsed); - $this->mOverhead[$fname] += $subcalls; - } - - $total = @ $this->mCollated['-total']; - $this->mCalls['-overhead-total'] = $profileCount; - - # Output - asort($this->mCollated, SORT_NUMERIC); - foreach ($this->mCollated as $fname => $elapsed) { - $calls = $this->mCalls[$fname]; - $percent = $total ? 100. * $elapsed / $total : 0; - $memory = $this->mMemory[$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) { - Profiler :: logToDB($fname, (float) ($elapsed * 1000), $calls); - } - } - $prof .= "\nTotal: $total\n\n"; - - return $prof; - } - - /** - * Counts the number of profiled function calls sitting under - * the given point in the call graph. Not the most efficient algo. - * - * @param $stack Array: - * @param $start Integer: - * @return Integer - * @private - */ - function calltreeCount(& $stack, $start) { - $level = $stack[$start][1]; - $count = 0; - for ($i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i --) { - $count ++; - } - return $count; - } - - /** - * @static - */ - function logToDB($name, $timeSum, $eventCount) { - # Warning: $wguname is a live patch, it should be moved to Setup.php - global $wguname, $wgProfilePerHost; - - $fname = 'Profiler::logToDB'; - $dbw = & wfGetDB(DB_MASTER); - if (!is_object($dbw)) - return false; - $errorState = $dbw->ignoreErrors( true ); - $profiling = $dbw->tableName('profiling'); - - $name = substr($name, 0, 255); - $encname = $dbw->strencode($name); - - if ($wgProfilePerHost) { - $pfhost = $wguname['nodename']; - } else { - $pfhost = ''; - } - - $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} ". - "WHERE pf_name='{$encname}' AND pf_server='{$pfhost}'"; - $dbw->query($sql); - - $rc = $dbw->affectedRows(); - if ($rc == 0) { - $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount, - 'pf_time' => $timeSum, 'pf_server' => $pfhost ), $fname, array ('IGNORE')); - } - // 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)"; - $dbw->ignoreErrors( $errorState ); - } - - /** - * Get the function name of the current profiling section - */ - function getCurrentSection() { - $elt = end($this->mWorkStack); - return $elt[0]; - } - -} - -?> diff --git a/includes/Setup.php b/includes/Setup.php index 381ea1b61b..6b2f5841c1 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -8,7 +8,10 @@ * This file is not a valid entry point, perform no further processing unless * MEDIAWIKI is defined */ -if( defined( 'MEDIAWIKI' ) ) { +if( !defined( 'MEDIAWIKI' ) ) { + echo "This file is part of MediaWiki, it is not a valid entry point.\n"; + exit( 1 ); +} # The main wiki script and things like database # conversion and maintenance scripts all share a @@ -16,35 +19,17 @@ if( defined( 'MEDIAWIKI' ) ) { # setting up a few globals. # +$fname = 'Setup.php'; +wfProfileIn( $fname ); + // Check to see if we are at the file scope if ( !isset( $wgVersion ) ) { echo "Error, Setup.php must be included from the file scope, after DefaultSettings.php\n"; die( 1 ); } -if( !isset( $wgProfiling ) ) - $wgProfiling = false; - require_once( "$IP/includes/AutoLoader.php" ); -if ( function_exists( 'wfProfileIn' ) ) { - /* nada, everything should be done already */ -} elseif ( $wgProfiling and (0 == rand() % $wgProfileSampleRate ) ) { - $wgProfiling = true; - if ($wgProfilerType == "") { - $wgProfiler = new Profiler(); - } else { - $prclass="Profiler{$wgProfilerType}"; - require_once( $prclass.".php" ); - $wgProfiler = new $prclass(); - } -} else { - require_once( "$IP/includes/ProfilerStub.php" ); -} - -$fname = 'Setup.php'; -wfProfileIn( $fname ); - wfProfileIn( $fname.'-exception' ); require_once( "$IP/includes/Exception.php" ); wfInstallExceptionHandler(); @@ -323,5 +308,4 @@ $wgFullyInitialised = true; wfProfileOut( $fname.'-extensions' ); wfProfileOut( $fname ); -} ?> diff --git a/includes/WebStart.php b/includes/WebStart.php new file mode 100644 index 0000000000..fc6d9695c3 --- /dev/null +++ b/includes/WebStart.php @@ -0,0 +1,52 @@ +$GLOBALS overwrite vulnerability'); +} + +# Valid web server entry point, enable includes. +# Please don't move this line to includes/Defines.php. This line essentially +# defines a valid entry point. If you put it in includes/Defines.php, then +# any script that includes it becomes an entry point, thereby defeating +# its purpose. +define( 'MEDIAWIKI', true ); + +# Start profiler +require_once( './StartProfiler.php' ); +wfProfileIn( 'WebStart.php-conf' ); + +# Load up some global defines. +require_once( './includes/Defines.php' ); + +# LocalSettings.php is the per site customization file. If it does not exit +# the wiki installer need to be launched or the generated file moved from +# ./config/ to ./ +if( !file_exists( './LocalSettings.php' ) ) { + $IP = '.'; + require_once( './includes/DefaultSettings.php' ); # used for printing the version + require_once( './includes/templates/NoLocalSettings.php' ); + die(); +} + +# Include this site setttings +require_once( './LocalSettings.php' ); +wfProfileOut( 'WebStart.php-conf' ); + +if ( !defined( 'MW_NO_SETUP' ) ) { + require_once( './includes/Setup.php' ); +} +?> diff --git a/includes/Wiki.php b/includes/Wiki.php index 6f01000382..d2f51f851d 100644 --- a/includes/Wiki.php +++ b/includes/Wiki.php @@ -302,8 +302,7 @@ class MediaWiki { * Ends this task peacefully */ function restInPeace ( &$loadBalancer ) { - wfProfileClose(); - logProfilingData(); + wfLogProfilingData(); $loadBalancer->closeAll(); wfDebug( "Request ended normally\n" ); } diff --git a/index.php b/index.php index f18fa8a6ed..00a29f2151 100644 --- a/index.php +++ b/index.php @@ -1,83 +1,7 @@ $GLOBALS overwrite vulnerability'); -} - -# Valid web server entry point, enable includes. -# Please don't move this line to includes/Defines.php. This line essentially -# defines a valid entry point. If you put it in includes/Defines.php, then -# any script that includes it becomes an entry point, thereby defeating -# its purpose. -define( 'MEDIAWIKI', true ); - -# Load up some global defines. -require_once( './includes/Defines.php' ); - -# LocalSettings.php is the per site customization file. If it does not exit -# the wiki installer need to be launched or the generated file moved from -# ./config/ to ./ -if( !file_exists( 'LocalSettings.php' ) ) { - $IP = '.'; - require_once( 'includes/DefaultSettings.php' ); # used for printing the version -?> - - - - MediaWiki <?php echo $wgVersion ?> - - - - - The MediaWiki logo - -

MediaWiki

-
- config/LocalSettings.php to the parent directory.' ); - } else { - echo( 'Please setup the wiki first.' ); - } - ?> - -
- - -$GLOBALS overwrite vulnerability'; - die( -1 ); -} - -require_once("./includes/Defines.php"); -require_once("./LocalSettings.php"); +define( 'MW_NO_SETUP', 1 ); +require_once( './includes/WebStart.php' ); require_once("./AdminSettings.php"); if (!$wgEnableProfileInfo) { diff --git a/redirect.php b/redirect.php index c0e8746ef6..4718600d6c 100644 --- a/redirect.php +++ b/redirect.php @@ -1,15 +1,6 @@ $GLOBALS overwrite vulnerability'; - die( -1 ); -} - -require_once( './includes/Defines.php' ); -require_once( './LocalSettings.php' ); +define( 'MW_NO_SETUP', 1 ); +require_once( './includes/WebStart.php' ); global $wgArticlePath; require_once( 'includes/WebRequest.php' ); diff --git a/thumb.php b/thumb.php index 54ec64cf00..d4e561e69e 100644 --- a/thumb.php +++ b/thumb.php @@ -4,18 +4,10 @@ * PHP script to stream out an image thumbnail. * If the file exists, we make do with abridged MediaWiki initialisation. */ - -define( 'MEDIAWIKI', true ); -unset( $IP ); -if ( isset( $_REQUEST['GLOBALS'] ) ) { - echo '$GLOBALS overwrite vulnerability'; - die( -1 ); -} - -define( 'MW_NO_OUTPUT_BUFFER', true ); - -require_once( './includes/Defines.php' ); -require_once( './LocalSettings.php' ); +define( 'MW_NO_SETUP', 1 ); +require_once( './includes/WebStart.php' ); +wfProfileIn( 'thumb.php' ); +wfProfileIn( 'thumb.php-start' ); require_once( 'GlobalFunctions.php' ); require_once( 'ImageFunctions.php' ); @@ -52,12 +44,14 @@ $thumbPath = wfImageThumbDir( $fileName ) . '/' . $thumbName; if ( is_file( $thumbPath ) && filemtime( $thumbPath ) >= filemtime( $imagePath ) ) { wfStreamFile( $thumbPath ); + // Can't log profiling data with no Setup.php exit; } // OK, no valid thumbnail, time to get out the heavy machinery +wfProfileOut( 'thumb.php-start' ); require_once( 'Setup.php' ); -wfProfileIn( 'thumb.php' ); +wfProfileIn( 'thumb.php-render' ); $img = Image::newFromName( $fileName ); if ( $img ) { @@ -79,7 +73,8 @@ if ( $thumb && $thumb->path ) { "; } +wfProfileOut( 'thumb.php-render' ); wfProfileOut( 'thumb.php' ); - +wfLogProfilingData(); ?> diff --git a/trackback.php b/trackback.php index 274a1c82d7..ae1e32b25a 100644 --- a/trackback.php +++ b/trackback.php @@ -4,21 +4,7 @@ * @package MediaWiki * @subpackage SpecialPage */ - -unset($IP); -define('MEDIAWIKI', true); -if ( isset( $_REQUEST['GLOBALS'] ) ) { - echo '$GLOBALS overwrite vulnerability'; - die( -1 ); -} - -require_once('./includes/Defines.php'); - -if (!file_exists('LocalSettings.php')) - exit; - -require_once('./LocalSettings.php'); -require_once('includes/Setup.php'); +require_once( './includes/WebStart.php' ); require_once('DatabaseFunctions.php');