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