From 98f1c31e81c65660bcdfbdfd9368353ed5c95145 Mon Sep 17 00:00:00 2001 From: Patrick O'Leary Date: Tue, 10 Nov 2015 09:40:30 -0600 Subject: [PATCH 1/2] on Linux, use CLOCK_MONOTONIC for clock_gettime() On Linux systems which are being driven by an external time source (NTP or PTP), it is possible that time appears to slew in reverse under `CLOCK_REALTIME`. Since the timer function is used to time durations of events (calls to `select()`), it is better to use `CLOCK_MONOTONIC`, which isn't subject to slewing. --- src/impl/unix.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 0b7ab42..e0a8953 100755 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -91,7 +91,7 @@ MillisecondTimer::timespec_now () time.tv_sec = mts.tv_sec; time.tv_nsec = mts.tv_nsec; # else - clock_gettime(CLOCK_REALTIME, &time); + clock_gettime(CLOCK_MONOTONIC, &time); # endif return time; } From c5b4bbd1816e0acb25d8b1ac302e097a1669dc44 Mon Sep 17 00:00:00 2001 From: Patrick O'Leary Date: Tue, 10 Nov 2015 17:28:45 -0600 Subject: [PATCH 2/2] on OS X, use SYSTEM_CLOCK, not CALENDAR_CLOCK Analogously to using `CLOCK_MONOTONIC` on Linux to time events in favor of `CLOCK_REALTIME`, `SYSTEM_CLOCK` should be used in favor of `CALENDAR_CLOCK` on OS X. Ref: http://stackoverflow.com/questions/11680461/monotonic-clock-on-osx --- src/impl/unix.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/impl/unix.cc b/src/impl/unix.cc index e0a8953..0ff251e 100755 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -85,7 +85,7 @@ MillisecondTimer::timespec_now () # ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time clock_serv_t cclock; mach_timespec_t mts; - host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); + host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); clock_get_time(cclock, &mts); mach_port_deallocate(mach_task_self(), cclock); time.tv_sec = mts.tv_sec;