perf riscv: Implement rdtsc()
Subsystems (3)
v2 cover perf riscv: Implement rdtsc()
tools/perf falls back to the __weak rdtsc() stub in tools/perf/util/tsc.c on every architecture that does not override it, riscv included. The stub returns a constant 0, so anything that reaches for the counter gets a value that means nothing and cannot tell it apart from a counter that happens to be at 0.
The time CSR is a natural TSC equivalent on riscv: readable from user mode, constant frequency, and the same counter that riscv's arch_perf_update_userpage() feeds the user page conversion fields from, ever since commit 83c5e13b8cbb ("riscv: Prepare for user-space perf event mmap support"). This series registers that on the tools side.
The two patches are split so that the first stands on its own:
1. Implement rdtsc() over the time CSR for riscv64.
2. Have the build system say which architectures have a usable counter, instead of a list of compiler macros inside the test.
What this is worth beyond the subtest result:
1. The subtest is the only reader of those conversion fields riscv can run, so it guards arithmetic that has shipped unexercised; registering the counter at the same time leaves a future riscv auxtrace driver the reading and the conversion already in place.
2. The flag lets a caller ask whether the architecture has a counter before it uses the value, without keeping its own copy of a list that has to be edited every time an architecture grows an implementation.
Changes since v1:
- Define rdtsc() for RV64 only. RV32 now keeps the weak stub.
- Declare the capability through the build system rather than through a weak arch__rdtsc_supported() hook, following the PERF_HAVE_JITDUMP pattern, as Leo Yan suggested.
Tests:
======
- riscv64 QEMU virt guest with sscofpmf, comparing the build before the series with the build after it: before, the "TSC support" subtest skips and the conversion subtest fails while comparing against the stub's constant 0; after, both report Ok.
- riscv64 and riscv32 cross builds: the compile command of tests/perf-time-to-tsc.o carries -DHAVE_RDTSC on rv64 and not on rv32, so RV32 stays on the weak stub.
- x86_64 host and arm64 cross builds: -DHAVE_RDTSC is in effect for both, and on the host the "TSC support" subtest still reports Ok.
Chen Pei (2):
perf riscv: Implement rdtsc()
perf tsc: Declare TSC support from the build system
tools/perf/Makefile.config | 4 ++++ tools/perf/arch/arm64/Makefile | 1 + tools/perf/arch/riscv/Makefile | 1 + tools/perf/arch/riscv/util/Build | 1 + tools/perf/arch/riscv/util/tsc.c | 27 +++++++++++++++++++++++++++ tools/perf/arch/x86/Makefile | 1 + tools/perf/tests/perf-time-to-tsc.c | 19 ++++--------------- 7 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 tools/perf/arch/riscv/util/tsc.c
Signature · 1 line
2.50.1
PATCH v2 1/2 perf riscv: Implement rdtsc()
Perf has no riscv implementation of rdtsc(), so it links against the weak stub in util/tsc.c and every caller reads a constant 0 that cannot be told apart from a counter standing still.
Implement rdtsc() over the time CSR with rdtime. The time CSR is a TSC equivalent on riscv: readable from user mode, constant frequency, and the same counter riscv's arch_perf_update_userpage() builds the user page conversion fields from, so the counter and perf time share a timebase.
Define it for RV64 only. Reading the counter whole on RV32 takes time and timeh in a loop, and no RV32 user in tools/perf needs it that much, so RV32 keeps the weak stub.
---
Changes in v2:
- Define rdtsc() for RV64 only.
tools/perf/arch/riscv/util/Build | 1 + tools/perf/arch/riscv/util/tsc.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tools/perf/arch/riscv/util/tsc.c
tools/perf/arch/riscv/util/Build +1 -0
@@ -1 +1,2 @@ perf-util-y += header.o +perf-util-y += tsc.o
tools/perf/arch/riscv/util/tsc.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/types.h> + +#include "../../../util/tsc.h" + +/* +* RV32 is left to the weak stub: reading the counter whole there takes time +* and timeh in a loop, and no RV32 user in tools/perf needs it that much. +*/ +#if defined(__riscv) && __riscv_xlen ==64 + +u64 rdtsc (void ) +{ +u64 val; + +/* +* The time CSR ticks at a constant frequency and is the same counter +* the kernel feeds into the user page conversion fields through +* sched_clock(), so it is a TSC equivalent here. +*/ + asmvolatile ("rdtime %0" :"=r" (val)); + +return val; +} + +#endif
Signature · 1 line
2.50.1
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
Perf has no riscv implementation of
rdtsc(), so it links against the weak stub inutil/tsc.cand every caller reads a constant 0 that cannot be told apart from a counter standing still.Implement
rdtsc()over the time CSR with rdtime. The time CSR is a TSC equivalent on riscv: readable from user mode, constant frequency, and the same counter riscv'sarch_perf_update_userpage()builds the user page conversion fields from, so the counter and perf time share a timebase.
Quoted 6 lines
Define it for RV64 only. Reading the counter whole on RV32 takes time and timeh in a loop, and no RV32 user in
tools/perfneeds it that much, so RV32 keeps the weak stub.Reviewed-by Anup Patel <[email protected]>Signed-off-by Chen Pei <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 9 lines · Chen Pei
Perf has no riscv implementation of
rdtsc(), so it links against the weak stub inutil/tsc.cand every caller reads a constant 0 that cannot be told apart from a counter standing still.Implement
rdtsc()over the time CSR with rdtime. The time CSR is a TSC equivalent on riscv: readable from user mode, constant frequency, and the same counter riscv'sarch_perf_update_userpage()builds the user page conversion fields from, so the counter and perf time share a timebase.
Quoted 6 lines
Define it for RV64 only. Reading the counter whole on RV32 takes time and timeh in a loop, and no RV32 user in
tools/perfneeds it that much, so RV32 keeps the weak stub.Reviewed-by Anup Patel <[email protected]>Signed-off-by Chen Pei <[email protected]>
- Paul
PATCH v2 2/2 perf tsc: Declare TSC support from the build system
In reply to Chen PeiThe "TSC support" subtest of the "Convert perf time to TSC" test decides whether the architecture is covered by matching compiler macros for x86_64, i386 and arm64. That list has to be edited for every new architecture and can drift apart from the set of architectures that really provide a counter.
Declare the capability where the implementation is, following the PERF_HAVE_JITDUMP pattern: an architecture with a usable counter sets PERF_HAVE_RDTSC in its arch Makefile, Makefile.config turns it into -DHAVE_RDTSC, and the test keys off that.
x86 and arm64 keep the same behaviour, riscv starts running the subtest where the previous patch defines rdtsc().
---
Changes in v2:
- Declare the capability through the build system instead of a weak arch__rdtsc_supported() hook.
tools/perf/Makefile.config | 4 ++++ tools/perf/arch/arm64/Makefile | 1 + tools/perf/arch/riscv/Makefile | 1 + tools/perf/arch/x86/Makefile | 1 + tools/perf/tests/perf-time-to-tsc.c | 19 ++++--------------- 5 files changed, 11 insertions(+), 15 deletions(-)
tools/perf/Makefile.config +4 -0
@@ -618,6 +618,10 @@ ifdef PERF_HAVE_JITDUMP endif endif +ifeq ($(PERF_HAVE_RDTSC),1) + CFLAGS += -DHAVE_RDTSC +endif + ifeq ($(SRCARCH),powerpc) ifndef NO_LIBDW CFLAGS += -DHAVE_SKIP_CALLCHAIN_IDX
tools/perf/arch/arm64/Makefile +1 -0
@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 PERF_HAVE_JITDUMP := 1 +PERF_HAVE_RDTSC := 1
tools/perf/arch/riscv/Makefile +1 -0
@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 PERF_HAVE_JITDUMP := 1 +PERF_HAVE_RDTSC := $(IS_64_BIT)
tools/perf/arch/x86/Makefile +1 -0
@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 PERF_HAVE_JITDUMP := 1 +PERF_HAVE_RDTSC := 1
tools/perf/tests/perf-time-to-tsc.c +4 -15
@@ -22,16 +22,6 @@#include "tests.h" #include "util/sample.h" -/* -* Except x86_64/i386 and Arm64, other archs don't support TSC in perf. Just -* enable the test for x86_64/i386 and Arm64 archs. -*/ -#if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__) -#define TSC_IS_SUPPORTED 1 -#else -#define TSC_IS_SUPPORTED 0 -#endif -#define CHECK__ (x) { \ while ((x) < 0) { \ pr_debug(#x " failed!\n"); \
@@ -49,11 +39,10 @@static int test__tsc_is_supported (struct test_suite *test__maybe_unused ,int subtest__maybe_unused ) { -if (!TSC_IS_SUPPORTED ) { -pr_debug ("Test not supported on this architecture\n" ); -return TEST_SKIP ; - } - +#ifndef HAVE_RDTSC +pr_debug ("Test not supported on this architecture\n" ); +return TEST_SKIP ; +#endif return TEST_OK ; }
Signature · 1 line
2.50.1
[email protected]
AI reviewRe: [PATCH v2 2/2] perf tsc: Declare TSC support from the build system
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
The "TSC support" subtest of the "Convert perf time to TSC" test decides whether the architecture is covered by matching compiler macros for
x86_64, i386 and arm64. That list has to be edited for every new architecture and can drift apart from the set of architectures that really provide a counter.Declare the capability where the implementation is, following the
PERF_HAVE_JITDUMPpattern: an architecture with a usable counter setsPERF_HAVE_RDTSCin its archMakefile, Makefile.config turns it into -DHAVE_RDTSC, and the test keys off that.
Quoted 4 lines
x86 and arm64 keep the same behaviour, riscv starts running the subtest where the previous patch defines
rdtsc().Signed-off-by Chen Pei <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v2 2/2] perf tsc: Declare TSC support from the build system
In reply to Chen PeiQuoted 4 lines · Chen Pei
x86 and arm64 keep the same behaviour, riscv starts running the subtest where the previous patch defines
rdtsc().Signed-off-by Chen Pei <[email protected]>
For arm64:
Re: [PATCH v2 2/2] perf tsc: Declare TSC support from the build system
In reply to Chen PeiQuoted 11 lines · Chen Pei
The "TSC support" subtest of the "Convert perf time to TSC" test decides whether the architecture is covered by matching compiler macros for
x86_64, i386 and arm64. That list has to be edited for every new architecture and can drift apart from the set of architectures that really provide a counter.Declare the capability where the implementation is, following the
PERF_HAVE_JITDUMPpattern: an architecture with a usable counter setsPERF_HAVE_RDTSCin its archMakefile, Makefile.config turns it into -DHAVE_RDTSC, and the test keys off that.
Quoted 4 lines
x86 and arm64 keep the same behaviour, riscv starts running the subtest where the previous patch defines
rdtsc().Signed-off-by Chen Pei <[email protected]>
One minor style comment below. In any case:
Quoted 75 lines
---
Changes in v2:
- Declare the capability through the build system instead of a weakarch__rdtsc_supported()hook.tools/perf/Makefile.config | 4 ++++ tools/perf/arch/arm64/Makefile | 1 + tools/perf/arch/riscv/Makefile | 1 + tools/perf/arch/x86/Makefile | 1 + tools/perf/tests/perf-time-to-tsc.c | 19 ++++--------------- 5 files changed, 11 insertions(+), 15 deletions(-)tools/perf/Makefile.config +4
-0@@ -618,6 +618,10 @@ ifdef PERF_HAVE_JITDUMP endif endif +ifeq ($(PERF_HAVE_RDTSC),1) + CFLAGS += -DHAVE_RDTSC +endif + ifeq ($(SRCARCH),powerpc) ifndef NO_LIBDW CFLAGS += -DHAVE_SKIP_CALLCHAIN_IDXtools/perf/arch/arm64/Makefile +1
-0@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 PERF_HAVE_JITDUMP := 1 +PERF_HAVE_RDTSC := 1tools/perf/arch/riscv/Makefile +1
-0@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 PERF_HAVE_JITDUMP := 1 +PERF_HAVE_RDTSC := $(IS_64_BIT)tools/perf/arch/x86/Makefile +1
-0@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 PERF_HAVE_JITDUMP := 1 +PERF_HAVE_RDTSC := 1tools/perf/tests/perf-time-to-tsc.c +0
-10@@ -22,16 +22,6 @@#include "tests.h" #include "util/sample.h" -/* -* Except x86_64/i386 and Arm64, other archs don't support TSC in perf. Just -* enable the test for x86_64/i386 and Arm64 archs. -*/ -#if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__) -#define TSC_IS_SUPPORTED 1 -#else -#define TSC_IS_SUPPORTED 0 -#endif -#define CHECK__ (x) { \ while ((x) < 0) { \ pr_debug(#x " failed!\n"); \@@ -49,11 +39,10 @@static int test__tsc_is_supported (struct test_suite *test__maybe_unused ,int subtest__maybe_unused )
tools/perf/tests/perf-time-to-tsc.c line 45
Quoted 10 lines
tools/perf/tests/perf-time-to-tsc.c +4
-5{ -if (!TSC_IS_SUPPORTED ) { -pr_debug ("Test not supported on this architecture\n" ); -return TEST_SKIP ; - } - +#ifndef HAVE_RDTSC +pr_debug ("Test not supported on this architecture\n" ); +return TEST_SKIP ; +#endif
Perhaps with #include <tools/config.h> the following is tidier:
if (!IS_BUILTIN(HAVE_RDTSC)) {
pr_debug("Test not supported on this architecture\n");
return TEST_SKIP;
}Quoted 3 lines
tools/perf/tests/perf-time-to-tsc.c +0
-0return TEST_OK ; }
Re: [PATCH v2 2/2] perf tsc: Declare TSC support from the build system
Quoted 12 lines · Adrian Hunter
Quoted 11 lines · Chen Pei
The "TSC support" subtest of the "Convert perf time to TSC" test decides whether the architecture is covered by matching compiler macros for
x86_64, i386 and arm64. That list has to be edited for every new architecture and can drift apart from the set of architectures that really provide a counter.Declare the capability where the implementation is, following the
PERF_HAVE_JITDUMPpattern: an architecture with a usable counter setsPERF_HAVE_RDTSCin its archMakefile, Makefile.config turns it into -DHAVE_RDTSC, and the test keys off that.
Quoted 8 lines
Quoted 4 lines
x86 and arm64 keep the same behaviour, riscv starts running the subtest where the previous patch defines
rdtsc().Signed-off-by Chen Pei <[email protected]>One minor style comment below. In any case:
Reviewed-by Adrian Hunter <[email protected]>
- Paul
Re: [PATCH v2 2/2] perf tsc: Declare TSC support from the build system
In reply to Adrian HunterHi Adrian,
Quoted 6 lines · Adrian Hunter
Perhaps with #include <
tools/config.h> the following is tidier:if (!IS_BUILTIN(HAVE_RDTSC)) { pr_debug("Test not supported on this architecture\n"); return TEST_SKIP; }
Thanks for the review. I'll change it as suggested and post v3.
Best regards,
Pei