2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-29 05:28:00 +00:00

Merge branch 'fanf-deduplicate-time-units' into 'main'

Deduplicate time unit conversion factors

See merge request isc-projects/bind9!7033
This commit is contained in:
Tony Finch 2022-11-25 13:47:15 +00:00
commit c1305baece
9 changed files with 69 additions and 68 deletions

View File

@ -1,3 +1,6 @@
6026. [cleanup] Deduplicate time unit conversion factors.
[GL !7033]
6025. [bug] Copy TLS identifier when setting up primaries for
catalog member zones. [GL #3638]

View File

@ -36,6 +36,7 @@
#include <isc/sockaddr.h>
#include <isc/string.h>
#include <isc/task.h>
#include <isc/time.h>
#include <isc/util.h>
#include <dns/byaddr.h>
@ -84,10 +85,6 @@
#define UDPTIMEOUT 5
#define MAXTRIES 0xffffffff
#define NS_PER_US 1000 /*%< Nanoseconds per microsecond. */
#define US_PER_SEC 1000000 /*%< Microseconds per second. */
#define US_PER_MS 1000 /*%< Microseconds per millisecond. */
static isc_mem_t *mctx = NULL;
static isc_task_t *global_task = NULL;
static isc_loopmgr_t *loopmgr = NULL;

View File

@ -33,6 +33,7 @@
#include <isc/string.h>
#include <isc/task.h>
#include <isc/tid.h>
#include <isc/time.h>
#include <isc/timer.h>
#include <isc/util.h>
@ -175,14 +176,11 @@
#define fctx_addref(f) fctx_attach((f), &(fetchctx_t *){ NULL })
#define fctx_unref(f) fctx_detach(&(fetchctx_t *){ (f) })
#define US_PER_SEC 1000000U
#define US_PER_MSEC 1000U
#define NS_PER_US 1000U
/*
* The maximum time we will wait for a single query.
*/
#define MAX_SINGLE_QUERY_TIMEOUT 9000U
#define MAX_SINGLE_QUERY_TIMEOUT_US (MAX_SINGLE_QUERY_TIMEOUT * US_PER_MSEC)
#define MAX_SINGLE_QUERY_TIMEOUT_US (MAX_SINGLE_QUERY_TIMEOUT * US_PER_MS)
/*
* We need to allow a individual query time to complete / timeout.
@ -1358,7 +1356,7 @@ fctx_cancelquery(resquery_t **queryp, isc_time_t *finish, bool no_response,
&query->start);
factor = DNS_ADB_RTTADJDEFAULT;
rttms = rtt / US_PER_MSEC;
rttms = rtt / US_PER_MS;
if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
inc_stats(fctx->res,
dns_resstatscounter_queryrtt0);
@ -2011,13 +2009,13 @@ fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
*/
isc_time_now(&now);
limit = isc_time_microdiff(&fctx->expires, &now);
if (limit < US_PER_MSEC) {
if (limit < US_PER_MS) {
FCTXTRACE("fetch already expired");
isc_interval_set(&fctx->interval, 0, 0);
return;
}
us = fctx->res->retryinterval * US_PER_MSEC;
us = fctx->res->retryinterval * US_PER_MS;
/*
* Exponential backoff after the first few tries.
@ -2056,7 +2054,7 @@ fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
if ((fctx->options & DNS_FETCHOPT_TRYSTALE_ONTIMEOUT) != 0) {
uint64_t stale = isc_time_microdiff(&fctx->expires_try_stale,
&now);
if (stale >= US_PER_MSEC && us > stale) {
if (stale >= US_PER_MS && us > stale) {
FCTXTRACE("setting stale timeout");
us = stale;
}
@ -2097,7 +2095,7 @@ resquery_timeout(resquery_t *query) {
*/
isc_time_now(&now);
timeleft = isc_time_microdiff(&fctx->expires_try_stale, &now);
if (timeleft >= US_PER_MSEC) {
if (timeleft >= US_PER_MS) {
return (ISC_R_SUCCESS);
}
@ -2126,8 +2124,8 @@ resquery_timeout(resquery_t *query) {
* resume waiting.
*/
timeleft = isc_time_microdiff(&fctx->next_timeout, &now);
if (timeleft >= US_PER_MSEC) {
dns_dispatch_resume(query->dispentry, (timeleft / US_PER_MSEC));
if (timeleft >= US_PER_MS) {
dns_dispatch_resume(query->dispentry, (timeleft / US_PER_MS));
return (ISC_R_COMPLETE);
}
@ -8156,7 +8154,7 @@ rctx_timedout(respctx_t *rctx) {
isc_time_now(&now);
/* netmgr timeouts are accurate to the millisecond */
if (isc_time_microdiff(&fctx->expires, &now) < US_PER_MSEC) {
if (isc_time_microdiff(&fctx->expires, &now) < US_PER_MS) {
FCTXTRACE("stopped trying to make fetch happen");
} else {
FCTXTRACE("query timed out; no response");

View File

@ -21,6 +21,15 @@
#include <isc/lang.h>
#include <isc/types.h>
enum {
MS_PER_SEC = 1000, /*%< Milliseonds per second. */
US_PER_MS = 1000, /*%< Microseconds per millisecond. */
US_PER_SEC = 1000 * 1000, /*%< Microseconds per second. */
NS_PER_US = 1000, /*%< Nanoseconds per millisecond. */
NS_PER_MS = 1000 * 1000, /*%< Nanoseconds per microsecond. */
NS_PER_SEC = 1000 * 1000 * 1000, /*%< Nanoseconds per second. */
};
/*
* ISC_FORMATHTTPTIMESTAMP_SIZE needs to be 30 in C locale and potentially
* more for other locales to handle longer national abbreviations when

View File

@ -22,10 +22,9 @@
#include <isc/stdtime.h>
#include <isc/strerr.h>
#include <isc/time.h>
#include <isc/util.h>
#define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
#if defined(CLOCK_REALTIME_COARSE)
#define CLOCKSOURCE CLOCK_REALTIME_COARSE
#elif defined(CLOCK_REALTIME_FAST)
@ -44,7 +43,7 @@ isc_stdtime_get(isc_stdtime_t *t) {
FATAL_SYSERROR(errno, "clock_gettime()");
}
REQUIRE(ts.tv_sec > 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_S);
REQUIRE(ts.tv_sec > 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_SEC);
*t = (isc_stdtime_t)ts.tv_sec;
}

View File

@ -30,11 +30,6 @@
#include <isc/tm.h>
#include <isc/util.h>
#define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
#define NS_PER_US 1000 /*%< Nanoseconds per microsecond. */
#define NS_PER_MS 1000000 /*%< Nanoseconds per millisecond. */
#define MS_PER_S 1000 /*%< Milliseonds per second. */
#if defined(CLOCK_REALTIME)
#define CLOCKSOURCE_HIRES CLOCK_REALTIME
#endif /* #if defined(CLOCK_REALTIME) */
@ -59,7 +54,7 @@ const isc_time_t *const isc_time_epoch = &epoch;
void
isc_time_set(isc_time_t *t, unsigned int seconds, unsigned int nanoseconds) {
REQUIRE(t != NULL);
REQUIRE(nanoseconds < NS_PER_S);
REQUIRE(nanoseconds < NS_PER_SEC);
t->seconds = seconds;
t->nanoseconds = nanoseconds;
@ -76,7 +71,7 @@ isc_time_settoepoch(isc_time_t *t) {
bool
isc_time_isepoch(const isc_time_t *t) {
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
if (t->seconds == 0 && t->nanoseconds == 0) {
return (true);
@ -96,7 +91,7 @@ time_now(isc_time_t *t, clockid_t clock) {
return (ISC_R_UNEXPECTED);
}
if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_S) {
if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_SEC) {
return (ISC_R_UNEXPECTED);
}
@ -131,14 +126,14 @@ isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
REQUIRE(t != NULL);
REQUIRE(i != NULL);
INSIST(i->nanoseconds < NS_PER_S);
INSIST(i->nanoseconds < NS_PER_SEC);
if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
UNEXPECTED_SYSERROR(errno, "clock_gettime()");
return (ISC_R_UNEXPECTED);
}
if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_S) {
if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_SEC) {
return (ISC_R_UNEXPECTED);
}
@ -156,9 +151,9 @@ isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
t->seconds = ts.tv_sec + i->seconds;
t->nanoseconds = ts.tv_nsec + i->nanoseconds;
if (t->nanoseconds >= NS_PER_S) {
if (t->nanoseconds >= NS_PER_SEC) {
t->seconds++;
t->nanoseconds -= NS_PER_S;
t->nanoseconds -= NS_PER_SEC;
}
return (ISC_R_SUCCESS);
@ -167,7 +162,7 @@ isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
int
isc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
REQUIRE(t1 != NULL && t2 != NULL);
INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
INSIST(t1->nanoseconds < NS_PER_SEC && t2->nanoseconds < NS_PER_SEC);
if (t1->seconds < t2->seconds) {
return (-1);
@ -187,7 +182,7 @@ isc_time_compare(const isc_time_t *t1, const isc_time_t *t2) {
isc_result_t
isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result) {
REQUIRE(t != NULL && i != NULL && result != NULL);
REQUIRE(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
REQUIRE(t->nanoseconds < NS_PER_SEC && i->nanoseconds < NS_PER_SEC);
/* Seconds */
#if HAVE_BUILTIN_ADD_OVERFLOW
@ -203,11 +198,11 @@ isc_time_add(const isc_time_t *t, const isc_interval_t *i, isc_time_t *result) {
/* Nanoseconds */
result->nanoseconds = t->nanoseconds + i->nanoseconds;
if (result->nanoseconds >= NS_PER_S) {
if (result->nanoseconds >= NS_PER_SEC) {
if (result->seconds == UINT_MAX) {
return (ISC_R_RANGE);
}
result->nanoseconds -= NS_PER_S;
result->nanoseconds -= NS_PER_SEC;
result->seconds++;
}
@ -218,7 +213,7 @@ isc_result_t
isc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
isc_time_t *result) {
REQUIRE(t != NULL && i != NULL && result != NULL);
REQUIRE(t->nanoseconds < NS_PER_S && i->nanoseconds < NS_PER_S);
REQUIRE(t->nanoseconds < NS_PER_SEC && i->nanoseconds < NS_PER_SEC);
/* Seconds */
#if HAVE_BUILTIN_SUB_OVERFLOW
@ -240,7 +235,7 @@ isc_time_subtract(const isc_time_t *t, const isc_interval_t *i,
return (ISC_R_RANGE);
}
result->seconds--;
result->nanoseconds = NS_PER_S + t->nanoseconds -
result->nanoseconds = NS_PER_SEC + t->nanoseconds -
i->nanoseconds;
}
@ -252,10 +247,10 @@ isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
uint64_t i1, i2, i3;
REQUIRE(t1 != NULL && t2 != NULL);
INSIST(t1->nanoseconds < NS_PER_S && t2->nanoseconds < NS_PER_S);
INSIST(t1->nanoseconds < NS_PER_SEC && t2->nanoseconds < NS_PER_SEC);
i1 = (uint64_t)t1->seconds * NS_PER_S + t1->nanoseconds;
i2 = (uint64_t)t2->seconds * NS_PER_S + t2->nanoseconds;
i1 = (uint64_t)t1->seconds * NS_PER_SEC + t1->nanoseconds;
i2 = (uint64_t)t2->seconds * NS_PER_SEC + t2->nanoseconds;
if (i1 <= i2) {
return (0);
@ -274,7 +269,7 @@ isc_time_microdiff(const isc_time_t *t1, const isc_time_t *t2) {
uint32_t
isc_time_seconds(const isc_time_t *t) {
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
return ((uint32_t)t->seconds);
}
@ -284,7 +279,7 @@ isc_time_secondsastimet(const isc_time_t *t, time_t *secondsp) {
time_t seconds;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
/*
* Ensure that the number of seconds represented by t->seconds
@ -321,7 +316,7 @@ uint32_t
isc_time_nanoseconds(const isc_time_t *t) {
REQUIRE(t != NULL);
ENSURE(t->nanoseconds < NS_PER_S);
ENSURE(t->nanoseconds < NS_PER_SEC);
return ((uint32_t)t->nanoseconds);
}
@ -329,9 +324,9 @@ isc_time_nanoseconds(const isc_time_t *t) {
uint32_t
isc_time_miliseconds(const isc_time_t *t) {
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
return ((t->seconds * MS_PER_S) + (t->nanoseconds / NS_PER_MS));
return ((t->seconds * MS_PER_SEC) + (t->nanoseconds / NS_PER_MS));
}
void
@ -341,7 +336,7 @@ isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) {
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(buf != NULL);
REQUIRE(len > 0);
@ -363,7 +358,7 @@ isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) {
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(buf != NULL);
REQUIRE(len > 0);
@ -405,7 +400,7 @@ isc_time_formatISO8601L(const isc_time_t *t, char *buf, unsigned int len) {
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(buf != NULL);
REQUIRE(len > 0);
@ -421,7 +416,7 @@ isc_time_formatISO8601Lms(const isc_time_t *t, char *buf, unsigned int len) {
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(buf != NULL);
REQUIRE(len > 0);
@ -441,7 +436,7 @@ isc_time_formatISO8601Lus(const isc_time_t *t, char *buf, unsigned int len) {
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(buf != NULL);
REQUIRE(len > 0);
@ -461,7 +456,7 @@ isc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) {
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(buf != NULL);
REQUIRE(len > 0);
@ -477,7 +472,7 @@ isc_time_formatISO8601ms(const isc_time_t *t, char *buf, unsigned int len) {
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(buf != NULL);
REQUIRE(len > 0);
@ -498,7 +493,7 @@ isc_time_formatISO8601us(const isc_time_t *t, char *buf, unsigned int len) {
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(buf != NULL);
REQUIRE(len > 0);
@ -520,7 +515,7 @@ isc_time_formatshorttimestamp(const isc_time_t *t, char *buf,
struct tm tm;
REQUIRE(t != NULL);
INSIST(t->nanoseconds < NS_PER_S);
INSIST(t->nanoseconds < NS_PER_SEC);
REQUIRE(buf != NULL);
REQUIRE(len > 0);

View File

@ -28,13 +28,12 @@
#include <isc/loop.h>
#include <isc/ratelimiter.h>
#include <isc/task.h>
#include <isc/time.h>
#include "ratelimiter.c"
#include <tests/isc.h>
#define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
isc_ratelimiter_t *rl = NULL;
ISC_LOOP_TEST_IMPL(ratelimiter_create) {
@ -220,7 +219,7 @@ ISC_LOOP_TEST_SETUP_TEARDOWN_IMPL(ratelimiter_pertick_interval) {
isc_event_t *event = NULL;
isc_interval_t interval;
isc_interval_set(&interval, 1, NS_PER_S / 10);
isc_interval_set(&interval, 1, NS_PER_SEC / 10);
expect_assert_failure(isc_ratelimiter_setinterval(NULL, &interval));
expect_assert_failure(isc_ratelimiter_setinterval(rl, NULL));
@ -263,7 +262,7 @@ ISC_LOOP_TEST_SETUP_TEARDOWN_IMPL(ratelimiter_pushpop) {
isc_event_t *event = NULL;
isc_interval_t interval;
isc_interval_set(&interval, 1, NS_PER_S / 10);
isc_interval_set(&interval, 1, NS_PER_SEC / 10);
isc_ratelimiter_setinterval(rl, &interval);
isc_ratelimiter_setpertic(rl, 2);

View File

@ -30,8 +30,7 @@
#include <tests/isc.h>
#define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
#define MAX_NS (NS_PER_S - 1)
#define MAX_NS (NS_PER_SEC - 1)
struct time_vectors {
isc_time_t a;
@ -43,13 +42,16 @@ struct time_vectors {
const struct time_vectors vectors_add[8] = {
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, ISC_R_SUCCESS },
{ { 0, MAX_NS }, { 0, MAX_NS }, { 1, MAX_NS - 1 }, ISC_R_SUCCESS },
{ { 0, NS_PER_S / 2 }, { 0, NS_PER_S / 2 }, { 1, 0 }, ISC_R_SUCCESS },
{ { 0, NS_PER_SEC / 2 },
{ 0, NS_PER_SEC / 2 },
{ 1, 0 },
ISC_R_SUCCESS },
{ { UINT_MAX, MAX_NS }, { 0, 0 }, { UINT_MAX, MAX_NS }, ISC_R_SUCCESS },
{ { UINT_MAX, 0 }, { 0, MAX_NS }, { UINT_MAX, MAX_NS }, ISC_R_SUCCESS },
{ { UINT_MAX, 0 }, { 1, 0 }, { 0, 0 }, ISC_R_RANGE },
{ { UINT_MAX, MAX_NS }, { 0, 1 }, { 0, 0 }, ISC_R_RANGE },
{ { UINT_MAX / 2 + 1, NS_PER_S / 2 },
{ UINT_MAX / 2, NS_PER_S / 2 },
{ { UINT_MAX / 2 + 1, NS_PER_SEC / 2 },
{ UINT_MAX / 2, NS_PER_SEC / 2 },
{ 0, 0 },
ISC_R_RANGE },
};
@ -57,9 +59,9 @@ const struct time_vectors vectors_add[8] = {
const struct time_vectors vectors_sub[7] = {
{ { 0, 0 }, { 0, 0 }, { 0, 0 }, ISC_R_SUCCESS },
{ { 1, 0 }, { 0, MAX_NS }, { 0, 1 }, ISC_R_SUCCESS },
{ { 1, NS_PER_S / 2 },
{ { 1, NS_PER_SEC / 2 },
{ 0, MAX_NS },
{ 0, NS_PER_S / 2 + 1 },
{ 0, NS_PER_SEC / 2 + 1 },
ISC_R_SUCCESS },
{ { UINT_MAX, MAX_NS }, { UINT_MAX, 0 }, { 0, MAX_NS }, ISC_R_SUCCESS },
{ { 0, 0 }, { 1, 0 }, { 0, 0 }, ISC_R_RANGE },

View File

@ -422,7 +422,6 @@ uint64_t timer_ticks;
isc_interval_t timer_interval;
isc_timertype_t timer_type;
#define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
ISC_LOOP_TEARDOWN_IMPL(timer_expect) {
uint64_t diff = (timer_stop - timer_start) / 1000000000;
assert_true(diff == timer_expect);
@ -491,7 +490,7 @@ ISC_LOOP_TEST_CUSTOM_IMPL(reschedule_from_callback,
teardown_loop_timer_expect) {
isc_timer_create(mainloop, timer_event, NULL, &timer);
isc_interval_set(&timer_interval, 0, NS_PER_S / 2);
isc_interval_set(&timer_interval, 0, NS_PER_SEC / 2);
isc_timer_start(timer, timer_type, &timer_interval);
}
@ -526,7 +525,7 @@ ISC_LOOP_TEST_CUSTOM_IMPL(reschedule_ticker, setup_loop_reschedule_ticker,
isc_timer_start(timer, timer_type, &timer_interval);
/* Then fire every 1/4 second */
isc_interval_set(&timer_interval, 0, NS_PER_S / 4);
isc_interval_set(&timer_interval, 0, NS_PER_SEC / 4);
}
ISC_TEST_LIST_START