From 4f8343b8b1cde3efda5f81d460af894e3fc43d50 Mon Sep 17 00:00:00 2001 From: Gabriele Gristina Date: Tue, 26 Dec 2017 13:49:17 +0100 Subject: [PATCH 1/3] Fix clock_gettime() on OSX --- include/timer.h | 7 +++++++ src/timer.c | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/timer.h b/include/timer.h index e46a40feb..7129303e8 100644 --- a/include/timer.h +++ b/include/timer.h @@ -6,6 +6,13 @@ #ifndef _TIMER_H #define _TIMER_H +#if defined(__APPLE__) +#include +#include +#include +#include +#endif + void hc_timer_set (hc_timer_t *a); double hc_timer_get (hc_timer_t a); diff --git a/src/timer.c b/src/timer.c index 01aa86e10..ad1ccbf86 100644 --- a/src/timer.c +++ b/src/timer.c @@ -33,7 +33,25 @@ inline double hc_timer_get (hc_timer_t a) inline void hc_timer_set (hc_timer_t* a) { + #if defined (__APPLE__) + // taken from proxmark3/client/util_posix + static uint64_t clock_start_time = 0; + static mach_timebase_info_data_t timebase_info = {0, 0}; + uint64_t now = mach_absolute_time(); + + if (clock_start_time == 0) + { + mach_timebase_info(&timebase_info); + clock_start_time = now; + } + + now = (uint64_t)((double)(now - clock_start_time) * (double)timebase_info.numer / (double)timebase_info.denom); + + a->tv_sec = now / 1000000000; + a->tv_nsec = now % 1000000000; + #else clock_gettime (CLOCK_MONOTONIC, a); + #endif } inline double hc_timer_get (hc_timer_t a) From 43ce4f61352d6fcd5af4ed9ea22226052ed2f111 Mon Sep 17 00:00:00 2001 From: Gabriele Gristina Date: Tue, 26 Dec 2017 14:09:30 +0100 Subject: [PATCH 2/3] Add checks for clock_gettime() support in Makefile --- include/timer.h | 2 +- src/Makefile | 6 ++++++ src/timer.c | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/timer.h b/include/timer.h index 7129303e8..63a23db30 100644 --- a/include/timer.h +++ b/include/timer.h @@ -6,7 +6,7 @@ #ifndef _TIMER_H #define _TIMER_H -#if defined(__APPLE__) +#if defined(__APPLE__) && defined(MISSING_CLOCK_GETTIME) #include #include #include diff --git a/src/Makefile b/src/Makefile index 5a2ff193c..ae491b6f5 100644 --- a/src/Makefile +++ b/src/Makefile @@ -92,6 +92,7 @@ ifeq ($(UNAME),Darwin) CC := clang # the sed -i option of macOS requires a parameter for the backup file (we just use "") SED_IN_PLACE := -i "" +PROD_VERS := $(shell sw_vers -productVersion | cut -d. -f2) endif ifeq ($(UNAME),FreeBSD) @@ -215,6 +216,11 @@ endif # FreeBSD ifeq ($(UNAME),Darwin) export MACOSX_DEPLOYMENT_TARGET=10.9 CFLAGS_NATIVE := $(CFLAGS) + +ifeq ($(shell test $(PROD_VERS) -le 11; echo $$?), 0) +CFLAGS_NATIVE += -DMISSING_CLOCK_GETTIME +endif + LFLAGS_NATIVE := $(LFLAGS) LFLAGS_NATIVE += -framework OpenCL LFLAGS_NATIVE += -lpthread diff --git a/src/timer.c b/src/timer.c index ad1ccbf86..4cda1f092 100644 --- a/src/timer.c +++ b/src/timer.c @@ -33,7 +33,7 @@ inline double hc_timer_get (hc_timer_t a) inline void hc_timer_set (hc_timer_t* a) { - #if defined (__APPLE__) + #if defined(__APPLE__) && defined(MISSING_CLOCK_GETTIME) // taken from proxmark3/client/util_posix static uint64_t clock_start_time = 0; static mach_timebase_info_data_t timebase_info = {0, 0}; From 03fab4a3451bdcad8a5927178ce5071919d58f6d Mon Sep 17 00:00:00 2001 From: Gabriele Gristina Date: Thu, 11 Jan 2018 02:41:16 +0100 Subject: [PATCH 3/3] Fix clock_gettime() on OSX (v2) --- include/timer.h | 7 ------- include/types.h | 2 ++ src/timer.c | 20 +++++--------------- 3 files changed, 7 insertions(+), 22 deletions(-) diff --git a/include/timer.h b/include/timer.h index 63a23db30..e46a40feb 100644 --- a/include/timer.h +++ b/include/timer.h @@ -6,13 +6,6 @@ #ifndef _TIMER_H #define _TIMER_H -#if defined(__APPLE__) && defined(MISSING_CLOCK_GETTIME) -#include -#include -#include -#include -#endif - void hc_timer_set (hc_timer_t *a); double hc_timer_get (hc_timer_t a); diff --git a/include/types.h b/include/types.h index 1cc61ef43..d6fbd14ef 100644 --- a/include/types.h +++ b/include/types.h @@ -47,6 +47,8 @@ typedef uint64_t u64; #if defined (_WIN) typedef LARGE_INTEGER hc_timer_t; +#elif defined(__APPLE__) && defined(MISSING_CLOCK_GETTIME) +typedef struct timeval hc_timer_t; #else typedef struct timespec hc_timer_t; #endif diff --git a/src/timer.c b/src/timer.c index 4cda1f092..415514456 100644 --- a/src/timer.c +++ b/src/timer.c @@ -34,21 +34,7 @@ inline double hc_timer_get (hc_timer_t a) inline void hc_timer_set (hc_timer_t* a) { #if defined(__APPLE__) && defined(MISSING_CLOCK_GETTIME) - // taken from proxmark3/client/util_posix - static uint64_t clock_start_time = 0; - static mach_timebase_info_data_t timebase_info = {0, 0}; - uint64_t now = mach_absolute_time(); - - if (clock_start_time == 0) - { - mach_timebase_info(&timebase_info); - clock_start_time = now; - } - - now = (uint64_t)((double)(now - clock_start_time) * (double)timebase_info.numer / (double)timebase_info.denom); - - a->tv_sec = now / 1000000000; - a->tv_nsec = now % 1000000000; + gettimeofday (a, NULL); #else clock_gettime (CLOCK_MONOTONIC, a); #endif @@ -60,6 +46,9 @@ inline double hc_timer_get (hc_timer_t a) hc_timer_set (&hr_tmp); + #if defined(__APPLE__) && defined(MISSING_CLOCK_GETTIME) + return (double) (((hr_tmp.tv_sec - (a).tv_sec) * 1000) + ((double) (hr_tmp.tv_usec - (a).tv_usec) / 1000)); + #else hc_timer_t s; s.tv_sec = hr_tmp.tv_sec - a.tv_sec; @@ -74,6 +63,7 @@ inline double hc_timer_get (hc_timer_t a) double r = ((double) s.tv_sec * 1000) + ((double) s.tv_nsec / 1000000); return r; + #endif } #endif