From d285833290316cb5dd1e7f1e52c96be3e9cf21cd Mon Sep 17 00:00:00 2001 From: Daniel Stone Date: Wed, 25 Oct 2006 23:57:00 +0300 Subject: GetTimeInMillis: spuport monotonic clock Add support for CLOCK_MONOTONIC from clock_gettime, and use that in GetTimeInMillis() if available, falling back to the old gettimeofday() implementation. This is _slightly_ faster on some 64-bit architectures, and _slightly_ slower on others (though barely measurable). --- os/utils.c | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'os') diff --git a/os/utils.c b/os/utils.c index 31ae26a18..379291c9d 100644 --- a/os/utils.c +++ b/os/utils.c @@ -53,6 +53,19 @@ OR PERFORMANCE OF THIS SOFTWARE. #include #endif +/* The world's most shocking hack, to ensure we get clock_gettime() and + * CLOCK_MONOTONIC. */ +#ifdef _POSIX_C_SOURCE +#define _SAVED_POSIX_C_SOURCE _POSIX_C_SOURCE +#undef _POSIX_C_SOURCE +#endif +#define _POSIX_C_SOURCE 199309L +#include +#undef _POSIX_C_SOURCE +#ifdef _SAVED_POSIX_C_SOURCE +#define _POSIX_C_SOURCE _SAVED_POSIX_C_SOURCE +#endif + #ifdef __CYGWIN__ #include #include @@ -92,7 +105,6 @@ OR PERFORMANCE OF THIS SOFTWARE. #if !defined(SYSV) && !defined(WIN32) && !defined(Lynx) && !defined(QNX4) #include #endif -#include #include #include /* for isspace */ #include @@ -256,6 +268,8 @@ int auditTrailLevel = 1; _X_EXPORT Bool Must_have_memory = FALSE; +static int monotonic_usable = -1; + #ifdef AIXV3 int SyncOn = 0; extern int SelectWaitTime; @@ -535,10 +549,27 @@ GiveUp(int sig) _X_EXPORT CARD32 GetTimeInMillis(void) { - struct timeval tp; + struct timeval tv; +#ifdef MONOTONIC_CLOCK + struct timespec tp; + int spare = 0; + + if (_X_UNLIKELY(monotonic_usable == -1)) { + if (clock_gettime(0, &tp) == 0 && + clock_getcpuclockid(0, &spare) == 0) + monotonic_usable = 1; + else + monotonic_usable = 0; + } + + if (_X_LIKELY(monotonic_usable == 1)) { + if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0) + return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000); + } +#endif - X_GETTIMEOFDAY(&tp); - return(tp.tv_sec * 1000) + (tp.tv_usec / 1000); + X_GETTIMEOFDAY(&tv); + return(tv.tv_sec * 1000) + (tv.tv_usec / 1000); } _X_EXPORT void -- cgit v1.2.3