diff options
Diffstat (limited to 'test/vfd_swmr_common.c')
-rw-r--r-- | test/vfd_swmr_common.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/test/vfd_swmr_common.c b/test/vfd_swmr_common.c index f09336e..b986b31 100644 --- a/test/vfd_swmr_common.c +++ b/test/vfd_swmr_common.c @@ -28,6 +28,43 @@ static const hid_t badhid = H5I_INVALID_HID; int verbosity = 2; +/* Return true no more than once in any `ival` interval of time, + * as measured by the system's monotonically increasing timer, to + * help rate-limit activities. + * + * Read the system's current time and compare it with the time stored in + * `last`. If the difference between `last` and the current time is + * greater than the duration `ival`, then record the current time at + * `last` and return true. Otherwise, return false. + */ +bool +below_speed_limit(struct timespec *last, const struct timespec *ival) +{ + struct timespec now; + bool result; + + assert(0 <= last->tv_nsec && last->tv_nsec < 1000000000L); + assert(0 <= ival->tv_nsec && ival->tv_nsec < 1000000000L); + + if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) + err(EXIT_FAILURE, "%s: clock_gettime", __func__); + + if (now.tv_sec - last->tv_sec > ival->tv_sec) + result = true; + else if (now.tv_sec - last->tv_sec < ival->tv_sec) + result = false; + else + result = (now.tv_nsec - last->tv_nsec >= ival->tv_nsec); + + if (result) + *last = now; + + return result; +} + +/* Like vsnprintf(3), but abort the program with an error message on + * `stderr` if the buffer is too small or some other error occurs. + */ void evsnprintf(char *buf, size_t bufsz, const char *fmt, va_list ap) { |