Commit 411ad22e authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf parse-events: Add pmu filter

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: default avatarIan Rogers <irogers@google.com>
Tested-by: default avatarKan 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.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 24d80818
...@@ -3335,6 +3335,14 @@ const char record_callchain_help[] = CALLCHAIN_RECORD_HELP ...@@ -3335,6 +3335,14 @@ const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
static bool dry_run; 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 * 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 * with it and switch to use the library functions in perf_evlist that came
...@@ -3343,7 +3351,7 @@ static bool dry_run; ...@@ -3343,7 +3351,7 @@ static bool dry_run;
* using pipes, etc. * using pipes, etc.
*/ */
static struct option __record_options[] = { 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", "event selector. use 'perf list' to list available events",
parse_events_option), parse_events_option),
OPT_CALLBACK(0, "filter", &record.evlist, "filter", OPT_CALLBACK(0, "filter", &record.evlist, "filter",
...@@ -3496,7 +3504,8 @@ static struct option __record_options[] = { ...@@ -3496,7 +3504,8 @@ static struct option __record_options[] = {
&record.switch_output.set, "signal or size[BKMG] or time[smhd]", &record.switch_output.set, "signal or size[BKMG] or time[smhd]",
"Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold", "Switch output when receiving SIGUSR2 (signal) or cross a size or time threshold",
"signal"), "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", "switch output event selector. use 'perf list' to list available events",
parse_events_option_new_evlist), parse_events_option_new_evlist),
OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files, OPT_INTEGER(0, "switch-max-files", &record.switch_output.num_files,
......
...@@ -101,6 +101,10 @@ ...@@ -101,6 +101,10 @@
static void print_counters(struct timespec *ts, int argc, const char **argv); static void print_counters(struct timespec *ts, int argc, const char **argv);
static struct evlist *evsel_list; 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 bool all_counters_use_bpf = true;
static struct target target = { static struct target target = {
...@@ -1096,8 +1100,8 @@ static int parse_hybrid_type(const struct option *opt, ...@@ -1096,8 +1100,8 @@ static int parse_hybrid_type(const struct option *opt,
return -1; return -1;
} }
evlist->hybrid_pmu_name = perf_pmu__hybrid_type_to_pmu(str); parse_events_option_args.pmu_filter = perf_pmu__hybrid_type_to_pmu(str);
if (!evlist->hybrid_pmu_name) { if (!parse_events_option_args.pmu_filter) {
fprintf(stderr, "--cputype %s is not supported!\n", str); fprintf(stderr, "--cputype %s is not supported!\n", str);
return -1; return -1;
} }
...@@ -1108,7 +1112,7 @@ static int parse_hybrid_type(const struct option *opt, ...@@ -1108,7 +1112,7 @@ static int parse_hybrid_type(const struct option *opt,
static struct option stat_options[] = { static struct option stat_options[] = {
OPT_BOOLEAN('T', "transaction", &transaction_run, OPT_BOOLEAN('T', "transaction", &transaction_run,
"hardware transaction statistics"), "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", "event selector. use 'perf list' to list available events",
parse_events_option), parse_events_option),
OPT_CALLBACK(0, "filter", &evsel_list, "filter", OPT_CALLBACK(0, "filter", &evsel_list, "filter",
......
...@@ -1440,12 +1440,15 @@ int cmd_top(int argc, const char **argv) ...@@ -1440,12 +1440,15 @@ int cmd_top(int argc, const char **argv)
.max_stack = sysctl__max_stack(), .max_stack = sysctl__max_stack(),
.nr_threads_synthesize = UINT_MAX, .nr_threads_synthesize = UINT_MAX,
}; };
struct parse_events_option_args parse_events_option_args = {
.evlistp = &top.evlist,
};
bool branch_call_mode = false; bool branch_call_mode = false;
struct record_opts *opts = &top.record_opts; struct record_opts *opts = &top.record_opts;
struct target *target = &opts->target; struct target *target = &opts->target;
const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL; const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
const struct option options[] = { 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", "event selector. use 'perf list' to list available events",
parse_events_option), parse_events_option),
OPT_U64('c', "count", &opts->user_interval, "event period to sample"), OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
......
...@@ -4591,8 +4591,11 @@ static int trace__parse_events_option(const struct option *opt, const char *str, ...@@ -4591,8 +4591,11 @@ static int trace__parse_events_option(const struct option *opt, const char *str,
err = 0; err = 0;
if (lists[0]) { if (lists[0]) {
struct parse_events_option_args parse_events_option_args = {
.evlistp = &trace->evlist,
};
struct option o = { struct option o = {
.value = &trace->evlist, .value = &parse_events_option_args,
}; };
err = parse_events_option(&o, lists[0], 0); err = parse_events_option(&o, lists[0], 0);
} }
......
...@@ -1952,7 +1952,8 @@ static int test_event_fake_pmu(const char *str) ...@@ -1952,7 +1952,8 @@ static int test_event_fake_pmu(const char *str)
return -ENOMEM; return -ENOMEM;
parse_events_error__init(&err); 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) { if (ret) {
pr_debug("failed to parse event '%s', err %d, str '%s'\n", pr_debug("failed to parse event '%s', err %d, str '%s'\n",
str, ret, err.str); str, ret, err.str);
......
...@@ -776,7 +776,8 @@ static int check_parse_id(const char *id, struct parse_events_error *error, ...@@ -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, '@')) for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
*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); free(dup);
evlist__delete(evlist); evlist__delete(evlist);
......
...@@ -67,7 +67,6 @@ struct evlist { ...@@ -67,7 +67,6 @@ struct evlist {
struct evsel *selected; struct evsel *selected;
struct events_stats stats; struct events_stats stats;
struct perf_env *env; struct perf_env *env;
const char *hybrid_pmu_name;
void (*trace_event_sample_raw)(struct evlist *evlist, void (*trace_event_sample_raw)(struct evlist *evlist,
union perf_event *event, union perf_event *event,
struct perf_sample *sample); struct perf_sample *sample);
......
...@@ -1441,8 +1441,8 @@ static int parse_ids(bool metric_no_merge, struct perf_pmu *fake_pmu, ...@@ -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); pr_debug("Parsing metric events '%s'\n", events.buf);
parse_events_error__init(&parse_error); parse_events_error__init(&parse_error);
ret = __parse_events(parsed_evlist, events.buf, &parse_error, fake_pmu, ret = __parse_events(parsed_evlist, events.buf, /*pmu_filter=*/NULL,
/*warn_if_reordered=*/false); &parse_error, fake_pmu, /*warn_if_reordered=*/false);
if (ret) { if (ret) {
parse_events_error__print(&parse_error, events.buf); parse_events_error__print(&parse_error, events.buf);
goto err_out; goto err_out;
......
...@@ -465,8 +465,24 @@ int parse_events__decode_legacy_cache(const char *name, int pmu_type, __u64 *con ...@@ -465,8 +465,24 @@ int parse_events__decode_legacy_cache(const char *name, int pmu_type, __u64 *con
return 0; 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, 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 list_head *head_config)
{ {
struct perf_pmu *pmu = NULL; struct perf_pmu *pmu = NULL;
...@@ -483,6 +499,9 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name, ...@@ -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)) if (!perf_pmu__supports_legacy_cache(pmu))
continue; continue;
if (parse_events__filter_pmu(parse_state, pmu))
continue;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, sizeof(attr));
attr.type = PERF_TYPE_HW_CACHE; attr.type = PERF_TYPE_HW_CACHE;
...@@ -493,8 +512,7 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name, ...@@ -493,8 +512,7 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
found_supported = true; found_supported = true;
if (head_config) { if (head_config) {
if (config_attr(&attr, head_config, err, if (config_attr(&attr, head_config, parse_state->error, config_term_common))
config_term_common))
return -EINVAL; return -EINVAL;
if (get_config_terms(head_config, &config_terms)) if (get_config_terms(head_config, &config_terms))
...@@ -1494,6 +1512,9 @@ int parse_events_add_numeric(struct parse_events_state *parse_state, ...@@ -1494,6 +1512,9 @@ int parse_events_add_numeric(struct parse_events_state *parse_state,
if (!perf_pmu__supports_wildcard_numeric(pmu)) if (!perf_pmu__supports_wildcard_numeric(pmu))
continue; continue;
if (parse_events__filter_pmu(parse_state, pmu))
continue;
found_supported = true; found_supported = true;
ret = __parse_events_add_numeric(parse_state, list, pmu, pmu->type, ret = __parse_events_add_numeric(parse_state, list, pmu, pmu->type,
config, head_config); config, head_config);
...@@ -1682,6 +1703,9 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state, ...@@ -1682,6 +1703,9 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
while ((pmu = perf_pmu__scan(pmu)) != NULL) { while ((pmu = perf_pmu__scan(pmu)) != NULL) {
struct perf_pmu_alias *alias; struct perf_pmu_alias *alias;
if (parse_events__filter_pmu(parse_state, pmu))
continue;
list_for_each_entry(alias, &pmu->aliases, list) { list_for_each_entry(alias, &pmu->aliases, list) {
if (!strcasecmp(alias->name, str)) { if (!strcasecmp(alias->name, str)) {
parse_events_copy_term_list(head, &orig_head); 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) ...@@ -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; 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, struct parse_events_error *err, struct perf_pmu *fake_pmu,
bool warn_if_reordered) bool warn_if_reordered)
{ {
...@@ -2132,6 +2156,7 @@ int __parse_events(struct evlist *evlist, const char *str, ...@@ -2132,6 +2156,7 @@ int __parse_events(struct evlist *evlist, const char *str,
.evlist = evlist, .evlist = evlist,
.stoken = PE_START_EVENTS, .stoken = PE_START_EVENTS,
.fake_pmu = fake_pmu, .fake_pmu = fake_pmu,
.pmu_filter = pmu_filter,
.match_legacy_cache_terms = true, .match_legacy_cache_terms = true,
}; };
int ret; int ret;
...@@ -2313,12 +2338,13 @@ void parse_events_error__print(struct parse_events_error *err, ...@@ -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 parse_events_option(const struct option *opt, const char *str,
int unset __maybe_unused) int unset __maybe_unused)
{ {
struct evlist *evlist = *(struct evlist **)opt->value; struct parse_events_option_args *args = opt->value;
struct parse_events_error err; struct parse_events_error err;
int ret; int ret;
parse_events_error__init(&err); 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) { if (ret) {
parse_events_error__print(&err, str); parse_events_error__print(&err, str);
...@@ -2331,22 +2357,21 @@ int parse_events_option(const struct option *opt, const char *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) 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; int ret;
if (*evlistp == NULL) { if (*args->evlistp == NULL) {
*evlistp = evlist__new(); *args->evlistp = evlist__new();
if (*evlistp == NULL) { if (*args->evlistp == NULL) {
fprintf(stderr, "Not enough memory to create evlist\n"); fprintf(stderr, "Not enough memory to create evlist\n");
return -1; return -1;
} }
} }
ret = parse_events_option(opt, str, unset); ret = parse_events_option(opt, str, unset);
if (ret) { if (ret) {
evlist__delete(*evlistp); evlist__delete(*args->evlistp);
*evlistp = NULL; *args->evlistp = NULL;
} }
return ret; return ret;
......
...@@ -22,17 +22,24 @@ bool is_event_supported(u8 type, u64 config); ...@@ -22,17 +22,24 @@ bool is_event_supported(u8 type, u64 config);
const char *event_type(int type); 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(const struct option *opt, const char *str, int unset);
int parse_events_option_new_evlist(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))) __attribute__((nonnull(1, 2, 4)))
int __parse_events(struct evlist *evlist, const char *str, struct parse_events_error *error, int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter,
struct perf_pmu *fake_pmu, bool warn_if_reordered); struct parse_events_error *error, struct perf_pmu *fake_pmu,
bool warn_if_reordered);
__attribute__((nonnull(1, 2, 3))) __attribute__((nonnull(1, 2, 3)))
static inline int parse_events(struct evlist *evlist, const char *str, static inline int parse_events(struct evlist *evlist, const char *str,
struct parse_events_error *err) 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); int parse_event(struct evlist *evlist, const char *str);
...@@ -122,11 +129,15 @@ struct parse_events_state { ...@@ -122,11 +129,15 @@ struct parse_events_state {
struct list_head *terms; struct list_head *terms;
int stoken; int stoken;
struct perf_pmu *fake_pmu; 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? */ /* Should PE_LEGACY_NAME tokens be generated for config terms? */
bool match_legacy_cache_terms; bool match_legacy_cache_terms;
bool wild_card_pmus; 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); void parse_events__shrink_config_terms(void);
int parse_events__is_hardcoded_term(struct parse_events_term *term); int parse_events__is_hardcoded_term(struct parse_events_term *term);
int parse_events_term__num(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, ...@@ -171,7 +182,7 @@ int parse_events_add_tool(struct parse_events_state *parse_state,
struct list_head *list, struct list_head *list,
int tool_event); int tool_event);
int parse_events_add_cache(struct list_head *list, int *idx, const char *name, 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); struct list_head *head_config);
int parse_events__decode_legacy_cache(const char *name, int pmu_type, __u64 *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, int parse_events_add_breakpoint(struct list_head *list, int *idx,
......
...@@ -312,6 +312,9 @@ PE_NAME opt_pmu_config ...@@ -312,6 +312,9 @@ PE_NAME opt_pmu_config
while ((pmu = perf_pmu__scan(pmu)) != NULL) { while ((pmu = perf_pmu__scan(pmu)) != NULL) {
char *name = pmu->name; char *name = pmu->name;
if (parse_events__filter_pmu(parse_state, pmu))
continue;
if (!strncmp(name, "uncore_", 7) && if (!strncmp(name, "uncore_", 7) &&
strncmp($1, "uncore_", 7)) strncmp($1, "uncore_", 7))
name += 7; name += 7;
...@@ -473,13 +476,12 @@ event_legacy_cache: ...@@ -473,13 +476,12 @@ event_legacy_cache:
PE_LEGACY_CACHE opt_event_config PE_LEGACY_CACHE opt_event_config
{ {
struct parse_events_state *parse_state = _parse_state; struct parse_events_state *parse_state = _parse_state;
struct parse_events_error *error = parse_state->error;
struct list_head *list; struct list_head *list;
int err; int err;
list = alloc_list(); list = alloc_list();
ABORT_ON(!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); parse_events_terms__delete($2);
free($1); free($1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment