summaryrefslogtreecommitdiffstats
path: root/test/vfd_swmr_common.c
diff options
context:
space:
mode:
authorDavid Young <dyoung@hdfgroup.org>2020-09-17 19:35:54 (GMT)
committerDavid Young <dyoung@hdfgroup.org>2020-09-17 19:35:54 (GMT)
commit8e2053bb3d29ab8f90cd28d7e66641ac2472063e (patch)
tree089e994dc2e2bd4bbea63b15f08fdbdc36bbc80a /test/vfd_swmr_common.c
parentda688311afebd2d6b188c50cbf78c5e05e757ba9 (diff)
downloadhdf5-8e2053bb3d29ab8f90cd28d7e66641ac2472063e.zip
hdf5-8e2053bb3d29ab8f90cd28d7e66641ac2472063e.tar.gz
hdf5-8e2053bb3d29ab8f90cd28d7e66641ac2472063e.tar.bz2
Move below_speed_limit() from vfd_swmr_bigset_writer.c to
vfd_swmr_common.c, document it, and fix a bug.
Diffstat (limited to 'test/vfd_swmr_common.c')
-rw-r--r--test/vfd_swmr_common.c37
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)
{