From ab453d2332019720609b2df9faa2b4823b2b03aa Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 12 Aug 2020 12:04:11 +0200 Subject: [PATCH] =?utf8?q?Fall=20back=20to=20gettimeofday=20if=20there?= =?utf8?q?=E2=80=99s=20no=20clock=5Fgettime.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- distbench/main.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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) { -- 2.47.2