Commit 8b734eaa authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf parse-events: Factor out '<event_or_pmu>/.../' parsing

Factor out the case of an event or PMU name followed by a slash based
term list. This is with a view to sharing the code with new legacy
hardware parsing. Use early return to reduce indentation in the code.
Make parse_events_add_pmu static now it doesn't need sharing with
parse-events.y.
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Reviewed-by: default avatarKan Liang <kan.liang@linux.intel.com>
Tested-by: default avatarAtish Patra <atishp@rivosinc.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Beeman Strong <beeman@rivosinc.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20240416061533.921723-2-irogers@google.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent e0c48bf9
...@@ -1385,7 +1385,7 @@ static bool config_term_percore(struct list_head *config_terms) ...@@ -1385,7 +1385,7 @@ static bool config_term_percore(struct list_head *config_terms)
return false; return false;
} }
int parse_events_add_pmu(struct parse_events_state *parse_state, static int parse_events_add_pmu(struct parse_events_state *parse_state,
struct list_head *list, const char *name, struct list_head *list, const char *name,
const struct parse_events_terms *const_parsed_terms, const struct parse_events_terms *const_parsed_terms,
bool auto_merge_stats, void *loc_) bool auto_merge_stats, void *loc_)
...@@ -1618,6 +1618,74 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state, ...@@ -1618,6 +1618,74 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
return ok ? 0 : -1; return ok ? 0 : -1;
} }
int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state,
const char *event_or_pmu,
const struct parse_events_terms *const_parsed_terms,
struct list_head **listp,
void *loc_)
{
char *pattern = NULL;
YYLTYPE *loc = loc_;
struct perf_pmu *pmu = NULL;
int ok = 0;
char *help;
*listp = malloc(sizeof(**listp));
if (!*listp)
return -ENOMEM;
INIT_LIST_HEAD(*listp);
/* Attempt to add to list assuming event_or_pmu is a PMU name. */
if (!parse_events_add_pmu(parse_state, *listp, event_or_pmu, const_parsed_terms,
/*auto_merge_stats=*/false, loc))
return 0;
/* Failed to add, try wildcard expansion of event_or_pmu as a PMU name. */
if (asprintf(&pattern, "%s*", event_or_pmu) < 0) {
zfree(listp);
return -ENOMEM;
}
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
const char *name = pmu->name;
if (parse_events__filter_pmu(parse_state, pmu))
continue;
if (!strncmp(name, "uncore_", 7) &&
strncmp(event_or_pmu, "uncore_", 7))
name += 7;
if (!perf_pmu__match(pattern, name, event_or_pmu) ||
!perf_pmu__match(pattern, pmu->alias_name, event_or_pmu)) {
bool auto_merge_stats = perf_pmu__auto_merge_stats(pmu);
if (!parse_events_add_pmu(parse_state, *listp, pmu->name,
const_parsed_terms,
auto_merge_stats, loc)) {
ok++;
parse_state->wild_card_pmus = true;
}
}
}
zfree(&pattern);
if (ok)
return 0;
/* Failure to add, assume event_or_pmu is an event name. */
zfree(listp);
if (!parse_events_multi_pmu_add(parse_state, event_or_pmu, const_parsed_terms, listp, loc))
return 0;
if (asprintf(&help, "Unable to find PMU or event on a PMU of '%s'", event_or_pmu) < 0)
help = NULL;
parse_events_error__handle(parse_state->error, loc->first_column,
strdup("Bad event or PMU"),
help);
zfree(listp);
return -EINVAL;
}
int parse_events__modifier_group(struct list_head *list, int parse_events__modifier_group(struct list_head *list,
char *event_mod) char *event_mod)
{ {
......
...@@ -209,10 +209,6 @@ int parse_events_add_breakpoint(struct parse_events_state *parse_state, ...@@ -209,10 +209,6 @@ int parse_events_add_breakpoint(struct parse_events_state *parse_state,
struct list_head *list, struct list_head *list,
u64 addr, char *type, u64 len, u64 addr, char *type, u64 len,
struct parse_events_terms *head_config); struct parse_events_terms *head_config);
int parse_events_add_pmu(struct parse_events_state *parse_state,
struct list_head *list, const char *name,
const struct parse_events_terms *const_parsed_terms,
bool auto_merge_stats, void *loc);
struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr, struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
const char *name, const char *metric_id, const char *name, const char *metric_id,
...@@ -223,6 +219,12 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state, ...@@ -223,6 +219,12 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
const struct parse_events_terms *const_parsed_terms, const struct parse_events_terms *const_parsed_terms,
struct list_head **listp, void *loc); struct list_head **listp, void *loc);
int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state,
const char *event_or_pmu,
const struct parse_events_terms *const_parsed_terms,
struct list_head **listp,
void *loc_);
void parse_events__set_leader(char *name, struct list_head *list); void parse_events__set_leader(char *name, struct list_head *list);
void parse_events_update_lists(struct list_head *list_event, void parse_events_update_lists(struct list_head *list_event,
struct list_head *list_all); struct list_head *list_all);
......
...@@ -273,78 +273,15 @@ event_def: event_pmu | ...@@ -273,78 +273,15 @@ event_def: event_pmu |
event_pmu: event_pmu:
PE_NAME opt_pmu_config PE_NAME opt_pmu_config
{ {
struct parse_events_state *parse_state = _parse_state;
/* List of created evsels. */ /* List of created evsels. */
struct list_head *list = NULL; struct list_head *list = NULL;
char *pattern = NULL; int err = parse_events_multi_pmu_add_or_add_pmu(_parse_state, $1, $2, &list, &@1);
#define CLEANUP \
do { \
parse_events_terms__delete($2); \
free(list); \
free($1); \
free(pattern); \
} while(0)
list = alloc_list(); parse_events_terms__delete($2);
if (!list) { free($1);
CLEANUP; if (err)
YYNOMEM; PE_ABORT(err);
}
/* Attempt to add to list assuming $1 is a PMU name. */
if (parse_events_add_pmu(parse_state, list, $1, $2, /*auto_merge_stats=*/false, &@1)) {
struct perf_pmu *pmu = NULL;
int ok = 0;
/* Failure to add, try wildcard expansion of $1 as a PMU name. */
if (asprintf(&pattern, "%s*", $1) < 0) {
CLEANUP;
YYNOMEM;
}
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
const char *name = pmu->name;
if (parse_events__filter_pmu(parse_state, pmu))
continue;
if (!strncmp(name, "uncore_", 7) &&
strncmp($1, "uncore_", 7))
name += 7;
if (!perf_pmu__match(pattern, name, $1) ||
!perf_pmu__match(pattern, pmu->alias_name, $1)) {
bool auto_merge_stats = perf_pmu__auto_merge_stats(pmu);
if (!parse_events_add_pmu(parse_state, list, pmu->name, $2,
auto_merge_stats, &@1)) {
ok++;
parse_state->wild_card_pmus = true;
}
}
}
if (!ok) {
/* Failure to add, assume $1 is an event name. */
zfree(&list);
ok = !parse_events_multi_pmu_add(parse_state, $1, $2, &list, &@1);
}
if (!ok) {
struct parse_events_error *error = parse_state->error;
char *help;
if (asprintf(&help, "Unable to find PMU or event on a PMU of '%s'", $1) < 0)
help = NULL;
parse_events_error__handle(error, @1.first_column,
strdup("Bad event or PMU"),
help);
CLEANUP;
YYABORT;
}
}
$$ = list; $$ = list;
list = NULL;
CLEANUP;
#undef CLEANUP
} }
| |
PE_NAME sep_dc PE_NAME sep_dc
......
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