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>
Subject: Re: [PATCH 2/5] rust: time: Introduce Duration type
Date: Tue, 26 Mar 2024 10:11:07 -0700	[thread overview]
Message-ID: <ZgMBqzv0r98_EGGR@boqun-archlinux> (raw)
In-Reply-To: <4hYJbftgOk1JOPbJ6CfKZell6ngp8GljwIUIB1vOQvIf-7jiogG5xDtCvcMlF7cIJAdy9fO5HLQh_8ohnWNB3MAaj0xjAGeyyDowemgunOU=@proton.me>

On Tue, Mar 26, 2024 at 04:50:02PM +0000, Benno Lossin wrote:
> On 24.03.24 23:33, Boqun Feng wrote:> From: Alice Ryhl <aliceryhl@google.com>
> > 
> > Introduce a type representing time duration. Define our own type instead
> > of using `core::time::Duration` because in kernel C code, an i64
> > (ktime_t) is used for representing time durations, an i64 backed
> > duration type is more efficient when interacting with time APIs in C.
> > 
> > Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> > [boqun: Rename `Ktime` to `Duration`, and make it a type of durations]
> > Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> > ---
> >  rust/kernel/time.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 44 insertions(+)
> > 
> > diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> > index bbb666e64dd7..b238b3a4e899 100644
> > --- a/rust/kernel/time.rs
> > +++ b/rust/kernel/time.rs
> > @@ -7,6 +7,9 @@
> >  //!
> >  //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h).
> > 
> > +/// The number of nanoseconds per millisecond.
> > +pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64;
> > +
> >  /// The time unit of Linux kernel. One jiffy equals (1/HZ) second.
> >  pub type Jiffies = core::ffi::c_ulong;
> > 
> > @@ -20,3 +23,44 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies {
> >      // matter what the argument is.
> >      unsafe { bindings::__msecs_to_jiffies(msecs) }
> >  }
> > +
> > +/// A time duration.
> > +///
> > +/// # Examples
> > +///
> > +/// ```
> > +/// let one_second = kernel::time::Duration::new(1000_000_000);
> > +///
> > +/// // 1 second is 1000 milliseconds.
> > +/// assert_eq!(one_second.to_ms(), 1000);
> > +/// ```
> > +#[repr(transparent)]
> > +#[derive(Copy, Clone, Debug)]
> > +pub struct Duration {
> > +    inner: i64,
> 
> Why not use the name `ns` or `nanos`?
> 

Good point, I will rename it in next version.

> > +}
> > +
> > +impl Duration {
> > +    /// Creates a new duration of `ns` nanoseconds.
> > +    pub const fn new(ns: i64) -> Self {
> > +        Self { inner: ns }
> > +    }
> > +
> > +    /// Divides the number of nanoseconds by a compile-time constant.
> > +    #[inline]
> > +    fn divns_constant<const DIV: i64>(self) -> i64 {
> > +        self.to_ns() / DIV
> > +    }
> 
> I am a bit confused, why is this better than writing
> `self.to_ns() / DIV` at the callsite?
> 

Hmm.. you're right, there should be no difference I think. If there is
nothing I'm missing from Alice, I will drop this function in the next
version.

Regards,
Boqun

> -- 
> Cheers,
> Benno
> 
> > +
> > +    /// Returns the number of milliseconds.
> > +    #[inline]
> > +    pub fn to_ms(self) -> i64 {
> > +        self.divns_constant::<NSEC_PER_MSEC>()
> > +    }
> > +
> > +    /// Returns the number of nanoseconds.
> > +    #[inline]
> > +    pub fn to_ns(self) -> i64 {
> > +        self.inner
> > +    }
> > +}
> > --
> > 2.44.0
> >

  reply	other threads:[~2024-03-26 17:11 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 [this message]
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
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=ZgMBqzv0r98_EGGR@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=jstultz@google.com \
    --cc=kernel@valentinobst.de \
    --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).