[PATCH v2 1/2] perf pmu: Add pmu name wildcards matching function

From: Jin Yao
Date: Wed Jun 30 2021 - 08:10:51 EST


Wildcards is supported on pmu name for dynamic pmu events
(e.g. uncore events). Now move the pmu name wildcards matching
to a new function perf_pmu__pattern_match().

Signed-off-by: Jin Yao <yao.jin@xxxxxxxxxxxxxxx>
---
tools/perf/util/parse-events.y | 7 +------
tools/perf/util/pmu.c | 16 ++++++++++++++++
tools/perf/util/pmu.h | 1 +
3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/parse-events.y b/tools/perf/util/parse-events.y
index aba12a4d488e..6d467ce745d8 100644
--- a/tools/perf/util/parse-events.y
+++ b/tools/perf/util/parse-events.y
@@ -311,12 +311,7 @@ event_pmu_name opt_pmu_config
CLEANUP_YYABORT;

while ((pmu = perf_pmu__scan(pmu)) != NULL) {
- char *name = pmu->name;
-
- if (!strncmp(name, "uncore_", 7) &&
- strncmp($1, "uncore_", 7))
- name += 7;
- if (!fnmatch(pattern, name, 0)) {
+ if (!perf_pmu__pattern_match(pmu, pattern, $1)) {
if (parse_events_copy_term_list(orig_terms, &terms))
CLEANUP_YYABORT;
if (!parse_events_add_pmu(_parse_state, list, pmu->name, terms, true, false))
diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index 88c8ecdc60b0..96f5ff9b5440 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -17,6 +17,7 @@
#include <locale.h>
#include <regex.h>
#include <perf/cpumap.h>
+#include <fnmatch.h>
#include "debug.h"
#include "evsel.h"
#include "pmu.h"
@@ -1872,3 +1873,18 @@ bool perf_pmu__has_hybrid(void)

return !list_empty(&perf_pmu__hybrid_pmus);
}
+
+int perf_pmu__pattern_match(struct perf_pmu *pmu, char *pattern, char *tok)
+{
+ char *name = pmu->name;
+
+ if (!strncmp(name, "uncore_", 7) &&
+ strncmp(tok, "uncore_", 7)) {
+ name += 7;
+ }
+
+ if (fnmatch(pattern, name, 0))
+ return -1;
+
+ return 0;
+}
diff --git a/tools/perf/util/pmu.h b/tools/perf/util/pmu.h
index a790ef758171..1b08e706d6e9 100644
--- a/tools/perf/util/pmu.h
+++ b/tools/perf/util/pmu.h
@@ -133,5 +133,6 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
char *name);

bool perf_pmu__has_hybrid(void);
+int perf_pmu__pattern_match(struct perf_pmu *pmu, char *pattern, char *tok);

#endif /* __PMU_H */
--
2.17.1