Skip to content

Commit

Permalink
PR #1644: unscaledcycleclock: remove RISC-V support
Browse files Browse the repository at this point in the history
Imported from GitHub PR #1644

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.

The RDTIME instruction is another less accurate alternative, however its frequency varies from board to board, and there is currently now way to get its frequency from userland [2].

Therefore this patch just removes the code for unscaledcycleclock on RISC-V. Without processor specific implementation, abseil relies on std::chrono::steady_clock::now().time_since_epoch() which is basically a wrapper around clock_gettime (CLOCK_MONOTONIC), which in turns use __vdso_clock_gettime(). On RISC-V this VDSO is just a wrapper around RDTIME correctly scaled to use nanoseconds units.

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] #1631
Merge 43356a2 into 76f8011

Merging this change closes #1644

COPYBARA_INTEGRATE_REVIEW=#1644 from aurel32:rv64-no-unscaledcycleclock 43356a2
PiperOrigin-RevId: 618286262
Change-Id: Ie4120a727e7d0bb185df6e06ea145c780ebe6652
  • Loading branch information
aurel32 authored and Copybara-Service committed Mar 22, 2024
1 parent 9a9502b commit 7335a36
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
12 changes: 0 additions & 12 deletions absl/base/internal/unscaledcycleclock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,6 @@ double UnscaledCycleClock::Frequency() {
return aarch64_timer_frequency;
}

#elif defined(__riscv)

int64_t UnscaledCycleClock::Now() {
int64_t virtual_timer_value;
asm volatile("rdcycle %0" : "=r"(virtual_timer_value));
return virtual_timer_value;
}

double UnscaledCycleClock::Frequency() {
return base_internal::NominalCPUFrequency();
}

#elif defined(_M_IX86) || defined(_M_X64)

#pragma intrinsic(__rdtsc)
Expand Down
8 changes: 4 additions & 4 deletions absl/base/internal/unscaledcycleclock_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

// The following platforms have an implementation of a hardware counter.
#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || \
defined(__powerpc__) || defined(__ppc__) || defined(__riscv) || \
defined(_M_IX86) || (defined(_M_X64) && !defined(_M_ARM64EC))
defined(__powerpc__) || defined(__ppc__) || defined(_M_IX86) || \
(defined(_M_X64) && !defined(_M_ARM64EC))
#define ABSL_HAVE_UNSCALED_CYCLECLOCK_IMPLEMENTATION 1
#else
#define ABSL_HAVE_UNSCALED_CYCLECLOCK_IMPLEMENTATION 0
Expand Down Expand Up @@ -53,8 +53,8 @@
#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) || \
defined(_M_IX86) || defined(_M_X64))
#if (defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
defined(_M_X64))
#define ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY
#endif
#endif
Expand Down

0 comments on commit 7335a36

Please sign in to comment.