Deduplicate time unit conversion factors

The various factors like NS_PER_MS are now defined in a single place
and the names are no longer inconsistent. I chose the _PER_SEC names
rather than _PER_S because it is slightly more clear in isolation;
but the smaller units are always NS, US, and MS.

(cherry picked from commit 00307fe318)
This commit is contained in:
Tony Finch
2022-11-04 12:02:33 +00:00
committed by Tony Finch
parent 2c03ac2f5f
commit 303cdf8e27
7 changed files with 66 additions and 63 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 dns_requestmgr_t *requestmgr = NULL;
static const char *batchname = NULL;

View File

@@ -28,6 +28,7 @@
#include <isc/stats.h>
#include <isc/string.h>
#include <isc/task.h>
#include <isc/time.h>
#include <isc/timer.h>
#include <isc/util.h>
@@ -170,14 +171,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.
@@ -1348,7 +1346,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);
@@ -1960,13 +1958,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.
@@ -2005,7 +2003,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;
}
@@ -2046,7 +2044,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);
}
@@ -2075,8 +2073,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);
}
@@ -8181,7 +8179,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. */
};
/***
*** Intervals
***/

View File

@@ -21,10 +21,9 @@
#include <time.h>
#include <isc/stdtime.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)
@@ -43,7 +42,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

@@ -29,11 +29,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) */
@@ -63,7 +58,7 @@ void
isc_interval_set(isc_interval_t *i, unsigned int seconds,
unsigned int nanoseconds) {
REQUIRE(i != NULL);
REQUIRE(nanoseconds < NS_PER_S);
REQUIRE(nanoseconds < NS_PER_SEC);
i->seconds = seconds;
i->nanoseconds = nanoseconds;
@@ -72,7 +67,7 @@ isc_interval_set(isc_interval_t *i, unsigned int seconds,
bool
isc_interval_iszero(const isc_interval_t *i) {
REQUIRE(i != NULL);
INSIST(i->nanoseconds < NS_PER_S);
INSIST(i->nanoseconds < NS_PER_SEC);
if (i->seconds == 0 && i->nanoseconds == 0) {
return (true);
@@ -84,9 +79,9 @@ isc_interval_iszero(const isc_interval_t *i) {
unsigned int
isc_interval_ms(const isc_interval_t *i) {
REQUIRE(i != NULL);
INSIST(i->nanoseconds < NS_PER_S);
INSIST(i->nanoseconds < NS_PER_SEC);
return ((i->seconds * MS_PER_S) + (i->nanoseconds / NS_PER_MS));
return ((i->seconds * MS_PER_SEC) + (i->nanoseconds / NS_PER_MS));
}
/***
@@ -101,7 +96,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;
@@ -118,7 +113,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);
@@ -138,7 +133,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);
}
@@ -173,14 +168,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);
}
@@ -198,9 +193,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);
@@ -209,7 +204,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);
@@ -229,7 +224,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_OVERFLOW
@@ -245,11 +240,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++;
}
@@ -260,7 +255,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_OVERFLOW
@@ -282,7 +277,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;
}
@@ -294,10 +289,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);
@@ -316,7 +311,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);
}
@@ -326,7 +321,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
@@ -363,7 +358,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);
}
@@ -375,7 +370,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);
@@ -397,7 +392,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);
@@ -439,7 +434,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);
@@ -455,7 +450,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);
@@ -475,7 +470,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);
@@ -495,7 +490,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);
@@ -511,7 +506,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);
@@ -532,7 +527,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);
@@ -554,7 +549,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

@@ -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 },