3 * This file is only included if profiling is enabled
8 * @param $functioname name of the function we will profile
10 function wfProfileIn( $functionname ) {
12 $wgProfiler->profileIn( $functionname );
16 * @param $functioname name of the function we have profiled
18 function wfProfileOut( $functionname = 'missing' ) {
20 $wgProfiler->profileOut( $functionname );
23 function wfGetProfilingOutput( $start, $elapsed ) {
25 return $wgProfiler->getOutput( $start, $elapsed );
28 function wfProfileClose() {
33 if( !function_exists( 'memory_get_usage' ) ) {
34 # Old PHP or --enable-memory-limit not compiled in
35 function memory_get_usage() {
46 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
47 var $mCalls = array(), $mTotals = array();
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();
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
);
77 wfDebug( "Profiling error, !\$bit: $functionname\n" );
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" );
88 $this->mStack
[] = $bit;
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";
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 ) {
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 );
147 foreach ( $this->mStack
as $index => $entry ) {
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;
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";
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
217 function calltreeCount( &$stack, $start ) {
218 $level = $stack[$start][1];
220 for( $i = $start - 1; $i >= 0 && $stack[$i][1] > $level; $i-- ) {
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}'";
241 $rc = $dbw->affectedRows();
243 $dbw->insert('profiling',array(
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' );