From ff63b53ff46ddebce073c10ca7d2c7d4668739fe Mon Sep 17 00:00:00 2001 From: Tony Finch Date: Tue, 17 Jan 2023 16:05:01 +0000 Subject: [PATCH] Add isc_time_monotonic() This is to simplify measurements of how long things take. --- CHANGES | 3 +++ lib/isc/include/isc/time.h | 31 +++++++++++++++++++++++++++++++ lib/isc/time.c | 12 ++++++++++++ 3 files changed, 46 insertions(+) diff --git a/CHANGES b/CHANGES index 748fe8afb5..6f5472d783 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +6085. [func] Add isc_time_monotonic() to simplify time measurements. + [GL !7468] + 6084. [bug] When BIND was built without jemalloc, the allocator flag ISC_MEM_ZERO could return non-zero memory. [GL #3845] diff --git a/lib/isc/include/isc/time.h b/lib/isc/include/isc/time.h index 5de7ab6970..e2f232a793 100644 --- a/lib/isc/include/isc/time.h +++ b/lib/isc/include/isc/time.h @@ -76,10 +76,35 @@ ISC_LANG_BEGINDECLS *\li 'i' is a valid pointer. */ +#define isc_interval_fromnanosecs(ns) isc_time_fromnanosecs(ns) +#define isc_interval_tonanosecs(i) isc_time_tonanosecs(i) + /*** *** Absolute Times ***/ +/*% + * A linear count of nanoseconds. + * + * 64 bits of nanoseconds is more than 500 years. + */ +typedef uint64_t isc_nanosecs_t; + +/*% + * Convert linear nanoseconds to an isc_time_t + */ +#define isc_nanosecs_fromtime(time) \ + (NS_PER_SEC * (isc_nanosecs_t)(time).seconds + (time).nanoseconds) + +/*% + * Construct an isc_time_t from linear nanoseconds + */ +#define isc_time_fromnanosecs(ns) \ + ((isc_time_t){ \ + .seconds = (ns) / NS_PER_SEC, \ + .nanoseconds = (ns) % NS_PER_SEC, \ + }) + /*% * The contents of this structure are private, and MUST NOT be accessed * directly by callers. @@ -136,6 +161,12 @@ isc_time_isepoch(const isc_time_t *t); *\li 't' is a valid pointer. */ +isc_nanosecs_t +isc_time_monotonic(void); +/*%< + * Returns the system's monotonic time in linear nanoseconds. + */ + isc_result_t isc_time_now(isc_time_t *t); /*%< diff --git a/lib/isc/time.c b/lib/isc/time.c index d1b55996dd..d01ed162b4 100644 --- a/lib/isc/time.c +++ b/lib/isc/time.c @@ -120,6 +120,18 @@ isc_time_now(isc_time_t *t) { return time_now(t, CLOCKSOURCE); } +isc_nanosecs_t +isc_time_monotonic(void) { + struct timespec ts; + + RUNTIME_CHECK(clock_gettime(CLOCK_MONOTONIC, &ts) != -1); + + return (isc_nanosecs_fromtime(((isc_time_t){ + .seconds = ts.tv_sec, + .nanoseconds = ts.tv_nsec, + }))); +} + isc_result_t isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) { struct timespec ts;