rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Benno Lossin <benno.lossin@proton.me>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Wedson Almeida Filho" <wedsonaf@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	"John Stultz" <jstultz@google.com>,
	"Stephen Boyd" <sboyd@kernel.org>,
	"Valentin Obst" <kernel@valentinobst.de>,
	"Heghedus Razvan" <heghedus.razvan@protonmail.com>,
	"Asahi Lina" <lina@asahilina.net>
Subject: Re: [PATCH 3/5] rust: time: Introduce clock reading framework
Date: Tue, 26 Mar 2024 12:19:13 -0700	[thread overview]
Message-ID: <ZgMfsaiOlA87PAUf@boqun-archlinux> (raw)
In-Reply-To: <c2XJe4xCqT5WrHu9XZKQ03ggEZLwd2KBuSBb9Onivny9v7fqKBkm1aywmztjiDyuLKqr2Igt_6XUJiuArLl9JVwq0xa4gKfocP2H5YZDrFk=@proton.me>

On Tue, Mar 26, 2024 at 05:00:39PM +0000, Benno Lossin wrote:
> On 24.03.24 23:33, Boqun Feng wrote:
> > 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@protonmail.com>
> > Signed-off-by: Heghedus Razvan <heghedus.razvan@protonmail.com>
> > Co-developed-by: Asahi Lina <lina@asahilina.net>
> > Signed-off-by: Asahi Lina <lina@asahilina.net>
> > Signed-off-by: Boqun Feng <boqun.feng@gmail.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 {}
> 
> Why is this trait not an extension of `Clock`?
> 

This was carried over from the old version, for myself, it doesn't make
much difference between:

	trait A { .. }
	trait B { .. }

	impl<T: A + B> ...

vs

	trait A { .. }
	trait B: A { .. }

	impl<T: B> ...

hence I kept it as it is, but yes, it a `Monotonic` *`Clock`*, so I will
change it in the next version.

> > +
> > +/// A timestamp of a given [`Clock`]
> 
> Missing '.'.
> 

Fixed locally.

> > +#[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 {
> 
> When compiling, this function is marked as dead-code in this patch.
> 

Hmm... I cannot trigger any compile errors with this patch. If you see
an error, could you share the build command? It's not a `pub` function
though.

Regards,
Boqun

> -- 
> Cheers,
> Benno
> 
> > +        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
> >

  reply	other threads:[~2024-03-26 19:19 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-24 22:33 [PATCH 0/5] rust: time: Add clock read support Boqun Feng
2024-03-24 22:33 ` [PATCH 1/5] rust: time: doc: Add missing C header link to jiffies Boqun Feng
2024-03-26 16:44   ` Benno Lossin
2024-03-24 22:33 ` [PATCH 2/5] rust: time: Introduce Duration type Boqun Feng
2024-03-26 16:50   ` Benno Lossin
2024-03-26 17:11     ` Boqun Feng
2024-03-26 17:17       ` Boqun Feng
2024-03-27 11:18         ` Benno Lossin
2024-03-24 22:33 ` [PATCH 3/5] rust: time: Introduce clock reading framework Boqun Feng
2024-03-26 17:00   ` Benno Lossin
2024-03-26 19:19     ` Boqun Feng [this message]
2024-03-27 12:50       ` Benno Lossin
2024-03-27 17:49         ` Boqun Feng
2024-03-24 22:33 ` [PATCH 4/5] rust: time: Support reading CLOCK_MONOTONIC Boqun Feng
2024-03-26 17:03   ` Benno Lossin
2024-03-26 19:21     ` Boqun Feng
2024-03-24 22:33 ` [PATCH 5/5] rust: time: Add Instant::elapsed() for monotonic clocks Boqun Feng
2024-03-26 17:13   ` Benno Lossin
2024-03-26 18:00     ` Boqun Feng
2024-03-26 18:04       ` Boqun Feng
2024-03-27  1:09       ` Boqun Feng
2024-03-27 11:31         ` Benno Lossin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZgMfsaiOlA87PAUf@boqun-archlinux \
    --to=boqun.feng@gmail.com \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=gary@garyguo.net \
    --cc=heghedus.razvan@protonmail.com \
    --cc=jstultz@google.com \
    --cc=kernel@valentinobst.de \
    --cc=lina@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=wedsonaf@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).