Skip to contentSkip to conversation

perf topdown: Don't require config1==0 for a slots event

PATCH In review Zide Chen 5 participants 9 messages Sep 16 – Sep 18 perf events

lore
Share & tools
lore

Zide Chen

author
#0 lore raw

PATCH 1/1 perf topdown: Don't require config1==0 for a slots event

arch_is_topdown_slots() requires config1 == 0, so it misidentified "cpu/slots,metrics_clear=1/" as not being the slots event, and an additional slots event gets inserted, which fails event scheduling since the group now needs two fixed counter 3.

  $ perf stat -e "{cpu/slots,metrics_clear=1/,cpu/topdown-retiring/}" -- sleep 1
  WARNING: events were regrouped to match PMUs
       <not counted>      slots
     <not supported>      cpu/slots,metrics_clear=1/
       <not counted>      cpu/topdown-retiring/

Drop the config1 == 0 check from arch_is_topdown_slots(). This is
safe: config1 has no bearing on the event's identifying config value (TOPDOWN_SLOTS, 0x0400).

The attr.type == PERF_TYPE_RAW check is what actually matters: only the core PMU is registered with PERF_TYPE_RAW, and on x86, 0x400 is guaranteed to be slots event on core CPU.

arch_is_topdown_metrics() keeps its own config1 == 0 check, since genuine metrics events have no "metrics_clear" and config1 remains 0.

Add a test verifying that an explicit "slots,metrics_clear=1" event in a group does not trigger an extra slots event injection.

Fixes 5b546de9cc17 ("perf topdown: Use attribute to see an event is a topdown metic or slots")
---
 tools/perf/arch/x86/tests/topdown.c | 28 ++++++++++++++++++++++++++++
 tools/perf/arch/x86/util/topdown.c  |  3 +--
 2 files changed, 29 insertions(+), 2 deletions(-)
tools/perf/arch/x86/tests/topdown.c +28 -0
index 2b6f47ce4932..b1ce9f83db00 100644
@@ -229,10 +229,38 @@ static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu
 	return TEST_OK;
 }
 
+/*
+ * An explicit "slots,metrics_clear=1" event is still the slots event and
+ * must not cause an extra slots event to be injected into the group.
+ */
+static int test__x86_topdown_metrics_clear(struct test_suite *test __maybe_unused,
+					   int subtest __maybe_unused)
+{
+	struct perf_pmu *pmu;
+	char event_str[128];
+	int ret;
+
+	if (!topdown_sys_has_perf_metrics())
+		return TEST_OK;
+
+	pmu = perf_pmus__find_by_type(PERF_TYPE_RAW);
+	if (!pmu || !perf_pmu__has_format(pmu, "metrics_clear"))
+		return TEST_OK;
+
+	snprintf(event_str, sizeof(event_str),
+		 "{%s/slots,metrics_clear=1/,%s/topdown-retiring/}",
+		 pmu->name, pmu->name);
+	ret = test_sort(event_str, 2, 1);
+	TEST_ASSERT_EQUAL("explicit metrics_clear slots event isn't duplicated", ret, TEST_OK);
+
+	return TEST_OK;
+}
+
 static struct test_case x86_topdown_tests[] = {
 	TEST_CASE("topdown events", x86_topdown),
 	TEST_CASE("topdown sorting", x86_topdown_sorting),
 	TEST_CASE("topdown slots injection", x86_topdown_slots_injection),
+	TEST_CASE("topdown metrics_clear no extra slots", x86_topdown_metrics_clear),
 	{ .name = NULL, }
 };
 
tools/perf/arch/x86/util/topdown.c +1 -2
index bafd285119d7..64bc64e335a2 100644
@@ -37,8 +37,7 @@ bool topdown_sys_has_perf_metrics(void)
 bool arch_is_topdown_slots(const struct evsel *evsel)
 {
 	return evsel->core.attr.type == PERF_TYPE_RAW &&
-	       evsel->core.attr.config == TOPDOWN_SLOTS &&
-	       evsel->core.attr.config1 == 0;
+	       evsel->core.attr.config == TOPDOWN_SLOTS;
 }
 
 bool arch_is_topdown_metrics(const struct evsel *evsel)
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] perf: missing explicit <stdio.h> and <string.h> inclusions --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] perf: missing explicit <stdio.h> and <string.h> inclusions
--

