7b64e0309176693ea1c9958752904842aba6e655
[lhc/web/wiklou.git] / includes / Profiling.php
1 <?php
2 /**
3 * This file is only included if profiling is enabled
4 * @package MediaWiki
5 */
6
7 /**
8 * @param $functioname name of the function we will profile
9 */
10 function wfProfileIn( $functionname ) {
11 global $wgProfiler;
12 $wgProfiler->profileIn( $functionname );
13 }
14
15 /**
16 * @param $functioname name of the function we have profiled
17 */
18 function wfProfileOut( $functionname = 'missing' ) {
19 global $wgProfiler;
20 $wgProfiler->profileOut( $functionname );
21 }
22
23 function wfGetProfilingOutput( $start, $elapsed ) {
24 global $wgProfiler;
25 return $wgProfiler->getOutput( $start, $elapsed );
26 }
27
28 function wfProfileClose() {
29 global $wgProfiler;
30 $wgProfiler->close();
31 }
32
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;
37 }
38 }
39
40 /**
41 * @todo document
42 * @package MediaWiki
43 */
44 class Profiler
45 {
46 var $mStack = array(), $mWorkStack = array(), $mCollated = array();
47 var $mCalls = array(), $mTotals = array();
48 /*
49 function Profiler()
50 {
51 $this->mProfileStack = array();
52 $this->mWorkStack = array();
53 $this->mCollated = array();
54 }
55 */
56
57 function profileIn( $functionname ) {
58 global $wgDebugFunctionEntry;
59 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
60 wfDebug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering '.$functionname."\n" );
61 }
62 $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(), memory_get_usage() );
63 }
64
65 function profileOut( $functionname ) {
66 $time = microtime();
67 $memory = memory_get_usage();
68 global $wgDebugProfiling, $wgDebugFunctionEntry;
69
70 if ( $wgDebugFunctionEntry && function_exists( 'wfDebug' ) ) {
71 wfDebug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Exiting '.$functionname."\n" );
72 }
73
74 $bit = array_pop( $this->mWorkStack );
75
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" );
84 }
85 }
86 $bit[] = $time;
87 $bit[] = $memory;
88 $this->mStack[] = $bit;
89 }
90 }
91
92 function close() {
93 while ( count( $this->mWorkStack ) ) {
94 $this->profileOut( 'close' );
95 }
96 }
97
98 function getOutput() {
99 global $wgDebugFunctionEntry;
100 $wgDebugFunctionEntry = false;
101
102 if( !count( $this->mStack ) ) {
103 return "No profiling output\n";
104 }
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();
114
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' );
121 }
122 wfProfileOut( '-overhead-total' );
123
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 if( $fname == '-overhead-total' ) {
134 $overheadTotal[] = $elapsed;
135 } elseif( $fname == '-overhead-internal' ) {
136 $overheadInternal[] = $elapsed;
137 }
138 }
139 $overheadTotal = array_sum( $overheadTotal ) / count( $overheadInternal );
140 $overheadInternal = array_sum( $overheadInternal ) / count( $overheadInternal );
141
142 # Collate
143 foreach ( $this->mStack as $index => $entry ) {
144 $fname = $entry[0];
145 $thislevel = $entry[1];
146 $start = explode( ' ', $entry[2]);
147 $start = (float)$start[0] + (float)$start[1];
148 $end = explode( ' ', $entry[4]);
149 $end = (float)$end[0] + (float)$end[1];
150 $elapsed = $end - $start;
151
152 $memory = $entry[5] - $entry[3];
153 $subcalls = $this->calltreeCount( $this->mStack, $index );
154
155 if( !preg_match( '/^-overhead/', $fname ) ) {
156 # Adjust for profiling overhead
157 $elapsed -= $overheadInternal;
158 $elapsed -= ($subcalls * $overheadTotal);
159 }
160
161 if ( !array_key_exists( $fname, $this->mCollated ) ) {
162 $this->mCollated[$fname] = 0;
163 $this->mCalls[$fname] = 0;
164 $this->mMemory[$fname] = 0;
165 $this->mMin[$fname] = 1 << 24;
166 $this->mMax[$fname] = 0;
167 $this->mOverhead[$fname] = $subcalls;
168 }
169
170 $this->mCollated[$fname] += $elapsed;
171 $this->mCalls[$fname] ++;
172 $this->mMemory[$fname] += $memory;
173 $this->mMin[$fname] = min( $this->mMin[$fname], $elapsed );
174 $this->mMax[$fname] = max( $this->mMax[$fname], $elapsed );
175 $this->mOverhead[$fname] += $subcalls;
176 }
177
178 $total = @$this->mCollated['-total'];
179 $this->mCalls['-overhead-total'] = $profileCount;
180
181 # Output
182 asort( $this->mCollated, SORT_NUMERIC );
183 foreach ( $this->mCollated as $fname => $elapsed ) {
184 $calls = $this->mCalls[$fname];
185 $percent = $total ? 100. * $elapsed / $total : 0;
186 $memory = $this->mMemory[$fname];
187 $prof .= sprintf( $format, $fname, $calls, (float)($elapsed * 1000),
188 (float)($elapsed * 1000) / $calls, $percent, $memory,
189 ($this->mMin[$fname] * 1000.0),
190 ($this->mMax[$fname] * 1000.0),
191 $this->mOverhead[$fname] );
192
193 global $wgProfileToDatabase;
194 if( $wgProfileToDatabase ) {
195 Profiler::logToDB( $fname, (float)($elapsed * 1000), $calls );
196 }
197 }
198 $prof .= "\nTotal: $total\n\n";
199
200 return $prof;
201 }
202
203 /**
204 * Counts the number of profiled function calls sitting under
205 * the given point in the call graph. Not the most efficient algo.
206 *
207 * @param array $stack
208 * @param int $start
209 * @return int
210 * @access private
211 */
212 function calltreeCount( &$stack, $start ) {
213 $level = $stack[$start][1];
214 $count = 0;
215 for( $i = $start - 1; $i >= 0 && $stack[$i][1] > $level; $i-- ) {
216 $count++;
217 }
218 return $count;
219 }
220
221 /**
222 * @static
223 */
224 function logToDB($name, $timeSum, $eventCount) {
225 $dbw =& wfGetDB( DB_MASTER );
226 $profiling = $dbw->tableName( 'profiling' );
227
228 $name = substr($name,0,255);
229 $encname = $dbw->strencode($name);
230 $sql = "UPDATE $profiling ".
231 "SET pf_count=pf_count+{$eventCount}, ".
232 "pf_time=pf_time + {$timeSum} ".
233 "WHERE pf_name='{$encname}'";
234 $dbw->query($sql);
235
236 $rc = $dbw->affectedRows();
237 if( $rc == 0) {
238 $dbw->insert('profiling',array(
239 'pf_name'=>$name,
240 'pf_count'=>$eventCount,
241 'pf_time'=>$timeSum),
242 $fname,array('IGNORE'));
243 }
244 // When we upgrade to mysql 4.1, the insert+update
245 // can be merged into just a insert with this construct added:
246 // "ON DUPLICATE KEY UPDATE ".
247 // "pf_count=pf_count + VALUES(pf_count), ".
248 // "pf_time=pf_time + VALUES(pf_time)";
249 }
250
251 }
252
253
254 $wgProfiler = new Profiler();
255 $wgProfiler->profileIn( '-total' );
256 ?>