Fix 2714 : backlink from special:whatlinkshere was hard set as 'existing'
[lhc/web/wiklou.git] / includes / Profiling.php
index 58026b0..6dc007c 100755 (executable)
@@ -44,30 +44,33 @@ 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;
                if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
                        wfDebug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
                }
-               $this->mWorkStack[] = array($functionname, count($this->mWorkStack), microtime(), memory_get_usage());
-               #$this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), $this->getUserTime(), memory_get_usage());
+               $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), $this->getTime(), memory_get_usage());
        }
 
        function profileOut($functionname) {
                $memory = memory_get_usage();
-               $time = microtime();
-               #$time = $this->getUserTime();
+               $time = $this->getTime();
 
-               global $wgDebugProfiling, $wgDebugFunctionEntry;
+               global $wgDebugFunctionEntry;
 
                if ($wgDebugFunctionEntry && function_exists('wfDebug')) {
                        wfDebug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
@@ -78,14 +81,18 @@ class Profiler {
                if (!$bit) {
                        wfDebug("Profiling error, !\$bit: $functionname\n");
                } else {
-                       if ($wgDebugProfiling) {
+                       //if ($wgDebugProfiling) {
                                if ($functionname == 'close') {
-                                       wfDebug("Profile section ended by close(): {$bit[0]}\n");
+                                       $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) {
-                                       wfDebug("Profiling error: in({$bit[0]}), out($functionname)\n");
+                                       $message = "Profiling error: in({$bit[0]}), out($functionname)";
+                                       wfDebug( "$message\n" );
+                                       $this->mStack[] = array( $message, 0, '0 0', 0, '0 0', 0 );
                                }
-                       }
+                       //}
                        $bit[] = $time;
                        $bit[] = $memory;
                        $this->mStack[] = $bit;
@@ -161,24 +168,6 @@ class Profiler {
 
                # The ugly double sprintf is to work around a PHP bug,
                # which has been fixed in recent releases.
-<<<<<<< Profiling.php
-               return sprintf("%10s %s %s\n", trim(sprintf("%7.3f", $delta * 1000.0)), $space, $fname);
-       }
-
-       function micro2Float($micro) {
-               list ($whole, $fractional) = explode(' ', $micro);
-               return (float) $whole + (float) $fractional;
-       }
-
-       function microDelta($start, $end) {
-               return $this->micro2Float($end) - $this->micro2Float($start);
-       }
-
-       function getFunctionReport() {
-               $width = 125;
-               $format = "%-". ($width -34)."s %6d %6.3f %6.3f %7.3f%% %6d (%6.3f-%6.3f) [%d]\n";
-               $titleFormat = "%-". ($width -34)."s %9s %9s %9s %9s %6s\n";
-=======
                return sprintf( "%10s %s %s\n",
                        trim( sprintf( "%7.3f", $delta * 1000.0 ) ),
                        $space, $fname );
@@ -194,6 +183,11 @@ class Profiler {
                       $this->micro2Float( $start );
        }
 
+       function getTime() {
+               return microtime();
+               #return $this->getUserTime();
+       }
+
        function getUserTime() {
                $ru = getrusage();
                return $ru['ru_utime.tv_sec'].' '.$ru['ru_utime.tv_usec'] / 1e6;
@@ -203,8 +197,7 @@ class Profiler {
                $width = 140;
                $nameWidth = $width - 65;
                $format =      "%-{$nameWidth}s %6d %13.3f %13.3f %13.3f%% %9d  (%13.3f -%13.3f) [%d]\n";
-               $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s %14s   %14s %9s\n";
->>>>>>> 1.29.2.6
+               $titleFormat = "%-{$nameWidth}s %6s %13s %13s %13s %9s\n";
                $prof = "\nProfiling data\n";
                $prof .= sprintf($titleFormat, 'Name', 'Calls', 'Total', 'Each', '%', 'Mem');
                $this->mCollated = array ();
@@ -257,10 +250,12 @@ class Profiler {
                        $subcalls = $this->calltreeCount($this->mStack, $index);
 
                        if (!preg_match('/^-overhead/', $fname)) {
-                               # Adjust for profiling overhead
-                               $elapsed -= $overheadInternal;
-                               $elapsed -= ($subcalls * $overheadTotal);
-                               $memory -= ($subcalls * $overheadMemory);
+                               # Adjust for profiling overhead (except special values with elapsed=0
+                               if ( $elapsed ) {
+                                       $elapsed -= $overheadInternal;
+                                       $elapsed -= ($subcalls * $overheadTotal);
+                                       $memory -= ($subcalls * $overheadMemory);
+                               }
                        }
 
                        if (!array_key_exists($fname, $this->mCollated)) {
@@ -289,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) {
@@ -323,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:
@@ -343,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');
+
 ?>