Merge "Removed some unnecessary code in LocalFileDeleteBatch"
[lhc/web/wiklou.git] / includes / profiler / ProfilerXhprof.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 /**
22 * Profiler wrapper for XHProf extension.
23 *
24 * Mimics the output of ProfilerStandard using data collected via the XHProf
25 * PHP extension.
26 *
27 * @code
28 * $wgProfiler['class'] = 'ProfilerXhprof';
29 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
30 * $wgProfiler['output'] = 'text';
31 * $wgProfiler['visible'] = true;
32 * @endcode
33 *
34 * @code
35 * $wgProfiler['class'] = 'ProfilerXhprof';
36 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
37 * $wgProfiler['output'] = 'udp';
38 * @endcode
39 *
40 * Rather than obeying wfProfileIn() and wfProfileOut() calls placed in the
41 * application code, ProfilerXhprof profiles all functions using the XHProf
42 * PHP extenstion. For PHP5 users, this extension can be installed via PECL or
43 * your operating system's package manager. XHProf support is built into HHVM.
44 *
45 * To restrict the functions for which profiling data is collected, you can
46 * use either a whitelist ($wgProfiler['include']) or a blacklist
47 * ($wgProfiler['exclude']) containing an array of function names. The
48 * blacklist functionality is built into HHVM and will completely exclude the
49 * named functions from profiling collection. The whitelist is implemented by
50 * Xhprof class which will filter the data collected by XHProf before reporting.
51 * See documentation for the Xhprof class and the XHProf extension for
52 * additional information.
53 *
54 * @author Bryan Davis <bd808@wikimedia.org>
55 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
56 * @ingroup Profiler
57 * @see Xhprof
58 * @see https://php.net/xhprof
59 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
60 */
61 class ProfilerXhprof extends Profiler {
62 /**
63 * @var Xhprof $xhprof
64 */
65 protected $xhprof;
66
67 /**
68 * Profiler for explicit, arbitrary, frame labels
69 * @var SectionProfiler
70 */
71 protected $sprofiler;
72
73 /**
74 * @param array $params
75 * @see Xhprof::__construct()
76 */
77 public function __construct( array $params = array() ) {
78 parent::__construct( $params );
79 $this->xhprof = new Xhprof( $params );
80 $this->sprofiler = new SectionProfiler();
81 }
82
83 /**
84 * No-op for xhprof profiling.
85 *
86 * Use the 'include' configuration key instead if you need to constrain
87 * the functions that are profiled.
88 *
89 * @param string $functionname
90 */
91 public function profileIn( $functionname ) {
92 }
93
94 /**
95 * No-op for xhprof profiling.
96 *
97 * Use the 'include' configuration key instead if you need to constrain
98 * the functions that are profiled.
99 *
100 * @param string $functionname
101 */
102 public function profileOut( $functionname ) {
103 }
104
105 public function scopedProfileIn( $section ) {
106 return $this->sprofiler->scopedProfileIn( $section );
107 }
108
109 /**
110 * No-op for xhprof profiling.
111 */
112 public function close() {
113 }
114
115 public function getFunctionStats() {
116 $metrics = $this->xhprof->getCompleteMetrics();
117 $profile = array();
118
119 $main = null; // units in ms
120 foreach ( $metrics as $fname => $stats ) {
121 // Convert elapsed times from μs to ms to match ProfilerStandard
122 $entry = array(
123 'name' => $fname,
124 'calls' => $stats['ct'],
125 'real' => $stats['wt']['total'] / 1000,
126 '%real' => $stats['wt']['percent'],
127 'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
128 '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
129 'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
130 '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
131 'min_real' => $stats['wt']['min'] / 1000,
132 'max_real' => $stats['wt']['max'] / 1000
133 );
134 $profile[] = $entry;
135 if ( $fname === 'main()' ) {
136 $main = $entry;
137 }
138 }
139
140 // Merge in all of the custom profile sections
141 foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
142 // @note: getFunctionStats() values already in ms
143 $stats['%real'] = $stats['real'] / $main['real'];
144 $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
145 $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
146 $profile[] = $stats; // assume no section names collide with $metrics
147 }
148
149 return $profile;
150 }
151
152 /**
153 * Returns a profiling output to be stored in debug file
154 *
155 * @return string
156 */
157 public function getOutput() {
158 return $this->getFunctionReport();
159 }
160
161 /**
162 * Get a report of profiled functions sorted by inclusive wall clock time
163 * in descending order.
164 *
165 * Each line of the report includes this data:
166 * - Function name
167 * - Number of times function was called
168 * - Total wall clock time spent in function in microseconds
169 * - Minimum wall clock time spent in function in microseconds
170 * - Average wall clock time spent in function in microseconds
171 * - Maximum wall clock time spent in function in microseconds
172 * - Percentage of total wall clock time spent in function
173 * - Total delta of memory usage from start to end of function in bytes
174 *
175 * @return string
176 */
177 protected function getFunctionReport() {
178 $data = $this->getFunctionStats();
179 usort( $data, function( $a, $b ) {
180 if ( $a['real'] === $b['real'] ) {
181 return 0;
182 }
183 return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
184 } );
185
186 $width = 140;
187 $nameWidth = $width - 65;
188 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
189 $out = array();
190 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
191 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
192 );
193 foreach ( $data as $stats ) {
194 $out[] = sprintf( $format,
195 $stats['name'],
196 $stats['calls'],
197 $stats['real'] * 1000,
198 $stats['min_real'] * 1000,
199 $stats['real'] / $stats['calls'] * 1000,
200 $stats['max_real'] * 1000,
201 $stats['%real'],
202 $stats['memory']
203 );
204 }
205 return implode( "\n", $out );
206 }
207 }