[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240324223339.971934-4-boqun.feng@gmail.com>
Date: Sun, 24 Mar 2024 15:33:37 -0700
From: Boqun Feng <boqun.feng@...il.com>
To: rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org,
Thomas Gleixner <tglx@...utronix.de>
Cc: Alice Ryhl <aliceryhl@...gle.com>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
benno.lossin@...ton.me,
Andreas Hindborg <a.hindborg@...sung.com>,
John Stultz <jstultz@...gle.com>,
Stephen Boyd <sboyd@...nel.org>,
Valentin Obst <kernel@...entinobst.de>,
Heghedus Razvan <heghedus.razvan@...tonmail.com>,
Asahi Lina <lina@...hilina.net>
Subject: [PATCH 3/5] rust: time: Introduce clock reading framework
To make sure Rust code doesn't mix timestamps from different clocks, a
type safe clock reading framework is introduced. It includes:
* A `Clock` trait that represents different clocks, to read a particular
clock, one needs implement the `Clock::now()` function.
* A `Instant<Clock>` type that represents timestamps of different
clocks, whose implementation is just a `ktime_t`, so all the
calculation on `ktime_t` should apply to it as well.
Co-developed-by: Heghedus Razvan <heghedus.razvan@...tonmail.com>
Signed-off-by: Heghedus Razvan <heghedus.razvan@...tonmail.com>
Co-developed-by: Asahi Lina <lina@...hilina.net>
Signed-off-by: Asahi Lina <lina@...hilina.net>
Signed-off-by: Boqun Feng <boqun.feng@...il.com>
---
rust/kernel/time.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index b238b3a4e899..0f9f5605ed48 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -6,6 +6,9 @@
//! have been ported or wrapped for usage by Rust code in the kernel.
//!
//! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h).
+//! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h).
+
+use core::marker::PhantomData;
/// The number of nanoseconds per millisecond.
pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64;
@@ -64,3 +67,49 @@ pub fn to_ns(self) -> i64 {
self.inner
}
}
+
+/// Represents a clock, that is, a unique time source and it can be queried for the current time.
+pub trait Clock: Sized {
+ /// Returns the current time for this clock.
+ fn now() -> Instant<Self>;
+}
+
+/// Marker trait for clock sources that are guaranteed to be monotonic.
+pub trait Monotonic {}
+
+/// A timestamp of a given [`Clock`]
+#[repr(transparent)]
+#[derive(Debug)]
+pub struct Instant<T: Clock> {
+ inner: bindings::ktime_t,
+ clock: PhantomData<T>,
+}
+
+impl<T: Clock> Clone for Instant<T> {
+ fn clone(&self) -> Self {
+ *self
+ }
+}
+
+impl<T: Clock> Copy for Instant<T> {}
+
+impl<T: Clock> Instant<T> {
+ fn new(ktime: bindings::ktime_t) -> Self {
+ Self {
+ inner: ktime,
+ clock: PhantomData,
+ }
+ }
+}
+
+impl<T: Clock> core::ops::Sub for Instant<T> {
+ type Output = Duration;
+
+ /// Returns the difference of two timestamps.
+ #[inline]
+ fn sub(self, other: Self) -> Self::Output {
+ // `ktime_t` is an `i64` value of nanoseconds, and kernel defines signed overflow to behave
+ // like 2s-complement, hence `wrapping_sub()` is used here to mirror `ktime_sub()`.
+ Duration::new(self.inner.wrapping_sub(other.inner))
+ }
+}
--
2.44.0
Powered by blists - more mailing lists