]> git.openfabrics.org - ~shefty/rdma-win.git/commitdiff
[ETC] Provide a common implementation for gettimeofday().
authorstansmith <stansmith@ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86>
Thu, 9 Jul 2009 17:56:49 +0000 (17:56 +0000)
committerstansmith <stansmith@ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86>
Thu, 9 Jul 2009 17:56:49 +0000 (17:56 +0000)
git-svn-id: svn://openib.tc.cornell.edu/gen1@2280 ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86

trunk/etc/user/gtod.c [new file with mode: 0644]
trunk/inc/user/linux/sys/time.h [new file with mode: 0644]

diff --git a/trunk/etc/user/gtod.c b/trunk/etc/user/gtod.c
new file mode 100644 (file)
index 0000000..5207a26
--- /dev/null
@@ -0,0 +1,111 @@
+#ifndef _GTOD_C_\r
+#define _GTOD_C_\r
+\r
+/*\r
+ * int gettimeofday(struct timeval *ptv, void *ignored)\r
+ */\r
+\r
+#include <windows.h>\r
+#include <winsock2.h>\r
+\r
+\r
+static __inline\r
+void FileTimeToTimeval(LPFILETIME pft, struct timeval * ptv)\r
+{ /* Note that LONGLONG is a 64-bit value */\r
+       LONGLONG ll;\r
+\r
+       if(!pft || !ptv) {\r
+               ptv->tv_sec = 0;\r
+               ptv->tv_usec = 0;\r
+               return;\r
+       }\r
+\r
+       ll = ((LONGLONG) pft->dwHighDateTime << 32);\r
+       ll += (LONGLONG) pft->dwLowDateTime;\r
+       ll -= 116444736000000000;\r
+\r
+       ptv->tv_sec = (long) (ll / 10000000);\r
+       ptv->tv_usec = (long) (ll - ((LONGLONG)(ptv->tv_sec) * 10000000)) / 10;\r
+}\r
+\r
+\r
+// static __inline\r
+int gettimeofday(struct timeval *ptv, void *ignored)\r
+{\r
+       static int QueryCounter = 2;\r
+       FILETIME CurrentTime;\r
+       UNREFERENCED_PARAMETER(ignored);     \r
+\r
+       if(!ptv) return -1;\r
+\r
+       if(QueryCounter)\r
+       {\r
+               static LARGE_INTEGER Frequency;\r
+               static LARGE_INTEGER Offset; /* counter offset for right time*/\r
+               static LARGE_INTEGER LastCounter;\r
+               LARGE_INTEGER Time;\r
+               LARGE_INTEGER Counter;\r
+       \r
+               GetSystemTimeAsFileTime(&CurrentTime);\r
+               QueryPerformanceCounter(&Counter);\r
+       \r
+               if(QueryCounter == 2)\r
+               {\r
+                       QueryCounter = 1;\r
+                       if(!QueryPerformanceFrequency(&Frequency))\r
+                       {\r
+                               QueryCounter = 0;\r
+                               Frequency.QuadPart = 10000000; /* prevent division by 0 */\r
+                       }\r
+       \r
+                       /* get time as a large integer */\r
+                       Counter.HighPart &= 0x7FL; /* Clear high bits to prevent overflow */\r
+                       Offset.LowPart = CurrentTime.dwLowDateTime;\r
+                       Offset.HighPart = (LONG) CurrentTime.dwHighDateTime;\r
+                       Offset.QuadPart -= Counter.QuadPart * 10000000 / Frequency.QuadPart;\r
+               }\r
+       \r
+               /* Convert counter to a 100 nanoseconds resolution timer value. */\r
+       \r
+               Counter.HighPart &= 0x7FL; /* Clear high bits to prevent overflows */\r
+               Counter.QuadPart *= 10000000; /* need time stamp in 100 ns units */\r
+               Counter.QuadPart /= Frequency.QuadPart;/* counter of 0.1 microseconds */\r
+       \r
+               if(LastCounter.QuadPart > Counter.QuadPart)\r
+               { /* Counter value wrapped */\r
+                       Offset.QuadPart += (0x7F00000000*10000000) / Frequency.QuadPart;\r
+               }\r
+               LastCounter = Counter;\r
+       \r
+               /* Add the in previous call calculated offset */\r
+               Counter.QuadPart += Offset.QuadPart;\r
+       \r
+               /* get time as a large integer */\r
+               Time.LowPart = CurrentTime.dwLowDateTime;\r
+               Time.HighPart = (LONG) CurrentTime.dwHighDateTime;\r
+       \r
+               /* keep time difference within an interval of +- 0.1 seconds\r
+               relative to the time function by adjusting the counters offset */\r
+       \r
+               if( ((Time.QuadPart + 1000000) < Counter.QuadPart) ||\r
+                               ((Time.QuadPart - 1000000) > Counter.QuadPart) )\r
+               { /* Adjust the offset */\r
+                       Offset.QuadPart += Time.QuadPart - Counter.QuadPart;\r
+                       Counter.QuadPart = Time.QuadPart;\r
+               }\r
+       \r
+               /* use the adjusted performance counter time for the time stamp */\r
+               CurrentTime.dwLowDateTime = Counter.LowPart;\r
+               CurrentTime.dwHighDateTime = Counter.HighPart;\r
+       }\r
+       else\r
+       {\r
+               GetSystemTimeAsFileTime(&CurrentTime);\r
+       }\r
+\r
+       FileTimeToTimeval(&CurrentTime,ptv);\r
+\r
+       return(0);\r
+}\r
+\r
+#endif /* _GTOD_C_ */\r
diff --git a/trunk/inc/user/linux/sys/time.h b/trunk/inc/user/linux/sys/time.h
new file mode 100644 (file)
index 0000000..50ba78e
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef _SYS_TIME_H_\r
+#define _SYS_TIME_H_\r
+\r
+#include <winsock2.h>\r
+\r
+struct timezone {\r
+        int     tz_minuteswest; /* minutes west of Greenwich */\r
+        int     tz_dsttime;     /* type of dst correction */\r
+};\r
+\r
+\r
+extern int gettimeofday(struct timeval *ptv, void *ignored);\r
+\r
+#endif\r