From: Sebastian Schmidt Date: Wed, 12 Aug 2020 10:04:11 +0000 (+0200) Subject: Fall back to gettimeofday if there’s no clock_gettime. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F3519%2Fhead;p=thirdparty%2Fcollectd.git Fall back to gettimeofday if there’s no clock_gettime. --- diff --git a/distbench/main.c b/distbench/main.c index 91c7d2f20..4a433a91e 100644 --- a/distbench/main.c +++ b/distbench/main.c @@ -3,7 +3,11 @@ #include #include #include +#if HAVE_CLOCK_GETTIME #include +#else +#include +#endif #include /* Local headers */ @@ -19,17 +23,29 @@ /* How many nanoseconds there are in a second. */ #define NANOS_PER_SECOND 10000000 +/* How many microseconds there are in a nanosecond. */ +#define MICROS_PER_NANO 1000 + /* How many iterations to run. */ #define ITERATIONS 10000000 -/* Returns the monotonic clock in nanoseconds. */ +/* Returns the clock in nanoseconds. */ static uint64_t get_clock() { +#if HAVE_CLOCK_GETTIME struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) error("Unable to retrieve monotonic clock: %s", strerror(errno)); return (ts.tv_sec * NANOS_PER_SECOND) + ts.tv_nsec; +#else + struct timeval tv; + + if (gettimeofday(&tv, NULL) == -1) + error("Unable to retrieve current time: %s", strerror(errno)); + + return (tv.tv_sec * NANOS_PER_SECOND) + (tv.tv_usec * MICROS_PER_NANO); +#endif } int main(int argc, char **argv) {