|
8 | 8 | /// The time unit of Linux kernel. One jiffy equals (1/HZ) second.
|
9 | 9 | pub type Jiffies = core::ffi::c_ulong;
|
10 | 10 |
|
| 11 | +/// Jiffies, but with a fixed width of 32bit. |
| 12 | +pub type Jiffies32 = u32; |
| 13 | + |
11 | 14 | /// The millisecond time unit.
|
12 | 15 | pub type Msecs = core::ffi::c_uint;
|
13 | 16 |
|
| 17 | +/// Milliseconds per second. |
| 18 | +pub const MSEC_PER_SEC: Msecs = 1000; |
| 19 | + |
| 20 | +/// The milliseconds time unit with a fixed width of 32bit. |
| 21 | +/// |
| 22 | +/// This is used in networking. |
| 23 | +pub type Msecs32 = u32; |
| 24 | + |
| 25 | +/// The microseconds time unit. |
| 26 | +pub type Usecs = u64; |
| 27 | + |
| 28 | +/// Microseconds per millisecond. |
| 29 | +pub const USEC_PER_MSEC: Usecs = 1000; |
| 30 | + |
| 31 | +/// Microseconds per second. |
| 32 | +pub const USEC_PER_SEC: Usecs = 1_000_000; |
| 33 | + |
| 34 | +/// The microseconds time unit with a fixed width of 32bit. |
| 35 | +/// |
| 36 | +/// This is used in networking. |
| 37 | +pub type Usecs32 = u32; |
| 38 | + |
| 39 | +/// The nanosecond time unit. |
| 40 | +pub type Nsecs = u64; |
| 41 | + |
| 42 | +/// Nanoseconds per microsecond. |
| 43 | +pub const NSEC_PER_USEC: Nsecs = 1000; |
| 44 | + |
| 45 | +/// Nanoseconds per millisecond. |
| 46 | +pub const NSEC_PER_MSEC: Nsecs = 1_000_000; |
| 47 | + |
14 | 48 | /// Converts milliseconds to jiffies.
|
15 | 49 | #[inline]
|
16 | 50 | pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
|
17 | 51 | // SAFETY: The `__msecs_to_jiffies` function is always safe to call no
|
18 | 52 | // matter what the argument is.
|
19 | 53 | unsafe { bindings::__msecs_to_jiffies(msecs) }
|
20 | 54 | }
|
| 55 | + |
| 56 | +/// Converts jiffies to milliseconds. |
| 57 | +#[inline] |
| 58 | +pub fn jiffies_to_msecs(jiffies: Jiffies) -> Msecs { |
| 59 | + // SAFETY: The `__msecs_to_jiffies` function is always safe to call no |
| 60 | + // matter what the argument is. |
| 61 | + unsafe { bindings::jiffies_to_msecs(jiffies) } |
| 62 | +} |
| 63 | + |
| 64 | +/// Returns the current time in 32bit jiffies. |
| 65 | +#[inline] |
| 66 | +pub fn jiffies32() -> Jiffies32 { |
| 67 | + // SAFETY: It is always atomic to read the lower 32bit of jiffies. |
| 68 | + unsafe { bindings::jiffies as u32 } |
| 69 | +} |
| 70 | + |
| 71 | +/// Returns the time elapsed since system boot, in nanoseconds. Does include the |
| 72 | +/// time the system was suspended. |
| 73 | +#[inline] |
| 74 | +pub fn ktime_get_boot_fast_ns() -> Nsecs { |
| 75 | + // SAFETY: FFI call without safety requirements. |
| 76 | + unsafe { bindings::ktime_get_boot_fast_ns() } |
| 77 | +} |
| 78 | + |
| 79 | +/// Returns the time elapsed since system boot, in 32bit microseconds. Does |
| 80 | +/// include the time the system was suspended. |
| 81 | +#[inline] |
| 82 | +pub fn ktime_get_boot_fast_us32() -> Usecs32 { |
| 83 | + (ktime_get_boot_fast_ns() / NSEC_PER_USEC) as Usecs32 |
| 84 | +} |
| 85 | + |
| 86 | +/// Returns the time elapsed since system boot, in 32bit milliseconds. Does |
| 87 | +/// include the time the system was suspended. |
| 88 | +#[inline] |
| 89 | +pub fn ktime_get_boot_fast_ms32() -> Msecs32 { |
| 90 | + (ktime_get_boot_fast_ns() / NSEC_PER_MSEC) as Msecs32 |
| 91 | +} |
0 commit comments