[ovs-dev] [PATCH] timeval: Initialize 'unix_epoch' for Windows correctly.

Gurucharan Shetty shettyg at nicira.com
Thu Jun 26 14:00:16 UTC 2014


Till now, we were initializing 'unix_epoch' through time_init().
But if there was a call directly to xgettimeofday(), we would
miss the initialization causing overflows. This would cause
'ovs-ofctl benchmark' to produce wrong results.

This commit fixes it by having a separate initialization function
for 'unix_epoch' and calling it from functions that actually use it.

Reported-by: Alessandro Pilotti <apilotti at cloudbasesolutions.com>
Reported-by: Linda Sun <lsun at vmware.com>
Signed-off-by: Gurucharan Shetty <gshetty at nicira.com>
---
 AUTHORS       |    1 +
 lib/timeval.c |   33 +++++++++++++++++++++++----------
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/AUTHORS b/AUTHORS
index 64ede54..17a85d0 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -150,6 +150,7 @@ Ahmed Bilal             numan252 at gmail.com
 Alan Shieh              ashieh at nicira.com
 Alban Browaeys          prahal at yahoo.com
 Alex Yip                alex at nicira.com
+Alessandro Pilotti      apilotti at cloudbasesolutions.com
 Alexey I. Froloff       raorn at altlinux.org
 Amar Padmanabhan        amar at nicira.com
 Amey Bhide              abhide at nicira.com
diff --git a/lib/timeval.c b/lib/timeval.c
index fec3cfa..25e903e 100644
--- a/lib/timeval.c
+++ b/lib/timeval.c
@@ -123,16 +123,6 @@ do_init_time(void)
 {
     struct timespec ts;
 
-#ifdef _WIN32
-    /* Calculate number of 100-nanosecond intervals till 01/01/1970. */
-    SYSTEMTIME unix_epoch_st = { 1970, 1, 0, 1, 0, 0, 0, 0};
-    FILETIME unix_epoch_ft;
-
-    SystemTimeToFileTime(&unix_epoch_st, &unix_epoch_ft);
-    unix_epoch.LowPart = unix_epoch_ft.dwLowDateTime;
-    unix_epoch.HighPart = unix_epoch_ft.dwHighDateTime;
-#endif
-
     coverage_init();
 
     init_clock(&monotonic_clock, (!clock_gettime(CLOCK_MONOTONIC, &ts)
@@ -377,6 +367,25 @@ time_boot_msec(void)
 }
 
 #ifdef _WIN32
+static void
+time_unix_epoch__(void)
+{
+    /* Calculate number of 100-nanosecond intervals till 01/01/1970. */
+    SYSTEMTIME unix_epoch_st = { 1970, 1, 0, 1, 0, 0, 0, 0};
+    FILETIME unix_epoch_ft;
+
+    SystemTimeToFileTime(&unix_epoch_st, &unix_epoch_ft);
+    unix_epoch.LowPart = unix_epoch_ft.dwLowDateTime;
+    unix_epoch.HighPart = unix_epoch_ft.dwHighDateTime;
+}
+
+static void
+time_unix_epoch(void)
+{
+    static pthread_once_t once = PTHREAD_ONCE_INIT;
+    pthread_once(&once, time_unix_epoch__);
+}
+
 static ULARGE_INTEGER
 xgetfiletime(void)
 {
@@ -415,6 +424,8 @@ clock_gettime(clock_t id, struct timespec *ts)
     } else if (id == CLOCK_REALTIME) {
         ULARGE_INTEGER current_time = xgetfiletime();
 
+        time_unix_epoch();
+
         /* Time from Epoch to now. */
         ts->tv_sec = (current_time.QuadPart - unix_epoch.QuadPart) / 10000000;
         ts->tv_nsec = ((current_time.QuadPart - unix_epoch.QuadPart) %
@@ -435,6 +446,8 @@ xgettimeofday(struct timeval *tv)
 #else
     ULARGE_INTEGER current_time = xgetfiletime();
 
+    time_unix_epoch();
+
     tv->tv_sec = (current_time.QuadPart - unix_epoch.QuadPart) / 10000000;
     tv->tv_usec = ((current_time.QuadPart - unix_epoch.QuadPart) %
                    10000000) / 10;
-- 
1.7.9.5




More information about the dev mailing list