[PATCH grout] replace clock_t with Grout specific high-res clock

Robin Jarry rjarry at redhat.com
Sun May 24 16:36:23 CEST 2026


Hi Morten,

Morten Brørup, May 24, 2026 at 12:50:
> 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 signedness:
> 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>

I have a few remarks:

1) Do we really need nanosecond precision? I am not worried about the
   date limit as 63 bits (signed 64bit integer) allow holding up to 292
   years. But this seems excessive for a simple timestamp.

   Or maybe you are planning on adding other less precise types?

2) Make sure to satisfy `make lint`:

$ make lint
[clang-format]
Clang-formatting 273 files
modules/infra/control/lacp.c:178:26: error: code should be clang-formatted [-Wclang-format-violations]
                                member->next_tx = now + LACP_SHORT_TIMEOUT * (gr_clock_ns_t)NS_PER_S;
                                                     ^
modules/ip/control/icmp.c:49:25: error: code should be clang-formatted [-Wclang-format-violations]
static struct rte_mbuf *get_icmp_response(uint16_t ident, uint16_t seq_num, gr_clock_ns_t *timestamp) {
                        ^
modules/l2/cli/fdb.c:141:17: error: code should be clang-formatted [-Wclang-format-violations]
                gr_table_cell(table, 6, "%ld", (gr_clock_ns() - fdb->last_seen) / INT64_C(1000000000));
                              ^
modules/l2/cli/fdb.c:141:88: error: code should be clang-formatted [-Wclang-format-violations]
                gr_table_cell(table, 6, "%ld", (gr_clock_ns() - fdb->last_seen) / INT64_C(1000000000));
                                                                                                     ^
[license-check]
[white-space]
[comments]
api/gr_clock.h:13:// Note: Using signed, to avoid need for casting to signed in calculations where race conditions may cause negative differences.<-- comment too long
api/gr_clock.h:16:// Get powered-on (non-suspended, non-hibernated) time since last boot (using a common clock across all processes).<-- comment too long
make: *** [GNUmakefile:181: lint] Error 123

-- 
Robin

> Freshest if eaten before date on carton.



More information about the grout mailing list