Merge "DumpUploads: output local path instead of mwstore path"
[lhc/web/wiklou.git] / includes / profiler / ProfilerXhprof.php
index f36cdc1..8fc0b77 100644 (file)
  *
  * To restrict the functions for which profiling data is collected, you can
  * use either a whitelist ($wgProfiler['include']) or a blacklist
- * ($wgProfiler['exclude']) containing an array of function names. The
- * blacklist functionality is built into HHVM and will completely exclude the
- * named functions from profiling collection. The whitelist is implemented by
- * Xhprof class which will filter the data collected by XHProf before reporting.
- * See documentation for the Xhprof class and the XHProf extension for
- * additional information.
+ * ($wgProfiler['exclude']) containing an array of function names.
+ * Shell-style patterns are also accepted.
  *
  * @author Bryan Davis <bd808@wikimedia.org>
  * @copyright © 2014 Bryan Davis and Wikimedia Foundation.
@@ -56,9 +52,9 @@
  */
 class ProfilerXhprof extends Profiler {
        /**
-        * @var Xhprof $xhprof
+        * @var XhprofData|null $xhprofData
         */
-       protected $xhprof;
+       protected $xhprofData;
 
        /**
         * Profiler for explicit, arbitrary, frame labels
@@ -70,14 +66,29 @@ class ProfilerXhprof extends Profiler {
         * @param array $params
         * @see Xhprof::__construct()
         */
-       public function __construct( array $params = array() ) {
+       public function __construct( array $params = [] ) {
                parent::__construct( $params );
-               $this->xhprof = new Xhprof( $params );
+
+               $flags = isset( $params['flags'] ) ? $params['flags'] : 0;
+               $options = isset( $params['exclude'] )
+                       ? [ 'ignored_functions' => $params['exclude'] ] : [];
+               Xhprof::enable( $flags, $options );
                $this->sprofiler = new SectionProfiler();
        }
 
+       /**
+        * @return XhprofData
+        */
+       public function getXhprofData() {
+               if ( !$this->xhprofData ) {
+                       $this->xhprofData = new XhprofData( Xhprof::disable(), $this->params );
+               }
+               return $this->xhprofData;
+       }
+
        public function scopedProfileIn( $section ) {
-               return $this->sprofiler->scopedProfileIn( $section );
+               $key = 'section.' . ltrim( $section, '.' );
+               return $this->sprofiler->scopedProfileIn( $key );
        }
 
        /**
@@ -86,14 +97,45 @@ class ProfilerXhprof extends Profiler {
        public function close() {
        }
 
+       /**
+        * Check if a function or section should be excluded from the output.
+        *
+        * @param string $name Function or section name.
+        * @return bool
+        */
+       private function shouldExclude( $name ) {
+               if ( $name === '-total' ) {
+                       return true;
+               }
+               if ( !empty( $this->params['include'] ) ) {
+                       foreach ( $this->params['include'] as $pattern ) {
+                               if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
+                                       return false;
+                               }
+                       }
+                       return true;
+               }
+               if ( !empty( $this->params['exclude'] ) ) {
+                       foreach ( $this->params['exclude'] as $pattern ) {
+                               if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
+                                       return true;
+                               }
+                       }
+               }
+               return false;
+       }
+
        public function getFunctionStats() {
-               $metrics = $this->xhprof->getCompleteMetrics();
-               $profile = array();
+               $metrics = $this->getXhprofData()->getCompleteMetrics();
+               $profile = [];
 
                $main = null; // units in ms
                foreach ( $metrics as $fname => $stats ) {
+                       if ( $this->shouldExclude( $fname ) ) {
+                               continue;
+                       }
                        // Convert elapsed times from μs to ms to match interface
-                       $entry = array(
+                       $entry = [
                                'name' => $fname,
                                'calls' => $stats['ct'],
                                'real' => $stats['wt']['total'] / 1000,
@@ -104,7 +146,7 @@ class ProfilerXhprof extends Profiler {
                                '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
                                'min_real' => $stats['wt']['min'] / 1000,
                                'max_real' => $stats['wt']['max'] / 1000
-                       );
+                       ];
                        $profile[] = $entry;
                        if ( $fname === 'main()' ) {
                                $main = $entry;
@@ -113,8 +155,7 @@ class ProfilerXhprof extends Profiler {
 
                // Merge in all of the custom profile sections
                foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
-                       if ( $stats['name'] === '-total' ) {
-                               // Discard section profiler running totals
+                       if ( $this->shouldExclude( $stats['name'] ) ) {
                                continue;
                        }
 
@@ -165,7 +206,7 @@ class ProfilerXhprof extends Profiler {
                $width = 140;
                $nameWidth = $width - 65;
                $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
-               $out = array();
+               $out = [];
                $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
                        'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
                );
@@ -189,6 +230,6 @@ class ProfilerXhprof extends Profiler {
         * @return array
         */
        public function getRawData() {
-               return $this->xhprof->getRawData();
+               return $this->getXhprofData()->getRawData();
        }
 }