From 8b12ae73e8322f6ed8c70f45cb24a20050bbe92f Mon Sep 17 00:00:00 2001 From: Baruch Even Date: Thu, 4 Sep 2014 22:11:45 +0300 Subject: [PATCH 1/2] Use CLOCK_MONOTONIC instead of CLOCK_REALTIME This prevents problems when the clock is jumping around which can happen when NTP starts or in a misconfigured environment with multiple NTP servers that are out of sync. --- libcurvecpr/lib/util.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libcurvecpr/lib/util.c b/libcurvecpr/lib/util.c index 08fc8cc..baf0f58 100644 --- a/libcurvecpr/lib/util.c +++ b/libcurvecpr/lib/util.c @@ -61,7 +61,12 @@ long long curvecpr_util_nanoseconds (void) if (clock_get_time(cclock, &t) != KERN_SUCCESS) return -1; -#else +#elif defined(CLOCK_MONOTONIC) + struct timespec t; + + if (clock_gettime(CLOCK_MONOTONIC, &t) != 0) + return -1; +#elif defined(CLOCK_REALTIME) struct timespec t; if (clock_gettime(CLOCK_REALTIME, &t) != 0) From 483c55e068b04abad3ebdd1674ac23a418930aa1 Mon Sep 17 00:00:00 2001 From: Baruch Even Date: Fri, 5 Sep 2014 20:56:00 +0300 Subject: [PATCH 2/2] Update test_nanoseconds to monotonic clock test --- libcurvecpr/test/util/test_nanoseconds.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libcurvecpr/test/util/test_nanoseconds.c b/libcurvecpr/test/util/test_nanoseconds.c index 28fc414..b82a187 100644 --- a/libcurvecpr/test/util/test_nanoseconds.c +++ b/libcurvecpr/test/util/test_nanoseconds.c @@ -8,14 +8,14 @@ START_TEST (test_nanoseconds) long long nanoseconds = curvecpr_util_nanoseconds(); fail_if(nanoseconds < 0); - fail_unless(nanoseconds > 1374047000000000000LL); + fail_unless(nanoseconds > 0); /* On OS X, we may cache the kernel clock reference. Make sure it still works. */ - nanoseconds = curvecpr_util_nanoseconds(); + long long nanoseconds1 = curvecpr_util_nanoseconds(); - fail_if(nanoseconds < 0); - fail_unless(nanoseconds > 1374047000000000000LL); + fail_if(nanoseconds1 < 0); + fail_unless(nanoseconds1 > nanoseconds); } END_TEST