2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 // Copyright (c) 2009 Facebook
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 // XHProf: A Hierarchical Profiler for PHP
21 // XHProf has two components:
23 // * This module is the UI/reporting component, used
24 // for viewing results of XHProf runs from a browser.
26 // * Data collection component: This is implemented
27 // as a PHP extension (XHProf).
29 // @author Kannan Muthukkaruppan
32 if (!isset($GLOBALS['XHPROF_LIB_ROOT'])) {
33 // by default, the parent directory is XHPROF lib root
34 $GLOBALS['XHPROF_LIB_ROOT'] = realpath(dirname(__FILE__) . '/..');
37 require_once $GLOBALS['XHPROF_LIB_ROOT'].'/utils/xhprof_lib.php';
38 require_once $GLOBALS['XHPROF_LIB_ROOT'].'/utils/callgraph_utils.php';
39 require_once $GLOBALS['XHPROF_LIB_ROOT'].'/utils/xhprof_runs.php';
43 * Our coding convention disallows relative paths in hrefs.
44 * Get the base URL path from the SCRIPT_NAME.
46 $base_path = rtrim(dirname($_SERVER['SCRIPT_NAME']), "/");
50 * Generate references to required stylesheets & javascript.
52 * If the calling script (such as index.php) resides in
53 * a different location that than 'xhprof_html' directory the
54 * caller must provide the URL path to 'xhprof_html' directory
55 * so that the correct location of the style sheets/javascript
56 * can be specified in the generated HTML.
59 function xhprof_include_js_css($ui_dir_url_path = null) {
61 if (empty($ui_dir_url_path)) {
62 $ui_dir_url_path = rtrim(dirname($_SERVER['SCRIPT_NAME']), "/");
66 echo "<link href='$ui_dir_url_path/css/xhprof.css' rel='stylesheet' ".
67 " type='text/css' />";
68 echo "<link href='$ui_dir_url_path/jquery/jquery.tooltip.css' ".
69 " rel='stylesheet' type='text/css' />";
70 echo "<link href='$ui_dir_url_path/jquery/jquery.autocomplete.css' ".
71 " rel='stylesheet' type='text/css' />";
74 echo "<script src='$ui_dir_url_path/jquery/jquery-1.2.6.js'>".
76 echo "<script src='$ui_dir_url_path/jquery/jquery.tooltip.js'>".
78 echo "<script src='$ui_dir_url_path/jquery/jquery.autocomplete.js'>"
80 echo "<script src='$ui_dir_url_path/js/xhprof_report.js'></script>";
85 * Formats call counts for XHProf reports.
88 * Call counts in single-run reports are integer values.
89 * However, call counts for aggregated reports can be
90 * fractional. This function will print integer values
91 * without decimal point, but with commas etc.
95 * It'll round fractional values to decimal precision of 3
96 * 4000.1212 ==> 4,000.121
100 function xhprof_count_format($num) {
101 $num = round($num, 3);
102 if (round($num) == $num) {
103 return number_format($num);
105 return number_format($num, 3);
109 function xhprof_percent_format($s, $precision = 1) {
110 return sprintf('%.'.$precision.'f%%', 100 * $s);
114 * Implodes the text for a bunch of actions (such as links, forms,
115 * into a HTML list and returns the text.
117 function xhprof_render_actions($actions) {
120 if (count($actions)) {
121 $out[] = '<ul class="xhprof_actions">';
122 foreach ($actions as $action) {
123 $out[] = '<li>'.$action.'</li>';
128 return implode('', $out);
133 * @param html-str $content the text/image/innerhtml/whatever for the link
134 * @param raw-str $href
135 * @param raw-str $class
137 * @param raw-str $title
138 * @param raw-str $target
139 * @param raw-str $onclick
140 * @param raw-str $style
141 * @param raw-str $access
142 * @param raw-str $onmouseover
143 * @param raw-str $onmouseout
144 * @param raw-str $onmousedown
145 * @param raw-str $dir
146 * @param raw-str $rel
148 function xhprof_render_link($content, $href, $class='', $id='', $title='',
150 $onclick='', $style='', $access='', $onmouseover='',
151 $onmouseout='', $onmousedown='') {
158 $link = '<a href="' . ($href) . '"';
164 $link .= ' class="' . ($class) . '"';
167 $link .= ' id="' . ($id) . '"';
170 $link .= ' title="' . ($title) . '"';
173 $link .= ' target="' . ($target) . '"';
175 if ($onclick && $href) {
176 $link .= ' onclick="' . ($onclick) . '"';
178 if ($style && $href) {
179 $link .= ' style="' . ($style) . '"';
181 if ($access && $href) {
182 $link .= ' accesskey="' . ($access) . '"';
185 $link .= ' onmouseover="' . ($onmouseover) . '"';
188 $link .= ' onmouseout="' . ($onmouseout) . '"';
191 $link .= ' onmousedown="' . ($onmousedown) . '"';
206 // default column to sort on -- wall time
209 // default is "single run" report
212 // call count data present?
213 $display_calls = true;
215 // The following column headers are sortable
216 $sortable_columns = array("fn" => 1,
234 // Textual descriptions for column headers in "single run" mode
235 $descriptions = array(
236 "fn" => "Function Name",
238 "Calls%" => "Calls%",
240 "wt" => "Incl. Wall Time<br>(microsec)",
241 "IWall%" => "IWall%",
242 "excl_wt" => "Excl. Wall Time<br>(microsec)",
243 "EWall%" => "EWall%",
245 "ut" => "Incl. User<br>(microsecs)",
246 "IUser%" => "IUser%",
247 "excl_ut" => "Excl. User<br>(microsec)",
248 "EUser%" => "EUser%",
250 "st" => "Incl. Sys <br>(microsec)",
252 "excl_st" => "Excl. Sys <br>(microsec)",
255 "cpu" => "Incl. CPU<br>(microsecs)",
257 "excl_cpu" => "Excl. CPU<br>(microsec)",
260 "mu" => "Incl.<br>MemUse<br>(bytes)",
261 "IMUse%" => "IMemUse%",
262 "excl_mu" => "Excl.<br>MemUse<br>(bytes)",
263 "EMUse%" => "EMemUse%",
265 "pmu" => "Incl.<br> PeakMemUse<br>(bytes)",
266 "IPMUse%" => "IPeakMemUse%",
267 "excl_pmu" => "Excl.<br>PeakMemUse<br>(bytes)",
268 "EPMUse%" => "EPeakMemUse%",
270 "samples" => "Incl. Samples",
271 "ISamples%" => "ISamples%",
272 "excl_samples" => "Excl. Samples",
273 "ESamples%" => "ESamples%",
276 // Formatting Callback Functions...
279 "ct" => "xhprof_count_format",
280 "Calls%" => "xhprof_percent_format",
282 "wt" => "number_format",
283 "IWall%" => "xhprof_percent_format",
284 "excl_wt" => "number_format",
285 "EWall%" => "xhprof_percent_format",
287 "ut" => "number_format",
288 "IUser%" => "xhprof_percent_format",
289 "excl_ut" => "number_format",
290 "EUser%" => "xhprof_percent_format",
292 "st" => "number_format",
293 "ISys%" => "xhprof_percent_format",
294 "excl_st" => "number_format",
295 "ESys%" => "xhprof_percent_format",
297 "cpu" => "number_format",
298 "ICpu%" => "xhprof_percent_format",
299 "excl_cpu" => "number_format",
300 "ECpu%" => "xhprof_percent_format",
302 "mu" => "number_format",
303 "IMUse%" => "xhprof_percent_format",
304 "excl_mu" => "number_format",
305 "EMUse%" => "xhprof_percent_format",
307 "pmu" => "number_format",
308 "IPMUse%" => "xhprof_percent_format",
309 "excl_pmu" => "number_format",
310 "EPMUse%" => "xhprof_percent_format",
312 "samples" => "number_format",
313 "ISamples%" => "xhprof_percent_format",
314 "excl_samples" => "number_format",
315 "ESamples%" => "xhprof_percent_format",
319 // Textual descriptions for column headers in "diff" mode
320 $diff_descriptions = array(
321 "fn" => "Function Name",
322 "ct" => "Calls Diff",
323 "Calls%" => "Calls<br>Diff%",
325 "wt" => "Incl. Wall<br>Diff<br>(microsec)",
326 "IWall%" => "IWall<br> Diff%",
327 "excl_wt" => "Excl. Wall<br>Diff<br>(microsec)",
328 "EWall%" => "EWall<br>Diff%",
330 "ut" => "Incl. User Diff<br>(microsec)",
331 "IUser%" => "IUser<br>Diff%",
332 "excl_ut" => "Excl. User<br>Diff<br>(microsec)",
333 "EUser%" => "EUser<br>Diff%",
335 "cpu" => "Incl. CPU Diff<br>(microsec)",
336 "ICpu%" => "ICpu<br>Diff%",
337 "excl_cpu" => "Excl. CPU<br>Diff<br>(microsec)",
338 "ECpu%" => "ECpu<br>Diff%",
340 "st" => "Incl. Sys Diff<br>(microsec)",
341 "ISys%" => "ISys<br>Diff%",
342 "excl_st" => "Excl. Sys Diff<br>(microsec)",
343 "ESys%" => "ESys<br>Diff%",
345 "mu" => "Incl.<br>MemUse<br>Diff<br>(bytes)",
346 "IMUse%" => "IMemUse<br>Diff%",
347 "excl_mu" => "Excl.<br>MemUse<br>Diff<br>(bytes)",
348 "EMUse%" => "EMemUse<br>Diff%",
350 "pmu" => "Incl.<br> PeakMemUse<br>Diff<br>(bytes)",
351 "IPMUse%" => "IPeakMemUse<br>Diff%",
352 "excl_pmu" => "Excl.<br>PeakMemUse<br>Diff<br>(bytes)",
353 "EPMUse%" => "EPeakMemUse<br>Diff%",
355 "samples" => "Incl. Samples Diff",
356 "ISamples%" => "ISamples Diff%",
357 "excl_samples" => "Excl. Samples Diff",
358 "ESamples%" => "ESamples Diff%",
361 // columns that'll be displayed in a top-level report
364 // columns that'll be displayed in a function's parent/child report
367 // Various total counts
373 * The subset of $possible_metrics that is present in the raw profile data.
378 * Callback comparison operator (passed to usort() for sorting array of
379 * tuples) that compares array elements based on the sort column
380 * specified in $sort_col (global parameter).
384 function sort_cbk($a, $b) {
388 if ($sort_col == "fn") {
390 // case insensitive ascending sort for function names
391 $left = strtoupper($a["fn"]);
392 $right = strtoupper($b["fn"]);
396 return ($left < $right) ? -1 : 1;
400 // descending sort for all others
401 $left = $a[$sort_col];
402 $right = $b[$sort_col];
404 // if diff mode, sort by absolute value of regression/improvement
407 $right = abs($right);
412 return ($left > $right) ? -1 : 1;
417 * Get the appropriate description for a statistic
418 * (depending upon whether we are in diff report mode
419 * or single run report mode).
423 function stat_description($stat) {
424 global $descriptions;
425 global $diff_descriptions;
429 return $diff_descriptions[$stat];
431 return $descriptions[$stat];
437 * Analyze raw data & generate the profiler report
438 * (common for both single run mode and diff mode).
442 function profiler_report ($url_params,
450 $run2_data = array()) {
459 // if we are reporting on a specific function, we can trim down
460 // the report(s) to just stuff that is relevant to this function.
461 // That way compute_flat_info()/compute_diff() etc. do not have
462 // to needlessly work hard on churning irrelevant data.
463 if (!empty($rep_symbol)) {
464 $run1_data = xhprof_trim_run($run1_data, array($rep_symbol));
466 $run2_data = xhprof_trim_run($run2_data, array($rep_symbol));
471 $run_delta = xhprof_compute_diff($run1_data, $run2_data);
472 $symbol_tab = xhprof_compute_flat_info($run_delta, $totals);
473 $symbol_tab1 = xhprof_compute_flat_info($run1_data, $totals_1);
474 $symbol_tab2 = xhprof_compute_flat_info($run2_data, $totals_2);
476 $symbol_tab = xhprof_compute_flat_info($run1_data, $totals);
479 $run1_txt = sprintf("<b>Run #%s:</b> %s",
482 $base_url_params = xhprof_array_unset(xhprof_array_unset($url_params,
486 $top_link_query_string = "$base_path/?" . http_build_query($base_url_params);
490 $base_url_params = xhprof_array_unset($base_url_params, 'run1');
491 $base_url_params = xhprof_array_unset($base_url_params, 'run2');
492 $run1_link = xhprof_render_link('View Run #' . $run1,
494 http_build_query(xhprof_array_set($base_url_params,
497 $run2_txt = sprintf("<b>Run #%s:</b> %s",
500 $run2_link = xhprof_render_link('View Run #' . $run2,
502 http_build_query(xhprof_array_set($base_url_params,
509 // set up the action links for operations that can be done on this report
511 $links [] = xhprof_render_link("View Top Level $diff_text Report",
512 $top_link_query_string);
515 $inverted_params = $url_params;
516 $inverted_params['run1'] = $url_params['run2'];
517 $inverted_params['run2'] = $url_params['run1'];
519 // view the different runs or invert the current diff
520 $links [] = $run1_link;
521 $links [] = $run2_link;
522 $links [] = xhprof_render_link('Invert ' . $diff_text . ' Report',
524 http_build_query($inverted_params));
527 // lookup function typeahead form
528 $links [] = '<input class="function_typeahead" ' .
529 ' type="input" size="40" maxlength="100" />';
531 echo xhprof_render_actions($links);
535 '<dl class=phprof_report_info>' .
536 ' <dt>' . $diff_text . ' Report</dt>' .
537 ' <dd>' . ($diff_mode ?
538 $run1_txt . '<br><b>vs.</b><br>' . $run2_txt :
542 ' <dd>Click a function name below to drill down.</dd>' .
544 '<div style="clear: both; margin: 3em 0em;"></div>';
547 if (!empty($rep_symbol)) {
548 if (!isset($symbol_tab[$rep_symbol])) {
549 echo "<hr>Symbol <b>$rep_symbol</b> not found in XHProf run</b><hr>";
553 /* single function report with parent/child information */
555 $info1 = isset($symbol_tab1[$rep_symbol]) ?
556 $symbol_tab1[$rep_symbol] : null;
557 $info2 = isset($symbol_tab2[$rep_symbol]) ?
558 $symbol_tab2[$rep_symbol] : null;
559 symbol_report($url_params, $run_delta, $symbol_tab[$rep_symbol],
564 symbol_report($url_params, $run1_data, $symbol_tab[$rep_symbol],
565 $sort, $rep_symbol, $run1);
568 /* flat top-level report of all functions */
569 full_report($url_params, $symbol_tab, $sort, $run1, $run2);
574 * Computes percentage for a pair of values, and returns it
577 function pct($a, $b) {
581 $res = (round(($a * 1000 / $b)) / 10);
587 * Given a number, returns the td class to use for display.
589 * For instance, negative numbers in diff reports comparing two runs (run1 & run2)
590 * represent improvement from run1 to run2. We use green to display those deltas,
591 * and red for regression deltas.
593 function get_print_class($num, $bold) {
603 $class = $vgbar; // green (improvement)
605 $class = $vrbar; // red (regression)
608 $class = $vbbar; // blue
612 $class = $vbar; // default (black)
619 * Prints a <td> element with a numeric value.
621 function print_td_num($num, $fmt_func, $bold=false, $attributes=null) {
623 $class = get_print_class($num, $bold);
625 if (!empty($fmt_func)) {
626 $num = call_user_func($fmt_func, $num);
629 print("<td $attributes $class>$num</td>\n");
633 * Prints a <td> element with a pecentage.
635 function print_td_pct($numer, $denom, $bold=false, $attributes=null) {
640 $class = get_print_class($numer, $bold);
645 $pct = xhprof_percent_format($numer / abs($denom));
648 print("<td $attributes $class>$pct</td>\n");
652 * Print "flat" data corresponding to one function.
656 function print_function_info($url_params, $info, $sort, $run1, $run2) {
657 static $odd_even = 0;
663 global $display_calls;
666 // Toggle $odd_or_even
667 $odd_even = 1 - $odd_even;
673 print('<tr bgcolor="#e5e5e5">');
676 $href = "$base_path/?" .
677 http_build_query(xhprof_array_set($url_params,
678 'symbol', $info["fn"]));
681 print(xhprof_render_link($info["fn"], $href));
682 print_source_link($info);
685 if ($display_calls) {
687 print_td_num($info["ct"], $format_cbk["ct"], ($sort_col == "ct"));
688 print_td_pct($info["ct"], $totals["ct"], ($sort_col == "ct"));
692 foreach ($metrics as $metric) {
694 print_td_num($info[$metric], $format_cbk[$metric],
695 ($sort_col == $metric));
696 print_td_pct($info[$metric], $totals[$metric],
697 ($sort_col == $metric));
700 print_td_num($info["excl_" . $metric],
701 $format_cbk["excl_" . $metric],
702 ($sort_col == "excl_" . $metric));
703 print_td_pct($info["excl_" . $metric],
705 ($sort_col == "excl_" . $metric));
712 * Print non-hierarchical (flat-view) of profiler data.
716 function print_flat_data($url_params, $title, $flat_data, $sort, $run1, $run2, $limit) {
719 global $sortable_columns;
723 $size = count($flat_data);
724 if (!$limit) { // no limit
728 $display_link = xhprof_render_link(" [ <b class=bubble>display all </b>]",
730 http_build_query(xhprof_array_set($url_params,
734 print("<h3 align=center>$title $display_link</h3><br>");
736 print('<table border=1 cellpadding=2 cellspacing=1 width="90%" '
737 .'rules=rows bordercolor="#bdc7d8" align=center>');
738 print('<tr bgcolor="#bdc7d8" align=right>');
740 foreach ($stats as $stat) {
741 $desc = stat_description($stat);
742 if (array_key_exists($stat, $sortable_columns)) {
743 $href = "$base_path/?"
744 . http_build_query(xhprof_array_set($url_params, 'sort', $stat));
745 $header = xhprof_render_link($desc, $href);
751 print("<th align=left><nobr>$header</th>");
752 else print("<th " . $vwbar . "><nobr>$header</th>");
757 $limit = min($size, $limit);
758 for ($i = 0; $i < $limit; $i++) {
759 print_function_info($url_params, $flat_data[$i], $sort, $run1, $run2);
762 // if $limit is negative, print abs($limit) items starting from the end
763 $limit = min($size, abs($limit));
764 for ($i = 0; $i < $limit; $i++) {
765 print_function_info($url_params, $flat_data[$size - $i - 1], $sort, $run1, $run2);
770 // let's print the display all link at the bottom as well...
772 echo '<div style="text-align: left; padding: 2em">' . $display_link . '</div>';
778 * Generates a tabular report for all functions. This is the top-level report.
782 function full_report($url_params, $symbol_tab, $sort, $run1, $run2) {
790 global $descriptions;
793 global $display_calls;
796 $possible_metrics = xhprof_get_possible_metrics();
800 $base_url_params = xhprof_array_unset(xhprof_array_unset($url_params,
803 $href1 = "$base_path/?" .
804 http_build_query(xhprof_array_set($base_url_params,
806 $href2 = "$base_path/?" .
807 http_build_query(xhprof_array_set($base_url_params,
810 print("<h3><center>Overall Diff Summary</center></h3>");
811 print('<table border=1 cellpadding=2 cellspacing=1 width="30%" '
812 .'rules=rows bordercolor="#bdc7d8" align=center>' . "\n");
813 print('<tr bgcolor="#bdc7d8" align=right>');
815 print("<th $vwbar>" . xhprof_render_link("Run #$run1", $href1) . "</th>");
816 print("<th $vwbar>" . xhprof_render_link("Run #$run2", $href2) . "</th>");
817 print("<th $vwbar>Diff</th>");
818 print("<th $vwbar>Diff%</th>");
821 if ($display_calls) {
823 print("<td>Number of Function Calls</td>");
824 print_td_num($totals_1["ct"], $format_cbk["ct"]);
825 print_td_num($totals_2["ct"], $format_cbk["ct"]);
826 print_td_num($totals_2["ct"] - $totals_1["ct"], $format_cbk["ct"], true);
827 print_td_pct($totals_2["ct"] - $totals_1["ct"], $totals_1["ct"], true);
831 foreach ($metrics as $metric) {
834 print("<td>" . str_replace("<br>", " ", $descriptions[$m]) . "</td>");
835 print_td_num($totals_1[$m], $format_cbk[$m]);
836 print_td_num($totals_2[$m], $format_cbk[$m]);
837 print_td_num($totals_2[$m] - $totals_1[$m], $format_cbk[$m], true);
838 print_td_pct($totals_2[$m] - $totals_1[$m], $totals_1[$m], true);
843 $callgraph_report_title = '[View Regressions/Improvements using Callgraph Diff]';
846 print("<p><center>\n");
848 print('<table cellpadding=2 cellspacing=1 width="30%" '
849 .'bgcolor="#bdc7d8" align=center>' . "\n");
851 echo "<th style='text-align:right'>Overall Summary</th>";
855 foreach ($metrics as $metric) {
857 echo "<td style='text-align:right; font-weight:bold'>Total "
858 . str_replace("<br>", " ", stat_description($metric)) . ":</td>";
859 echo "<td>" . number_format($totals[$metric]) . " "
860 . $possible_metrics[$metric][1] . "</td>";
864 if ($display_calls) {
866 echo "<td style='text-align:right; font-weight:bold'>Number of Function Calls:</td>";
867 echo "<td>" . number_format($totals['ct']) . "</td>";
872 print("</center></p>\n");
874 $callgraph_report_title = '[View Full Callgraph]';
877 print("<center><br><h3>" .
878 xhprof_render_link($callgraph_report_title,
879 "$base_path/callgraph.php" . "?" . http_build_query($url_params))
883 $flat_data = array();
884 foreach ($symbol_tab as $symbol => $info) {
886 $tmp["fn"] = $symbol;
889 usort($flat_data, 'sort_cbk');
893 if (!empty($url_params['all'])) {
895 $limit = 0; // display all rows
898 $limit = 100; // display only limited number of rows
901 $desc = str_replace("<br>", " ", $descriptions[$sort_col]);
905 $title = "Total Diff Report: '
906 .'Sorted by absolute value of regression/improvement in $desc";
908 $title = "Top 100 <i style='color:red'>Regressions</i>/"
909 . "<i style='color:green'>Improvements</i>: "
910 . "Sorted by $desc Diff";
914 $title = "Sorted by $desc";
916 $title = "Displaying top $limit functions: Sorted by $desc";
919 print_flat_data($url_params, $title, $flat_data, $sort, $run1, $run2, $limit);
924 * Return attribute names and values to be used by javascript tooltip.
926 function get_tooltip_attributes($type, $metric) {
927 return "type='$type' metric='$metric'";
931 * Print info for a parent or child function in the
932 * parent & children report.
936 function pc_info($info, $base_ct, $base_info, $parent) {
940 global $display_calls;
944 else $type = "Child";
946 if ($display_calls) {
947 $mouseoverct = get_tooltip_attributes($type, "ct");
949 print_td_num($info["ct"], $format_cbk["ct"], ($sort_col == "ct"), $mouseoverct);
950 print_td_pct($info["ct"], $base_ct, ($sort_col == "ct"), $mouseoverct);
953 /* Inclusive metric values */
954 foreach ($metrics as $metric) {
955 print_td_num($info[$metric], $format_cbk[$metric],
956 ($sort_col == $metric),
957 get_tooltip_attributes($type, $metric));
958 print_td_pct($info[$metric], $base_info[$metric], ($sort_col == $metric),
959 get_tooltip_attributes($type, $metric));
963 function print_pc_array($url_params, $results, $base_ct, $base_info, $parent,
967 // Construct section title
969 $title = 'Parent function';
972 $title = 'Child function';
974 if (count($results) > 1) {
978 print("<tr bgcolor='#e0e0ff'><td>");
979 print("<b><i><center>" . $title . "</center></i></b>");
983 foreach ($results as $info) {
984 $href = "$base_path/?" .
985 http_build_query(xhprof_array_set($url_params,
986 'symbol', $info["fn"]));
988 $odd_even = 1 - $odd_even;
994 print('<tr bgcolor="#e5e5e5">');
997 print("<td>" . xhprof_render_link($info["fn"], $href));
998 print_source_link($info);
1000 pc_info($info, $base_ct, $base_info, $parent);
1005 function print_source_link($info) {
1006 if (strncmp($info['fn'], 'run_init', 8) && $info['fn'] !== 'main()') {
1007 if (defined('XHPROF_SYMBOL_LOOKUP_URL')) {
1008 $link = xhprof_render_link(
1010 XHPROF_SYMBOL_LOOKUP_URL . '?symbol='.rawurlencode($info["fn"]));
1011 print(' ('.$link.')');
1017 function print_symbol_summary($symbol_info, $stat, $base) {
1019 $val = $symbol_info[$stat];
1020 $desc = str_replace("<br>", " ", stat_description($stat));
1022 print("$desc: </td>");
1023 print(number_format($val));
1024 print(" (" . pct($val, $base) . "% of overall)");
1025 if (substr($stat, 0, 4) == "excl") {
1026 $func_base = $symbol_info[str_replace("excl_", "", $stat)];
1027 print(" (" . pct($val, $func_base) . "% of this function)");
1033 * Generates a report for a single function/symbol.
1037 function symbol_report($url_params,
1038 $run_data, $symbol_info, $sort, $rep_symbol,
1040 $symbol_info1 = null,
1042 $symbol_info2 = null) {
1047 global $sortable_columns;
1050 global $descriptions;
1053 global $display_calls;
1056 $possible_metrics = xhprof_get_possible_metrics();
1059 $diff_text = "<b>Diff</b>";
1060 $regr_impr = "<i style='color:red'>Regression</i>/<i style='color:green'>Improvement</i>";
1068 $base_url_params = xhprof_array_unset(xhprof_array_unset($url_params,
1071 $href1 = "$base_path?"
1072 . http_build_query(xhprof_array_set($base_url_params, 'run', $run1));
1073 $href2 = "$base_path?"
1074 . http_build_query(xhprof_array_set($base_url_params, 'run', $run2));
1076 print("<h3 align=center>$regr_impr summary for $rep_symbol<br><br></h3>");
1077 print('<table border=1 cellpadding=2 cellspacing=1 width="30%" '
1078 .'rules=rows bordercolor="#bdc7d8" align=center>' . "\n");
1079 print('<tr bgcolor="#bdc7d8" align=right>');
1080 print("<th align=left>$rep_symbol</th>");
1081 print("<th $vwbar><a href=" . $href1 . ">Run #$run1</a></th>");
1082 print("<th $vwbar><a href=" . $href2 . ">Run #$run2</a></th>");
1083 print("<th $vwbar>Diff</th>");
1084 print("<th $vwbar>Diff%</th>");
1088 if ($display_calls) {
1089 print("<td>Number of Function Calls</td>");
1090 print_td_num($symbol_info1["ct"], $format_cbk["ct"]);
1091 print_td_num($symbol_info2["ct"], $format_cbk["ct"]);
1092 print_td_num($symbol_info2["ct"] - $symbol_info1["ct"],
1093 $format_cbk["ct"], true);
1094 print_td_pct($symbol_info2["ct"] - $symbol_info1["ct"],
1095 $symbol_info1["ct"], true);
1100 foreach ($metrics as $metric) {
1103 // Inclusive stat for metric
1105 print("<td>" . str_replace("<br>", " ", $descriptions[$m]) . "</td>");
1106 print_td_num($symbol_info1[$m], $format_cbk[$m]);
1107 print_td_num($symbol_info2[$m], $format_cbk[$m]);
1108 print_td_num($symbol_info2[$m] - $symbol_info1[$m], $format_cbk[$m], true);
1109 print_td_pct($symbol_info2[$m] - $symbol_info1[$m], $symbol_info1[$m], true);
1112 // AVG (per call) Inclusive stat for metric
1114 print("<td>" . str_replace("<br>", " ", $descriptions[$m]) . " per call </td>");
1117 if ($symbol_info1['ct'] > 0) {
1118 $avg_info1 = ($symbol_info1[$m] / $symbol_info1['ct']);
1120 if ($symbol_info2['ct'] > 0) {
1121 $avg_info2 = ($symbol_info2[$m] / $symbol_info2['ct']);
1123 print_td_num($avg_info1, $format_cbk[$m]);
1124 print_td_num($avg_info2, $format_cbk[$m]);
1125 print_td_num($avg_info2 - $avg_info1, $format_cbk[$m], true);
1126 print_td_pct($avg_info2 - $avg_info1, $avg_info1, true);
1129 // Exclusive stat for metric
1130 $m = "excl_" . $metric;
1131 print('<tr style="border-bottom: 1px solid black;">');
1132 print("<td>" . str_replace("<br>", " ", $descriptions[$m]) . "</td>");
1133 print_td_num($symbol_info1[$m], $format_cbk[$m]);
1134 print_td_num($symbol_info2[$m], $format_cbk[$m]);
1135 print_td_num($symbol_info2[$m] - $symbol_info1[$m], $format_cbk[$m], true);
1136 print_td_pct($symbol_info2[$m] - $symbol_info1[$m], $symbol_info1[$m], true);
1143 print("<br><h4><center>");
1144 print("Parent/Child $regr_impr report for <b>$rep_symbol</b>");
1146 $callgraph_href = "$base_path/callgraph.php?"
1147 . http_build_query(xhprof_array_set($url_params, 'func', $rep_symbol));
1149 print(" <a href='$callgraph_href'>[View Callgraph $diff_text]</a><br>");
1151 print("</center></h4><br>");
1153 print('<table border=1 cellpadding=2 cellspacing=1 width="90%" '
1154 .'rules=rows bordercolor="#bdc7d8" align=center>' . "\n");
1155 print('<tr bgcolor="#bdc7d8" align=right>');
1157 foreach ($pc_stats as $stat) {
1158 $desc = stat_description($stat);
1159 if (array_key_exists($stat, $sortable_columns)) {
1161 $href = "$base_path/?" .
1162 http_build_query(xhprof_array_set($url_params,
1164 $header = xhprof_render_link($desc, $href);
1170 print("<th align=left><nobr>$header</th>");
1171 else print("<th " . $vwbar . "><nobr>$header</th>");
1175 print("<tr bgcolor='#e0e0ff'><td>");
1176 print("<b><i><center>Current Function</center></i></b>");
1177 print("</td></tr>");
1180 // make this a self-reference to facilitate copy-pasting snippets to e-mails
1181 print("<td><a href=''>$rep_symbol</a>");
1182 print_source_link(array('fn' => $rep_symbol));
1185 if ($display_calls) {
1187 print_td_num($symbol_info["ct"], $format_cbk["ct"]);
1188 print_td_pct($symbol_info["ct"], $totals["ct"]);
1191 // Inclusive Metrics for current function
1192 foreach ($metrics as $metric) {
1193 print_td_num($symbol_info[$metric], $format_cbk[$metric], ($sort_col == $metric));
1194 print_td_pct($symbol_info[$metric], $totals[$metric], ($sort_col == $metric));
1198 print("<tr bgcolor='#ffffff'>");
1199 print("<td style='text-align:right;color:blue'>"
1200 ."Exclusive Metrics $diff_text for Current Function</td>");
1202 if ($display_calls) {
1204 print("<td $vbar></td>");
1205 print("<td $vbar></td>");
1208 // Exclusive Metrics for current function
1209 foreach ($metrics as $metric) {
1210 print_td_num($symbol_info["excl_" . $metric], $format_cbk["excl_" . $metric],
1211 ($sort_col == $metric),
1212 get_tooltip_attributes("Child", $metric));
1213 print_td_pct($symbol_info["excl_" . $metric], $symbol_info[$metric],
1214 ($sort_col == $metric),
1215 get_tooltip_attributes("Child", $metric));
1219 // list of callers/parent functions
1221 if ($display_calls) {
1222 $base_ct = $symbol_info["ct"];
1226 foreach ($metrics as $metric) {
1227 $base_info[$metric] = $symbol_info[$metric];
1229 foreach ($run_data as $parent_child => $info) {
1230 list($parent, $child) = xhprof_parse_parent_child($parent_child);
1231 if (($child == $rep_symbol) && ($parent)) {
1233 $info_tmp["fn"] = $parent;
1234 $results[] = $info_tmp;
1237 usort($results, 'sort_cbk');
1239 if (count($results) > 0) {
1240 print_pc_array($url_params, $results, $base_ct, $base_info, true,
1244 // list of callees/child functions
1247 foreach ($run_data as $parent_child => $info) {
1248 list($parent, $child) = xhprof_parse_parent_child($parent_child);
1249 if ($parent == $rep_symbol) {
1251 $info_tmp["fn"] = $child;
1252 $results[] = $info_tmp;
1253 if ($display_calls) {
1254 $base_ct += $info["ct"];
1258 usort($results, 'sort_cbk');
1260 if (count($results)) {
1261 print_pc_array($url_params, $results, $base_ct, $base_info, false,
1267 // These will be used for pop-up tips/help.
1268 // Related javascript code is in: xhprof_report.js
1270 print('<script language="javascript">' . "\n");
1271 print("var func_name = '\"" . $rep_symbol . "\"';\n");
1272 print("var total_child_ct = " . $base_ct . ";\n");
1273 if ($display_calls) {
1274 print("var func_ct = " . $symbol_info["ct"] . ";\n");
1276 print("var func_metrics = new Array();\n");
1277 print("var metrics_col = new Array();\n");
1278 print("var metrics_desc = new Array();\n");
1280 print("var diff_mode = true;\n");
1282 print("var diff_mode = false;\n");
1284 $column_index = 3; // First three columns are Func Name, Calls, Calls%
1285 foreach ($metrics as $metric) {
1286 print("func_metrics[\"" . $metric . "\"] = " . round($symbol_info[$metric]) . ";\n");
1287 print("metrics_col[\"". $metric . "\"] = " . $column_index . ";\n");
1288 print("metrics_desc[\"". $metric . "\"] = \"" . $possible_metrics[$metric][2] . "\";\n");
1290 // each metric has two columns..
1299 * Generate the profiler report for a single run.
1303 function profiler_single_run_report ($url_params,
1310 init_metrics($xhprof_data, $rep_symbol, $sort, false);
1312 profiler_report($url_params, $rep_symbol, $sort, $run, $run_desc,
1319 * Generate the profiler report for diff mode (delta between two runs).
1323 function profiler_diff_report($url_params,
1334 // Initialize what metrics we'll display based on data in Run2
1335 init_metrics($xhprof_data2, $rep_symbol, $sort, true);
1337 profiler_report($url_params,
1350 * Generate a XHProf Display View given the various URL parameters
1351 * as arguments. The first argument is an object that implements
1352 * the iXHProfRuns interface.
1354 * @param object $xhprof_runs_impl An object that implements
1355 * the iXHProfRuns interface
1357 * @param array $url_params Array of non-default URL params.
1359 * @param string $source Category/type of the run. The source in
1360 * combination with the run id uniquely
1361 * determines a profiler run.
1363 * @param string $run run id, or comma separated sequence of
1364 * run ids. The latter is used if an aggregate
1365 * report of the runs is desired.
1367 * @param string $wts Comma separate list of integers.
1368 * Represents the weighted ratio in
1369 * which which a set of runs will be
1370 * aggregated. [Used only for aggregate
1373 * @param string $symbol Function symbol. If non-empty then the
1374 * parent/child view of this function is
1375 * displayed. If empty, a flat-profile view
1376 * of the functions is displayed.
1378 * @param string $run1 Base run id (for diff reports)
1380 * @param string $run2 New run id (for diff reports)
1383 function displayXHProfReport($xhprof_runs_impl, $url_params, $source,
1384 $run, $wts, $symbol, $sort, $run1, $run2) {
1386 if ($run) { // specific run to display?
1388 // run may be a single run or a comma separate list of runs
1389 // that'll be aggregated. If "wts" (a comma separated list
1390 // of integral weights is specified), the runs will be
1391 // aggregated in that ratio.
1393 $runs_array = explode(",", $run);
1395 if (count($runs_array) == 1) {
1396 $xhprof_data = $xhprof_runs_impl->get_run($runs_array[0],
1401 $wts_array = explode(",", $wts);
1405 $data = xhprof_aggregate_runs($xhprof_runs_impl,
1406 $runs_array, $wts_array, $source, false);
1407 $xhprof_data = $data['raw'];
1408 $description = $data['description'];
1412 profiler_single_run_report($url_params,
1419 } else if ($run1 && $run2) { // diff report for two runs
1421 $xhprof_data1 = $xhprof_runs_impl->get_run($run1, $source, $description1);
1422 $xhprof_data2 = $xhprof_runs_impl->get_run($run2, $source, $description2);
1424 profiler_diff_report($url_params,
1435 echo "No XHProf runs specified in the URL.";
1436 if (method_exists($xhprof_runs_impl, 'list_runs')) {
1437 $xhprof_runs_impl->list_runs();