From a2483cea0e218cf67ff18b2856b970feaf906bab Mon Sep 17 00:00:00 2001 From: Aurelien Jarno Date: Thu, 29 Feb 2024 19:46:14 +0100 Subject: [PATCH] unscaledcycleclock: use RDTIME instead of RDCYCLE on RISC-V Starting with Linux 6.6 [1], RDCYCLE is a privileged instruction on RISC-V and can't be used directly from userland. There is a sysctl option to change that as a transition period, but it will eventually disappear. Use RDTIME instead, which while less accurate has the advantage of being synchronized between CPU (and thus monotonic) and of constant frequency. Therefore this change also fixes another issue caused by wrong time measurement when a process is rescheduled from one CPU to another [2]. Given RDTIME uses a timer independent from the CPU, this patche removes the ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY define accordingly. This fixes the testsuite on riscv64, tested on a VisionFive 2 board. [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=cc4c07c89aada16229084eeb93895c95b7eabaa3 [2] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1025221 --- absl/base/internal/unscaledcycleclock.cc | 2 +- absl/base/internal/unscaledcycleclock_config.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/absl/base/internal/unscaledcycleclock.cc b/absl/base/internal/unscaledcycleclock.cc index 05e0e7ba5ee..06374915c0d 100644 --- a/absl/base/internal/unscaledcycleclock.cc +++ b/absl/base/internal/unscaledcycleclock.cc @@ -125,7 +125,7 @@ double UnscaledCycleClock::Frequency() { int64_t UnscaledCycleClock::Now() { int64_t virtual_timer_value; - asm volatile("rdcycle %0" : "=r"(virtual_timer_value)); + asm volatile("rdtime %0" : "=r"(virtual_timer_value)); return virtual_timer_value; } diff --git a/absl/base/internal/unscaledcycleclock_config.h b/absl/base/internal/unscaledcycleclock_config.h index 24b324ac99f..9f21a1da6e6 100644 --- a/absl/base/internal/unscaledcycleclock_config.h +++ b/absl/base/internal/unscaledcycleclock_config.h @@ -53,7 +53,7 @@ #if ABSL_USE_UNSCALED_CYCLECLOCK // This macro can be used to test if UnscaledCycleClock::Frequency() // is NominalCPUFrequency() on a particular platform. -#if (defined(__i386__) || defined(__x86_64__) || defined(__riscv) || \ +#if (defined(__i386__) || defined(__x86_64__) || \ defined(_M_IX86) || defined(_M_X64)) #define ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY #endif