Remove Profiler::setInstance()
[lhc/web/wiklou.git] / includes / profiler / Profiler.php
1 <?php
2 /**
3 * Base class 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 * Profiler base class that defines the interface and some trivial
27 * functionality
28 *
29 * @ingroup Profiler
30 */
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;
36
37 /** @var TransactionProfiler */
38 protected $trxProfiler;
39
40 // @codingStandardsIgnoreStart PSR2.Classes.PropertyDeclaration.Underscore
41 /** @var Profiler Do not call this outside Profiler and ProfileSection */
42 public static $__instance = null;
43 // @codingStandardsIgnoreEnd
44
45 /**
46 * @param array $params
47 */
48 public function __construct( array $params ) {
49 if ( isset( $params['profileID'] ) ) {
50 $this->mProfileID = $params['profileID'];
51 }
52 $this->trxProfiler = new TransactionProfiler();
53 }
54
55 /**
56 * Singleton
57 * @return Profiler
58 */
59 final public static function instance() {
60 if ( self::$__instance === null ) {
61 global $wgProfiler;
62 if ( is_array( $wgProfiler ) ) {
63 if ( !isset( $wgProfiler['class'] ) ) {
64 $class = 'ProfilerStub';
65 } else {
66 $class = $wgProfiler['class'];
67 }
68 self::$__instance = new $class( $wgProfiler );
69 } else {
70 self::$__instance = new ProfilerStub( array() );
71 }
72 }
73 return self::$__instance;
74 }
75
76 /**
77 * Return whether this a stub profiler
78 *
79 * @return bool
80 */
81 abstract public function isStub();
82
83 /**
84 * Return whether this profiler stores data
85 *
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.
89 *
90 * @see Profiler::logData()
91 * @return bool
92 */
93 abstract public function isPersistent();
94
95 /**
96 * @param string $id
97 */
98 public function setProfileID( $id ) {
99 $this->mProfileID = $id;
100 }
101
102 /**
103 * @return string
104 */
105 public function getProfileID() {
106 if ( $this->mProfileID === false ) {
107 return wfWikiID();
108 } else {
109 return $this->mProfileID;
110 }
111 }
112
113 /**
114 * Called by wfProfieIn()
115 *
116 * @param string $functionname
117 */
118 abstract public function profileIn( $functionname );
119
120 /**
121 * Called by wfProfieOut()
122 *
123 * @param string $functionname
124 */
125 abstract public function profileOut( $functionname );
126
127 /**
128 * Mark a DB as in a transaction with one or more writes pending
129 *
130 * Note that there can be multiple connections to a single DB.
131 *
132 * @param string $server DB server
133 * @param string $db DB name
134 * @param string $id Resource ID string of connection
135 */
136 public function transactionWritingIn( $server, $db, $id = '' ) {
137 $this->trxProfiler->transactionWritingIn( $server, $db, $id );
138 }
139
140 /**
141 * Mark a DB as no longer in a transaction
142 *
143 * This will check if locks are possibly held for longer than
144 * needed and log any affected transactions to a special DB log.
145 * Note that there can be multiple connections to a single DB.
146 *
147 * @param string $server DB server
148 * @param string $db DB name
149 * @param string $id Resource ID string of connection
150 */
151 public function transactionWritingOut( $server, $db, $id = '' ) {
152 $this->trxProfiler->transactionWritingOut( $server, $db, $id );
153 }
154
155 /**
156 * Close opened profiling sections
157 */
158 abstract public function close();
159
160 /**
161 * Log the data to some store or even the page output
162 */
163 abstract public function logData();
164
165 /**
166 * Mark this call as templated or not
167 *
168 * @param bool $t
169 */
170 public function setTemplated( $t ) {
171 $this->mTemplated = $t;
172 }
173
174 /**
175 * Returns a profiling output to be stored in debug file
176 *
177 * @return string
178 */
179 abstract public function getOutput();
180
181 /**
182 * @return array
183 */
184 abstract public function getRawData();
185
186 /**
187 * Get the initial time of the request, based either on $wgRequestTime or
188 * $wgRUstart. Will return null if not able to find data.
189 *
190 * @param string|bool $metric Metric to use, with the following possibilities:
191 * - user: User CPU time (without system calls)
192 * - cpu: Total CPU time (user and system calls)
193 * - wall (or any other string): elapsed time
194 * - false (default): will fall back to default metric
195 * @return float|null
196 */
197 protected function getTime( $metric = 'wall' ) {
198 if ( $metric === 'cpu' || $metric === 'user' ) {
199 $ru = wfGetRusage();
200 if ( !$ru ) {
201 return 0;
202 }
203 $time = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
204 if ( $metric === 'cpu' ) {
205 # This is the time of system calls, added to the user time
206 # it gives the total CPU time
207 $time += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
208 }
209 return $time;
210 } else {
211 return microtime( true );
212 }
213 }
214
215 /**
216 * Get the initial time of the request, based either on $wgRequestTime or
217 * $wgRUstart. Will return null if not able to find data.
218 *
219 * @param string|bool $metric Metric to use, with the following possibilities:
220 * - user: User CPU time (without system calls)
221 * - cpu: Total CPU time (user and system calls)
222 * - wall (or any other string): elapsed time
223 * - false (default): will fall back to default metric
224 * @return float|null
225 */
226 protected function getInitialTime( $metric = 'wall' ) {
227 global $wgRequestTime, $wgRUstart;
228
229 if ( $metric === 'cpu' || $metric === 'user' ) {
230 if ( !count( $wgRUstart ) ) {
231 return null;
232 }
233
234 $time = $wgRUstart['ru_utime.tv_sec'] + $wgRUstart['ru_utime.tv_usec'] / 1e6;
235 if ( $metric === 'cpu' ) {
236 # This is the time of system calls, added to the user time
237 # it gives the total CPU time
238 $time += $wgRUstart['ru_stime.tv_sec'] + $wgRUstart['ru_stime.tv_usec'] / 1e6;
239 }
240 return $time;
241 } else {
242 if ( empty( $wgRequestTime ) ) {
243 return null;
244 } else {
245 return $wgRequestTime;
246 }
247 }
248 }
249
250 /**
251 * Add an entry in the debug log file
252 *
253 * @param string $s String to output
254 */
255 protected function debug( $s ) {
256 if ( function_exists( 'wfDebug' ) ) {
257 wfDebug( $s );
258 }
259 }
260
261 /**
262 * Add an entry in the debug log group
263 *
264 * @param string $group Group to send the message to
265 * @param string $s String to output
266 */
267 protected function debugGroup( $group, $s ) {
268 if ( function_exists( 'wfDebugLog' ) ) {
269 wfDebugLog( $group, $s );
270 }
271 }
272 }