3 * Profiler storing information in the DB.
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
24 use Wikimedia\Rdbms\DBError
;
27 * Logs profiling data into the local DB
32 class ProfilerOutputDb
extends ProfilerOutput
{
33 /** @var bool Whether to store host data with profiling calls */
34 private $perHost = false;
36 public function __construct( Profiler
$collector, array $params ) {
37 parent
::__construct( $collector, $params );
39 // Initialize per-host profiling from config, back-compat if available
40 if ( isset( $this->params
['perHost'] ) ) {
41 $this->perHost
= $this->params
['perHost'];
45 public function canUse() {
46 # Do not log anything if database is readonly (T7375)
50 public function log( array $stats ) {
52 $dbw = wfGetDB( DB_MASTER
);
53 } catch ( DBError
$e ) {
58 $dbw->onTransactionCommitOrIdle( function () use ( $stats, $fname, $dbw ) {
59 $pfhost = $this->perHost ?
wfHostname() : '';
60 // Sqlite: avoid excess b-tree rebuilds (mostly for non-WAL mode)
61 // non-Sqlite: lower contention with small transactions
62 $useTrx = ( $dbw->getType() === 'sqlite' );
65 $useTrx ?
$dbw->startAtomic( $fname ) : null;
67 foreach ( $stats as $data ) {
68 $name = $data['name'];
69 $eventCount = $data['calls'];
70 $timeSum = (float)$data['real'];
71 $memorySum = (float)$data['memory'];
72 $name = substr( $name, 0, 255 );
75 $timeSum = $timeSum >= 0 ?
$timeSum : 0;
76 $memorySum = $memorySum >= 0 ?
$memorySum : 0;
78 $dbw->upsert( 'profiling',
81 'pf_count' => $eventCount,
82 'pf_time' => $timeSum,
83 'pf_memory' => $memorySum,
84 'pf_server' => $pfhost
86 [ [ 'pf_name', 'pf_server' ] ],
88 "pf_count=pf_count+{$eventCount}",
89 "pf_time=pf_time+{$timeSum}",
90 "pf_memory=pf_memory+{$memorySum}",
95 } catch ( DBError
$e ) {
100 $useTrx ?
$dbw->endAtomic( $fname ) : null;
101 } catch ( DBError
$e ) {