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.
| Clock | Reads | Steps? | For |
|---|---|---|---|
SystemClock | Timestamp | yes | a stamp other machines compare |
CoarseSystemClock | Timestamp | yes | whether a heartbeat or expiry is due |
TaiClock (Linux) | TaiTimestamp | yes | a stamp on the timescale PTP keeps |
MonotonicClock | Uptime | never | a deadline, a timeout |
CoarseMonotonicClock | Uptime | never | a far deadline, polled often |
RawMonotonicClock | RawUptime | never | a span no time service’s slewing touches |
BootClock | BootUptime | never | a timeout that runs through a suspension |
ProcessCpuClock | Timedelta | never | the CPU time the process has used |
ThreadCpuClock | Timedelta | never | the CPU time the calling thread has used |
Counter | Tickstamp | never | a stamp or a span in one instruction |
ManualClock | any point | by hand | a test or a replay on one thread |
AtomicManualClock | any point | by hand | a 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.
| Feature | Adds |
|---|---|
std | the OS clocks, and an x86_64 counter’s measured rate |
Structs§
- Atomic
Manual Clock - A clock set and moved by hand, shared across threads.
- Boot
Clock - The boot clock, which counts the time the machine was suspended:
CLOCK_BOOTTIMEon Linux,CLOCK_MONOTONICon macOS. - Coarse
Monotonic Clock - The monotonic clock as the kernel’s timer last left it:
CLOCK_MONOTONIC_COARSEon Linux,CLOCK_UPTIME_RAW_APPROXon macOS. - Coarse
System Clock - The wall clock as the kernel’s timer last left it:
CLOCK_REALTIME_COARSEon Linux,CLOCK_REALTIMEon macOS, which has no coarse wall clock. - Counter
- The CPU’s free-running counter:
cntvct_el0onaarch64, the time-stamp counter onx86_64. - Manual
Clock - A clock set and moved by hand, read on one thread.
- Monotonic
Clock - The monotonic clock:
CLOCK_MONOTONICon Linux,CLOCK_UPTIME_RAWon macOS. - Process
CpuClock - The CPU time the process has used, on every thread,
CLOCK_PROCESS_CPUTIME_ID. - RawMonotonic
Clock - The monotonic clock at the hardware’s own rate:
CLOCK_MONOTONIC_RAWon Linux,CLOCK_UPTIME_RAWon macOS. - System
Clock - The wall clock,
CLOCK_REALTIME. - TaiClock
- International Atomic Time,
CLOCK_TAI, on Linux. - Thread
CpuClock - The CPU time the calling thread has used,
CLOCK_THREAD_CPUTIME_ID.
Enums§
- Counter
Error - Why
Counter::discoverfound no counter to read as time.
Traits§
- Clock
- A clock: what time it is now.