3 * Transaction profiling for contention
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 * @author Aaron Schulz
26 * Helper class that detects high-contention DB queries via profiling calls
28 * This class is meant to work with a DatabaseBase object, which manages queries
32 class TransactionProfiler
{
33 /** @var float Seconds */
34 protected $dbLockThreshold = 3.0;
35 /** @var float Seconds */
36 protected $eventThreshold = .25;
38 protected $affectedThreshold = 500;
40 /** @var array transaction ID => (write start time, list of DBs involved) */
41 protected $dbTrxHoldingLocks = array();
42 /** @var array transaction ID => list of (query name, start time, end time) */
43 protected $dbTrxMethodTimes = array();
46 * Mark a DB as in a transaction with one or more writes pending
48 * Note that there can be multiple connections to a single DB.
50 * @param string $server DB server
51 * @param string $db DB name
52 * @param string $id ID string of transaction
54 public function transactionWritingIn( $server, $db, $id ) {
55 $name = "{$server} ({$db}) (TRX#$id)";
56 if ( isset( $this->dbTrxHoldingLocks
[$name] ) ) {
57 wfDebugLog( 'DBPerformance', "Nested transaction for '$name' - out of sync." );
59 $this->dbTrxHoldingLocks
[$name] = array(
60 'start' => microtime( true ),
61 'conns' => array(), // all connections involved
63 $this->dbTrxMethodTimes
[$name] = array();
65 foreach ( $this->dbTrxHoldingLocks
as $name => &$info ) {
66 // Track all DBs in transactions for this transaction
67 $info['conns'][$name] = 1;
72 * Register the name and time of a method for slow DB trx detection
74 * This assumes that all queries are synchronous (non-overlapping)
76 * @param string $query Function name or generalized SQL
77 * @param float $sTime Starting UNIX wall time
78 * @param bool $isWrite Whether this is a write query
79 * @param integer $n Number of affected rows
81 public function recordQueryCompletion( $query, $sTime, $isWrite = false, $n = 0 ) {
82 $eTime = microtime( true );
83 $elapsed = ( $eTime - $sTime );
85 if ( $isWrite && $n > $this->affectedThreshold
&& PHP_SAPI
!== 'cli' ) {
86 wfDebugLog( 'DBPerformance',
87 "Query affected $n rows:\n" . $query . "\n" . wfBacktrace( true ) );
90 if ( !$this->dbTrxHoldingLocks
) {
93 } elseif ( !$isWrite && $elapsed < $this->eventThreshold
) {
94 // Not an important query nor slow enough
98 foreach ( $this->dbTrxHoldingLocks
as $name => $info ) {
99 $lastQuery = end( $this->dbTrxMethodTimes
[$name] );
101 // Additional query in the trx...
102 $lastEnd = $lastQuery[2];
103 if ( $sTime >= $lastEnd ) { // sanity check
104 if ( ( $sTime - $lastEnd ) > $this->eventThreshold
) {
105 // Add an entry representing the time spent doing non-queries
106 $this->dbTrxMethodTimes
[$name][] = array( '...delay...', $lastEnd, $sTime );
108 $this->dbTrxMethodTimes
[$name][] = array( $query, $sTime, $eTime );
111 // First query in the trx...
112 if ( $sTime >= $info['start'] ) { // sanity check
113 $this->dbTrxMethodTimes
[$name][] = array( $query, $sTime, $eTime );
120 * Mark a DB as no longer in a transaction
122 * This will check if locks are possibly held for longer than
123 * needed and log any affected transactions to a special DB log.
124 * Note that there can be multiple connections to a single DB.
126 * @param string $server DB server
127 * @param string $db DB name
128 * @param string $id ID string of transaction
130 public function transactionWritingOut( $server, $db, $id ) {
131 $name = "{$server} ({$db}) (TRX#$id)";
132 if ( !isset( $this->dbTrxMethodTimes
[$name] ) ) {
133 wfDebugLog( 'DBPerformance', "Detected no transaction for '$name' - out of sync." );
136 // Fill in the last non-query period...
137 $lastQuery = end( $this->dbTrxMethodTimes
[$name] );
139 $now = microtime( true );
140 $lastEnd = $lastQuery[2];
141 if ( ( $now - $lastEnd ) > $this->eventThreshold
) {
142 $this->dbTrxMethodTimes
[$name][] = array( '...delay...', $lastEnd, $now );
145 // Check for any slow queries or non-query periods...
147 foreach ( $this->dbTrxMethodTimes
[$name] as $info ) {
148 $elapsed = ( $info[2] - $info[1] );
149 if ( $elapsed >= $this->dbLockThreshold
) {
155 $dbs = implode( ', ', array_keys( $this->dbTrxHoldingLocks
[$name]['conns'] ) );
156 $msg = "Sub-optimal transaction on DB(s) [{$dbs}]:\n";
157 foreach ( $this->dbTrxMethodTimes
[$name] as $i => $info ) {
158 list( $query, $sTime, $end ) = $info;
159 $msg .= sprintf( "%d\t%.6f\t%s\n", $i, ( $end - $sTime ), $query );
161 wfDebugLog( 'DBPerformance', $msg );
163 unset( $this->dbTrxHoldingLocks
[$name] );
164 unset( $this->dbTrxMethodTimes
[$name] );