Skip to contentSkip to conversation

perf/x86/amd: Update generalized events

PATCH In review Sandipan Das 3 participants 9 messages Mar 25, 2024 – Mar 26, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover perf/x86/amd: Update generalized events

Update mappings for some generalized hardware (PERF_TYPE_HARDWARE) events. This includes changes to the stalled-cycles-* events and the addition of the ref-cycles events based on the core microarchitecture.

Sandipan Das (2):
perf/x86/amd/core: Update stalled-cycles-* events for Zen 2 and later perf/x86/amd/core: Add ref-cycles event for Zen 4 and later

 arch/x86/events/amd/core.c | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 perf/x86/amd/core: Update stalled-cycles-* events for Zen 2 and later

AMD processors based on Zen 2 and later microarchitectures do not support PMCx087 (instruction pipe stalls) which is used as the backing event for "stalled-cycles-frontend" and "stalled-cycles-backend". Use PMCx0A9 (cycles where micro-op queue is empty) instead to count frontend stalls and remove the entry for backend stalls since there is no direct replacement.

---
 arch/x86/events/amd/core.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
arch/x86/events/amd/core.c +17 -3
index aec16e581f5b..afe4a809f2ed 100644
@@ -250,7 +250,7 @@ static const u64 amd_perfmon_event_map[PERF_COUNT_HW_MAX] =
 /*
  * AMD Performance Monitor Family 17h and later:
  */
-static const u64 amd_f17h_perfmon_event_map[PERF_COUNT_HW_MAX] =
+static const u64 amd_zen1_perfmon_event_map[PERF_COUNT_HW_MAX] =
 {
 	[PERF_COUNT_HW_CPU_CYCLES]		= 0x0076,
 	[PERF_COUNT_HW_INSTRUCTIONS]		= 0x00c0,
@@ -262,10 +262,24 @@ static const u64 amd_f17h_perfmon_event_map[PERF_COUNT_HW_MAX] =
 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND]	= 0x0187,
 };
 
+static const u64 amd_zen2_perfmon_event_map[PERF_COUNT_HW_MAX] =
+{
+	[PERF_COUNT_HW_CPU_CYCLES]		= 0x0076,
+	[PERF_COUNT_HW_INSTRUCTIONS]		= 0x00c0,
+	[PERF_COUNT_HW_CACHE_REFERENCES]	= 0xff60,
+	[PERF_COUNT_HW_CACHE_MISSES]		= 0x0964,
+	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= 0x00c2,
+	[PERF_COUNT_HW_BRANCH_MISSES]		= 0x00c3,
+	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND]	= 0x00a9,
+};
+
 static u64 amd_pmu_event_map(int hw_event)
 {
-	if (boot_cpu_data.x86 >= 0x17)
-		return amd_f17h_perfmon_event_map[hw_event];
+	if (cpu_feature_enabled(X86_FEATURE_ZEN2) || boot_cpu_data.x86 >= 0x19)
+		return amd_zen2_perfmon_event_map[hw_event];
+
+	if (cpu_feature_enabled(X86_FEATURE_ZEN1))
+		return amd_zen1_perfmon_event_map[hw_event];
 
 	return amd_perfmon_event_map[hw_event];
 }
Signature · 1 line
2.34.1
#5 lore raw

PATCH 2/2 perf/x86/amd/core: Add ref-cycles event for Zen 4 and later

In reply to Sandipan Das

Add the "ref-cycles" event for AMD processors based on Zen 4 and later microarchitectures. The backing event is based on PMCx120 which counts cycles not in halt state in P0 frequency (same as MPERF).

---
 arch/x86/events/amd/core.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/x86/events/amd/core.c +15 -0
index afe4a809f2ed..685bfa860d67 100644
@@ -273,8 +273,23 @@ static const u64 amd_zen2_perfmon_event_map[PERF_COUNT_HW_MAX] =
 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND]	= 0x00a9,
 };
 
