Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unscaledcycleclock: use RDTIME instead of RDCYCLE on RISC-V #1631

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion absl/base/internal/unscaledcycleclock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion absl/base/internal/unscaledcycleclock_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need an implementation of UnscaledCycleClock::Frequency() now that these are not the same thing. The code in this repo may not call that method, but code exists that does.

https://riscv.org/wp-content/uploads/2016/06/riscv-spec-v2.1.pdf says

The RDTIME pseudo-instruction reads the low XLEN bits of the time CSR, which counts wall-clock
real time that has passed from an arbitrary start time in the past. RDTIMEH is an RV32I-only instruction that reads bits 63–32 of the same real-time counter. The underlying 64-bit counter should
never overflow in practice. The execution environment should provide a means of determining the
period of the real-time counter (seconds/tick). The period must be constant. The real-time clocks
of all hardware threads in a single user application should be synchronized to within one tick of the
real-time clock. The environment should provide a means to determine the accuracy of the clock.

I have no idea how to get that.

If you are just trying to get something to build, -DABSL_USE_UNSCALED_CYCLECLOCK=0 should work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The frequency is implementation dependent, and there is no easy way to get it. Could it be measured instead? Basically how it is done for the ABSL_INTERNAL_UNSCALED_CYCLECLOCK_FREQUENCY_IS_CPU_FREQUENCY?

#endif
Expand Down