Merge "Adding basic profiler sampling support and restored the --profiler script...
[lhc/web/wiklou.git] / includes / profiler / ProfilerXhprof.php
1 <?php
2 /**
3 * @section LICENSE
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.
8 *
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.
13 *
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
18 *
19 * @file
20 */
21
22 /**
23 * Profiler wrapper for XHProf extension.
24 *
25 * Mimics the output of ProfilerStandard using data collected via the XHProf
26 * PHP extension.
27 *
28 * @code
29 * $wgProfiler['class'] = 'ProfilerXhprof';
30 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
31 * $wgProfiler['output'] = 'text';
32 * $wgProfiler['visible'] = true;
33 * @endcode
34 *
35 * @code
36 * $wgProfiler['class'] = 'ProfilerXhprof';
37 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
38 * $wgProfiler['output'] = 'udp';
39 * @endcode
40 *
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.
45 *
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.
54 *
55 * @author Bryan Davis <bd808@wikimedia.org>
56 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
57 * @ingroup Profiler
58 * @see Xhprof
59 * @see https://php.net/xhprof
60 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
61 */
62 class ProfilerXhprof extends Profiler {
63
64 /**
65 * @var Xhprof $xhprof
66 */
67 protected $xhprof;
68
69 /**
70 * Type of report to send when logData() is called.
71 * @var string $logType
72 */
73 protected $logType;
74
75 /**
76 * Should profile report sent to in page content be visible?
77 * @var bool $visible
78 */
79 protected $visible;
80
81 /**
82 * @param array $params
83 * @see Xhprof::__construct()
84 */
85 public function __construct( array $params = array() ) {
86 $params = array_merge(
87 array(
88 'log' => 'text',
89 'visible' => false
90 ),
91 $params
92 );
93 parent::__construct( $params );
94 $this->logType = $params['log'];
95 $this->visible = $params['visible'];
96 $this->xhprof = new Xhprof( $params );
97 }
98
99 public function isStub() {
100 return false;
101 }
102
103 /**
104 * No-op for xhprof profiling.
105 *
106 * Use the 'include' configuration key instead if you need to constrain
107 * the functions that are profiled.
108 *
109 * @param string $functionname
110 */
111 public function profileIn( $functionname ) {
112 }
113
114 /**
115 * No-op for xhprof profiling.
116 *
117 * Use the 'include' configuration key instead if you need to constrain
118 * the functions that are profiled.
119 *
120 * @param string $functionname
121 */
122 public function profileOut( $functionname ) {
123 }
124
125 /**
126 * No-op for xhprof profiling.
127 */
128 public function close() {
129 }
130
131 public function getFunctionStats() {
132 $metrics = $this->xhprof->getCompleteMetrics();
133 $profile = array();
134
135 foreach ( $metrics as $fname => $stats ) {
136 // Convert elapsed times from μs to ms to match ProfilerStandard
137 $profile[] = array(
138 'name' => $fname,
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
148 );
149 }
150
151 return $profile;
152 }
153
154 /**
155 * Returns a profiling output to be stored in debug file
156 *
157 * @return string
158 */
159 public function getOutput() {
160 return $this->getFunctionReport();
161 }
162
163 /**
164 * Get a report of profiled functions sorted by inclusive wall clock time
165 * in descending order.
166 *
167 * Each line of the report includes this data:
168 * - Function name
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
176 *
177 * @return string
178 */
179 protected function getFunctionReport() {
180 $data = $this->xhprof->getInclusiveMetrics();
181 uasort( $data, Xhprof::makeSortFunction( 'wt', 'total' ) );
182
183 $width = 140;
184 $nameWidth = $width - 65;
185 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
186 $out = array();
187 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
188 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
189 );
190 foreach ( $data as $func => $stats ) {
191 $out[] = sprintf( $format,
192 $func,
193 $stats['ct'],
194 $stats['wt']['total'],
195 $stats['wt']['min'],
196 $stats['wt']['mean'],
197 $stats['wt']['max'],
198 $stats['wt']['percent'],
199 isset( $stats['mu'] ) ? $stats['mu']['total'] : 0
200 );
201 }
202 return implode( "\n", $out );
203 }
204
205 /**
206 * Get a brief report of profiled functions sorted by inclusive wall clock
207 * time in descending order.
208 *
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
213 * - Function name
214 *
215 * @param string $header Header text to prepend to report
216 * @param string $footer Footer text to append to report
217 * @return string
218 */
219 protected function getSummaryReport( $header = '', $footer = '' ) {
220 $data = $this->xhprof->getInclusiveMetrics();
221 uasort( $data, Xhprof::makeSortFunction( 'wt', 'total' ) );
222
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,
229 $stats['ct'],
230 $func
231 );
232 }
233 $out[] = $footer;
234 return implode( "\n", $out );
235 }
236 }