]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Temporarily backed out time backtrack handling code
authorjames <james@e7ae566f-a301-0410-adde-c780ea21d3b5>
Thu, 13 Apr 2006 09:48:11 +0000 (09:48 +0000)
committerjames <james@e7ae566f-a301-0410-adde-c780ea21d3b5>
Thu, 13 Apr 2006 09:48:11 +0000 (09:48 +0000)
due to issues on Windows.

Rewrote gettimeofday function for Windows to be
simpler and more efficient.

git-svn-id: http://svn.openvpn.net/projects/openvpn/branches/BETA21/openvpn@1005 e7ae566f-a301-0410-adde-c780ea21d3b5

init.c
otime.c
otime.h
syshead.h

diff --git a/init.c b/init.c
index de0a40b6c1c3d9630ecee752804e7c93c6c8b655..c0eeb1a9863ebce6bdf382d02c054e3ca9d7fbfc 100644 (file)
--- a/init.c
+++ b/init.c
@@ -286,6 +286,11 @@ init_static (void)
   return false;
 #endif
 
+#ifdef TIME_TEST
+  time_test ();
+  return false;
+#endif
+
   return true;
 }
 
diff --git a/otime.c b/otime.c
index fbef02d0ba6dcb0c521677c39cedbd948aaa1486..e172805ac7060c802feda47483bbbf40940345c4 100644 (file)
--- a/otime.c
+++ b/otime.c
 
 time_t now = 0;            /* GLOBAL */
 
-#if TIME_BACKTRACK_PROTECTION
-
-static time_t now_adj = 0; /* GLOBAL */
-
-/*
- * Try to filter out time instability caused by the system
- * clock backtracking or jumping forward.
- */
-
-void
-update_now (const time_t system_time)
-{
-  const int threshold = 86400; /* threshold at which to dampen forward jumps */
-  time_t real_time = system_time + now_adj;
-  if (real_time > now)
-    {
-      const time_t overshoot = real_time - now - 1;
-      if (overshoot > threshold && now_adj >= overshoot)
-        {
-          now_adj -= overshoot;
-          real_time -= overshoot;
-        }
-      now = real_time;
-    }
-  else if (real_time < now)
-    {
-      now_adj += (now - real_time);
-    }
-}
-
 #ifdef HAVE_GETTIMEOFDAY
 
 time_t now_usec = 0;       /* GLOBAL */
 
-void
-update_now_usec (struct timeval *tv)
-{
-  const time_t last = now;
-  update_now (tv->tv_sec);
-  if (now > last || tv->tv_usec > now_usec)
-    now_usec = tv->tv_usec;
-}
-
-#endif
-
 #endif
 
 /* 
@@ -193,53 +152,84 @@ frequency_limit_event_allowed (struct frequency_limit *f)
 
 #ifdef WIN32
 
-static double counterPerMicrosecond = -1.0;            /* GLOBAL */
-static unsigned __int64 frequency = 0;                 /* GLOBAL */
-static unsigned __int64 timeSecOffset = 0;             /* GLOBAL */
-static unsigned __int64 startPerformanceCounter = 0;   /* GLOBAL */
+static time_t gtc_base = 0;
+static DWORD gtc_last = 0;
+static time_t last_sec = 0;
+static unsigned int last_msec = 0;
+static bool bt_last = false;
 
