From ef2ad6b9988a9f03aa2a2961b4e0950e7e2e1ec8 Mon Sep 17 00:00:00 2001 From: David Joseph Date: Tue, 9 Apr 2013 13:00:42 -0400 Subject: [PATCH] regressions: Implement common_gettimeofday. Windows uses GetSystemTimeAsFileTime, which has some differences with Unix' gettimeofday. This commit also reduces the number of iterations for the ck_ticket_pb validation to 1000 - the test was taking too long on MinGW systems. --- .../ck_spinlock/validate/ck_ticket_pb.c | 4 ++ regressions/ck_stack/validate/pair.c | 4 +- regressions/ck_stack/validate/pop.c | 4 +- regressions/ck_stack/validate/push.c | 4 +- regressions/common.h | 45 +++++++++++++++++++ 5 files changed, 55 insertions(+), 6 deletions(-) diff --git a/regressions/ck_spinlock/validate/ck_ticket_pb.c b/regressions/ck_spinlock/validate/ck_ticket_pb.c index e62ee0e..28cba32 100644 --- a/regressions/ck_spinlock/validate/ck_ticket_pb.c +++ b/regressions/ck_spinlock/validate/ck_ticket_pb.c @@ -1,2 +1,6 @@ #include "../ck_ticket_pb.h" +#ifdef _WIN32 +#undef ITERATE +#define ITERATE 1000 +#endif #include "validate.h" diff --git a/regressions/ck_stack/validate/pair.c b/regressions/ck_stack/validate/pair.c index c26b11c..f4adae5 100644 --- a/regressions/ck_stack/validate/pair.c +++ b/regressions/ck_stack/validate/pair.c @@ -233,11 +233,11 @@ main(int argc, char *argv[]) for (i = 0; i < nthr; i++) pthread_create(&thread[i], NULL, stack_thread, bucket + i); - gettimeofday(&stv, NULL); + common_gettimeofday(&stv, NULL); barrier = 1; for (i = 0; i < nthr; i++) pthread_join(thread[i], NULL); - gettimeofday(&etv, NULL); + common_gettimeofday(&etv, NULL); stack_assert(); printf("%3llu %.6lf\n", nthr, TVTOD(etv) - TVTOD(stv)); diff --git a/regressions/ck_stack/validate/pop.c b/regressions/ck_stack/validate/pop.c index 3d06aef..5881bfa 100644 --- a/regressions/ck_stack/validate/pop.c +++ b/regressions/ck_stack/validate/pop.c @@ -253,11 +253,11 @@ main(int argc, char *argv[]) for (i = 0; i < nthr; i++) pthread_create(&thread[i], NULL, stack_thread, NULL); - gettimeofday(&stv, NULL); + common_gettimeofday(&stv, NULL); barrier = 1; for (i = 0; i < nthr; i++) pthread_join(thread[i], NULL); - gettimeofday(&etv, NULL); + common_gettimeofday(&etv, NULL); stack_assert(); printf("%3llu %.6lf\n", nthr, TVTOD(etv) - TVTOD(stv)); diff --git a/regressions/ck_stack/validate/push.c b/regressions/ck_stack/validate/push.c index 44d515b..9b5ae7e 100644 --- a/regressions/ck_stack/validate/push.c +++ b/regressions/ck_stack/validate/push.c @@ -232,11 +232,11 @@ main(int argc, char *argv[]) for (i = 0; i < nthr; i++) pthread_create(&thread[i], NULL, stack_thread, bucket + i * n); - gettimeofday(&stv, NULL); + common_gettimeofday(&stv, NULL); barrier = 1; for (i = 0; i < nthr; i++) pthread_join(thread[i], NULL); - gettimeofday(&etv, NULL); + common_gettimeofday(&etv, NULL); stack_assert(); printf("%3llu %.6lf\n", nthr, TVTOD(etv) - TVTOD(stv)); diff --git a/regressions/common.h b/regressions/common.h index 98e0901..1cdc174 100644 --- a/regressions/common.h +++ b/regressions/common.h @@ -41,9 +41,12 @@ #if defined(_WIN32) #include +#include #include +#define DELTA_EPOCH 11644473600000000ULL #else #include +#include #include #endif @@ -127,6 +130,48 @@ common_sleep(unsigned int n) #endif } +CK_CC_INLINE static int +common_gettimeofday(struct timeval *tv, void *tz) +{ +#ifdef _WIN32 + FILETIME ft; + uint64_t tmp_time = 0; + static bool tzflag = false; + + if (tv != NULL) { + GetSystemTimeAsFileTime(&ft); + tmp_time |= ft.dwHighDateTime; + tmp_time <<= 32; + tmp_time |= ft.dwLowDateTime; + + /* GetSystemTimeAsFileTime returns 100 nanosecond intervals. */ + tmp_time /= 10; + + /* Windows' epoch starts on 01/01/1601, while Unix' starts on 01/01/1970. */ + tmp_time -= DELTA_EPOCH; + } + + tv->tv_sec = (long)(tmp_time / 1000000UL); + tv->tv_usec = (long)(tmp_time % 1000000UL); + + if (tz != NULL) { + if (tzflag == false) { + _tzset(); + tzflag = true; + } + + struct timezone *tzp = (struct timezone *)tz; + + tzp->tz_minuteswest = _timezone / 60; + tzp->tz_dsttime = _daylight; + } + + return 0; +#else + return gettimeofday(tv, tz); +#endif +} + CK_CC_UNUSED static unsigned int common_alarm(void (*sig_handler)(int), void *alarm_event, unsigned int duration) {