3 * Base class 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 * Profiler base class that defines the interface and some trivial
31 abstract class Profiler
{
32 /** @var string|bool Profiler ID for bucketing data */
33 protected $mProfileID = false;
34 /** @var bool Whether MediaWiki is in a SkinTemplate output context */
35 protected $mTemplated = false;
37 /** @var TransactionProfiler */
38 protected $trxProfiler;
40 // @codingStandardsIgnoreStart PSR2.Classes.PropertyDeclaration.Underscore
41 /** @var Profiler Do not call this outside Profiler and ProfileSection */
42 public static $__instance = null;
43 // @codingStandardsIgnoreEnd
46 * @param array $params
48 public function __construct( array $params ) {
49 if ( isset( $params['profileID'] ) ) {
50 $this->mProfileID
= $params['profileID'];
52 $this->trxProfiler
= new TransactionProfiler();
59 final public static function instance() {
60 if ( self
::$__instance === null ) {
62 if ( is_array( $wgProfiler ) ) {
63 if ( !isset( $wgProfiler['class'] ) ) {
64 $class = 'ProfilerStub';
66 $class = $wgProfiler['class'];
68 self
::$__instance = new $class( $wgProfiler );
70 self
::$__instance = new ProfilerStub( array() );
73 return self
::$__instance;
77 * Set the profiler to a specific profiler instance. Mostly for dumpHTML
80 final public static function setInstance( Profiler
$p ) {
81 self
::$__instance = $p;
85 * Return whether this a stub profiler
89 abstract public function isStub();
92 * Return whether this profiler stores data
94 * Called by Parser::braceSubstitution. If true, the parser will not
95 * generate per-title profiling sections, to avoid overloading the
96 * profiling data collector.
98 * @see Profiler::logData()
101 abstract public function isPersistent();
106 public function setProfileID( $id ) {
107 $this->mProfileID
= $id;
113 public function getProfileID() {
114 if ( $this->mProfileID
=== false ) {
117 return $this->mProfileID
;
122 * Called by wfProfieIn()
124 * @param string $functionname
126 abstract public function profileIn( $functionname );
129 * Called by wfProfieOut()
131 * @param string $functionname
133 abstract public function profileOut( $functionname );
136 * Mark a DB as in a transaction with one or more writes pending
138 * Note that there can be multiple connections to a single DB.
140 * @param string $server DB server
141 * @param string $db DB name
142 * @param string $id Resource ID string of connection
144 public function transactionWritingIn( $server, $db, $id = '' ) {
145 $this->trxProfiler
->transactionWritingIn( $server, $db, $id );
149 * Mark a DB as no longer in a transaction
151 * This will check if locks are possibly held for longer than
152 * needed and log any affected transactions to a special DB log.
153 * Note that there can be multiple connections to a single DB.
155 * @param string $server DB server
156 * @param string $db DB name
157 * @param string $id Resource ID string of connection
159 public function transactionWritingOut( $server, $db, $id = '' ) {
160 $this->trxProfiler
->transactionWritingOut( $server, $db, $id );
164 * Close opened profiling sections
166 abstract public function close();
169 * Log the data to some store or even the page output
171 abstract public function logData();
174 * Mark this call as templated or not
178 public function setTemplated( $t ) {
179 $this->mTemplated
= $t;
183 * Returns a profiling output to be stored in debug file
187 abstract public function getOutput();
192 abstract public function getRawData();
195 * Get the initial time of the request, based either on $wgRequestTime or
196 * $wgRUstart. Will return null if not able to find data.
198 * @param string|bool $metric Metric to use, with the following possibilities:
199 * - user: User CPU time (without system calls)
200 * - cpu: Total CPU time (user and system calls)
201 * - wall (or any other string): elapsed time
202 * - false (default): will fall back to default metric
205 protected function getTime( $metric = 'wall' ) {
206 if ( $metric === 'cpu' ||
$metric === 'user' ) {
211 $time = $ru['ru_utime.tv_sec'] +
$ru['ru_utime.tv_usec'] / 1e6
;
212 if ( $metric === 'cpu' ) {
213 # This is the time of system calls, added to the user time
214 # it gives the total CPU time
215 $time +
= $ru['ru_stime.tv_sec'] +
$ru['ru_stime.tv_usec'] / 1e6
;
219 return microtime( true );
224 * Get the initial time of the request, based either on $wgRequestTime or
225 * $wgRUstart. Will return null if not able to find data.
227 * @param string|bool $metric Metric to use, with the following possibilities:
228 * - user: User CPU time (without system calls)
229 * - cpu: Total CPU time (user and system calls)
230 * - wall (or any other string): elapsed time
231 * - false (default): will fall back to default metric
234 protected function getInitialTime( $metric = 'wall' ) {
235 global $wgRequestTime, $wgRUstart;
237 if ( $metric === 'cpu' ||
$metric === 'user' ) {
238 if ( !count( $wgRUstart ) ) {
242 $time = $wgRUstart['ru_utime.tv_sec'] +
$wgRUstart['ru_utime.tv_usec'] / 1e6
;
243 if ( $metric === 'cpu' ) {
244 # This is the time of system calls, added to the user time
245 # it gives the total CPU time
246 $time +
= $wgRUstart['ru_stime.tv_sec'] +
$wgRUstart['ru_stime.tv_usec'] / 1e6
;
250 if ( empty( $wgRequestTime ) ) {
253 return $wgRequestTime;
259 * Add an entry in the debug log file
261 * @param string $s String to output
263 protected function debug( $s ) {
264 if ( function_exists( 'wfDebug' ) ) {
270 * Add an entry in the debug log group
272 * @param string $group Group to send the message to
273 * @param string $s String to output
275 protected function debugGroup( $group, $s ) {
276 if ( function_exists( 'wfDebugLog' ) ) {
277 wfDebugLog( $group, $s );