Try multiple clock sources at runtime.

This commit is contained in:
John Hood
2016-12-23 21:28:45 -05:00
parent fad60f9ca3
commit b2eac32ebc
+30 -15
View File
@@ -37,12 +37,15 @@
#include <errno.h> #include <errno.h>
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
#include <time.h> #include <time.h>
#elif HAVE_MACH_ABSOLUTE_TIME #endif
#include <mach/mach_time.h> #if HAVE_MACH_ABSOLUTE_TIME
#elif HAVE_GETTIMEOFDAY #include <mach/error.h>
#include <sys/time.h> #include <mach/mach_time.h>
#include <stdio.h> #endif
#if HAVE_GETTIMEOFDAY
#include <sys/time.h>
#include <stdio.h>
#endif #endif
static uint64_t millis_cache = -1; static uint64_t millis_cache = -1;
@@ -58,32 +61,44 @@ uint64_t frozen_timestamp( void )
void freeze_timestamp( void ) void freeze_timestamp( void )
{ {
// Try all our clock sources till we get something. This could
// break if a source only sometimes works in a given process.
#if HAVE_CLOCK_GETTIME #if HAVE_CLOCK_GETTIME
// Preferred clock source-- portable, monotonic, (should be)
// adjusted after system sleep
struct timespec tp; struct timespec tp;
if ( clock_gettime( CLOCK_MONOTONIC, &tp ) < 0 ) { // Check for presence, for OS X SDK >= 10.12 and runtime < 10.12
/* did not succeed */ if ( &clock_gettime != NULL && clock_gettime( CLOCK_MONOTONIC, &tp ) == 0 ) {
} else {
uint64_t millis = tp.tv_nsec / 1000000; uint64_t millis = tp.tv_nsec / 1000000;
millis += uint64_t( tp.tv_sec ) * 1000; millis += uint64_t( tp.tv_sec ) * 1000;
millis_cache = millis; millis_cache = millis;
return; return;
} }
#elif HAVE_MACH_ABSOLUTE_TIME #endif
#if HAVE_MACH_ABSOLUTE_TIME
// Monotonic, not adjusted after system sleep. OS X 10.12 has
// mach_continuous_time(), but also has clock_gettime().
static mach_timebase_info_data_t s_timebase_info; static mach_timebase_info_data_t s_timebase_info;
static double absolute_to_millis; static double absolute_to_millis = 0.0;
if (s_timebase_info.denom == 0) { if (absolute_to_millis == 0.0) {
mach_timebase_info(&s_timebase_info); if (ERR_SUCCESS == mach_timebase_info(&s_timebase_info)) {
absolute_to_millis = 1e-6 * s_timebase_info.numer / s_timebase_info.denom; absolute_to_millis = 1e-6 * s_timebase_info.numer / s_timebase_info.denom;
} else
absolute_to_millis = -1.0;
} }
// NB: mach_absolute_time() returns "absolute time units" // NB: mach_absolute_time() returns "absolute time units"
// We need to apply a conversion to get milliseconds. // We need to apply a conversion to get milliseconds.
if (absolute_to_millis > 0.0) {
millis_cache = mach_absolute_time() * absolute_to_millis; millis_cache = mach_absolute_time() * absolute_to_millis;
return; return;
#elif HAVE_GETTIMEOFDAY }
#endif
#if HAVE_GETTIMEOFDAY
// Not monotonic.
// NOTE: If time steps backwards, timeouts may be confused. // NOTE: If time steps backwards, timeouts may be confused.
struct timeval tv; struct timeval tv;
if ( gettimeofday(&tv, NULL) ) { if ( gettimeofday(&tv, NULL) ) {
@@ -96,6 +111,6 @@ void freeze_timestamp( void )
return; return;
} }
#else #else
# error "Don't know how to get a timestamp on this platform" # error "gettimeofday() unavailable-- required as timer of last resort"
#endif #endif
} }