4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
23 * Profiler wrapper for XHProf extension.
25 * Mimics the output of ProfilerStandard using data collected via the XHProf
29 * $wgProfiler['class'] = 'ProfilerXhprof';
30 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
31 * $wgProfiler['output'] = 'text';
32 * $wgProfiler['visible'] = true;
36 * $wgProfiler['class'] = 'ProfilerXhprof';
37 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
38 * $wgProfiler['output'] = 'udp';
41 * Rather than obeying wfProfileIn() and wfProfileOut() calls placed in the
42 * application code, ProfilerXhprof profiles all functions using the XHProf
43 * PHP extenstion. For PHP5 users, this extension can be installed via PECL or
44 * your operating system's package manager. XHProf support is built into HHVM.
46 * To restrict the functions for which profiling data is collected, you can
47 * use either a whitelist ($wgProfiler['include']) or a blacklist
48 * ($wgProfiler['exclude']) containing an array of function names. The
49 * blacklist functionality is built into HHVM and will completely exclude the
50 * named functions from profiling collection. The whitelist is implemented by
51 * Xhprof class which will filter the data collected by XHProf before reporting.
52 * See documentation for the Xhprof class and the XHProf extension for
53 * additional information.
55 * @author Bryan Davis <bd808@wikimedia.org>
56 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
59 * @see https://php.net/xhprof
60 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
62 class ProfilerXhprof
extends Profiler
{
70 * Type of report to send when logData() is called.
71 * @var string $logType
76 * Should profile report sent to in page content be visible?
82 * @param array $params
83 * @see Xhprof::__construct()
85 public function __construct( array $params = array() ) {
86 $params = array_merge(
93 parent
::__construct( $params );
94 $this->logType
= $params['log'];
95 $this->visible
= $params['visible'];
96 $this->xhprof
= new Xhprof( $params );
99 public function isStub() {
104 * No-op for xhprof profiling.
106 * Use the 'include' configuration key instead if you need to constrain
107 * the functions that are profiled.
109 * @param string $functionname
111 public function profileIn( $functionname ) {
115 * No-op for xhprof profiling.
117 * Use the 'include' configuration key instead if you need to constrain
118 * the functions that are profiled.
120 * @param string $functionname
122 public function profileOut( $functionname ) {
126 * No-op for xhprof profiling.
128 public function close() {
131 public function getFunctionStats() {
132 $metrics = $this->xhprof
->getCompleteMetrics();
135 foreach ( $metrics as $fname => $stats ) {
136 // Convert elapsed times from μs to ms to match ProfilerStandard
139 'calls' => $stats['ct'],
140 'real' => $stats['wt']['total'] / 1000,
141 '%real' => $stats['wt']['percent'],
142 'cpu' => isset( $stats['cpu'] ) ?
$stats['cpu']['total'] / 1000 : 0,
143 '%cpu' => isset( $stats['cpu'] ) ?
$stats['cpu']['percent'] : 0,
144 'memory' => isset( $stats['mu'] ) ?
$stats['mu']['total'] : 0,
145 '%memory' => isset( $stats['mu'] ) ?
$stats['mu']['percent'] : 0,
146 'min' => $stats['wt']['min'] / 1000,
147 'max' => $stats['wt']['max'] / 1000
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->xhprof
->getInclusiveMetrics();
181 uasort( $data, Xhprof
::makeSortFunction( 'wt', 'total' ) );
184 $nameWidth = $width - 65;
185 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
187 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
188 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
190 foreach ( $data as $func => $stats ) {
191 $out[] = sprintf( $format,
194 $stats['wt']['total'],
196 $stats['wt']['mean'],
198 $stats['wt']['percent'],
199 isset( $stats['mu'] ) ?
$stats['mu']['total'] : 0
202 return implode( "\n", $out );
206 * Get a brief report of profiled functions sorted by inclusive wall clock
207 * time in descending order.
209 * Each line of the report includes this data:
210 * - Percentage of total wall clock time spent in function
211 * - Total wall clock time spent in function in seconds
212 * - Number of times function was called
215 * @param string $header Header text to prepend to report
216 * @param string $footer Footer text to append to report
219 protected function getSummaryReport( $header = '', $footer = '' ) {
220 $data = $this->xhprof
->getInclusiveMetrics();
221 uasort( $data, Xhprof
::makeSortFunction( 'wt', 'total' ) );
223 $format = '%6.2f%% %3.6f %6d - %s';
224 $out = array( $header );
225 foreach ( $data as $func => $stats ) {
226 $out[] = sprintf( $format,
227 $stats['wt']['percent'],
228 $stats['wt']['total'] / 1e6
,
234 return implode( "\n", $out );