ProfilerXhprof.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. /**
  21. * Profiler wrapper for XHProf extension.
  22. *
  23. * @code
  24. * $wgProfiler['class'] = ProfilerXhprof::class;
  25. * $wgProfiler['flags'] = XHPROF_FLAGS_NO_BUILTINS;
  26. * $wgProfiler['output'] = 'text';
  27. * $wgProfiler['visible'] = true;
  28. * @endcode
  29. *
  30. * @code
  31. * $wgProfiler['class'] = ProfilerXhprof::class;
  32. * $wgProfiler['flags'] = XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_NO_BUILTINS;
  33. * $wgProfiler['output'] = 'udp';
  34. * @endcode
  35. *
  36. * ProfilerXhprof profiles all functions using the XHProf PHP extenstion.
  37. * For PHP5 users, this extension can be installed via PECL or your operating
  38. * system's package manager. XHProf support is built into HHVM.
  39. *
  40. * To restrict the functions for which profiling data is collected, you can
  41. * use either a whitelist ($wgProfiler['include']) or a blacklist
  42. * ($wgProfiler['exclude']) containing an array of function names.
  43. * Shell-style patterns are also accepted.
  44. *
  45. * It is also possible to use the Tideways PHP extension, which is mostly
  46. * a drop-in replacement for Xhprof. Just change the XHPROF_FLAGS_* constants
  47. * to TIDEWAYS_FLAGS_*.
  48. *
  49. * @copyright © 2014 Wikimedia Foundation and contributors
  50. * @ingroup Profiler
  51. * @see Xhprof
  52. * @see https://php.net/xhprof
  53. * @see https://github.com/facebook/hhvm/blob/master/hphp/doc/profiling.md
  54. * @see https://github.com/tideways/php-profiler-extension
  55. */
  56. class ProfilerXhprof extends Profiler {
  57. /**
  58. * @var XhprofData|null $xhprofData
  59. */
  60. protected $xhprofData;
  61. /**
  62. * Profiler for explicit, arbitrary, frame labels
  63. * @var SectionProfiler
  64. */
  65. protected $sprofiler;
  66. /**
  67. * @param array $params
  68. * @see Xhprof::__construct()
  69. */
  70. public function __construct( array $params = [] ) {
  71. parent::__construct( $params );
  72. $flags = isset( $params['flags'] ) ? $params['flags'] : 0;
  73. $options = isset( $params['exclude'] )
  74. ? [ 'ignored_functions' => $params['exclude'] ] : [];
  75. Xhprof::enable( $flags, $options );
  76. $this->sprofiler = new SectionProfiler();
  77. }
  78. /**
  79. * @return XhprofData
  80. */
  81. public function getXhprofData() {
  82. if ( !$this->xhprofData ) {
  83. $this->xhprofData = new XhprofData( Xhprof::disable(), $this->params );
  84. }
  85. return $this->xhprofData;
  86. }
  87. public function scopedProfileIn( $section ) {
  88. $key = 'section.' . ltrim( $section, '.' );
  89. return $this->sprofiler->scopedProfileIn( $key );
  90. }
  91. /**
  92. * No-op for xhprof profiling.
  93. */
  94. public function close() {
  95. }
  96. /**
  97. * Check if a function or section should be excluded from the output.
  98. *
  99. * @param string $name Function or section name.
  100. * @return bool
  101. */
  102. private function shouldExclude( $name ) {
  103. if ( $name === '-total' ) {
  104. return true;
  105. }
  106. if ( !empty( $this->params['include'] ) ) {
  107. foreach ( $this->params['include'] as $pattern ) {
  108. if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. if ( !empty( $this->params['exclude'] ) ) {
  115. foreach ( $this->params['exclude'] as $pattern ) {
  116. if ( fnmatch( $pattern, $name, FNM_NOESCAPE ) ) {
  117. return true;
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. public function getFunctionStats() {
  124. $metrics = $this->getXhprofData()->getCompleteMetrics();
  125. $profile = [];
  126. $main = null; // units in ms
  127. foreach ( $metrics as $fname => $stats ) {
  128. if ( $this->shouldExclude( $fname ) ) {
  129. continue;
  130. }
  131. // Convert elapsed times from μs to ms to match interface
  132. $entry = [
  133. 'name' => $fname,
  134. 'calls' => $stats['ct'],
  135. 'real' => $stats['wt']['total'] / 1000,
  136. '%real' => $stats['wt']['percent'],
  137. 'cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['total'] / 1000 : 0,
  138. '%cpu' => isset( $stats['cpu'] ) ? $stats['cpu']['percent'] : 0,
  139. 'memory' => isset( $stats['mu'] ) ? $stats['mu']['total'] : 0,
  140. '%memory' => isset( $stats['mu'] ) ? $stats['mu']['percent'] : 0,
  141. 'min_real' => $stats['wt']['min'] / 1000,
  142. 'max_real' => $stats['wt']['max'] / 1000
  143. ];
  144. $profile[] = $entry;
  145. if ( $fname === 'main()' ) {
  146. $main = $entry;
  147. }
  148. }
  149. // Merge in all of the custom profile sections
  150. foreach ( $this->sprofiler->getFunctionStats() as $stats ) {
  151. if ( $this->shouldExclude( $stats['name'] ) ) {
  152. continue;
  153. }
  154. // @note: getFunctionStats() values already in ms
  155. $stats['%real'] = $main['real'] ? $stats['real'] / $main['real'] * 100 : 0;
  156. $stats['%cpu'] = $main['cpu'] ? $stats['cpu'] / $main['cpu'] * 100 : 0;
  157. $stats['%memory'] = $main['memory'] ? $stats['memory'] / $main['memory'] * 100 : 0;
  158. $profile[] = $stats; // assume no section names collide with $metrics
  159. }
  160. return $profile;
  161. }
  162. /**
  163. * Returns a profiling output to be stored in debug file
  164. *
  165. * @return string
  166. */
  167. public function getOutput() {
  168. return $this->getFunctionReport();
  169. }
  170. /**
  171. * Get a report of profiled functions sorted by inclusive wall clock time
  172. * in descending order.
  173. *
  174. * Each line of the report includes this data:
  175. * - Function name
  176. * - Number of times function was called
  177. * - Total wall clock time spent in function in microseconds
  178. * - Minimum wall clock time spent in function in microseconds
  179. * - Average wall clock time spent in function in microseconds
  180. * - Maximum wall clock time spent in function in microseconds
  181. * - Percentage of total wall clock time spent in function
  182. * - Total delta of memory usage from start to end of function in bytes
  183. *
  184. * @return string
  185. */
  186. protected function getFunctionReport() {
  187. $data = $this->getFunctionStats();
  188. usort( $data, function ( $a, $b ) {
  189. if ( $a['real'] === $b['real'] ) {
  190. return 0;
  191. }
  192. return ( $a['real'] > $b['real'] ) ? -1 : 1; // descending
  193. } );
  194. $width = 140;
  195. $nameWidth = $width - 65;
  196. $format = "%-{$nameWidth}s %6d %9d %9d %9d %9d %7.3f%% %9d";
  197. $out = [];
  198. $out[] = sprintf( "%-{$nameWidth}s %6s %9s %9s %9s %9s %7s %9s",
  199. 'Name', 'Calls', 'Total', 'Min', 'Each', 'Max', '%', 'Mem'
  200. );
  201. foreach ( $data as $stats ) {
  202. $out[] = sprintf( $format,
  203. $stats['name'],
  204. $stats['calls'],
  205. $stats['real'] * 1000,
  206. $stats['min_real'] * 1000,
  207. $stats['real'] / $stats['calls'] * 1000,
  208. $stats['max_real'] * 1000,
  209. $stats['%real'],
  210. $stats['memory']
  211. );
  212. }
  213. return implode( "\n", $out );
  214. }
  215. /**
  216. * Retrieve raw data from xhprof
  217. * @return array
  218. */
  219. public function getRawData() {
  220. return $this->getXhprofData()->getRawData();
  221. }
  222. }