[PATCH 08/21] perf metric: Only add a referenced metric once

From: Ian Rogers
Date: Thu Oct 07 2021 - 12:58:21 EST


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
once. 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>
---
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 e4ce19389258..6c4c51e35aa7 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -870,12 +870,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;
--
2.33.0.882.g93a45727a2-goog