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. The
44 * blacklist functionality is built into HHVM and will completely exclude the
45 * named functions from profiling collection. The whitelist is implemented by
46 * Xhprof class which will filter the data collected by XHProf before reporting.
47 * See documentation for the Xhprof class and the XHProf extension for
48 * additional information.
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
57 class ProfilerXhprof
extends Profiler
{
64 * Profiler for explicit, arbitrary, frame labels
65 * @var SectionProfiler
70 * @param array $params
71 * @see Xhprof::__construct()
73 public function __construct( array $params = array() ) {
74 parent
::__construct( $params );
75 $this->xhprof
= new Xhprof( $params );
76 $this->sprofiler
= new SectionProfiler();
79 public function scopedProfileIn( $section ) {
80 return $this->sprofiler
->scopedProfileIn( $section );
84 * No-op for xhprof profiling.
86 public function close() {
89 public function getFunctionStats() {
90 $metrics = $this->xhprof
->getCompleteMetrics();
93 $main = null; // units in ms
94 foreach ( $metrics as $fname => $stats ) {
95 // Convert elapsed times from μs to ms to match interface
98 'calls' => $stats['ct'],
99 'real' => $stats['wt']['total'] / 1000,
100 '%real' => $stats['wt']['percent'],
101 'cpu' => isset( $stats['cpu'] ) ?
$stats['cpu']['total'] / 1000 : 0,
102 '%cpu' => isset( $stats['cpu'] ) ?
$stats['cpu']['percent'] : 0,
103 'memory' => isset( $stats['mu'] ) ?
$stats['mu']['total'] : 0,
104 '%memory' => isset( $stats['mu'] ) ?
$stats['mu']['percent'] : 0,
105 'min_real' => $stats['wt']['min'] / 1000,
106 'max_real' => $stats['wt']['max'] / 1000
109 if ( $fname === 'main()' ) {
114 // Merge in all of the custom profile sections
115 foreach ( $this->sprofiler
->getFunctionStats() as $stats ) {
116 if ( $stats['name'] === '-total' ) {
117 // Discard section profiler running totals
121 // @note: getFunctionStats() values already in ms
122 $stats['%real'] = $main['real'] ?
$stats['real'] / $main['real'] * 100 : 0;
123 $stats['%cpu'] = $main['cpu'] ?
$stats['cpu'] / $main['cpu'] * 100 : 0;
124 $stats['%memory'] = $main['memory'] ?
$stats['memory'] / $main['memory'] * 100 : 0;
125 $profile[] = $stats; // assume no section names collide with $metrics
132 * Returns a profiling output to be stored in debug file
136 public function getOutput() {
137 return $this->getFunctionReport();
141 * Get a report of profiled functions sorted by inclusive wall clock time
142 * in descending order.
144 * Each line of the report includes this data:
146 * - Number of times function was called
147 * - Total wall clock time spent in function in microseconds
148 * - Minimum wall clock time spent in function in microseconds
149 * - Average wall clock time spent in function in microseconds
150 * - Maximum wall clock time spent in function in microseconds
151 * - Percentage of total wall clock time spent in function
152 * - Total delta of memory usage from start to end of function in bytes
156 protected function getFunctionReport() {
157 $data = $this->getFunctionStats();
158 usort( $data, function( $a, $b ) {
159 if ( $a['real'] === $b['real'] ) {
162 return ( $a['real'] > $b['real'] ) ?
-1 : 1; // descending
166 $nameWidth = $width - 65;
167 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
169 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
170 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
172 foreach ( $data as $stats ) {
173 $out[] = sprintf( $format,
176 $stats['real'] * 1000,
177 $stats['min_real'] * 1000,
178 $stats['real'] / $stats['calls'] * 1000,
179 $stats['max_real'] * 1000,
184 return implode( "\n", $out );