Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / profiler / ProfilerXhprof.php
blob1bf4f545833044096c7b965aef9b5fb57844ec10
1 <?php
2 /**
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
18 * @file
21 /**
22 * Profiler wrapper for XHProf extension.
24 * @code
25 * $wgProfiler['class'] = 'ProfilerXhprof';
26 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
27 * $wgProfiler['output'] = 'text';
28 * $wgProfiler['visible'] = true;
29 * @endcode
31 * @code
32 * $wgProfiler['class'] = 'ProfilerXhprof';
33 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
34 * $wgProfiler['output'] = 'udp';
35 * @endcode
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.
52 * @ingroup Profiler
53 * @see Xhprof
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 {
59 /**
60 * @var XhprofData|null $xhprofData
62 protected $xhprofData;
64 /**
65 * Profiler for explicit, arbitrary, frame labels
66 * @var SectionProfiler
68 protected $sprofiler;
70 /**
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();
84 /**
85 * @return XhprofData
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 );
99 /**
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.
109 * @return bool
111 private function shouldExclude( $name ) {
112 if ( $name === '-total' ) {
113 return true;
115 if ( !empty( $this->params['include'] ) ) {
116 foreach ( $this->params['include'] as $pattern ) {
117 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
118 return false;
121 return true;
123 if ( !empty( $this->params['exclude'] ) ) {
124 foreach ( $this->params['exclude'] as $pattern ) {
125 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
126 return true;
130 return false;
133 public function getFunctionStats() {
134 $metrics = $this->getXhprofData()->getCompleteMetrics();
135 $profile = [];
137 $main = null; // units in ms
138 foreach ( $metrics as $fname => $stats ) {
139 if ( $this->shouldExclude( $fname ) ) {
140 continue;
142 // Convert elapsed times from μs to ms to match interface
143 $entry = [
144 'name' => $fname,
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
155 $profile[] = $entry;
156 if ( $fname === 'main()' ) {
157 $main = $entry;
161 // Merge in all of the custom profile sections
162 foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
163 if ( $this->shouldExclude( $stats['name'] ) ) {
164 continue;
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
174 return $profile;
178 * Returns a profiling output to be stored in debug file
180 * @return string
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:
191 * - Function name
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
200 * @return string
202 protected function getFunctionReport() {
203 $data = $this->getFunctionStats();
204 usort( $data, function( $a, $b ) {
205 if ( $a['real'] === $b['real'] ) {
206 return 0;
208 return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
209 } );
211 $width = 140;
212 $nameWidth = $width - 65;
213 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
214 $out = [];
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,
220 $stats['name'],
221 $stats['calls'],
222 $stats['real'] * 1000,
223 $stats['min_real'] * 1000,
224 $stats['real'] / $stats['calls'] * 1000,
225 $stats['max_real'] * 1000,
226 $stats['%real'],
227 $stats['memory']
230 return implode( "\n", $out );
234 * Retrieve raw data from xhprof
235 * @return array
237 public function getRawData() {
238 return $this->getXhprofData()->getRawData();