Fix 2714 : backlink from special:whatlinkshere was hard set as 'existing'
[lhc/web/wiklou.git] / includes / Profiling.php
index 634735c..6dc007c 100755 (executable)
@@ -44,14 +44,19 @@ if (!function_exists('memory_get_usage')) {
 class Profiler {
        var $mStack = array (), $mWorkStack = array (), $mCollated = array ();
        var $mCalls = array (), $mTotals = array ();
-       /*
+       
        function Profiler()
        {
-               $this->mProfileStack = array();
-               $this->mWorkStack = array();
-               $this->mCollated = array();
+               // Push an entry for the pre-profile setup time onto the stack
+               global $wgRequestTime;
+               if ( !empty( $wgRequestTime ) ) {
+                       $this->mWorkStack[] = array( '-total', 0, $wgRequestTime, 0 );
+                       $this->mStack[] = array( '-setup', 1, $wgRequestTime, 0, microtime(), 0 );
+               } else {
+                       $this->profileIn( '-total' );
+               }
+                       
        }
-       */
 
        function profileIn($functionname) {
                global $wgDebugFunctionEntry;
@@ -65,7 +70,7 @@ class Profiler {
                $memory = memory_get_usage();
                $time = $this->getTime();
 
-               global $wgDebugProfiling, $wgDebugFunctionEntry;
+               global $wgDebugFunctionEntry;
 
                if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
                        wfDebug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
@@ -78,13 +83,13 @@ class Profiler {
                } else {
                        //if ($wgDebugProfiling) {
                                if ($functionname == 'close') {
-                                       $message = "Profile section ended by close(): {$bit[0]}\n";
-                                       wfDebug( $message );
+                                       $message = "Profile section ended by close(): {$bit[0]}";
+                                       wfDebug( "$message\n" );
                                        $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
                                }
                                elseif ($bit[0] != $functionname) {
-                                       $message = "Profiling error: in({$bit[0]}), out($functionname)\n";
-                                       wfDebug( $message );
+                                       $message = "Profiling error: in({$bit[0]}), out($functionname)";
+                                       wfDebug( "$message\n" );
                                        $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
                                }
                        //}
@@ -279,7 +284,7 @@ class Profiler {
                        $calls = $this->mCalls[$fname];
                        $percent = $total ? 100. * $elapsed / $total : 0;
                        $memory = $this->mMemory[$fname];
-                       $prof .= sprintf($format, $fname, $calls, (float) ($elapsed * 1000), (float) ($elapsed * 1000) / $calls, $percent, $memory, ($this->mMin[$fname] * 1000.0), ($this->mMax[$fname] * 1000.0), $this->mOverhead[$fname]);
+                       $prof .= sprintf($format, substr($fname, 0, $nameWidth), $calls, (float) ($elapsed * 1000), (float) ($elapsed * 1000) / $calls, $percent, $memory, ($this->mMin[$fname] * 1000.0), ($this->mMax[$fname] * 1000.0), $this->mOverhead[$fname]);
 
                        global $wgProfileToDatabase;
                        if ($wgProfileToDatabase) {
@@ -313,18 +318,23 @@ class Profiler {
         * @static
         */
        function logToDB($name, $timeSum, $eventCount) {
+               # Warning: $wguname is a live patch, it should be moved to Setup.php
+               global $wguname;
+
                $fname = 'Profiler::logToDB';
                $dbw = & wfGetDB(DB_MASTER);
                $profiling = $dbw->tableName('profiling');
 
                $name = substr($name, 0, 255);
                $encname = $dbw->strencode($name);
-               $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} "."WHERE pf_name='{$encname}'";
+               $sql = "UPDATE $profiling "."SET pf_count=pf_count+{$eventCount}, "."pf_time=pf_time + {$timeSum} ".
+                       "WHERE pf_name='{$encname}' AND pf_server='{$wguname['nodename']}'";
                $dbw->query($sql);
 
                $rc = $dbw->affectedRows();
                if ($rc == 0) {
-                       $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount, 'pf_time' => $timeSum), $fname, array ('IGNORE'));
+                       $dbw->insert('profiling', array ('pf_name' => $name, 'pf_count' => $eventCount, 
+                               'pf_time' => $timeSum, 'pf_server' => $wguname['nodename'] ), $fname, array ('IGNORE'));
                }
                // When we upgrade to mysql 4.1, the insert+update
                // can be merged into just a insert with this construct added:
@@ -333,8 +343,16 @@ class Profiler {
                //     "pf_time=pf_time + VALUES(pf_time)"; 
        }
 
+       /**
+        * Get the function name of the current profiling section
+        */
+       function getCurrentSection() {
+               $elt = end($this->mWorkStack);
+               return $elt[0];
+       }
+
 }
 
 $wgProfiler = new Profiler();
-$wgProfiler->profileIn('-total');
+
 ?>