Do *NOT* force all wikis into a non-functional initial state with a broken whitelist...
[mediawiki.git] / includes / Profiling.php
blobe7e089ac6ce372aa42a5c1cffc395f1ad2b69a29
1 <?php
2 /**
3 * This file is only included if profiling is enabled
4 * @package MediaWiki
5 */
7 /**
8 * @param $functioname name of the function we will profile
9 */
10 function wfProfileIn( $functionname ) {
11 global $wgProfiler;
12 $wgProfiler->profileIn( $functionname );
15 /**
16 * @param $functioname name of the function we have profiled
18 function wfProfileOut( $functionname = 'missing' ) {
19 global $wgProfiler;
20 $wgProfiler->profileOut( $functionname );
23 function wfGetProfilingOutput( $start, $elapsed ) {
24 global $wgProfiler;
25 return $wgProfiler->getOutput( $start, $elapsed );
28 function wfProfileClose() {
29 global $wgProfiler;
30 $wgProfiler->close();
33 if( !function_exists( 'memory_get_usage' ) ) {
34 # Old PHP or --enable-memory-limit not compiled in
35 function memory_get_usage() {
36 return 0;
40 /**
41 * @todo document
42 * @package MediaWiki
44 class Profiler
46 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
47 var $mCalls = array(), $mTotals = array();
49 function Profiler()
51 $this->mProfileStack = array();
52 $this->mWorkStack = array();
53 $this->mCollated = array();
57 function profileIn( $functionname ) {
58 global $wgDebugFunctionEntry;
59 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
60 wfDebug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering '.$functionname."\n" );
62 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(), memory_get_usage() );
65 function profileOut( $functionname ) {
66 $memory = memory_get_usage();
67 $time = microtime();
68 global $wgDebugProfiling, $wgDebugFunctionEntry;
70 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
71 wfDebug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Exiting '.$functionname."\n" );
74 $bit = array_pop( $this->mWorkStack );
76 if ( !$bit ) {
77 wfDebug( "Profiling error, !\$bit: $functionname\n" );
78 } else {
79 if ( $wgDebugProfiling ) {
80 if ( $functionname == 'close' ) {
81 wfDebug( "Profile section ended by close(): {$bit[0]}\n" );
82 } elseif ( $bit[0] != $functionname ) {
83 wfDebug( "Profiling error: in({$bit[0]}), out($functionname)\n" );
86 $bit[] = $time;
87 $bit[] = $memory;
88 $this->mStack[] = $bit;
92 function close() {
93 while ( count( $this->mWorkStack ) ) {
94 $this->profileOut( 'close' );
98 function getOutput() {
99 global $wgDebugFunctionEntry;
100 $wgDebugFunctionEntry = false;
102 if( !count( $this->mStack ) ) {
103 return "No profiling output\n";
105 $this->close();
106 $width = 125;
107 $format = "%-" . ($width - 34) . "s %6d %6.3f %6.3f %7.3f%% %6d (%6.3f-%6.3f) [%d]\n";
108 $titleFormat = "%-" . ($width - 34) . "s %9s %9s %9s %9s %6s\n";
109 $prof = "\nProfiling data\n";
110 $prof .= sprintf( $titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem' );
111 $this->mCollated = array();
112 $this->mCalls = array();
113 $this->mMemory = array();
115 # Estimate profiling overhead
116 $profileCount = count( $this->mStack );
117 wfProfileIn( '-overhead-total' );
118 for ($i=0; $i<$profileCount ; $i++) {
119 wfProfileIn( '-overhead-internal' );
120 wfProfileOut( '-overhead-internal' );
122 wfProfileOut( '-overhead-total' );
124 # First, subtract the overhead!
125 foreach( $this->mStack as $entry ) {
126 $fname = $entry[0];
127 $thislevel = $entry[1];
128 $start = explode( ' ', $entry[2]);
129 $start = (float)$start[0] + (float)$start[1];
130 $end = explode( ' ', $entry[4]);
131 $end = (float)$end[0] + (float)$end[1];
132 $elapsed = $end - $start;
133 $memory = $entry[5] - $entry[3];
135 if( $fname == '-overhead-total' ) {
136 $overheadTotal[] = $elapsed;
137 $overheadMemory[] = $memory;
138 } elseif( $fname == '-overhead-internal' ) {
139 $overheadInternal[] = $elapsed;
142 $overheadTotal = array_sum( $overheadTotal ) / count( $overheadInternal );
143 $overheadMemory = array_sum( $overheadMemory ) / count( $overheadInternal );
144 $overheadInternal = array_sum( $overheadInternal ) / count( $overheadInternal );
146 # Collate
147 foreach ( $this->mStack as $index => $entry ) {
148 $fname = $entry[0];
149 $thislevel = $entry[1];
150 $start = explode( ' ', $entry[2]);
151 $start = (float)$start[0] + (float)$start[1];
152 $end = explode( ' ', $entry[4]);
153 $end = (float)$end[0] + (float)$end[1];
154 $elapsed = $end - $start;
156 $memory = $entry[5] - $entry[3];
157 $subcalls = $this->calltreeCount( $this->mStack, $index );
159 if( !preg_match( '/^-overhead/', $fname ) ) {
160 # Adjust for profiling overhead
161 $elapsed -= $overheadInternal;
162 $elapsed -= ($subcalls * $overheadTotal);
163 $memory -= ($subcalls * $overheadMemory);
166 if ( !array_key_exists( $fname, $this->mCollated ) ) {
167 $this->mCollated[$fname] = 0;
168 $this->mCalls[$fname] = 0;
169 $this->mMemory[$fname] = 0;
170 $this->mMin[$fname] = 1 << 24;
171 $this->mMax[$fname] = 0;
172 $this->mOverhead[$fname] = 0;
175 $this->mCollated[$fname] += $elapsed;
176 $this->mCalls[$fname] ++;
177 $this->mMemory[$fname] += $memory;
178 $this->mMin[$fname] = min( $this->mMin[$fname], $elapsed );
179 $this->mMax[$fname] = max( $this->mMax[$fname], $elapsed );
180 $this->mOverhead[$fname] += $subcalls;
183 $total = @$this->mCollated['-total'];
184 $this->mCalls['-overhead-total'] = $profileCount;
186 # Output
187 asort( $this->mCollated, SORT_NUMERIC );
188 foreach ( $this->mCollated as $fname => $elapsed ) {
189 $calls = $this->mCalls[$fname];
190 $percent = $total ? 100. * $elapsed / $total : 0;
191 $memory = $this->mMemory[$fname];
192 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
193 (float)($elapsed * 1000) / $calls, $percent, $memory,
194 ($this->mMin[$fname] * 1000.0),
195 ($this->mMax[$fname] * 1000.0),
196 $this->mOverhead[$fname] );
198 global $wgProfileToDatabase;
199 if( $wgProfileToDatabase ) {
200 Profiler::logToDB( $fname, (float)($elapsed * 1000), $calls );
203 $prof .= "\nTotal: $total\n\n";
205 return $prof;
209 * Counts the number of profiled function calls sitting under
210 * the given point in the call graph. Not the most efficient algo.
212 * @param array $stack
213 * @param int $start
214 * @return int
215 * @access private
217 function calltreeCount( &$stack, $start ) {
218 $level = $stack[$start][1];
219 $count = 0;
220 for( $i = $start - 1; $i >= 0 && $stack[$i][1] > $level; $i-- ) {
221 $count++;
223 return $count;
227 * @static
229 function logToDB($name, $timeSum, $eventCount) {
230 $dbw =& wfGetDB( DB_MASTER );
231 $profiling = $dbw->tableName( 'profiling' );
233 $name = substr($name,0,255);
234 $encname = $dbw->strencode($name);
235 $sql = "UPDATE $profiling ".
236 "SET pf_count=pf_count+{$eventCount}, ".
237 "pf_time=pf_time + {$timeSum} ".
238 "WHERE pf_name='{$encname}'";
239 $dbw->query($sql);
241 $rc = $dbw->affectedRows();
242 if( $rc == 0) {
243 $dbw->insert('profiling',array(
244 'pf_name'=>$name,
245 'pf_count'=>$eventCount,
246 'pf_time'=>$timeSum),
247 $fname,array('IGNORE'));
249 // When we upgrade to mysql 4.1, the insert+update
250 // can be merged into just a insert with this construct added:
251 // "ON DUPLICATE KEY UPDATE ".
252 // "pf_count=pf_count + VALUES(pf_count), ".
253 // "pf_time=pf_time + VALUES(pf_time)";
259 $wgProfiler = new Profiler();
260 $wgProfiler->profileIn( '-total' );