From: Boqun Feng <boqun.feng@gmail.com>
To: Asahi Lina <lina@asahilina.net>
Cc: "Thomas Gleixner" <tglx@linutronix.de>,
"Miguel Ojeda" <miguel.ojeda.sandonis@gmail.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>,
"John Stultz" <jstultz@google.com>,
"Stephen Boyd" <sboyd@kernel.org>,
linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
asahi@lists.linux.dev,
"Heghedus Razvan" <heghedus.razvan@protonmail.com>
Subject: Re: [PATCH] rust: time: New module for timekeeping functions
Date: Tue, 21 Feb 2023 21:20:10 -0800 [thread overview]
Message-ID: <Y/WmCtTw2NPFdJBC@Boquns-Mac-mini.local> (raw)
In-Reply-To: <03e9af69-3fea-032d-6dc3-ac0ea8e57471@asahilina.net>
On Wed, Feb 22, 2023 at 01:45:58PM +0900, Asahi Lina wrote:
> On 22/02/2023 11.54, Boqun Feng wrote:
> > On Wed, Feb 22, 2023 at 01:24:53AM +0100, Thomas Gleixner wrote:
> >> Miguel!
> >>
> >> On Tue, Feb 21 2023 at 23:29, Miguel Ojeda wrote:
> >>> On Tue, Feb 21, 2023 at 7:45 PM Thomas Gleixner <tglx@linutronix.de> wrote:
> >>>>
> >>>> But xb abd xr are the same datatype because they represent a time delta.
> >>>
> >>> In principle, one could also have different duration types too. For
> >>> instance, C++'s `std::chrono::duration` type is parametrized on the
> >>> representation type and the tick period, and thus an operation between
> >>> two time points like t1 - t0 returns a duration type that depends on
> >>> the type of the time points, i.e. which clock they were obtained from.
> >>
> >> Correct, but for practical purposes I'd assume that the timestamps
> >> retrieved via ktime_get*() have the same granularity, i.e. 1ns.
> >>
> >> TBH, that's not entirely correct because:
> >>
> >> - the underlying hardware clocksource might not have a 1ns
> >> resolution
> >>
> >> - the CLOCK_*_COARSE implementations are only advanced once per
> >> tick, but are executing significantly faster because they avoid
> >> the hardware counter access.
> >>
> >> But that's an assumption which has proven to be workable and correct
> >> with the full zoo of hardware supported by the kernel.
> >>
> >> The point is that all CLOCK_* variants, except CLOCK_REALTIME and
> >> CLOCK_TAI are guaranteed to never go backwards.
> >>
> >> CLOCK_REALTIME and CLOCK_TAI are special as they can be set by user
> >> space and CLOCK_REALTIME has the extra oddities of leap seconds. But
> >> that's a well understood issue and is not specific to the kernel.
> >>
> >> Back to time deltas (or duration types). Independent of the above it
> >> might make sense to be type strict about these as well. Especially if we
> >> go one step further and have timers based on CLOCK_* which need to be
> >> armed by either timestamps for absolute expiry or time deltas for
> >> relative to now expiry. I definitely can see a point for requiring
> >> matching time delta types there.
> >>
> >> That said, I have no strong opinions about this particular detail and
> >> leave it to the Rusties to agree on something sensible.
> >>
> >
> > I'd like to propose something below to make thing forward quickly:
> >
> > Given Lina only uses CLOCK_BOOTTIME and CLOCK_MONOTONIC, I'd say we
> > reuse core::time::Duration and probably remain its ">=0" semantics even
> > in the future we change its internal representation to u64.
> >
> > For timestamp type, use Instant semantics and use different types for
> > different clocks, i.e. similar to the implementation from Heghedus (much
> > better than mine!). But we can avoid implementing a fully version of
> > Instant, and focus on just the piece that Lina needs, which I believe
> > it's elapsed()?
> >
> > For the future, if we were to support non-monotonic timestamp, maybe we
> > use the different type name like TimeStamp and TimeDelta.
> >
> > In short:
> >
> > * For monotonic clocks, Instant + Duration, and keep them similar
> > to std semantics.
> >
> > * For non-monotonic clocks, don't worry it right now, and
> > probably different types for both stamps and deltas.
> >
> > Thoughts?
>
> I actually only used CLOCK_MONOTONIC in the end, so I could even leave
> CLOCK_BOOTTIME for later, though I like the idea of having scaffolding
> for several clock types even if we only implement one initially.
>
> This works for me, if you're happy with the idea I'll give it a spin
> based on Heghedus' example. Heghedus, is it okay if I put you down as
> Co-developed-by and can I get a signoff? ^^
>
> For the actual Instant type, I was thinking it makes sense to just
> internally represent it as a newtype of Duration as well. Then all the
> math becomes trivial based on Duration operations, and when we replace
> Duration with a new u64 type it'll all work out the same. Fundamentally
> that means Instant types are internally stored as the Duration between
> the epoch (e.g. system boot) subject to the way that clock ticks, which
> I think is a reasonable internal representation? (In other words, it's
Sounds even better ;-) It means Instant and Duration have the exact same
behavior about sub and so on.
Regards,
Boqun
> the same as my original patch behind the scenes, but wrapped in type
> safety).
>
> ~~ Lina
next prev parent reply other threads:[~2023-02-22 5:20 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-21 7:06 [PATCH] rust: time: New module for timekeeping functions Asahi Lina
2023-02-21 7:25 ` Eric Curtin
2023-02-21 11:23 ` Björn Roy Baron
2023-02-21 12:32 ` Thomas Gleixner
2023-02-21 14:06 ` Boqun Feng
2023-02-21 16:02 ` Thomas Gleixner
2023-02-21 16:31 ` Asahi Lina
2023-02-21 18:45 ` Thomas Gleixner
2023-02-21 21:33 ` Heghedus Razvan
2023-02-22 0:01 ` Thomas Gleixner
2023-02-22 19:55 ` Gary Guo
2023-02-21 22:29 ` Miguel Ojeda
2023-02-22 0:24 ` Thomas Gleixner
2023-02-22 2:54 ` Boqun Feng
2023-02-22 4:45 ` Asahi Lina
2023-02-22 5:20 ` Boqun Feng [this message]
2023-02-22 6:52 ` Heghedus Razvan
2023-02-22 12:29 ` Miguel Ojeda
2023-02-22 12:28 ` Miguel Ojeda
2023-02-21 16:27 ` Asahi Lina
2023-02-21 16:37 ` Asahi Lina
2023-02-21 19:00 ` Thomas Gleixner
2023-02-21 19:49 ` Boqun Feng
2023-02-22 4:56 ` Asahi Lina
2023-02-22 8:33 ` Thomas Gleixner
2023-02-21 17:13 ` Josh Stone
2023-02-21 21:46 ` Thomas Gleixner
2023-02-22 9:43 ` Gaelan Steele
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=Y/WmCtTw2NPFdJBC@Boquns-Mac-mini.local \
--to=boqun.feng@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=asahi@lists.linux.dev \
--cc=bjorn3_gh@protonmail.com \
--cc=gary@garyguo.net \
--cc=heghedus.razvan@protonmail.com \
--cc=jstultz@google.com \
--cc=lina@asahilina.net \
--cc=linux-kernel@vger.kernel.org \
--cc=miguel.ojeda.sandonis@gmail.com \
--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