commit 197eea3115a0d065dc53e6f96ad2fd38fdb9bc99
Author: Zide Chen <[email protected]>

perf topdown: Don't require config1==0 for a slots event

This commit modifies the topdown slots architecture check to no longer require config1 == 0, fixing an issue where explicit slots events were duplicated in scheduling. It also adds a new test to verify that an explicit metrics_clear slots event doesn't trigger extra injections.

Quoted 4 lines
tools/perf/arch/x86/tests/topdown.c +0 -0
index 2b6f47ce49324..b1ce9f83db00e 100644

[ ... ]

Quoted 15 lines
tools/perf/arch/x86/tests/topdown.c +11 -0
@@ -229,10 +229,38 @@ static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu
 	return TEST_OK;
 }
 
+/*
+ * An explicit "slots,metrics_clear=1" event is still the slots event and
+ * must not cause an extra slots event to be injected into the group.
+ */
+static int test__x86_topdown_metrics_clear(struct test_suite *test __maybe_unused,
+					   int subtest __maybe_unused)
+{
+	struct perf_pmu *pmu;
+	char event_str[128];
+	int ret;
+

tools/perf/arch/x86/tests/topdown.c static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu · line 252

Quoted 10 lines
tools/perf/arch/x86/tests/topdown.c +10 -0
+	if (!topdown_sys_has_perf_metrics())
+		return TEST_OK;
+
+	pmu = perf_pmus__find_by_type(PERF_TYPE_RAW);
+	if (!pmu || !perf_pmu__has_format(pmu, "metrics_clear"))
+		return TEST_OK;
+
+	snprintf(event_str, sizeof(event_str),
+		 "{%s/slots,metrics_clear=1/,%s/topdown-retiring/}",
+		 pmu->name, pmu->name);

[Severity: Low]
Does this new function test__x86_topdown_metrics_clear() need explicit header inclusions for musl compatibility?

This code relies on an implicit declaration of <stdio.h> for snprintf(). Missing POSIX headers can cause build failures when compiling the perf tool with strict libc implementations like musl instead of glibc.

Should <stdio.h> (and potentially <string.h>) be explicitly included at the top of this file to prevent musl compilation failures?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#2 lore raw
Quoted 49 lines · On 9/16/2026 3:44 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] perf: missing explicit <stdio.h> and <string.h> inclusions
--

commit 197eea3115a0d065dc53e6f96ad2fd38fdb9bc99
Author: Zide Chen <[email protected]>

perf topdown: Don't require config1==0 for a slots event

This commit modifies the topdown slots architecture check to no longer require config1 == 0, fixing an issue where explicit slots events were duplicated in scheduling. It also adds a new test to verify that an explicit metrics_clear slots event doesn't trigger extra injections.

Quoted 4 lines
tools/perf/arch/x86/tests/topdown.c +0 -0
index 2b6f47ce49324..b1ce9f83db00e 100644

[ ... ]

Quoted 15 lines
tools/perf/arch/x86/tests/topdown.c +11 -0
@@ -229,10 +229,38 @@ static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu
 	return TEST_OK;
 }
 
