Add isc_time_monotonic()

This is to simplify measurements of how long things take.
This commit is contained in:
Tony Finch 2023-01-17 16:05:01 +00:00 committed by Tony Finch
parent 89a3ff1d06
commit ff63b53ff4
3 changed files with 46 additions and 0 deletions

View file

@ -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]

View 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);
/*%<

View 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;