diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2019-09-03 18:07:28 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2019-09-03 18:07:28 +0000 |
commit | e69717d9e0cd107f461abff85f255be82d7bd69b (patch) | |
tree | a1158470fe0ea9afea4fb95e974464ac61fe058e /src | |
parent | 2aa26bce30a3a1d46979a011d85928dda927932c (diff) | |
download | skalibs-e69717d9e0cd107f461abff85f255be82d7bd69b.tar.xz |
Big wallclock/stopwatch refactor. It was long overdue.
* --enable-clock and --enable-monotonic are gone
* tain_sysclock() has been renamed tain_wallclock_read()
* tain_wallclock_read() reads from CLOCK_REALTIME (or gettimeofday())
* tain_clockmon[_init]() have been renamed to tain_stopwatch_[read|init]()
and now accept a monotonic clock name as an extra argument
* tain_now() points to the system (wall) clock by default
* tain_now_set_[stopwatch|wallclock]() can be used to switch
Now to make a pass on all skarnet.org programs and add a
tain_now_set_stopwatch() call everywhere needed... >.>
Diffstat (limited to 'src')
-rw-r--r-- | src/include/skalibs/tai.h | 23 | ||||
-rw-r--r-- | src/libstddjb/sysclock_get.c | 10 | ||||
-rw-r--r-- | src/libstddjb/sysclock_set.c | 14 | ||||
-rw-r--r-- | src/libstddjb/tain_clockmon.c | 47 | ||||
-rw-r--r-- | src/libstddjb/tain_now.c | 44 | ||||
-rw-r--r-- | src/libstddjb/tain_now_set_stopwatch.c | 36 | ||||
-rw-r--r-- | src/libstddjb/tain_now_set_wallclock.c | 10 | ||||
-rw-r--r-- | src/libstddjb/tain_stopwatch.c | 50 | ||||
-rw-r--r-- | src/libstddjb/tain_wallclock_read.c (renamed from src/libstddjb/tain_sysclock.c) | 2 | ||||
-rw-r--r-- | src/libstddjb/timestamp_r.c | 2 | ||||
-rw-r--r-- | src/sysdeps/tryclockboot.c | 10 |
11 files changed, 127 insertions, 121 deletions
diff --git a/src/include/skalibs/tai.h b/src/include/skalibs/tai.h index f3699ce..2bdf524 100644 --- a/src/include/skalibs/tai.h +++ b/src/include/skalibs/tai.h @@ -86,19 +86,24 @@ extern int timespec_from_tain_relative (struct timespec *, tain_t const *) ; extern int timespec_from_tain (struct timespec *, tain_t const *) ; extern int timespec_sysclock_from_tain (struct timespec *, tain_t const *) ; -extern int sysclock_get (tain_t *) ; -extern int tain_sysclock (tain_t *) ; +typedef int tain_clockread_func_t (tain_t *) ; +typedef tain_clockread_func_t *tain_clockread_func_t_ref ; + extern int tain_from_sysclock (tain_t *, tain_t const *) ; extern int sysclock_from_tain (tain_t *, tain_t const *) ; -#define tain_sysclock_g() tain_sysclock(&STAMP) -extern int tain_clockmon_init (tain_t *) ; -extern int tain_clockmon (tain_t *, tain_t const *) ; -#define tain_clockmon_g(offset) tain_clockmon(&STAMP, (offset)) -extern int tain_init (void) ; -extern int tain_now (tain_t *) ; -#define tain_now_g() tain_now(&STAMP) +extern tain_clockread_func_t sysclock_get ; +extern tain_clockread_func_t tain_wallclock_read ; +#define tain_wallclock_read_g() tain_wallclock_read(&STAMP) +extern int tain_stopwatch_init (clock_t, tain_t *) ; +extern int tain_stopwatch_read (tain_t *, clock_t, tain_t const *) ; +#define tain_stopwatch_read_g(cl, offset) tain_stopwatch_read(&STAMP, (cl), offset) +extern tain_clockread_func_t_ref tain_now ; +#define tain_now_g() (*tain_now)(&STAMP) #define tain_copynow(t) (*(t) = STAMP) +extern void tain_now_set_wallclock (void) ; +extern void tain_now_set_stopwatch (void) ; + extern int sysclock_set (tain_t const *) ; extern int tain_setnow (tain_t const *) ; diff --git a/src/libstddjb/sysclock_get.c b/src/libstddjb/sysclock_get.c index 3d14f96..6c5041d 100644 --- a/src/libstddjb/sysclock_get.c +++ b/src/libstddjb/sysclock_get.c @@ -1,17 +1,9 @@ /* ISC license. */ -#include <skalibs/config.h> #include <skalibs/sysdeps.h> #include <skalibs/tai.h> -#ifdef SKALIBS_FLAG_USERT -# ifndef SKALIBS_HASCLOCKRT -# undef SKALIBS_FLAG_USERT -# warning "SKALIBS_FLAG_USERT set but SKALIBS_HASCLOCKRT not found. Clearing SKALIBS_FLAG_USERT." -# endif -#endif - -#ifdef SKALIBS_FLAG_USERT +#ifdef SKALIBS_HASCLOCKRT #include <time.h> diff --git a/src/libstddjb/sysclock_set.c b/src/libstddjb/sysclock_set.c index 0ec51c6..57e2abe 100644 --- a/src/libstddjb/sysclock_set.c +++ b/src/libstddjb/sysclock_set.c @@ -2,24 +2,16 @@ /* MT-unsafe */ -#include <skalibs/config.h> #include <skalibs/sysdeps.h> -#ifdef SKALIBS_FLAG_USERT -# ifndef SKALIBS_HASCLOCKRT -# undef SKALIBS_FLAG_USERT -# warning "SKALIBS_FLAG_USERT set but SKALIBS_HASCLOCKRT not found. Clearing SKALIBS_FLAG_USERT." -# endif -#endif - -#ifndef SKALIBS_FLAG_USERT +#ifndef SKALIBS_HASCLOCKRT # ifndef SKALIBS_HASSETTIMEOFDAY -# error "SKALIBS_FLAG_USERT clear but SKALIBS_HASSETTIMEOFDAY not found. How do your set your system clock?" +# error "neither clockrt nor settimeofday sysdeps are present. How do your set your system clock?" # endif #endif -#ifdef SKALIBS_FLAG_USERT +#ifdef SKALIBS_HASCLOCKRT #include <time.h> #include <skalibs/tai.h> diff --git a/src/libstddjb/tain_clockmon.c b/src/libstddjb/tain_clockmon.c deleted file mode 100644 index f14cd31..0000000 --- a/src/libstddjb/tain_clockmon.c +++ /dev/null @@ -1,47 +0,0 @@ -/* ISC license. */ - -#include <skalibs/sysdeps.h> -#include <skalibs/tai.h> - -#ifdef SKALIBS_HASCLOCKMON - -#include <time.h> - -int tain_clockmon_init (tain_t *offset) -{ - tain_t a, b ; - struct timespec ts ; - if (!tain_sysclock(&a)) return 0 ; - if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) return 0 ; - if (!tain_from_timespec(&b, &ts)) return 0 ; - tain_add(&a, &a, &tain_nano500) ; - tain_sub(offset, &a, &b) ; - return 1 ; -} - -int tain_clockmon (tain_t *a, tain_t const *offset) -{ - struct timespec ts ; - if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) return 0 ; - if (!tain_from_timespec(a, &ts)) return 0 ; - tain_add(a, a, offset) ; - return 1 ; -} - -#else - -#include <errno.h> - -int tain_clockmon_init (tain_t *offset) -{ - (void)offset ; - return (errno = ENOSYS, 0) ; -} - -int tain_clockmon (tain_t *a, tain_t const *offset) -{ - (void)a ; (void)offset ; - return (errno = ENOSYS, 0) ; -} - -#endif diff --git a/src/libstddjb/tain_now.c b/src/libstddjb/tain_now.c index 157087a..d6625d6 100644 --- a/src/libstddjb/tain_now.c +++ b/src/libstddjb/tain_now.c @@ -2,48 +2,6 @@ /* MT-unsafe */ -#include <skalibs/config.h> -#include <skalibs/sysdeps.h> #include <skalibs/tai.h> -#ifdef SKALIBS_FLAG_USEMON -# ifndef SKALIBS_HASCLOCKMON -# undef SKALIBS_FLAG_USEMON -# warning "SKALIBS_FLAG_USEMON set but SKALIBS_HASCLOCKMON not found. Clearing SKALIBS_FLAG_USEMON." -# endif -#endif - - -#ifdef SKALIBS_FLAG_USEMON - -static tain_t offset ; - -int tain_init () -{ - return tain_clockmon_init(&offset) ; -} - -int tain_now (tain_t *a) -{ - static int initted = 0 ; - if (!initted) - { - if (!tain_clockmon_init(&offset)) return 0 ; - initted = 1 ; - } - return tain_clockmon(a, &offset) ; -} - -#else - -int tain_init () -{ - return 1 ; -} - -int tain_now (tain_t *a) -{ - return tain_sysclock(a) ; -} - -#endif +tain_clockread_func_t_ref tain_now = &tain_wallclock_read ; diff --git a/src/libstddjb/tain_now_set_stopwatch.c b/src/libstddjb/tain_now_set_stopwatch.c new file mode 100644 index 0000000..184a40a --- /dev/null +++ b/src/libstddjb/tain_now_set_stopwatch.c @@ -0,0 +1,36 @@ +/* ISC license. */ + +/* MT-unsafe */ + +#include <skalibs/tai.h> + +#if defined(SKALIBS_HAS_CLOCKRT) && (defined(SKALIBS_HAS_CLOCKMON) || defined(SKALIBS_HAS_CLOCKBOOT)) + +#include <time.h> + +#ifdef SKALIBS_HAS_CLOCKMON +# define SKALIBS_STOPWATCH CLOCK_MONOTONIC +#else +# define SKALIBS_STOPWATCH CLOCK_BOOTTIME +#endif + +static tain_t offset ; + +static int tain_now_stopwatch (tain_t *a) +{ + return tain_stopwatch_read(a, SKALIBS_STOPWATCH, &offset) ; +} + +void tain_now_set_stopwatch (void) +{ + if (!tain_stopwatch_init(SKALIBS_STOPWATCH, &offset)) return ; + tain_now = &tain_now_stopwatch ; +} + +#else + +void tain_now_set_stopwatch (void) +{ +} + +#endif diff --git a/src/libstddjb/tain_now_set_wallclock.c b/src/libstddjb/tain_now_set_wallclock.c new file mode 100644 index 0000000..a3d8b61 --- /dev/null +++ b/src/libstddjb/tain_now_set_wallclock.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +/* MT-unsafe */ + +#include <skalibs/tai.h> + +void tain_now_set_wallclock (void) +{ + tain_now = &tain_wallclock_read ; +} diff --git a/src/libstddjb/tain_stopwatch.c b/src/libstddjb/tain_stopwatch.c new file mode 100644 index 0000000..2bb4974 --- /dev/null +++ b/src/libstddjb/tain_stopwatch.c @@ -0,0 +1,50 @@ +/* ISC license. */ + +#include <skalibs/sysdeps.h> +#include <skalibs/tai.h> + +#if defined(SKALIBS_HASCLOCKRT) && (defined(SKALIBS_HASCLOCKMON) || defined(SKALIBS_HASCLOCKBOOT)) + +#include <time.h> + +int tain_stopwatch_init (clock_t cl, tain_t *offset) +{ + tain_t a, b ; + struct timespec ts ; + if (!tain_wallclock_read(&a)) return 0 ; + if (clock_gettime(cl, &ts) < 0) return 0 ; + if (!tain_from_timespec(&b, &ts)) return 0 ; + tain_add(&a, &a, &tain_nano500) ; + tain_sub(offset, &a, &b) ; + return 1 ; +} + +int tain_stopwatch_read (tain_t *a, clock_t cl, tain_t const *offset) +{ + struct timespec ts ; + if (clock_gettime(cl, &ts) < 0) return 0 ; + if (!tain_from_timespec(a, &ts)) return 0 ; + tain_add(a, a, offset) ; + return 1 ; +} + +#else + +#include <errno.h> + +int tain_stopwatch_init (clock_t cl, tain_t *offset) +{ + (void)cl ; + (void)offset ; + return (errno = ENOSYS, 0) ; +} + +int tain_stopwatch_read (tain_t *a, clock_t cl, tain_t const *offset) +{ + (void)a ; + (void)cl ; + (void)offset ; + return (errno = ENOSYS, 0) ; +} + +#endif diff --git a/src/libstddjb/tain_sysclock.c b/src/libstddjb/tain_wallclock_read.c index 3b98eb1..96ed861 100644 --- a/src/libstddjb/tain_sysclock.c +++ b/src/libstddjb/tain_wallclock_read.c @@ -2,7 +2,7 @@ #include <skalibs/tai.h> -int tain_sysclock (tain_t *a) +int tain_wallclock_read (tain_t *a) { tain_t aa ; if (!sysclock_get(&aa)) return 0 ; diff --git a/src/libstddjb/timestamp_r.c b/src/libstddjb/timestamp_r.c index 6eb8ab7..46be7a4 100644 --- a/src/libstddjb/timestamp_r.c +++ b/src/libstddjb/timestamp_r.c @@ -4,7 +4,7 @@ int timestamp_r (char *s, tain_t *stamp) { - if (!tain_sysclock(stamp)) return 0 ; + if (!tain_wallclock_read(stamp)) return 0 ; timestamp_fmt(s, stamp) ; return 1 ; } diff --git a/src/sysdeps/tryclockboot.c b/src/sysdeps/tryclockboot.c new file mode 100644 index 0000000..de8bf5c --- /dev/null +++ b/src/sysdeps/tryclockboot.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include <time.h> + +int main (void) +{ + struct timespec ts ; + if (clock_gettime(CLOCK_BOOTTIME, &ts) < 0) return 111 ; + return 0 ; +} |