Skip to contentSkip to conversation

perf pmu: Event parsing and listing fixes

PATCH Superseded James Clark 2 participants 9 messages Jun 17, 2024 – Jun 26, 2024 4 messages not in the archive perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover perf pmu: Event parsing and listing fixes

The second fix is related to the discussion here [1].

The first fix is unrelated but I just noticed it while fixing the listing issue.

[1]: https://lore.kernel.org/all/[email protected]/

James Clark (2):
perf pmu: Restore full PMU name wildcard support perf pmu: Don't de-duplicate core PMUs

 tools/perf/tests/pmu.c | 78 ++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/pmu.c  | 26 ++++++++++----
 2 files changed, 97 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 perf pmu: Restore full PMU name wildcard support

Commit b2b9d3a3f021 ("perf pmu: Support wildcards on pmu name in dynamic pmu events") gives the following example for wildcarding a subset of PMUs:

E.g., in a system with the following dynamic pmus:

        mypmu_0
        mypmu_1
        mypmu_2
        mypmu_4

perf stat -e mypmu_[01]/<config>/

Since commit f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()"), only "*" has been supported, removing the ability to subset PMUs, even though parse-events.l still supports ? and [] characters.

Fix it by using fnmatch() when any glob character is detected and add a test which covers that and other scenarios of perf_pmu__match_ignoring_suffix().

Fixes f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()")
---
 tools/perf/tests/pmu.c | 78 ++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/pmu.c  |  2 +-
 2 files changed, 79 insertions(+), 1 deletion(-)
tools/perf/tests/pmu.c +78 -0
index cc88b5920c3e..fd07331b2d6e 100644
@@ -437,12 +437,90 @@ static int test__name_cmp(struct test_suite *test __maybe_unused, int subtest __
 	return TEST_OK;
 }
 
