Remove Profiler::isStub()
[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 /**
99 * No-op for xhprof profiling.
100 *
101 * Use the 'include' configuration key instead if you need to constrain
102 * the functions that are profiled.
103 *
104 * @param string $functionname
105 */
106 public function profileIn( $functionname ) {
107 }
108
109 /**
110 * No-op for xhprof profiling.
111 *
112 * Use the 'include' configuration key instead if you need to constrain
113 * the functions that are profiled.
114 *
115 * @param string $functionname
116 */
117 public function profileOut( $functionname ) {
118 }
119
120 public function scopedProfileIn( $section ) {
121 static $exists = null;
122 // Only HHVM supports this, not the standard PECL extension
123 if ( $exists === null ) {
124 $exists = function_exists( 'xhprof_frame_begin' );
125 }
126
127 if ( $exists ) {
128 xhprof_frame_begin( $section );
129 return new ScopedCallback( function() use ( $section ) {
130 xhprof_frame_end();
131 } );
132 }
133
134 return null;
135 }
136
137 /**
138 * No-op for xhprof profiling.
139 */
140 public function close() {
141 }
142
143 public function getFunctionStats() {
144 $metrics = $this->xhprof->getCompleteMetrics();
145 $profile = array();
146
147 foreach ( $metrics as $fname => $stats ) {
148 // Convert elapsed times from μs to ms to match ProfilerStandard
149 $profile[] = array(
150 'name' => $fname,
151 'calls' => $stats['ct'],
152 'real' => $stats['wt']['total'] / 1000,
153 '%real' => $stats['wt']['percent'],
154 'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
155 '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
156 'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
157 '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
158 'min' => $stats['wt']['min'] / 1000,
159 'max' => $stats['wt']['max'] / 1000
160 );
161 }
162
163 return $profile;
164 }
165
166 /**
167 * Returns a profiling output to be stored in debug file
168 *
169 * @return string
170 */
171 public function getOutput() {
172 return $this->getFunctionReport();
173 }
174
175 /**
176 * Get a report of profiled functions sorted by inclusive wall clock time
177 * in descending order.
178 *
179 * Each line of the report includes this data:
180 * - Function name
181 * - Number of times function was called
182 * - Total wall clock time spent in function in microseconds
183 * - Minimum wall clock time spent in function in microseconds
184 * - Average wall clock time spent in function in microseconds
185 * - Maximum wall clock time spent in function in microseconds
186 * - Percentage of total wall clock time spent in function
187 * - Total delta of memory usage from start to end of function in bytes
188 *
189 * @return string
190 */
191 protected function getFunctionReport() {
192 $data = $this->xhprof->getInclusiveMetrics();
193 uasort( $data, Xhprof::makeSortFunction( 'wt', 'total' ) );
194
195 $width = 140;
196 $nameWidth = $width - 65;
197 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
198 $out = array();
199 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
200 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
201 );
202 foreach ( $data as $func => $stats ) {
203 $out[] = sprintf( $format,
204 $func,
205 $stats['ct'],
206 $stats['wt']['total'],
207 $stats['wt']['min'],
208 $stats['wt']['mean'],
209 $stats['wt']['max'],
210 $stats['wt']['percent'],
211 isset( $stats['mu'] ) ? $stats['mu']['total'] : 0
212 );
213 }
214 return implode( "\n", $out );
215 }
216
217 /**
218 * Get a brief report of profiled functions sorted by inclusive wall clock
219 * time in descending order.
220 *
221 * Each line of the report includes this data:
222 * - Percentage of total wall clock time spent in function
223 * - Total wall clock time spent in function in seconds
224 * - Number of times function was called
225 * - Function name
226 *
227 * @param string $header Header text to prepend to report
228 * @param string $footer Footer text to append to report
229 * @return string
230 */
231 protected function getSummaryReport( $header = '', $footer = '' ) {
232 $data = $this->xhprof->getInclusiveMetrics();
233 uasort( $data, Xhprof::makeSortFunction( 'wt', 'total' ) );
234
235 $format = '%6.2f%% %3.6f %6d - %s';
236 $out = array( $header );
237 foreach ( $data as $func => $stats ) {
238 $out[] = sprintf( $format,
239 $stats['wt']['percent'],
240 $stats['wt']['total'] / 1e6,
241 $stats['ct'],
242 $func
243 );
244 }
245 $out[] = $footer;
246 return implode( "\n", $out );
247 }
248 }