From 56525d640d63da8710d97503ebc0ef6279afcb54 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?=
Date: Wed, 30 Nov 2016 17:13:16 +0100
Subject: [PATCH 2/2] Prefer clock_gettime(CLOCK_MONOTONIC)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
gettimeofday() reports wrong elapsed real time if a time step was
inserted while running a program. This can happen on initial time
adjustment from NTP server or by manual adjustement by date command.
This patch uses clock_gettime(CLOCK_MONOTONIC) instead (if available)
that does not suffer from the issue.
Signed-off-by: Petr Písař
---
configure.ac | 1 +
resuse.c | 27 +++++++++++++++++++++++++--
2 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/configure.ac b/configure.ac
index 2380b76..658a254 100644
--- a/configure.ac
+++ b/configure.ac
@@ -29,6 +29,7 @@ dnl Checks for library functions.
AC_FUNC_VPRINTF
AC_FUNC_WAIT3
AC_CHECK_FUNCS(strerror)
+AC_CHECK_FUNCS(clock_gettime)
AC_MSG_CHECKING(for getpagesize)
AC_TRY_LINK([#include ],
diff --git a/resuse.c b/resuse.c
index 4133941..da0da64 100644
--- a/resuse.c
+++ b/resuse.c
@@ -23,7 +23,14 @@
#include "wait.h"
#include "port.h"
-#if !HAVE_WAIT3
+#if HAVE_WAIT3
+# if HAVE_CLOCK_GETTIME
+# ifndef _POSIX_C_SOURCE
+# define _POSIX_C_SOURCE 199309L
+# endif
+# include
+# endif
+#else
# include
# ifndef HZ
# include
@@ -48,7 +55,14 @@ resuse_start (resp)
RESUSE *resp;
{
#if HAVE_WAIT3
+#if HAVE_CLOCK_GETTIME
+ struct timespec res;
+ clock_gettime(CLOCK_MONOTONIC, &res);
+ resp->start.tv_sec = res.tv_sec;
+ resp->start.tv_usec = res.tv_nsec / 1000;
+#else
gettimeofday (&resp->start, (struct timezone *) 0);
+#endif /* !HAVE_CLOCK_GETTIME */
#else
long value;
struct tms tms;
@@ -56,7 +70,7 @@ resuse_start (resp)
value = times (&tms);
resp->start.tv_sec = value / HZ;
resp->start.tv_usec = value % HZ * (1000000 / HZ);
-#endif
+#endif /* !HAVE_WAIT3 */
}
/* Wait for and fill in data on child process PID.
@@ -76,6 +90,9 @@ resuse_end (pid, resp)
int status;
#if HAVE_WAIT3
+#if HAVE_CLOCK_GETTIME
+ struct timespec res;
+#endif
pid_t caught;
/* Ignore signals, but don't ignore the children. When wait3
@@ -86,7 +103,13 @@ resuse_end (pid, resp)
return 0;
}
+#if HAVE_CLOCK_GETTIME
+ clock_gettime(CLOCK_MONOTONIC, &res);
+ resp->elapsed.tv_sec = res.tv_sec;
+ resp->elapsed.tv_usec = res.tv_nsec / 1000;
+#else
gettimeofday (&resp->elapsed, (struct timezone *) 0);
+#endif
#else /* !HAVE_WAIT3 */
long value;
struct tms tms;
--
2.7.4