[Pixman] [PATCH 2/4] Add gettime() routine to test utils

Dmitri Vorobiev dmitri.vorobiev at movial.com
Fri Sep 17 07:52:21 PDT 2010


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 deletions(-)

diff --git a/configure.ac b/configure.ac
index dbff2a6..80f8443 100644
--- a/configure.ac
+++ b/configure.ac
@@ -606,7 +606,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
@@ -623,6 +623,12 @@ if test x$have_alarm = xyes; then
    AC_DEFINE(HAVE_ALARM, 1, [Whether we have alarm()])
 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 b10450e..4674aa2 100644
--- a/test/utils.c
+++ b/test/utils.c
@@ -1,6 +1,12 @@
 #include "utils.h"
 #include <signal.h>
 
+#ifdef HAVE_GETTIMEOFDAY
+#include <sys/time.h>
+#else
+#include <time.h>
+#endif
+
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -325,6 +331,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 5074bb5..5a94d94 100644
--- a/test/utils.h
+++ b/test/utils.h
@@ -55,6 +55,10 @@ image_endian_swap (pixman_image_t *img, int bpp);
 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,
-- 
1.6.3.3



More information about the Pixman mailing list