View source
<?php
namespace Drupal\xhprof\XHProfLib\Parser;
use Drupal\xhprof\XHProfLib\Report\ReportConstants;
use Drupal\xhprof\XHProfLib\Run;
use Drupal\xhprof\XHProfLib\Symbol\Symbol;
abstract class BaseParser implements ParserInterface {
protected $stats;
protected $pc_stats;
protected $metrics;
protected $diff_mode;
protected $sort_col;
protected $display_calls;
protected $sort;
protected $symbol;
private $overall_totals = [
"ct" => 0,
"wt" => 0,
"ut" => 0,
"st" => 0,
"cpu" => 0,
"mu" => 0,
"pmu" => 0,
"samples" => 0,
];
protected $mainSymbol;
protected $run;
public function __construct(Run $run, $sort, $symbol) {
$this->sort = $sort;
$this->run = $run;
$this->symbol = $symbol;
$this->diff_mode = FALSE;
$this->mainSymbol = $run
->getMainSymbol();
$this
->initMetrics($run
->getSymbols(), $symbol, $sort);
}
protected function initMetrics($symbols, $symbol, $sort) {
if (!empty($sort)) {
if (array_key_exists($sort, ReportConstants::getSortableColumns())) {
$this->sort_col = $sort;
}
else {
print "Invalid Sort Key {$sort} specified in URL";
}
}
$wt = $this->mainSymbol
->getWt();
if (!isset($wt)) {
if ($this->sort_col == "wt") {
$this->sort_col = "samples";
}
$this->display_calls = FALSE;
}
else {
$this->display_calls = TRUE;
}
if (!empty($symbol)) {
$this->sort_col = str_replace("excl_", "", $this->sort_col);
}
if ($this->display_calls) {
$this->stats = [
"fn",
"ct",
"Calls%",
];
}
else {
$this->stats = [
"fn",
];
}
$this->pc_stats = $this->stats;
$possible_metrics = $this
->getPossibleMetrics($symbols);
foreach ($possible_metrics as $metric => $desc) {
$mainMetric = $this->mainSymbol
->getMetric($metric);
if (isset($mainMetric)) {
$metrics[] = $metric;
$this->stats[] = $metric;
$this->stats[] = "I" . $desc[0] . "%";
$this->stats[] = "excl_" . $metric;
$this->stats[] = "E" . $desc[0] . "%";
$this->pc_stats[] = $metric;
$this->pc_stats[] = "I" . $desc[0] . "%";
}
}
}
public function getPossibleMetrics() {
return [
"wt" => [
"Wall",
"μs",
"walltime",
],
"ut" => [
"User",
"μs",
"user cpu time",
],
"st" => [
"Sys",
"μs",
"system cpu time",
],
"cpu" => [
"Cpu",
"μs",
"cpu time",
],
"mu" => [
"MUse",
"bytes",
"memory usage",
],
"pmu" => [
"PMUse",
"bytes",
"peak memory usage",
],
"samples" => [
"Samples",
"samples",
"cpu time",
],
];
}
public function getMetrics() {
$possible_metrics = $this
->getPossibleMetrics();
$metrics = [];
foreach ($possible_metrics as $metric => $desc) {
$mainMetric = $this->mainSymbol
->getMetric($metric);
if (isset($mainMetric)) {
$metrics[] = $metric;
}
}
return $metrics;
}
protected function computeFlatInfo($symbols) {
$metrics = $this
->getMetrics();
$symbol_tab = $this
->computeInclusiveTimes($symbols);
foreach ($metrics as $metric) {
$this->overall_totals[$metric] = $this->mainSymbol
->getMetric($metric);
}
foreach ($symbol_tab as $symbol => $info) {
foreach ($metrics as $metric) {
$symbol_tab[$symbol]["excl_" . $metric] = $symbol_tab[$symbol][$metric];
}
$this->overall_totals["ct"] += $info["ct"];
}
foreach ($symbols as $symbol) {
$parent = $symbol
->getParent();
if ($parent) {
foreach ($metrics as $metric) {
if (isset($symbol_tab[$parent])) {
$symbol_tab[$parent]["excl_" . $metric] -= $symbol
->getMetric($metric);
}
}
}
}
return $symbol_tab;
}
protected function computeInclusiveTimes($symbols) {
$metrics = $this
->getMetrics();
$symbol_tab = [];
foreach ($symbols as $symbol) {
$child = $symbol
->getChild();
if (!isset($symbol_tab[$child])) {
$symbol_tab[$child] = [
"ct" => $symbol
->getCt(),
];
foreach ($metrics as $metric) {
$symbol_tab[$child][$metric] = $symbol
->getMetric($metric);
}
}
else {
$symbol_tab[$child]["ct"] += $symbol
->getCt();
foreach ($metrics as $metric) {
$symbol_tab[$child][$metric] += $symbol
->getMetric($metric);
}
}
}
return $symbol_tab;
}
function trimRun($symbols, $functions_to_keep) {
$function_map = array_fill_keys($functions_to_keep, 1);
$function_map['main()'] = 1;
$new_symbols = [];
foreach ($symbols as $symbol) {
$parent = $symbol
->getParent();
$child = $symbol
->getChild();
if (isset($function_map[$parent]) || isset($function_map[$child])) {
$new_symbols["{$parent}==>{$child}"] = $symbol;
}
}
return $new_symbols;
}
function arraySet($arr, $k, $v) {
$arr[$k] = $v;
return $arr;
}
function arrayUnset($arr, $k) {
unset($arr[$k]);
return $arr;
}
public function getTotals() {
return $this->overall_totals;
}
public function getDisplayCalls() {
return $this->display_calls;
}
}