Use monotonic clock in Select.c (#5865)
authorPaolo Capriotti <p.capriotti@gmail.com>
Tue, 20 Mar 2012 11:57:28 +0000 (11:57 +0000)
committerPaolo Capriotti <p.capriotti@gmail.com>
Mon, 16 Apr 2012 14:18:19 +0000 (15:18 +0100)
rts/posix/Clock.h [new file with mode: 0644]
rts/posix/GetTime.c
rts/posix/Itimer.c
rts/posix/Select.c

diff --git a/rts/posix/Clock.h b/rts/posix/Clock.h
new file mode 100644 (file)
index 0000000..5062023
--- /dev/null
@@ -0,0 +1,32 @@
+/* -----------------------------------------------------------------------------
+ *
+ * (c) The GHC Team, 2012
+ *
+ * Posix monotonic clock
+ *
+ * ---------------------------------------------------------------------------*/
+
+#ifndef POSIX_CLOCK_H
+#define POSIX_CLOCK_H
+
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#ifdef HAVE_TIME_H
+# include <time.h>
+#endif
+
+#ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+#endif
+
+#ifdef HAVE_CLOCK_GETTIME
+# ifdef _POSIX_MONOTONIC_CLOCK
+#  define CLOCK_ID CLOCK_MONOTONIC
+# else
+#  define CLOCK_ID CLOCK_REALTIME
+# endif
+#endif
+
+#endif /* POSIX_CLOCK_H */
index 16511ce..4abc82f 100644 (file)
 
 #include "Rts.h"
 #include "GetTime.h"
-
-#ifdef HAVE_TIME_H
-# include <time.h>
-#endif
-
-#ifdef HAVE_SYS_TIME_H
-# include <sys/time.h>
-#endif
+#include "Clock.h"
 
 #if HAVE_SYS_RESOURCE_H
 # include <sys/resource.h>
 #endif
 
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
 #ifdef HAVE_SYS_TIMES_H
 # include <sys/times.h>
 #endif
@@ -77,9 +66,17 @@ Time getProcessCPUTime(void)
 
 Time getProcessElapsedTime(void)
 {
+#ifdef HAVE_CLOCK_GETTIME
+    struct timespec ts;
+
+    clock_gettime(CLOCK_ID, &ts);
+    return SecondsToTime(ts.tv_sec) + NSToTime(ts.tv_nsec);
+#else
     struct timeval tv;
+
     gettimeofday(&tv, (struct timezone *) NULL);
     return SecondsToTime(tv.tv_sec) + USToTime(tv.tv_usec);
+#endif
 }
 
 void getProcessTimes(Time *user, Time *elapsed)
index 13ba345..8c9b1f8 100644 (file)
@@ -24,7 +24,7 @@
 #include "Itimer.h"
 #include "Proftimer.h"
 #include "Schedule.h"
-#include "Select.h"
+#include "Clock.h"
 
 /* As recommended in the autoconf manual */
 # ifdef TIME_WITH_SYS_TIME
@@ -123,7 +123,6 @@ initTicker (Time interval, TickProc handle_tick)
 #if defined(USE_TIMER_CREATE)
     {
         struct sigevent ev;
-        clockid_t clock;
 
         // Keep programs like valgrind happy
         memset(&ev, 0, sizeof(ev));
@@ -131,13 +130,7 @@ initTicker (Time interval, TickProc handle_tick)
         ev.sigev_notify = SIGEV_SIGNAL;
         ev.sigev_signo  = ITIMER_SIGNAL;
 
-#if defined(CLOCK_MONOTONIC)
-        clock = CLOCK_MONOTONIC;
-#else
-        clock = CLOCK_REALTIME;
-#endif
-
-        if (timer_create(clock, &ev, &timer) != 0) {
+        if (timer_create(CLOCK_ID, &ev, &timer) != 0) {
             sysErrorBelch("timer_create");
             stg_exit(EXIT_FAILURE);
         }
index a2a66a6..5acb817 100644 (file)
@@ -20,6 +20,7 @@
 #include "Select.h"
 #include "AwaitEvent.h"
 #include "Stats.h"
+#include "GetTime.h"
 
 # ifdef HAVE_SYS_SELECT_H
 #  include <sys/select.h>
 #  include <sys/types.h>
 # endif
 
-# ifdef HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# endif
-
 #include <errno.h>
 #include <string.h>
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
+#include "Clock.h"
 
 #if !defined(THREADED_RTS)
 
@@ -64,7 +59,7 @@
  */
 static LowResTime getLowResTimeOfDay(void)
 {
-    return TimeToLowResTimeRoundDown(stat_getElapsedTime());
+    return TimeToLowResTimeRoundDown(getProcessElapsedTime());
 }
 
 /*
@@ -74,7 +69,7 @@ LowResTime getDelayTarget (HsInt us)
 {
     // round up the target time, because we never want to sleep *less*
     // than the desired amount.
-    return TimeToLowResTimeRoundUp(stat_getElapsedTime() + USToTime(us));
+    return TimeToLowResTimeRoundUp(getProcessElapsedTime() + USToTime(us));
 }
 
 /* There's a clever trick here to avoid problems when the time wraps
@@ -277,7 +272,7 @@ awaitEvent(rtsBool wait)
          /* check for threads that need waking up 
           */
           wakeUpSleepingThreads(getLowResTimeOfDay());
-         
+
          /* If new runnable threads have arrived, stop waiting for
           * I/O and run them.
           */