tarantool_module/
clock.rs

1//! The `clock` module returns time values derived from the Posix / C
2//! [CLOCK_GETTIME](http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html)
3//! function or equivalent.
4//!
5//! Most functions in the module return a number of seconds; functions whose names end in “64”
6//! return a 64-bit number of nanoseconds.
7//!
8//! - [time()](fn.time.html) - Get the wall clock time in seconds
9//! - [time64()](fn.time64.html) - Get the wall clock time in nanoseconds
10//! - [monotonic()](fn.monotonic.html) - Get the monotonic time in seconds
11//! - [monotonic64()](fn.monotonic64.html) - Get the monotonic time in nanoseconds
12//! - [proc()](fn.proc.html) - Get the processor time in seconds
13//! - [proc64()](fn.proc64.html) - Get the processor time in nanoseconds
14//! - [thread()](fn.thread.html) - Get the thread time in seconds
15//! - [thread64()](fn.thread64.html) - Get the thread time in nanoseconds
16//!
17//! See also:
18//! - [Lua reference: Module clock](https://www.tarantool.io/en/doc/latest/reference/reference_lua/clock/)
19//! - [C API reference: Module clock](https://www.tarantool.io/en/doc/latest/dev_guide/reference_capi/clock/)
20
21/// The wall clock time.
22///
23/// Derived from C function `clock_gettime(CLOCK_REALTIME)`.
24/// This is the best function for knowing what the official time is, as determined by the system administrator.
25///
26/// Return: seconds or nanoseconds since epoch (1970-01-01 00:00:00), adjusted.
27/// Return type: `u64` or `f64`
28///
29/// Example:
30/// ```rust
31/// // This will print an approximate number of years since 1970.
32/// use tarantool_module::clock::time;
33/// println!("{}", time() / (365 * 24 * 60 * 60));
34/// ```
35///
36/// See also: [fiber::time()](../fiber/fn.time.html), [fiber::time64()](../fiber/fn.time64.html)
37#[inline(always)]
38pub fn time() -> f64 {
39    unsafe { ffi::clock_realtime() }
40}
41
42/// See: [time()](fn.time.html)
43#[inline(always)]
44pub fn time64() -> u64 {
45    unsafe { ffi::clock_realtime64() }
46}
47
48/// The monotonic time.
49///
50/// Derived from C function `clock_gettime(CLOCK_MONOTONIC)`.
51/// Monotonic time is similar to wall clock time but is not affected by changes to or from daylight saving time, or by
52/// changes done by a user. This is the best function to use with benchmarks that need to calculate elapsed time.
53///
54/// Return: seconds or nanoseconds since the last time that the computer was booted.
55/// Return type: `u64` or `f64`
56///
57/// Example:
58/// ```rust
59/// // This will print nanoseconds since the start.
60/// use tarantool_module::clock::monotonic64;
61/// println!("{}", monotonic64());
62/// ```
63#[inline(always)]
64pub fn monotonic() -> f64 {
65    unsafe { ffi::clock_monotonic() }
66}
67
68/// See: [monotonic()](fn.monotonic.html)
69#[inline(always)]
70pub fn monotonic64() -> u64 {
71    unsafe { ffi::clock_monotonic64() }
72}
73
74/// The processor time.
75///
76/// Derived from C function `clock_gettime(CLOCK_PROCESS_CPUTIME_ID)`.
77/// This is the best function to use with benchmarks that need to calculate how much time has been spent within a CPU.
78///
79/// Return: seconds or nanoseconds since processor start.
80/// Return type: `u64` or `f64`
81///
82/// Example:
83/// ```rust
84/// // This will print nanoseconds in the CPU since the start.
85/// use tarantool_module::clock::process64;
86/// println!("{}", process64());
87/// ```
88#[inline(always)]
89pub fn process() -> f64 {
90    unsafe { ffi::clock_process() }
91}
92
93/// See: [process()](fn.process.html)
94#[inline(always)]
95pub fn process64() -> u64 {
96    unsafe { ffi::clock_process64() }
97}
98
99/// The thread time.
100///
101/// Derived from C function `clock_gettime(CLOCK_THREAD_CPUTIME_ID)`.
102/// This is the best function to use with benchmarks that need to calculate how much time has been spent within a
103/// thread within a CPU.
104///
105/// Return: seconds or nanoseconds since the transaction processor thread started.
106/// Return type: `u64` or `f64`
107///
108/// Example:
109/// ```rust
110/// // This will print seconds in the thread since the start.
111/// use tarantool_module::clock::thread64;
112/// println!("{}", thread64());
113/// ```
114#[inline(always)]
115pub fn thread() -> f64 {
116    unsafe { ffi::clock_thread() }
117}
118
119/// See: [thread()](fn.thread.html)
120#[inline(always)]
121pub fn thread64() -> u64 {
122    unsafe { ffi::clock_thread64() }
123}
124
125mod ffi {
126    extern "C" {
127        pub fn clock_realtime() -> f64;
128        pub fn clock_monotonic() -> f64;
129        pub fn clock_process() -> f64;
130        pub fn clock_thread() -> f64;
131        pub fn clock_realtime64() -> u64;
132        pub fn clock_monotonic64() -> u64;
133        pub fn clock_process64() -> u64;
134        pub fn clock_thread64() -> u64;
135    }
136}