From 05b8da1a4909ac8a733103d0527d08c76a190741 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Wed, 15 Feb 2023 19:42:08 +0100 Subject: [PATCH] Use CLOCK_MONOTONIC for benchmarking, if possible --- build.zig | 3 +++ configure.ac | 2 +- test/default/cmptest.h | 17 ++++++++++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/build.zig b/build.zig index eff3b2fc..91c1eef5 100644 --- a/build.zig +++ b/build.zig @@ -70,6 +70,7 @@ pub fn build(b: *std.build.Builder) !void { lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1"); lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1"); + lib.defineCMacro("HAVE_CLOCK_GETTIME", "1"); lib.defineCMacro("HAVE_GETPID", "1"); lib.defineCMacro("HAVE_INLINE_ASM", "1"); lib.defineCMacro("HAVE_MADVISE", "1"); @@ -100,6 +101,7 @@ pub fn build(b: *std.build.Builder) !void { lib.defineCMacro("HAVE_ARC4RANDOM_BUF", "1"); lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1"); lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1"); + lib.defineCMacro("HAVE_CLOCK_GETTIME", "1"); lib.defineCMacro("HAVE_GETENTROPY", "1"); lib.defineCMacro("HAVE_GETPID", "1"); lib.defineCMacro("HAVE_MADVISE", "1"); @@ -121,6 +123,7 @@ pub fn build(b: *std.build.Builder) !void { .wasi => { lib.defineCMacro("HAVE_ARC4RANDOM", "1"); lib.defineCMacro("HAVE_ARC4RANDOM_BUF", "1"); + lib.defineCMacro("HAVE_CLOCK_GETTIME", "1"); lib.defineCMacro("HAVE_GETENTROPY", "1"); lib.defineCMacro("HAVE_NANOSLEEP", "1"); lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1"); diff --git a/configure.ac b/configure.ac index 0e550fed..5e830db5 100644 --- a/configure.ac +++ b/configure.ac @@ -905,7 +905,7 @@ AS_IF([test "x$WASI" = "x"],[ AC_CHECK_FUNCS([getauxva elf_aux_info]) ]) -AC_CHECK_FUNCS([posix_memalign nanosleep]) +AC_CHECK_FUNCS([posix_memalign nanosleep clock_gettime]) AS_IF([test "x$WASI" = "x"],[ AC_CHECK_FUNCS([memset_s explicit_bzero memset_explicit explicit_memset]) diff --git a/test/default/cmptest.h b/test/default/cmptest.h index b1261bdb..54a02070 100644 --- a/test/default/cmptest.h +++ b/test/default/cmptest.h @@ -118,16 +118,23 @@ static int mempool_free_all(void) static unsigned long long now(void) { - struct timeval tp; - unsigned long long now; +#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) + struct timespec tp; + + if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) { + abort(); + } + return (unsigned long long) tp.tv_sec * 1000000ULL + + (unsigned long long) tp.tv_nsec / 1000ULL; +#else + struct timeval tp; if (gettimeofday(&tp, NULL) != 0) { abort(); } - now = ((unsigned long long) tp.tv_sec * 1000000ULL) + + return (unsigned long long) tp.tv_sec * 1000000ULL + (unsigned long long) tp.tv_usec; - - return now; +#endif } int main(void)