Merge "Convert article delete to use OOUI"
[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.
44 * Shell-style patterns are also accepted.
45 *
46 * It is also possible to use the Tideways PHP extension, which is mostly
47 * a drop-in replacement for Xhprof. Just change the XHPROF_FLAGS_* constants
48 * to TIDEWAYS_FLAGS_*.
49 *
50 * @copyright © 2014 Wikimedia Foundation and contributors
51 * @ingroup Profiler
52 * @see Xhprof
53 * @see https://php.net/xhprof
54 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
55 * @see https://github.com/tideways/php-profiler-extension
56 */
57 class ProfilerXhprof extends Profiler {
58 /**
59 * @var XhprofData|null $xhprofData
60 */
61 protected $xhprofData;
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 = [] ) {
74 parent::__construct( $params );
75
76 $flags = isset( $params['flags'] ) ? $params['flags'] : 0;
77 $options = isset( $params['exclude'] )
78 ? [ 'ignored_functions' => $params['exclude'] ] : [];
79 Xhprof::enable( $flags, $options );
80 $this->sprofiler = new SectionProfiler();
81 }
82
83 /**
84 * @return XhprofData
85 */
86 public function getXhprofData() {
87 if ( !$this->xhprofData ) {
88 $this->xhprofData = new XhprofData( Xhprof::disable(), $this->params );
89 }
90 return $this->xhprofData;
91 }
92
93 public function scopedProfileIn( $section ) {
94 $key = 'section.' . ltrim( $section, '.' );
95 return $this->sprofiler->scopedProfileIn( $key );
96 }
97
98 /**
99 * No-op for xhprof profiling.
100 */
101 public function close() {
102 }
103
104 /**
105 * Check if a function or section should be excluded from the output.
106 *
107 * @param string $name Function or section name.
108 * @return bool
109 */
110 private function shouldExclude( $name ) {
111 if ( $name === '-total' ) {
112 return true;
113 }
114 if ( !empty( $this->params['include'] ) ) {
115 foreach ( $this->params['include'] as $pattern ) {
116 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
117 return false;
118 }
119 }
120 return true;
121 }
122 if ( !empty( $this->params['exclude'] ) ) {
123 foreach ( $this->params['exclude'] as $pattern ) {
124 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
125 return true;
126 }
127 }
128 }
129 return false;
130 }
131
132 public function getFunctionStats() {
133 $metrics = $this->getXhprofData()->getCompleteMetrics();
134 $profile = [];
135
136 $main = null; // units in ms
137 foreach ( $metrics as $fname => $stats ) {
138 if ( $this->shouldExclude( $fname ) ) {
139 continue;
140 }
141 // Convert elapsed times from μs to ms to match interface
142 $entry = [
143 'name' => $fname,
144 'calls' => $stats['ct'],
145 'real' => $stats['wt']['total'] / 1000,
146 '%real' => $stats['wt']['percent'],
147 'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
148 '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
149 'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
150 '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
151 'min_real' => $stats['wt']['min'] / 1000,
152 'max_real' => $stats['wt']['max'] / 1000
153 ];
154 $profile[] = $entry;
155 if ( $fname === 'main()' ) {
156 $main = $entry;
157 }
158 }
159
160 // Merge in all of the custom profile sections
161 foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
162 if ( $this->shouldExclude( $stats['name'] ) ) {
163 continue;
164 }
165
166 // @note: getFunctionStats() values already in ms
167 $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
168 $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
169 $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
170 $profile[] = $stats; // assume no section names collide with $metrics
171 }
172
173 return $profile;
174 }
175
176 /**
177 * Returns a profiling output to be stored in debug file
178 *
179 * @return string
180 */
181 public function getOutput() {
182 return $this->getFunctionReport();
183 }
184
185 /**
186 * Get a report of profiled functions sorted by inclusive wall clock time
187 * in descending order.
188 *
189 * Each line of the report includes this data:
190 * - Function name
191 * - Number of times function was called
192 * - Total wall clock time spent in function in microseconds
193 * - Minimum wall clock time spent in function in microseconds
194 * - Average wall clock time spent in function in microseconds
195 * - Maximum wall clock time spent in function in microseconds
196 * - Percentage of total wall clock time spent in function
197 * - Total delta of memory usage from start to end of function in bytes
198 *
199 * @return string
200 */
201 protected function getFunctionReport() {
202 $data = $this->getFunctionStats();
203 usort( $data, function ( $a, $b ) {
204 if ( $a['real'] === $b['real'] ) {
205 return 0;
206 }
207 return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
208 } );
209
210 $width = 140;
211 $nameWidth = $width - 65;
212 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
213 $out = [];
214 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
215 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
216 );
217 foreach ( $data as $stats ) {
218 $out[] = sprintf( $format,
219 $stats['name'],
220 $stats['calls'],
221 $stats['real'] * 1000,
222 $stats['min_real'] * 1000,
223 $stats['real'] / $stats['calls'] * 1000,
224 $stats['max_real'] * 1000,
225 $stats['%real'],
226 $stats['memory']
227 );
228 }
229 return implode( "\n", $out );
230 }
231
232 /**
233 * Retrieve raw data from xhprof
234 * @return array
235 */
236 public function getRawData() {
237 return $this->getXhprofData()->getRawData();
238 }
239 }