Skip to content

Commit f91fa2a

Browse files
captain5050acmel
authored andcommitted
perf pmu: Refactor perf_pmu__match()
Move all implementation to pmu code. Don't allocate a fnmatch wildcard pattern, matching ignoring the suffix already handles this, and only use fnmatch if the given PMU name has a '*' in it. Signed-off-by: Ian Rogers <[email protected]> Reviewed-by: Kan Liang <[email protected]> Tested-by: Atish Patra <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Beeman Strong <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 90b2c21 commit f91fa2a

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

tools/perf/util/parse-events.c

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,6 @@ int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state
16111611
struct list_head **listp,
16121612
void *loc_)
16131613
{
1614-
char *pattern = NULL;
16151614
YYLTYPE *loc = loc_;
16161615
struct perf_pmu *pmu;
16171616
int ok = 0;
@@ -1631,22 +1630,9 @@ int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state
16311630

16321631
pmu = NULL;
16331632
/* Failed to add, try wildcard expansion of event_or_pmu as a PMU name. */
1634-
if (asprintf(&pattern, "%s*", event_or_pmu) < 0) {
1635-
zfree(listp);
1636-
return -ENOMEM;
1637-
}
1638-
16391633
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
1640-
const char *name = pmu->name;
1641-
1642-
if (parse_events__filter_pmu(parse_state, pmu))
1643-
continue;
1644-
1645-
if (!strncmp(name, "uncore_", 7) &&
1646-
strncmp(event_or_pmu, "uncore_", 7))
1647-
name += 7;
1648-
if (!perf_pmu__match(pattern, name, event_or_pmu) ||
1649-
!perf_pmu__match(pattern, pmu->alias_name, event_or_pmu)) {
1634+
if (!parse_events__filter_pmu(parse_state, pmu) &&
1635+
perf_pmu__match(pmu, event_or_pmu)) {
16501636
bool auto_merge_stats = perf_pmu__auto_merge_stats(pmu);
16511637

16521638
if (!parse_events_add_pmu(parse_state, *listp, pmu,
@@ -1657,7 +1643,6 @@ int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state
16571643
}
16581644
}
16591645
}
1660-
zfree(&pattern);
16611646
if (ok)
16621647
return 0;
16631648

tools/perf/util/pmu.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,18 +2064,29 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
20642064
name ?: "N/A", buf, config_name, config);
20652065
}
20662066

2067-
int perf_pmu__match(const char *pattern, const char *name, const char *tok)
2067+
bool perf_pmu__match(const struct perf_pmu *pmu, const char *tok)
20682068
{
2069-
if (!name)
2070-
return -1;
2069+
const char *name = pmu->name;
2070+
bool need_fnmatch = strchr(tok, '*') != NULL;
20712071

2072-
if (fnmatch(pattern, name, 0))
2073-
return -1;
2072+
if (!strncmp(tok, "uncore_", 7))
2073+
tok += 7;
2074+
if (!strncmp(name, "uncore_", 7))
2075+
name += 7;
20742076

2075-
if (tok && !perf_pmu__match_ignoring_suffix(name, tok))
2076-
return -1;
2077+
if (perf_pmu__match_ignoring_suffix(name, tok) ||
2078+
(need_fnmatch && !fnmatch(tok, name, 0)))
2079+
return true;
20772080

2078-
return 0;
2081+
name = pmu->alias_name;
2082+
if (!name)
2083+
return false;
2084+
2085+
if (!strncmp(name, "uncore_", 7))
2086+
name += 7;
2087+
2088+
return perf_pmu__match_ignoring_suffix(name, tok) ||
2089+
(need_fnmatch && !fnmatch(tok, name, 0));
20792090
}
20802091

20812092
double __weak perf_pmu__cpu_slots_per_cycle(void)

tools/perf/util/pmu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
263263
const char *config_name);
264264
void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu);
265265

266-
int perf_pmu__match(const char *pattern, const char *name, const char *tok);
266+
bool perf_pmu__match(const struct perf_pmu *pmu, const char *tok);
267267

268268
double perf_pmu__cpu_slots_per_cycle(void);
269269
int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size);

0 commit comments

Comments
 (0)