+/*
+ * An explicit "slots,metrics_clear=1" event is still the slots event and
+ * must not cause an extra slots event to be injected into the group.
+ */
+static int test__x86_topdown_metrics_clear(struct test_suite *test __maybe_unused,
+					   int subtest __maybe_unused)
+{
+	struct perf_pmu *pmu;
+	char event_str[128];
+	int ret;
+

tools/perf/arch/x86/tests/topdown.c static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu · line 252

Quoted 10 lines
tools/perf/arch/x86/tests/topdown.c +10 -0
+	if (!topdown_sys_has_perf_metrics())
+		return TEST_OK;
+
+	pmu = perf_pmus__find_by_type(PERF_TYPE_RAW);
+	if (!pmu || !perf_pmu__has_format(pmu, "metrics_clear"))
+		return TEST_OK;
+
+	snprintf(event_str, sizeof(event_str),
+		 "{%s/slots,metrics_clear=1/,%s/topdown-retiring/}",
+		 pmu->name, pmu->name);

[Severity: Low]
Does this new function test__x86_topdown_metrics_clear() need explicit header inclusions for musl compatibility?

Quoted 6 lines

This code relies on an implicit declaration of <stdio.h> for snprintf(). Missing POSIX headers can cause build failures when compiling the perf tool with strict libc implementations like musl instead of glibc.

Should <stdio.h> (and potentially <string.h>) be explicitly included at the top of this file to prevent musl compilation failures?

Not needed. tools/perf/util/pmu.h, along with debug.h (which is also included), explicitly pulls in <stdio.h>.

Additionally, this file already contains other calls to snprintf(). If the required header dependency were missing, those existing uses would already fail to build.

reviewer
#3 lore raw
Quoted 58 lines · Zide
Quoted 49 lines · On 9/16/2026 3:44 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] perf: missing explicit <stdio.h> and <string.h> inclusions
--

commit 197eea3115a0d065dc53e6f96ad2fd38fdb9bc99
Author: Zide Chen <[email protected]>

perf topdown: Don't require config1==0 for a slots event

This commit modifies the topdown slots architecture check to no longer require config1 == 0, fixing an issue where explicit slots events were duplicated in scheduling. It also adds a new test to verify that an explicit metrics_clear slots event doesn't trigger extra injections.

Quoted 4 lines
tools/perf/arch/x86/tests/topdown.c +0 -0
index 2b6f47ce49324..b1ce9f83db00e 100644

[ ... ]

Quoted 15 lines
tools/perf/arch/x86/tests/topdown.c +11 -0
@@ -229,10 +229,38 @@ static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu
     return TEST_OK;
 }
 
+/*
+ * An explicit "slots,metrics_clear=1" event is still the slots event and
+ * must not cause an extra slots event to be injected into the group.
+ */
+static int test__x86_topdown_metrics_clear(struct test_suite *test __maybe_unused,
+                                       int subtest __maybe_unused)
+{
+    struct perf_pmu *pmu;
+    char event_str[128];
+    int ret;
+

tools/perf/arch/x86/tests/topdown.c static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu · line 252

Quoted 10 lines
tools/perf/arch/x86/tests/topdown.c +10 -0
+    if (!topdown_sys_has_perf_metrics())
+            return TEST_OK;
+
+    pmu = perf_pmus__find_by_type(PERF_TYPE_RAW);
+    if (!pmu || !perf_pmu__has_format(pmu, "metrics_clear"))
+            return TEST_OK;
+
+    snprintf(event_str, sizeof(event_str),
+             "{%s/slots,metrics_clear=1/,%s/topdown-retiring/}",
+             pmu->name, pmu->name);

[Severity: Low]
Does this new function test__x86_topdown_metrics_clear() need explicit header inclusions for musl compatibility?

Quoted 6 lines

This code relies on an implicit declaration of <stdio.h> for snprintf(). Missing POSIX headers can cause build failures when compiling the perf tool with strict libc implementations like musl instead of glibc.

Should <stdio.h> (and potentially <string.h>) be explicitly included at the top of this file to prevent musl compilation failures?

Quoted 6 lines

Not needed. tools/perf/util/pmu.h, along with debug.h (which is also included), explicitly pulls in <stdio.h>.

Additionally, this file already contains other calls to snprintf(). If the required header dependency were missing, those existing uses would already fail to build.

In general we want to avoid transitive includes as they are brittle. For example, if pmu.h and/or debug.h were changed, it might break musl.

Thanks,
Ian

author
#4 lore raw
Quoted 62 lines · Ian Rogers
Quoted 61 lines · Zide
Quoted 49 lines · On 9/16/2026 3:44 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] perf: missing explicit <stdio.h> and <string.h> inclusions
--

