Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  //
   3  //  Copyright (c) 2009 Facebook
   4  //
   5  //  Licensed under the Apache License, Version 2.0 (the "License");
   6  //  you may not use this file except in compliance with the License.
   7  //  You may obtain a copy of the License at
   8  //
   9  //      http://www.apache.org/licenses/LICENSE-2.0
  10  //
  11  //  Unless required by applicable law or agreed to in writing, software
  12  //  distributed under the License is distributed on an "AS IS" BASIS,
  13  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14  //  See the License for the specific language governing permissions and
  15  //  limitations under the License.
  16  //
  17  
  18  //
  19  // This file defines the interface iXHProfRuns and also provides a default
  20  // implementation of the interface (class XHProfRuns).
  21  //
  22  
  23  /**
  24   * iXHProfRuns interface for getting/saving a XHProf run.
  25   *
  26   * Clients can either use the default implementation,
  27   * namely XHProfRuns_Default, of this interface or define
  28   * their own implementation.
  29   *
  30   * @author Kannan
  31   */
  32  interface iXHProfRuns {
  33  
  34    /**
  35     * Returns XHProf data given a run id ($run) of a given
  36     * type ($type).
  37     *
  38     * Also, a brief description of the run is returned via the
  39     * $run_desc out parameter.
  40     */
  41    public function get_run($run_id, $type, &$run_desc);
  42  
  43    /**
  44     * Save XHProf data for a profiler run of specified type
  45     * ($type).
  46     *
  47     * The caller may optionally pass in run_id (which they
  48     * promise to be unique). If a run_id is not passed in,
  49     * the implementation of this method must generated a
  50     * unique run id for this saved XHProf run.
  51     *
  52     * Returns the run id for the saved XHProf run.
  53     *
  54     */
  55    public function save_run($xhprof_data, $type, $run_id = null);
  56  }
  57  
  58  
  59  /**
  60   * XHProfRuns_Default is the default implementation of the
  61   * iXHProfRuns interface for saving/fetching XHProf runs.
  62   *
  63   * It stores/retrieves runs to/from a filesystem directory
  64   * specified by the "xhprof.output_dir" ini parameter.
  65   *
  66   * @author Kannan
  67   */
  68  class XHProfRuns_Default implements iXHProfRuns {
  69  
  70    private $dir = '';
  71    private $suffix = 'xhprof';
  72  
  73    private function gen_run_id($type) {
  74      return uniqid();
  75    }
  76  
  77    private function file_name($run_id, $type) {
  78  
  79      $file = "$run_id.$type." . $this->suffix;
  80  
  81      if (!empty($this->dir)) {
  82        $file = $this->dir . "/" . $file;
  83      }
  84      return $file;
  85    }
  86  
  87    public function __construct($dir = null) {
  88  
  89      // if user hasn't passed a directory location,
  90      // we use the xhprof.output_dir ini setting
  91      // if specified, else we default to the directory
  92      // in which the error_log file resides.
  93  
  94      if (empty($dir)) {
  95        $dir = ini_get("xhprof.output_dir");
  96        if (empty($dir)) {
  97  
  98          $dir = sys_get_temp_dir();
  99  
 100          xhprof_error("Warning: Must specify directory location for XHProf runs. ".
 101                       "Trying {$dir} as default. You can either pass the " .
 102                       "directory location as an argument to the constructor ".
 103                       "for XHProfRuns_Default() or set xhprof.output_dir ".
 104                       "ini param.");
 105        }
 106      }
 107      $this->dir = $dir;
 108    }
 109  
 110    public function get_run($run_id, $type, &$run_desc) {
 111      $file_name = $this->file_name($run_id, $type);
 112  
 113      if (!file_exists($file_name)) {
 114        xhprof_error("Could not find file $file_name");
 115        $run_desc = "Invalid Run Id = $run_id";
 116        return null;
 117      }
 118  
 119      $contents = file_get_contents($file_name);
 120      $run_desc = "XHProf Run (Namespace=$type)";
 121      return unserialize($contents);
 122    }
 123  
 124    public function save_run($xhprof_data, $type, $run_id = null) {
 125  
 126      // Use PHP serialize function to store the XHProf's
 127      // raw profiler data.
 128      $xhprof_data = serialize($xhprof_data);
 129  
 130      if ($run_id === null) {
 131        $run_id = $this->gen_run_id($type);
 132      }
 133  
 134      $file_name = $this->file_name($run_id, $type);
 135      $file = fopen($file_name, 'w');
 136  
 137      if ($file) {
 138        fwrite($file, $xhprof_data);
 139        fclose($file);
 140      } else {
 141        xhprof_error("Could not open $file_name\n");
 142      }
 143  
 144      // echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n";
 145      return $run_id;
 146    }
 147  
 148    function list_runs() {
 149      if (is_dir($this->dir)) {
 150          echo "<hr/>Existing runs:\n<ul>\n";
 151          $files = glob("{$this->dir}/*.{$this->suffix}");
 152  	 	 usort($files, function($a, $b) {return filemtime($b) - filemtime($a);});
 153          foreach ($files as $file) {
 154              list($run,$source) = explode('.', basename($file));
 155              echo '<li><a href="' . htmlentities($_SERVER['SCRIPT_NAME'])
 156                  . '?run=' . htmlentities($run) . '&source='
 157                  . htmlentities($source) . '">'
 158                  . htmlentities(basename($file)) . "</a><small> "
 159                  . date("Y-m-d H:i:s", filemtime($file)) . "</small></li>\n";
 160          }
 161          echo "</ul>\n";
 162      }
 163    }
 164  }