we have a few places that use a uint64_t with the number of nanosecons of uptime the machine has. this factors it out to make them a bit more generally available.
i was going to add yet another one of these to pfsync, but thought it might be a good idea to factor them out first. ok? Index: kern/kern_tc.c =================================================================== RCS file: /cvs/src/sys/kern/kern_tc.c,v retrieving revision 1.72 diff -u -p -r1.72 kern_tc.c --- kern/kern_tc.c 30 Apr 2021 13:52:48 -0000 1.72 +++ kern/kern_tc.c 4 Jun 2021 02:28:31 -0000 @@ -196,6 +196,21 @@ binuptime(struct bintime *bt) } void +getbinuptime(struct bintime *bt) +{ + struct timehands *th; + u_int gen; + + do { + th = timehands; + gen = th->th_generation; + membar_consumer(); + *bt = th->th_offset; + membar_consumer(); + } while (gen == 0 || gen != th->th_generation); +} + +void nanouptime(struct timespec *tsp) { struct bintime bt; @@ -233,6 +248,34 @@ getuptime(void) return now; #endif +} + +uint64_t +nsecuptime(void) +{ + struct bintime bt; + uint64_t nsec; + + binuptime(&bt); + + nsec = (1000000000ULL * (bt.frac >> 32)) >> 32; + nsec += bt.sec * 1000000000ULL; + + return (nsec); +} + +uint64_t +getnsecuptime(void) +{ + struct bintime bt; + uint64_t nsec; + + getbinuptime(&bt); + + nsec = (1000000000ULL * (bt.frac >> 32)) >> 32; + nsec += bt.sec * 1000000000ULL; + + return (nsec); } void Index: kern/subr_pool.c =================================================================== RCS file: /cvs/src/sys/kern/subr_pool.c,v retrieving revision 1.233 diff -u -p -r1.233 subr_pool.c --- kern/subr_pool.c 10 Mar 2021 10:21:47 -0000 1.233 +++ kern/subr_pool.c 4 Jun 2021 02:28:31 -0000 @@ -272,19 +272,6 @@ struct task pool_gc_task = TASK_INITIALI #define POOL_WAIT_FREE SEC_TO_NSEC(1) #define POOL_WAIT_GC SEC_TO_NSEC(8) -/* - * TODO Move getnsecuptime() to kern_tc.c and document it when we - * have callers in other modules. - */ -static uint64_t -getnsecuptime(void) -{ - struct timespec now; - - getnanouptime(&now); - return TIMESPEC_TO_NSEC(&now); -} - RBT_PROTOTYPE(phtree, pool_page_header, ph_node, phtree_compare); static inline int Index: kern/vfs_sync.c =================================================================== RCS file: /cvs/src/sys/kern/vfs_sync.c,v retrieving revision 1.65 diff -u -p -r1.65 vfs_sync.c --- kern/vfs_sync.c 14 Jan 2021 03:32:01 -0000 1.65 +++ kern/vfs_sync.c 4 Jun 2021 02:28:31 -0000 @@ -132,19 +132,6 @@ vn_syncer_add_to_worklist(struct vnode * } /* - * TODO Move getnsecuptime() to kern_tc.c and document it when we have - * more users in the kernel. - */ -static uint64_t -getnsecuptime(void) -{ - struct timespec now; - - getnanouptime(&now); - return TIMESPEC_TO_NSEC(&now); -} - -/* * System filesystem synchronizer daemon. */ void Index: net/bpf.c =================================================================== RCS file: /cvs/src/sys/net/bpf.c,v retrieving revision 1.204 diff -u -p -r1.204 bpf.c --- net/bpf.c 23 Apr 2021 03:43:19 -0000 1.204 +++ net/bpf.c 4 Jun 2021 02:28:31 -0000 @@ -421,19 +421,6 @@ bpfclose(dev_t dev, int flag, int mode, (d)->bd_fbuf = NULL; /* - * TODO Move nsecuptime() into kern_tc.c and document it when we have - * more users elsewhere in the kernel. - */ -static uint64_t -nsecuptime(void) -{ - struct timespec now; - - nanouptime(&now); - return TIMESPEC_TO_NSEC(&now); -} - -/* * bpfread - read next chunk of packets from buffers */ int Index: sys/time.h =================================================================== RCS file: /cvs/src/sys/sys/time.h,v retrieving revision 1.58 diff -u -p -r1.58 time.h --- sys/time.h 13 Jan 2021 16:28:50 -0000 1.58 +++ sys/time.h 4 Jun 2021 02:28:31 -0000 @@ -290,6 +290,7 @@ void binuptime(struct bintime *); void nanouptime(struct timespec *); void microuptime(struct timeval *); +void getbinuptime(struct bintime *); void getnanouptime(struct timespec *); void getmicrouptime(struct timeval *); @@ -302,6 +303,9 @@ void nanoruntime(struct timespec *); time_t gettime(void); time_t getuptime(void); + +uint64_t nsecuptime(void); +uint64_t getnsecuptime(void); struct proc; int clock_gettime(struct proc *, clockid_t, struct timespec *);