commit 197eea3115a0d065dc53e6f96ad2fd38fdb9bc99
Author: Zide Chen <[email protected]>

perf topdown: Don't require config1==0 for a slots event

This commit modifies the topdown slots architecture check to no longer require config1 == 0, fixing an issue where explicit slots events were duplicated in scheduling. It also adds a new test to verify that an explicit metrics_clear slots event doesn't trigger extra injections.

Quoted 4 lines
tools/perf/arch/x86/tests/topdown.c +0 -0
index 2b6f47ce49324..b1ce9f83db00e 100644

[ ... ]

Quoted 15 lines
tools/perf/arch/x86/tests/topdown.c +11 -0
@@ -229,10 +229,38 @@ static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu
     return TEST_OK;
 }
 
+/*
+ * An explicit "slots,metrics_clear=1" event is still the slots event and
+ * must not cause an extra slots event to be injected into the group.
+ */
+static int test__x86_topdown_metrics_clear(struct test_suite *test __maybe_unused,
+                                       int subtest __maybe_unused)
+{
+    struct perf_pmu *pmu;
+    char event_str[128];
+    int ret;
+

tools/perf/arch/x86/tests/topdown.c static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu · line 252

Quoted 10 lines
tools/perf/arch/x86/tests/topdown.c +10 -0
+    if (!topdown_sys_has_perf_metrics())
+            return TEST_OK;
+
+    pmu = perf_pmus__find_by_type(PERF_TYPE_RAW);
+    if (!pmu || !perf_pmu__has_format(pmu, "metrics_clear"))
+            return TEST_OK;
+
+    snprintf(event_str, sizeof(event_str),
+             "{%s/slots,metrics_clear=1/,%s/topdown-retiring/}",
+             pmu->name, pmu->name);

[Severity: Low]
Does this new function test__x86_topdown_metrics_clear() need explicit header inclusions for musl compatibility?

Quoted 6 lines

This code relies on an implicit declaration of <stdio.h> for snprintf(). Missing POSIX headers can cause build failures when compiling the perf tool with strict libc implementations like musl instead of glibc.

Should <stdio.h> (and potentially <string.h>) be explicitly included at the top of this file to prevent musl compilation failures?

Not needed. tools/perf/util/pmu.h, along with debug.h (which is also included), explicitly pulls in <stdio.h>.

Quoted 7 lines
Quoted 3 lines

Additionally, this file already contains other calls to snprintf(). If the required header dependency were missing, those existing uses would already fail to build.

In general we want to avoid transitive includes as they are brittle. For example, if pmu.h and/or debug.h were changed, it might break musl.

OK, then I can add stdio.h and string.h to topdown.c.

Quoted 2 lines

Thanks,
Ian

reviewer Reviewed-by on v1 1/1
#5 lore raw
In reply to Zide Chen
Quoted 20 lines · Zide Chen

arch_is_topdown_slots() requires config1 == 0, so it misidentified "cpu/slots,metrics_clear=1/" as not being the slots event, and an additional slots event gets inserted, which fails event scheduling since the group now needs two fixed counter 3.

  $ perf stat -e "{cpu/slots,metrics_clear=1/,cpu/topdown-retiring/}" -- sleep 1
  WARNING: events were regrouped to match PMUs
       <not counted>      slots
     <not supported>      cpu/slots,metrics_clear=1/
       <not counted>      cpu/topdown-retiring/

Drop the config1 == 0 check from arch_is_topdown_slots(). This is
safe: config1 has no bearing on the event's identifying config value (TOPDOWN_SLOTS, 0x0400).

The attr.type == PERF_TYPE_RAW check is what actually matters: only the core PMU is registered with PERF_TYPE_RAW, and on x86, 0x400 is guaranteed to be slots event on core CPU.

Quoted 8 lines

arch_is_topdown_metrics() keeps its own config1 == 0 check, since genuine metrics events have no "metrics_clear" and config1 remains 0.

Add a test verifying that an explicit "slots,metrics_clear=1" event in a group does not trigger an extra slots event injection.

Fixes 5b546de9cc17 ("perf topdown: Use attribute to see an event is a topdown metic or slots")

Note, there's a Sashiko warning about a potentially missing #include for musl compatibility that maybe you or Arnaldo could address.

Thanks,
Ian

Quoted 65 lines
---
 tools/perf/arch/x86/tests/topdown.c | 28 ++++++++++++++++++++++++++++
 tools/perf/arch/x86/util/topdown.c  |  3 +--
 2 files changed, 29 insertions(+), 2 deletions(-)
tools/perf/arch/x86/tests/topdown.c +28 -0
index 2b6f47ce4932..b1ce9f83db00 100644
@@ -229,10 +229,38 @@ static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu
        return TEST_OK;
 }
 
