Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / libs / XhprofData.php
blobc6da432eff83b75adbba76257f5a3470d592c735
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use RunningStat\RunningStat;
23 /**
24 * Convenience class for working with XHProf profiling data
25 * <https://github.com/phacility/xhprof>. XHProf can be installed as a PECL
26 * package for use with PHP5 (Zend PHP) and is built-in to HHVM 3.3.0.
28 * @author Bryan Davis <bd808@wikimedia.org>
29 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
30 * @since 1.28
32 class XhprofData {
34 /**
35 * @var array $config
37 protected $config;
39 /**
40 * Hierarchical profiling data returned by xhprof.
41 * @var array $hieraData
43 protected $hieraData;
45 /**
46 * Per-function inclusive data.
47 * @var array $inclusive
49 protected $inclusive;
51 /**
52 * Per-function inclusive and exclusive data.
53 * @var array $complete
55 protected $complete;
57 /**
58 * Configuration data can contain:
59 * - include: Array of function names to include in profiling.
60 * - sort: Key to sort per-function reports on.
62 * @param array $data Xhprof profiling data, as returned by xhprof_disable()
63 * @param array $config
65 public function __construct( array $data, array $config = [] ) {
66 $this->config = array_merge( [
67 'include' => null,
68 'sort' => 'wt',
69 ], $config );
71 $this->hieraData = $this->pruneData( $data );
74 /**
75 * Get raw data collected by xhprof.
77 * Each key in the returned array is an edge label for the call graph in
78 * the form "caller==>callee". There is once special case edge labled
79 * simply "main()" which represents the global scope entry point of the
80 * application.
82 * XHProf will collect different data depending on the flags that are used:
83 * - ct: Number of matching events seen.
84 * - wt: Inclusive elapsed wall time for this event in microseconds.
85 * - cpu: Inclusive elapsed cpu time for this event in microseconds.
86 * (XHPROF_FLAGS_CPU)
87 * - mu: Delta of memory usage from start to end of callee in bytes.
88 * (XHPROF_FLAGS_MEMORY)
89 * - pmu: Delta of peak memory usage from start to end of callee in
90 * bytes. (XHPROF_FLAGS_MEMORY)
91 * - alloc: Delta of amount memory requested from malloc() by the callee,
92 * in bytes. (XHPROF_FLAGS_MALLOC)
93 * - free: Delta of amount of memory passed to free() by the callee, in
94 * bytes. (XHPROF_FLAGS_MALLOC)
96 * @return array
97 * @see getInclusiveMetrics()
98 * @see getCompleteMetrics()
100 public function getRawData() {
101 return $this->hieraData;
105 * Convert an xhprof data key into an array of ['parent', 'child']
106 * function names.
108 * The resulting array is left padded with nulls, so a key
109 * with no parent (eg 'main()') will return [null, 'function'].
111 * @return array
113 public static function splitKey( $key ) {
114 return array_pad( explode( '==>', $key, 2 ), -2, null );
118 * Remove data for functions that are not included in the 'include'
119 * configuration array.
121 * @param array $data Raw xhprof data
122 * @return array
124 protected function pruneData( $data ) {
125 if ( !$this->config['include'] ) {
126 return $data;
129 $want = array_fill_keys( $this->config['include'], true );
130 $want['main()'] = true;
132 $keep = [];
133 foreach ( $data as $key => $stats ) {
134 list( $parent, $child ) = self::splitKey( $key );
135 if ( isset( $want[$parent] ) || isset( $want[$child] ) ) {
136 $keep[$key] = $stats;
139 return $keep;
143 * Get the inclusive metrics for each function call. Inclusive metrics
144 * for given function include the metrics for all functions that were
145 * called from that function during the measurement period.
147 * See getRawData() for a description of the metric that are returned for
148 * each funcition call. The values for the wt, cpu, mu and pmu metrics are
149 * arrays with these values:
150 * - total: Cumulative value
151 * - min: Minimum value
152 * - mean: Mean (average) value
153 * - max: Maximum value
154 * - variance: Variance (spread) of the values
156 * @return array
157 * @see getRawData()
158 * @see getCompleteMetrics()
160 public function getInclusiveMetrics() {
161 if ( $this->inclusive === null ) {
162 $main = $this->hieraData['main()'];
163 $hasCpu = isset( $main['cpu'] );
164 $hasMu = isset( $main['mu'] );
165 $hasAlloc = isset( $main['alloc'] );
167 $this->inclusive = [];
168 foreach ( $this->hieraData as $key => $stats ) {
169 list( $parent, $child ) = self::splitKey( $key );
170 if ( !isset( $this->inclusive[$child] ) ) {
171 $this->inclusive[$child] = [
172 'ct' => 0,
173 'wt' => new RunningStat(),
175 if ( $hasCpu ) {
176 $this->inclusive[$child]['cpu'] = new RunningStat();
178 if ( $hasMu ) {
179 $this->inclusive[$child]['mu'] = new RunningStat();
180 $this->inclusive[$child]['pmu'] = new RunningStat();
182 if ( $hasAlloc ) {
183 $this->inclusive[$child]['alloc'] = new RunningStat();
184 $this->inclusive[$child]['free'] = new RunningStat();
188 $this->inclusive[$child]['ct'] += $stats['ct'];
189 foreach ( $stats as $stat => $value ) {
190 if ( $stat === 'ct' ) {
191 continue;
194 if ( !isset( $this->inclusive[$child][$stat] ) ) {
195 // Ignore unknown stats
196 continue;
199 for ( $i = 0; $i < $stats['ct']; $i++ ) {
200 $this->inclusive[$child][$stat]->addObservation(
201 $value / $stats['ct']
207 // Convert RunningStat instances to static arrays and add
208 // percentage stats.
209 foreach ( $this->inclusive as $func => $stats ) {
210 foreach ( $stats as $name => $value ) {
211 if ( $value instanceof RunningStat ) {
212 $total = $value->m1 * $value->n;
213 $percent = ( isset( $main[$name] ) && $main[$name] )
214 ? 100 * $total / $main[$name]
215 : 0;
216 $this->inclusive[$func][$name] = [
217 'total' => $total,
218 'min' => $value->min,
219 'mean' => $value->m1,
220 'max' => $value->max,
221 'variance' => $value->m2,
222 'percent' => $percent,
228 uasort( $this->inclusive, self::makeSortFunction(
229 $this->config['sort'], 'total'
230 ) );
232 return $this->inclusive;
236 * Get the inclusive and exclusive metrics for each function call.
238 * In addition to the normal data contained in the inclusive metrics, the
239 * metrics have an additional 'exclusive' measurement which is the total
240 * minus the totals of all child function calls.
242 * @return array
243 * @see getRawData()
244 * @see getInclusiveMetrics()
246 public function getCompleteMetrics() {
247 if ( $this->complete === null ) {
248 // Start with inclusive data
249 $this->complete = $this->getInclusiveMetrics();
251 foreach ( $this->complete as $func => $stats ) {
252 foreach ( $stats as $stat => $value ) {
253 if ( $stat === 'ct' ) {
254 continue;
256 // Initialize exclusive data with inclusive totals
257 $this->complete[$func][$stat]['exclusive'] = $value['total'];
259 // Add sapce for call tree information to be filled in later
260 $this->complete[$func]['calls'] = [];
261 $this->complete[$func]['subcalls'] = [];
264 foreach ( $this->hieraData as $key => $stats ) {
265 list( $parent, $child ) = self::splitKey( $key );
266 if ( $parent !== null ) {
267 // Track call tree information
268 $this->complete[$child]['calls'][$parent] = $stats;
269 $this->complete[$parent]['subcalls'][$child] = $stats;
272 if ( isset( $this->complete[$parent] ) ) {
273 // Deduct child inclusive data from exclusive data
274 foreach ( $stats as $stat => $value ) {
275 if ( $stat === 'ct' ) {
276 continue;
279 if ( !isset( $this->complete[$parent][$stat] ) ) {
280 // Ignore unknown stats
281 continue;
284 $this->complete[$parent][$stat]['exclusive'] -= $value;
289 uasort( $this->complete, self::makeSortFunction(
290 $this->config['sort'], 'exclusive'
291 ) );
293 return $this->complete;
297 * Get a list of all callers of a given function.
299 * @param string $function Function name
300 * @return array
301 * @see getEdges()
303 public function getCallers( $function ) {
304 $edges = $this->getCompleteMetrics();
305 if ( isset( $edges[$function]['calls'] ) ) {
306 return array_keys( $edges[$function]['calls'] );
307 } else {
308 return [];
313 * Get a list of all callees from a given function.
315 * @param string $function Function name
316 * @return array
317 * @see getEdges()
319 public function getCallees( $function ) {
320 $edges = $this->getCompleteMetrics();
321 if ( isset( $edges[$function]['subcalls'] ) ) {
322 return array_keys( $edges[$function]['subcalls'] );
323 } else {
324 return [];
329 * Find the critical path for the given metric.
331 * @param string $metric Metric to find critical path for
332 * @return array
334 public function getCriticalPath( $metric = 'wt' ) {
335 $func = 'main()';
336 $path = [
337 $func => $this->hieraData[$func],
339 while ( $func ) {
340 $callees = $this->getCallees( $func );
341 $maxCallee = null;
342 $maxCall = null;
343 foreach ( $callees as $callee ) {
344 $call = "{$func}==>{$callee}";
345 if ( $maxCall === null ||
346 $this->hieraData[$call][$metric] >
347 $this->hieraData[$maxCall][$metric]
349 $maxCallee = $callee;
350 $maxCall = $call;
353 if ( $maxCall !== null ) {
354 $path[$maxCall] = $this->hieraData[$maxCall];
356 $func = $maxCallee;
358 return $path;
362 * Make a closure to use as a sort function. The resulting function will
363 * sort by descending numeric values (largest value first).
365 * @param string $key Data key to sort on
366 * @param string $sub Sub key to sort array values on
367 * @return Closure
369 public static function makeSortFunction( $key, $sub ) {
370 return function ( $a, $b ) use ( $key, $sub ) {
371 if ( isset( $a[$key] ) && isset( $b[$key] ) ) {
372 // Descending sort: larger values will be first in result.
373 // Assumes all values are numeric.
374 // Values for 'main()' will not have sub keys
375 $valA = is_array( $a[$key] ) ? $a[$key][$sub] : $a[$key];
376 $valB = is_array( $b[$key] ) ? $b[$key][$sub] : $b[$key];
377 return $valB - $valA;
378 } else {
379 // Sort datum with the key before those without
380 return isset( $a[$key] ) ? -1 : 1;