[Spread-cvs] commit: r459 - in trunk: . daemon

jschultz at spread.org jschultz at spread.org
Fri Jan 13 16:08:57 EST 2012


Author: jschultz
Date: 2012-01-13 16:08:57 -0500 (Fri, 13 Jan 2012)
New Revision: 459

Modified:
   trunk/configure.in
   trunk/daemon/Changelog
   trunk/daemon/config.h.in
   trunk/daemon/events.c
Log:
On POSIX systems use monotonic clock (i.e. - clock_gettime(CLOCK_MONOTONIC, ...)) inside events.c rather than wall clock time, which can jump when set.


Modified: trunk/configure.in
===================================================================
--- trunk/configure.in	2012-01-11 18:37:42 UTC (rev 458)
+++ trunk/configure.in	2012-01-13 21:08:57 UTC (rev 459)
@@ -113,6 +113,19 @@
 dnl    Checks for time functions
 AC_CHECK_FUNCS(gettimeofday time)
 
+# Check for clock_gettime(CLOCK_MONOTONIC, ...)
+AC_CACHE_CHECK([for clock_gettime(CLOCK_MONOTONIC)], ac_cv_clock_gettime_monotonic, [
+	AC_TRY_COMPILE(
+		[ #include <time.h> ],
+		[ struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); ],
+		[ ac_cv_clock_gettime_monotonic="yes" ],
+		[ ac_cv_clock_gettime_monotonic="no" ]
+	)
+])
+if test "x$ac_cv_clock_gettime_monotonic" = "xyes" ; then
+	AC_DEFINE(HAVE_CLOCK_GETTIME_CLOCK_MONOTONIC, 1, [Have clock_gettime(CLOCK_MONOTONIC, ...)!])
+fi
+
 # Check for broken snprintf
 if test "x$ac_cv_func_snprintf" = "xyes" ; then
 	AC_MSG_CHECKING([whether snprintf correctly terminates long strings])

Modified: trunk/daemon/Changelog
===================================================================
--- trunk/daemon/Changelog	2012-01-11 18:37:42 UTC (rev 458)
+++ trunk/daemon/Changelog	2012-01-13 21:08:57 UTC (rev 459)
@@ -1,3 +1,10 @@
+Fri Jan 13 15:39:48 2012  John Schultz <jschultz at spreadconcepts.com>
+
+	* configure.in, events.c: Added a monotonic timer based on
+	clock_gettime(CLOCK_MONOTONIC, ...).  Events will use this
+	fcn internally when available rather than wall clock time
+	which can jump backwards and forwards (e.g. - when set).
+
 Tue Jan 10 14:50:23 2012  John Schultz <jschultz at spreadconcepts.com>
 
 	* mutex.h, sp.c: Changed mutex.h to add Once_execute capabilities.

Modified: trunk/daemon/config.h.in
===================================================================
--- trunk/daemon/config.h.in	2012-01-11 18:37:42 UTC (rev 458)
+++ trunk/daemon/config.h.in	2012-01-13 21:08:57 UTC (rev 459)
@@ -28,6 +28,9 @@
 /* Define to 1 if you have the `bcopy' function. */
 #undef HAVE_BCOPY
 
+/* Have clock_gettime(CLOCK_MONOTONIC, ...)! */
+#undef HAVE_CLOCK_GETTIME_CLOCK_MONOTONIC
+
 /* clock_t type */
 #undef HAVE_CLOCK_T
 
@@ -121,6 +124,9 @@
 /* Define to 1 if you have the <signal.h> header file. */
 #undef HAVE_SIGNAL_H
 
+/* sockaddr_in type has sin_len field */
+#undef HAVE_SIN_LEN_IN_SOCKADDR_IN
+
 /* size_t type */
 #undef HAVE_SIZE_T
 
@@ -290,7 +296,7 @@
 #undef SPREAD_ETCDIR
 
 /* "Specify location of Unix Domain Socket for client-daemon communication on
-   local machine" */
+   local machine." */
 #undef SP_UNIX_SOCKET
 
 /* Define to 1 if you have the ANSI C header files. */

Modified: trunk/daemon/events.c
===================================================================
--- trunk/daemon/events.c	2012-01-11 18:37:42 UTC (rev 458)
+++ trunk/daemon/events.c	2012-01-13 21:08:57 UTC (rev 459)
@@ -86,6 +86,8 @@
 	fd_event	events[MAX_FD_EVENTS];
 } fd_queue;
 
+static sp_time E_get_time_monotonic(void);
+
 static	time_event	*Time_queue;
 static	sp_time		Now;
 
@@ -117,7 +119,7 @@
         }
 	Active_priority = LOW_PRIORITY;
 
-	E_get_time();
+	E_get_time_monotonic();
 
 	Alarm( EVENTS, "E_init: went ok\n");
 