-/*
- * gettimeofday for windows
- *
- * CounterPerMicrosecond is the number of counts per microsecond.
- * Double is required if we have less than 1 counter per microsecond.  This has not been tested.
- * On a PIII 700, I get about 3.579545.  This is guaranteed not to change while the processor is running.
- * We really don't need to check for loop detection.  On my machine it would take about 59645564 days to loop.
- * (2^64) / frequency / 60 / 60 / 24.
- *
- */
-int
-gettimeofday(struct timeval *tv, void *tz)
+static void
+gettimeofday_calibrate (void)
 {
-  unsigned __int64 counter;
-
-  QueryPerformanceCounter((LARGE_INTEGER *) &counter);
-
-  if (counter < startPerformanceCounter || counterPerMicrosecond == -1.0)
-    {
-      time_t t;
-      mutex_lock (L_GETTIMEOFDAY);
-
-      QueryPerformanceFrequency((LARGE_INTEGER *) &frequency);
-
-      counterPerMicrosecond = (double) ((__int64) frequency) / 1000000.0f;
+  const time_t t = time(NULL);
+  const DWORD gtc = GetTickCount();
+  gtc_base = t - gtc/1000;
+  gtc_last = gtc;
+}
 
-      time(&t);
-      QueryPerformanceCounter((LARGE_INTEGER *) &counter);
-      startPerformanceCounter = counter;
+int
+gettimeofday (struct timeval *tv, void *tz)
+{
+  const DWORD gtc = GetTickCount();
+  bool bt = false;
+  time_t sec;
+  unsigned int msec;
+  const int backtrack_hold_seconds = 10;
 
-      counter /= frequency;
+  if (!gtc_base || gtc < gtc_last)
+    gettimeofday_calibrate ();
+  gtc_last = gtc;
 
-      timeSecOffset = t - counter;
+  sec = gtc_base + gtc / 1000;
+  msec = gtc % 1000;
 
-      mutex_unlock (L_GETTIMEOFDAY);
-      QueryPerformanceCounter((LARGE_INTEGER *) &counter);
+  if (sec == last_sec)
+    {
+      if (msec < last_msec)
+       {
+         msec = last_msec;
+         bt = true;
+       }
+    }
+  else if (sec < last_sec)
+    {
+      if (sec > last_sec - backtrack_hold_seconds)
+       {
+         sec = last_sec;
+         msec = last_msec;
+       }
+      bt = true;
     }
 
-  tv->tv_sec = (counter / frequency) + timeSecOffset;
-  tv->tv_usec = ((__int64) (((__int64) counter) / counterPerMicrosecond) % 1000000);
+  tv->tv_sec = last_sec = sec;
+  tv->tv_usec = (last_msec = msec) * 1000;
+
+  if (bt && !bt_last)
+    gettimeofday_calibrate ();
+  bt_last = bt;
 
   return 0;
 }
 
 #endif /* WIN32 */
+
+#ifdef TIME_TEST
+void
+time_test (void)
+{
+  struct timeval tv;
+  time_t t;
+  int i;
+  for (i = 0; i < 10000; ++i)
+    {
+      t = time(NULL);
+      gettimeofday (&tv, NULL);
+#if 1
+      msg (M_INFO, "t=%u s=%u us=%u",
+              (unsigned int)t,
+              (unsigned int)tv.tv_sec,
+              (unsigned int)tv.tv_usec);
+#endif
+    }
+}
+#endif
diff --git a/otime.h b/otime.h
index 6744c4d007cff021b27c04189c354972b86910f0..2dca3968812040e77717b1dae72322fe61a51412 100644 (file)
--- a/otime.h
+++ b/otime.h
@@ -56,44 +56,23 @@ const char *tv_string_abs (const struct timeval *tv, struct gc_arena *gc);
 
 extern time_t now; /* updated frequently to time(NULL) */
 
-#if TIME_BACKTRACK_PROTECTION
-
-void update_now (const time_t system_time);
+void time_test (void);
 
 static inline void
 update_time (void)
 {
-  update_now (time (NULL));
-}
-
-#ifdef HAVE_GETTIMEOFDAY
-
-extern time_t now_usec;
-void update_now_usec (struct timeval *tv);
-
-static inline int
-openvpn_gettimeofday (struct timeval *tv, void *tz)
-{
-  const int status = gettimeofday (tv, tz);
-  if (!status)
+#if defined(WIN32) && defined(HAVE_GETTIMEOFDAY)
+  struct timeval tv;
+  if (!gettimeofday (&tv, NULL))
     {
-      update_now_usec (tv);
-      tv->tv_sec = now;
-      tv->tv_usec = now_usec;
+      if (tv.tv_sec != now)
+       now = tv.tv_sec;
     }
-  return status;
-}
-
-#endif
-
 #else
-
-static inline void
-update_time (void)
-{
   const time_t real_time = time (NULL);
   if (real_time != now)
     now = real_time;
+#endif
 }
 
 #ifdef HAVE_GETTIMEOFDAY
@@ -106,8 +85,6 @@ openvpn_gettimeofday (struct timeval *tv, void *tz)
 
 #endif
 
-#endif
-
 static inline time_t
 openvpn_time (time_t *t)
 {
index f296fe018dfcee721e3424bdf5940604bd90cd32..78eeebd00dc68a430f964512e39609b3b356a861 100644 (file)
--- a/syshead.h
+++ b/syshead.h
@@ -498,7 +498,7 @@ socket_defined (const socket_descriptor_t sd)
  * Reduce sensitivity to system clock instability
  * and backtracks.
  */
-#define TIME_BACKTRACK_PROTECTION 1
+//#define TIME_BACKTRACK_PROTECTION 1 // JYFIXME
 
 /*
  * Is non-blocking connect() supported?