88196db1306ee8d0497e09dfe06667b3df3b0350
[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 * Mimics the output of ProfilerStandard using data collected via the XHProf
25 * PHP extension.
26 *
27 * @code
28 * $wgProfiler['class'] = 'ProfilerXhprof';
29 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
30 * $wgProfiler['output'] = 'text';
31 * $wgProfiler['visible'] = true;
32 * @endcode
33 *
34 * @code
35 * $wgProfiler['class'] = 'ProfilerXhprof';
36 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
37 * $wgProfiler['output'] = 'udp';
38 * @endcode
39 *
40 * Rather than obeying wfProfileIn() and wfProfileOut() calls placed in the
41 * application code, ProfilerXhprof profiles all functions using the XHProf
42 * PHP extenstion. For PHP5 users, this extension can be installed via PECL or
43 * your operating system's package manager. XHProf support is built into HHVM.
44 *
45 * To restrict the functions for which profiling data is collected, you can
46 * use either a whitelist ($wgProfiler['include']) or a blacklist
47 * ($wgProfiler['exclude']) containing an array of function names. The
48 * blacklist functionality is built into HHVM and will completely exclude the
49 * named functions from profiling collection. The whitelist is implemented by
50 * Xhprof class which will filter the data collected by XHProf before reporting.
51 * See documentation for the Xhprof class and the XHProf extension for
52 * additional information.
53 *
54 * @author Bryan Davis <bd808@wikimedia.org>
55 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
56 * @ingroup Profiler
57 * @see Xhprof
58 * @see https://php.net/xhprof
59 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
60 */
61 class ProfilerXhprof extends Profiler {
62
63 /**
64 * @var Xhprof $xhprof
65 */
66 protected $xhprof;
67
68 /**
69 * Type of report to send when logData() is called.
70 * @var string $logType
71 */
72 protected $logType;
73
74 /**
75 * Should profile report sent to in page content be visible?
76 * @var bool $visible
77 */
78 protected $visible;
79
80 /**
81 * @param array $params
82 * @see Xhprof::__construct()
83 */
84 public function __construct( array $params = array() ) {
85 $params = array_merge(
86 array(
87 'log' => 'text',
88 'visible' => false
89 ),
90 $params
91 );
92 parent::__construct( $params );
93 $this->logType = $params['log'];
94 $this->visible = $params['visible'];
95 $this->xhprof = new Xhprof( $params );
96 }
97
98 public function isStub() {
99 return false;
100 }
101
102 /**
103 * No-op for xhprof profiling.
104 *
105 * Use the 'include' configuration key instead if you need to constrain
106 * the functions that are profiled.
107 *
108 * @param string $functionname
109 */
110 public function profileIn( $functionname ) {
111 }
112
113 /**
114 * No-op for xhprof profiling.
115 *
116 * Use the 'include' configuration key instead if you need to constrain
117 * the functions that are profiled.
118 *
119 * @param string $functionname
120 */
121 public function profileOut( $functionname ) {
122 }
123
124 public function scopedProfileIn( $section ) {
125 static $exists = null;
126 // Only HHVM supports this, not the standard PECL extension
127 if ( $exists === null ) {
128 $exists = function_exists( 'xhprof_frame_begin' );
129 }
130
131 if ( $exists ) {
132 xhprof_frame_begin( $section );
133 return new ScopedCallback( function() use ( $section ) {
134 xhprof_frame_end();
135 } );
136 }
137
138 return null;
139 }
140
141 /**
142 * No-op for xhprof profiling.
143 */
144 public function close() {
145 }
146
147 public function getFunctionStats() {
148 $metrics = $this->xhprof->getCompleteMetrics();
149 $profile = array();
150
151 foreach ( $metrics as $fname => $stats ) {
152 // Convert elapsed times from μs to ms to match ProfilerStandard
153 $profile[] = array(
154 'name' => $fname,
155 'calls' => $stats['ct'],
156 'real' => $stats['wt']['total'] / 1000,
157 '%real' => $stats['wt']['percent'],
158 'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
159 '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
160 'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
161 '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
162 'min' => $stats['wt']['min'] / 1000,
163 'max' => $stats['wt']['max'] / 1000
164 );
165 }
166
167 return $profile;
168 }
169
170 /**
171 * Returns a profiling output to be stored in debug file
172 *
173 * @return string
174 */
175 public function getOutput() {
176 return $this->getFunctionReport();
177 }
178
179 /**
180 * Get a report of profiled functions sorted by inclusive wall clock time
181 * in descending order.
182 *
183 * Each line of the report includes this data:
184 * - Function name
185 * - Number of times function was called
186 * - Total wall clock time spent in function in microseconds
187 * - Minimum wall clock time spent in function in microseconds
188 * - Average wall clock time spent in function in microseconds
189 * - Maximum wall clock time spent in function in microseconds
190 * - Percentage of total wall clock time spent in function
191 * - Total delta of memory usage from start to end of function in bytes
192 *
193 * @return string
194 */
195 protected function getFunctionReport() {
196 $data = $this->xhprof->getInclusiveMetrics();
197 uasort( $data, Xhprof::makeSortFunction( 'wt', 'total' ) );
198
199 $width = 140;
200 $nameWidth = $width - 65;
201 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
202 $out = array();
203 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
204 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
205 );
206 foreach ( $data as $func => $stats ) {
207 $out[] = sprintf( $format,
208 $func,
209 $stats['ct'],
210 $stats['wt']['total'],
211 $stats['wt']['min'],
212 $stats['wt']['mean'],
213 $stats['wt']['max'],
214 $stats['wt']['percent'],
215 isset( $stats['mu'] ) ? $stats['mu']['total'] : 0
216 );
217 }
218 return implode( "\n", $out );
219 }
220
221 /**
222 * Get a brief report of profiled functions sorted by inclusive wall clock
223 * time in descending order.
224 *
225 * Each line of the report includes this data:
226 * - Percentage of total wall clock time spent in function
227 * - Total wall clock time spent in function in seconds
228 * - Number of times function was called
229 * - Function name
230 *
231 * @param string $header Header text to prepend to report
232 * @param string $footer Footer text to append to report
233 * @return string
234 */
235 protected function getSummaryReport( $header = '', $footer = '' ) {
236 $data = $this->xhprof->getInclusiveMetrics();
237 uasort( $data, Xhprof::makeSortFunction( 'wt', 'total' ) );
238
239 $format = '%6.2f%% %3.6f %6d - %s';
240 $out = array( $header );
241 foreach ( $data as $func => $stats ) {
242 $out[] = sprintf( $format,
243 $stats['wt']['percent'],
244 $stats['wt']['total'] / 1e6,
245 $stats['ct'],
246 $func
247 );
248 }
249 $out[] = $footer;
250 return implode( "\n", $out );
251 }
252 }