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