Commit 749b7506 authored by Wouter Klouwen's avatar Wouter Klouwen

ProcessUNIX: Use monotonic clock in kwsysProcessTimeGetCurrent() for POSIX

While a process is running, it is possible for other processes such as
NTP to alter the system clock in large jumps.

To make the measurement of how long a process has taken to run
impervious to such large jumps it is helpful to use a monotonic clock.
This is a clock that takes an arbitrary starting point that does not
change.

clock_gettime() is a POSIX method that implements this clock. This is
not available on all platforms, so this commit adds a check for its
presence. If the monotonic timer is available, use that in
kwsysProcessTimeGetCurrent(), otherwise falls back to previous
non-monotonic behaviour.
parent 239bc737
......@@ -445,8 +445,13 @@ KWSYS_PLATFORM_C_TEST(KWSYS_C_HAS_PTRDIFF_T
"Checking whether C compiler has ptrdiff_t in stddef.h" DIRECT)
KWSYS_PLATFORM_C_TEST(KWSYS_C_HAS_SSIZE_T
"Checking whether C compiler has ssize_t in unistd.h" DIRECT)
IF(KWSYS_USE_Process)
KWSYS_PLATFORM_C_TEST(KWSYS_C_HAS_CLOCK_GETTIME_MONOTONIC
"Checking whether C compiler has clock_gettime" DIRECT)
ENDIF()
SET_SOURCE_FILES_PROPERTIES(ProcessUNIX.c System.c PROPERTIES
COMPILE_FLAGS "-DKWSYS_C_HAS_PTRDIFF_T=${KWSYS_C_HAS_PTRDIFF_T} -DKWSYS_C_HAS_SSIZE_T=${KWSYS_C_HAS_SSIZE_T}"
COMPILE_FLAGS "-DKWSYS_C_HAS_PTRDIFF_T=${KWSYS_C_HAS_PTRDIFF_T} -DKWSYS_C_HAS_SSIZE_T=${KWSYS_C_HAS_SSIZE_T} -DKWSYS_C_HAS_CLOCK_GETTIME_MONOTONIC=${KWSYS_C_HAS_CLOCK_GETTIME_MONOTONIC}"
)
IF(DEFINED KWSYS_PROCESS_USE_SELECT)
......
......@@ -2026,7 +2026,15 @@ static kwsysProcessTime kwsysProcessTimeGetCurrent(void)
{
kwsysProcessTime current;
kwsysProcessTimeNative current_native;
#if KWSYS_C_HAS_CLOCK_GETTIME_MONOTONIC
struct timespec current_timespec;
clock_gettime(CLOCK_MONOTONIC, &current_timespec);
current_native.tv_sec = current_timespec.tv_sec;
current_native.tv_usec = current_timespec.tv_nsec / 1000;
#else
gettimeofday(&current_native, 0);
#endif
current.tv_sec = (long)current_native.tv_sec;
current.tv_usec = (long)current_native.tv_usec;
return current;
......
......@@ -55,6 +55,21 @@ int KWSYS_PLATFORM_TEST_C_MAIN()
}
#endif
#ifdef TEST_KWSYS_C_HAS_CLOCK_GETTIME_MONOTONIC
#if defined(__APPLE__)
#include <AvailabilityMacros.h>
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101200
#error "clock_gettime not available on macOS < 10.12"
#endif
#endif
#include <time.h>
int KWSYS_PLATFORM_TEST_C_MAIN()
{
struct timespec ts;
return clock_gettime(CLOCK_MONOTONIC, &ts);
}
#endif
#ifdef TEST_KWSYS_C_TYPE_MACROS
char* info_macros =
#if defined(__SIZEOF_SHORT__)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment