Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 48b7f4d

Browse files
DarksonnKAGA-KOKO
authored andcommitted
rust: time: Add Ktime
Introduce a wrapper around `ktime_t` with a few different useful methods. Rust Binder will use these bindings to compute how many milliseconds a transaction has been active for when dumping the current state of the Binder driver. This replicates the logic in C Binder [1]. For a usage example in Rust Binder, see [2]. ktime_get() cannot be safely called in NMI context. This requirement is not checked by these abstractions, but it is intended that klint [3] or a similar tool will be used to check it in the future. Signed-off-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Boqun Feng <boqun.feng@gmail.com> Reviewed-by: Thomas Gleixner <tglx@linutronix.de> Acked-by: Miguel Ojeda <ojeda@kernel.org> Link: https://lore.kernel.org/r/20240322-rust-ktime_ms_delta-v2-1-d98de1f7c282@google.com Link: https://lore.kernel.org/lkml/5ac8c0d09392290be789423f0dd78a520b830fab.1682333709.git.zhangchuang3@xiaomi.com/ [1] Link: https://r.android.com/3004103 [2] Link: https://rust-for-linux.com/klint [3]
1 parent 8ff1e6c commit 48b7f4d

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

rust/kernel/time.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
//! This module contains the kernel APIs related to time and timers that
66
//! have been ported or wrapped for usage by Rust code in the kernel.
77
8+
/// The number of nanoseconds per millisecond.
9+
pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64;
10+
811
/// The time unit of Linux kernel. One jiffy equals (1/HZ) second.
912
pub type Jiffies = core::ffi::c_ulong;
1013

@@ -18,3 +21,60 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
1821
// matter what the argument is.
1922
unsafe { bindings::__msecs_to_jiffies(msecs) }
2023
}
24+
25+
/// A Rust wrapper around a `ktime_t`.
26+
#[repr(transparent)]
27+
#[derive(Copy, Clone)]
28+
pub struct Ktime {
29+
inner: bindings::ktime_t,
30+
}
31+
32+
impl Ktime {
33+
/// Create a `Ktime` from a raw `ktime_t`.
34+
#[inline]
35+
pub fn from_raw(inner: bindings::ktime_t) -> Self {
36+
Self { inner }
37+
}
38+
39+
/// Get the current time using `CLOCK_MONOTONIC`.
40+
#[inline]
41+
pub fn ktime_get() -> Self {
42+
// SAFETY: It is always safe to call `ktime_get` outside of NMI context.
43+
Self::from_raw(unsafe { bindings::ktime_get() })
44+
}
45+
46+
/// Divide the number of nanoseconds by a compile-time constant.
47+
#[inline]
48+
fn divns_constant<const DIV: i64>(self) -> i64 {
49+
self.to_ns() / DIV
50+
}
51+
52+
/// Returns the number of nanoseconds.
53+
#[inline]
54+
pub fn to_ns(self) -> i64 {
55+
self.inner
56+
}
57+
58+
/// Returns the number of milliseconds.
59+
#[inline]
60+
pub fn to_ms(self) -> i64 {
61+
self.divns_constant::<NSEC_PER_MSEC>()
62+
}
63+
}
64+
65+
/// Returns the number of milliseconds between two ktimes.
66+
#[inline]
67+
pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 {
68+
(later - earlier).to_ms()
69+
}
70+
71+
impl core::ops::Sub for Ktime {
72+
type Output = Ktime;
73+
74+
#[inline]
75+
fn sub(self, other: Ktime) -> Ktime {
76+
Self {
77+
inner: self.inner - other.inner,
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)