perf test: Handle metric reuse in pmu-events parsing test
authorJohn Garry <john.garry@huawei.com>
Wed, 7 Apr 2021 10:32:46 +0000 (18:32 +0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 8 Apr 2021 17:24:00 +0000 (14:24 -0300)
The pmu-events parsing test does not handle metric reuse at all.

Introduce some simple handling to resolve metrics who reference other
metrics.

Reviewed-by: Kajol Jain <kjain@linux.ibm.com>
Signed-off-by: John Garry <john.garry@huawei.com>
Tested-by: Paul A. Clarke <pc@us.ibm.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Shaokun Zhang <zhangshaokun@hisilicon.com>
Cc: Will Deacon <will@kernel.org>
Cc: linuxarm@huawei.com
Cc: linux-arm-kernel@lists.infradead.org
Link: https://lore.kernel.org/r/1617791570-165223-3-git-send-email-john.garry@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/pmu-events.c

index 0ca6a5a..cb5b25d 100644 (file)
@@ -12,6 +12,7 @@
 #include "util/evlist.h"
 #include "util/expr.h"
 #include "util/parse-events.h"
+#include "metricgroup.h"
 
 struct perf_pmu_test_event {
        /* used for matching against events from generated pmu-events.c */
@@ -471,6 +472,71 @@ static void expr_failure(const char *msg,
        pr_debug("On expression %s\n", pe->metric_expr);
 }
 
+struct metric {
+       struct list_head list;
+       struct metric_ref metric_ref;
+};
+
+static int resolve_metric_simple(struct expr_parse_ctx *pctx,
+                                struct list_head *compound_list,
+                                struct pmu_events_map *map,
+                                const char *metric_name)
+{
+       struct hashmap_entry *cur, *cur_tmp;
+       struct metric *metric, *tmp;
+       size_t bkt;
+       bool all;
+       int rc;
+
+       do {
+               all = true;
+               hashmap__for_each_entry_safe((&pctx->ids), cur, cur_tmp, bkt) {
+                       struct metric_ref *ref;
+                       struct pmu_event *pe;
+
+                       pe = metricgroup__find_metric(cur->key, map);
+                       if (!pe)
+                               continue;
+
+                       if (!strcmp(metric_name, (char *)cur->key)) {
+                               pr_warning("Recursion detected for metric %s\n", metric_name);
+                               rc = -1;
+                               goto out_err;
+                       }
+
+                       all = false;
+
+                       /* The metric key itself needs to go out.. */
+                       expr__del_id(pctx, cur->key);
+
+                       metric = malloc(sizeof(*metric));
+                       if (!metric) {
+                               rc = -ENOMEM;
+                               goto out_err;
+                       }
+
+                       ref = &metric->metric_ref;
+                       ref->metric_name = pe->metric_name;
+                       ref->metric_expr = pe->metric_expr;
+                       list_add_tail(&metric->list, compound_list);
+
+                       rc = expr__find_other(pe->metric_expr, NULL, pctx, 0);
+                       if (rc)
+                               goto out_err;
+                       break; /* The hashmap has been modified, so restart */
+               }
+       } while (!all);
+
+       return 0;
+
+out_err:
+       list_for_each_entry_safe(metric, tmp, compound_list, list)
+               free(metric);
+
+       return rc;
+
+}
+
 static int test_parsing(void)
 {
        struct pmu_events_map *cpus_map = perf_pmu__find_map(NULL);
@@ -488,7 +554,9 @@ static int test_parsing(void)
                        break;
                j = 0;
                for (;;) {
+                       struct metric *metric, *tmp;
                        struct hashmap_entry *cur;
+                       LIST_HEAD(compound_list);
                        size_t bkt;
 
                        pe = &map->table[j++];
@@ -504,6 +572,13 @@ static int test_parsing(void)
                                continue;
                        }
 
+                       if (resolve_metric_simple(&ctx, &compound_list, map,
+                                                 pe->metric_name)) {
+                               expr_failure("Could not resolve metrics", map, pe);
+                               ret++;
+                               goto exit; /* Don't tolerate errors due to severity */
+                       }
+
                        /*
                         * Add all ids with a made up value. The value may
                         * trigger divide by zero when subtracted and so try to
@@ -519,6 +594,11 @@ static int test_parsing(void)
                                        ret++;
                        }
 
+                       list_for_each_entry_safe(metric, tmp, &compound_list, list) {
+                               expr__add_ref(&ctx, &metric->metric_ref);
+                               free(metric);
+                       }
+
                        if (expr__parse(&result, &ctx, pe->metric_expr, 0)) {
                                expr_failure("Parse failed", map, pe);
                                ret++;
@@ -527,6 +607,7 @@ static int test_parsing(void)
                }
        }
        /* TODO: fail when not ok */
+exit:
        return ret == 0 ? TEST_OK : TEST_SKIP;
 }