Blame SOURCES/time-1.8-Prefer-clock_gettime-CLOCK_MONOTONIC.patch

27f975
From a3c400a8553b598bc2fd01eb0f63c5748b2147e1 Mon Sep 17 00:00:00 2001
27f975
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
27f975
Date: Wed, 8 Nov 2017 17:02:42 +0100
27f975
Subject: [PATCH] Prefer clock_gettime(CLOCK_MONOTONIC)
27f975
MIME-Version: 1.0
27f975
Content-Type: text/plain; charset=UTF-8
27f975
Content-Transfer-Encoding: 8bit
27f975
27f975
gettimeofday() reports wrong elapsed real time if a time step was
27f975
inserted while running a program. This can happen on initial time
27f975
adjustment from NTP server or by manual adjustement by date command.
27f975
27f975
This patch uses clock_gettime(CLOCK_MONOTONIC) instead (if available)
27f975
that does not suffer from the issue.
27f975
27f975
<http://lists.gnu.org/archive/html/bug-gnu-utils/2013-09/msg00008.html>
27f975
27f975
Signed-off-by: Petr Písař <ppisar@redhat.com>
27f975
---
27f975
 configure.ac |  3 +++
27f975
 src/resuse.c | 27 +++++++++++++++++++++++++--
27f975
 2 files changed, 28 insertions(+), 2 deletions(-)
27f975
27f975
diff --git a/configure.ac b/configure.ac
27f975
index ede8fd5..d2950bd 100644
27f975
--- a/configure.ac
27f975
+++ b/configure.ac
27f975
@@ -72,6 +72,9 @@ dnl Checks for library functions.
27f975
 AC_FUNC_VPRINTF
27f975
 AC_FUNC_WAIT3
27f975
 AC_CHECK_FUNCS(strerror)
27f975
+AC_SEARCH_LIBS(clock_gettime, [rt])
27f975
+test "$ac_cv_search_clock_gettime" != "no" && \
27f975
+    AC_DEFINE([HAVE_CLOCK_GETTIME], [1], [System provides clock_gettime() call])
27f975
 
27f975
 
27f975
 # What memory units are reported by getrusage(2) ?
27f975
diff --git a/src/resuse.c b/src/resuse.c
27f975
index d2ab870..ec54863 100644
27f975
--- a/src/resuse.c
27f975
+++ b/src/resuse.c
27f975
@@ -26,7 +26,14 @@
27f975
 #include <sys/wait.h>
27f975
 #include <sys/resource.h>
27f975
 
27f975
-#if !HAVE_WAIT3
27f975
+#if HAVE_WAIT3
27f975
+# if HAVE_CLOCK_GETTIME
27f975
+#  ifndef _POSIX_C_SOURCE
27f975
+#   define _POSIX_C_SOURCE 199309L
27f975
+#  endif
27f975
+#  include <time.h>
27f975
+# endif
27f975
+#else
27f975
 # include <sys/times.h>
27f975
 # ifndef HZ
27f975
 #  include <sys/param.h>
27f975
@@ -51,7 +58,14 @@ resuse_start (resp)
27f975
      RESUSE *resp;
27f975
 {
27f975
 #if HAVE_WAIT3
27f975
+#if HAVE_CLOCK_GETTIME
27f975
+  struct timespec res;
27f975
+  clock_gettime(CLOCK_MONOTONIC, &res;;
27f975
+  resp->start.tv_sec = res.tv_sec;
27f975
+  resp->start.tv_usec = res.tv_nsec / 1000;
27f975
+#else
27f975
   gettimeofday (&resp->start, (struct timezone *) 0);
27f975
+#endif /* !HAVE_CLOCK_GETTIME */
27f975
 #else
27f975
   long value;
27f975
   struct tms tms;
27f975
@@ -59,7 +73,7 @@ resuse_start (resp)
27f975
   value = times (&tms);
27f975
   resp->start.tv_sec = value / HZ;
27f975
   resp->start.tv_usec = value % HZ * (1000000 / HZ);
27f975
-#endif
27f975
+#endif /* !HAVE_WAIT3 */
27f975
 }
27f975
 
27f975
 /* Wait for and fill in data on child process PID.
27f975
@@ -79,6 +93,9 @@ resuse_end (pid, resp)
27f975
   int status;
27f975
 
27f975
 #if HAVE_WAIT3
27f975
+#if HAVE_CLOCK_GETTIME
27f975
+  struct timespec res;
27f975
+#endif
27f975
   pid_t caught;
27f975
 
27f975
   /* Ignore signals, but don't ignore the children.  When wait3
27f975
@@ -89,7 +106,13 @@ resuse_end (pid, resp)
27f975
 	return 0;
27f975
     }
27f975
 
27f975
+#if HAVE_CLOCK_GETTIME
27f975
+  clock_gettime(CLOCK_MONOTONIC, &res;;
27f975
+  resp->elapsed.tv_sec = res.tv_sec;
27f975
+  resp->elapsed.tv_usec = res.tv_nsec / 1000;
27f975
+#else
27f975
   gettimeofday (&resp->elapsed, (struct timezone *) 0);
27f975
+#endif
27f975
 #else  /* !HAVE_WAIT3 */
27f975
   long value;
27f975
   struct tms tms;
27f975
-- 
27f975
2.13.6
27f975