@@ -126,6 +128,8 @@
 
 sp_time	E_get_time(void)
 {
+        sp_time t;
+
 #ifndef	ARCH_PC_WIN95
         struct timeval  read_time;
 
@@ -138,8 +142,8 @@
 
 	ret = gettimeofday( &read_time, &dummy_tz );
 	if ( ret < 0 ) Alarm( EXIT, "E_get_time: gettimeofday problems.\n" );
-        Now.sec = read_time.tv_sec;
-        Now.usec = read_time.tv_usec;
+        t.sec  = read_time.tv_sec;
+        t.usec = read_time.tv_usec;
 
 #else	/* ARCH_PC_WIN95 */
 
@@ -147,17 +151,39 @@
 
 	_ftime( &timebuffer );
 
-	Now.sec = timebuffer.time;
-	Now.usec= timebuffer.millitm;
-	Now.usec= Now.usec * 1000;
+	t.sec   = timebuffer.time;
+	t.usec  = timebuffer.millitm;
+	t.usec *= 1000;	
 
 #endif	/* ARCH_PC_WIN95 */
 #if 0
-	Alarm( EVENTS, "E_get_time: time is (%d, %d)\n", Now.sec, Now.usec);
+	Alarm( EVENTS, "E_get_time: time is (%d, %d)\n", t.sec, t.usec);
 #endif
-	return ( Now );
+	return ( t );
 }
 
+static sp_time E_get_time_monotonic(void)
+#ifdef HAVE_CLOCK_GETTIME_CLOCK_MONOTONIC
+{
+  struct timespec t;
+
+  if (clock_gettime(CLOCK_MONOTONIC, &t) != 0) {
+    Alarm( EXIT, "E_get_time_monotonic: clock_gettime problems: %d '%s'\n", errno, sys_errlist[errno] );
+  }
+
+  Now.sec  = t.tv_sec;
+  Now.usec = (t.tv_nsec + 500) / 1000;
+
+  return Now;
+}
+#else
+{
+  Now = E_get_time();
+
+  return Now;
+}
+#endif
+
 sp_time	E_sub_time( sp_time t, sp_time delta_t )
 {
 	sp_time	res;
@@ -208,7 +234,7 @@
 
 	t_e       = new( TIME_EVENT );
 
-	t_e->t    = E_add_time( E_get_time(), delta_time );
+	t_e->t    = E_add_time( E_get_time_monotonic(), delta_time );
 	t_e->func = func;
         t_e->code = code;
         t_e->data = data;
@@ -583,18 +609,18 @@
 	/* Handle time events */
 	timeout = long_timeout;
 #ifdef TESTTIME
-        start = E_get_time();
+        start = E_get_time_monotonic();
 #endif
 	while( Time_queue != NULL )
 	{
 #ifdef BADCLOCK
 		if ( clock_sync >= 0 )
 		{
-		    E_get_time();
+		    E_get_time_monotonic();
 		    clock_sync = -20;
 		}
 #else
-                E_get_time();
+                E_get_time_monotonic();
 #endif
 		if ( !first && E_compare_time( Now, Time_queue->t ) >= 0 )
 		{
@@ -613,7 +639,7 @@
 			Now = E_add_time( Now, mili_sec );
 			clock_sync++;
 #else
-                        E_get_time();
+                        E_get_time_monotonic();
 #endif
                         if (Exit_events) goto end_handler;
 		}else{
@@ -624,7 +650,7 @@
         if (timeout.sec < 0 )
                 timeout.sec = timeout.usec = 0; /* this can happen until first is unset */
 #ifdef TESTTIME
-        stop = E_get_time();
+        stop = E_get_time_monotonic();
         tmp_late = E_sub_time(stop, start);
         Alarm(DEBUG, "Events: TimeEv's took %d %d to handle\n", tmp_late.sec, tmp_late.usec); 
 #endif
@@ -661,7 +687,7 @@
 				  &current_mask[EXCEPT_FD], &sel_timeout );
 	}
 #ifdef TESTTIME
-        start = E_get_time();
+        start = E_get_time_monotonic();
         tmp_late = E_sub_time(start, stop);
         Alarm( DEBUG, "Events: Waiting for fd or timout took %d %d asked for %d %d\n", tmp_late.sec, tmp_late.usec, req_time.sec, req_time.usec);
 #endif
@@ -688,7 +714,7 @@
 		    Now = E_add_time( Now, mili_sec );
 		    clock_sync++;
 #else
-                    E_get_time();
+                    E_get_time_monotonic();
 #endif
                     if (Exit_events) goto end_handler;
 		}
@@ -704,7 +730,7 @@
                 first = 0;
 
 #ifdef TESTTIME
-        stop = E_get_time();
+        stop = E_get_time_monotonic();
         tmp_late = E_sub_time(stop, start);
         Alarm(DEBUG, "Events: High & Med took %d %d time to handle\n", tmp_late.sec, tmp_late.usec);
 #endif
@@ -734,14 +760,14 @@
 		Now = E_add_time( Now, mili_sec );
 		clock_sync++;
 #else
-                E_get_time();
+                E_get_time_monotonic();
 #endif
                 if (Exit_events) goto end_handler;
 		break;
 	    }
 	}	
 #ifdef TESTTIME
-        start = E_get_time();
+        start = E_get_time_monotonic();
         tmp_late = E_sub_time(start, stop);
         Alarm(DEBUG, "Events: Low priority took %d %d to handle\n", tmp_late.sec, tmp_late.usec);
 #endif




More information about the Spread-cvs mailing list