Crate os_clock

Source
Expand description

Access various operating system clocks (such as per-thread CPU Time, system clock, monotomic, etc) on Unix-family systems.

§Thread clocks:

Sendable per-thread CPU clocks are unique to this crate:

let clock = os_clock::cpu_clock_for_current_thread().unwrap();

let start_time = clock.get_time().unwrap();
// Do some work for 5ms...
assert!(clock.get_time().unwrap() > start_time + Duration::from_millis(5));

// Notably, a clock for the CPU time of one thread can be accessed from another thread:
std::thread::spawn(move || {
    assert!(clock.get_time().unwrap() > Duration::from_millis(5));

    let self_clock = os_clock::cpu_clock_for_current_thread().unwrap();
    assert!(self_clock.get_time().unwrap() < Duration::from_millis(1));
})
.join()

// Clocks count from the thread's spawn time
let new_clock = os_clock::cpu_clock_for_current_thread().unwrap();
assert!(new_clock.get_time().unwrap() > Duration::from_millis(5));

// Use a timer to start counting from the moment the timer is created
let timer = new_clock.start_timer().unwrap();
assert!(timer.elapsed().unwrap() < Duration::from_millis(1));
// Do some work for 5ms...
assert!(timer.elapsed().unwrap() > Duration::from_millis(5));

Structs§

PosixClock
The POSIX clockid_t represents numerous types of clocks (wall, cpu, etc) However, they share a common API
Timer
Helper to measure time elapsed

Constants§

MONOTONIC_CLOCK
The system-wide monotonic clock (defined as a clock that cannot be set and cannot have backwards clock jumps)
PROCESS_CLOCK
The process-wide cpu-time clock
REALTIME_CLOCK
The system-wide realtime clock

Traits§

Clock

Functions§

cpu_clock_for_current_thread
Get a clock for the CPU time of the current thread
get_current_thread_cpu_time
Get the CPU time of the current thread

Type Aliases§

ThreadCPUClock
A type alias for compatibility with Mach