From: cheloha Date: Sat, 19 Jun 2021 13:49:39 +0000 (+0000) Subject: timecounting: add FRAC_TO_NSEC(), BINTIME_TO_NSEC() X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=2f582782e0cb5e3b9836cc833ce069c1563538c4;p=openbsd timecounting: add FRAC_TO_NSEC(), BINTIME_TO_NSEC() Refactor the fraction-to-nanosecond conversion from BINTIME_TO_TIMESPEC() into a dedicated routine, FRAC_TO_NSEC(), so we can reuse it elsewhere. Then add a new BINTIME_TO_NSEC() function to sys/time.h to deduplicate conversion code in nsecuptime(), getnsecuptime(), and tc_setclock(). Thread: https://marc.info/?l=openbsd-tech&m=162376993926751&w=2 ok dlg@ --- diff --git a/sys/kern/kern_tc.c b/sys/kern/kern_tc.c index f56ed182df1..69a4a3b19db 100644 --- a/sys/kern/kern_tc.c +++ b/sys/kern/kern_tc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_tc.c,v 1.73 2021/06/15 05:24:46 dlg Exp $ */ +/* $OpenBSD: kern_tc.c,v 1.74 2021/06/19 13:49:39 cheloha Exp $ */ /* * Copyright (c) 2000 Poul-Henning Kamp @@ -254,28 +254,18 @@ uint64_t nsecuptime(void) { struct bintime bt; - uint64_t nsec; binuptime(&bt); - - nsec = (1000000000ULL * (bt.frac >> 32)) >> 32; - nsec += bt.sec * 1000000000ULL; - - return (nsec); + return BINTIME_TO_NSEC(&bt); } uint64_t getnsecuptime(void) { struct bintime bt; - uint64_t nsec; getbinuptime(&bt); - - nsec = (1000000000ULL * (bt.frac >> 32)) >> 32; - nsec += bt.sec * 1000000000ULL; - - return (nsec); + return BINTIME_TO_NSEC(&bt); } void @@ -567,8 +557,7 @@ tc_setclock(const struct timespec *ts) #ifndef SMALL_KERNEL /* convert the bintime to ticks */ bintimesub(&new_naptime, &old_naptime, &elapsed); - adj_ticks = (uint64_t)hz * elapsed.sec + - (((uint64_t)1000000 * (uint32_t)(elapsed.frac >> 32)) >> 32) / tick; + adj_ticks = BINTIME_TO_NSEC(&elapsed) / tick_nsec; if (adj_ticks > 0) { if (adj_ticks > INT_MAX) adj_ticks = INT_MAX; diff --git a/sys/sys/time.h b/sys/sys/time.h index 7f1b0163b28..ac0eb2f35f7 100644 --- a/sys/sys/time.h +++ b/sys/sys/time.h @@ -1,4 +1,4 @@ -/* $OpenBSD: time.h,v 1.60 2021/06/15 05:24:47 dlg Exp $ */ +/* $OpenBSD: time.h,v 1.61 2021/06/19 13:49:39 cheloha Exp $ */ /* $NetBSD: time.h,v 1.18 1996/04/23 10:29:33 mycroft Exp $ */ /* @@ -222,11 +222,17 @@ bintimesub(const struct bintime *bt, const struct bintime *ct, * time_second ticks after N.999999999 not after N.4999999999 */ +static inline uint32_t +FRAC_TO_NSEC(uint64_t frac) +{ + return ((frac >> 32) * 1000000000ULL) >> 32; +} + static inline void 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); + ts->tv_nsec = FRAC_TO_NSEC(bt->frac); } static inline void @@ -412,6 +418,12 @@ TIMESPEC_TO_NSEC(const struct timespec *ts) return ts->tv_sec * 1000000000ULL + ts->tv_nsec; } +static inline uint64_t +BINTIME_TO_NSEC(const struct bintime *bt) +{ + return bt->sec * 1000000000ULL + FRAC_TO_NSEC(bt->frac); +} + #else /* !_KERNEL */ #include