+/**
+ * Test perf_pmu__match() that's used to search for a PMU given a name passed
+ * on the command line. The name that's passed may also be a filename type glob
+ * match.
+ */
+static int test__pmu_match(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
+{
+	struct perf_pmu test_pmu;
+
+	test_pmu.name = "pmuname";
+	TEST_ASSERT_EQUAL("Exact match", perf_pmu__match(&test_pmu, "pmuname"),	     true);
+	TEST_ASSERT_EQUAL("Longer token", perf_pmu__match(&test_pmu, "longertoken"), false);
+	TEST_ASSERT_EQUAL("Shorter token", perf_pmu__match(&test_pmu, "pmu"),	     false);
+
+	test_pmu.name = "pmuname_10";
+	TEST_ASSERT_EQUAL("Diff suffix_", perf_pmu__match(&test_pmu, "pmuname_2"),  false);
+	TEST_ASSERT_EQUAL("Sub suffix_",  perf_pmu__match(&test_pmu, "pmuname_1"),  true);
+	TEST_ASSERT_EQUAL("Same suffix_", perf_pmu__match(&test_pmu, "pmuname_10"), true);
+	TEST_ASSERT_EQUAL("No suffix_",   perf_pmu__match(&test_pmu, "pmuname"),    true);
+	TEST_ASSERT_EQUAL("Underscore_",  perf_pmu__match(&test_pmu, "pmuname_"),   true);
+	TEST_ASSERT_EQUAL("Substring_",   perf_pmu__match(&test_pmu, "pmuna"),      false);
+
+	test_pmu.name = "pmuname_ab23";
+	TEST_ASSERT_EQUAL("Diff suffix hex_", perf_pmu__match(&test_pmu, "pmuname_2"),    false);
+	TEST_ASSERT_EQUAL("Sub suffix hex_",  perf_pmu__match(&test_pmu, "pmuname_ab"),   true);
+	TEST_ASSERT_EQUAL("Same suffix hex_", perf_pmu__match(&test_pmu, "pmuname_ab23"), true);
+	TEST_ASSERT_EQUAL("No suffix hex_",   perf_pmu__match(&test_pmu, "pmuname"),      true);
+	TEST_ASSERT_EQUAL("Underscore hex_",  perf_pmu__match(&test_pmu, "pmuname_"),     true);
+	TEST_ASSERT_EQUAL("Substring hex_",   perf_pmu__match(&test_pmu, "pmuna"),	 false);
+
+	test_pmu.name = "pmuname10";
+	TEST_ASSERT_EQUAL("Diff suffix", perf_pmu__match(&test_pmu, "pmuname2"),  false);
+	TEST_ASSERT_EQUAL("Sub suffix",  perf_pmu__match(&test_pmu, "pmuname1"),  true);
+	TEST_ASSERT_EQUAL("Same suffix", perf_pmu__match(&test_pmu, "pmuname10"), true);
+	TEST_ASSERT_EQUAL("No suffix",   perf_pmu__match(&test_pmu, "pmuname"),   true);
+	TEST_ASSERT_EQUAL("Underscore",  perf_pmu__match(&test_pmu, "pmuname_"),  false);
+	TEST_ASSERT_EQUAL("Substring",   perf_pmu__match(&test_pmu, "pmuna"),     false);
+
+	test_pmu.name = "pmunameab23";
+	TEST_ASSERT_EQUAL("Diff suffix hex", perf_pmu__match(&test_pmu, "pmuname2"),    false);
+	TEST_ASSERT_EQUAL("Sub suffix hex",  perf_pmu__match(&test_pmu, "pmunameab"),   true);
+	TEST_ASSERT_EQUAL("Same suffix hex", perf_pmu__match(&test_pmu, "pmunameab23"), true);
+	TEST_ASSERT_EQUAL("No suffix hex",   perf_pmu__match(&test_pmu, "pmuname"),     true);
+	TEST_ASSERT_EQUAL("Underscore hex",  perf_pmu__match(&test_pmu, "pmuname_"),    false);
+	TEST_ASSERT_EQUAL("Substring hex",   perf_pmu__match(&test_pmu, "pmuna"),	false);
+
+	/*
+	 * 2 hex chars or less are not considered suffixes so it shouldn't be
+	 * possible to wildcard by skipping the suffix. Therefore there are more
+	 * false results here than above.
+	 */
+	test_pmu.name = "pmuname_a3";
+	TEST_ASSERT_EQUAL("Diff suffix 2 hex_", perf_pmu__match(&test_pmu, "pmuname_2"),  false);
+	/*
+	 * This one should be false, but because pmuname_a3 ends in 3 which is
+	 * decimal, it's not possible to determine if it's a short hex suffix or
+	 * a normal decimal suffix following text. And we want to match on any
+	 * length of decimal suffix. Run the test anyway and expect the wrong
+	 * result. And slightly fuzzy matching shouldn't do too much harm.
+	 */
+	TEST_ASSERT_EQUAL("Sub suffix 2 hex_",  perf_pmu__match(&test_pmu, "pmuname_a"),  true);
+	TEST_ASSERT_EQUAL("Same suffix 2 hex_", perf_pmu__match(&test_pmu, "pmuname_a3"), true);
+	TEST_ASSERT_EQUAL("No suffix 2 hex_",   perf_pmu__match(&test_pmu, "pmuname"),    false);
+	TEST_ASSERT_EQUAL("Underscore 2 hex_",  perf_pmu__match(&test_pmu, "pmuname_"),   false);
+	TEST_ASSERT_EQUAL("Substring 2 hex_",   perf_pmu__match(&test_pmu, "pmuna"),	  false);
+
+	test_pmu.name = "pmuname_5";
+	TEST_ASSERT_EQUAL("Glob 1", perf_pmu__match(&test_pmu, "pmu*"),		   true);
+	TEST_ASSERT_EQUAL("Glob 2", perf_pmu__match(&test_pmu, "nomatch*"),	   false);
+	TEST_ASSERT_EQUAL("Seq 1",  perf_pmu__match(&test_pmu, "pmuname_[12345]"), true);
+	TEST_ASSERT_EQUAL("Seq 2",  perf_pmu__match(&test_pmu, "pmuname_[67890]"), false);
+	TEST_ASSERT_EQUAL("? 1",    perf_pmu__match(&test_pmu, "pmuname_?"),	   true);
+	TEST_ASSERT_EQUAL("? 2",    perf_pmu__match(&test_pmu, "pmuname_1?"),	   false);
+
+	return TEST_OK;
+}
+
 static struct test_case tests__pmu[] = {
 	TEST_CASE("Parsing with PMU format directory", pmu_format),
 	TEST_CASE("Parsing with PMU event", pmu_events),
 	TEST_CASE("PMU event names", pmu_event_names),
 	TEST_CASE("PMU name combining", name_len),
 	TEST_CASE("PMU name comparison", name_cmp),
+	TEST_CASE("PMU cmdline match", pmu_match),
 	{	.name = NULL, }
 };
 
tools/perf/util/pmu.c +1 -1
index c94a91645b21..97d74fe6d816 100644
@@ -2150,7 +2150,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config,
 bool perf_pmu__match(const struct perf_pmu *pmu, const char *tok)
 {
 	const char *name = pmu->name;
-	bool need_fnmatch = strchr(tok, '*') != NULL;
+	bool need_fnmatch = strisglob(tok);
 
 	if (!strncmp(tok, "uncore_", 7))
 		tok += 7;
Signature · 1 line
2.34.1

A message that is not in the archive (sent off-list or not yet received): CAP-5=fUZkAs9h+fLiJOeL9p_K3auQcn30mXvXZWRYMchmT9ZPA@mail.gmail.com

#3 lore raw

Re: [PATCH 1/2] perf pmu: Restore full PMU name wildcard support

Quoted 27 lines · Ian Rogers
Quoted 17 lines · James Clark

Commit b2b9d3a3f021 ("perf pmu: Support wildcards on pmu name in dynamic pmu events") gives the following example for wildcarding a subset of PMUs:

E.g., in a system with the following dynamic pmus:

        mypmu_0
        mypmu_1
        mypmu_2
        mypmu_4

perf stat -e mypmu_[01]/<config>/

Since commit f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()"), only "*" has been supported, removing the ability to subset PMUs, even though parse-events.l still supports ? and [] characters.

Quoted 7 lines

Fix it by using fnmatch() when any glob character is detected and add a test which covers that and other scenarios of perf_pmu__match_ignoring_suffix().

Fixes f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()")
Quoted 7 lines

We use regular expression matching elsewhere rather than fnmatch. We can also precompile the matchers using lex. I'm not sure we shouldn't be looking for an opportunity to remove fnmatch rather than expand upon it.

Thanks,
Ian

Presumably you mean we can do the removal of fnmatch after this fix goes in, rather than instead of? Because this is a user facing change in behaviour but the removal of fnmatch would be an non-user facing code refactor?

It's technically not an "expansion" because we always used fnmatch and the linked commit hasn't made it to a release yet.

Quoted 169 lines

---

Quoted 166 lines
 tools/perf/tests/pmu.c | 78 ++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/pmu.c  |  2 +-
 2 files changed, 79 insertions(+), 1 deletion(-)
tools/perf/tests/pmu.c +0 -0
index cc88b5920c3e..fd07331b2d6e 100644
@@ -437,12 +437,90 @@ static int test__name_cmp(struct test_suite *test

__maybe_unused, int subtest __

        return TEST_OK;

}

+/**
+ * Test perf_pmu__match() that's used to search for a PMU given a name passed
+ * on the command line. The name that's passed may also be a filename type glob
+ * match.
+ */
+static int test__pmu_match(struct test_suite *test __maybe_unused, int subtest __maybe_unused)

tools/perf/tests/pmu.c +5 -0
+{
+       struct perf_pmu test_pmu;
+
+       test_pmu.name = "pmuname";
+       TEST_ASSERT_EQUAL("Exact match", perf_pmu__match(&test_pmu,

"pmuname"), true);
+ TEST_ASSERT_EQUAL("Longer token", perf_pmu__match(&test_pmu,
"longertoken"), false);
+ TEST_ASSERT_EQUAL("Shorter token", perf_pmu__match(&test_pmu,
"pmu"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmuname_10";
+       TEST_ASSERT_EQUAL("Diff suffix_", perf_pmu__match(&test_pmu,

"pmuname_2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix_", perf_pmu__match(&test_pmu,
"pmuname_1"), true);
+ TEST_ASSERT_EQUAL("Same suffix_", perf_pmu__match(&test_pmu,
"pmuname_10"), true);
+ TEST_ASSERT_EQUAL("No suffix_", perf_pmu__match(&test_pmu,
"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore_", perf_pmu__match(&test_pmu,
"pmuname_"), true);
+ TEST_ASSERT_EQUAL("Substring_", perf_pmu__match(&test_pmu,
"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmuname_ab23";
+       TEST_ASSERT_EQUAL("Diff suffix hex_", perf_pmu__match(&test_pmu,

"pmuname_2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix hex_", perf_pmu__match(&test_pmu,
"pmuname_ab"), true);
+ TEST_ASSERT_EQUAL("Same suffix hex_", perf_pmu__match(&test_pmu,
"pmuname_ab23"), true);
+ TEST_ASSERT_EQUAL("No suffix hex_", perf_pmu__match(&test_pmu,
"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore hex_", perf_pmu__match(&test_pmu,
"pmuname_"), true);
+ TEST_ASSERT_EQUAL("Substring hex_", perf_pmu__match(&test_pmu,
"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmuname10";
+       TEST_ASSERT_EQUAL("Diff suffix", perf_pmu__match(&test_pmu,

"pmuname2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix", perf_pmu__match(&test_pmu,
"pmuname1"), true);
+ TEST_ASSERT_EQUAL("Same suffix", perf_pmu__match(&test_pmu,
"pmuname10"), true);
+ TEST_ASSERT_EQUAL("No suffix", perf_pmu__match(&test_pmu,
"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore", perf_pmu__match(&test_pmu,
"pmuname_"), false);
+ TEST_ASSERT_EQUAL("Substring", perf_pmu__match(&test_pmu,
"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmunameab23";
+       TEST_ASSERT_EQUAL("Diff suffix hex", perf_pmu__match(&test_pmu,

"pmuname2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix hex", perf_pmu__match(&test_pmu,
"pmunameab"), true);
+ TEST_ASSERT_EQUAL("Same suffix hex", perf_pmu__match(&test_pmu,
"pmunameab23"), true);
+ TEST_ASSERT_EQUAL("No suffix hex", perf_pmu__match(&test_pmu,
"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore hex", perf_pmu__match(&test_pmu,
"pmuname_"), false);
+ TEST_ASSERT_EQUAL("Substring hex", perf_pmu__match(&test_pmu,
"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       /*
+        * 2 hex chars or less are not considered suffixes so it shouldn't

be
+ * possible to wildcard by skipping the suffix. Therefore there
are more

tools/perf/tests/pmu.c +4 -0
+        * false results here than above.
+        */
+       test_pmu.name = "pmuname_a3";
+       TEST_ASSERT_EQUAL("Diff suffix 2 hex_", perf_pmu__match(&test_pmu,

"pmuname_2"), false);

tools/perf/tests/pmu.c +2 -0
+       /*
+        * This one should be false, but because pmuname_a3 ends in 3

which is
+ * decimal, it's not possible to determine if it's a short hex
suffix or
+ * a normal decimal suffix following text. And we want to match on
any
+ * length of decimal suffix. Run the test anyway and expect the
wrong

tools/perf/tests/pmu.c +3 -0
+        * result. And slightly fuzzy matching shouldn't do too much harm.
+        */
+       TEST_ASSERT_EQUAL("Sub suffix 2 hex_",  perf_pmu__match(&test_pmu,

"pmuname_a"), true);
+ TEST_ASSERT_EQUAL("Same suffix 2 hex_", perf_pmu__match(&test_pmu,
"pmuname_a3"), true);
+ TEST_ASSERT_EQUAL("No suffix 2 hex_", perf_pmu__match(&test_pmu,
"pmuname"), false);
+ TEST_ASSERT_EQUAL("Underscore 2 hex_", perf_pmu__match(&test_pmu,
"pmuname_"), false);
+ TEST_ASSERT_EQUAL("Substring 2 hex_", perf_pmu__match(&test_pmu,
"pmuna"), false);

tools/perf/tests/pmu.c +4 -0
+
+       test_pmu.name = "pmuname_5";
+       TEST_ASSERT_EQUAL("Glob 1", perf_pmu__match(&test_pmu, "pmu*"),
        true);
+       TEST_ASSERT_EQUAL("Glob 2", perf_pmu__match(&test_pmu,

"nomatch*"), false);
+ TEST_ASSERT_EQUAL("Seq 1", perf_pmu__match(&test_pmu,
"pmuname_[12345]"), true);
+ TEST_ASSERT_EQUAL("Seq 2", perf_pmu__match(&test_pmu,
"pmuname_[67890]"), false);
+ TEST_ASSERT_EQUAL("? 1", perf_pmu__match(&test_pmu,
"pmuname_?"), true);
+ TEST_ASSERT_EQUAL("? 2", perf_pmu__match(&test_pmu,
"pmuname_1?"), false);

tools/perf/tests/pmu.c +5 -0
+
+       return TEST_OK;
+}
+
 static struct test_case tests__pmu[] = {
        TEST_CASE("Parsing with PMU format directory", pmu_format),
        TEST_CASE("Parsing with PMU event", pmu_events),
        TEST_CASE("PMU event names", pmu_event_names),
        TEST_CASE("PMU name combining", name_len),
        TEST_CASE("PMU name comparison", name_cmp),
+       TEST_CASE("PMU cmdline match", pmu_match),
        {       .name = NULL, }
 };
tools/perf/util/pmu.c +1 -1
index c94a91645b21..97d74fe6d816 100644
@@ -2150,7 +2150,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu
 *pmu, __u64 config,
 bool perf_pmu__match(const struct perf_pmu *pmu, const char *tok)
 {
        const char *name = pmu->name;
-       bool need_fnmatch = strchr(tok, '*') != NULL;
+       bool need_fnmatch = strisglob(tok);
 
        if (!strncmp(tok, "uncore_", 7))
                tok += 7;

--
2.34.1

A message that is not in the archive (sent off-list or not yet received): CAP-5=fUkZnG0gaJ_76Azn643DXZSq9xhpX=+U6Mjhtnko8PyLw@mail.gmail.com

#5 lore raw

Re: [PATCH 1/2] perf pmu: Restore full PMU name wildcard support

Quoted 49 lines · Ian Rogers
Quoted 38 lines · James Clark
Quoted 27 lines · Ian Rogers
Quoted 17 lines · James Clark

Commit b2b9d3a3f021 ("perf pmu: Support wildcards on pmu name in dynamic pmu events") gives the following example for wildcarding a subset of PMUs:

E.g., in a system with the following dynamic pmus:

        mypmu_0
        mypmu_1
        mypmu_2
        mypmu_4

perf stat -e mypmu_[01]/<config>/

Since commit f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()"), only "*" has been supported, removing the ability to subset PMUs, even though parse-events.l still supports ? and [] characters.

Quoted 7 lines

Fix it by using fnmatch() when any glob character is detected and add a test which covers that and other scenarios of perf_pmu__match_ignoring_suffix().

Fixes f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()")
Quoted 7 lines

We use regular expression matching elsewhere rather than fnmatch. We can also precompile the matchers using lex. I'm not sure we shouldn't be looking for an opportunity to remove fnmatch rather than expand upon it.

Thanks,
Ian

Quoted 8 lines

Presumably you mean we can do the removal of fnmatch after this fix goes in,
rather than instead of? Because this is a user facing change in behaviour but the removal of fnmatch would be an non-user facing code refactor?

It's technically not an "expansion" because we always used fnmatch and the linked commit hasn't made it to a release yet.

Quoted 8 lines

The main place the expansion gets added is parse-events.c, previously parse-events.y. If we're adding the expansion ourselves then we can choose the form we add it. Some coming servers will have 100s of PMUs and so I'm worried about the scanning cost when a PMU isn't specified.

Thanks,
Ian

I think I might not be following. If a PMU isn't specified then perf_pmu__match() is never called so no cost is incurred. I also don't add any new calls to fnmatch().

I only updated the gate on whether the existing fnmatch() is called from "*" to "*[?". So it only happens when one of those characters is in the PMU name, but it already happens when '*' is in the name.

But yes I agree about the potential performance issues. With a large number of PMUs if regex is faster we can update it to use that instead. But we should make sure the regex supports the same wildcarding options as before (unless we want to remove the range options, but that's a separate thing I suppose).

Quoted 180 lines
Quoted 179 lines
Quoted 85 lines

---

Quoted 83 lines
 tools/perf/tests/pmu.c | 78 ++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/pmu.c  |  2 +-
 2 files changed, 79 insertions(+), 1 deletion(-)
tools/perf/tests/pmu.c +0 -0
index cc88b5920c3e..fd07331b2d6e 100644
@@ -437,12 +437,90 @@ static int test__name_cmp(struct test_suite *test

__maybe_unused, int subtest __

        return TEST_OK;

}

+/**
+ * Test perf_pmu__match() that's used to search for a PMU given a name passed
+ * on the command line. The name that's passed may also be a filename type glob
+ * match.
+ */
+static int test__pmu_match(struct test_suite *test __maybe_unused, int subtest __maybe_unused)

tools/perf/tests/pmu.c +5 -0
+{
+       struct perf_pmu test_pmu;
+
+       test_pmu.name = "pmuname";
+       TEST_ASSERT_EQUAL("Exact match", perf_pmu__match(&test_pmu,

"pmuname"), true);
+ TEST_ASSERT_EQUAL("Longer token", perf_pmu__match(&test_pmu,
"longertoken"), false);
+ TEST_ASSERT_EQUAL("Shorter token", perf_pmu__match(&test_pmu,
"pmu"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmuname_10";
+       TEST_ASSERT_EQUAL("Diff suffix_", perf_pmu__match(&test_pmu,

"pmuname_2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix_", perf_pmu__match(&test_pmu,
"pmuname_1"), true);
+ TEST_ASSERT_EQUAL("Same suffix_", perf_pmu__match(&test_pmu,
"pmuname_10"), true);
+ TEST_ASSERT_EQUAL("No suffix_", perf_pmu__match(&test_pmu,
"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore_", perf_pmu__match(&test_pmu,
"pmuname_"), true);
+ TEST_ASSERT_EQUAL("Substring_", perf_pmu__match(&test_pmu,
"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmuname_ab23";
+       TEST_ASSERT_EQUAL("Diff suffix hex_", perf_pmu__match(&test_pmu,

"pmuname_2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix hex_", perf_pmu__match(&test_pmu,
"pmuname_ab"), true);
+ TEST_ASSERT_EQUAL("Same suffix hex_", perf_pmu__match(&test_pmu,
"pmuname_ab23"), true);
+ TEST_ASSERT_EQUAL("No suffix hex_", perf_pmu__match(&test_pmu,
"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore hex_", perf_pmu__match(&test_pmu,
"pmuname_"), true);
+ TEST_ASSERT_EQUAL("Substring hex_", perf_pmu__match(&test_pmu,
"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmuname10";
+       TEST_ASSERT_EQUAL("Diff suffix", perf_pmu__match(&test_pmu,

"pmuname2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix", perf_pmu__match(&test_pmu,
"pmuname1"), true);
+ TEST_ASSERT_EQUAL("Same suffix", perf_pmu__match(&test_pmu,
"pmuname10"), true);
+ TEST_ASSERT_EQUAL("No suffix", perf_pmu__match(&test_pmu,
"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore", perf_pmu__match(&test_pmu,
"pmuname_"), false);
+ TEST_ASSERT_EQUAL("Substring", perf_pmu__match(&test_pmu,
"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmunameab23";
+       TEST_ASSERT_EQUAL("Diff suffix hex", perf_pmu__match(&test_pmu,

"pmuname2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix hex", perf_pmu__match(&test_pmu,
"pmunameab"), true);
+ TEST_ASSERT_EQUAL("Same suffix hex", perf_pmu__match(&test_pmu,
"pmunameab23"), true);
+ TEST_ASSERT_EQUAL("No suffix hex", perf_pmu__match(&test_pmu,

Quoted 8 lines

tools/perf/tests/pmu.c static int test__name_cmp(struct test_suite *test __maybe_unused, int subtest __

Quoted 8 lines

"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore hex", perf_pmu__match(&test_pmu,
"pmuname_"), false);
+ TEST_ASSERT_EQUAL("Substring hex", perf_pmu__match(&test_pmu,
"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       /*
+        * 2 hex chars or less are not considered suffixes so it

shouldn't

Quoted 7 lines

tools/perf/tests/pmu.c static int test__name_cmp(struct test_suite *test __maybe_unused, int subtest __

Quoted 7 lines

be
+ * possible to wildcard by skipping the suffix. Therefore there
are more

tools/perf/tests/pmu.c +4 -0
+        * false results here than above.
+        */
+       test_pmu.name = "pmuname_a3";
+       TEST_ASSERT_EQUAL("Diff suffix 2 hex_",

perf_pmu__match(&test_pmu,

Quoted 7 lines
Quoted 7 lines

"pmuname_2"), false);

tools/perf/tests/pmu.c +2 -0
+       /*
+        * This one should be false, but because pmuname_a3 ends in 3

which is
+ * decimal, it's not possible to determine if it's a short hex
suffix or
+ * a normal decimal suffix following text. And we want to match

on

Quoted 4 lines
Quoted 4 lines

any
+ * length of decimal suffix. Run the test anyway and expect the
wrong
+ * result. And slightly fuzzy matching shouldn't do too much

harm.

Quoted 2 lines

tools/perf/tests/pmu.c

Quoted 2 lines
tools/perf/tests/pmu.c +2 -0
+        */
+       TEST_ASSERT_EQUAL("Sub suffix 2 hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines

"pmuname_a"), true);
+ TEST_ASSERT_EQUAL("Same suffix 2 hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines

"pmuname_a3"), true);
+ TEST_ASSERT_EQUAL("No suffix 2 hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines

"pmuname"), false);
+ TEST_ASSERT_EQUAL("Underscore 2 hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines

"pmuname_"), false);
+ TEST_ASSERT_EQUAL("Substring 2 hex_",

perf_pmu__match(&test_pmu,

Quoted 48 lines
Quoted 47 lines

"pmuna"), false);

tools/perf/tests/pmu.c +4 -0
+
+       test_pmu.name = "pmuname_5";
+       TEST_ASSERT_EQUAL("Glob 1", perf_pmu__match(&test_pmu, "pmu*"),
        true);
+       TEST_ASSERT_EQUAL("Glob 2", perf_pmu__match(&test_pmu,

"nomatch*"), false);
+ TEST_ASSERT_EQUAL("Seq 1", perf_pmu__match(&test_pmu,
"pmuname_[12345]"), true);
+ TEST_ASSERT_EQUAL("Seq 2", perf_pmu__match(&test_pmu,
"pmuname_[67890]"), false);
+ TEST_ASSERT_EQUAL("? 1", perf_pmu__match(&test_pmu,
"pmuname_?"), true);
+ TEST_ASSERT_EQUAL("? 2", perf_pmu__match(&test_pmu,
"pmuname_1?"), false);

tools/perf/tests/pmu.c +5 -0
+
+       return TEST_OK;
+}
+
 static struct test_case tests__pmu[] = {
        TEST_CASE("Parsing with PMU format directory", pmu_format),
        TEST_CASE("Parsing with PMU event", pmu_events),
        TEST_CASE("PMU event names", pmu_event_names),
        TEST_CASE("PMU name combining", name_len),
        TEST_CASE("PMU name comparison", name_cmp),
+       TEST_CASE("PMU cmdline match", pmu_match),
        {       .name = NULL, }
 };
tools/perf/util/pmu.c +1 -1
index c94a91645b21..97d74fe6d816 100644
@@ -2150,7 +2150,7 @@ void perf_pmu__warn_invalid_config(struct perf_pmu
 *pmu, __u64 config,
 bool perf_pmu__match(const struct perf_pmu *pmu, const char *tok)
 {
        const char *name = pmu->name;
-       bool need_fnmatch = strchr(tok, '*') != NULL;
+       bool need_fnmatch = strisglob(tok);
 
        if (!strncmp(tok, "uncore_", 7))
                tok += 7;

--
2.34.1

A message that is not in the archive (sent off-list or not yet received): CAP-5=fVqf0B+Fs8vSAvyPf7UpUo1U8HMkDbgb3csJ4s0O1kiog@mail.gmail.com

#7 lore raw

Re: [PATCH 1/2] perf pmu: Restore full PMU name wildcard support

Quoted 76 lines · Ian Rogers
Quoted 74 lines · James Clark
Quoted 8 lines · Ian Rogers
Quoted 6 lines · James Clark
Quoted 3 lines · Ian Rogers
Quoted 1 line · James Clark

Commit b2b9d3a3f021 ("perf pmu: Support wildcards on pmu name in

dynamic

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

pmu events") gives the following example for wildcarding a subset of PMUs:

E.g., in a system with the following dynamic pmus:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
        mypmu_0
        mypmu_1
        mypmu_2
        mypmu_4

perf stat -e mypmu_[01]/<config>/

Since commit f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()"),

only

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"*" has been supported, removing the ability to subset PMUs, even

though

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

parse-events.l still supports ? and [] characters.

Fix it by using fnmatch() when any glob character is detected and add

a

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

test which covers that and other scenarios of
perf_pmu__match_ignoring_suffix().

Fixes f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()")

We use regular expression matching elsewhere rather than fnmatch. We

can

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

also precompile the matchers using lex. I'm not sure we shouldn't be looking for an opportunity to remove fnmatch rather than expand upon

it.

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Thanks,
Ian

Presumably you mean we can do the removal of fnmatch after this fix goes in,
rather than instead of? Because this is a user facing change in

behaviour

Quoted 3 lines
Quoted 3 lines

but the removal of fnmatch would be an non-user facing code refactor?

It's technically not an "expansion" because we always used fnmatch and

the

Quoted 5 lines
Quoted 2 lines

linked commit hasn't made it to a release yet.

The main place the expansion gets added is parse-events.c, previously parse-events.y. If we're adding the expansion ourselves then we can

choose

Quoted 6 lines

the form we add it. Some coming servers will have 100s of PMUs and so I'm worried about the scanning cost when a PMU isn't specified.

Thanks,
Ian

I think I might not be following. If a PMU isn't specified then perf_pmu__match() is never called so no cost is incurred. I also don't add any new calls to fnmatch().

Quoted 8 lines
Quoted 4 lines

I only updated the gate on whether the existing fnmatch() is called from "*" to "*[?". So it only happens when one of those characters is in the PMU name, but it already happens when '*' is in the name.

Right. I'm not saying there is anything wrong in the change or an additional cost, what the issue is is that currently only really '*' requires fnmatch and that's because the event parsing adds it. It could

It's not only '*' that requires it, see the example I added in the commit message:

./perf stat -e mypmu_[01]/<config>/

'?' was supported before as well which could be useful.

Quoted 5 lines

similarly add '.*' if we did regular expression matching. By expanding what we pass to fnmatch from the command line the more committed we are to fnmatch rather than regular expressions - which is what we use everywhere else in the code. So maybe it was a feature that this wasn't working.

But we haven't had a release of Perf yet where more is passed to fnmatch(). Before f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()") everything was passed to fnmatch(). After that unreleased commit only things with '*' are. Now with this change only "*?[", so it's less not more.

I don't think there is any commitment to keep it, we can always remove fnmatch in the future. But it looks like a mistake to me because the title says "refactor" when it actually removes a feature.

Quoted 215 lines

Thanks,
Ian

But yes I agree about the potential performance issues. With a large

Quoted 207 lines

number of PMUs if regex is faster we can update it to use that instead. But we should make sure the regex supports the same wildcarding options as before (unless we want to remove the range options, but that's a separate thing I suppose).

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

---

Quoted 1 line
 tools/perf/tests/pmu.c | 78

++++++++++++++++++++++++++++++++++++++++++

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

tools/perf/tests/pmu.c static int test__name_cmp(struct test_suite

Quoted 8 lines
 tools/perf/util/pmu.c  |  2 +-
 2 files changed, 79 insertions(+), 1 deletion(-)
tools/perf/tests/pmu.c +0 -0
index cc88b5920c3e..fd07331b2d6e 100644
@@ -437,12 +437,90 @@ static int test__name_cmp(struct test_suite

*test

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

__maybe_unused, int subtest __

        return TEST_OK;

}

+/**
+ * Test perf_pmu__match() that's used to search for a PMU given a

name

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

passed
+ * on the command line. The name that's passed may also be a filename type glob
+ * match.
+ */
+static int test__pmu_match(struct test_suite *test __maybe_unused,

int

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

subtest __maybe_unused)

tools/perf/tests/pmu.c +5 -0
+{
+       struct perf_pmu test_pmu;
+
+       test_pmu.name = "pmuname";
+       TEST_ASSERT_EQUAL("Exact match", perf_pmu__match(&test_pmu,

"pmuname"), true);
+ TEST_ASSERT_EQUAL("Longer token", perf_pmu__match(&test_pmu,
"longertoken"), false);
+ TEST_ASSERT_EQUAL("Shorter token", perf_pmu__match(&test_pmu,
"pmu"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmuname_10";
+       TEST_ASSERT_EQUAL("Diff suffix_", perf_pmu__match(&test_pmu,

"pmuname_2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix_", perf_pmu__match(&test_pmu,
"pmuname_1"), true);
+ TEST_ASSERT_EQUAL("Same suffix_", perf_pmu__match(&test_pmu,
"pmuname_10"), true);
+ TEST_ASSERT_EQUAL("No suffix_", perf_pmu__match(&test_pmu,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

tools/perf/tests/pmu.c static int test__name_cmp(struct test_suite *test __maybe_unused, int subtest __

Quoted 8 lines

"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore_", perf_pmu__match(&test_pmu,
"pmuname_"), true);
+ TEST_ASSERT_EQUAL("Substring_", perf_pmu__match(&test_pmu,
"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmuname_ab23";
+       TEST_ASSERT_EQUAL("Diff suffix hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"pmuname_2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"pmuname_ab"), true);
+ TEST_ASSERT_EQUAL("Same suffix hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"pmuname_ab23"), true);
+ TEST_ASSERT_EQUAL("No suffix hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"pmuname_"), true);
+ TEST_ASSERT_EQUAL("Substring hex_",

perf_pmu__match(&test_pmu,

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmuname10";
+       TEST_ASSERT_EQUAL("Diff suffix", perf_pmu__match(&test_pmu,

"pmuname2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix", perf_pmu__match(&test_pmu,
"pmuname1"), true);
+ TEST_ASSERT_EQUAL("Same suffix", perf_pmu__match(&test_pmu,
"pmuname10"), true);
+ TEST_ASSERT_EQUAL("No suffix", perf_pmu__match(&test_pmu,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

tools/perf/tests/pmu.c static int test__name_cmp(struct test_suite *test __maybe_unused, int subtest __

Quoted 8 lines

"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore", perf_pmu__match(&test_pmu,
"pmuname_"), false);
+ TEST_ASSERT_EQUAL("Substring", perf_pmu__match(&test_pmu,
"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmunameab23";
+       TEST_ASSERT_EQUAL("Diff suffix hex",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"pmuname2"), false);
+ TEST_ASSERT_EQUAL("Sub suffix hex",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"pmunameab"), true);
+ TEST_ASSERT_EQUAL("Same suffix hex",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"pmunameab23"), true);
+ TEST_ASSERT_EQUAL("No suffix hex",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"pmuname"), true);
+ TEST_ASSERT_EQUAL("Underscore hex",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

"pmuname_"), false);
+ TEST_ASSERT_EQUAL("Substring hex",

perf_pmu__match(&test_pmu,

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

tools/perf/tests/pmu.c static int test__name_cmp(struct test_suite *test __maybe_unused, int subtest __

Quoted 4 lines

"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       /*
+        * 2 hex chars or less are not considered suffixes so it

shouldn't

Quoted 2 lines
Quoted 2 lines

be
+ * possible to wildcard by skipping the suffix. Therefore

there

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

are more

tools/perf/tests/pmu.c +4 -0
+        * false results here than above.
+        */
+       test_pmu.name = "pmuname_a3";
+       TEST_ASSERT_EQUAL("Diff suffix 2 hex_",
Quoted 8 lines
Quoted 8 lines

perf_pmu__match(&test_pmu,

Quoted 7 lines
Quoted 7 lines

"pmuname_2"), false);

tools/perf/tests/pmu.c +2 -0
+       /*
+        * This one should be false, but because pmuname_a3 ends in 3

which is
+ * decimal, it's not possible to determine if it's a short hex
suffix or
+ * a normal decimal suffix following text. And we want to

match

Quoted 3 lines
Quoted 3 lines

on

Quoted 2 lines
Quoted 2 lines

any
+ * length of decimal suffix. Run the test anyway and expect

the

Quoted 14 lines
Quoted 14 lines
Quoted 2 lines
Quoted 2 lines

wrong
+ * result. And slightly fuzzy matching shouldn't do too much

harm.

Quoted 2 lines

tools/perf/tests/pmu.c

Quoted 2 lines
tools/perf/tests/pmu.c +2 -0
+        */
+       TEST_ASSERT_EQUAL("Sub suffix 2 hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines

"pmuname_a"), true);
+ TEST_ASSERT_EQUAL("Same suffix 2 hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines

"pmuname_a3"), true);
+ TEST_ASSERT_EQUAL("No suffix 2 hex_",

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines

"pmuname"), false);
+ TEST_ASSERT_EQUAL("Underscore 2 hex_",

Quoted 8 lines
Quoted 8 lines

perf_pmu__match(&test_pmu,

Quoted 2 lines
Quoted 2 lines

"pmuname_"), false);
+ TEST_ASSERT_EQUAL("Substring 2 hex_",

perf_pmu__match(&test_pmu,

Quoted 4 lines
Quoted 4 lines

"pmuna"), false);

tools/perf/tests/pmu.c +3 -0
+
+       test_pmu.name = "pmuname_5";
+       TEST_ASSERT_EQUAL("Glob 1", perf_pmu__match(&test_pmu,

"pmu*"),

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
        true);

+ TEST_ASSERT_EQUAL("Glob 2", perf_pmu__match(&test_pmu,
"nomatch*"), false);
+ TEST_ASSERT_EQUAL("Seq 1", perf_pmu__match(&test_pmu,
"pmuname_[12345]"), true);
+ TEST_ASSERT_EQUAL("Seq 2", perf_pmu__match(&test_pmu,
"pmuname_[67890]"), false);
+ TEST_ASSERT_EQUAL("? 1", perf_pmu__match(&test_pmu,
"pmuname_?"), true);
+ TEST_ASSERT_EQUAL("? 2", perf_pmu__match(&test_pmu,
"pmuname_1?"), false);

tools/perf/tests/pmu.c +5 -0
+
+       return TEST_OK;
+}
+
 static struct test_case tests__pmu[] = {
        TEST_CASE("Parsing with PMU format directory", pmu_format),
        TEST_CASE("Parsing with PMU event", pmu_events),
        TEST_CASE("PMU event names", pmu_event_names),
        TEST_CASE("PMU name combining", name_len),
        TEST_CASE("PMU name comparison", name_cmp),
+       TEST_CASE("PMU cmdline match", pmu_match),
        {       .name = NULL, }
 };
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

tools/perf/util/pmu.c void perf_pmu__warn_invalid_config(struct

Quoted 5 lines
tools/perf/util/pmu.c +0 -0
index c94a91645b21..97d74fe6d816 100644
@@ -2150,7 +2150,7 @@ void perf_pmu__warn_invalid_config(struct

perf_pmu

Quoted 16 lines
Quoted 15 lines
Quoted 14 lines
Quoted 13 lines
tools/perf/util/pmu.c +1 -1
 *pmu, __u64 config,
 bool perf_pmu__match(const struct perf_pmu *pmu, const char *tok)
 {
        const char *name = pmu->name;
-       bool need_fnmatch = strchr(tok, '*') != NULL;
+       bool need_fnmatch = strisglob(tok);
 
        if (!strncmp(tok, "uncore_", 7))
                tok += 7;

--
2.34.1

maintainer
#8 lore raw

Re: [PATCH 1/2] perf pmu: Restore full PMU name wildcard support

Quoted 101 lines · James Clark
Quoted 76 lines · Ian Rogers
Quoted 74 lines · James Clark
Quoted 8 lines · Ian Rogers
Quoted 6 lines · James Clark
Quoted 3 lines · Ian Rogers
Quoted 1 line · James Clark

Commit b2b9d3a3f021 ("perf pmu: Support wildcards on pmu name in

dynamic

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

pmu events") gives the following example for wildcarding a subset of PMUs:

E.g., in a system with the following dynamic pmus:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
        mypmu_0
        mypmu_1
        mypmu_2
        mypmu_4

perf stat -e mypmu_[01]/<config>/

Since commit f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()"),

only

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"*" has been supported, removing the ability to subset PMUs, even

though

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

parse-events.l still supports ? and [] characters.

Fix it by using fnmatch() when any glob character is detected and add

a

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

test which covers that and other scenarios of
perf_pmu__match_ignoring_suffix().

Fixes f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()")

We use regular expression matching elsewhere rather than fnmatch. We

can

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

also precompile the matchers using lex. I'm not sure we shouldn't be looking for an opportunity to remove fnmatch rather than expand upon

it.

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Thanks,
Ian

Presumably you mean we can do the removal of fnmatch after this fix goes in,
rather than instead of? Because this is a user facing change in

behaviour

Quoted 3 lines
Quoted 3 lines

but the removal of fnmatch would be an non-user facing code refactor?

It's technically not an "expansion" because we always used fnmatch and

the

Quoted 5 lines
Quoted 2 lines

linked commit hasn't made it to a release yet.

The main place the expansion gets added is parse-events.c, previously parse-events.y. If we're adding the expansion ourselves then we can

choose

Quoted 6 lines

the form we add it. Some coming servers will have 100s of PMUs and so I'm worried about the scanning cost when a PMU isn't specified.

Thanks,
Ian

I think I might not be following. If a PMU isn't specified then perf_pmu__match() is never called so no cost is incurred. I also don't add any new calls to fnmatch().

Quoted 8 lines
Quoted 4 lines

I only updated the gate on whether the existing fnmatch() is called from "*" to "*[?". So it only happens when one of those characters is in the PMU name, but it already happens when '*' is in the name.

Right. I'm not saying there is anything wrong in the change or an additional cost, what the issue is is that currently only really '*' requires fnmatch and that's because the event parsing adds it. It could

It's not only '*' that requires it, see the example I added in the commit message:

./perf stat -e mypmu_[01]/<config>/

'?' was supported before as well which could be useful.

Quoted 5 lines

similarly add '.*' if we did regular expression matching. By expanding what we pass to fnmatch from the command line the more committed we are to fnmatch rather than regular expressions - which is what we use everywhere else in the code. So maybe it was a feature that this wasn't working.

Quoted 8 lines

But we haven't had a release of Perf yet where more is passed to fnmatch(). Before f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()") everything was passed to fnmatch(). After that unreleased commit only things with '*' are. Now with this change only "*?[", so it's less not more.

I don't think there is any commitment to keep it, we can always remove fnmatch in the future. But it looks like a mistake to me because the title says "refactor" when it actually removes a feature.

Ian, are you ok with this now?

Thanks,
Namhyung

#9 lore raw

Re: [PATCH 1/2] perf pmu: Restore full PMU name wildcard support

Quoted 107 lines · Namhyung Kim
Quoted 106 lines · James Clark
Quoted 76 lines · Ian Rogers
Quoted 74 lines · James Clark
Quoted 8 lines · Ian Rogers
Quoted 6 lines · James Clark
Quoted 3 lines · Ian Rogers
Quoted 1 line · James Clark

Commit b2b9d3a3f021 ("perf pmu: Support wildcards on pmu name in

dynamic

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

pmu events") gives the following example for wildcarding a subset of PMUs:

E.g., in a system with the following dynamic pmus:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
        mypmu_0
        mypmu_1
        mypmu_2
        mypmu_4

perf stat -e mypmu_[01]/<config>/

Since commit f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()"),

only

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"*" has been supported, removing the ability to subset PMUs, even

though

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

parse-events.l still supports ? and [] characters.

Fix it by using fnmatch() when any glob character is detected and add

a

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

test which covers that and other scenarios of
perf_pmu__match_ignoring_suffix().

Fixes f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()")

We use regular expression matching elsewhere rather than fnmatch. We

can

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

also precompile the matchers using lex. I'm not sure we shouldn't be looking for an opportunity to remove fnmatch rather than expand upon

it.

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

Thanks,
Ian

Presumably you mean we can do the removal of fnmatch after this fix goes in,
rather than instead of? Because this is a user facing change in

behaviour

Quoted 3 lines
Quoted 3 lines

but the removal of fnmatch would be an non-user facing code refactor?

It's technically not an "expansion" because we always used fnmatch and

the

Quoted 5 lines
Quoted 2 lines

linked commit hasn't made it to a release yet.

The main place the expansion gets added is parse-events.c, previously parse-events.y. If we're adding the expansion ourselves then we can

choose

Quoted 6 lines

the form we add it. Some coming servers will have 100s of PMUs and so I'm worried about the scanning cost when a PMU isn't specified.

Thanks,
Ian

I think I might not be following. If a PMU isn't specified then perf_pmu__match() is never called so no cost is incurred. I also don't add any new calls to fnmatch().

Quoted 8 lines
Quoted 4 lines

I only updated the gate on whether the existing fnmatch() is called from "*" to "*[?". So it only happens when one of those characters is in the PMU name, but it already happens when '*' is in the name.

Right. I'm not saying there is anything wrong in the change or an additional cost, what the issue is is that currently only really '*' requires fnmatch and that's because the event parsing adds it. It could

It's not only '*' that requires it, see the example I added in the commit message:

./perf stat -e mypmu_[01]/<config>/

'?' was supported before as well which could be useful.

Quoted 5 lines

similarly add '.*' if we did regular expression matching. By expanding what we pass to fnmatch from the command line the more committed we are to fnmatch rather than regular expressions - which is what we use everywhere else in the code. So maybe it was a feature that this wasn't working.

But we haven't had a release of Perf yet where more is passed to fnmatch(). Before f91fa2ae6360 ("perf pmu: Refactor perf_pmu__match()") everything was passed to fnmatch(). After that unreleased commit only things with '*' are. Now with this change only "*?[", so it's less not more.

Quoted 9 lines
Quoted 3 lines

I don't think there is any commitment to keep it, we can always remove fnmatch in the future. But it looks like a mistake to me because the title says "refactor" when it actually removes a feature.

Ian, are you ok with this now?

Thanks,
Namhyung

I don't feel too strongly about this one, unless anyone really is still using my_pmu[01].

For patch 2 I was going to resend with Ian's suggested-by tag, but I think we should try to get that one in as a test is failing.

#10 lore raw

PATCH 2/2 perf pmu: Don't de-duplicate core PMUs

In reply to James Clark

Arm PMUs have a suffix, either a single decimal (armv8_pmuv3_0) or 3 hex digits which (armv8_cortex_a53) which Perf assumes are both strippable suffixes for the purposes of deduplication. S390 "cpum_cf" is a similarly suffixed core PMU but is only two characters so is not treated as strippable because the rules are a minimum of 3 hex characters or 1 decimal character.

There are two paths involved in listing PMU events:

* HW/cache event printing assumes core PMUs don't have suffixes so doesn't try to strip.
* Sysfs PMU events share the printing function with uncore PMUs which strips.

This results in slightly inconsistent Perf list behavior if a core PMU has a suffix:

  # perf list
  ...
  armv8_pmuv3_0/branch-load-misses/
  armv8_pmuv3/l3d_cache_wb/          [Kernel PMU event]
  ...

Fix it by partially reverting back to the old list behavior where stripping was only done for uncore PMUs. For example commit 8d9f5146f5da ("perf pmus: Sort pmus by name then suffix") mentions that only PMUs starting 'uncore_' are considered to have a potential suffix. This change doesn't go back that far, but does only strip PMUs that are !is_core. This keeps the desirable behavior where the many possibly duplicated uncore PMUs aren't repeated, but it doesn't break listing for core PMUs.

Searching for a PMU continues to use the new stripped comparison functions, meaning that it's still possible to request an event by specifying the common part of a PMU name, or even open events on multiple similarly named PMUs. For example:

  # perf stat -e armv8_cortex/inst_retired/
  5777173628      armv8_cortex_a53/inst_retired/          (99.93%)
  7469626951      armv8_cortex_a57/inst_retired/          (49.88%)
Fixes 3241d46f5f54 ("perf pmus: Sort/merge/aggregate PMUs like mrvl_ddr_pmu")
---
 tools/perf/util/pmu.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
tools/perf/util/pmu.c +18 -6
index 97d74fe6d816..b73946ba9d05 100644
@@ -847,6 +847,22 @@ __weak const struct pmu_metrics_table *pmu_metrics_table__find(void)
 	return perf_pmu__find_metrics_table(NULL);
 }
 
+/**
+ * Return the length of the PMU name not including the suffix for uncore PMUs.
+ *
+ * We want to deduplicate many similar uncore PMUs by stripping their suffixes,
+ * but there are never going to be too many core PMUs and the suffixes might be
+ * interesting. "arm_cortex_a53" vs "arm_cortex_a57" or "cpum_cf" for example.
+ *
+ * @skip_duplicate_pmus: False in verbose mode so all uncore PMUs are visible
+ */
+static size_t pmu_deduped_name_len(const struct perf_pmu *pmu, bool skip_duplicate_pmus)
+{
+	return skip_duplicate_pmus && !pmu->is_core
+		? pmu_name_len_no_suffix(pmu->name)
+		: strlen(pmu->name);
+}
+
 /**
  * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any
  *                                   trailing suffix? The Suffix must be in form
@@ -1796,9 +1812,7 @@ static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
 			  const struct perf_pmu_alias *alias, bool skip_duplicate_pmus)
 {
 	struct parse_events_term *term;
-	size_t pmu_name_len = skip_duplicate_pmus
-		? pmu_name_len_no_suffix(pmu->name)
-		: strlen(pmu->name);
+	size_t pmu_name_len = pmu_deduped_name_len(pmu, skip_duplicate_pmus);
 	int used = snprintf(buf, len, "%.*s/%s", (int)pmu_name_len, pmu->name, alias->name);
 
 	list_for_each_entry(term, &alias->terms.terms, list) {
@@ -1839,9 +1853,7 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
 		size_t buf_used, pmu_name_len;
 
 		info.pmu_name = event->pmu_name ?: pmu->name;
-		pmu_name_len = skip_duplicate_pmus
-			? pmu_name_len_no_suffix(info.pmu_name)
-			: strlen(info.pmu_name);
+		pmu_name_len = pmu_deduped_name_len(pmu, skip_duplicate_pmus);
 		info.alias = NULL;
 		if (event->desc) {
 			info.name = event->name;
Signature · 1 line
2.34.1

A message that is not in the archive (sent off-list or not yet received): CAP-5=fWzmz+_9-Rz1xPwN0sniGvhyiRtrR-OCqAJgiWybpoCXg@mail.gmail.com

#12 lore raw

Re: [PATCH 2/2] perf pmu: Don't de-duplicate core PMUs

Quoted 49 lines · Ian Rogers
Quoted 37 lines · James Clark

Arm PMUs have a suffix, either a single decimal (armv8_pmuv3_0) or 3 hex digits which (armv8_cortex_a53) which Perf assumes are both strippable suffixes for the purposes of deduplication. S390 "cpum_cf" is a similarly suffixed core PMU but is only two characters so is not treated as strippable because the rules are a minimum of 3 hex characters or 1 decimal character.

There are two paths involved in listing PMU events:

* HW/cache event printing assumes core PMUs don't have suffixes so doesn't try to strip.
* Sysfs PMU events share the printing function with uncore PMUs which strips.

This results in slightly inconsistent Perf list behavior if a core PMU has a suffix:

  # perf list
  ...
  armv8_pmuv3_0/branch-load-misses/
  armv8_pmuv3/l3d_cache_wb/          [Kernel PMU event]
  ...

Fix it by partially reverting back to the old list behavior where stripping was only done for uncore PMUs. For example commit 8d9f5146f5da ("perf pmus: Sort pmus by name then suffix") mentions that only PMUs starting 'uncore_' are considered to have a potential suffix. This change doesn't go back that far, but does only strip PMUs that are !is_core. This keeps the desirable behavior where the many possibly duplicated uncore PMUs aren't repeated, but it doesn't break listing for core PMUs.

Searching for a PMU continues to use the new stripped comparison functions, meaning that it's still possible to request an event by specifying the common part of a PMU name, or even open events on multiple similarly named PMUs. For example:

Quoted 9 lines
  # perf stat -e armv8_cortex/inst_retired/
  5777173628      armv8_cortex_a53/inst_retired/          (99.93%)
  7469626951      armv8_cortex_a57/inst_retired/          (49.88%)
Fixes 3241d46f5f54 ("perf pmus: Sort/merge/aggregate PMUs like

mrvl_ddr_pmu")

Quoted 4 lines

Should this have my suggested by tag?

Thanks,
Ian

Yep, you're right I can add it

Quoted 74 lines

---

Quoted 71 lines
 tools/perf/util/pmu.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)
tools/perf/util/pmu.c +2 -0
index 97d74fe6d816..b73946ba9d05 100644
@@ -847,6 +847,22 @@ __weak const struct pmu_metrics_table
 *pmu_metrics_table__find(void)
        return perf_pmu__find_metrics_table(NULL);
 }
 
+/**
+ * Return the length of the PMU name not including the suffix for uncore

PMUs.
+ *
+ * We want to deduplicate many similar uncore PMUs by stripping their suffixes,
+ * but there are never going to be too many core PMUs and the suffixes might be
+ * interesting. "arm_cortex_a53" vs "arm_cortex_a57" or "cpum_cf" for example.
+ *
+ * @skip_duplicate_pmus: False in verbose mode so all uncore PMUs are visible
+ */
+static size_t pmu_deduped_name_len(const struct perf_pmu *pmu, bool skip_duplicate_pmus)

tools/perf/util/pmu.c +6 -0
+{
+       return skip_duplicate_pmus && !pmu->is_core
+               ? pmu_name_len_no_suffix(pmu->name)
+               : strlen(pmu->name);
+}
+
 /**
  * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring

any
* trailing suffix? The Suffix must be
in form

tools/perf/util/pmu.c +1 -3
@@ -1796,9 +1812,7 @@ static char *format_alias(char *buf, int len, const
 struct perf_pmu *pmu,
                          const struct perf_pmu_alias *alias, bool
 skip_duplicate_pmus)
 {
        struct parse_events_term *term;
-       size_t pmu_name_len = skip_duplicate_pmus
-               ? pmu_name_len_no_suffix(pmu->name)
-               : strlen(pmu->name);
+       size_t pmu_name_len = pmu_deduped_name_len(pmu,
 skip_duplicate_pmus);
        int used = snprintf(buf, len, "%.*s/%s", (int)pmu_name_len,

pmu->name, alias->name);

        list_for_each_entry(term, &alias->terms.terms, list) {
tools/perf/util/pmu.c +1 -3
@@ -1839,9 +1853,7 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu,
 bool skip_duplicate_pmus,
                size_t buf_used, pmu_name_len;
 
                info.pmu_name = event->pmu_name ?: pmu->name;
-               pmu_name_len = skip_duplicate_pmus
-                       ? pmu_name_len_no_suffix(info.pmu_name)
-                       : strlen(info.pmu_name);
+               pmu_name_len = pmu_deduped_name_len(pmu,
 skip_duplicate_pmus);
                info.alias = NULL;
                if (event->desc) {
                        info.name = event->name;

--
2.34.1