[PATCH grout v2] replace clock_t with Grout specific high-res clock
Morten Brørup
mb at smartsharesystems.com
Mon May 25 20:17:49 CEST 2026
> From: Robin Jarry [mailto:rjarry at redhat.com]
> Sent: Monday, 25 May 2026 19.12
>
> Morten Brørup, May 25, 2026 at 17:41:
> > The clock_t type is implementation specific.
> > E.g. on Linux, it is microseconds, and on Windows it is milliseconds.
> >
> > Replace all uses of clock_t and its accompanying function,
> gr_clock_us(),
> > with a new nanosecond resolution clock type, gr_clock_ns_t, and its
> > accompanying function, gr_grout_ns().
> >
> > Note about resolution and size of the new clock type:
> > A clock with nanosecond resolution can be used for purposes not
> possible
> > with microsecond resolution, e.g. shaping and pacing. For reference,
> the
> > duration of a minimum size (84 byte) packet on a 100 Gbit/s Ethernet
> link
> > is 6.72 ns.
> > The size of the new type is 64 bit, the same size as the type it
> replaces
> > (on supported implementations). Changing the resolution to
> nanoseconds
> > thus does not impact the size of data structures where it is used.
> >
> > Note about signedness of the new clock type:
> > I condidered making the gr_clock_ns_t unsigned, but that would
> require
> > additional considerations in code where it is used, e.g. for
> calculating
> > age, to prevent wraparound in race conditions. E.g.:
> > age = (gr_clock_ns() - fdb->last_seen) / NS_PER_S;
> > If the current thread reads the clock using gr_clock_ns(), and
> another
> > thread races to set fdb->last_seen afterwards, the result of the
> > subtraction is negative. The division by NS_PER_S makes the age zero.
> > If gr_clock_ns_t was unsigned, the negative result of the subtraction
> > would be a very large unsigned number. Dividing this very large
> number
> > by NS_PER_S would be a large unsigned number, not zero.
> > Obviously, this could be fixed by type casting gr_clock_ns_t values
> to
> > signed int64_t everywhere they are used with subtraction. But such a
> > requirement increases the risk of bugs.
> > So I decided to make it signed, like type_t.
> >
> > Signed-off-by: Morten Brørup <mb at smartsharesystems.com>
> > ---
> > v2:
> > * Fixed lines exceeding 100 characters.
> > * Added note about resolution and size to the patch description.
> > ---
>
> Acked-by: Robin Jarry <rjarry at redhat.com>
>
> Applied after fixing clang-format and a few cosmetic details, thanks.
>
> https://github.com/DPDK/grout/commit/fe0c7f9a36548c05a6537bcf068324352b
> 823cac
>
Thank you.
Good idea with the GR_NS_PER_S to add the type cast.
It makes some constant additions much easier to read, e.g. in the LACP module.
Now I can proceed with the performance patch that triggered this one. :-)
More information about the grout
mailing list