MediaWiki  1.34.0
ProfilerOutputDb.php
Go to the documentation of this file.
1 <?php
25 
35  private $perHost = false;
36 
37  public function __construct( Profiler $collector, array $params ) {
38  parent::__construct( $collector, $params );
39  wfDeprecated( __CLASS__, '1.34' );
40 
41  // Initialize per-host profiling from config, back-compat if available
42  if ( isset( $this->params['perHost'] ) ) {
43  $this->perHost = $this->params['perHost'];
44  }
45  }
46 
47  public function canUse() {
48  # Do not log anything if database is readonly (T7375)
49  return !wfReadOnly();
50  }
51 
52  public function log( array $stats ) {
53  try {
54  $dbw = wfGetDB( DB_MASTER );
55  } catch ( DBError $e ) {
56  return; // ignore
57  }
58 
59  $fname = __METHOD__;
60  $dbw->onTransactionCommitOrIdle( function () use ( $stats, $fname, $dbw ) {
61  $pfhost = $this->perHost ? wfHostname() : '';
62  // Sqlite: avoid excess b-tree rebuilds (mostly for non-WAL mode)
63  // non-Sqlite: lower contention with small transactions
64  $useTrx = ( $dbw->getType() === 'sqlite' );
65 
66  try {
67  $useTrx ? $dbw->startAtomic( $fname ) : null;
68 
69  foreach ( $stats as $data ) {
70  $name = $data['name'];
71  $eventCount = $data['calls'];
72  $timeSum = (float)$data['real'];
73  $memorySum = (float)$data['memory'];
74  $name = substr( $name, 0, 255 );
75 
76  // Kludge
77  $timeSum = $timeSum >= 0 ? $timeSum : 0;
78  $memorySum = $memorySum >= 0 ? $memorySum : 0;
79 
80  $dbw->upsert( 'profiling',
81  [
82  'pf_name' => $name,
83  'pf_count' => $eventCount,
84  'pf_time' => $timeSum,
85  'pf_memory' => $memorySum,
86  'pf_server' => $pfhost
87  ],
88  [ [ 'pf_name', 'pf_server' ] ],
89  [
90  "pf_count=pf_count+{$eventCount}",
91  "pf_time=pf_time+{$timeSum}",
92  "pf_memory=pf_memory+{$memorySum}",
93  ],
94  $fname
95  );
96  }
97  } catch ( DBError $e ) {
98  // ignore
99  }
100 
101  try {
102  $useTrx ? $dbw->endAtomic( $fname ) : null;
103  } catch ( DBError $e ) {
104  // ignore
105  }
106  } );
107  }
108 }
wfHostname
wfHostname()
Fetch server name for use in error reporting etc.
Definition: GlobalFunctions.php:1326
wfReadOnly
wfReadOnly()
Check whether the wiki is in read-only mode.
Definition: GlobalFunctions.php:1171
ProfilerOutput\$collector
Profiler $collector
Definition: ProfilerOutput.php:29
Wikimedia\Rdbms\DBError
Database error base class.
Definition: DBError.php:30
ProfilerOutputDb\$perHost
bool $perHost
Whether to store host data with profiling calls.
Definition: ProfilerOutputDb.php:35
ProfilerOutput\$params
array $params
Configuration of $wgProfiler.
Definition: ProfilerOutput.php:31
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1044
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2575
Profiler
Profiler base class that defines the interface and some trivial functionality.
Definition: Profiler.php:33
DB_MASTER
const DB_MASTER
Definition: defines.php:26
ProfilerOutputDb\__construct
__construct(Profiler $collector, array $params)
Definition: ProfilerOutputDb.php:37
ProfilerOutputDb\log
log(array $stats)
Log MediaWiki-style profiling data.
Definition: ProfilerOutputDb.php:52
ProfilerOutput
Base class for profiling output.
Definition: ProfilerOutput.php:27
ProfilerOutputDb\canUse
canUse()
Can this output type be used?
Definition: ProfilerOutputDb.php:47
ProfilerOutputDb
Logs profiling data into the local DB.
Definition: ProfilerOutputDb.php:33