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 * Rather than obeying wfProfileIn() and wfProfileOut() calls placed in the
38 * application code, ProfilerXhprof profiles all functions using the XHProf
39 * PHP extenstion. For PHP5 users, this extension can be installed via PECL or
40 * your operating system's package manager. XHProf support is built into HHVM.
42 * To restrict the functions for which profiling data is collected, you can
43 * use either a whitelist ($wgProfiler['include']) or a blacklist
44 * ($wgProfiler['exclude']) containing an array of function names. The
45 * blacklist functionality is built into HHVM and will completely exclude the
46 * named functions from profiling collection. The whitelist is implemented by
47 * Xhprof class which will filter the data collected by XHProf before reporting.
48 * See documentation for the Xhprof class and the XHProf extension for
49 * additional information.
51 * @author Bryan Davis <bd808@wikimedia.org>
52 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
55 * @see https://php.net/xhprof
56 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
58 class ProfilerXhprof
extends Profiler
{
65 * Profiler for explicit, arbitrary, frame labels
66 * @var SectionProfiler
71 * @param array $params
72 * @see Xhprof::__construct()
74 public function __construct( array $params = array() ) {
75 parent
::__construct( $params );
76 $this->xhprof
= new Xhprof( $params );
77 $this->sprofiler
= new SectionProfiler();
81 * No-op for xhprof profiling.
83 * Use the 'include' configuration key instead if you need to constrain
84 * the functions that are profiled.
86 * @param string $functionname
88 public function profileIn( $functionname ) {
92 * No-op for xhprof profiling.
94 * Use the 'include' configuration key instead if you need to constrain
95 * the functions that are profiled.
97 * @param string $functionname
99 public function profileOut( $functionname ) {
102 public function scopedProfileIn( $section ) {
103 return $this->sprofiler
->scopedProfileIn( $section );
107 * No-op for xhprof profiling.
109 public function close() {
112 public function getFunctionStats() {
113 $metrics = $this->xhprof
->getCompleteMetrics();
116 $main = null; // units in ms
117 foreach ( $metrics as $fname => $stats ) {
118 // Convert elapsed times from μs to ms to match interface
121 'calls' => $stats['ct'],
122 'real' => $stats['wt']['total'] / 1000,
123 '%real' => $stats['wt']['percent'],
124 'cpu' => isset( $stats['cpu'] ) ?
$stats['cpu']['total'] / 1000 : 0,
125 '%cpu' => isset( $stats['cpu'] ) ?
$stats['cpu']['percent'] : 0,
126 'memory' => isset( $stats['mu'] ) ?
$stats['mu']['total'] : 0,
127 '%memory' => isset( $stats['mu'] ) ?
$stats['mu']['percent'] : 0,
128 'min_real' => $stats['wt']['min'] / 1000,
129 'max_real' => $stats['wt']['max'] / 1000
132 if ( $fname === 'main()' ) {
137 // Merge in all of the custom profile sections
138 foreach ( $this->sprofiler
->getFunctionStats() as $stats ) {
139 if ( $stats['name'] === '-total' ) {
140 // Discard section profiler running totals
144 // @note: getFunctionStats() values already in ms
145 $stats['%real'] = $main['real'] ?
$stats['real'] / $main['real'] * 100 : 0;
146 $stats['%cpu'] = $main['cpu'] ?
$stats['cpu'] / $main['cpu'] * 100 : 0;
147 $stats['%memory'] = $main['memory'] ?
$stats['memory'] / $main['memory'] * 100 : 0;
148 $profile[] = $stats; // assume no section names collide with $metrics
155 * Returns a profiling output to be stored in debug file
159 public function getOutput() {
160 return $this->getFunctionReport();
164 * Get a report of profiled functions sorted by inclusive wall clock time
165 * in descending order.
167 * Each line of the report includes this data:
169 * - Number of times function was called
170 * - Total wall clock time spent in function in microseconds
171 * - Minimum wall clock time spent in function in microseconds
172 * - Average wall clock time spent in function in microseconds
173 * - Maximum wall clock time spent in function in microseconds
174 * - Percentage of total wall clock time spent in function
175 * - Total delta of memory usage from start to end of function in bytes
179 protected function getFunctionReport() {
180 $data = $this->getFunctionStats();
181 usort( $data, function( $a, $b ) {
182 if ( $a['real'] === $b['real'] ) {
185 return ( $a['real'] > $b['real'] ) ?
-1 : 1; // descending
189 $nameWidth = $width - 65;
190 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
192 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
193 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
195 foreach ( $data as $stats ) {
196 $out[] = sprintf( $format,
199 $stats['real'] * 1000,
200 $stats['min_real'] * 1000,
201 $stats['real'] / $stats['calls'] * 1000,
202 $stats['max_real'] * 1000,
207 return implode( "\n", $out );