perf parse-events: Add pmu filter
authorIan Rogers <irogers@google.com>
Tue, 2 May 2023 22:38:36 +0000 (15:38 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 15 May 2023 12:12:14 +0000 (09:12 -0300)
To support the cputype argument added to "perf stat" for hybrid it is
necessary to filter events during wildcard matching. Add a scanner
argument for the filter and checking it when wildcard matching.

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Kan Liang <kan.liang@linux.intel.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ahmad Yasin <ahmad.yasin@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Caleb Biggers <caleb.biggers@intel.com>
Cc: Edward Baker <edward.baker@intel.com>
Cc: Florian Fischer <florian.fischer@muhq.space>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kang Minchul <tegongkang@gmail.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Perry Taylor <perry.taylor@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Rob Herring <robh@kernel.org>
Cc: Samantha Alt <samantha.alt@intel.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Sumanth Korikkar <sumanthk@linux.ibm.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Cc: Weilin Wang <weilin.wang@intel.com>
Cc: Xing Zhengjun <zhengjun.xing@linux.intel.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Link: https://lore.kernel.org/r/20230502223851.2234828-30-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-record.c
tools/perf/builtin-stat.c
tools/perf/builtin-top.c
tools/perf/builtin-trace.c
tools/perf/tests/parse-events.c
tools/perf/tests/pmu-events.c
tools/perf/util/evlist.h
tools/perf/util/metricgroup.c
tools/perf/util/parse-events.c
tools/perf/util/parse-events.h
tools/perf/util/parse-events.y

index efa03e4..ec0f2d5 100644 (file)
@@ -3335,6 +3335,14 @@ const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
 
 static bool dry_run;
 
+static struct parse_events_option_args parse_events_option_args = {
+       .evlistp = &record.evlist,
+};
+
+static struct parse_events_option_args switch_output_parse_events_option_args = {
+       .evlistp = &record.sb_evlist,
+};
+
 /*
  * XXX Will stay a global variable till we fix builtin-script.c to stop messing
  * with it and switch to use the library functions in perf_evlist that came
@@ -3343,7 +3351,7 @@ static bool dry_run;
  * using pipes, etc.
  */
 static struct option __record_options[] = {
-       OPT_CALLBACK('e', "event", &record.evlist, "event",
+       OPT_CALLBACK('e', "event", &parse_events_option_args, "event",
                     "event selector. use 'perf list' to list available events",
                     parse_events_option),
        OPT_CALLBACK(0, "filter", &record.evlist, "filter",
@@ -3496,7 +3504,8 @@ static struct option __record_options[] = {
                          &record.switch_output.set, "signal or size[BKMG] or time[smhd]",
                          "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold",
                          "signal"),
-       OPT_CALLBACK_SET(0, "switch-output-event", &record.sb_evlist, &record.switch_output_event_set, "switch output event",
+       OPT_CALLBACK_SET(0, "switch-output-event", &switch_output_parse_events_option_args,
+                        &record.switch_output_event_set, "switch output event",
                         "switch output event selector. use 'perf list' to list available events",
                         parse_events_option_new_evlist),
        OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files,
index b9ad32f..de2e915 100644 (file)
 static void print_counters(struct timespec *ts, int argc, const char **argv);
 
 static struct evlist   *evsel_list;
+static struct parse_events_option_args parse_events_option_args = {
+       .evlistp = &evsel_list,
+};
+
 static bool all_counters_use_bpf = true;
 
 static struct target target = {
@@ -1096,8 +1100,8 @@ static int parse_hybrid_type(const struct option *opt,
                return -1;
        }
 
-       evlist->hybrid_pmu_name = perf_pmu__hybrid_type_to_pmu(str);
-       if (!evlist->hybrid_pmu_name) {
+       parse_events_option_args.pmu_filter = perf_pmu__hybrid_type_to_pmu(str);
+       if (!parse_events_option_args.pmu_filter) {
                fprintf(stderr, "--cputype %s is not supported!\n", str);
                return -1;
        }
@@ -1108,7 +1112,7 @@ static int parse_hybrid_type(const struct option *opt,
 static struct option stat_options[] = {
        OPT_BOOLEAN('T', "transaction", &transaction_run,
                    "hardware transaction statistics"),
-       OPT_CALLBACK('e', "event", &evsel_list, "event",
+       OPT_CALLBACK('e', "event", &parse_events_option_args, "event",
                     "event selector. use 'perf list' to list available events",
                     parse_events_option),
        OPT_CALLBACK(0, "filter", &evsel_list, "filter",
index eb57401..48ee49e 100644 (file)
@@ -1440,12 +1440,15 @@ int cmd_top(int argc, const char **argv)
                .max_stack           = sysctl__max_stack(),
                .nr_threads_synthesize = UINT_MAX,
        };
+       struct parse_events_option_args parse_events_option_args = {
+               .evlistp = &top.evlist,
+       };
        bool branch_call_mode = false;
        struct record_opts *opts = &top.record_opts;
        struct target *target = &opts->target;
        const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
        const struct option options[] = {
-       OPT_CALLBACK('e', "event", &top.evlist, "event",
+       OPT_CALLBACK('e', "event", &parse_events_option_args, "event",
                     "event selector. use 'perf list' to list available events",
                     parse_events_option),
        OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
index 8ee3a45..b49d3ab 100644 (file)
@@ -4591,8 +4591,11 @@ do_concat:
        err = 0;
 
        if (lists[0]) {
+               struct parse_events_option_args parse_events_option_args = {
+                       .evlistp = &trace->evlist,
+               };
                struct option o = {
-                       .value = &trace->evlist,
+                       .value = &parse_events_option_args,
                };
                err = parse_events_option(&o, lists[0], 0);
        }
index c3afd0b..0d0c869 100644 (file)
@@ -1952,7 +1952,8 @@ static int test_event_fake_pmu(const char *str)
                return -ENOMEM;
 
        parse_events_error__init(&err);
-       ret = __parse_events(evlist, str, &err, &perf_pmu__fake, /*warn_if_reordered=*/true);
+       ret = __parse_events(evlist, str, /*pmu_filter=*/NULL, &err,
+                            &perf_pmu__fake, /*warn_if_reordered=*/true);
        if (ret) {
                pr_debug("failed to parse event '%s', err %d, str '%s'\n",
                         str, ret, err.str);
index a2cde61..734004f 100644 (file)
@@ -776,7 +776,8 @@ static int check_parse_id(const char *id, struct parse_events_error *error,
        for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
                *cur = '/';
 
-       ret = __parse_events(evlist, dup, error, fake_pmu, /*warn_if_reordered=*/true);
+       ret = __parse_events(evlist, dup, /*pmu_filter=*/NULL, error, fake_pmu,
+                            /*warn_if_reordered=*/true);
        free(dup);
 
        evlist__delete(evlist);
index 46cf402..e7e5540 100644 (file)
@@ -67,7 +67,6 @@ struct evlist {
        struct evsel *selected;
        struct events_stats stats;
        struct perf_env *env;
-       const char *hybrid_pmu_name;
        void (*trace_event_sample_raw)(struct evlist *evlist,
                                       union perf_event *event,
                                       struct perf_sample *sample);
index 4e7d41d..908124d 100644 (file)
@@ -1441,8 +1441,8 @@ static int parse_ids(bool metric_no_merge, struct perf_pmu *fake_pmu,
        }
        pr_debug("Parsing metric events '%s'\n", events.buf);
        parse_events_error__init(&parse_error);
-       ret = __parse_events(parsed_evlist, events.buf, &parse_error, fake_pmu,
-                            /*warn_if_reordered=*/false);
+       ret = __parse_events(parsed_evlist, events.buf, /*pmu_filter=*/NULL,
+                            &parse_error, fake_pmu, /*warn_if_reordered=*/false);
        if (ret) {
                parse_events_error__print(&parse_error, events.buf);
                goto err_out;
index 1d8c3cf..d9d964b 100644 (file)
@@ -465,8 +465,24 @@ int parse_events__decode_legacy_cache(const char *name, int pmu_type, __u64 *con
        return 0;
 }
 
+/**
+ * parse_events__filter_pmu - returns false if a wildcard PMU should be
+ *                            considered, true if it should be filtered.
+ */
+bool parse_events__filter_pmu(const struct parse_events_state *parse_state,
+                             const struct perf_pmu *pmu)
+{
+       if (parse_state->pmu_filter == NULL)
+               return false;
+
+       if (pmu->name == NULL)
+               return true;
+
+       return strcmp(parse_state->pmu_filter, pmu->name) != 0;
+}
+
 int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
-                          struct parse_events_error *err,
+                          struct parse_events_state *parse_state,
                           struct list_head *head_config)
 {
        struct perf_pmu *pmu = NULL;
@@ -483,6 +499,9 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
                if (!perf_pmu__supports_legacy_cache(pmu))
                        continue;
 
+               if (parse_events__filter_pmu(parse_state, pmu))
+                       continue;
+
                memset(&attr, 0, sizeof(attr));
                attr.type = PERF_TYPE_HW_CACHE;
 
@@ -493,8 +512,7 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
                found_supported = true;
 
                if (head_config) {
-                       if (config_attr(&attr, head_config, err,
-                                               config_term_common))
+                       if (config_attr(&attr, head_config, parse_state->error, config_term_common))
                                return -EINVAL;
 
                        if (get_config_terms(head_config, &config_terms))
@@ -1494,6 +1512,9 @@ int parse_events_add_numeric(struct parse_events_state *parse_state,
                if (!perf_pmu__supports_wildcard_numeric(pmu))
                        continue;
 
+               if (parse_events__filter_pmu(parse_state, pmu))
+                       continue;
+
                found_supported = true;
                ret = __parse_events_add_numeric(parse_state, list, pmu, pmu->type,
                                                 config, head_config);
@@ -1682,6 +1703,9 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
        while ((pmu = perf_pmu__scan(pmu)) != NULL) {
                struct perf_pmu_alias *alias;
 
+               if (parse_events__filter_pmu(parse_state, pmu))
+                       continue;
+
                list_for_each_entry(alias, &pmu->aliases, list) {
                        if (!strcasecmp(alias->name, str)) {
                                parse_events_copy_term_list(head, &orig_head);
@@ -2121,7 +2145,7 @@ static bool parse_events__sort_events_and_fix_groups(struct list_head *list)
        return idx_changed || num_leaders != orig_num_leaders;
 }
 
-int __parse_events(struct evlist *evlist, const char *str,
+int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter,
                   struct parse_events_error *err, struct perf_pmu *fake_pmu,
                   bool warn_if_reordered)
 {
@@ -2132,6 +2156,7 @@ int __parse_events(struct evlist *evlist, const char *str,
                .evlist   = evlist,
                .stoken   = PE_START_EVENTS,
                .fake_pmu = fake_pmu,
+               .pmu_filter = pmu_filter,
                .match_legacy_cache_terms = true,
        };
        int ret;
@@ -2313,12 +2338,13 @@ void parse_events_error__print(struct parse_events_error *err,
 int parse_events_option(const struct option *opt, const char *str,
                        int unset __maybe_unused)
 {
-       struct evlist *evlist = *(struct evlist **)opt->value;
+       struct parse_events_option_args *args = opt->value;
        struct parse_events_error err;
        int ret;
 
        parse_events_error__init(&err);
-       ret = parse_events(evlist, str, &err);
+       ret = __parse_events(*args->evlistp, str, args->pmu_filter, &err,
+                            /*fake_pmu=*/NULL, /*warn_if_reordered=*/true);
 
        if (ret) {
                parse_events_error__print(&err, str);
@@ -2331,22 +2357,21 @@ int parse_events_option(const struct option *opt, const char *str,
 
 int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset)
 {
-       struct evlist **evlistp = opt->value;
+       struct parse_events_option_args *args = opt->value;
        int ret;
 
-       if (*evlistp == NULL) {
-               *evlistp = evlist__new();
+       if (*args->evlistp == NULL) {
+               *args->evlistp = evlist__new();
 
-               if (*evlistp == NULL) {
+               if (*args->evlistp == NULL) {
                        fprintf(stderr, "Not enough memory to create evlist\n");
                        return -1;
                }
        }
-
        ret = parse_events_option(opt, str, unset);
        if (ret) {
-               evlist__delete(*evlistp);
-               *evlistp = NULL;
+               evlist__delete(*args->evlistp);
+               *args->evlistp = NULL;
        }
 
        return ret;
index 77b8f7e..d4cbda6 100644 (file)
@@ -22,17 +22,24 @@ bool is_event_supported(u8 type, u64 config);
 
 const char *event_type(int type);
 
+/* Arguments encoded in opt->value. */
+struct parse_events_option_args {
+       struct evlist **evlistp;
+       const char *pmu_filter;
+};
 int parse_events_option(const struct option *opt, const char *str, int unset);
 int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset);
-__attribute__((nonnull(1, 2, 3)))
-int __parse_events(struct evlist *evlist, const char *str, struct parse_events_error *error,
-                  struct perf_pmu *fake_pmu, bool warn_if_reordered);
+__attribute__((nonnull(1, 2, 4)))
+int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter,
+                  struct parse_events_error *error, struct perf_pmu *fake_pmu,
+                  bool warn_if_reordered);
 
 __attribute__((nonnull(1, 2, 3)))
 static inline int parse_events(struct evlist *evlist, const char *str,
                               struct parse_events_error *err)
 {
-       return __parse_events(evlist, str, err, /*fake_pmu=*/NULL, /*warn_if_reordered=*/true);
+       return __parse_events(evlist, str, /*pmu_filter=*/NULL, err, /*fake_pmu=*/NULL,
+                             /*warn_if_reordered=*/true);
 }
 
 int parse_event(struct evlist *evlist, const char *str);
@@ -122,11 +129,15 @@ struct parse_events_state {
        struct list_head          *terms;
        int                        stoken;
        struct perf_pmu           *fake_pmu;
+       /* If non-null, when wildcard matching only match the given PMU. */
+       const char                *pmu_filter;
        /* Should PE_LEGACY_NAME tokens be generated for config terms? */
        bool                       match_legacy_cache_terms;
        bool                       wild_card_pmus;
 };
 
+bool parse_events__filter_pmu(const struct parse_events_state *parse_state,
+                             const struct perf_pmu *pmu);
 void parse_events__shrink_config_terms(void);
 int parse_events__is_hardcoded_term(struct parse_events_term *term);
 int parse_events_term__num(struct parse_events_term **term,
@@ -171,7 +182,7 @@ int parse_events_add_tool(struct parse_events_state *parse_state,
                          struct list_head *list,
                          int tool_event);
 int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
-                          struct parse_events_error *error,
+                          struct parse_events_state *parse_state,
                           struct list_head *head_config);
 int parse_events__decode_legacy_cache(const char *name, int pmu_type, __u64 *config);
 int parse_events_add_breakpoint(struct list_head *list, int *idx,
index e709508..c95877c 100644 (file)
@@ -312,6 +312,9 @@ PE_NAME opt_pmu_config
                while ((pmu = perf_pmu__scan(pmu)) != NULL) {
                        char *name = pmu->name;
 
+                       if (parse_events__filter_pmu(parse_state, pmu))
+                               continue;
+
                        if (!strncmp(name, "uncore_", 7) &&
                            strncmp($1, "uncore_", 7))
                                name += 7;
@@ -473,13 +476,12 @@ event_legacy_cache:
 PE_LEGACY_CACHE opt_event_config
 {
        struct parse_events_state *parse_state = _parse_state;
-       struct parse_events_error *error = parse_state->error;
        struct list_head *list;
        int err;
 
        list = alloc_list();
        ABORT_ON(!list);
-       err = parse_events_add_cache(list, &parse_state->idx, $1, error, $2);
+       err = parse_events_add_cache(list, &parse_state->idx, $1, parse_state, $2);
 
        parse_events_terms__delete($2);
        free($1);