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

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