[1] | 1 | /* $Id: time-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[26212] | 3 | * IPRT - Time, Windows.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #define LOG_GROUP RTLOGGROUP_TIME
|
---|
[70405] | 42 | #include <iprt/nt/nt-and-windows.h>
|
---|
[1] | 43 |
|
---|
| 44 | #include <iprt/time.h>
|
---|
[26212] | 45 | #include "internal/iprt.h"
|
---|
| 46 |
|
---|
[1] | 47 | #include <iprt/asm.h>
|
---|
[2612] | 48 | #include <iprt/assert.h>
|
---|
[76452] | 49 | #include <iprt/errcore.h>
|
---|
[1] | 50 | #include "internal/time.h"
|
---|
[70402] | 51 | #include "internal-r3-win.h"
|
---|
[1] | 52 |
|
---|
[46223] | 53 | /*
|
---|
| 54 | * Note! The selected time source be the exact same one as we use in kernel land!
|
---|
| 55 | */
|
---|
[46231] | 56 | //#define USE_TICK_COUNT
|
---|
[6267] | 57 | //#define USE_PERFORMANCE_COUNTER
|
---|
| 58 | //# define USE_FILE_TIME
|
---|
[46223] | 59 | //#if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
|
---|
[46231] | 60 | # define USE_INTERRUPT_TIME
|
---|
[46223] | 61 | //#else
|
---|
| 62 | //# define USE_TICK_COUNT
|
---|
| 63 | //#endif
|
---|
[1] | 64 |
|
---|
[6267] | 65 |
|
---|
| 66 |
|
---|
[1] | 67 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
| 68 | {
|
---|
| 69 | #if defined USE_TICK_COUNT
|
---|
[20736] | 70 | /*
|
---|
| 71 | * This would work if it didn't flip over every 49 (or so) days.
|
---|
[6267] | 72 | */
|
---|
[52822] | 73 | return (uint64_t)GetTickCount() * RT_NS_1MS_64;
|
---|
[1] | 74 |
|
---|
| 75 | #elif defined USE_PERFORMANCE_COUNTER
|
---|
[20736] | 76 | /*
|
---|
| 77 | * Slow and not derived from InterruptTime.
|
---|
[6267] | 78 | */
|
---|
[1] | 79 | static LARGE_INTEGER llFreq;
|
---|
| 80 | static unsigned uMult;
|
---|
| 81 | if (!llFreq.QuadPart)
|
---|
| 82 | {
|
---|
| 83 | if (!QueryPerformanceFrequency(&llFreq))
|
---|
[52822] | 84 | return (uint64_t)GetTickCount() * RT_NS_1MS_64;
|
---|
[1] | 85 | llFreq.QuadPart /= 1000;
|
---|
[6267] | 86 | uMult = 1000000; /* no math genius, but this seemed to help avoiding floating point. */
|
---|
[1] | 87 | }
|
---|
| 88 |
|
---|
| 89 | LARGE_INTEGER ll;
|
---|
| 90 | if (QueryPerformanceCounter(&ll))
|
---|
| 91 | return (ll.QuadPart * uMult) / llFreq.QuadPart;
|
---|
[52822] | 92 | return (uint64_t)GetTickCount() * RT_NS_1MS_64;
|
---|
[1] | 93 |
|
---|
| 94 | #elif defined USE_FILE_TIME
|
---|
[20736] | 95 | /*
|
---|
| 96 | * This is SystemTime not InterruptTime.
|
---|
[6267] | 97 | */
|
---|
[1] | 98 | uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
|
---|
| 99 | GetSystemTimeAsFileTime((LPFILETIME)&u64);
|
---|
| 100 | return u64 * 100;
|
---|
| 101 |
|
---|
| 102 | #elif defined USE_INTERRUPT_TIME
|
---|
[20736] | 103 | /*
|
---|
[70402] | 104 | * Use interrupt time if we can (not possible on NT 3.1).
|
---|
[70405] | 105 | * Note! We cannot entirely depend on g_enmWinVer here as we're likely to
|
---|
| 106 | * get called before IPRT is initialized. Ditto g_hModNtDll.
|
---|
[6267] | 107 | */
|
---|
[70405] | 108 | static PFNRTLGETINTERRUPTTIMEPRECISE s_pfnRtlGetInterruptTimePrecise = NULL;
|
---|
| 109 | static int volatile s_iCanUseUserSharedData = -1;
|
---|
| 110 | int iCanUseUserSharedData = s_iCanUseUserSharedData;
|
---|
| 111 | if (iCanUseUserSharedData != -1)
|
---|
[70402] | 112 | { /* likely */ }
|
---|
| 113 | else
|
---|
[1] | 114 | {
|
---|
[70402] | 115 | /* We may be called before g_enmWinVer has been initialized. */
|
---|
| 116 | if (g_enmWinVer != kRTWinOSType_UNKNOWN)
|
---|
[70405] | 117 | iCanUseUserSharedData = g_enmWinVer > kRTWinOSType_NT310;
|
---|
[70402] | 118 | else
|
---|
| 119 | {
|
---|
| 120 | DWORD dwVer = GetVersion();
|
---|
[70405] | 121 | iCanUseUserSharedData = (dwVer & 0xff) != 3 || ((dwVer >> 8) & 0xff) >= 50;
|
---|
[70402] | 122 | }
|
---|
[70405] | 123 | if (iCanUseUserSharedData != 0)
|
---|
| 124 | {
|
---|
| 125 | FARPROC pfn = GetProcAddress(g_hModNtDll ? g_hModNtDll : GetModuleHandleW(L"ntdll"), "RtlGetInterruptTimePrecise");
|
---|
| 126 | if (pfn != NULL)
|
---|
| 127 | {
|
---|
| 128 | ASMAtomicWritePtr(&s_pfnRtlGetInterruptTimePrecise, pfn);
|
---|
| 129 | iCanUseUserSharedData = 42;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | s_iCanUseUserSharedData = iCanUseUserSharedData;
|
---|
[1] | 133 | }
|
---|
| 134 |
|
---|
[70405] | 135 | if (iCanUseUserSharedData != 0)
|
---|
[1] | 136 | {
|
---|
[70402] | 137 | LARGE_INTEGER Time;
|
---|
[70405] | 138 | if (iCanUseUserSharedData == 42)
|
---|
[70402] | 139 | {
|
---|
[70405] | 140 | uint64_t iIgnored;
|
---|
| 141 | Time.QuadPart = s_pfnRtlGetInterruptTimePrecise(&iIgnored);
|
---|
| 142 | }
|
---|
| 143 | else
|
---|
| 144 | {
|
---|
| 145 | PKUSER_SHARED_DATA pUserSharedData = (PKUSER_SHARED_DATA)MM_SHARED_USER_DATA_VA;
|
---|
| 146 | do
|
---|
| 147 | {
|
---|
| 148 | Time.HighPart = pUserSharedData->InterruptTime.High1Time;
|
---|
| 149 | Time.LowPart = pUserSharedData->InterruptTime.LowPart;
|
---|
| 150 | } while (pUserSharedData->InterruptTime.High2Time != Time.HighPart);
|
---|
| 151 | }
|
---|
[1] | 152 |
|
---|
[70402] | 153 | return (uint64_t)Time.QuadPart * 100;
|
---|
| 154 | }
|
---|
[1] | 155 |
|
---|
[70402] | 156 | return (uint64_t)GetTickCount() * RT_NS_1MS_64;
|
---|
| 157 |
|
---|
[1] | 158 | #else
|
---|
| 159 | # error "Must select a method bright guy!"
|
---|
| 160 | #endif
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
| 165 | {
|
---|
| 166 | return rtTimeGetSystemNanoTS();
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 |
|
---|
| 170 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
| 171 | {
|
---|
[52822] | 172 | return rtTimeGetSystemNanoTS() / RT_NS_1MS;
|
---|
[1] | 173 | }
|
---|
| 174 |
|
---|
| 175 |
|
---|
[2612] | 176 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
|
---|
[1] | 177 | {
|
---|
[2612] | 178 | uint64_t u64;
|
---|
| 179 | AssertCompile(sizeof(u64) == sizeof(FILETIME));
|
---|
[96476] | 180 | if (g_pfnGetSystemTimeAsFileTime)
|
---|
| 181 | g_pfnGetSystemTimeAsFileTime((LPFILETIME)&u64);
|
---|
| 182 | else
|
---|
| 183 | {
|
---|
| 184 | SYSTEMTIME SysTime = {0};
|
---|
| 185 | GetSystemTime(&SysTime);
|
---|
| 186 | BOOL fRet = SystemTimeToFileTime(&SysTime, (LPFILETIME)&u64);
|
---|
| 187 | Assert(fRet); RT_NOREF(fRet);
|
---|
| 188 | }
|
---|
[1] | 189 | return RTTimeSpecSetNtTime(pTime, u64);
|
---|
[96476] | 190 |
|
---|
[1] | 191 | }
|
---|
| 192 |
|
---|
[2612] | 193 |
|
---|
| 194 | RTDECL(PRTTIMESPEC) RTTimeLocalNow(PRTTIMESPEC pTime)
|
---|
| 195 | {
|
---|
| 196 | uint64_t u64Local;
|
---|
[96476] | 197 | if (g_pfnGetSystemTimeAsFileTime)
|
---|
| 198 | {
|
---|
| 199 | uint64_t u64;
|
---|
| 200 | AssertCompile(sizeof(u64) == sizeof(FILETIME));
|
---|
| 201 | g_pfnGetSystemTimeAsFileTime((LPFILETIME)&u64);
|
---|
| 202 | if (!FileTimeToLocalFileTime((FILETIME const *)&u64, (LPFILETIME)&u64Local))
|
---|
| 203 | u64Local = u64;
|
---|
| 204 | }
|
---|
| 205 | else
|
---|
| 206 | {
|
---|
| 207 | SYSTEMTIME SysTime = {0};
|
---|
| 208 | GetLocalTime(&SysTime);
|
---|
| 209 | BOOL fRet = SystemTimeToFileTime(&SysTime, (LPFILETIME)&u64Local);
|
---|
| 210 | Assert(fRet); RT_NOREF(fRet);
|
---|
| 211 | }
|
---|
[2629] | 212 | return RTTimeSpecSetNtTime(pTime, u64Local);
|
---|
[2612] | 213 | }
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 | RTDECL(int64_t) RTTimeLocalDeltaNano(void)
|
---|
| 217 | {
|
---|
| 218 | /*
|
---|
[2629] | 219 | * UTC = local + Tzi.Bias;
|
---|
[2612] | 220 | * The bias is given in minutes.
|
---|
| 221 | */
|
---|
| 222 | TIME_ZONE_INFORMATION Tzi;
|
---|
| 223 | Tzi.Bias = 0;
|
---|
| 224 | if (GetTimeZoneInformation(&Tzi) != TIME_ZONE_ID_INVALID)
|
---|
[52823] | 225 | return -(int64_t)Tzi.Bias * 60 * RT_NS_1SEC_64;
|
---|
[2612] | 226 | return 0;
|
---|
| 227 | }
|
---|
| 228 |
|
---|