64d2de71be301643fefdfe344b662bab0acdde2f
[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 * @code
25 * $wgProfiler['class'] = 'ProfilerXhprof';
26 * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
27 * $wgProfiler['output'] = 'text';
28 * $wgProfiler['visible'] = true;
29 * @endcode
30 *
31 * @code
32 * $wgProfiler['class'] = 'ProfilerXhprof';
33 * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
34 * $wgProfiler['output'] = 'udp';
35 * @endcode
36 *
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.
41 *
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.
50 *
51 * @author Bryan Davis <bd808@wikimedia.org>
52 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
53 * @ingroup Profiler
54 * @see Xhprof
55 * @see https://php.net/xhprof
56 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
57 */
58 class ProfilerXhprof extends Profiler {
59 /**
60 * @var Xhprof $xhprof
61 */
62 protected $xhprof;
63
64 /**
65 * Profiler for explicit, arbitrary, frame labels
66 * @var SectionProfiler
67 */
68 protected $sprofiler;
69
70 /**
71 * @param array $params
72 * @see Xhprof::__construct()
73 */
74 public function __construct( array $params = array() ) {
75 parent::__construct( $params );
76 $this->xhprof = new Xhprof( $params );
77 $this->sprofiler = new SectionProfiler();
78 }
79
80 /**
81 * No-op for xhprof profiling.
82 *
83 * Use the 'include' configuration key instead if you need to constrain
84 * the functions that are profiled.
85 *
86 * @param string $functionname
87 */
88 public function profileIn( $functionname ) {
89 }
90
91 /**
92 * No-op for xhprof profiling.
93 *
94 * Use the 'include' configuration key instead if you need to constrain
95 * the functions that are profiled.
96 *
97 * @param string $functionname
98 */
99 public function profileOut( $functionname ) {
100 }
101
102 public function scopedProfileIn( $section ) {
103 return $this->sprofiler->scopedProfileIn( $section );
104 }
105
106 /**
107 * No-op for xhprof profiling.
108 */
109 public function close() {
110 }
111
112 public function getFunctionStats() {
113 $metrics = $this->xhprof->getCompleteMetrics();
114 $profile = array();
115
116 $main = null; // units in ms
117 foreach ( $metrics as $fname => $stats ) {
118 // Convert elapsed times from μs to ms to match interface
119 $entry = array(
120 'name' => $fname,
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
130 );
131 $profile[] = $entry;
132 if ( $fname === 'main()' ) {
133 $main = $entry;
134 }
135 }
136
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
141 continue;
142 }
143
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
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->getFunctionStats();
181 usort( $data, function( $a, $b ) {
182 if ( $a['real'] === $b['real'] ) {
183 return 0;
184 }
185 return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
186 } );
187
188 $width = 140;
189 $nameWidth = $width - 65;
190 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
191 $out = array();
192 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
193 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
194 );
195 foreach ( $data as $stats ) {
196 $out[] = sprintf( $format,
197 $stats['name'],
198 $stats['calls'],
199 $stats['real'] * 1000,
200 $stats['min_real'] * 1000,
201 $stats['real'] / $stats['calls'] * 1000,
202 $stats['max_real'] * 1000,
203 $stats['%real'],
204 $stats['memory']
205 );
206 }
207 return implode( "\n", $out );
208 }
209 }