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

Optimize clock_gettime. #734

Merged
merged 1 commit into from
Jul 11, 2023
Merged
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
6 changes: 6 additions & 0 deletions src/backend/linux_raw/vdso_wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub(crate) fn clock_gettime(which_clock: ClockId) -> __kernel_timespec {
None => init_clock_gettime(),
};
let r0 = callee(which_clock as c::c_int, result.as_mut_ptr());
// The `ClockId` enum only contains clocks which never fail. It may be
// tempting to change this to `debug_assert_eq`, however they can still
// fail on uncommon kernel configs, so we leave this in place to ensure
// that we don't execute UB if they ever do fail.
assert_eq!(r0, 0);
result.assume_init()
}
Expand Down Expand Up @@ -227,6 +231,7 @@ pub(super) type SyscallType = unsafe extern "C" fn();

/// Initialize `CLOCK_GETTIME` and return its value.
#[cfg(feature = "time")]
#[cold]
fn init_clock_gettime() -> ClockGettimeType {
init();
// SAFETY: Load the function address from static storage that we
Expand All @@ -236,6 +241,7 @@ fn init_clock_gettime() -> ClockGettimeType {

/// Initialize `SYSCALL` and return its value.
#[cfg(target_arch = "x86")]
#[cold]
fn init_syscall() -> SyscallType {
init();
// SAFETY: Load the function address from static storage that we
Expand Down
85 changes: 85 additions & 0 deletions tests/time/clocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//! Test all the `ClockId` clocks, which should never fail.

#![cfg(not(any(apple, target_os = "wasi")))]

#[cfg(not(any(solarish, target_os = "netbsd", target_os = "redox")))]
use rustix::time::{clock_gettime, ClockId};

/// Attempt to test that the uptime clock is monotonic. Time may or may not
/// advance, but it shouldn't regress.
#[cfg(any(freebsdlike, target_os = "openbsd"))]
#[test]
fn test_uptime_clock() {
let a = clock_gettime(ClockId::Uptime);
let b = clock_gettime(ClockId::Uptime);
if b.tv_sec == a.tv_sec {
assert!(b.tv_nsec >= a.tv_nsec);
} else {
assert!(b.tv_sec > a.tv_sec);
}
}

/// Attempt to test that the process CPU-time clock is monotonic. Time may or
/// may not advance, but it shouldn't regress.
#[cfg(not(any(solarish, target_os = "netbsd", target_os = "redox")))]
#[test]
fn test_process_cputime_clock() {
let a = clock_gettime(ClockId::ProcessCPUTime);
let b = clock_gettime(ClockId::ProcessCPUTime);
if b.tv_sec == a.tv_sec {
assert!(b.tv_nsec >= a.tv_nsec);
} else {
assert!(b.tv_sec > a.tv_sec);
}
}

/// Attempt to test that the thread CPU-time clock is monotonic. Time may or
/// may not advance, but it shouldn't regress.
#[cfg(not(any(solarish, target_os = "netbsd", target_os = "redox")))]
#[test]
fn test_thread_cputime_clock() {
let a = clock_gettime(ClockId::ThreadCPUTime);
let b = clock_gettime(ClockId::ThreadCPUTime);
if b.tv_sec == a.tv_sec {
assert!(b.tv_nsec >= a.tv_nsec);
} else {
assert!(b.tv_sec > a.tv_sec);
}
}

#[cfg(any(linux_kernel, target_os = "freebsd"))]
#[test]
fn test_realtime_coarse_clock() {
let a = clock_gettime(ClockId::RealtimeCoarse);

// Test that the timespec is valid; there's not much else we can say.
assert!(a.tv_nsec < 1_000_000_000);
}

/// Attempt to test that the coarse monotonic clock is monotonic. Time may or
/// may not advance, but it shouldn't regress.
#[cfg(any(linux_kernel, target_os = "freebsd"))]
#[test]
fn test_monotonic_coarse_clock() {
let a = clock_gettime(ClockId::MonotonicCoarse);
let b = clock_gettime(ClockId::MonotonicCoarse);
if b.tv_sec == a.tv_sec {
assert!(b.tv_nsec >= a.tv_nsec);
} else {
assert!(b.tv_sec > a.tv_sec);
}
}

/// Attempt to test that the raw monotonic clock is monotonic. Time may or
/// may not advance, but it shouldn't regress.
#[cfg(linux_kernel)]
#[test]
fn test_monotonic_raw_clock() {
let a = clock_gettime(ClockId::MonotonicRaw);
let b = clock_gettime(ClockId::MonotonicRaw);
if b.tv_sec == a.tv_sec {
assert!(b.tv_nsec >= a.tv_nsec);
} else {
assert!(b.tv_sec > a.tv_sec);
}
}
1 change: 1 addition & 0 deletions tests/time/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![cfg(feature = "time")]
#![cfg(not(any(windows, target_os = "espidf")))]

mod clocks;
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
mod dynamic_clocks;
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
Expand Down