Re: [PATCH 5/5] rust: time: Add Instant::elapsed() for monotonic clocks

From: Boqun Feng
Date: Tue Mar 26 2024 - 14:05:42 EST


On Tue, Mar 26, 2024 at 11:00:52AM -0700, Boqun Feng wrote:
[...]
> >
> > 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

To be accurate, the "ts = ts3 + d" can have (and should have) a branch
to check overflows (and reset to KTIME_MAX if so), but that's the only
branch.

Regards,
Boqun

> 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
> > >