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 5/5] rust: time: Add Instant::elapsed() for monotonic clocks
Date: Tue, 26 Mar 2024 11:00:52 -0700	[thread overview]
Message-ID: <ZgMNVNOjoLH4S4Fb@boqun-archlinux> (raw)
In-Reply-To: <kZmwObXVcyEJFVR3I05Nab0WdjcccDVARoOm6U9EmXhd89fxYRjvNmEAUZsueWTUlBlHp8jpU3i2YfgN9aWCLZameta0tPT_OgQdBOWUIko=@proton.me>

On Tue, Mar 26, 2024 at 05:13:38PM +0000, Benno Lossin wrote:
> On 24.03.24 23:33, Boqun Feng wrote:
> > This is a convenient way to do:
> > 
> > 	t1 = Clock::now();
> > 	...
> > 	delta =  Clock::now() - t1;
> > 
> > Hence add it.
> > 
> > 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 | 25 +++++++++++++++++++++++++
> >  1 file changed, 25 insertions(+)
> > 
> > diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
> > index 5cd669cbea01..cd1e45169517 100644
> > --- a/rust/kernel/time.rs
> > +++ b/rust/kernel/time.rs
> > @@ -114,6 +114,31 @@ fn sub(self, other: Self) -> Self::Output {
> >      }
> >  }
> > 
> > +impl<T: Clock + Monotonic> Instant<T> {
> > +    /// Returns the time elapsed since this [`Instant`].
> > +    ///
> > +    /// This provides a convenient way to calculate time elapsed since a previous [`Clock::now`].
> > +    /// Note even though the function only exists for monotonic clocks, it could still return
> > +    /// negative [`Duration`] if the current time is earlier than the time of `&self`, and this
> > +    /// could happen if `&self` is a timestamp generated by a [`Instant`] + [`Duration`].
> 
> But there currently is no way to add an `Instant<T>` to a `Duration`.
> 

This is kinda the disadvantages of "upstreaming the bits you only need",
we know for sure there will be a way to generate an `Instant` with an
addition of a `Duration`. I can of course provide that function in this
series. But let's settle down on "negative durations" first.

> > +    ///
> > +    /// But for typical usages, it should always return non-negative [`Duration`]:
> > +    ///
> > +    /// # Examples
> > +    ///
> > +    /// ```
> > +    /// use kernel::time::{Clock, clock::KernelTime};
> > +    ///
> > +    /// let ts = KernelTime::now();
> > +    ///
> > +    /// // `KernelTime` is monotonic.
> > +    /// assert!(ts.elapsed().to_ns() >= 0);
> 
> Now that I thought a bit more about the design, I think allowing
> negative durations is a bad idea.
> Do you disagree?
> 

So yes, I don't think allowing negative duration is really good design.
But as I mentioned in the cover letter, I hope to support cases where:

	d = ts2 - ts1;
	ts = ts3 + d;

	(where ts1, ts2, ts3 is Instant, and d is of course Duration)

without any branch instruction in the asm code. It's useful in the case
where ts1 is a old time base, and ts3 is the new one, and you want to
"remain" the delta between ts2 and t1 and apply that on ts3. To me there
are three options to achieve that: 1) allow negative durations (this
also mirrors what `ktime_t` represents for timedelta AKAIU), 2) have
a timedelta type that differs from Duration, and it can be negative, 3)
provide a function to do the above calculation for `Instant`. I choose
the first one because it's quick and simple (also easy to map to
`ktime_t`). But I don't have my own preference on these three options.

Regards,
Boqun

> If there is a case where you have a non-monotonic clock, or you are not
> sure if two timestamps are in the correct relation, we could have a
> function that returns a `Option<Duration>` or `Result<Duration>`.
> 
> -- 
> Cheers,
> Benno
> 
> > +    /// ```
> > +    pub fn elapsed(&self) -> Duration {
> > +        T::now() - *self
> > +    }
> > +}
> > +
> >  /// Contains the various clock source types available to the kernel.
> >  pub mod clock {
> >      use super::*;
> > --
> > 2.44.0
> >

  reply	other threads:[~2024-03-26 18:01 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
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 [this message]
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=ZgMNVNOjoLH4S4Fb@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).