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).
This commit is contained in:
Daniel Stone 2006-10-25 23:57:00 +03:00 committed by Daniel Stone
commit d285833290
3 changed files with 79 additions and 6 deletions

View file

@ -53,6 +53,19 @@ OR PERFORMANCE OF THIS SOFTWARE.
#include <dix-config.h>
#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 <time.h>
#undef _POSIX_C_SOURCE
#ifdef _SAVED_POSIX_C_SOURCE
#define _POSIX_C_SOURCE _SAVED_POSIX_C_SOURCE
#endif
#ifdef __CYGWIN__
#include <stdlib.h>
#include <signal.h>
@ -92,7 +105,6 @@ OR PERFORMANCE OF THIS SOFTWARE.
#if !defined(SYSV) && !defined(WIN32) && !defined(Lynx) && !defined(QNX4)
#include <sys/resource.h>
#endif
#include <time.h>
#include <sys/stat.h>
#include <ctype.h> /* for isspace */
#include <stdarg.h>
@ -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;
X_GETTIMEOFDAY(&tp);
return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
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(&tv);
return(tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
_X_EXPORT void