+static const u64 amd_zen4_perfmon_event_map[PERF_COUNT_HW_MAX] =
+{
+	[PERF_COUNT_HW_CPU_CYCLES]		= 0x0076,
+	[PERF_COUNT_HW_INSTRUCTIONS]		= 0x00c0,
+	[PERF_COUNT_HW_CACHE_REFERENCES]	= 0xff60,
+	[PERF_COUNT_HW_CACHE_MISSES]		= 0x0964,
+	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= 0x00c2,
+	[PERF_COUNT_HW_BRANCH_MISSES]		= 0x00c3,
+	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND]	= 0x00a9,
+	[PERF_COUNT_HW_REF_CPU_CYCLES]		= 0x100000120,
+};
+
 static u64 amd_pmu_event_map(int hw_event)
 {
+	if (cpu_feature_enabled(X86_FEATURE_ZEN4) || boot_cpu_data.x86 >= 0x1a)
+		return amd_zen4_perfmon_event_map[hw_event];
+
 	if (cpu_feature_enabled(X86_FEATURE_ZEN2) || boot_cpu_data.x86 >= 0x19)
 		return amd_zen2_perfmon_event_map[hw_event];
 
Signature · 1 line
2.34.1
reviewer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] perf/x86/amd/core: Update stalled-cycles-* events for Zen 2 and later

In reply to Sandipan Das
Quoted 9 lines · Sandipan Das

AMD processors based on Zen 2 and later microarchitectures do not support PMCx087 (instruction pipe stalls) which is used as the backing event for "stalled-cycles-frontend" and "stalled-cycles-backend". Use PMCx0A9 (cycles where micro-op queue is empty) instead to count frontend stalls and remove the entry for backend stalls since there is no direct replacement.

This looks good to me. Should there be a Fixes tag for the sake of backports?

Thanks,
Ian

Quoted 47 lines
---
 arch/x86/events/amd/core.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
arch/x86/events/amd/core.c +17 -4
index aec16e581f5b..afe4a809f2ed 100644
@@ -250,7 +250,7 @@ static const u64 amd_perfmon_event_map[PERF_COUNT_HW_MAX] =
 /*
  * AMD Performance Monitor Family 17h and later:
  */
-static const u64 amd_f17h_perfmon_event_map[PERF_COUNT_HW_MAX] =
+static const u64 amd_zen1_perfmon_event_map[PERF_COUNT_HW_MAX] =
 {
        [PERF_COUNT_HW_CPU_CYCLES]              = 0x0076,
        [PERF_COUNT_HW_INSTRUCTIONS]            = 0x00c0,
@@ -262,10 +262,24 @@ static const u64 amd_f17h_perfmon_event_map[PERF_COUNT_HW_MAX] =
        [PERF_COUNT_HW_STALLED_CYCLES_BACKEND]  = 0x0187,
 };
 
+static const u64 amd_zen2_perfmon_event_map[PERF_COUNT_HW_MAX] =
+{
+       [PERF_COUNT_HW_CPU_CYCLES]              = 0x0076,
+       [PERF_COUNT_HW_INSTRUCTIONS]            = 0x00c0,
+       [PERF_COUNT_HW_CACHE_REFERENCES]        = 0xff60,
+       [PERF_COUNT_HW_CACHE_MISSES]            = 0x0964,
+       [PERF_COUNT_HW_BRANCH_INSTRUCTIONS]     = 0x00c2,
+       [PERF_COUNT_HW_BRANCH_MISSES]           = 0x00c3,
+       [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00a9,
+};
+
 static u64 amd_pmu_event_map(int hw_event)
 {
-       if (boot_cpu_data.x86 >= 0x17)
-               return amd_f17h_perfmon_event_map[hw_event];
+       if (cpu_feature_enabled(X86_FEATURE_ZEN2) || boot_cpu_data.x86 >= 0x19)
+               return amd_zen2_perfmon_event_map[hw_event];
+
+       if (cpu_feature_enabled(X86_FEATURE_ZEN1))
+               return amd_zen1_perfmon_event_map[hw_event];
 
        return amd_perfmon_event_map[hw_event];
 }
--

2.34.1

reviewer Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] perf/x86/amd/core: Add ref-cycles event for Zen 4 and later

In reply to Sandipan Das
Quoted 4 lines · Sandipan Das

Add the "ref-cycles" event for AMD processors based on Zen 4 and later microarchitectures. The backing event is based on PMCx120 which counts cycles not in halt state in P0 frequency (same as MPERF).

