Merge "Fix escaping for MSSQL LIKE queries."
[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 * ProfilerXhprof profiles all functions using the XHProf PHP extenstion.
38 * For PHP5 users, this extension can be installed via PECL or your operating
39 * system's package manager. XHProf support is built into HHVM.
40 *
41 * To restrict the functions for which profiling data is collected, you can
42 * use either a whitelist ($wgProfiler['include']) or a blacklist
43 * ($wgProfiler['exclude']) containing an array of function names.
44 * Shell-style patterns are also accepted.
45 *
46 * @author Bryan Davis <bd808@wikimedia.org>
47 * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
48 * @ingroup Profiler
49 * @see Xhprof
50 * @see https://php.net/xhprof
51 * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
52 */
53 class ProfilerXhprof extends Profiler {
54 /**
55 * @var Xhprof $xhprof
56 */
57 protected $xhprof;
58
59 /**
60 * Profiler for explicit, arbitrary, frame labels
61 * @var SectionProfiler
62 */
63 protected $sprofiler;
64
65 /**
66 * @param array $params
67 * @see Xhprof::__construct()
68 */
69 public function __construct( array $params = [] ) {
70 parent::__construct( $params );
71 $this->xhprof = new Xhprof( $params );
72 $this->sprofiler = new SectionProfiler();
73 }
74
75 public function scopedProfileIn( $section ) {
76 $key = 'section.' . ltrim( $section, '.' );
77 return $this->sprofiler->scopedProfileIn( $key );
78 }
79
80 /**
81 * No-op for xhprof profiling.
82 */
83 public function close() {
84 }
85
86 /**
87 * Check if a function or section should be excluded from the output.
88 *
89 * @param string $name Function or section name.
90 * @return bool
91 */
92 private function shouldExclude( $name ) {
93 if ( $name === '-total' ) {
94 return true;
95 }
96 if ( !empty( $this->params['include'] ) ) {
97 foreach ( $this->params['include'] as $pattern ) {
98 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
99 return false;
100 }
101 }
102 return true;
103 }
104 if ( !empty( $this->params['exclude'] ) ) {
105 foreach ( $this->params['exclude'] as $pattern ) {
106 if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
107 return true;
108 }
109 }
110 }
111 return false;
112 }
113
114 public function getFunctionStats() {
115 $metrics = $this->xhprof->getCompleteMetrics();
116 $profile = [];
117
118 $main = null; // units in ms
119 foreach ( $metrics as $fname => $stats ) {
120 if ( $this->shouldExclude( $fname ) ) {
121 continue;
122 }
123 // Convert elapsed times from μs to ms to match interface
124 $entry = [
125 'name' => $fname,
126 'calls' => $stats['ct'],
127 'real' => $stats['wt']['total'] / 1000,
128 '%real' => $stats['wt']['percent'],
129 'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
130 '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
131 'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
132 '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
133 'min_real' => $stats['wt']['min'] / 1000,
134 'max_real' => $stats['wt']['max'] / 1000
135 ];
136 $profile[] = $entry;
137 if ( $fname === 'main()' ) {
138 $main = $entry;
139 }
140 }
141
142 // Merge in all of the custom profile sections
143 foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
144 if ( $this->shouldExclude( $stats['name'] ) ) {
145 continue;
146 }
147
148 // @note: getFunctionStats() values already in ms
149 $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
150 $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
151 $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
152 $profile[] = $stats; // assume no section names collide with $metrics
153 }
154
155 return $profile;
156 }
157
158 /**
159 * Returns a profiling output to be stored in debug file
160 *
161 * @return string
162 */
163 public function getOutput() {
164 return $this->getFunctionReport();
165 }
166
167 /**
168 * Get a report of profiled functions sorted by inclusive wall clock time
169 * in descending order.
170 *
171 * Each line of the report includes this data:
172 * - Function name
173 * - Number of times function was called
174 * - Total wall clock time spent in function in microseconds
175 * - Minimum wall clock time spent in function in microseconds
176 * - Average wall clock time spent in function in microseconds
177 * - Maximum wall clock time spent in function in microseconds
178 * - Percentage of total wall clock time spent in function
179 * - Total delta of memory usage from start to end of function in bytes
180 *
181 * @return string
182 */
183 protected function getFunctionReport() {
184 $data = $this->getFunctionStats();
185 usort( $data, function( $a, $b ) {
186 if ( $a['real'] === $b['real'] ) {
187 return 0;
188 }
189 return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
190 } );
191
192 $width = 140;
193 $nameWidth = $width - 65;
194 $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
195 $out = [];
196 $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
197 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
198 );
199 foreach ( $data as $stats ) {
200 $out[] = sprintf( $format,
201 $stats['name'],
202 $stats['calls'],
203 $stats['real'] * 1000,
204 $stats['min_real'] * 1000,
205 $stats['real'] / $stats['calls'] * 1000,
206 $stats['max_real'] * 1000,
207 $stats['%real'],
208 $stats['memory']
209 );
210 }
211 return implode( "\n", $out );
212 }
213
214 /**
215 * Retrieve raw data from xhprof
216 * @return array
217 */
218 public function getRawData() {
219 return $this->xhprof->getRawData();
220 }
221 }