[PATCH 5.15 72/83] perf metric: Only add a referenced metric once

From: Greg Kroah-Hartman
Date: Mon Oct 03 2022 - 03:35:13 EST

From: Ian Rogers <irogers@xxxxxxxxxx>

[ Upstream commit a3de76903dd0786a8661e9e6eb9054a7519e10e7 ]

If a metric references other metrics then the same other metrics may be
referenced more than once, but the events and metric ref are only needed

An example of this is in tests/parse-metric.c where DCache_L2_Hits
references the metric DCache_L2_All_Hits twice, once directly and once
through DCache_L2_All.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
Acked-by: Andi Kleen <ak@xxxxxxxxxxxxxxx>
Cc: Adrian Hunter <adrian.hunter@xxxxxxxxx>
Cc: Alexander Antonov <alexander.antonov@xxxxxxxxxxxxxxx>
Cc: Alexander Shishkin <alexander.shishkin@xxxxxxxxxxxxxxx>
Cc: Andrew Kilroy <andrew.kilroy@xxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Changbin Du <changbin.du@xxxxxxxxx>
Cc: Denys Zagorui <dzagorui@xxxxxxxxx>
Cc: Fabian Hemmer <copy@xxxxxxx>
Cc: Felix Fietkau <nbd@xxxxxxxx>
Cc: Heiko Carstens <hca@xxxxxxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Jacob Keller <jacob.e.keller@xxxxxxxxx>
Cc: Jiapeng Chong <jiapeng.chong@xxxxxxxxxxxxxxxxx>
Cc: Jin Yao <yao.jin@xxxxxxxxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Joakim Zhang <qiangqing.zhang@xxxxxxx>
Cc: John Garry <john.garry@xxxxxxxxxx>
Cc: Kajol Jain <kjain@xxxxxxxxxxxxx>
Cc: Kan Liang <kan.liang@xxxxxxxxxxxxxxx>
Cc: Kees Kook <keescook@xxxxxxxxxxxx>
Cc: Mark Rutland <mark.rutland@xxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Cc: Nicholas Fraser <nfraser@xxxxxxxxxxxxxxx>
Cc: Nick Desaulniers <ndesaulniers@xxxxxxxxxx>
Cc: Paul Clarke <pc@xxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Riccardo Mancini <rickyman7@xxxxxxxxx>
Cc: Sami Tolvanen <samitolvanen@xxxxxxxxxx>
Cc: ShihCheng Tu <mrtoastcheng@xxxxxxxxx>
Cc: Song Liu <songliubraving@xxxxxx>
Cc: Stephane Eranian <eranian@xxxxxxxxxx>
Cc: Sumanth Korikkar <sumanthk@xxxxxxxxxxxxx>
Cc: Thomas Richter <tmricht@xxxxxxxxxxxxx>
Cc: Wan Jiabing <wanjiabing@xxxxxxxx>
Cc: Zhen Lei <thunder.leizhen@xxxxxxxxxx>
Link: https://lore.kernel.org/r/20211015172132.1162559-9-irogers@xxxxxxxxxx
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Stable-dep-of: 71c86cda750b ("perf parse-events: Remove "not supported" hybrid cache events")
Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>
tools/perf/util/metricgroup.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 2dc2a0dcf846..ec8195f1ab50 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -836,12 +836,18 @@ static int __add_metric(struct list_head *metric_list,
*mp = m;
} else {
- * We got here for the referenced metric, via the
- * recursive metricgroup__add_metric call, add
- * it to the parent group.
+ * This metric was referenced in a metric higher in the
+ * tree. Check if the same metric is already resolved in the
+ * metric_refs list.
m = *mp;

+ list_for_each_entry(ref, &m->metric_refs, list) {
+ if (!strcmp(pe->metric_name, ref->metric_name))
+ return 0;
+ }
+ /*Add the new referenced metric to the pare the parent group. */
ref = malloc(sizeof(*ref));
if (!ref)
return -ENOMEM;