From: cheloha Date: Tue, 13 Dec 2022 17:30:36 +0000 (+0000) Subject: timecounting: add getbinruntime(), getnsecruntime() X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=6fba7c695c88ce551cbfb5461cad0d4f29e0d23b;p=openbsd timecounting: add getbinruntime(), getnsecruntime() The networking people want a fast, monotonic clock that only advances while the system is not suspended. The runtime clock satisfies most of these requirements, so introduce getnsecruntime() to provide a fast means for reading it. Based on patches from jca@ and claudio@. ok yasuoka@ --- diff --git a/sys/kern/kern_tc.c b/sys/kern/kern_tc.c index e8cc25b02d6..16cf1fa70b7 100644 --- a/sys/kern/kern_tc.c +++ b/sys/kern/kern_tc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_tc.c,v 1.80 2022/12/05 23:18:37 deraadt Exp $ */ +/* $OpenBSD: kern_tc.c,v 1.81 2022/12/13 17:30:36 cheloha Exp $ */ /* * Copyright (c) 2000 Poul-Henning Kamp @@ -294,6 +294,30 @@ nanoruntime(struct timespec *ts) BINTIME_TO_TIMESPEC(&bt, ts); } +void +getbinruntime(struct bintime *bt) +{ + struct timehands *th; + u_int gen; + + do { + th = timehands; + gen = th->th_generation; + membar_consumer(); + bintimesub(&th->th_offset, &th->th_naptime, bt); + membar_consumer(); + } while (gen == 0 || gen != th->th_generation); +} + +uint64_t +getnsecruntime(void) +{ + struct bintime bt; + + getbinruntime(&bt); + return BINTIME_TO_NSEC(&bt); +} + void bintime(struct bintime *bt) { diff --git a/sys/sys/time.h b/sys/sys/time.h index d4736c97cb6..20bfba4cbb8 100644 --- a/sys/sys/time.h +++ b/sys/sys/time.h @@ -1,4 +1,4 @@ -/* $OpenBSD: time.h,v 1.62 2022/07/23 22:58:51 cheloha Exp $ */ +/* $OpenBSD: time.h,v 1.63 2022/12/13 17:30:36 cheloha Exp $ */ /* $NetBSD: time.h,v 1.18 1996/04/23 10:29:33 mycroft Exp $ */ /* @@ -318,6 +318,9 @@ void nanoboottime(struct timespec *); void binruntime(struct bintime *); void nanoruntime(struct timespec *); +void getbinruntime(struct bintime *); +uint64_t getnsecruntime(void); + time_t gettime(void); time_t getuptime(void);