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

Dmitri Vorobiev dmitri.vorobiev at movial.com
Wed Sep 15 07:34:58 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 |    5 +++++
 test/utils.c |   14 ++++++++++++++
 test/utils.h |    4 ++++
 3 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/configure.ac b/configure.ac
index dbff2a6..3589092 100644
--- a/configure.ac
+++ b/configure.ac
@@ -623,6 +623,11 @@ 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)
+if test x$have_gettimeofday = 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 d95cbc2..f79978c 100644
--- a/test/utils.c
+++ b/test/utils.c
@@ -325,6 +325,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 a39af02..0ab9d1a 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