Remove back-compat profiling configuration
[lhc/web/wiklou.git] / includes / profiler / Profiler.php
1 <?php
2 /**
3 * Base class and functions for profiling.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Profiler
22 * @defgroup Profiler Profiler
23 */
24
25 /**
26 * Get system resource usage of current request context.
27 * Invokes the getrusage(2) system call, requesting RUSAGE_SELF if on PHP5
28 * or RUSAGE_THREAD if on HHVM. Returns false if getrusage is not available.
29 *
30 * @since 1.24
31 * @return array|bool Resource usage data or false if no data available.
32 */
33 function wfGetRusage() {
34 if ( !function_exists( 'getrusage' ) ) {
35 return false;
36 } elseif ( defined ( 'HHVM_VERSION' ) ) {
37 return getrusage( 2 /* RUSAGE_THREAD */ );
38 } else {
39 return getrusage( 0 /* RUSAGE_SELF */ );
40 }
41 }
42
43 /**
44 * Begin profiling of a function
45 * @param string $functionname Name of the function we will profile
46 */
47 function wfProfileIn( $functionname ) {
48 if ( Profiler::$__instance === null ) { // use this directly to reduce overhead
49 Profiler::instance();
50 }
51 if ( !( Profiler::$__instance instanceof ProfilerStub ) ) {
52 Profiler::$__instance->profileIn( $functionname );
53 }
54 }
55
56 /**
57 * Stop profiling of a function
58 * @param string $functionname Name of the function we have profiled
59 */
60 function wfProfileOut( $functionname = 'missing' ) {
61 if ( Profiler::$__instance === null ) { // use this directly to reduce overhead
62 Profiler::instance();
63 }
64 if ( !( Profiler::$__instance instanceof ProfilerStub ) ) {
65 Profiler::$__instance->profileOut( $functionname );
66 }
67 }
68
69 /**
70 * Class for handling function-scope profiling
71 *
72 * @since 1.22
73 */
74 class ProfileSection {
75 protected $name; // string; method name
76 protected $enabled = false; // boolean; whether profiling is enabled
77
78 /**
79 * Begin profiling of a function and return an object that ends profiling of
80 * the function when that object leaves scope. As long as the object is not
81 * specifically linked to other objects, it will fall out of scope at the same
82 * moment that the function to be profiled terminates.
83 *
84 * This is typically called like:
85 * <code>$section = new ProfileSection( __METHOD__ );</code>
86 *
87 * @param string $name Name of the function to profile
88 */
89 public function __construct( $name ) {
90 $this->name = $name;
91 if ( Profiler::$__instance === null ) { // use this directly to reduce overhead
92 Profiler::instance();
93 }
94 if ( !( Profiler::$__instance instanceof ProfilerStub ) ) {
95 $this->enabled = true;
96 Profiler::$__instance->profileIn( $this->name );
97 }
98 }
99
100 function __destruct() {
101 if ( $this->enabled ) {
102 Profiler::$__instance->profileOut( $this->name );
103 }
104 }
105 }
106
107 /**
108 * Profiler base class that defines the interface and some trivial functionality
109 *
110 * @ingroup Profiler
111 */
112 abstract class Profiler {
113 /** @var string|bool Profiler ID for bucketing data */
114 protected $mProfileID = false;
115 /** @var bool Whether MediaWiki is in a SkinTemplate output context */
116 protected $mTemplated = false;
117
118 /** @var TransactionProfiler */
119 protected $trxProfiler;
120
121 // @codingStandardsIgnoreStart PSR2.Classes.PropertyDeclaration.Underscore
122 /** @var Profiler Do not call this outside Profiler and ProfileSection */
123 public static $__instance = null;
124 // @codingStandardsIgnoreEnd
125
126 /**
127 * @param array $params
128 */
129 public function __construct( array $params ) {
130 if ( isset( $params['profileID'] ) ) {
131 $this->mProfileID = $params['profileID'];
132 }
133 $this->trxProfiler = new TransactionProfiler();
134 }
135
136 /**
137 * Singleton
138 * @return Profiler
139 */
140 final public static function instance() {
141 if ( self::$__instance === null ) {
142 global $wgProfiler;
143 if ( is_array( $wgProfiler ) ) {
144 if ( !isset( $wgProfiler['class'] ) ) {
145 $class = 'ProfilerStub';
146 } else {
147 $class = $wgProfiler['class'];
148 }
149 self::$__instance = new $class( $wgProfiler );
150 } else {
151 self::$__instance = new ProfilerStub( array() );
152 }
153 }
154 return self::$__instance;
155 }
156
157 /**
158 * Set the profiler to a specific profiler instance. Mostly for dumpHTML
159 * @param Profiler $p
160 */
161 final public static function setInstance( Profiler $p ) {
162 self::$__instance = $p;
163 }
164
165 /**
166 * Return whether this a stub profiler
167 *
168 * @return bool
169 */
170 abstract public function isStub();
171
172 /**
173 * Return whether this profiler stores data
174 *
175 * Called by Parser::braceSubstitution. If true, the parser will not
176 * generate per-title profiling sections, to avoid overloading the
177 * profiling data collector.
178 *
179 * @see Profiler::logData()
180 * @return bool
181 */
182 abstract public function isPersistent();
183
184 /**
185 * @param string $id
186 */
187 public function setProfileID( $id ) {
188 $this->mProfileID = $id;
189 }
190
191 /**
192 * @return string
193 */
194 public function getProfileID() {
195 if ( $this->mProfileID === false ) {
196 return wfWikiID();
197 } else {
198 return $this->mProfileID;
199 }
200 }
201
202 /**
203 * Called by wfProfieIn()
204 *
205 * @param string $functionname
206 */
207 abstract public function profileIn( $functionname );
208
209 /**
210 * Called by wfProfieOut()
211 *
212 * @param string $functionname
213 */
214 abstract public function profileOut( $functionname );
215
216 /**
217 * Mark a DB as in a transaction with one or more writes pending
218 *
219 * Note that there can be multiple connections to a single DB.
220 *
221 * @param string $server DB server
222 * @param string $db DB name
223 * @param string $id Resource ID string of connection
224 */
225 public function transactionWritingIn( $server, $db, $id = '' ) {
226 $this->trxProfiler->transactionWritingIn( $server, $db, $id );
227 }
228
229 /**
230 * Mark a DB as no longer in a transaction
231 *
232 * This will check if locks are possibly held for longer than
233 * needed and log any affected transactions to a special DB log.
234 * Note that there can be multiple connections to a single DB.
235 *
236 * @param string $server DB server
237 * @param string $db DB name
238 * @param string $id Resource ID string of connection
239 */
240 public function transactionWritingOut( $server, $db, $id = '' ) {
241 $this->trxProfiler->transactionWritingOut( $server, $db, $id );
242 }
243
244 /**
245 * Close opened profiling sections
246 */
247 abstract public function close();
248
249 /**
250 * Log the data to some store or even the page output
251 */
252 abstract public function logData();
253
254 /**
255 * Mark this call as templated or not
256 *
257 * @param bool $t
258 */
259 public function setTemplated( $t ) {
260 $this->mTemplated = $t;
261 }
262
263 /**
264 * Returns a profiling output to be stored in debug file
265 *
266 * @return string
267 */
268 abstract public function getOutput();
269
270 /**
271 * @return array
272 */
273 abstract public function getRawData();
274
275 /**
276 * Get the initial time of the request, based either on $wgRequestTime or
277 * $wgRUstart. Will return null if not able to find data.
278 *
279 * @param string|bool $metric Metric to use, with the following possibilities:
280 * - user: User CPU time (without system calls)
281 * - cpu: Total CPU time (user and system calls)
282 * - wall (or any other string): elapsed time
283 * - false (default): will fall back to default metric
284 * @return float|null
285 */
286 protected function getTime( $metric = 'wall' ) {
287 if ( $metric === 'cpu' || $metric === 'user' ) {
288 $ru = wfGetRusage();
289 if ( !$ru ) {
290 return 0;
291 }
292 $time = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
293 if ( $metric === 'cpu' ) {
294 # This is the time of system calls, added to the user time
295 # it gives the total CPU time
296 $time += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
297 }
298 return $time;
299 } else {
300 return microtime( true );
301 }
302 }
303
304 /**
305 * Get the initial time of the request, based either on $wgRequestTime or
306 * $wgRUstart. Will return null if not able to find data.
307 *
308 * @param string|bool $metric Metric to use, with the following possibilities:
309 * - user: User CPU time (without system calls)
310 * - cpu: Total CPU time (user and system calls)
311 * - wall (or any other string): elapsed time
312 * - false (default): will fall back to default metric
313 * @return float|null
314 */
315 protected function getInitialTime( $metric = 'wall' ) {
316 global $wgRequestTime, $wgRUstart;
317
318 if ( $metric === 'cpu' || $metric === 'user' ) {
319 if ( !count( $wgRUstart ) ) {
320 return null;
321 }
322
323 $time = $wgRUstart['ru_utime.tv_sec'] + $wgRUstart['ru_utime.tv_usec'] / 1e6;
324 if ( $metric === 'cpu' ) {
325 # This is the time of system calls, added to the user time
326 # it gives the total CPU time
327 $time += $wgRUstart['ru_stime.tv_sec'] + $wgRUstart['ru_stime.tv_usec'] / 1e6;
328 }
329 return $time;
330 } else {
331 if ( empty( $wgRequestTime ) ) {
332 return null;
333 } else {
334 return $wgRequestTime;
335 }
336 }
337 }
338
339 /**
340 * Add an entry in the debug log file
341 *
342 * @param string $s String to output
343 */
344 protected function debug( $s ) {
345 if ( function_exists( 'wfDebug' ) ) {
346 wfDebug( $s );
347 }
348 }
349
350 /**
351 * Add an entry in the debug log group
352 *
353 * @param string $group Group to send the message to
354 * @param string $s String to output
355 */
356 protected function debugGroup( $group, $s ) {
357 if ( function_exists( 'wfDebugLog' ) ) {
358 wfDebugLog( $group, $s );
359 }
360 }
361 }
362
363 /**
364 * Helper class that detects high-contention DB queries via profiling calls
365 *
366 * This class is meant to work with a Profiler, as the later already knows
367 * when methods start and finish (which may take place during transactions).
368 *
369 * @since 1.24
370 */
371 class TransactionProfiler {
372 /** @var float Seconds */
373 protected $mDBLockThreshold = 3.0;
374 /** @var array DB/server name => (active trx count, time, DBs involved) */
375 protected $mDBTrxHoldingLocks = array();
376 /** @var array DB/server name => list of (function name, elapsed time) */
377 protected $mDBTrxMethodTimes = array();
378
379 /**
380 * Mark a DB as in a transaction with one or more writes pending
381 *
382 * Note that there can be multiple connections to a single DB.
383 *
384 * @param string $server DB server
385 * @param string $db DB name
386 * @param string $id ID string of transaction
387 */
388 public function transactionWritingIn( $server, $db, $id ) {
389 $name = "{$server} ({$db}) (TRX#$id)";
390 if ( isset( $this->mDBTrxHoldingLocks[$name] ) ) {
391 wfDebugLog( 'DBPerformance', "Nested transaction for '$name' - out of sync." );
392 }
393 $this->mDBTrxHoldingLocks[$name] =
394 array( 'start' => microtime( true ), 'conns' => array() );
395 $this->mDBTrxMethodTimes[$name] = array();
396
397 foreach ( $this->mDBTrxHoldingLocks as $name => &$info ) {
398 $info['conns'][$name] = 1; // track all DBs in transactions for this transaction
399 }
400 }
401
402 /**
403 * Register the name and time of a method for slow DB trx detection
404 *
405 * This method is only to be called by the Profiler class as methods finish
406 *
407 * @param string $method Function name
408 * @param float $realtime Wal time ellapsed
409 */
410 public function recordFunctionCompletion( $method, $realtime ) {
411 if ( !$this->mDBTrxHoldingLocks ) {
412 return; // short-circuit
413 // @todo hardcoded check is a tad janky (what about FOR UPDATE?)
414 } elseif ( !preg_match( '/^query-m: (?!SELECT)/', $method )
415 && $realtime < $this->mDBLockThreshold
416 ) {
417 return; // not a DB master query nor slow enough
418 }
419 $now = microtime( true );
420 foreach ( $this->mDBTrxHoldingLocks as $name => $info ) {
421 // Hacky check to exclude entries from before the first TRX write
422 if ( ( $now - $realtime ) >= $info['start'] ) {
423 $this->mDBTrxMethodTimes[$name][] = array( $method, $realtime );
424 }
425 }
426 }
427
428 /**
429 * Mark a DB as no longer in a transaction
430 *
431 * This will check if locks are possibly held for longer than
432 * needed and log any affected transactions to a special DB log.
433 * Note that there can be multiple connections to a single DB.
434 *
435 * @param string $server DB server
436 * @param string $db DB name
437 * @param string $id ID string of transaction
438 */
439 public function transactionWritingOut( $server, $db, $id ) {
440 $name = "{$server} ({$db}) (TRX#$id)";
441 if ( !isset( $this->mDBTrxMethodTimes[$name] ) ) {
442 wfDebugLog( 'DBPerformance', "Detected no transaction for '$name' - out of sync." );
443 return;
444 }
445 $slow = false;
446 foreach ( $this->mDBTrxMethodTimes[$name] as $info ) {
447 $realtime = $info[1];
448 if ( $realtime >= $this->mDBLockThreshold ) {
449 $slow = true;
450 break;
451 }
452 }
453 if ( $slow ) {
454 $dbs = implode( ', ', array_keys( $this->mDBTrxHoldingLocks[$name]['conns'] ) );
455 $msg = "Sub-optimal transaction on DB(s) [{$dbs}]:\n";
456 foreach ( $this->mDBTrxMethodTimes[$name] as $i => $info ) {
457 list( $method, $realtime ) = $info;
458 $msg .= sprintf( "%d\t%.6f\t%s\n", $i, $realtime, $method );
459 }
460 wfDebugLog( 'DBPerformance', $msg );
461 }
462 unset( $this->mDBTrxHoldingLocks[$name] );
463 unset( $this->mDBTrxMethodTimes[$name] );
464 }
465 }