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.
25 * $wgProfiler['class'] = 'ProfilerXhprof';
26 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
27 * $wgProfiler['output'] = 'text';
28 * $wgProfiler['visible'] = true;
32 * $wgProfiler['class'] = 'ProfilerXhprof';
33 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
34 * $wgProfiler['output'] = 'udp';
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.
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.
46 * It is also possible to use the Tideways PHP extension, which is mostly
47 * a drop-in replacement for Xhprof. Just change the XHPROF_FLAGS_* constants
48 * to TIDEWAYS_FLAGS_*.
50 * @author Bryan Davis <bd808@wikimedia.org>
51 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
54 * @see https://php.net/xhprof
55 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
56 * @see https://github.com/tideways/php-profiler-extension
58 class ProfilerXhprof
extends Profiler
{
60 * @var XhprofData|null $xhprofData
62 protected $xhprofData;
65 * Profiler for explicit, arbitrary, frame labels
66 * @var SectionProfiler
71 * @param array $params
72 * @see Xhprof::__construct()
74 public function __construct( array $params = [] ) {
75 parent
::__construct( $params );
77 $flags = isset( $params['flags'] ) ?
$params['flags'] : 0;
78 $options = isset( $params['exclude'] )
79 ?
[ 'ignored_functions' => $params['exclude'] ] : [];
80 Xhprof
::enable( $flags, $options );
81 $this->sprofiler
= new SectionProfiler();
87 public function getXhprofData() {
88 if ( !$this->xhprofData
) {
89 $this->xhprofData
= new XhprofData( Xhprof
::disable(), $this->params
);
91 return $this->xhprofData
;
94 public function scopedProfileIn( $section ) {
95 $key = 'section.' . ltrim( $section, '.' );
96 return $this->sprofiler
->scopedProfileIn( $key );
100 * No-op for xhprof profiling.
102 public function close() {
106 * Check if a function or section should be excluded from the output.
108 * @param string $name Function or section name.
111 private function shouldExclude( $name ) {
112 if ( $name === '-total' ) {
115 if ( !empty( $this->params
['include'] ) ) {
116 foreach ( $this->params
['include'] as $pattern ) {
117 if ( fnmatch( $pattern, $name, FNM_NOESCAPE
) ) {
123 if ( !empty( $this->params
['exclude'] ) ) {
124 foreach ( $this->params
['exclude'] as $pattern ) {
125 if ( fnmatch( $pattern, $name, FNM_NOESCAPE
) ) {
133 public function getFunctionStats() {
134 $metrics = $this->getXhprofData()->getCompleteMetrics();
137 $main = null; // units in ms
138 foreach ( $metrics as $fname => $stats ) {
139 if ( $this->shouldExclude( $fname ) ) {
142 // Convert elapsed times from μs to ms to match interface
145 'calls' => $stats['ct'],
146 'real' => $stats['wt']['total'] / 1000,
147 '%real' => $stats['wt']['percent'],
148 'cpu' => isset( $stats['cpu'] ) ?
$stats['cpu']['total'] / 1000 : 0,
149 '%cpu' => isset( $stats['cpu'] ) ?
$stats['cpu']['percent'] : 0,
150 'memory' => isset( $stats['mu'] ) ?
$stats['mu']['total'] : 0,
151 '%memory' => isset( $stats['mu'] ) ?
$stats['mu']['percent'] : 0,
152 'min_real' => $stats['wt']['min'] / 1000,
153 'max_real' => $stats['wt']['max'] / 1000
156 if ( $fname === 'main()' ) {
161 // Merge in all of the custom profile sections
162 foreach ( $this->sprofiler
->getFunctionStats() as $stats ) {
163 if ( $this->shouldExclude( $stats['name'] ) ) {
167 // @note: getFunctionStats() values already in ms
168 $stats['%real'] = $main['real'] ?
$stats['real'] / $main['real'] * 100 : 0;
169 $stats['%cpu'] = $main['cpu'] ?
$stats['cpu'] / $main['cpu'] * 100 : 0;
170 $stats['%memory'] = $main['memory'] ?
$stats['memory'] / $main['memory'] * 100 : 0;
171 $profile[] = $stats; // assume no section names collide with $metrics
178 * Returns a profiling output to be stored in debug file
182 public function getOutput() {
183 return $this->getFunctionReport();
187 * Get a report of profiled functions sorted by inclusive wall clock time
188 * in descending order.
190 * Each line of the report includes this data:
192 * - Number of times function was called
193 * - Total wall clock time spent in function in microseconds
194 * - Minimum wall clock time spent in function in microseconds
195 * - Average wall clock time spent in function in microseconds
196 * - Maximum wall clock time spent in function in microseconds
197 * - Percentage of total wall clock time spent in function
198 * - Total delta of memory usage from start to end of function in bytes
202 protected function getFunctionReport() {
203 $data = $this->getFunctionStats();
204 usort( $data, function( $a, $b ) {
205 if ( $a['real'] === $b['real'] ) {
208 return ( $a['real'] > $b['real'] ) ?
-1 : 1; // descending
212 $nameWidth = $width - 65;
213 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
215 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
216 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
218 foreach ( $data as $stats ) {
219 $out[] = sprintf( $format,
222 $stats['real'] * 1000,
223 $stats['min_real'] * 1000,
224 $stats['real'] / $stats['calls'] * 1000,
225 $stats['max_real'] * 1000,
230 return implode( "\n", $out );
234 * Retrieve raw data from xhprof
237 public function getRawData() {
238 return $this->getXhprofData()->getRawData();