3 * Base class and functions for profiling.
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.
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.
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
22 * @defgroup Profiler Profiler
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.
31 * @return array|bool Resource usage data or false if no data available.
33 function wfGetRusage() {
34 if ( !function_exists( 'getrusage' ) ) {
36 } elseif ( defined ( 'HHVM_VERSION' ) ) {
37 return getrusage( 2 /* RUSAGE_THREAD */ );
39 return getrusage( 0 /* RUSAGE_SELF */ );
44 * Begin profiling of a function
45 * @param string $functionname Name of the function we will profile
47 function wfProfileIn( $functionname ) {
48 if ( Profiler
::$__instance === null ) { // use this directly to reduce overhead
51 if ( !( Profiler
::$__instance instanceof ProfilerStub
) ) {
52 Profiler
::$__instance->profileIn( $functionname );
57 * Stop profiling of a function
58 * @param string $functionname Name of the function we have profiled
60 function wfProfileOut( $functionname = 'missing' ) {
61 if ( Profiler
::$__instance === null ) { // use this directly to reduce overhead
64 if ( !( Profiler
::$__instance instanceof ProfilerStub
) ) {
65 Profiler
::$__instance->profileOut( $functionname );
70 * Class for handling function-scope profiling
74 class ProfileSection
{
75 protected $name; // string; method name
76 protected $enabled = false; // boolean; whether profiling is enabled
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.
84 * This is typically called like:
85 * <code>$section = new ProfileSection( __METHOD__ );</code>
87 * @param string $name Name of the function to profile
89 public function __construct( $name ) {
91 if ( Profiler
::$__instance === null ) { // use this directly to reduce overhead
94 if ( !( Profiler
::$__instance instanceof ProfilerStub
) ) {
95 $this->enabled
= true;
96 Profiler
::$__instance->profileIn( $this->name
);
100 function __destruct() {
101 if ( $this->enabled
) {
102 Profiler
::$__instance->profileOut( $this->name
);
108 * Profiler base class that defines the interface and some trivial functionality
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;
118 /** @var TransactionProfiler */
119 protected $trxProfiler;
121 // @codingStandardsIgnoreStart PSR2.Classes.PropertyDeclaration.Underscore
122 /** @var Profiler Do not call this outside Profiler and ProfileSection */
123 public static $__instance = null;
124 // @codingStandardsIgnoreEnd
127 * @param array $params
129 public function __construct( array $params ) {
130 if ( isset( $params['profileID'] ) ) {
131 $this->mProfileID
= $params['profileID'];
133 $this->trxProfiler
= new TransactionProfiler();
140 final public static function instance() {
141 if ( self
::$__instance === null ) {
143 if ( is_array( $wgProfiler ) ) {
144 if ( !isset( $wgProfiler['class'] ) ) {
145 $class = 'ProfilerStub';
147 $class = $wgProfiler['class'];
149 self
::$__instance = new $class( $wgProfiler );
151 self
::$__instance = new ProfilerStub( array() );
154 return self
::$__instance;
158 * Set the profiler to a specific profiler instance. Mostly for dumpHTML
161 final public static function setInstance( Profiler
$p ) {
162 self
::$__instance = $p;
166 * Return whether this a stub profiler
170 abstract public function isStub();
173 * Return whether this profiler stores data
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.
179 * @see Profiler::logData()
182 abstract public function isPersistent();
187 public function setProfileID( $id ) {
188 $this->mProfileID
= $id;
194 public function getProfileID() {
195 if ( $this->mProfileID
=== false ) {
198 return $this->mProfileID
;
203 * Called by wfProfieIn()
205 * @param string $functionname
207 abstract public function profileIn( $functionname );
210 * Called by wfProfieOut()
212 * @param string $functionname
214 abstract public function profileOut( $functionname );
217 * Mark a DB as in a transaction with one or more writes pending
219 * Note that there can be multiple connections to a single DB.
221 * @param string $server DB server
222 * @param string $db DB name
223 * @param string $id Resource ID string of connection
225 public function transactionWritingIn( $server, $db, $id = '' ) {
226 $this->trxProfiler
->transactionWritingIn( $server, $db, $id );
230 * Mark a DB as no longer in a transaction
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.
236 * @param string $server DB server
237 * @param string $db DB name
238 * @param string $id Resource ID string of connection
240 public function transactionWritingOut( $server, $db, $id = '' ) {
241 $this->trxProfiler
->transactionWritingOut( $server, $db, $id );
245 * Close opened profiling sections
247 abstract public function close();
250 * Log the data to some store or even the page output
252 abstract public function logData();
255 * Mark this call as templated or not
259 public function setTemplated( $t ) {
260 $this->mTemplated
= $t;
264 * Returns a profiling output to be stored in debug file
268 abstract public function getOutput();
273 abstract public function getRawData();
276 * Get the initial time of the request, based either on $wgRequestTime or
277 * $wgRUstart. Will return null if not able to find data.
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
286 protected function getTime( $metric = 'wall' ) {
287 if ( $metric === 'cpu' ||
$metric === 'user' ) {
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
;
300 return microtime( true );
305 * Get the initial time of the request, based either on $wgRequestTime or
306 * $wgRUstart. Will return null if not able to find data.
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
315 protected function getInitialTime( $metric = 'wall' ) {
316 global $wgRequestTime, $wgRUstart;
318 if ( $metric === 'cpu' ||
$metric === 'user' ) {
319 if ( !count( $wgRUstart ) ) {
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
;
331 if ( empty( $wgRequestTime ) ) {
334 return $wgRequestTime;
340 * Add an entry in the debug log file
342 * @param string $s String to output
344 protected function debug( $s ) {
345 if ( function_exists( 'wfDebug' ) ) {
351 * Add an entry in the debug log group
353 * @param string $group Group to send the message to
354 * @param string $s String to output
356 protected function debugGroup( $group, $s ) {
357 if ( function_exists( 'wfDebugLog' ) ) {
358 wfDebugLog( $group, $s );
364 * Helper class that detects high-contention DB queries via profiling calls
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).
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();
380 * Mark a DB as in a transaction with one or more writes pending
382 * Note that there can be multiple connections to a single DB.
384 * @param string $server DB server
385 * @param string $db DB name
386 * @param string $id ID string of transaction
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." );
393 $this->mDBTrxHoldingLocks
[$name] =
394 array( 'start' => microtime( true ), 'conns' => array() );
395 $this->mDBTrxMethodTimes
[$name] = array();
397 foreach ( $this->mDBTrxHoldingLocks
as $name => &$info ) {
398 $info['conns'][$name] = 1; // track all DBs in transactions for this transaction
403 * Register the name and time of a method for slow DB trx detection
405 * This method is only to be called by the Profiler class as methods finish
407 * @param string $method Function name
408 * @param float $realtime Wal time ellapsed
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
417 return; // not a DB master query nor slow enough
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 );
429 * Mark a DB as no longer in a transaction
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.
435 * @param string $server DB server
436 * @param string $db DB name
437 * @param string $id ID string of transaction
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." );
446 foreach ( $this->mDBTrxMethodTimes
[$name] as $info ) {
447 $realtime = $info[1];
448 if ( $realtime >= $this->mDBLockThreshold
) {
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 );
460 wfDebugLog( 'DBPerformance', $msg );
462 unset( $this->mDBTrxHoldingLocks
[$name] );
463 unset( $this->mDBTrxMethodTimes
[$name] );