This reminds me that we lack smi cost and an smi_cycles metric for AMD, here is an Intel one:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/pmu-events/arch/x86/skylakex/skx-metrics.json?h=perf-tools-next#n274
The metric uses APERF but runs with freeze_on_smi set:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-stat.c?h=perf-tools-next#n2115
so the delta between cycles and aperf is the cycles in SMI. It would be great if we could get something similar on AMD.

Quoted 1 line

Thanks,
Ian

Quoted 35 lines
---
 arch/x86/events/amd/core.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/x86/events/amd/core.c +15 -1
index afe4a809f2ed..685bfa860d67 100644
@@ -273,8 +273,23 @@ static const u64 amd_zen2_perfmon_event_map[PERF_COUNT_HW_MAX] =
        [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00a9,
 };
 
+static const u64 amd_zen4_perfmon_event_map[PERF_COUNT_HW_MAX] =
+{
+       [PERF_COUNT_HW_CPU_CYCLES]              = 0x0076,
+       [PERF_COUNT_HW_INSTRUCTIONS]            = 0x00c0,
+       [PERF_COUNT_HW_CACHE_REFERENCES]        = 0xff60,
+       [PERF_COUNT_HW_CACHE_MISSES]            = 0x0964,
+       [PERF_COUNT_HW_BRANCH_INSTRUCTIONS]     = 0x00c2,
+       [PERF_COUNT_HW_BRANCH_MISSES]           = 0x00c3,
+       [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00a9,
+       [PERF_COUNT_HW_REF_CPU_CYCLES]          = 0x100000120,
+};
+
 static u64 amd_pmu_event_map(int hw_event)
 {
+       if (cpu_feature_enabled(X86_FEATURE_ZEN4) || boot_cpu_data.x86 >= 0x1a)
+               return amd_zen4_perfmon_event_map[hw_event];
+
        if (cpu_feature_enabled(X86_FEATURE_ZEN2) || boot_cpu_data.x86 >= 0x19)
                return amd_zen2_perfmon_event_map[hw_event];
 
--

2.34.1

#3 lore raw

Re: [PATCH 1/2] perf/x86/amd/core: Update stalled-cycles-* events for Zen 2 and later

In reply to Ian Rogers
Quoted 9 lines · Ian Rogers
Quoted 8 lines · Sandipan Das

AMD processors based on Zen 2 and later microarchitectures do not support PMCx087 (instruction pipe stalls) which is used as the backing event for "stalled-cycles-frontend" and "stalled-cycles-backend". Use PMCx0A9 (cycles where micro-op queue is empty) instead to count frontend stalls and remove the entry for backend stalls since there is no direct replacement.

Quoted 4 lines
Quoted 1 line

This looks good to me. Should there be a Fixes tag for the sake of backports?

My bad. Yes, there should a fixes tag.

Fixes 3fe3331bb285 ("perf/x86/amd: Add event map for AMD Family 17h")

Ingo, I see that you have already pushed this to the perf/urgent branch. Should I send a v2?

Quoted 52 lines

Thanks,
Ian

Quoted 47 lines
---
 arch/x86/events/amd/core.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
arch/x86/events/amd/core.c +17 -4
index aec16e581f5b..afe4a809f2ed 100644
@@ -250,7 +250,7 @@ static const u64 amd_perfmon_event_map[PERF_COUNT_HW_MAX] =
 /*
  * AMD Performance Monitor Family 17h and later:
  */
-static const u64 amd_f17h_perfmon_event_map[PERF_COUNT_HW_MAX] =
+static const u64 amd_zen1_perfmon_event_map[PERF_COUNT_HW_MAX] =
 {
        [PERF_COUNT_HW_CPU_CYCLES]              = 0x0076,
        [PERF_COUNT_HW_INSTRUCTIONS]            = 0x00c0,
@@ -262,10 +262,24 @@ static const u64 amd_f17h_perfmon_event_map[PERF_COUNT_HW_MAX] =
        [PERF_COUNT_HW_STALLED_CYCLES_BACKEND]  = 0x0187,
 };
 
+static const u64 amd_zen2_perfmon_event_map[PERF_COUNT_HW_MAX] =
+{
+       [PERF_COUNT_HW_CPU_CYCLES]              = 0x0076,
+       [PERF_COUNT_HW_INSTRUCTIONS]            = 0x00c0,
+       [PERF_COUNT_HW_CACHE_REFERENCES]        = 0xff60,
+       [PERF_COUNT_HW_CACHE_MISSES]            = 0x0964,
+       [PERF_COUNT_HW_BRANCH_INSTRUCTIONS]     = 0x00c2,
+       [PERF_COUNT_HW_BRANCH_MISSES]           = 0x00c3,
+       [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00a9,
+};
+
 static u64 amd_pmu_event_map(int hw_event)
 {
-       if (boot_cpu_data.x86 >= 0x17)
-               return amd_f17h_perfmon_event_map[hw_event];
+       if (cpu_feature_enabled(X86_FEATURE_ZEN2) || boot_cpu_data.x86 >= 0x19)
+               return amd_zen2_perfmon_event_map[hw_event];
+
+       if (cpu_feature_enabled(X86_FEATURE_ZEN1))
+               return amd_zen1_perfmon_event_map[hw_event];
 
        return amd_perfmon_event_map[hw_event];
 }
--

2.34.1

#7 lore raw

Re: [PATCH 2/2] perf/x86/amd/core: Add ref-cycles event for Zen 4 and later

In reply to Ian Rogers
Quoted 6 lines · Ian Rogers
Quoted 4 lines · Sandipan Das

Add the "ref-cycles" event for AMD processors based on Zen 4 and later microarchitectures. The backing event is based on PMCx120 which counts cycles not in halt state in P0 frequency (same as MPERF).

Quoted 8 lines

This reminds me that we lack smi cost and an smi_cycles metric for AMD, here is an Intel one:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/pmu-events/arch/x86/skylakex/skx-metrics.json?h=perf-tools-next#n274
The metric uses APERF but runs with freeze_on_smi set:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-stat.c?h=perf-tools-next#n2115
so the delta between cycles and aperf is the cycles in SMI. It would be great if we could get something similar on AMD.

Thanks for the suggestion. I found PMCx02B ("ls_smi_rx" in perf JSONs) in the AMD PPRs which counts the number of SMIs received but there's no way to know how many cycles were spent in System Management Mode. I also could not find an equivalent for the Freeze on SMI feature.

Quoted 42 lines
Quoted 1 line

Thanks,
Ian

Quoted 35 lines
---
 arch/x86/events/amd/core.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/x86/events/amd/core.c +15 -1
index afe4a809f2ed..685bfa860d67 100644
@@ -273,8 +273,23 @@ static const u64 amd_zen2_perfmon_event_map[PERF_COUNT_HW_MAX] =
        [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00a9,
 };
 
+static const u64 amd_zen4_perfmon_event_map[PERF_COUNT_HW_MAX] =
+{
+       [PERF_COUNT_HW_CPU_CYCLES]              = 0x0076,
+       [PERF_COUNT_HW_INSTRUCTIONS]            = 0x00c0,
+       [PERF_COUNT_HW_CACHE_REFERENCES]        = 0xff60,
+       [PERF_COUNT_HW_CACHE_MISSES]            = 0x0964,
+       [PERF_COUNT_HW_BRANCH_INSTRUCTIONS]     = 0x00c2,
+       [PERF_COUNT_HW_BRANCH_MISSES]           = 0x00c3,
+       [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00a9,
+       [PERF_COUNT_HW_REF_CPU_CYCLES]          = 0x100000120,
+};
+
 static u64 amd_pmu_event_map(int hw_event)
 {
+       if (cpu_feature_enabled(X86_FEATURE_ZEN4) || boot_cpu_data.x86 >= 0x1a)
+               return amd_zen4_perfmon_event_map[hw_event];
+
        if (cpu_feature_enabled(X86_FEATURE_ZEN2) || boot_cpu_data.x86 >= 0x19)
                return amd_zen2_perfmon_event_map[hw_event];
 
--

2.34.1

#4 lore raw

Re: [PATCH 1/2] perf/x86/amd/core: Update stalled-cycles-* events for Zen 2 and later

In reply to Sandipan Das
Quoted 15 lines · Sandipan Das
Quoted 9 lines · Ian Rogers
Quoted 8 lines · Sandipan Das

AMD processors based on Zen 2 and later microarchitectures do not support PMCx087 (instruction pipe stalls) which is used as the backing event for "stalled-cycles-frontend" and "stalled-cycles-backend". Use PMCx0A9 (cycles where micro-op queue is empty) instead to count frontend stalls and remove the entry for backend stalls since there is no direct replacement.

Quoted 4 lines
Quoted 1 line

This looks good to me. Should there be a Fixes tag for the sake of backports?

Quoted 6 lines

My bad. Yes, there should a fixes tag.

Fixes 3fe3331bb285 ("perf/x86/amd: Add event map for AMD Family 17h")

Ingo, I see that you have already pushed this to the perf/urgent branch. Should I send a v2?

That's OK, no need, I've amended the commits in perf/urgent. 2019 is quite a way back for a Fixes tag though. :-)

Thanks,

Ingo

reviewer
#8 lore raw

Re: [PATCH 2/2] perf/x86/amd/core: Add ref-cycles event for Zen 4 and later

In reply to Sandipan Das
Quoted 17 lines · Sandipan Das
Quoted 6 lines · Ian Rogers
Quoted 4 lines · Sandipan Das

Add the "ref-cycles" event for AMD processors based on Zen 4 and later microarchitectures. The backing event is based on PMCx120 which counts cycles not in halt state in P0 frequency (same as MPERF).

Quoted 8 lines

This reminds me that we lack smi cost and an smi_cycles metric for AMD, here is an Intel one:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/pmu-events/arch/x86/skylakex/skx-metrics.json?h=perf-tools-next#n274
The metric uses APERF but runs with freeze_on_smi set:
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/tools/perf/builtin-stat.c?h=perf-tools-next#n2115
so the delta between cycles and aperf is the cycles in SMI. It would be great if we could get something similar on AMD.

Quoted 4 lines

Thanks for the suggestion. I found PMCx02B ("ls_smi_rx" in perf JSONs) in the AMD PPRs which counts the number of SMIs received but there's no way to know how many cycles were spent in System Management Mode. I also could not find an equivalent for the Freeze on SMI feature.

Thanks Sandipan, I found similar. Fwiw on Intel I'd written this in the new (out for review) python format stuff:
https://lore.kernel.org/lkml/[email protected]/

Ian

Quoted 43 lines
Quoted 42 lines
Quoted 1 line

Thanks,
Ian

Quoted 35 lines
---
 arch/x86/events/amd/core.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/x86/events/amd/core.c +15 -1
index afe4a809f2ed..685bfa860d67 100644
@@ -273,8 +273,23 @@ static const u64 amd_zen2_perfmon_event_map[PERF_COUNT_HW_MAX] =
        [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00a9,
 };
 
+static const u64 amd_zen4_perfmon_event_map[PERF_COUNT_HW_MAX] =
+{
+       [PERF_COUNT_HW_CPU_CYCLES]              = 0x0076,
+       [PERF_COUNT_HW_INSTRUCTIONS]            = 0x00c0,
+       [PERF_COUNT_HW_CACHE_REFERENCES]        = 0xff60,
+       [PERF_COUNT_HW_CACHE_MISSES]            = 0x0964,
+       [PERF_COUNT_HW_BRANCH_INSTRUCTIONS]     = 0x00c2,
+       [PERF_COUNT_HW_BRANCH_MISSES]           = 0x00c3,
+       [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = 0x00a9,
+       [PERF_COUNT_HW_REF_CPU_CYCLES]          = 0x100000120,
+};
+
 static u64 amd_pmu_event_map(int hw_event)
 {
+       if (cpu_feature_enabled(X86_FEATURE_ZEN4) || boot_cpu_data.x86 >= 0x1a)
+               return amd_zen4_perfmon_event_map[hw_event];
+
        if (cpu_feature_enabled(X86_FEATURE_ZEN2) || boot_cpu_data.x86 >= 0x19)
                return amd_zen2_perfmon_event_map[hw_event];
 
--

2.34.1