xhprof_runs.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // This file defines the interface iXHProfRuns and also provides a default
  19. // implementation of the interface (class XHProfRuns).
  20. //
  21. /**
  22. * iXHProfRuns interface for getting/saving a XHProf run.
  23. *
  24. * Clients can either use the default implementation,
  25. * namely XHProfRuns_Default, of this interface or define
  26. * their own implementation.
  27. *
  28. * @author Kannan
  29. */
  30. interface iXHProfRuns {
  31. /**
  32. * Returns XHProf data given a run id ($run) of a given
  33. * type ($type).
  34. *
  35. * Also, a brief description of the run is returned via the
  36. * $run_desc out parameter.
  37. */
  38. public function get_run($run_id, $type, &$run_desc);
  39. /**
  40. * Save XHProf data for a profiler run of specified type
  41. * ($type).
  42. *
  43. * The caller may optionally pass in run_id (which they
  44. * promise to be unique). If a run_id is not passed in,
  45. * the implementation of this method must generated a
  46. * unique run id for this saved XHProf run.
  47. *
  48. * Returns the run id for the saved XHProf run.
  49. *
  50. */
  51. public function save_run($xhprof_data, $type, $run_id = null);
  52. }
  53. /**
  54. * XHProfRuns_Default is the default implementation of the
  55. * iXHProfRuns interface for saving/fetching XHProf runs.
  56. *
  57. * It stores/retrieves runs to/from a filesystem directory
  58. * specified by the "xhprof.output_dir" ini parameter.
  59. *
  60. * @author Kannan
  61. */
  62. class XHProfRuns_Default implements iXHProfRuns {
  63. private $dir = '';
  64. private $suffix = 'xhprof';
  65. private function gen_run_id($type) {
  66. return uniqid();
  67. }
  68. private function file_name($run_id, $type) {
  69. $file = "$run_id.$type." . $this->suffix;
  70. if (!empty($this->dir)) {
  71. $file = $this->dir . "/" . $file;
  72. }
  73. return $file;
  74. }
  75. public function __construct($dir = null) {
  76. // if user hasn't passed a directory location,
  77. // we use the xhprof.output_dir ini setting
  78. // if specified, else we default to the directory
  79. // in which the error_log file resides.
  80. if (empty($dir)) {
  81. $dir = ini_get("xhprof.output_dir");
  82. if (empty($dir)) {
  83. $dir = sys_get_temp_dir();
  84. xhprof_error("Warning: Must specify directory location for XHProf runs. ".
  85. "Trying {$dir} as default. You can either pass the " .
  86. "directory location as an argument to the constructor ".
  87. "for XHProfRuns_Default() or set xhprof.output_dir ".
  88. "ini param.");
  89. }
  90. }
  91. $this->dir = $dir;
  92. }
  93. public function get_run($run_id, $type, &$run_desc) {
  94. $file_name = $this->file_name($run_id, $type);
  95. if (!file_exists($file_name)) {
  96. xhprof_error("Could not find file $file_name");
  97. $run_desc = "Invalid Run Id = $run_id";
  98. return null;
  99. }
  100. $contents = file_get_contents($file_name);
  101. $run_desc = "XHProf Run (Namespace=$type)";
  102. return unserialize($contents);
  103. }
  104. public function save_run($xhprof_data, $type, $run_id = null) {
  105. // Use PHP serialize function to store the XHProf's
  106. // raw profiler data.
  107. $xhprof_data = serialize($xhprof_data);
  108. if ($run_id === null) {
  109. $run_id = $this->gen_run_id($type);
  110. }
  111. $file_name = $this->file_name($run_id, $type);
  112. $file = fopen($file_name, 'w');
  113. if ($file) {
  114. fwrite($file, $xhprof_data);
  115. fclose($file);
  116. } else {
  117. xhprof_error("Could not open $file_name\n");
  118. }
  119. // echo "Saved run in {$file_name}.\nRun id = {$run_id}.\n";
  120. return $run_id;
  121. }
  122. function list_runs() {
  123. if (is_dir($this->dir)) {
  124. echo "<hr/>Existing runs:\n<ul>\n";
  125. $files = glob("{$this->dir}/*.{$this->suffix}");
  126. usort($files, create_function('$a,$b', 'return filemtime($b) - filemtime($a);'));
  127. foreach ($files as $file) {
  128. list($run,$source) = explode('.', basename($file));
  129. echo '<li><a href="' . htmlentities($_SERVER['SCRIPT_NAME'])
  130. . '?run=' . htmlentities($run) . '&source='
  131. . htmlentities($source) . '">'
  132. . htmlentities(basename($file)) . "</a><small> "
  133. . date("Y-m-d H:i:s", filemtime($file)) . "</small></li>\n";
  134. }
  135. echo "</ul>\n";
  136. }
  137. }
  138. }