123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945 |
- <?php
- function xhprof_error($message) {
- error_log($message);
- }
- function xhprof_get_possible_metrics() {
- static $possible_metrics =
- array("wt" => array("Wall", "microsecs", "walltime"),
- "ut" => array("User", "microsecs", "user cpu time"),
- "st" => array("Sys", "microsecs", "system cpu time"),
- "cpu" => array("Cpu", "microsecs", "cpu time"),
- "mu" => array("MUse", "bytes", "memory usage"),
- "pmu" => array("PMUse", "bytes", "peak memory usage"),
- "samples" => array("Samples", "samples", "cpu time"));
- return $possible_metrics;
- }
- function init_metrics($xhprof_data, $rep_symbol, $sort, $diff_report = false) {
- global $stats;
- global $pc_stats;
- global $metrics;
- global $diff_mode;
- global $sortable_columns;
- global $sort_col;
- global $display_calls;
- $diff_mode = $diff_report;
- if (!empty($sort)) {
- if (array_key_exists($sort, $sortable_columns)) {
- $sort_col = $sort;
- } else {
- print("Invalid Sort Key $sort specified in URL");
- }
- }
-
-
- if (!isset($xhprof_data["main()"]["wt"])) {
- if ($sort_col == "wt") {
- $sort_col = "samples";
- }
-
-
-
-
-
- $display_calls = false;
- } else {
- $display_calls = true;
- }
-
-
- if (!empty($rep_symbol)) {
- $sort_col = str_replace("excl_", "", $sort_col);
- }
- if ($display_calls) {
- $stats = array("fn", "ct", "Calls%");
- } else {
- $stats = array("fn");
- }
- $pc_stats = $stats;
- $possible_metrics = xhprof_get_possible_metrics();
- foreach ($possible_metrics as $metric => $desc) {
- if (isset($xhprof_data["main()"][$metric])) {
- $metrics[] = $metric;
-
-
- $stats[] = $metric;
- $stats[] = "I" . $desc[0] . "%";
- $stats[] = "excl_" . $metric;
- $stats[] = "E" . $desc[0] . "%";
-
-
- $pc_stats[] = $metric;
- $pc_stats[] = "I" . $desc[0] . "%";
- }
- }
- }
- function xhprof_get_metrics($xhprof_data) {
-
- $possible_metrics = xhprof_get_possible_metrics();
-
-
- $metrics = array();
- foreach ($possible_metrics as $metric => $desc) {
- if (isset($xhprof_data["main()"][$metric])) {
- $metrics[] = $metric;
- }
- }
- return $metrics;
- }
- function xhprof_parse_parent_child($parent_child) {
- $ret = explode("==>", $parent_child);
-
- if (isset($ret[1])) {
- return $ret;
- }
- return array(null, $ret[0]);
- }
- function xhprof_build_parent_child_key($parent, $child) {
- if ($parent) {
- return $parent . "==>" . $child;
- } else {
- return $child;
- }
- }
- function xhprof_valid_run($run_id, $raw_data) {
- $main_info = $raw_data["main()"];
- if (empty($main_info)) {
- xhprof_error("XHProf: main() missing in raw data for Run ID: $run_id");
- return false;
- }
-
- if (isset($main_info["wt"])) {
- $metric = "wt";
- } else if (isset($main_info["samples"])) {
- $metric = "samples";
- } else {
- xhprof_error("XHProf: Wall Time information missing from Run ID: $run_id");
- return false;
- }
- foreach ($raw_data as $info) {
- $val = $info[$metric];
-
- if ($val < 0) {
- xhprof_error("XHProf: $metric should not be negative: Run ID $run_id"
- . serialize($info));
- return false;
- }
- if ($val > (86400000000)) {
- xhprof_error("XHProf: $metric > 1 day found in Run ID: $run_id "
- . serialize($info));
- return false;
- }
- }
- return true;
- }
- function xhprof_trim_run($raw_data, $functions_to_keep) {
-
- $function_map = array_fill_keys($functions_to_keep, 1);
-
-
- $function_map['main()'] = 1;
- $new_raw_data = array();
- foreach ($raw_data as $parent_child => $info) {
- list($parent, $child) = xhprof_parse_parent_child($parent_child);
- if (isset($function_map[$parent]) || isset($function_map[$child])) {
- $new_raw_data[$parent_child] = $info;
- }
- }
- return $new_raw_data;
- }
- function xhprof_normalize_metrics($raw_data, $num_runs) {
- if (empty($raw_data) || ($num_runs == 0)) {
- return $raw_data;
- }
- $raw_data_total = array();
- if (isset($raw_data["==>main()"]) && isset($raw_data["main()"])) {
- xhprof_error("XHProf Error: both ==>main() and main() set in raw data...");
- }
- foreach ($raw_data as $parent_child => $info) {
- foreach ($info as $metric => $value) {
- $raw_data_total[$parent_child][$metric] = ($value / $num_runs);
- }
- }
- return $raw_data_total;
- }
- function xhprof_aggregate_runs($xhprof_runs_impl, $runs,
- $wts, $source="phprof",
- $use_script_name=false) {
- $raw_data_total = null;
- $raw_data = null;
- $metrics = array();
- $run_count = count($runs);
- $wts_count = count($wts);
- if (($run_count == 0) ||
- (($wts_count > 0) && ($run_count != $wts_count))) {
- return array('description' => 'Invalid input..',
- 'raw' => null);
- }
- $bad_runs = array();
- foreach ($runs as $idx => $run_id) {
- $raw_data = $xhprof_runs_impl->get_run($run_id, $source, $description);
-
- if ($idx == 0) {
- foreach ($raw_data["main()"] as $metric => $val) {
- if ($metric != "pmu") {
-
-
-
- if (isset($val)) {
- $metrics[] = $metric;
- }
- }
- }
- }
- if (!xhprof_valid_run($run_id, $raw_data)) {
- $bad_runs[] = $run_id;
- continue;
- }
- if ($use_script_name) {
- $page = $description;
-
-
-
-
-
-
-
-
- if ($page) {
- foreach ($raw_data["main()"] as $metric => $val) {
- $fake_edge[$metric] = $val;
- $new_main[$metric] = $val + 0.00001;
- }
- $raw_data["main()"] = $new_main;
- $raw_data[xhprof_build_parent_child_key("main()",
- "__script::$page")]
- = $fake_edge;
- } else {
- $use_script_name = false;
- }
- }
-
- $wt = ($wts_count == 0) ? 1 : $wts[$idx];
-
- foreach ($raw_data as $parent_child => $info) {
- if ($use_script_name) {
-
-
- if (substr($parent_child, 0, 9) == "main()==>") {
- $child = substr($parent_child, 9);
-
- if (substr($child, 0, 10) != "__script::") {
- $parent_child = xhprof_build_parent_child_key("__script::$page",
- $child);
- }
- }
- }
- if (!isset($raw_data_total[$parent_child])) {
- foreach ($metrics as $metric) {
- $raw_data_total[$parent_child][$metric] = ($wt * $info[$metric]);
- }
- } else {
- foreach ($metrics as $metric) {
- $raw_data_total[$parent_child][$metric] += ($wt * $info[$metric]);
- }
- }
- }
- }
- $runs_string = implode(",", $runs);
- if (isset($wts)) {
- $wts_string = "in the ratio (" . implode(":", $wts) . ")";
- $normalization_count = array_sum($wts);
- } else {
- $wts_string = "";
- $normalization_count = $run_count;
- }
- $run_count = $run_count - count($bad_runs);
- $data['description'] = "Aggregated Report for $run_count runs: ".
- "$runs_string $wts_string\n";
- $data['raw'] = xhprof_normalize_metrics($raw_data_total,
- $normalization_count);
- $data['bad_runs'] = $bad_runs;
- return $data;
- }
- function xhprof_compute_flat_info($raw_data, &$overall_totals) {
- global $display_calls;
- $metrics = xhprof_get_metrics($raw_data);
- $overall_totals = array("ct" => 0,
- "wt" => 0,
- "ut" => 0,
- "st" => 0,
- "cpu" => 0,
- "mu" => 0,
- "pmu" => 0,
- "samples" => 0
- );
-
- $symbol_tab = xhprof_compute_inclusive_times($raw_data);
-
- foreach ($metrics as $metric) {
- $overall_totals[$metric] = $symbol_tab["main()"][$metric];
- }
-
- foreach ($symbol_tab as $symbol => $info) {
- foreach ($metrics as $metric) {
- $symbol_tab[$symbol]["excl_" . $metric] = $symbol_tab[$symbol][$metric];
- }
- if ($display_calls) {
-
- $overall_totals["ct"] += $info["ct"];
- }
- }
-
- foreach ($raw_data as $parent_child => $info) {
- list($parent, $child) = xhprof_parse_parent_child($parent_child);
- if ($parent) {
- foreach ($metrics as $metric) {
-
- if (isset($symbol_tab[$parent])) {
- $symbol_tab[$parent]["excl_" . $metric] -= $info[$metric];
- }
- }
- }
- }
- return $symbol_tab;
- }
- function xhprof_compute_diff($xhprof_data1, $xhprof_data2) {
- global $display_calls;
-
- $metrics = xhprof_get_metrics($xhprof_data2);
- $xhprof_delta = $xhprof_data2;
- foreach ($xhprof_data1 as $parent_child => $info) {
- if (!isset($xhprof_delta[$parent_child])) {
-
-
- if ($display_calls) {
- $xhprof_delta[$parent_child] = array("ct" => 0);
- } else {
- $xhprof_delta[$parent_child] = array();
- }
- foreach ($metrics as $metric) {
- $xhprof_delta[$parent_child][$metric] = 0;
- }
- }
- if ($display_calls) {
- $xhprof_delta[$parent_child]["ct"] -= $info["ct"];
- }
- foreach ($metrics as $metric) {
- $xhprof_delta[$parent_child][$metric] -= $info[$metric];
- }
- }
- return $xhprof_delta;
- }
- function xhprof_compute_inclusive_times($raw_data) {
- global $display_calls;
- $metrics = xhprof_get_metrics($raw_data);
- $symbol_tab = array();
-
- foreach ($raw_data as $parent_child => $info) {
- list($parent, $child) = xhprof_parse_parent_child($parent_child);
- if ($parent == $child) {
-
- xhprof_error("Error in Raw Data: parent & child are both: $parent");
- return;
- }
- if (!isset($symbol_tab[$child])) {
- if ($display_calls) {
- $symbol_tab[$child] = array("ct" => $info["ct"]);
- } else {
- $symbol_tab[$child] = array();
- }
- foreach ($metrics as $metric) {
- $symbol_tab[$child][$metric] = $info[$metric];
- }
- } else {
- if ($display_calls) {
-
- $symbol_tab[$child]["ct"] += $info["ct"];
- }
-
- foreach ($metrics as $metric) {
- $symbol_tab[$child][$metric] += $info[$metric];
- }
- }
- }
- return $symbol_tab;
- }
- function xhprof_prune_run($raw_data, $prune_percent) {
- $main_info = $raw_data["main()"];
- if (empty($main_info)) {
- xhprof_error("XHProf: main() missing in raw data");
- return false;
- }
-
- if (isset($main_info["wt"])) {
- $prune_metric = "wt";
- } else if (isset($main_info["samples"])) {
- $prune_metric = "samples";
- } else {
- xhprof_error("XHProf: for main() we must have either wt "
- ."or samples attribute set");
- return false;
- }
-
- $metrics = array();
- foreach ($main_info as $metric => $val) {
- if (isset($val)) {
- $metrics[] = $metric;
- }
- }
- $prune_threshold = (($main_info[$prune_metric] * $prune_percent) / 100.0);
- init_metrics($raw_data, null, null, false);
- $flat_info = xhprof_compute_inclusive_times($raw_data);
- foreach ($raw_data as $parent_child => $info) {
- list($parent, $child) = xhprof_parse_parent_child($parent_child);
-
- if ($flat_info[$child][$prune_metric] < $prune_threshold) {
- unset($raw_data[$parent_child]);
- } else if ($parent &&
- ($parent != "__pruned__()") &&
- ($flat_info[$parent][$prune_metric] < $prune_threshold)) {
-
-
-
-
- $pruned_edge = xhprof_build_parent_child_key("__pruned__()", $child);
- if (isset($raw_data[$pruned_edge])) {
- foreach ($metrics as $metric) {
- $raw_data[$pruned_edge][$metric]+=$raw_data[$parent_child][$metric];
- }
- } else {
- $raw_data[$pruned_edge] = $raw_data[$parent_child];
- }
- unset($raw_data[$parent_child]);
- }
- }
- return $raw_data;
- }
- function xhprof_array_set($arr, $k, $v) {
- $arr[$k] = $v;
- return $arr;
- }
- function xhprof_array_unset($arr, $k) {
- unset($arr[$k]);
- return $arr;
- }
- define('XHPROF_STRING_PARAM', 1);
- define('XHPROF_UINT_PARAM', 2);
- define('XHPROF_FLOAT_PARAM', 3);
- define('XHPROF_BOOL_PARAM', 4);
- function xhprof_get_param_helper($param) {
- $val = null;
- if (isset($_GET[$param]))
- $val = $_GET[$param];
- else if (isset($_POST[$param])) {
- $val = $_POST[$param];
- }
- return $val;
- }
- function xhprof_get_string_param($param, $default = '') {
- $val = xhprof_get_param_helper($param);
- if ($val === null)
- return $default;
- return $val;
- }
- function xhprof_get_uint_param($param, $default = 0) {
- $val = xhprof_get_param_helper($param);
- if ($val === null)
- $val = $default;
-
- $val = trim($val);
-
- if (ctype_digit($val)) {
- return $val;
- }
- xhprof_error("$param is $val. It must be an unsigned integer.");
- return null;
- }
- function xhprof_get_float_param($param, $default = 0) {
- $val = xhprof_get_param_helper($param);
- if ($val === null)
- $val = $default;
-
- $val = trim($val);
-
- if (true)
- return (float)$val;
- xhprof_error("$param is $val. It must be a float.");
- return null;
- }
- function xhprof_get_bool_param($param, $default = false) {
- $val = xhprof_get_param_helper($param);
- if ($val === null)
- $val = $default;
-
- $val = trim($val);
- switch (strtolower($val)) {
- case '0':
- case '1':
- $val = (bool)$val;
- break;
- case 'true':
- case 'on':
- case 'yes':
- $val = true;
- break;
- case 'false':
- case 'off':
- case 'no':
- $val = false;
- break;
- default:
- xhprof_error("$param is $val. It must be a valid boolean string.");
- return null;
- }
- return $val;
- }
- function xhprof_param_init($params) {
-
- foreach ($params as $k => $v) {
- switch ($v[0]) {
- case XHPROF_STRING_PARAM:
- $p = xhprof_get_string_param($k, $v[1]);
- break;
- case XHPROF_UINT_PARAM:
- $p = xhprof_get_uint_param($k, $v[1]);
- break;
- case XHPROF_FLOAT_PARAM:
- $p = xhprof_get_float_param($k, $v[1]);
- break;
- case XHPROF_BOOL_PARAM:
- $p = xhprof_get_bool_param($k, $v[1]);
- break;
- default:
- xhprof_error("Invalid param type passed to xhprof_param_init: "
- . $v[0]);
- exit();
- }
- if ($k === 'run') {
- $p = implode(',', array_filter(explode(',', $p), 'ctype_xdigit'));
- }
-
- $GLOBALS[$k] = $p;
- }
- }
- function xhprof_get_matching_functions($q, $xhprof_data) {
- $matches = array();
- foreach ($xhprof_data as $parent_child => $info) {
- list($parent, $child) = xhprof_parse_parent_child($parent_child);
- if (stripos($parent, $q) !== false) {
- $matches[$parent] = 1;
- }
- if (stripos($child, $q) !== false) {
- $matches[$child] = 1;
- }
- }
- $res = array_keys($matches);
-
- asort($res);
- return ($res);
- }
|