+/*
+ * An explicit "slots,metrics_clear=1" event is still the slots event and
+ * must not cause an extra slots event to be injected into the group.
+ */
+static int test__x86_topdown_metrics_clear(struct test_suite *test __maybe_unused,
+                                          int subtest __maybe_unused)
+{
+       struct perf_pmu *pmu;
+       char event_str[128];
+       int ret;
+
+       if (!topdown_sys_has_perf_metrics())
+               return TEST_OK;
+
+       pmu = perf_pmus__find_by_type(PERF_TYPE_RAW);
+       if (!pmu || !perf_pmu__has_format(pmu, "metrics_clear"))
+               return TEST_OK;
+
+       snprintf(event_str, sizeof(event_str),
+                "{%s/slots,metrics_clear=1/,%s/topdown-retiring/}",
+                pmu->name, pmu->name);
+       ret = test_sort(event_str, 2, 1);
+       TEST_ASSERT_EQUAL("explicit metrics_clear slots event isn't duplicated", ret, TEST_OK);
+
+       return TEST_OK;
+}
+
 static struct test_case x86_topdown_tests[] = {
        TEST_CASE("topdown events", x86_topdown),
        TEST_CASE("topdown sorting", x86_topdown_sorting),
        TEST_CASE("topdown slots injection", x86_topdown_slots_injection),
+       TEST_CASE("topdown metrics_clear no extra slots", x86_topdown_metrics_clear),
        { .name = NULL, }
 };
 
tools/perf/arch/x86/util/topdown.c +1 -3
index bafd285119d7..64bc64e335a2 100644
@@ -37,8 +37,7 @@ bool topdown_sys_has_perf_metrics(void)
 bool arch_is_topdown_slots(const struct evsel *evsel)
 {
        return evsel->core.attr.type == PERF_TYPE_RAW &&
-              evsel->core.attr.config == TOPDOWN_SLOTS &&
-              evsel->core.attr.config1 == 0;
+              evsel->core.attr.config == TOPDOWN_SLOTS;
 }
 
 bool arch_is_topdown_metrics(const struct evsel *evsel)
--

2.55.0

#6 lore raw
Quoted 27 lines · Ian Rogers
Quoted 26 lines · Zide Chen

arch_is_topdown_slots() requires config1 == 0, so it misidentified "cpu/slots,metrics_clear=1/" as not being the slots event, and an additional slots event gets inserted, which fails event scheduling since the group now needs two fixed counter 3.

  $ perf stat -e "{cpu/slots,metrics_clear=1/,cpu/topdown-retiring/}" -- sleep 1
  WARNING: events were regrouped to match PMUs
       <not counted>      slots
     <not supported>      cpu/slots,metrics_clear=1/
       <not counted>      cpu/topdown-retiring/

Drop the config1 == 0 check from arch_is_topdown_slots(). This is
safe: config1 has no bearing on the event's identifying config value (TOPDOWN_SLOTS, 0x0400).

