380d80fddc1f34e5c2cc10f4467794f82906ed0c
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 $profileID = false;
34 /** @var bool Whether MediaWiki is in a SkinTemplate output context */
35 protected $templated = 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->profileID
= $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 * Return whether this a stub profiler
81 abstract public function isStub();
84 * Return whether this profiler stores data
86 * Called by Parser::braceSubstitution. If true, the parser will not
87 * generate per-title profiling sections, to avoid overloading the
88 * profiling data collector.
90 * @see Profiler::logData()
93 abstract public function isPersistent();
98 public function setProfileID( $id ) {
99 $this->profileID
= $id;
105 public function getProfileID() {
106 if ( $this->profileID
=== false ) {
109 return $this->profileID
;
114 * Called by wfProfieIn()
116 * @param string $functionname
118 abstract public function profileIn( $functionname );
121 * Called by wfProfieOut()
123 * @param string $functionname
125 abstract public function profileOut( $functionname );
128 * @return TransactionProfiler
131 public function getTransactionProfiler() {
132 return $this->trxProfiler
;
136 * Close opened profiling sections
138 abstract public function close();
141 * Log the data to some store or even the page output
143 abstract public function logData();
146 * Mark this call as templated or not
150 public function setTemplated( $t ) {
151 $this->templated
= $t;
155 * Returns a profiling output to be stored in debug file
159 abstract public function getOutput();
162 * Get data for the debugging toolbar.
166 abstract public function getRawData();
169 * Get the initial time of the request, based either on $wgRequestTime or
170 * $wgRUstart. Will return null if not able to find data.
172 * @param string|bool $metric Metric to use, with the following possibilities:
173 * - user: User CPU time (without system calls)
174 * - cpu: Total CPU time (user and system calls)
175 * - wall (or any other string): elapsed time
176 * - false (default): will fall back to default metric
179 protected function getTime( $metric = 'wall' ) {
180 if ( $metric === 'cpu' ||
$metric === 'user' ) {
185 $time = $ru['ru_utime.tv_sec'] +
$ru['ru_utime.tv_usec'] / 1e6
;
186 if ( $metric === 'cpu' ) {
187 # This is the time of system calls, added to the user time
188 # it gives the total CPU time
189 $time +
= $ru['ru_stime.tv_sec'] +
$ru['ru_stime.tv_usec'] / 1e6
;
193 return microtime( true );
198 * Get the initial time of the request, based either on $wgRequestTime or
199 * $wgRUstart. Will return null if not able to find data.
201 * @param string|bool $metric Metric to use, with the following possibilities:
202 * - user: User CPU time (without system calls)
203 * - cpu: Total CPU time (user and system calls)
204 * - wall (or any other string): elapsed time
205 * - false (default): will fall back to default metric
208 protected function getInitialTime( $metric = 'wall' ) {
209 global $wgRequestTime, $wgRUstart;
211 if ( $metric === 'cpu' ||
$metric === 'user' ) {
212 if ( !count( $wgRUstart ) ) {
216 $time = $wgRUstart['ru_utime.tv_sec'] +
$wgRUstart['ru_utime.tv_usec'] / 1e6
;
217 if ( $metric === 'cpu' ) {
218 # This is the time of system calls, added to the user time
219 # it gives the total CPU time
220 $time +
= $wgRUstart['ru_stime.tv_sec'] +
$wgRUstart['ru_stime.tv_usec'] / 1e6
;
224 if ( empty( $wgRequestTime ) ) {
227 return $wgRequestTime;