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 * @author Bryan Davis <bd808@wikimedia.org>
47 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
50 * @see https://php.net/xhprof
51 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
53 class ProfilerXhprof
extends Profiler
{
55 * @var XhprofData|null $xhprofData
57 protected $xhprofData;
60 * Profiler for explicit, arbitrary, frame labels
61 * @var SectionProfiler
66 * @param array $params
67 * @see Xhprof::__construct()
69 public function __construct( array $params = [] ) {
70 parent
::__construct( $params );
72 $flags = isset( $params['flags'] ) ?
$params['flags'] : 0;
73 $options = isset( $params['exclude'] )
74 ?
[ 'ignored_functions' => $params['exclude'] ] : [];
75 Xhprof
::enable( $flags, $options );
76 $this->sprofiler
= new SectionProfiler();
82 public function getXhprofData() {
83 if ( !$this->xhprofData
) {
84 $this->xhprofData
= new XhprofData( Xhprof
::disable(), $this->params
);
86 return $this->xhprofData
;
89 public function scopedProfileIn( $section ) {
90 $key = 'section.' . ltrim( $section, '.' );
91 return $this->sprofiler
->scopedProfileIn( $key );
95 * No-op for xhprof profiling.
97 public function close() {
101 * Check if a function or section should be excluded from the output.
103 * @param string $name Function or section name.
106 private function shouldExclude( $name ) {
107 if ( $name === '-total' ) {
110 if ( !empty( $this->params
['include'] ) ) {
111 foreach ( $this->params
['include'] as $pattern ) {
112 if ( fnmatch( $pattern, $name, FNM_NOESCAPE
) ) {
118 if ( !empty( $this->params
['exclude'] ) ) {
119 foreach ( $this->params
['exclude'] as $pattern ) {
120 if ( fnmatch( $pattern, $name, FNM_NOESCAPE
) ) {
128 public function getFunctionStats() {
129 $metrics = $this->getXhprofData()->getCompleteMetrics();
132 $main = null; // units in ms
133 foreach ( $metrics as $fname => $stats ) {
134 if ( $this->shouldExclude( $fname ) ) {
137 // Convert elapsed times from μs to ms to match interface
140 'calls' => $stats['ct'],
141 'real' => $stats['wt']['total'] / 1000,
142 '%real' => $stats['wt']['percent'],
143 'cpu' => isset( $stats['cpu'] ) ?
$stats['cpu']['total'] / 1000 : 0,
144 '%cpu' => isset( $stats['cpu'] ) ?
$stats['cpu']['percent'] : 0,
145 'memory' => isset( $stats['mu'] ) ?
$stats['mu']['total'] : 0,
146 '%memory' => isset( $stats['mu'] ) ?
$stats['mu']['percent'] : 0,
147 'min_real' => $stats['wt']['min'] / 1000,
148 'max_real' => $stats['wt']['max'] / 1000
151 if ( $fname === 'main()' ) {
156 // Merge in all of the custom profile sections
157 foreach ( $this->sprofiler
->getFunctionStats() as $stats ) {
158 if ( $this->shouldExclude( $stats['name'] ) ) {
162 // @note: getFunctionStats() values already in ms
163 $stats['%real'] = $main['real'] ?
$stats['real'] / $main['real'] * 100 : 0;
164 $stats['%cpu'] = $main['cpu'] ?
$stats['cpu'] / $main['cpu'] * 100 : 0;
165 $stats['%memory'] = $main['memory'] ?
$stats['memory'] / $main['memory'] * 100 : 0;
166 $profile[] = $stats; // assume no section names collide with $metrics
173 * Returns a profiling output to be stored in debug file
177 public function getOutput() {
178 return $this->getFunctionReport();
182 * Get a report of profiled functions sorted by inclusive wall clock time
183 * in descending order.
185 * Each line of the report includes this data:
187 * - Number of times function was called
188 * - Total wall clock time spent in function in microseconds
189 * - Minimum wall clock time spent in function in microseconds
190 * - Average wall clock time spent in function in microseconds
191 * - Maximum wall clock time spent in function in microseconds
192 * - Percentage of total wall clock time spent in function
193 * - Total delta of memory usage from start to end of function in bytes
197 protected function getFunctionReport() {
198 $data = $this->getFunctionStats();
199 usort( $data, function( $a, $b ) {
200 if ( $a['real'] === $b['real'] ) {
203 return ( $a['real'] > $b['real'] ) ?
-1 : 1; // descending
207 $nameWidth = $width - 65;
208 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
210 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
211 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
213 foreach ( $data as $stats ) {
214 $out[] = sprintf( $format,
217 $stats['real'] * 1000,
218 $stats['min_real'] * 1000,
219 $stats['real'] / $stats['calls'] * 1000,
220 $stats['max_real'] * 1000,
225 return implode( "\n", $out );
229 * Retrieve raw data from xhprof
232 public function getRawData() {
233 return $this->getXhprofData()->getRawData();