Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[lhc/web/wiklou.git] / includes / profiler / output / ProfilerOutputDb.php
1 <?php
2 /**
3 * Profiler storing information in the DB.
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 */
23
24 use Wikimedia\Rdbms\Database;
25 use Wikimedia\Rdbms\DBError;
26
27 /**
28 * Logs profiling data into the local DB
29 *
30 * @ingroup Profiler
31 * @since 1.25
32 */
33 class ProfilerOutputDb extends ProfilerOutput {
34 /** @var bool Whether to store host data with profiling calls */
35 private $perHost = false;
36
37 public function __construct( Profiler $collector, array $params ) {
38 parent::__construct( $collector, $params );
39
40 // Initialize per-host profiling from config, back-compat if available
41 if ( isset( $this->params['perHost'] ) ) {
42 $this->perHost = $this->params['perHost'];
43 }
44 }
45
46 public function canUse() {
47 # Do not log anything if database is readonly (T7375)
48 return !wfReadOnly();
49 }
50
51 public function log( array $stats ) {
52 try {
53 $dbw = wfGetDB( DB_MASTER );
54 } catch ( DBError $e ) {
55 return; // ignore
56 }
57
58 $fname = __METHOD__;
59 $dbw->onTransactionCommitOrIdle( function ( Database $dbw ) use ( $stats, $fname ) {
60 $pfhost = $this->perHost ? wfHostname() : '';
61 // Sqlite: avoid excess b-tree rebuilds (mostly for non-WAL mode)
62 // non-Sqlite: lower contention with small transactions
63 $useTrx = ( $dbw->getType() === 'sqlite' );
64
65 try {
66 $useTrx ? $dbw->startAtomic( $fname ) : null;
67
68 foreach ( $stats as $data ) {
69 $name = $data['name'];
70 $eventCount = $data['calls'];
71 $timeSum = (float)$data['real'];
72 $memorySum = (float)$data['memory'];
73 $name = substr( $name, 0, 255 );
74
75 // Kludge
76 $timeSum = $timeSum >= 0 ? $timeSum : 0;
77 $memorySum = $memorySum >= 0 ? $memorySum : 0;
78
79 $dbw->upsert( 'profiling',
80 [
81 'pf_name' => $name,
82 'pf_count' => $eventCount,
83 'pf_time' => $timeSum,
84 'pf_memory' => $memorySum,
85 'pf_server' => $pfhost
86 ],
87 [ [ 'pf_name', 'pf_server' ] ],
88 [
89 "pf_count=pf_count+{$eventCount}",
90 "pf_time=pf_time+{$timeSum}",
91 "pf_memory=pf_memory+{$memorySum}",
92 ],
93 $fname
94 );
95 }
96 } catch ( DBError $e ) {
97 // ignore
98 }
99
100 try {
101 $useTrx ? $dbw->endAtomic( $fname ) : null;
102 } catch ( DBError $e ) {
103 // ignore
104 }
105 } );
106 }
107 }