MediaWiki  master
ProfilerXhprof.php
Go to the documentation of this file.
1 <?php
34 class ProfilerXhprof extends Profiler {
38  protected $xhprofData;
39 
44  protected $sprofiler;
45 
67  public function __construct( array $params = [] ) {
68  parent::__construct( $params );
69 
70  // See T180183 and T247332 for why we need the 'running' option.
71  if ( empty( $params['running'] ) ) {
72  $flags = $params['flags'] ?? 0;
73  if ( function_exists( 'xhprof_enable' ) ) {
74  $options = isset( $params['exclude'] )
75  ? [ 'ignored_functions' => $params['exclude'] ]
76  : [];
77  xhprof_enable( $flags, $options );
78  } elseif ( function_exists( 'tideways_xhprof_enable' ) ) {
79  if ( isset( $params['exclude'] ) ) {
80  throw new Exception( 'The exclude option is not supported in tideways_xhprof' );
81  }
82  tideways_xhprof_enable( $flags );
83  } else {
84  throw new Exception( 'Neither xhprof nor tideways_xhprof is installed' );
85  }
86  }
87 
88  $this->sprofiler = new SectionProfiler();
89  }
90 
94  public function getXhprofData() {
95  if ( !$this->xhprofData ) {
96  if ( function_exists( 'xhprof_disable' ) ) {
97  $data = xhprof_disable();
98  } elseif ( function_exists( 'tideways_xhprof_disable' ) ) {
99  $data = tideways_xhprof_disable();
100  } else {
101  throw new Exception( 'Neither xhprof nor tideways_xhprof is installed' );
102  }
103  $this->xhprofData = new XhprofData( $data, $this->params );
104  }
105  return $this->xhprofData;
106  }
107 
108  public function scopedProfileIn( $section ) {
109  $key = 'section.' . ltrim( $section, '.' );
110  return $this->sprofiler->scopedProfileIn( $key );
111  }
112 
116  public function close() {
117  }
118 
125  private function shouldExclude( $name ) {
126  if ( $name === '-total' ) {
127  return true;
128  }
129  if ( !empty( $this->params['include'] ) ) {
130  foreach ( $this->params['include'] as $pattern ) {
131  if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
132  return false;
133  }
134  }
135  return true;
136  }
137  if ( !empty( $this->params['exclude'] ) ) {
138  foreach ( $this->params['exclude'] as $pattern ) {
139  if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
140  return true;
141  }
142  }
143  }
144  return false;
145  }
146 
147  public function getFunctionStats() {
148  $metrics = $this->getXhprofData()->getCompleteMetrics();
149  $profile = [];
150 
151  $main = null; // units in ms
152  foreach ( $metrics as $fname => $stats ) {
153  if ( $this->shouldExclude( $fname ) ) {
154  continue;
155  }
156  // Convert elapsed times from μs to ms to match interface
157  $entry = [
158  'name' => $fname,
159  'calls' => $stats['ct'],
160  'real' => $stats['wt']['total'] / 1000,
161  '%real' => $stats['wt']['percent'],
162  'cpu' => ( $stats['cpu']['total'] ?? 0 ) / 1000,
163  '%cpu' => $stats['cpu']['percent'] ?? 0,
164  'memory' => $stats['mu']['total'] ?? 0,
165  '%memory' => $stats['mu']['percent'] ?? 0,
166  'min_real' => $stats['wt']['min'] / 1000,
167  'max_real' => $stats['wt']['max'] / 1000
168  ];
169  $profile[] = $entry;
170  if ( $fname === 'main()' ) {
171  $main = $entry;
172  }
173  }
174 
175  // Merge in all of the custom profile sections
176  foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
177  if ( $this->shouldExclude( $stats['name'] ) ) {
178  continue;
179  }
180 
181  // @note: getFunctionStats() values already in ms
182  $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
183  $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
184  $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
185  $profile[] = $stats; // assume no section names collide with $metrics
186  }
187 
188  return $profile;
189  }
190 
196  public function getOutput() {
197  return $this->getFunctionReport();
198  }
199 
216  protected function getFunctionReport() {
217  $data = $this->getFunctionStats();
218  usort( $data, static function ( $a, $b ) {
219  return $b['real'] <=> $a['real']; // descending
220  } );
221 
222  $width = 140;
223  $nameWidth = $width - 65;
224  $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
225  $out = [];
226  $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
227  'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
228  );
229  foreach ( $data as $stats ) {
230  $out[] = sprintf( $format,
231  $stats['name'],
232  $stats['calls'],
233  $stats['real'] * 1000,
234  $stats['min_real'] * 1000,
235  $stats['real'] / $stats['calls'] * 1000,
236  $stats['max_real'] * 1000,
237  $stats['%real'],
238  $stats['memory']
239  );
240  }
241  return implode( "\n", $out );
242  }
243 
248  public function getRawData() {
249  return $this->getXhprofData()->getRawData();
250  }
251 }
Profiler that captures all function calls from the XHProf PHP extension.
XhprofData null $xhprofData
getFunctionStats()
Get the aggregated inclusive profiling data for each method.
getRawData()
Retrieve raw data from xhprof.
scopedProfileIn( $section)
Mark the start of a custom profiling frame (e.g.
close()
No-op for xhprof profiling.
__construct(array $params=[])
getFunctionReport()
Get a report of profiled functions sorted by inclusive wall clock time in descending order.
getOutput()
Returns a profiling output to be stored in debug file.
SectionProfiler $sprofiler
Profiler for explicit, arbitrary, frame labels.
Profiler base class that defines the interface and some shared functionality.
Definition: Profiler.php:37
array $params
All of the params passed from $wgProfiler.
Definition: Profiler.php:41
Arbitrary section name based PHP profiling.
Convenience class for working with XHProf profiling data https://github.com/phacility/xhprof.
Definition: XhprofData.php:30