Merge "Correctly handle incorrect namespace in cleanupTitles.php"
[lhc/web/wiklou.git] / includes / profiler / Profiler.php
index d5cdf28..7b8f340 100644 (file)
@@ -25,7 +25,7 @@
 
 /**
  * Begin profiling of a function
- * @param string $functionname name of the function we will profile
+ * @param string $functionname Name of the function we will profile
  */
 function wfProfileIn( $functionname ) {
        if ( Profiler::$__instance === null ) { // use this directly to reduce overhead
@@ -38,7 +38,7 @@ function wfProfileIn( $functionname ) {
 
 /**
  * Stop profiling of a function
- * @param string $functionname name of the function we have profiled
+ * @param string $functionname Name of the function we have profiled
  */
 function wfProfileOut( $functionname = 'missing' ) {
        if ( Profiler::$__instance === null ) { // use this directly to reduce overhead
@@ -126,7 +126,7 @@ abstract class Profiler {
                        if ( is_array( $wgProfiler ) ) {
                                if ( !isset( $wgProfiler['class'] ) ) {
                                        $class = 'ProfilerStub';
-                               } elseif ( $wgProfiler['class'] === 'Profiler'  ) {
+                               } elseif ( $wgProfiler['class'] === 'Profiler' ) {
                                        $class = 'ProfilerStub'; // b/c; don't explode
                                } else {
                                        $class = $wgProfiler['class'];
@@ -196,7 +196,7 @@ abstract class Profiler {
        /**
         * Called by wfProfieOut()
         *
-        * @param  string $functionname
+        * @param string $functionname
         */
        abstract public function profileOut( $functionname );
 
@@ -326,7 +326,7 @@ abstract class Profiler {
        /**
         * Add an entry in the debug log file
         *
-        * @param string $s to output
+        * @param string $s String to output
         */
        protected function debug( $s ) {
                if ( function_exists( 'wfDebug' ) ) {
@@ -338,7 +338,7 @@ abstract class Profiler {
         * Add an entry in the debug log group
         *
         * @param string $group Group to send the message to
-        * @param string $s to output
+        * @param string $s String to output
         */
        protected function debugGroup( $group, $s ) {
                if ( function_exists( 'wfDebugLog' ) ) {
@@ -356,7 +356,7 @@ abstract class Profiler {
  * @since 1.24
  */
 class TransactionProfiler {
-       /** @var float seconds */
+       /** @var float Seconds */
        protected $mDBLockThreshold = 3.0;
        /** @var array DB/server name => (active trx count, time, DBs involved) */
        protected $mDBTrxHoldingLocks = array();
@@ -375,12 +375,12 @@ class TransactionProfiler {
        public function transactionWritingIn( $server, $db, $id ) {
                $name = "{$server} ({$db}) (TRX#$id)";
                if ( isset( $this->mDBTrxHoldingLocks[$name] ) ) {
-                       ++$this->mDBTrxHoldingLocks[$name]['refs'];
-               } else {
-                       $this->mDBTrxHoldingLocks[$name] = array(
-                               'refs' => 1, 'start' => microtime( true ), 'conns' => array() );
-                       $this->mDBTrxMethodTimes[$name] = array();
+                       wfDebugLog( 'DBPerformance', "Nested transaction for '$name' - out of sync." );
                }
+               $this->mDBTrxHoldingLocks[$name] =
+                       array( 'start' => microtime( true ), 'conns' => array() );
+               $this->mDBTrxMethodTimes[$name] = array();
+
                foreach ( $this->mDBTrxHoldingLocks as $name => &$info ) {
                        $info['conns'][$name] = 1; // track all DBs in transactions for this transaction
                }
@@ -397,7 +397,7 @@ class TransactionProfiler {
        public function recordFunctionCompletion( $method, $realtime ) {
                if ( !$this->mDBTrxHoldingLocks ) {
                        return; // short-circuit
-               // @TODO: hardcoded check is a tad janky (what about FOR UPDATE?)
+               // @todo hardcoded check is a tad janky (what about FOR UPDATE?)
                } elseif ( !preg_match( '/^query-m: (?!SELECT)/', $method )
                        && $realtime < $this->mDBLockThreshold
                ) {
@@ -425,26 +425,28 @@ class TransactionProfiler {
         */
        public function transactionWritingOut( $server, $db, $id ) {
                $name = "{$server} ({$db}) (TRX#$id)";
-               if ( --$this->mDBTrxHoldingLocks[$name]['refs'] <= 0 ) {
-                       $slow = false;
-                       foreach ( $this->mDBTrxMethodTimes[$name] as $info ) {
-                               $realtime = $info[1];
-                               if ( $realtime >= $this->mDBLockThreshold ) {
-                                       $slow = true;
-                                       break;
-                               }
+               if ( !isset( $this->mDBTrxMethodTimes[$name] ) ) {
+                       wfDebugLog( 'DBPerformance', "Detected no transaction for '$name' - out of sync." );
+                       return;
+               }
+               $slow = false;
+               foreach ( $this->mDBTrxMethodTimes[$name] as $info ) {
+                       $realtime = $info[1];
+                       if ( $realtime >= $this->mDBLockThreshold ) {
+                               $slow = true;
+                               break;
                        }
-                       if ( $slow ) {
-                               $dbs = implode( ', ', array_keys( $this->mDBTrxHoldingLocks[$name]['conns'] ) );
-                               $msg = "Sub-optimal transaction on DB(s) [{$dbs}]:\n";
-                               foreach ( $this->mDBTrxMethodTimes[$name] as $i => $info ) {
-                                       list( $method, $realtime ) = $info;
-                                       $msg .= sprintf( "%d\t%.6f\t%s\n", $i, $realtime, $method );
-                               }
-                               wfDebugLog( 'DBPerformance', $msg );
+               }
+               if ( $slow ) {
+                       $dbs = implode( ', ', array_keys( $this->mDBTrxHoldingLocks[$name]['conns'] ) );
+                       $msg = "Sub-optimal transaction on DB(s) [{$dbs}]:\n";
+                       foreach ( $this->mDBTrxMethodTimes[$name] as $i => $info ) {
+                               list( $method, $realtime ) = $info;
+                               $msg .= sprintf( "%d\t%.6f\t%s\n", $i, $realtime, $method );
                        }
-                       unset( $this->mDBTrxHoldingLocks[$name] );
-                       unset( $this->mDBTrxMethodTimes[$name] );
+                       wfDebugLog( 'DBPerformance', $msg );
                }
+               unset( $this->mDBTrxHoldingLocks[$name] );
+               unset( $this->mDBTrxMethodTimes[$name] );
        }
 }