[RFC PATCH v5 3/6] perf stat: Add retire latency values into the expr_parse_ctx to prepare for final metric calculation

From: weilin . wang
Date: Tue Mar 26 2024 - 16:35:44 EST


From: Weilin Wang <weilin.wang@xxxxxxxxx>

Retire latency values of events are used in metric formulas. This update adds
code to process data from perf record for required retire latency values.

Signed-off-by: Weilin Wang <weilin.wang@xxxxxxxxx>
Reviewed-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-stat.c | 1 +
tools/perf/util/metricgroup.h | 4 ++++
tools/perf/util/stat-shadow.c | 19 +++++++++++++++++++
3 files changed, 24 insertions(+)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index fb08cef42d95..fc887d9aaa66 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -786,6 +786,7 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
if (!strcmp(evname, t->event.name)) {
t->count += 1;
t->sum += sample->retire_lat;
+ t->val = (double) t->sum / t->count;
break;
}
}
diff --git a/tools/perf/util/metricgroup.h b/tools/perf/util/metricgroup.h
index e5bafb45d4d9..b073682b4067 100644
--- a/tools/perf/util/metricgroup.h
+++ b/tools/perf/util/metricgroup.h
@@ -76,8 +76,12 @@ struct tpebs_event {

struct tpebs_retire_lat {
struct tpebs_event event;
+ /* Count of retire_latency values found in sample data */
size_t count;
+ /* Sum of all the retire_latency values in sample data */
int sum;
+ /* Average of retire_latency, val = sum / count */
+ double val;
};

struct metric_event *metricgroup__lookup(struct rblist *metric_events,
diff --git a/tools/perf/util/stat-shadow.c b/tools/perf/util/stat-shadow.c
index 3466aa952442..8fc3415de106 100644
--- a/tools/perf/util/stat-shadow.c
+++ b/tools/perf/util/stat-shadow.c
@@ -355,6 +355,20 @@ static void print_nsecs(struct perf_stat_config *config,
print_metric(config, ctxp, NULL, NULL, "CPUs utilized", 0);
}

+static int prepare_retire_lat(struct expr_parse_ctx *pctx,
+ struct list_head *retire_lats)
+{
+ int ret = 0;
+ struct tpebs_retire_lat *t;
+
+ list_for_each_entry(t, retire_lats, nd) {
+ ret = expr__add_id_val(pctx, strdup(t->tpebs_name), t->val);
+ if (ret < 0)
+ return ret;
+ }
+ return ret;
+}
+
static int prepare_metric(const struct metric_expr *mexp,
const struct evsel *evsel,
struct expr_parse_ctx *pctx,
@@ -486,6 +500,11 @@ static void generic_metric(struct perf_stat_config *config,
pctx->sctx.user_requested_cpu_list = strdup(config->user_requested_cpu_list);
pctx->sctx.runtime = runtime;
pctx->sctx.system_wide = config->system_wide;
+ i = prepare_retire_lat(pctx, &config->tpebs_results);
+ if (i < 0) {
+ expr__ctx_free(pctx);
+ return;
+ }
i = prepare_metric(mexp, evsel, pctx, aggr_idx);
if (i < 0) {
expr__ctx_free(pctx);
--
2.43.0