The attr.type == PERF_TYPE_RAW check is what actually matters: only the core PMU is registered with PERF_TYPE_RAW, and on x86, 0x400 is guaranteed to be slots event on core CPU.

arch_is_topdown_metrics() keeps its own config1 == 0 check, since genuine metrics events have no "metrics_clear" and config1 remains 0.

Add a test verifying that an explicit "slots,metrics_clear=1" event in a group does not trigger an extra slots event injection.

Quoted 7 lines
Quoted 2 lines
Fixes 5b546de9cc17 ("perf topdown: Use attribute to see an event is a topdown metic or slots")

Note, there's a Sashiko warning about a potentially missing #include for musl compatibility that maybe you or Arnaldo could address.

Merging, will look add address that comment,

Thanks,

- Arnaldo

Quoted 68 lines

Thanks,
Ian

Quoted 65 lines
---
 tools/perf/arch/x86/tests/topdown.c | 28 ++++++++++++++++++++++++++++
 tools/perf/arch/x86/util/topdown.c  |  3 +--
 2 files changed, 29 insertions(+), 2 deletions(-)
tools/perf/arch/x86/tests/topdown.c +28 -0
index 2b6f47ce4932..b1ce9f83db00 100644
@@ -229,10 +229,38 @@ static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu
        return TEST_OK;
 }
 
+/*
+ * An explicit "slots,metrics_clear=1" event is still the slots event and
+ * must not cause an extra slots event to be injected into the group.
+ */
+static int test__x86_topdown_metrics_clear(struct test_suite *test __maybe_unused,
+                                          int subtest __maybe_unused)
+{
+       struct perf_pmu *pmu;
+       char event_str[128];
+       int ret;
+
+       if (!topdown_sys_has_perf_metrics())
+               return TEST_OK;
+
+       pmu = perf_pmus__find_by_type(PERF_TYPE_RAW);
+       if (!pmu || !perf_pmu__has_format(pmu, "metrics_clear"))
+               return TEST_OK;
+
+       snprintf(event_str, sizeof(event_str),
+                "{%s/slots,metrics_clear=1/,%s/topdown-retiring/}",
+                pmu->name, pmu->name);
+       ret = test_sort(event_str, 2, 1);
+       TEST_ASSERT_EQUAL("explicit metrics_clear slots event isn't duplicated", ret, TEST_OK);
+
+       return TEST_OK;
+}
+
 static struct test_case x86_topdown_tests[] = {
        TEST_CASE("topdown events", x86_topdown),
        TEST_CASE("topdown sorting", x86_topdown_sorting),
        TEST_CASE("topdown slots injection", x86_topdown_slots_injection),
+       TEST_CASE("topdown metrics_clear no extra slots", x86_topdown_metrics_clear),
        { .name = NULL, }
 };
 
tools/perf/arch/x86/util/topdown.c +1 -3
index bafd285119d7..64bc64e335a2 100644
@@ -37,8 +37,7 @@ bool topdown_sys_has_perf_metrics(void)
 bool arch_is_topdown_slots(const struct evsel *evsel)
 {
        return evsel->core.attr.type == PERF_TYPE_RAW &&
-              evsel->core.attr.config == TOPDOWN_SLOTS &&
-              evsel->core.attr.config1 == 0;
+              evsel->core.attr.config == TOPDOWN_SLOTS;
 }
 
 bool arch_is_topdown_metrics(const struct evsel *evsel)
--

2.55.0

#7 lore raw
Quoted 27 lines · Arnaldo Carvalho de Melo
Quoted 26 lines · Ian Rogers
Quoted 25 lines · Zide Chen

arch_is_topdown_slots() requires config1 == 0, so it misidentified "cpu/slots,metrics_clear=1/" as not being the slots event, and an additional slots event gets inserted, which fails event scheduling since the group now needs two fixed counter 3.

  $ perf stat -e "{cpu/slots,metrics_clear=1/,cpu/topdown-retiring/}" -- sleep 1
  WARNING: events were regrouped to match PMUs
       <not counted>      slots
     <not supported>      cpu/slots,metrics_clear=1/
       <not counted>      cpu/topdown-retiring/

