]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
Fall back to gettimeofday if there’s no clock_gettime. 3519/head
authorSebastian Schmidt <yath@google.com>
Wed, 12 Aug 2020 10:04:11 +0000 (12:04 +0200)
committerSebastian Schmidt <yath@google.com>
Wed, 12 Aug 2020 10:05:35 +0000 (12:05 +0200)
distbench/main.c

index 91c7d2f20ebc61931c958e696002edf272cbcb92..4a433a91ec03df41dc88ef3c15ab89791932a573 100644 (file)
@@ -3,7 +3,11 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
+#if HAVE_CLOCK_GETTIME
 #include <time.h>
+#else
+#include <sys/time.h>
+#endif
 #include <unistd.h>
 
 /* Local headers */
 /* 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) {