Merge "FormatMetadata::fetchExtendedMetadata: Ignore multiple EXIF/XMP values"
[lhc/web/wiklou.git] / includes / profiler / SectionProfiler.php
1 <?php
2 /**
3 * Arbitrary section name based PHP profiling.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Profiler
22 * @author Aaron Schulz
23 */
24
25 /**
26 * Custom PHP profiler for parser/DB type section names that xhprof/xdebug can't handle
27 *
28 * @since 1.25
29 */
30 class SectionProfiler {
31 /** @var array List of resolved profile calls with start/end data */
32 protected $stack = array();
33 /** @var array Queue of open profile calls with start data */
34 protected $workStack = array();
35
36 /** @var array Map of (function name => aggregate data array) */
37 protected $collated = array();
38 /** @var bool */
39 protected $collateDone = false;
40 /** @var bool Whether to collect the full stack trace or just aggregates */
41 protected $collateOnly = true;
42
43 /** @var array Cache of a standard broken collation entry */
44 protected $errorEntry;
45
46 /**
47 * @param array $params
48 */
49 public function __construct( array $params = array() ) {
50 $this->errorEntry = $this->getErrorEntry();
51 $this->collateOnly = empty( $params['trace'] );
52 }
53
54 /**
55 * @param string $section
56 * @return ScopedCallback
57 */
58 public function scopedProfileIn( $section ) {
59 $this->profileInInternal( $section );
60
61 $that = $this;
62 return new ScopedCallback( function() use ( $that, $section ) {
63 $that->profileOutInternal( $section );
64 } );
65 }
66
67 /**
68 * @param ScopedCallback $section
69 */
70 public function scopedProfileOut( ScopedCallback &$section ) {
71 $section = null;
72 }
73
74 /**
75 * Get the aggregated inclusive profiling data for each method
76 *
77 * The percent time for each time is based on the current "total" time
78 * used is based on all methods so far. This method can therefore be
79 * called several times in between several profiling calls without the
80 * delays in usage of the profiler skewing the results. A "-total" entry
81 * is always included in the results.
82 *
83 * @return array List of method entries arrays, each having:
84 * - name : method name
85 * - calls : the number of invoking calls
86 * - real : real time ellapsed (ms)
87 * - %real : percent real time
88 * - cpu : real time ellapsed (ms)
89 * - %cpu : percent real time
90 * - memory : memory used (bytes)
91 * - %memory : percent memory used
92 */
93 public function getFunctionStats() {
94 $this->collateData();
95
96 $totalCpu = 0.0;
97 $totalReal = 0.0;
98 $totalMem = 0;
99 foreach ( $this->collated as $fname => $data ) {
100 $totalCpu += $data['cpu'];
101 $totalReal += $data['real'];
102 $totalMem += $data['memory'];
103 }
104
105 $profile = array();
106 foreach ( $this->collated as $fname => $data ) {
107 $profile[] = array(
108 'name' => $fname,
109 'calls' => $data['count'],
110 'real' => $data['real'] * 1000,
111 '%real' => $totalReal ? 100 * $data['real'] / $totalReal : 0,
112 'cpu' => $data['cpu'] * 1000,
113 '%cpu' => $totalCpu ? 100 * $data['cpu'] / $totalCpu : 0,
114 'memory' => $data['memory'],
115 '%memory' => $totalMem ? 100 * $data['memory'] / $totalMem : 0,
116 );
117 }
118
119 $profile[] = array(
120 'name' => '-total',
121 'calls' => 1,
122 'real' => 1000 * $totalReal,
123 '%real' => 100,
124 'cpu' => 1000 * $totalCpu,
125 '%cpu' => 100,
126 'memory' => $totalMem,
127 '%memory' => 100,
128 );
129
130 return $profile;
131 }
132
133 /**
134 * @return array Initial collation entry
135 */
136 protected function getZeroEntry() {
137 return array(
138 'cpu' => 0.0,
139 'real' => 0.0,
140 'memory' => 0,
141 'count' => 0
142 );
143 }
144
145 /**
146 * @return array Initial collation entry for errors
147 */
148 protected function getErrorEntry() {
149 $entry = $this->getZeroEntry();
150 $entry['count'] = 1;
151 return $entry;
152 }
153
154 /**
155 * Update the collation entry for a given method name
156 *
157 * @param string $name
158 * @param float $elapsedCpu
159 * @param float $elapsedReal
160 * @param int $memChange
161 */
162 protected function updateEntry( $name, $elapsedCpu, $elapsedReal, $memChange ) {
163 $entry =& $this->collated[$name];
164 if ( !is_array( $entry ) ) {
165 $entry = $this->getZeroEntry();
166 $this->collated[$name] =& $entry;
167 }
168 $entry['cpu'] += $elapsedCpu;
169 $entry['real'] += $elapsedReal;
170 $entry['memory'] += $memChange > 0 ? $memChange : 0;
171 $entry['count']++;
172 }
173
174 /**
175 * This method should not be called outside SectionProfiler
176 *
177 * @param string $functionname
178 */
179 public function profileInInternal( $functionname ) {
180 $this->workStack[] = array(
181 $functionname,
182 count( $this->workStack ),
183 $this->getTime( 'time' ),
184 $this->getTime( 'cpu' ),
185 memory_get_usage()
186 );
187 }
188
189 /**
190 * This method should not be called outside SectionProfiler
191 *
192 * @param string $functionname
193 */
194 public function profileOutInternal( $functionname ) {
195 $item = array_pop( $this->workStack );
196 if ( $item === null ) {
197 $this->debugGroup( 'profileerror', "Profiling error: $functionname" );
198 return;
199 }
200 list( $ofname, /* $ocount */, $ortime, $octime, $omem ) = $item;
201
202 if ( $functionname === 'close' ) {
203 $message = "Profile section ended by close(): {$ofname}";
204 $this->debugGroup( 'profileerror', $message );
205 if ( $this->collateOnly ) {
206 $this->collated[$message] = $this->errorEntry;
207 } else {
208 $this->stack[] = array( $message, 0, 0.0, 0.0, 0, 0.0, 0.0, 0 );
209 }
210 $functionname = $ofname;
211 } elseif ( $ofname !== $functionname ) {
212 $message = "Profiling error: in({$ofname}), out($functionname)";
213 $this->debugGroup( 'profileerror', $message );
214 if ( $this->collateOnly ) {
215 $this->collated[$message] = $this->errorEntry;
216 } else {
217 $this->stack[] = array( $message, 0, 0.0, 0.0, 0, 0.0, 0.0, 0 );
218 }
219 }
220 $realTime = $this->getTime( 'wall' );
221 $cpuTime = $this->getTime( 'cpu' );
222 if ( $this->collateOnly ) {
223 $elapsedcpu = $cpuTime - $octime;
224 $elapsedreal = $realTime - $ortime;
225 $memchange = memory_get_usage() - $omem;
226 $this->updateEntry( $functionname, $elapsedcpu, $elapsedreal, $memchange );
227 } else {
228 $this->stack[] = array_merge( $item,
229 array( $realTime, $cpuTime, memory_get_usage() ) );
230 }
231 }
232
233 /**
234 * Returns a tree of function calls with their real times
235 * @return string
236 */
237 public function getCallTreeReport() {
238 if ( $this->collateOnly ) {
239 throw new Exception( "Tree is only available for trace profiling." );
240 }
241 return implode( '', array_map(
242 array( $this, 'getCallTreeLine' ), $this->remapCallTree( $this->stack )
243 ) );
244 }
245
246 /**
247 * Recursive function the format the current profiling array into a tree
248 *
249 * @param array $stack Profiling array
250 * @return array
251 */
252 protected function remapCallTree( array $stack ) {
253 if ( count( $stack ) < 2 ) {
254 return $stack;
255 }
256 $outputs = array();
257 for ( $max = count( $stack ) - 1; $max > 0; ) {
258 /* Find all items under this entry */
259 $level = $stack[$max][1];
260 $working = array();
261 for ( $i = $max -1; $i >= 0; $i-- ) {
262 if ( $stack[$i][1] > $level ) {
263 $working[] = $stack[$i];
264 } else {
265 break;
266 }
267 }
268 $working = $this->remapCallTree( array_reverse( $working ) );
269 $output = array();
270 foreach ( $working as $item ) {
271 array_push( $output, $item );
272 }
273 array_unshift( $output, $stack[$max] );
274 $max = $i;
275
276 array_unshift( $outputs, $output );
277 }
278 $final = array();
279 foreach ( $outputs as $output ) {
280 foreach ( $output as $item ) {
281 $final[] = $item;
282 }
283 }
284 return $final;
285 }
286
287 /**
288 * Callback to get a formatted line for the call tree
289 * @param array $entry
290 * @return string
291 */
292 protected function getCallTreeLine( $entry ) {
293 // $entry has (name, level, stime, scpu, smem, etime, ecpu, emem)
294 list( $fname, $level, $startreal, , , $endreal ) = $entry;
295 $delta = $endreal - $startreal;
296 $space = str_repeat( ' ', $level );
297 # The ugly double sprintf is to work around a PHP bug,
298 # which has been fixed in recent releases.
299 return sprintf( "%10s %s %s\n",
300 trim( sprintf( "%7.3f", $delta * 1000.0 ) ), $space, $fname );
301 }
302
303 /**
304 * Populate collated data
305 */
306 protected function collateData() {
307 if ( $this->collateDone ) {
308 return;
309 }
310 $this->collateDone = true;
311 // Close opened profiling sections
312 while ( count( $this->workStack ) ) {
313 $this->profileOutInternal( 'close' );
314 }
315
316 if ( $this->collateOnly ) {
317 return; // already collated as methods exited
318 }
319
320 $this->collated = array();
321
322 # Estimate profiling overhead
323 $profileCount = count( $this->stack );
324 $this->calculateOverhead( $profileCount );
325
326 # First, subtract the overhead!
327 $overheadTotal = $overheadMemory = $overheadInternal = array();
328 foreach ( $this->stack as $entry ) {
329 // $entry is (name,pos,rtime0,cputime0,mem0,rtime1,cputime1,mem1)
330 $fname = $entry[0];
331 $elapsed = $entry[5] - $entry[2];
332 $memchange = $entry[7] - $entry[4];
333
334 if ( $fname === '-overhead-total' ) {
335 $overheadTotal[] = $elapsed;
336 $overheadMemory[] = max( 0, $memchange );
337 } elseif ( $fname === '-overhead-internal' ) {
338 $overheadInternal[] = $elapsed;
339 }
340 }
341 $overheadTotal = $overheadTotal ?
342 array_sum( $overheadTotal ) / count( $overheadInternal ) : 0;
343 $overheadMemory = $overheadMemory ?
344 array_sum( $overheadMemory ) / count( $overheadInternal ) : 0;
345 $overheadInternal = $overheadInternal ?
346 array_sum( $overheadInternal ) / count( $overheadInternal ) : 0;
347
348 # Collate
349 foreach ( $this->stack as $index => $entry ) {
350 // $entry is (name,pos,rtime0,cputime0,mem0,rtime1,cputime1,mem1)
351 $fname = $entry[0];
352 $elapsedCpu = $entry[6] - $entry[3];
353 $elapsedReal = $entry[5] - $entry[2];
354 $memchange = $entry[7] - $entry[4];
355 $subcalls = $this->calltreeCount( $this->stack, $index );
356
357 if ( substr( $fname, 0, 9 ) !== '-overhead' ) {
358 # Adjust for profiling overhead (except special values with elapsed=0
359 if ( $elapsed ) {
360 $elapsed -= $overheadInternal;
361 $elapsed -= ( $subcalls * $overheadTotal );
362 $memchange -= ( $subcalls * $overheadMemory );
363 }
364 }
365
366 $this->updateEntry( $fname, $elapsedCpu, $elapsedReal, $memchange );
367 }
368
369 $this->collated['-overhead-total']['count'] = $profileCount;
370 arsort( $this->collated, SORT_NUMERIC );
371 }
372
373 /**
374 * Dummy calls to calculate profiling overhead
375 *
376 * @param int $profileCount
377 */
378 protected function calculateOverhead( $profileCount ) {
379 $this->profileInInternal( '-overhead-total' );
380 for ( $i = 0; $i < $profileCount; $i++ ) {
381 $this->profileInInternal( '-overhead-internal' );
382 $this->profileOutInternal( '-overhead-internal' );
383 }
384 $this->profileOutInternal( '-overhead-total' );
385 }
386
387 /**
388 * Counts the number of profiled function calls sitting under
389 * the given point in the call graph. Not the most efficient algo.
390 *
391 * @param array $stack
392 * @param int $start
393 * @return int
394 */
395 protected function calltreeCount( $stack, $start ) {
396 $level = $stack[$start][1];
397 $count = 0;
398 for ( $i = $start -1; $i >= 0 && $stack[$i][1] > $level; $i-- ) {
399 $count ++;
400 }
401 return $count;
402 }
403
404 /**
405 * Get the initial time of the request, based either on $wgRequestTime or
406 * $wgRUstart. Will return null if not able to find data.
407 *
408 * @param string|bool $metric Metric to use, with the following possibilities:
409 * - user: User CPU time (without system calls)
410 * - cpu: Total CPU time (user and system calls)
411 * - wall (or any other string): elapsed time
412 * - false (default): will fall back to default metric
413 * @return float|null
414 */
415 protected function getTime( $metric = 'wall' ) {
416 if ( $metric === 'cpu' || $metric === 'user' ) {
417 $ru = wfGetRusage();
418 if ( !$ru ) {
419 return 0;
420 }
421 $time = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
422 if ( $metric === 'cpu' ) {
423 # This is the time of system calls, added to the user time
424 # it gives the total CPU time
425 $time += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
426 }
427 return $time;
428 } else {
429 return microtime( true );
430 }
431 }
432
433 /**
434 * Add an entry in the debug log file
435 *
436 * @param string $s String to output
437 */
438 protected function debug( $s ) {
439 if ( function_exists( 'wfDebug' ) ) {
440 wfDebug( $s );
441 }
442 }
443
444 /**
445 * Add an entry in the debug log group
446 *
447 * @param string $group Group to send the message to
448 * @param string $s String to output
449 */
450 protected function debugGroup( $group, $s ) {
451 if ( function_exists( 'wfDebugLog' ) ) {
452 wfDebugLog( $group, $s );
453 }
454 }
455 }