Drop the config1 == 0 check from arch_is_topdown_slots(). This is
safe: config1 has no bearing on the event's identifying config value (TOPDOWN_SLOTS, 0x0400).

The attr.type == PERF_TYPE_RAW check is what actually matters: only the core PMU is registered with PERF_TYPE_RAW, and on x86, 0x400 is guaranteed to be slots event on core CPU.

arch_is_topdown_metrics() keeps its own config1 == 0 check, since genuine metrics events have no "metrics_clear" and config1 remains 0.

Add a test verifying that an explicit "slots,metrics_clear=1" event in a group does not trigger an extra slots event injection.

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
Fixes 5b546de9cc17 ("perf topdown: Use attribute to see an event is a topdown metic or slots")

Note, there's a Sashiko warning about a potentially missing #include for musl compatibility that maybe you or Arnaldo could address.

Merging, will look add address that comment,

@Arnaldo, Zide posted the v2 patch to address the Sashiko comments.
https://lore.kernel.org/all/[email protected]/

You may directly merge that patch. :)

Thanks.

Quoted 73 lines

Thanks,

- Arnaldo

Quoted 68 lines

Thanks,
Ian

Quoted 65 lines
---
 tools/perf/arch/x86/tests/topdown.c | 28 ++++++++++++++++++++++++++++
 tools/perf/arch/x86/util/topdown.c  |  3 +--
 2 files changed, 29 insertions(+), 2 deletions(-)
tools/perf/arch/x86/tests/topdown.c +28 -0
index 2b6f47ce4932..b1ce9f83db00 100644
@@ -229,10 +229,38 @@ static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu
        return TEST_OK;
 }
 
+/*
+ * An explicit "slots,metrics_clear=1" event is still the slots event and
+ * must not cause an extra slots event to be injected into the group.
+ */
+static int test__x86_topdown_metrics_clear(struct test_suite *test __maybe_unused,
+                                          int subtest __maybe_unused)
+{
+       struct perf_pmu *pmu;
+       char event_str[128];
+       int ret;
+
+       if (!topdown_sys_has_perf_metrics())
+               return TEST_OK;
+
+       pmu = perf_pmus__find_by_type(PERF_TYPE_RAW);
+       if (!pmu || !perf_pmu__has_format(pmu, "metrics_clear"))
+               return TEST_OK;
+
+       snprintf(event_str, sizeof(event_str),
+                "{%s/slots,metrics_clear=1/,%s/topdown-retiring/}",
+                pmu->name, pmu->name);
+       ret = test_sort(event_str, 2, 1);
+       TEST_ASSERT_EQUAL("explicit metrics_clear slots event isn't duplicated", ret, TEST_OK);
+
+       return TEST_OK;
+}
+
 static struct test_case x86_topdown_tests[] = {
        TEST_CASE("topdown events", x86_topdown),
        TEST_CASE("topdown sorting", x86_topdown_sorting),
        TEST_CASE("topdown slots injection", x86_topdown_slots_injection),
+       TEST_CASE("topdown metrics_clear no extra slots", x86_topdown_metrics_clear),
        { .name = NULL, }
 };
 
tools/perf/arch/x86/util/topdown.c +1 -3
index bafd285119d7..64bc64e335a2 100644
@@ -37,8 +37,7 @@ bool topdown_sys_has_perf_metrics(void)
 bool arch_is_topdown_slots(const struct evsel *evsel)
 {
        return evsel->core.attr.type == PERF_TYPE_RAW &&
-              evsel->core.attr.config == TOPDOWN_SLOTS &&
-              evsel->core.attr.config1 == 0;
+              evsel->core.attr.config == TOPDOWN_SLOTS;
 }
 
 bool arch_is_topdown_metrics(const struct evsel *evsel)
--

2.55.0

Reviewed-by on v1 1/1
#8 lore raw
In reply to Zide Chen

