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.
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.
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
22 * Profiler wrapper for XHProf extension.
24 * Mimics the output of ProfilerStandard using data collected via the XHProf
28 * $wgProfiler['class'] = 'ProfilerXhprof';
29 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
30 * $wgProfiler['output'] = 'text';
31 * $wgProfiler['visible'] = true;
35 * $wgProfiler['class'] = 'ProfilerXhprof';
36 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
37 * $wgProfiler['output'] = 'udp';
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.
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.
54 * @author Bryan Davis <bd808@wikimedia.org>
55 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
58 * @see https://php.net/xhprof
59 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
61 class ProfilerXhprof
extends Profiler
{
69 * Type of report to send when logData() is called.
70 * @var string $logType
75 * Should profile report sent to in page content be visible?
81 * @param array $params
82 * @see Xhprof::__construct()
84 public function __construct( array $params = array() ) {
85 $params = array_merge(
92 parent
::__construct( $params );
93 $this->logType
= $params['log'];
94 $this->visible
= $params['visible'];
95 $this->xhprof
= new Xhprof( $params );
98 public function isStub() {
103 * No-op for xhprof profiling.
105 * Use the 'include' configuration key instead if you need to constrain
106 * the functions that are profiled.
108 * @param string $functionname
110 public function profileIn( $functionname ) {
114 * No-op for xhprof profiling.
116 * Use the 'include' configuration key instead if you need to constrain
117 * the functions that are profiled.
119 * @param string $functionname
121 public function profileOut( $functionname ) {
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' );
132 xhprof_frame_begin( $section );
133 return new ScopedCallback( function() use ( $section ) {
142 * No-op for xhprof profiling.
144 public function close() {
147 public function getFunctionStats() {
148 $metrics = $this->xhprof
->getCompleteMetrics();
151 foreach ( $metrics as $fname => $stats ) {
152 // Convert elapsed times from μs to ms to match ProfilerStandard
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
171 * Returns a profiling output to be stored in debug file
175 public function getOutput() {
176 return $this->getFunctionReport();
180 * Get a report of profiled functions sorted by inclusive wall clock time
181 * in descending order.
183 * Each line of the report includes this data:
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
195 protected function getFunctionReport() {
196 $data = $this->xhprof
->getInclusiveMetrics();
197 uasort( $data, Xhprof
::makeSortFunction( 'wt', 'total' ) );
200 $nameWidth = $width - 65;
201 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
203 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
204 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
206 foreach ( $data as $func => $stats ) {
207 $out[] = sprintf( $format,
210 $stats['wt']['total'],
212 $stats['wt']['mean'],
214 $stats['wt']['percent'],
215 isset( $stats['mu'] ) ?
$stats['mu']['total'] : 0
218 return implode( "\n", $out );
222 * Get a brief report of profiled functions sorted by inclusive wall clock
223 * time in descending order.
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
231 * @param string $header Header text to prepend to report
232 * @param string $footer Footer text to append to report
235 protected function getSummaryReport( $header = '', $footer = '' ) {
236 $data = $this->xhprof
->getInclusiveMetrics();
237 uasort( $data, Xhprof
::makeSortFunction( 'wt', 'total' ) );
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
,
250 return implode( "\n", $out );