On Tue, Jun 15, 2021 at 10:10:54AM -0500, Scott Cheloha wrote: > > [...] > > To tidy it up I'd like to refactor the fraction-to-nanosecond and > bintime-to-nanosecond conversions into new functions so we only need > to write them once. > > ok?
Updated diff. We can also use BINTIME_TO_NSEC() in tc_setclock(). Index: sys/time.h =================================================================== RCS file: /cvs/src/sys/sys/time.h,v retrieving revision 1.60 diff -u -p -r1.60 time.h --- sys/time.h 15 Jun 2021 05:24:47 -0000 1.60 +++ sys/time.h 15 Jun 2021 20:39:52 -0000 @@ -222,11 +222,17 @@ bintimesub(const struct bintime *bt, con * 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 @@ -250,6 +256,12 @@ TIMEVAL_TO_BINTIME(const struct timeval bt->sec = (time_t)tv->tv_sec; /* 18446744073709 = int(2^64 / 1000000) */ bt->frac = (uint64_t)tv->tv_usec * (uint64_t)18446744073709ULL; +} + +static inline uint64_t +BINTIME_TO_NSEC(const struct bintime *bt) +{ + return bt->sec * 1000000000ULL + FRAC_TO_NSEC(bt->frac); } #endif Index: kern/kern_tc.c =================================================================== RCS file: /cvs/src/sys/kern/kern_tc.c,v retrieving revision 1.73 diff -u -p -r1.73 kern_tc.c --- kern/kern_tc.c 15 Jun 2021 05:24:46 -0000 1.73 +++ kern/kern_tc.c 15 Jun 2021 20:39:53 -0000 @@ -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;