Use CLOCK_MONOTONIC for benchmarking, if possible

This commit is contained in:
Frank Denis 2023-02-15 19:42:08 +01:00
parent 39b4300cf2
commit abfbcab412
3 changed files with 16 additions and 6 deletions

View File

@ -70,6 +70,7 @@ pub fn build(b: *std.build.Builder) !void {
lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1"); lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1");
lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1"); lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1");
lib.defineCMacro("HAVE_CLOCK_GETTIME", "1");
lib.defineCMacro("HAVE_GETPID", "1"); lib.defineCMacro("HAVE_GETPID", "1");
lib.defineCMacro("HAVE_INLINE_ASM", "1"); lib.defineCMacro("HAVE_INLINE_ASM", "1");
lib.defineCMacro("HAVE_MADVISE", "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_ARC4RANDOM_BUF", "1");
lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1"); lib.defineCMacro("HAVE_CATCHABLE_ABRT", "1");
lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1"); lib.defineCMacro("HAVE_CATCHABLE_SEGV", "1");
lib.defineCMacro("HAVE_CLOCK_GETTIME", "1");
lib.defineCMacro("HAVE_GETENTROPY", "1"); lib.defineCMacro("HAVE_GETENTROPY", "1");
lib.defineCMacro("HAVE_GETPID", "1"); lib.defineCMacro("HAVE_GETPID", "1");
lib.defineCMacro("HAVE_MADVISE", "1"); lib.defineCMacro("HAVE_MADVISE", "1");
@ -121,6 +123,7 @@ pub fn build(b: *std.build.Builder) !void {
.wasi => { .wasi => {
lib.defineCMacro("HAVE_ARC4RANDOM", "1"); lib.defineCMacro("HAVE_ARC4RANDOM", "1");
lib.defineCMacro("HAVE_ARC4RANDOM_BUF", "1"); lib.defineCMacro("HAVE_ARC4RANDOM_BUF", "1");
lib.defineCMacro("HAVE_CLOCK_GETTIME", "1");
lib.defineCMacro("HAVE_GETENTROPY", "1"); lib.defineCMacro("HAVE_GETENTROPY", "1");
lib.defineCMacro("HAVE_NANOSLEEP", "1"); lib.defineCMacro("HAVE_NANOSLEEP", "1");
lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1"); lib.defineCMacro("HAVE_POSIX_MEMALIGN", "1");

View File

@ -881,7 +881,7 @@ AS_IF([test "x$WASI" = "x"],[
AC_CHECK_FUNCS([getauxva elf_aux_info]) 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"],[ AS_IF([test "x$WASI" = "x"],[
AC_CHECK_FUNCS([memset_s explicit_bzero memset_explicit explicit_memset]) AC_CHECK_FUNCS([memset_s explicit_bzero memset_explicit explicit_memset])

View File

@ -118,16 +118,23 @@ static int mempool_free_all(void)
static unsigned long long now(void) static unsigned long long now(void)
{ {
#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; struct timeval tp;
unsigned long long now;
if (gettimeofday(&tp, NULL) != 0) { if (gettimeofday(&tp, NULL) != 0) {
abort(); abort();
} }
now = ((unsigned long long) tp.tv_sec * 1000000ULL) + return (unsigned long long) tp.tv_sec * 1000000ULL +
(unsigned long long) tp.tv_usec; (unsigned long long) tp.tv_usec;
#endif
return now;
} }
int main(void) int main(void)