Fix variable name in doc comment added in r78192
[lhc/web/wiklou.git] / includes / Profiler.php
index 45b5b8b..6deb742 100644 (file)
@@ -1,7 +1,10 @@
 <?php
 /**
+ * @defgroup Profiler Profiler
+ *
+ * @file
+ * @ingroup Profiler
  * This file is only included if profiling is enabled
- * @addtogroup Profiler
  */
 
 /** backward compatibility */
@@ -9,7 +12,7 @@ $wgProfiling = true;
 
 /**
  * Begin profiling of a function
- * @param $functioname name of the function we will profile
+ * @param $functionname String: name of the function we will profile
  */
 function wfProfileIn( $functionname ) {
        global $wgProfiler;
@@ -18,7 +21,7 @@ function wfProfileIn( $functionname ) {
 
 /**
  * Stop profiling of a function
- * @param $functioname name of the function we have profiled
+ * @param $functionname String: name of the function we have profiled
  */
 function wfProfileOut( $functionname = 'missing' ) {
        global $wgProfiler;
@@ -28,8 +31,8 @@ function wfProfileOut( $functionname = 'missing' ) {
 /**
  * Returns a profiling output to be stored in debug file
  *
- * @param float $start
- * @param float $elapsed time elapsed since the beginning of the request
+ * @param $start Float
+ * @param $elapsed Float: time elapsed since the beginning of the request
  */
 function wfGetProfilingOutput( $start, $elapsed ) {
        global $wgProfiler;
@@ -52,12 +55,13 @@ if (!function_exists('memory_get_usage')) {
 }
 
 /**
+ * @ingroup Profiler
  * @todo document
- * @addtogroup Profiler
  */
 class Profiler {
        var $mStack = array (), $mWorkStack = array (), $mCollated = array ();
        var $mCalls = array (), $mTotals = array ();
+       var $mTemplated = false;
 
        function __construct() {
                // Push an entry for the pre-profile setup time onto the stack
@@ -72,11 +76,12 @@ class Profiler {
 
        /**
         * Called by wfProfieIn()
-        * @param string $functionname
+        *
+        * @param $functionname String
         */
        function profileIn( $functionname ) {
-               global $wgDebugFunctionEntry;
-
+               global $wgDebugFunctionEntry, $wgProfiling;
+               if( !$wgProfiling ) return;
                if( $wgDebugFunctionEntry ){
                        $this->debug( str_repeat( ' ', count( $this->mWorkStack ) ) . 'Entering ' . $functionname . "\n" );
                }
@@ -86,11 +91,12 @@ class Profiler {
 
        /**
         * Called by wfProfieOut()
-        * @param string $functionname
+        *
+        * @param $functionname String
         */
        function profileOut($functionname) {
-               global $wgDebugFunctionEntry;
-
+               global $wgDebugFunctionEntry, $wgProfiling;
+               if( !$wgProfiling ) return;
                $memory = memory_get_usage();
                $time = $this->getTime();
 
@@ -125,13 +131,28 @@ class Profiler {
         * called by wfProfileClose()
         */
        function close() {
+               global $wgProfiling;
+
+               # Avoid infinite loop
+               if( !$wgProfiling )
+                       return;
+
                while( count( $this->mWorkStack ) ){
                        $this->profileOut( 'close' );
                }
        }
 
        /**
-        * called by wfGetProfilingOutput()
+        * Mark this call as templated or not
+        *
+        * @param $t Boolean
+        */
+       function setTemplated( $t ) {
+               $this->mTemplated = $t;
+       }
+
+       /**
+        * Called by wfGetProfilingOutput()
         */
        function getOutput() {
                global $wgDebugFunctionEntry, $wgProfileCallTree;
@@ -142,7 +163,12 @@ class Profiler {
                }
                $this->close();
 
-               if( $wgProfileCallTree ){
+               if( $wgProfileCallTree ) {
+                       global $wgProfileToDatabase;
+                       # XXX: We must call $this->getFunctionReport() to log to the DB
+                       if( $wgProfileToDatabase ) {
+                               $this->getFunctionReport();
+                       }
                        return $this->getCallTree();
                } else {
                        return $this->getFunctionReport();
@@ -150,7 +176,7 @@ class Profiler {
        }
 
        /**
-        * returns a tree of function call instead of a list of functions
+        * Returns a tree of function call instead of a list of functions
         */
        function getCallTree() {
                return implode( '', array_map( array( &$this, 'getCallTreeLine' ), $this->remapCallTree( $this->mStack ) ) );
@@ -159,7 +185,7 @@ class Profiler {
        /**
         * Recursive function the format the current profiling array into a tree
         *
-        * @param array $stack profiling array
+        * @param $stack profiling array
         */
        function remapCallTree( $stack ) {
                if( count( $stack ) < 2 ){
@@ -199,16 +225,13 @@ class Profiler {
        /**
         * Callback to get a formatted line for the call tree
         */
-       function getCallTreeLine($entry) {
+       function getCallTreeLine( $entry ) {
                list( $fname, $level, $start, /* $x */, $end)  = $entry;
                $delta = $end - $start;
                $space = str_repeat(' ', $level);
-
                # The ugly double sprintf is to work around a PHP bug,
                # which has been fixed in recent releases.
-               return sprintf( "%10s %s %s\n",
-                       trim( sprintf( "%7.3f", $delta * 1000.0 ) ),
-                       $space, $fname );
+               return sprintf( "%10s %s %s\n", trim( sprintf( "%7.3f", $delta * 1000.0 ) ), $space, $fname );
        }
 
        function getTime() {
@@ -248,6 +271,7 @@ class Profiler {
                wfProfileOut( '-overhead-total' );
 
                # First, subtract the overhead!
+               $overheadTotal = $overheadMemory = $overheadInternal = array();
                foreach( $this->mStack as $entry ){
                        $fname = $entry[0];
                        $start = $entry[2];
@@ -263,9 +287,9 @@ class Profiler {
                                $overheadInternal[] = $elapsed;
                        }
                }
-               $overheadTotal = array_sum( $overheadTotal ) / count( $overheadInternal );
-               $overheadMemory = array_sum( $overheadMemory ) / count( $overheadInternal );
-               $overheadInternal = array_sum( $overheadInternal ) / count( $overheadInternal );
+               $overheadTotal = $overheadTotal ? array_sum( $overheadTotal ) / count( $overheadInternal ) : 0;
+               $overheadMemory = $overheadMemory ? array_sum( $overheadMemory ) / count( $overheadInternal ) : 0;
+               $overheadInternal = $overheadInternal ? array_sum( $overheadInternal ) / count( $overheadInternal ) : 0;
 
                # Collate
                foreach( $this->mStack as $index => $entry ){
@@ -313,8 +337,8 @@ class Profiler {
                        $percent = $total ? 100. * $elapsed / $total : 0;
                        $memory = $this->mMemory[$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]);
-
-                       if( $wgProfileToDatabase ){
+                       # Log to the DB
+                       if( $wgProfileToDatabase ) {
                                self::logToDB($fname, (float) ($elapsed * 1000), $calls, (float) ($memory) );
                        }
                }
@@ -344,16 +368,16 @@ class Profiler {
        /**
         * Log a function into the database.
         *
-        * @param string $name function name
-        * @param float $timeSum
-        * @param int $eventCount number of times that function was called
+        * @param $name String: function name
+        * @param $timeSum Float
+        * @param $eventCount Integer: number of times that function was called
+        * @param $memorySum Integer: memory used by the function
         */
        static function logToDB( $name, $timeSum, $eventCount, $memorySum ){
                # Do not log anything if database is readonly (bug 5375)
                if( wfReadOnly() ) { return; }
 
-               # Warning: $wguname is a live patch, it should be moved to Setup.php
-               global $wguname, $wgProfilePerHost;
+               global $wgProfilePerHost;
 
                $dbw = wfGetDB( DB_MASTER );
                if( !is_object( $dbw ) )
@@ -363,7 +387,7 @@ class Profiler {
                $name = substr($name, 0, 255);
 
                if( $wgProfilePerHost ){
-                       $pfhost = $wguname['nodename'];
+                       $pfhost = wfHostname();
                } else {
                        $pfhost = '';
                }
@@ -409,7 +433,8 @@ class Profiler {
 
        /**
         * Get function caller
-        * @param int $level
+        *
+        * @param $level Integer
         */
        static function getCaller( $level ) {
                $backtrace = wfDebugBacktrace();
@@ -427,7 +452,8 @@ class Profiler {
 
        /**
         * Add an entry in the debug log file
-        * @param string $s string to output
+        *
+        * @param $s String to output
         */
        function debug( $s ) {
                if( function_exists( 'wfDebug' ) ) {