Merge "Made LocalFile avoid duplicate (name,timestamp) pairs"
[mediawiki.git] / includes / profiler / Profiler.php
blobf6bafd5d7d89f53ad986ecca4543ddb2d0522524
1 <?php
2 /**
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
20 * @file
21 * @ingroup Profiler
22 * @defgroup Profiler Profiler
23 * This file is only included if profiling is enabled
26 /**
27 * Begin profiling of a function
28 * @param string $functionname name of the function we will profile
30 function wfProfileIn( $functionname ) {
31 if ( Profiler::$__instance === null ) { // use this directly to reduce overhead
32 Profiler::instance();
34 if ( !( Profiler::$__instance instanceof ProfilerStub ) ) {
35 Profiler::$__instance->profileIn( $functionname );
39 /**
40 * Stop profiling of a function
41 * @param string $functionname name of the function we have profiled
43 function wfProfileOut( $functionname = 'missing' ) {
44 if ( Profiler::$__instance === null ) { // use this directly to reduce overhead
45 Profiler::instance();
47 if ( !( Profiler::$__instance instanceof ProfilerStub ) ) {
48 Profiler::$__instance->profileOut( $functionname );
52 /**
53 * Class for handling function-scope profiling
55 * @since 1.22
57 class ProfileSection {
58 protected $name; // string; method name
59 protected $enabled = false; // boolean; whether profiling is enabled
61 /**
62 * Begin profiling of a function and return an object that ends profiling of
63 * the function when that object leaves scope. As long as the object is not
64 * specifically linked to other objects, it will fall out of scope at the same
65 * moment that the function to be profiled terminates.
67 * This is typically called like:
68 * <code>$section = new ProfileSection( __METHOD__ );</code>
70 * @param string $name Name of the function to profile
72 public function __construct( $name ) {
73 $this->name = $name;
74 if ( Profiler::$__instance === null ) { // use this directly to reduce overhead
75 Profiler::instance();
77 if ( !( Profiler::$__instance instanceof ProfilerStub ) ) {
78 $this->enabled = true;
79 Profiler::$__instance->profileIn( $this->name );
83 function __destruct() {
84 if ( $this->enabled ) {
85 Profiler::$__instance->profileOut( $this->name );
90 /**
91 * Profiler base class that defines the interface and some trivial functionality
93 * @ingroup Profiler
95 abstract class Profiler {
96 /** @var string|bool Profiler ID for bucketing data */
97 protected $mProfileID = false;
98 /** @var bool Whether MediaWiki is in a SkinTemplate output context */
99 protected $mTemplated = false;
101 /** @var TransactionProfiler */
102 protected $trxProfiler;
104 // @codingStandardsIgnoreStart PSR2.Classes.PropertyDeclaration.Underscore
105 /** @var Profiler Do not call this outside Profiler and ProfileSection */
106 public static $__instance = null;
107 // @codingStandardsIgnoreEnd
110 * @param array $params
112 public function __construct( array $params ) {
113 if ( isset( $params['profileID'] ) ) {
114 $this->mProfileID = $params['profileID'];
116 $this->trxProfiler = new TransactionProfiler();
120 * Singleton
121 * @return Profiler
123 final public static function instance() {
124 if ( self::$__instance === null ) {
125 global $wgProfiler;
126 if ( is_array( $wgProfiler ) ) {
127 if ( !isset( $wgProfiler['class'] ) ) {
128 $class = 'ProfilerStub';
129 } elseif ( $wgProfiler['class'] === 'Profiler' ) {
130 $class = 'ProfilerStub'; // b/c; don't explode
131 } else {
132 $class = $wgProfiler['class'];
134 self::$__instance = new $class( $wgProfiler );
135 } elseif ( $wgProfiler instanceof Profiler ) {
136 self::$__instance = $wgProfiler; // back-compat
137 } else {
138 self::$__instance = new ProfilerStub( array() );
141 return self::$__instance;
145 * Set the profiler to a specific profiler instance. Mostly for dumpHTML
146 * @param Profiler $p
148 final public static function setInstance( Profiler $p ) {
149 self::$__instance = $p;
153 * Return whether this a stub profiler
155 * @return bool
157 abstract public function isStub();
160 * Return whether this profiler stores data
162 * Called by Parser::braceSubstitution. If true, the parser will not
163 * generate per-title profiling sections, to avoid overloading the
164 * profiling data collector.
166 * @see Profiler::logData()
167 * @return bool
169 abstract public function isPersistent();
172 * @param string $id
174 public function setProfileID( $id ) {
175 $this->mProfileID = $id;
179 * @return string
181 public function getProfileID() {
182 if ( $this->mProfileID === false ) {
183 return wfWikiID();
184 } else {
185 return $this->mProfileID;
190 * Called by wfProfieIn()
192 * @param string $functionname
194 abstract public function profileIn( $functionname );
197 * Called by wfProfieOut()
199 * @param string $functionname
201 abstract public function profileOut( $functionname );
204 * Mark a DB as in a transaction with one or more writes pending
206 * Note that there can be multiple connections to a single DB.
208 * @param string $server DB server
209 * @param string $db DB name
210 * @param string $id Resource ID string of connection
212 public function transactionWritingIn( $server, $db, $id = '' ) {
213 $this->trxProfiler->transactionWritingIn( $server, $db, $id );
217 * Mark a DB as no longer in a transaction
219 * This will check if locks are possibly held for longer than
220 * needed and log any affected transactions to a special DB log.
221 * Note that there can be multiple connections to a single DB.
223 * @param string $server DB server
224 * @param string $db DB name
225 * @param string $id Resource ID string of connection
227 public function transactionWritingOut( $server, $db, $id = '' ) {
228 $this->trxProfiler->transactionWritingOut( $server, $db, $id );
232 * Close opened profiling sections
234 abstract public function close();
237 * Log the data to some store or even the page output
239 abstract public function logData();
242 * Mark this call as templated or not
244 * @param bool $t
246 public function setTemplated( $t ) {
247 $this->mTemplated = $t;
251 * Returns a profiling output to be stored in debug file
253 * @return string
255 abstract public function getOutput();
258 * @return array
260 abstract public function getRawData();
263 * Get the initial time of the request, based either on $wgRequestTime or
264 * $wgRUstart. Will return null if not able to find data.
266 * @param string|bool $metric Metric to use, with the following possibilities:
267 * - user: User CPU time (without system calls)
268 * - cpu: Total CPU time (user and system calls)
269 * - wall (or any other string): elapsed time
270 * - false (default): will fall back to default metric
271 * @return float|null
273 protected function getTime( $metric = 'wall' ) {
274 if ( $metric === 'cpu' || $metric === 'user' ) {
275 if ( !function_exists( 'getrusage' ) ) {
276 return 0;
278 $ru = getrusage();
279 $time = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
280 if ( $metric === 'cpu' ) {
281 # This is the time of system calls, added to the user time
282 # it gives the total CPU time
283 $time += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
285 return $time;
286 } else {
287 return microtime( true );
292 * Get the initial time of the request, based either on $wgRequestTime or
293 * $wgRUstart. Will return null if not able to find data.
295 * @param string|bool $metric Metric to use, with the following possibilities:
296 * - user: User CPU time (without system calls)
297 * - cpu: Total CPU time (user and system calls)
298 * - wall (or any other string): elapsed time
299 * - false (default): will fall back to default metric
300 * @return float|null
302 protected function getInitialTime( $metric = 'wall' ) {
303 global $wgRequestTime, $wgRUstart;
305 if ( $metric === 'cpu' || $metric === 'user' ) {
306 if ( !count( $wgRUstart ) ) {
307 return null;
310 $time = $wgRUstart['ru_utime.tv_sec'] + $wgRUstart['ru_utime.tv_usec'] / 1e6;
311 if ( $metric === 'cpu' ) {
312 # This is the time of system calls, added to the user time
313 # it gives the total CPU time
314 $time += $wgRUstart['ru_stime.tv_sec'] + $wgRUstart['ru_stime.tv_usec'] / 1e6;
316 return $time;
317 } else {
318 if ( empty( $wgRequestTime ) ) {
319 return null;
320 } else {
321 return $wgRequestTime;
327 * Add an entry in the debug log file
329 * @param string $s to output
331 protected function debug( $s ) {
332 if ( function_exists( 'wfDebug' ) ) {
333 wfDebug( $s );
338 * Add an entry in the debug log group
340 * @param string $group Group to send the message to
341 * @param string $s to output
343 protected function debugGroup( $group, $s ) {
344 if ( function_exists( 'wfDebugLog' ) ) {
345 wfDebugLog( $group, $s );
351 * Helper class that detects high-contention DB queries via profiling calls
353 * This class is meant to work with a Profiler, as the later already knows
354 * when methods start and finish (which may take place during transactions).
356 * @since 1.24
358 class TransactionProfiler {
359 /** @var float seconds */
360 protected $mDBLockThreshold = 5.0;
361 /** @var array DB/server name => (active trx count,timestamp) */
362 protected $mDBTrxHoldingLocks = array();
363 /** @var array DB/server name => list of (function name, elapsed time) */
364 protected $mDBTrxMethodTimes = array();
367 * Mark a DB as in a transaction with one or more writes pending
369 * Note that there can be multiple connections to a single DB.
371 * @param string $server DB server
372 * @param string $db DB name
373 * @param string $id Resource ID string of connection
375 public function transactionWritingIn( $server, $db, $id ) {
376 $name = "{$server} ({$db}) ($id)";
377 if ( isset( $this->mDBTrxHoldingLocks[$name] ) ) {
378 ++$this->mDBTrxHoldingLocks[$name]['refs'];
379 } else {
380 $this->mDBTrxHoldingLocks[$name] = array( 'refs' => 1, 'start' => microtime( true ) );
381 $this->mDBTrxMethodTimes[$name] = array();
386 * Register the name and time of a method for slow DB trx detection
388 * This method is only to be called by the Profiler class as methods finish
390 * @param string $method Function name
391 * @param float $realtime Wal time ellapsed
393 public function recordFunctionCompletion( $method, $realtime ) {
394 if ( !$this->mDBTrxHoldingLocks ) {
395 return; // short-circuit
396 // @TODO: hardcoded check is a tad janky (what about FOR UPDATE?)
397 } elseif ( !preg_match( '/^query-m: (?!SELECT)/', $method )
398 && $realtime < $this->mDBLockThreshold
400 return; // not a DB master query nor slow enough
402 $now = microtime( true );
403 foreach ( $this->mDBTrxHoldingLocks as $name => $info ) {
404 // Hacky check to exclude entries from before the first TRX write
405 if ( ( $now - $realtime ) >= $info['start'] ) {
406 $this->mDBTrxMethodTimes[$name][] = array( $method, $realtime );
412 * Mark a DB as no longer in a transaction
414 * This will check if locks are possibly held for longer than
415 * needed and log any affected transactions to a special DB log.
416 * Note that there can be multiple connections to a single DB.
418 * @param string $server DB server
419 * @param string $db DB name
420 * @param string $id Resource ID string of connection
422 public function transactionWritingOut( $server, $db, $id ) {
423 $name = "{$server} ({$db}) ($id)";
424 if ( --$this->mDBTrxHoldingLocks[$name]['refs'] <= 0 ) {
425 $slow = false;
426 foreach ( $this->mDBTrxMethodTimes[$name] as $info ) {
427 list( $method, $realtime ) = $info;
428 if ( $realtime >= $this->mDBLockThreshold ) {
429 $slow = true;
430 break;
433 if ( $slow ) {
434 $dbs = implode( ', ', array_keys( $this->mDBTrxHoldingLocks ) );
435 $msg = "Sub-optimal transaction on DB(s) {$dbs}:\n";
436 foreach ( $this->mDBTrxMethodTimes[$name] as $i => $info ) {
437 list( $method, $realtime ) = $info;
438 $msg .= sprintf( "%d\t%.6f\t%s\n", $i, $realtime, $method );
440 wfDebugLog( 'DBPerformance', $msg );
442 unset( $this->mDBTrxHoldingLocks[$name] );
443 unset( $this->mDBTrxMethodTimes[$name] );