perf parse-events: Avoid scanning PMUs that can't contain events

Add perf_pmus__scan_for_event that only reads sysfs for pmus that
could contain a given event.

Signed-off-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250624231837.179536-2-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
This commit is contained in:
Ian Rogers 2025-06-24 16:18:35 -07:00 committed by Namhyung Kim
parent 9c9f4a27eb
commit e1ec69ed5d
3 changed files with 97 additions and 15 deletions

View File

@ -490,7 +490,7 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
int ret = 0;
struct evsel *first_wildcard_match = NULL;
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
while ((pmu = perf_pmus__scan_for_event(pmu, name)) != NULL) {
LIST_HEAD(config_terms);
struct perf_event_attr attr;
@ -1681,7 +1681,8 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
INIT_LIST_HEAD(list);
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
while ((pmu = perf_pmus__scan_for_event(pmu, event_name)) != NULL) {
if (parse_events__filter_pmu(parse_state, pmu))
continue;
@ -1760,19 +1761,21 @@ int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state
pmu = NULL;
/* Failed to add, try wildcard expansion of event_or_pmu as a PMU name. */
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
if (!parse_events__filter_pmu(parse_state, pmu) &&
perf_pmu__wildcard_match(pmu, event_or_pmu)) {
if (!parse_events_add_pmu(parse_state, *listp, pmu,
const_parsed_terms,
first_wildcard_match,
/*alternate_hw_config=*/PERF_COUNT_HW_MAX)) {
ok++;
parse_state->wild_card_pmus = true;
}
if (first_wildcard_match == NULL)
first_wildcard_match =
container_of((*listp)->prev, struct evsel, core.node);
while ((pmu = perf_pmus__scan_matching_wildcard(pmu, event_or_pmu)) != NULL) {
if (parse_events__filter_pmu(parse_state, pmu))
continue;
if (!parse_events_add_pmu(parse_state, *listp, pmu,
const_parsed_terms,
first_wildcard_match,
/*alternate_hw_config=*/PERF_COUNT_HW_MAX)) {
ok++;
parse_state->wild_card_pmus = true;
}
if (first_wildcard_match == NULL) {
first_wildcard_match =
container_of((*listp)->prev, struct evsel, core.node);
}
}
if (ok)

View File

@ -19,6 +19,7 @@
#include "tool_pmu.h"
#include "print-events.h"
#include "strbuf.h"
#include "string2.h"
/*
* core_pmus: A PMU belongs to core_pmus if it's name is "cpu" or it's sysfs
@ -350,6 +351,82 @@ struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu)
return NULL;
}
struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event)
{
bool use_core_pmus = !pmu || pmu->is_core;
if (!pmu) {
/* Hwmon filename values that aren't used. */
enum hwmon_type type;
int number;
/*
* Core PMUs, other sysfs PMUs and tool PMU can take all event
* types or aren't wother optimizing for.
*/
unsigned int to_read_pmus = PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
PERF_TOOL_PMU_TYPE_TOOL_MASK;
/* Could the event be a hwmon event? */
if (parse_hwmon_filename(event, &type, &number, /*item=*/NULL, /*alarm=*/NULL))
to_read_pmus |= PERF_TOOL_PMU_TYPE_HWMON_MASK;
pmu_read_sysfs(to_read_pmus);
pmu = list_prepare_entry(pmu, &core_pmus, list);
}
if (use_core_pmus) {
list_for_each_entry_continue(pmu, &core_pmus, list)
return pmu;
pmu = NULL;
pmu = list_prepare_entry(pmu, &other_pmus, list);
}
list_for_each_entry_continue(pmu, &other_pmus, list)
return pmu;
return NULL;
}
struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard)
{
bool use_core_pmus = !pmu || pmu->is_core;
if (!pmu) {
/*
* Core PMUs, other sysfs PMUs and tool PMU can have any name or
* aren't wother optimizing for.
*/
unsigned int to_read_pmus = PERF_TOOL_PMU_TYPE_PE_CORE_MASK |
PERF_TOOL_PMU_TYPE_PE_OTHER_MASK |
PERF_TOOL_PMU_TYPE_TOOL_MASK;
/*
* Hwmon PMUs have an alias from a sysfs name like hwmon0,
* hwmon1, etc. or have a name of hwmon_<name>. They therefore
* can only have a wildcard match if the wildcard begins with
* "hwmon".
*/
if (strisglob(wildcard) ||
(strlen(wildcard) >= 5 && strncmp("hwmon", wildcard, 5) == 0))
to_read_pmus |= PERF_TOOL_PMU_TYPE_HWMON_MASK;
pmu_read_sysfs(to_read_pmus);
pmu = list_prepare_entry(pmu, &core_pmus, list);
}
if (use_core_pmus) {
list_for_each_entry_continue(pmu, &core_pmus, list) {
if (perf_pmu__wildcard_match(pmu, wildcard))
return pmu;
}
pmu = NULL;
pmu = list_prepare_entry(pmu, &other_pmus, list);
}
list_for_each_entry_continue(pmu, &other_pmus, list) {
if (perf_pmu__wildcard_match(pmu, wildcard))
return pmu;
}
return NULL;
}
static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
{
bool use_core_pmus = !pmu || pmu->is_core;

View File

@ -19,6 +19,8 @@ struct perf_pmu *perf_pmus__find_by_type(unsigned int type);
struct perf_pmu *perf_pmus__scan(struct perf_pmu *pmu);
struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu);
struct perf_pmu *perf_pmus__scan_for_event(struct perf_pmu *pmu, const char *event);
struct perf_pmu *perf_pmus__scan_matching_wildcard(struct perf_pmu *pmu, const char *wildcard);
const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str);