1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//! The `clock` module returns time values derived from the Posix / C
//! [CLOCK_GETTIME](http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html)
//! function or equivalent.
//!
//! Most functions in the module return a number of seconds; functions whose names end in “64”
//! return a 64-bit number of nanoseconds.
//!
//! - [time()](fn.time.html) - Get the wall clock time in seconds
//! - [time64()](fn.time64.html) - Get the wall clock time in nanoseconds
//! - [monotonic()](fn.monotonic.html) - Get the monotonic time in seconds
//! - [monotonic64()](fn.monotonic64.html) - Get the monotonic time in nanoseconds
//! - [proc()](fn.proc.html) - Get the processor time in seconds
//! - [proc64()](fn.proc64.html) - Get the processor time in nanoseconds
//! - [thread()](fn.thread.html) - Get the thread time in seconds
//! - [thread64()](fn.thread64.html) - Get the thread time in nanoseconds
//!
//! See also:
//! - [Lua reference: Module clock](https://www.tarantool.io/en/doc/latest/reference/reference_lua/clock/)
//! - [C API reference: Module clock](https://www.tarantool.io/en/doc/latest/dev_guide/reference_capi/clock/)

/// The wall clock time.
///
/// Derived from C function `clock_gettime(CLOCK_REALTIME)`.
/// This is the best function for knowing what the official time is, as determined by the system administrator.
///
/// Return: seconds or nanoseconds since epoch (1970-01-01 00:00:00), adjusted.
/// Return type: `u64` or `f64`
///
/// Example:
/// ```rust
/// // This will print an approximate number of years since 1970.
/// use tarantool_module::clock::time;
/// println!("{}", time() / (365 * 24 * 60 * 60));
/// ```
///
/// See also: [fiber::time()](../fiber/fn.time.html), [fiber::time64()](../fiber/fn.time64.html)
#[inline(always)]
pub fn time() -> f64 {
    unsafe { ffi::clock_realtime() }
}

/// See: [time()](fn.time.html)
#[inline(always)]
pub fn time64() -> u64 {
    unsafe { ffi::clock_realtime64() }
}

/// The monotonic time.
///
/// Derived from C function `clock_gettime(CLOCK_MONOTONIC)`.
/// Monotonic time is similar to wall clock time but is not affected by changes to or from daylight saving time, or by
/// changes done by a user. This is the best function to use with benchmarks that need to calculate elapsed time.
///
/// Return: seconds or nanoseconds since the last time that the computer was booted.
/// Return type: `u64` or `f64`
///
/// Example:
/// ```rust
/// // This will print nanoseconds since the start.
/// use tarantool_module::clock::monotonic64;
/// println!("{}", monotonic64());
/// ```
#[inline(always)]
pub fn monotonic() -> f64 {
    unsafe { ffi::clock_monotonic() }
}

/// See: [monotonic()](fn.monotonic.html)
#[inline(always)]
pub fn monotonic64() -> u64 {
    unsafe { ffi::clock_monotonic64() }
}

/// The processor time.
///
/// Derived from C function `clock_gettime(CLOCK_PROCESS_CPUTIME_ID)`.
/// This is the best function to use with benchmarks that need to calculate how much time has been spent within a CPU.
///
/// Return: seconds or nanoseconds since processor start.
/// Return type: `u64` or `f64`
///
/// Example:
/// ```rust
/// // This will print nanoseconds in the CPU since the start.
/// use tarantool_module::clock::process64;
/// println!("{}", process64());
/// ```
#[inline(always)]
pub fn process() -> f64 {
    unsafe { ffi::clock_process() }
}

/// See: [process()](fn.process.html)
#[inline(always)]
pub fn process64() -> u64 {
    unsafe { ffi::clock_process64() }
}

/// The thread time.
///
/// Derived from C function `clock_gettime(CLOCK_THREAD_CPUTIME_ID)`.
/// This is the best function to use with benchmarks that need to calculate how much time has been spent within a
/// thread within a CPU.
///
/// Return: seconds or nanoseconds since the transaction processor thread started.
/// Return type: `u64` or `f64`
///
/// Example:
/// ```rust
/// // This will print seconds in the thread since the start.
/// use tarantool_module::clock::thread64;
/// println!("{}", thread64());
/// ```
#[inline(always)]
pub fn thread() -> f64 {
    unsafe { ffi::clock_thread() }
}

/// See: [thread()](fn.thread.html)
#[inline(always)]
pub fn thread64() -> u64 {
    unsafe { ffi::clock_thread64() }
}

mod ffi {
    extern "C" {
        pub fn clock_realtime() -> f64;
        pub fn clock_monotonic() -> f64;
        pub fn clock_process() -> f64;
        pub fn clock_thread() -> f64;
        pub fn clock_realtime64() -> u64;
        pub fn clock_monotonic64() -> u64;
        pub fn clock_process64() -> u64;
        pub fn clock_thread64() -> u64;
    }
}