Skip to main content

Crate t2t_clock

Crate t2t_clock 

Source
Expand description

Clocks that read the time: the OS’s wall and monotonic clocks, the CPU’s counter, and clocks set by hand.

Each clock gives its reading through one verb, Clock::now, and the reading’s type says which timeline it came from: subtracting an Uptime from a Timestamp does not compile.

ClockReadsSteps?For
SystemClockTimestampyesa stamp other machines compare
CoarseSystemClockTimestampyeswhether a heartbeat or expiry is due
TaiClock (Linux)TaiTimestampyesa stamp on the timescale PTP keeps
MonotonicClockUptimenevera deadline, a timeout
CoarseMonotonicClockUptimenevera far deadline, polled often
RawMonotonicClockRawUptimenevera span no time service’s slewing touches
BootClockBootUptimenevera timeout that runs through a suspension
ProcessCpuClockTimedeltaneverthe CPU time the process has used
ThreadCpuClockTimedeltaneverthe CPU time the calling thread has used
CounterTickstampnevera stamp or a span in one instruction
ManualClockany pointby handa test or a replay on one thread
AtomicManualClockany pointby handa test or a replay shared across threads

The OS clocks need the std feature, on 64-bit Linux or macOS, and read the clock the table’s name says on each; each clock’s docs give the ids. Counter reads the virtual counter on aarch64 and the time-stamp counter on x86_64, with or without std.

§Examples

use t2t_clock::{Clock, ManualClock};
use t2t_core::{Timedelta, Timestamp};

/// Whether the sample stamped at `stamp` is older than a second.
fn is_stale<C: Clock<Reading = Timestamp>>(clock: &C, stamp: Timestamp) -> bool {
    clock.now() - stamp > Timedelta::SECOND
}

let clock = ManualClock::new(Timestamp::from_secs(10));
assert!(!is_stale(&clock, Timestamp::from_secs(10)), "fresh at capture");
clock.advance(Timedelta::from_secs(2));
assert!(is_stale(&clock, Timestamp::from_secs(10)), "stale two seconds on");

§Crate Features

None is on by default, and nothing reaches the operating system unless std is named; what std adds is on 64-bit Linux and macOS.

FeatureAdds
stdthe OS clocks, and an x86_64 counter’s measured rate

Structs§

AtomicManualClock
A clock set and moved by hand, shared across threads.
BootClock64-bit and std and (Linux or macOS)
The boot clock, which counts the time the machine was suspended: CLOCK_BOOTTIME on Linux, CLOCK_MONOTONIC on macOS.
CoarseMonotonicClock64-bit and std and (Linux or macOS)
The monotonic clock as the kernel’s timer last left it: CLOCK_MONOTONIC_COARSE on Linux, CLOCK_UPTIME_RAW_APPROX on macOS.
CoarseSystemClock64-bit and std and (Linux or macOS)
The wall clock as the kernel’s timer last left it: CLOCK_REALTIME_COARSE on Linux, CLOCK_REALTIME on macOS, which has no coarse wall clock.
CounterAArch64 or x86-64
The CPU’s free-running counter: cntvct_el0 on aarch64, the time-stamp counter on x86_64.
ManualClock
A clock set and moved by hand, read on one thread.
MonotonicClock64-bit and std and (Linux or macOS)
The monotonic clock: CLOCK_MONOTONIC on Linux, CLOCK_UPTIME_RAW on macOS.
ProcessCpuClock64-bit and std and (Linux or macOS)
The CPU time the process has used, on every thread, CLOCK_PROCESS_CPUTIME_ID.
RawMonotonicClock64-bit and std and (Linux or macOS)
The monotonic clock at the hardware’s own rate: CLOCK_MONOTONIC_RAW on Linux, CLOCK_UPTIME_RAW on macOS.
SystemClock64-bit and std and (Linux or macOS)
The wall clock, CLOCK_REALTIME.
TaiClock64-bit and Linux and std and (Linux or macOS)
International Atomic Time, CLOCK_TAI, on Linux.
ThreadCpuClock64-bit and std and (Linux or macOS)
The CPU time the calling thread has used, CLOCK_THREAD_CPUTIME_ID.

Enums§

CounterErrorAArch64 or x86-64
Why Counter::discover found no counter to read as time.

Traits§

Clock
A clock: what time it is now.