[clblas] 33/125: OSX: Provide nanosecond resolution timer for tests

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Fri May 29 06:57:20 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch master
in repository clblas.

commit 6bd10142f48ade74d1ecf4430b2728b2926233ee
Author: Christian Kellner <christian at kellner.me>
Date:   Wed Nov 6 14:14:58 2013 +0100

    OSX: Provide nanosecond resolution timer for tests
    
    Directly implement the getCurrentTime function on OSX instead
    of providing a gettimeofday based clock_gettime implementation;
    we use mach_absolute_time together with mach_timebase_info.
    NB: The multiplication is done first and then the division
    because the risk of overflowing seems smaller then precision
    loss together with the risk of actually having a ratio of zero,
    i.e. when denom > numer, when doing it the other way around.
---
 src/tests/include/timer.h |  6 +++++
 src/tests/timer.c         | 69 ++++++++++++++++++++++++++++-------------------
 2 files changed, 47 insertions(+), 28 deletions(-)

diff --git a/src/tests/include/timer.h b/src/tests/include/timer.h
index 29353ff..41c8e27 100644
--- a/src/tests/include/timer.h
+++ b/src/tests/include/timer.h
@@ -27,6 +27,12 @@ extern "C" {
 typedef unsigned long long nano_time_t;
 #define NANOTIME_MAX (~0ULL - 1)
 
+#elif defined(__APPLE__)
+#include <stdint.h>
+
+typedef uint64_t nano_time_t;
+#define NANOTIME_MAX (UINT64_MAX - 1)
+
 #else
 
 typedef unsigned long nano_time_t;
diff --git a/src/tests/timer.c b/src/tests/timer.c
index 8b9c54d..0184479 100644
--- a/src/tests/timer.c
+++ b/src/tests/timer.c
@@ -79,27 +79,52 @@ sleepTime(nano_time_t time) {
 
 #include <time.h>
 
-#ifdef __APPLE__
-#include <sys/time.h>
-// we dont have clock_gettime on mac, fake it
-// NB: this is *not* nano-second precision
-#define CLOCK_REALTIME 0
-static int
-clock_gettime(int time_id, struct timespec *t)
+#if defined(__APPLE__) && defined(__MACH__)
+
+#include <assert.h>
+#include <mach/mach.h>
+#include <mach/mach_time.h>
+#include <pthread.h>
+
+// see https://developer.apple.com/library/mac/qa/qa1398/_index.html
+static mach_timebase_info_data_t mtb_;
+
+static void
+init_timebase_conv_(void)
 {
-  struct timeval nuc;
-  int err;
+    kern_return_t err;
 
-  err = gettimeofday(&nuc, NULL);
-  if (err != 0) {
-    return err;
-  }
+    err = mach_timebase_info(&mtb_);
+    assert(err == KERN_SUCCESS);
+}
 
-  t->tv_sec = nuc.tv_sec;
-  t->tv_nsec = nuc.tv_usec * 1000;
+nano_time_t
+getCurrentTime(void)
+{
+     static pthread_once_t once = PTHREAD_ONCE_INIT;
+     uint64_t              now;
 
-  return 0;
+     pthread_once(&once, init_timebase_conv_);
+     now = mach_absolute_time();
+
+     return (now * mtb_.numer) / mtb_.denom;
 }
+
+#else /* ! (_MCS_VER || __APPLE__) */
+
+nano_time_t
+getCurrentTime(void)
+{
+    int err;
+    struct timespec t;
+
+    err = clock_gettime(CLOCK_REALTIME, &t);
+    if (err == 0) {
+        return (t.tv_sec * 1000000000UL + t.tv_nsec);
+    }
+    return 0;
+}
+
 #endif
 
 
@@ -122,18 +147,6 @@ conv2millisec(nano_time_t t)
     return t/1000000;
 }
 
-nano_time_t
-getCurrentTime(void)
-{
-    int err;
-    struct timespec t;
-
-    err = clock_gettime(CLOCK_REALTIME, &t);
-    if (err == 0) {
-        return (t.tv_sec * 1000000000UL + t.tv_nsec);
-    }
-    return 0;
-}
 
 void
 sleepTime(nano_time_t time) {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/clblas.git



More information about the debian-science-commits mailing list