From cab3261c0da6e833d803a7f3ccab600adca7abe1 Mon Sep 17 00:00:00 2001 From: Dmitri Vorobiev Date: Fri, 17 Sep 2010 17:52:21 +0300 Subject: [PATCH] Add gettime() routine to test utils Impending benchmark code will need a function to get current time in seconds, and this patch introduces such routine. We try to use the POSIX gettimeofday() function when available, and fall back to clock() when not. --- configure.ac | 8 +++++++- test/utils.c | 20 ++++++++++++++++++++ test/utils.h | 4 ++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 47213a0..4b38abd 100644 --- a/configure.ac +++ b/configure.ac @@ -609,7 +609,7 @@ AC_SUBST(DEP_CFLAGS) AC_SUBST(DEP_LIBS) dnl ===================================== -dnl posix_memalign, sigaction, alarm +dnl posix_memalign, sigaction, alarm, gettimeofday AC_CHECK_FUNC(posix_memalign, have_posix_memalign=yes, have_posix_memalign=no) if test x$have_posix_memalign = xyes; then @@ -639,6 +639,12 @@ if test x$have_getpagesize = xyes; then AC_DEFINE(HAVE_GETPAGESIZE, 1, [Whether we have getpagesize()]) fi +AC_CHECK_FUNC(gettimeofday, have_gettimeofday=yes, have_gettimeofday=no) +AC_CHECK_HEADER(sys/time.h, have_sys_time_h=yes, have_sys_time_h=no) +if test x$have_gettimeofday = xyes && test x$have_sys_time_h = xyes; then + AC_DEFINE(HAVE_GETTIMEOFDAY, 1, [Whether we have gettimeofday()]) +fi + dnl ===================================== dnl Thread local storage diff --git a/test/utils.c b/test/utils.c index ccc637e..2cd763d 100644 --- a/test/utils.c +++ b/test/utils.c @@ -1,6 +1,12 @@ #include "utils.h" #include +#ifdef HAVE_GETTIMEOFDAY +#include +#else +#include +#endif + #ifdef HAVE_UNISTD_H #include #endif @@ -431,6 +437,20 @@ fuzzer_test_main (const char *test_name, return 0; } +/* Try to obtain current time in seconds */ +double +gettime (void) +{ +#ifdef HAVE_GETTIMEOFDAY + struct timeval tv; + + gettimeofday (&tv, NULL); + return (double)((int64_t)tv.tv_sec * 1000000 + tv.tv_usec) / 1000000.; +#else + return (double)clock() / (double)CLOCKS_PER_SEC; +#endif +} + static const char *global_msg; static void diff --git a/test/utils.h b/test/utils.h index 06cd857..e7920f0 100644 --- a/test/utils.h +++ b/test/utils.h @@ -64,6 +64,10 @@ fence_free (void *data); uint8_t * make_random_bytes (int n_bytes); +/* Return current time in seconds */ +double +gettime (void); + /* main body of the fuzzer test */ int fuzzer_test_main (const char *test_name, -- 2.7.4