Remove unused 'XMPGetInfo' and 'XMPGetResults' hooks
[lhc/web/wiklou.git] / includes / profiler / ProfilerXhprof.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 /**
22 * Profiler wrapper for XHProf extension.
23 *
24 * @code
25 * $wgProfiler['class'] = 'ProfilerXhprof';
26 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
27 * $wgProfiler['output'] = 'text';
28 * $wgProfiler['visible'] = true;
29 * @endcode
30 *
31 * @code
32 * $wgProfiler['class'] = 'ProfilerXhprof';
33 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
34 * $wgProfiler['output'] = 'udp';
35 * @endcode
36 *
37 * ProfilerXhprof profiles all functions using the XHProf PHP extenstion.
38 * For PHP5 users, this extension can be installed via PECL or your operating
39 * system's package manager. XHProf support is built into HHVM.
40 *
41 * To restrict the functions for which profiling data is collected, you can
42 * use either a whitelist ($wgProfiler['include']) or a blacklist
43 * ($wgProfiler['exclude']) containing an array of function names. The
44 * blacklist functionality is built into HHVM and will completely exclude the
45 * named functions from profiling collection. The whitelist is implemented by
46 * Xhprof class which will filter the data collected by XHProf before reporting.
47 * See documentation for the Xhprof class and the XHProf extension for
48 * additional information.
49 *
50 * @author Bryan Davis <bd808@wikimedia.org>
51 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
52 * @ingroup Profiler
53 * @see Xhprof
54 * @see https://php.net/xhprof
55 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
56 */
57 class ProfilerXhprof extends Profiler {
58 /**
59 * @var Xhprof $xhprof
60 */
61 protected $xhprof;
62
63 /**
64 * Profiler for explicit, arbitrary, frame labels
65 * @var SectionProfiler
66 */
67 protected $sprofiler;
68
69 /**
70 * @param array $params
71 * @see Xhprof::__construct()
72 */
73 public function __construct( array $params = array() ) {
74 parent::__construct( $params );
75 $this->xhprof = new Xhprof( $params );
76 $this->sprofiler = new SectionProfiler();
77 }
78
79 public function scopedProfileIn( $section ) {
80 return $this->sprofiler->scopedProfileIn( $section );
81 }
82
83 /**
84 * No-op for xhprof profiling.
85 */
86 public function close() {
87 }
88
89 public function getFunctionStats() {
90 $metrics = $this->xhprof->getCompleteMetrics();
91 $profile = array();
92
93 $main = null; // units in ms
94 foreach ( $metrics as $fname => $stats ) {
95 // Convert elapsed times from μs to ms to match interface
96 $entry = array(
97 'name' => $fname,
98 'calls' => $stats['ct'],
99 'real' => $stats['wt']['total'] / 1000,
100 '%real' => $stats['wt']['percent'],
101 'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
102 '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
103 'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
104 '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
105 'min_real' => $stats['wt']['min'] / 1000,
106 'max_real' => $stats['wt']['max'] / 1000
107 );
108 $profile[] = $entry;
109 if ( $fname === 'main()' ) {
110 $main = $entry;
111 }
112 }
113
114 // Merge in all of the custom profile sections
115 foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
116 if ( $stats['name'] === '-total' ) {
117 // Discard section profiler running totals
118 continue;
119 }
120
121 // @note: getFunctionStats() values already in ms
122 $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
123 $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
124 $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
125 $profile[] = $stats; // assume no section names collide with $metrics
126 }
127
128 return $profile;
129 }
130
131 /**
132 * Returns a profiling output to be stored in debug file
133 *
134 * @return string
135 */
136 public function getOutput() {
137 return $this->getFunctionReport();
138 }
139
140 /**
141 * Get a report of profiled functions sorted by inclusive wall clock time
142 * in descending order.
143 *
144 * Each line of the report includes this data:
145 * - Function name
146 * - Number of times function was called
147 * - Total wall clock time spent in function in microseconds
148 * - Minimum wall clock time spent in function in microseconds
149 * - Average wall clock time spent in function in microseconds
150 * - Maximum wall clock time spent in function in microseconds
151 * - Percentage of total wall clock time spent in function
152 * - Total delta of memory usage from start to end of function in bytes
153 *
154 * @return string
155 */
156 protected function getFunctionReport() {
157 $data = $this->getFunctionStats();
158 usort( $data, function( $a, $b ) {
159 if ( $a['real'] === $b['real'] ) {
160 return 0;
161 }
162 return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
163 } );
164
165 $width = 140;
166 $nameWidth = $width - 65;
167 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
168 $out = array();
169 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
170 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
171 );
172 foreach ( $data as $stats ) {
173 $out[] = sprintf( $format,
174 $stats['name'],
175 $stats['calls'],
176 $stats['real'] * 1000,
177 $stats['min_real'] * 1000,
178 $stats['real'] / $stats['calls'] * 1000,
179 $stats['max_real'] * 1000,
180 $stats['%real'],
181 $stats['memory']
182 );
183 }
184 return implode( "\n", $out );
185 }
186
187 /**
188 * Retrieve raw data from xhprof
189 * @return array
190 */
191 public function getRawData() {
192 return $this->xhprof->getRawData();
193 }
194 }