]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add isc_time_monotonic()
authorTony Finch <fanf@isc.org>
Tue, 17 Jan 2023 16:05:01 +0000 (16:05 +0000)
committerTony Finch <dot@dotat.at>
Mon, 6 Feb 2023 12:14:51 +0000 (12:14 +0000)
This is to simplify measurements of how long things take.

CHANGES
lib/isc/include/isc/time.h
lib/isc/time.c

diff --git a/CHANGES b/CHANGES
index 748fe8afb57120fccc2ca12166a9d21202c15a80..6f5472d783b5be434b5eddffc1efba50a12f79cb 100644 (file)
--- 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]
 
index 5de7ab6970f0baa086cc680f5ef6dd0d469a3dd0..e2f232a79308f6e0f5752698f0272ab86afb267e 100644 (file)
@@ -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);
 /*%<
index d1b55996ddccdef34e941b0e3230980908fa4352..d01ed162b4eafe21e3d0394042cae6c1d355fec4 100644 (file)
@@ -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;