xhprof_runs.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 function gen_run_id($type) {
  65. return uniqid();
  66. }
  67. private function file_name($run_id, $type) {
  68. $file = "$run_id.$type";
  69. if (!empty($this->dir)) {
  70. $file = $this->dir . "/" . $file;
  71. }
  72. return $file;
  73. }
  74. public function __construct($dir = null) {
  75. // if user hasn't passed a directory location,
  76. // we use the xhprof.output_dir ini setting
  77. // if specified, else we default to the directory
  78. // in which the error_log file resides.
  79. if (empty($dir)) {
  80. $dir = ini_get("xhprof.output_dir");
  81. if (empty($dir)) {
  82. // some default that at least works on unix...
  83. $dir = "/tmp";
  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. }