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

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