LGTM. Thanks.

Quoted 89 lines · Zide Chen

arch_is_topdown_slots() requires config1 == 0, so it misidentified "cpu/slots,metrics_clear=1/" as not being the slots event, and an additional slots event gets inserted, which fails event scheduling since the group now needs two fixed counter 3.

  $ perf stat -e "{cpu/slots,metrics_clear=1/,cpu/topdown-retiring/}" -- sleep 1
  WARNING: events were regrouped to match PMUs
       <not counted>      slots
     <not supported>      cpu/slots,metrics_clear=1/
       <not counted>      cpu/topdown-retiring/

Drop the config1 == 0 check from arch_is_topdown_slots(). This is
safe: config1 has no bearing on the event's identifying config value (TOPDOWN_SLOTS, 0x0400).

The attr.type == PERF_TYPE_RAW check is what actually matters: only the core PMU is registered with PERF_TYPE_RAW, and on x86, 0x400 is guaranteed to be slots event on core CPU.

arch_is_topdown_metrics() keeps its own config1 == 0 check, since genuine metrics events have no "metrics_clear" and config1 remains 0.

Add a test verifying that an explicit "slots,metrics_clear=1" event in a group does not trigger an extra slots event injection.

Fixes 5b546de9cc17 ("perf topdown: Use attribute to see an event is a topdown metic or slots")
---
 tools/perf/arch/x86/tests/topdown.c | 28 ++++++++++++++++++++++++++++
 tools/perf/arch/x86/util/topdown.c  |  3 +--
 2 files changed, 29 insertions(+), 2 deletions(-)
tools/perf/arch/x86/tests/topdown.c +28 -0
index 2b6f47ce4932..b1ce9f83db00 100644
@@ -229,10 +229,38 @@ static int test__x86_topdown_slots_injection(struct test_suite *test __maybe_unu
 	return TEST_OK;
 }
 
+/*
+ * An explicit "slots,metrics_clear=1" event is still the slots event and
+ * must not cause an extra slots event to be injected into the group.
+ */
+static int test__x86_topdown_metrics_clear(struct test_suite *test __maybe_unused,
+					   int subtest __maybe_unused)
+{
+	struct perf_pmu *pmu;
+	char event_str[128];
+	int ret;
+
+	if (!topdown_sys_has_perf_metrics())
+		return TEST_OK;
+
+	pmu = perf_pmus__find_by_type(PERF_TYPE_RAW);
+	if (!pmu || !perf_pmu__has_format(pmu, "metrics_clear"))
+		return TEST_OK;
+
+	snprintf(event_str, sizeof(event_str),
+		 "{%s/slots,metrics_clear=1/,%s/topdown-retiring/}",
+		 pmu->name, pmu->name);
+	ret = test_sort(event_str, 2, 1);
+	TEST_ASSERT_EQUAL("explicit metrics_clear slots event isn't duplicated", ret, TEST_OK);
+
+	return TEST_OK;
+}
+
 static struct test_case x86_topdown_tests[] = {
 	TEST_CASE("topdown events", x86_topdown),
 	TEST_CASE("topdown sorting", x86_topdown_sorting),
 	TEST_CASE("topdown slots injection", x86_topdown_slots_injection),
+	TEST_CASE("topdown metrics_clear no extra slots", x86_topdown_metrics_clear),
 	{ .name = NULL, }
 };
 
tools/perf/arch/x86/util/topdown.c +1 -2
index bafd285119d7..64bc64e335a2 100644
@@ -37,8 +37,7 @@ bool topdown_sys_has_perf_metrics(void)
 bool arch_is_topdown_slots(const struct evsel *evsel)
 {
 	return evsel->core.attr.type == PERF_TYPE_RAW &&
-	       evsel->core.attr.config == TOPDOWN_SLOTS &&
-	       evsel->core.attr.config1 == 0;
+	       evsel->core.attr.config == TOPDOWN_SLOTS;
 }
 
 bool arch_is_topdown_metrics(const struct evsel *evsel)