diff options
author | cheloha <cheloha@cvs.openbsd.org> | 2019-06-03 01:27:31 +0000 |
---|---|---|
committer | cheloha <cheloha@cvs.openbsd.org> | 2019-06-03 01:27:31 +0000 |
commit | 8462915bcbbfceaacadcb124aea7494de9067b67 (patch) | |
tree | cc2797608205d270b64d672b420ef800760197b9 /sys | |
parent | 1a6db7afbda625bea54b11bd578b5ee38ec96b99 (diff) |
Switch from bintime_add() et al. to bintimeadd(9).
Basically just make all the bintime routines look and behave more like
the timeradd(3) macros.
Switch to three-argument forms for structure math, introduce and use
bintimecmp(9), and rename the structure conversion routines to resemble
e.g. TIMEVAL_TO_TIMESPEC(3).
Document all of this in a new bintimeadd.9 page.
Code input from mpi@, manpage input from schwarze@.
code ok mpi@, docs ok schwarze@, docs probably still ok jmc@
Diffstat (limited to 'sys')
-rw-r--r-- | sys/kern/kern_tc.c | 52 | ||||
-rw-r--r-- | sys/kern/kern_time.c | 6 | ||||
-rw-r--r-- | sys/sys/time.h | 59 |
3 files changed, 55 insertions, 62 deletions
diff --git a/sys/kern/kern_tc.c b/sys/kern/kern_tc.c index 4717adc908e..16cf6cdc605 100644 --- a/sys/kern/kern_tc.c +++ b/sys/kern/kern_tc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_tc.c,v 1.47 2019/05/22 19:59:37 cheloha Exp $ */ +/* $OpenBSD: kern_tc.c,v 1.48 2019/06/03 01:27:30 cheloha Exp $ */ /* * Copyright (c) 2000 Poul-Henning Kamp <phk@FreeBSD.org> @@ -163,7 +163,7 @@ microboottime(struct timeval *tvp) struct bintime bt; binboottime(&bt); - bintime2timeval(&bt, tvp); + BINTIME_TO_TIMEVAL(&bt, tvp); } void @@ -177,7 +177,7 @@ binuptime(struct bintime *bt) gen = th->th_generation; membar_consumer(); *bt = th->th_offset; - bintime_addx(bt, th->th_scale * tc_delta(th)); + bintimeaddfrac(bt, th->th_scale * tc_delta(th), bt); membar_consumer(); } while (gen == 0 || gen != th->th_generation); } @@ -188,7 +188,7 @@ nanouptime(struct timespec *tsp) struct bintime bt; binuptime(&bt); - bintime2timespec(&bt, tsp); + BINTIME_TO_TIMESPEC(&bt, tsp); } void @@ -197,7 +197,7 @@ microuptime(struct timeval *tvp) struct bintime bt; binuptime(&bt); - bintime2timeval(&bt, tvp); + BINTIME_TO_TIMEVAL(&bt, tvp); } void @@ -211,8 +211,8 @@ bintime(struct bintime *bt) gen = th->th_generation; membar_consumer(); *bt = th->th_offset; - bintime_addx(bt, th->th_scale * tc_delta(th)); - bintime_add(bt, &th->th_boottime); + bintimeaddfrac(bt, th->th_scale * tc_delta(th), bt); + bintimeadd(bt, &th->th_boottime, bt); membar_consumer(); } while (gen == 0 || gen != th->th_generation); } @@ -223,7 +223,7 @@ nanotime(struct timespec *tsp) struct bintime bt; bintime(&bt); - bintime2timespec(&bt, tsp); + BINTIME_TO_TIMESPEC(&bt, tsp); } void @@ -232,7 +232,7 @@ microtime(struct timeval *tvp) struct bintime bt; bintime(&bt); - bintime2timeval(&bt, tvp); + BINTIME_TO_TIMEVAL(&bt, tvp); } void @@ -245,7 +245,7 @@ getnanouptime(struct timespec *tsp) th = timehands; gen = th->th_generation; membar_consumer(); - bintime2timespec(&th->th_offset, tsp); + BINTIME_TO_TIMESPEC(&th->th_offset, tsp); membar_consumer(); } while (gen == 0 || gen != th->th_generation); } @@ -260,7 +260,7 @@ getmicrouptime(struct timeval *tvp) th = timehands; gen = th->th_generation; membar_consumer(); - bintime2timeval(&th->th_offset, tvp); + BINTIME_TO_TIMEVAL(&th->th_offset, tvp); membar_consumer(); } while (gen == 0 || gen != th->th_generation); } @@ -361,9 +361,9 @@ tc_setrealtimeclock(const struct timespec *ts) rw_enter_write(&tc_lock); mtx_enter(&windup_mtx); binuptime(&bt2); - timespec2bintime(ts, &bt); - bintime_sub(&bt, &bt2); - bintime_add(&bt2, &timehands->th_boottime); + TIMESPEC_TO_BINTIME(ts, &bt); + bintimesub(&bt, &bt2, &bt); + bintimeadd(&bt2, &timehands->th_boottime, &bt2); /* XXX fiddle all the little crinkly bits around the fiords... */ tc_windup(&bt, NULL, &zero); @@ -373,7 +373,7 @@ tc_setrealtimeclock(const struct timespec *ts) enqueue_randomness(ts->tv_sec); if (timestepwarnings) { - bintime2timespec(&bt2, &ts2); + BINTIME_TO_TIMESPEC(&bt2, &ts2); log(LOG_INFO, "Time stepped from %lld.%09ld to %lld.%09ld\n", (long long)ts2.tv_sec, ts2.tv_nsec, (long long)ts->tv_sec, ts->tv_nsec); @@ -408,15 +408,13 @@ tc_setclock(const struct timespec *ts) enqueue_randomness(ts->tv_sec); mtx_enter(&windup_mtx); - timespec2bintime(ts, &bt); - bintime_sub(&bt, &timehands->th_boottime); + TIMESPEC_TO_BINTIME(ts, &bt); + bintimesub(&bt, &timehands->th_boottime, &bt); /* * Don't rewind the offset. */ - if (bt.sec < timehands->th_offset.sec || - (bt.sec == timehands->th_offset.sec && - bt.frac < timehands->th_offset.frac)) + if (bintimecmp(&bt, &timehands->th_offset, <)) rewind = 1; bt2 = timehands->th_offset; @@ -426,7 +424,7 @@ tc_setclock(const struct timespec *ts) mtx_leave(&windup_mtx); if (rewind) { - bintime2timespec(&bt, &earlier); + BINTIME_TO_TIMESPEC(&bt, &earlier); printf("%s: cannot rewind uptime to %lld.%09ld\n", __func__, (long long)earlier.tv_sec, earlier.tv_nsec); return; @@ -434,8 +432,8 @@ tc_setclock(const struct timespec *ts) #ifndef SMALL_KERNEL /* convert the bintime to ticks */ - bintime_sub(&bt, &bt2); - bintime_add(&naptime, &bt); + bintimesub(&bt, &bt2, &bt); + bintimeadd(&naptime, &bt, &naptime); adj_ticks = (uint64_t)hz * bt.sec + (((uint64_t)1000000 * (uint32_t)(bt.frac >> 32)) >> 32) / tick; if (adj_ticks > 0) { @@ -499,7 +497,7 @@ tc_windup(struct bintime *new_boottime, struct bintime *new_offset, ncount = 0; th->th_offset_count += delta; th->th_offset_count &= th->th_counter->tc_counter_mask; - bintime_addx(&th->th_offset, th->th_scale * delta); + bintimeaddfrac(&th->th_offset, th->th_scale * delta, &th->th_offset); #ifdef notyet /* @@ -533,7 +531,7 @@ tc_windup(struct bintime *new_boottime, struct bintime *new_offset, * case we missed a leap second. */ bt = th->th_offset; - bintime_add(&bt, &th->th_boottime); + bintimeadd(&bt, &th->th_boottime, &bt); i = bt.sec - tho->th_microtime.tv_sec; if (i > LARGE_STEP) i = 2; @@ -542,8 +540,8 @@ tc_windup(struct bintime *new_boottime, struct bintime *new_offset, /* Update the UTC timestamps used by the get*() functions. */ /* XXX shouldn't do this here. Should force non-`get' versions. */ - bintime2timeval(&bt, &th->th_microtime); - bintime2timespec(&bt, &th->th_nanotime); + BINTIME_TO_TIMEVAL(&bt, &th->th_microtime); + BINTIME_TO_TIMESPEC(&bt, &th->th_nanotime); /* Now is a good time to change timecounters. */ if (th->th_counter != active_tc) { diff --git a/sys/kern/kern_time.c b/sys/kern/kern_time.c index ca109f81434..b9030067cfb 100644 --- a/sys/kern/kern_time.c +++ b/sys/kern/kern_time.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_time.c,v 1.118 2019/06/01 14:11:17 mpi Exp $ */ +/* $OpenBSD: kern_time.c,v 1.119 2019/06/03 01:27:30 cheloha Exp $ */ /* $NetBSD: kern_time.c,v 1.20 1996/02/18 11:57:06 fvdl Exp $ */ /* @@ -116,8 +116,8 @@ clock_gettime(struct proc *p, clockid_t clock_id, struct timespec *tp) break; case CLOCK_UPTIME: binuptime(&bt); - bintime_sub(&bt, &naptime); - bintime2timespec(&bt, tp); + bintimesub(&bt, &naptime, &bt); + BINTIME_TO_TIMESPEC(&bt, tp); break; case CLOCK_MONOTONIC: case CLOCK_BOOTTIME: diff --git a/sys/sys/time.h b/sys/sys/time.h index b4e0d20c688..8d4beeec964 100644 --- a/sys/sys/time.h +++ b/sys/sys/time.h @@ -1,4 +1,4 @@ -/* $OpenBSD: time.h,v 1.40 2019/01/19 01:53:44 cheloha Exp $ */ +/* $OpenBSD: time.h,v 1.41 2019/06/03 01:27:30 cheloha Exp $ */ /* $NetBSD: time.h,v 1.18 1996/04/23 10:29:33 mycroft Exp $ */ /* @@ -172,39 +172,38 @@ struct bintime { uint64_t frac; }; +#define bintimecmp(btp, ctp, cmp) \ + ((btp)->sec == (ctp)->sec ? \ + (btp)->frac cmp (ctp)->frac : \ + (btp)->sec cmp (ctp)->sec) + static __inline void -bintime_addx(struct bintime *bt, uint64_t x) +bintimeaddfrac(const struct bintime *bt, uint64_t x, struct bintime *ct) { - uint64_t u; - - u = bt->frac; - bt->frac += x; - if (u > bt->frac) - bt->sec++; + ct->sec = bt->sec; + if (bt->frac > bt->frac + x) + ct->sec++; + ct->frac = bt->frac + x; } static __inline void -bintime_add(struct bintime *bt, const struct bintime *bt2) +bintimeadd(const struct bintime *bt, const struct bintime *ct, + struct bintime *dt) { - uint64_t u; - - u = bt->frac; - bt->frac += bt2->frac; - if (u > bt->frac) - bt->sec++; - bt->sec += bt2->sec; + dt->sec = bt->sec + ct->sec; + if (bt->frac > bt->frac + ct->frac) + dt->sec++; + dt->frac = bt->frac + ct->frac; } static __inline void -bintime_sub(struct bintime *bt, const struct bintime *bt2) +bintimesub(const struct bintime *bt, const struct bintime *ct, + struct bintime *dt) { - uint64_t u; - - u = bt->frac; - bt->frac -= bt2->frac; - if (u < bt->frac) - bt->sec--; - bt->sec -= bt2->sec; + dt->sec = bt->sec - ct->sec; + if (bt->frac < bt->frac - ct->frac) + dt->sec--; + dt->frac = bt->frac - ct->frac; } /*- @@ -222,34 +221,30 @@ bintime_sub(struct bintime *bt, const struct bintime *bt2) */ static __inline void -bintime2timespec(const struct bintime *bt, struct timespec *ts) +BINTIME_TO_TIMESPEC(const struct bintime *bt, struct timespec *ts) { - ts->tv_sec = bt->sec; ts->tv_nsec = (long)(((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32); } static __inline void -timespec2bintime(const struct timespec *ts, struct bintime *bt) +TIMESPEC_TO_BINTIME(const struct timespec *ts, struct bintime *bt) { - bt->sec = ts->tv_sec; /* 18446744073 = int(2^64 / 1000000000) */ bt->frac = (uint64_t)ts->tv_nsec * (uint64_t)18446744073ULL; } static __inline void -bintime2timeval(const struct bintime *bt, struct timeval *tv) +BINTIME_TO_TIMEVAL(const struct bintime *bt, struct timeval *tv) { - tv->tv_sec = bt->sec; tv->tv_usec = (long)(((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32); } static __inline void -timeval2bintime(const struct timeval *tv, struct bintime *bt) +TIMEVAL_TO_BINTIME(const struct timeval *tv, struct bintime *bt) { - bt->sec = (time_t)tv->tv_sec; /* 18446744073709 = int(2^64 / 1000000) */ bt->frac = (uint64_t)tv->tv_usec * (uint64_t)18446744073709ULL; |