[PATCH v2 5/8] kernel32: Implement GetSystemTimeAsFileTime() directly.

Huw Davies huw at codeweavers.com
Tue May 14 03:53:45 CDT 2019


On an i7-8700 CPU @ 3.20GHz with HZ=1000 it cuts the call cost from
~18ns to ~12ns.

Signed-off-by: Huw Davies <huw at codeweavers.com>
---
 dlls/kernel32/time.c | 37 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)

diff --git a/dlls/kernel32/time.c b/dlls/kernel32/time.c
index 41742894dd..6516bca972 100644
--- a/dlls/kernel32/time.c
+++ b/dlls/kernel32/time.c
@@ -59,6 +59,10 @@ WINE_DEFAULT_DEBUG_CHANNEL(time);
 #define FILETIME2LL( pft, ll) \
     ll = (((LONGLONG)((pft)->dwHighDateTime))<<32) + (pft)-> dwLowDateTime ;
 
+#define TICKSPERSEC        10000000
+#define SECS_1601_TO_1970  ((369 * 365 + 89) * (ULONGLONG)(60 * 60 * 24))
+#define TICKS_1601_TO_1970 (SECS_1601_TO_1970 * TICKSPERSEC)
+
 static const WCHAR mui_stdW[] = { 'M','U','I','_','S','t','d',0 };
 static const WCHAR mui_dltW[] = { 'M','U','I','_','D','l','t',0 };
 
@@ -764,7 +768,38 @@ void WINAPI GetSystemTimeAsFileTime( FILETIME *time )
 {
     LARGE_INTEGER t;
 
-    NtQuerySystemTime( &t );
+#ifdef HAVE_CLOCK_GETTIME
+    struct timespec ts;
+    static clockid_t clock_id = CLOCK_MONOTONIC; /* placeholder */
+
+    if (clock_id == CLOCK_MONOTONIC)
+    {
+#ifdef CLOCK_REALTIME_COARSE
+        struct timespec res;
+
+        /* Use CLOCK_REALTIME_COARSE if it has 1 ms or better resolution */
+        if (!clock_getres( CLOCK_REALTIME_COARSE, &res ) && res.tv_sec == 0 && res.tv_nsec <= 1000000)
+            clock_id = CLOCK_REALTIME_COARSE;
+        else
+#endif /* CLOCK_REALTIME_COARSE */
+            clock_id = CLOCK_REALTIME;
+    }
+
+    if (!clock_gettime( clock_id, &ts ))
+    {
+        t.QuadPart = ts.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
+        t.QuadPart += (ts.tv_nsec + 50) / 100;
+    }
+    else
+#endif /* HAVE_CLOCK_GETTIME */
+    {
+        struct timeval now;
+
+        gettimeofday( &now, 0 );
+        t.QuadPart = now.tv_sec * (ULONGLONG)TICKSPERSEC + TICKS_1601_TO_1970;
+        t.QuadPart += now.tv_usec * 10;
+    }
+
     time->dwLowDateTime = t.u.LowPart;
     time->dwHighDateTime = t.u.HighPart;
 }
-- 
2.17.1




More information about the wine-devel mailing list