* Made the profiler work in HipHop:
authorTim Starling <tstarling@users.mediawiki.org>
Tue, 31 May 2011 06:05:05 +0000 (06:05 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Tue, 31 May 2011 06:05:05 +0000 (06:05 +0000)
** Don't try to set a global variable in the same file as a class definition (Profiler.php). Set it in WebStart.php instead.
** In StartProfiler.sample, don't use require_once() to get ProfilerStub.

* Removed the setproctitle() stuff from ProfilerStub, the extension is not maintained and doesn't work with Apache 2.x
* Added an optimisation to wfProfileIn() and wfProfileOut() to reduce the overhead when profiling is not enabled
* Added the ability to configure in StartProfiler.php whether CPU time or wall-clock time is used, avoiding recompilation

StartProfiler.sample
includes/WebStart.php
includes/profiler/Profiler.php
includes/profiler/ProfilerStub.php

index f55b4d6..6bce634 100644 (file)
@@ -1,10 +1,8 @@
 <?php
 
-require_once( dirname( __FILE__ ) . '/includes/profiler/ProfilerStub.php' );
-
 /**
  * To use a profiler, copy this file to StartProfiler.php,
- * delete the PHP line above, and add something like this:
+ * and add something like this:
  *
  *   $wgProfiler['class'] = 'Profiler';
  *
index ca46b89..6ace96c 100644 (file)
@@ -81,17 +81,19 @@ if ( isset( $_SERVER['MW_COMPILED'] ) ) {
        # Start the autoloader, so that extensions can derive classes from core files
        require_once( "$IP/includes/AutoLoader.php" );
 
-       # Start profiler
-       # @todo FIXME: Rewrite wfProfileIn/wfProfileOut so that they can work in compiled mode
+       # Load the profiler
        require_once( "$IP/includes/profiler/Profiler.php" );
-       if ( file_exists( "$IP/StartProfiler.php" ) ) {
-               require_once( "$IP/StartProfiler.php" );
-       }
 
        # Load up some global defines.
        require_once( "$IP/includes/Defines.php" );
 }
 
+# Start the profiler
+$wgProfiler = array();
+if ( file_exists( "$IP/StartProfiler.php" ) ) {
+       require( "$IP/StartProfiler.php" );
+}
+
 wfProfileIn( 'WebStart.php-conf' );
 
 # Load default settings
index db5db52..c9ac4eb 100644 (file)
@@ -7,21 +7,15 @@
  * This file is only included if profiling is enabled
  */
 
-/**
- * Default profiling configuration. Permitted keys are:
- *   class   : Name of Profiler or subclass to use for profiling
- *   visible : Whether to output the profile info [ProfilerSimpleText only]
- */
-$wgProfiler = array(
-       'class' => 'ProfilerStub',
-);
-
 /**
  * Begin profiling of a function
  * @param $functionname String: name of the function we will profile
  */
 function wfProfileIn( $functionname ) {
-       Profiler::instance()->profileIn( $functionname );
+       global $wgProfiler;
+       if ( isset( $wgProfiler['class'] ) ) {
+               Profiler::instance()->profileIn( $functionname );
+       }
 }
 
 /**
@@ -29,7 +23,10 @@ function wfProfileIn( $functionname ) {
  * @param $functionname String: name of the function we have profiled
  */
 function wfProfileOut( $functionname = 'missing' ) {
-       Profiler::instance()->profileOut( $functionname );
+       global $wgProfiler;
+       if ( isset( $wgProfiler['class'] ) ) {
+               Profiler::instance()->profileOut( $functionname );
+       }
 }
 
 /**
@@ -40,11 +37,12 @@ class Profiler {
        var $mStack = array (), $mWorkStack = array (), $mCollated = array ();
        var $mCalls = array (), $mTotals = array ();
        var $mTemplated = false;
+       var $mTimeMetric = 'wall';
        private $mCollateDone = false;
        protected $mProfileID = false;
        private static $__instance = null;
 
-       function __construct() {
+       function __construct( $params ) {
                // Push an entry for the pre-profile setup time onto the stack
                global $wgRequestTime;
                if ( !empty( $wgRequestTime ) ) {
@@ -53,6 +51,9 @@ class Profiler {
                } else {
                        $this->profileIn( '-total' );
                }
+               if ( isset( $params['timeMetric'] ) ) {
+                       $this->mTimeMetric = $params['timeMetric'];
+               }
        }
 
        /**
@@ -63,14 +64,13 @@ class Profiler {
                if( is_null( self::$__instance ) ) {
                        global $wgProfiler;
                        if( is_array( $wgProfiler ) ) {
-                               $class = $wgProfiler['class'];
+                               $class = isset( $wgProfiler['class'] ) ? $wgProfiler['class'] : 'ProfilerStub';
                                self::$__instance = new $class( $wgProfiler );
                        } elseif( $wgProfiler instanceof Profiler ) {
                                self::$__instance = $wgProfiler; // back-compat
                        } else {
                                self::$__instance = new ProfilerStub;
                        }
-                       
                }
                return self::$__instance;
        }
@@ -253,8 +253,11 @@ class Profiler {
        }
 
        function getTime() {
-               return microtime(true);
-               #return $this->getUserTime();
+               if ( $this->mTimeMetric === 'user' ) {
+                       return $this->getUserTime();
+               } else {
+                       return microtime(true);
+               }
        }
 
        function getUserTime() {
@@ -474,7 +477,7 @@ class Profiler {
         * @param $s String to output
         */
        function debug( $s ) {
-               if( function_exists( 'wfDebug' ) ) {
+               if( defined( 'MW_COMPILED' ) || function_exists( 'wfDebug' ) ) {
                        wfDebug( $s );
                }
        }
index 5c82609..58c1975 100644 (file)
@@ -5,57 +5,11 @@
  * @ingroup Profiler
  */
 class ProfilerStub extends Profiler {
-
-       /**
-        * is setproctitle function available?
-        * @var bool
-        */
-       private $haveProctitle;
-       private $hackWhere = array();
-
-       /**
-        * Constructor. Check for proctitle.
-        */
-       public function __construct() {
-               $this->haveProctitle = function_exists( 'setproctitle' );
-       }
-
        public function isStub() {
                return true;
        }
-
-       /**
-        * Begin profiling of a function
-        * @param $fn string
-        */
-       public function profileIn( $fn ) {
-               global $wgDBname;
-               if( $this->haveProctitle ){
-                       $this->hackWhere[] = $fn;
-                       setproctitle( $fn . " [$wgDBname]" );
-               }
-       }
-
-       /**
-        * Stop profiling of a function
-        * @param $fn string
-        */
-       public function profileOut( $fn ) {
-               global $wgDBname;
-               if( !$this->haveProctitle ) {
-                       return;
-               }
-               if( count( $this->hackWhere ) ) {
-                       array_pop( $this->hackWhere );
-               }
-               if( count( $this->hackWhere ) ) {
-                       setproctitle( $this->hackWhere[count( $this->hackWhere )-1] . " [$wgDBname]" );
-               }
-       }
-
-       /**
-        * Does nothing, just for compatibility
-        */
+       public function profileIn( $fn ) {}
+       public function profileOut( $fn ) {}
        public function getOutput() {}
        public function close() {}
 }