VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/time-win.cpp@ 103795

Last change on this file since 103795 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.0 KB
Line 
1/* $Id: time-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Time, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
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 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
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
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.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_TIME
42#include <iprt/nt/nt-and-windows.h>
43
44#include <iprt/time.h>
45#include "internal/iprt.h"
46
47#include <iprt/asm.h>
48#include <iprt/assert.h>
49#include <iprt/errcore.h>
50#include "internal/time.h"
51#include "internal-r3-win.h"
52
53/*
54 * Note! The selected time source be the exact same one as we use in kernel land!
55 */
56//#define USE_TICK_COUNT
57//#define USE_PERFORMANCE_COUNTER
58//# define USE_FILE_TIME
59//#if defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
60# define USE_INTERRUPT_TIME
61//#else
62//# define USE_TICK_COUNT
63//#endif
64
65
66
67DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
68{
69#if defined USE_TICK_COUNT
70 /*
71 * This would work if it didn't flip over every 49 (or so) days.
72 */
73 return (uint64_t)GetTickCount() * RT_NS_1MS_64;
74
75#elif defined USE_PERFORMANCE_COUNTER
76 /*
77 * Slow and not derived from InterruptTime.
78 */
79 static LARGE_INTEGER llFreq;
80 static unsigned uMult;
81 if (!llFreq.QuadPart)
82 {
83 if (!QueryPerformanceFrequency(&llFreq))
84 return (uint64_t)GetTickCount() * RT_NS_1MS_64;
85 llFreq.QuadPart /= 1000;
86 uMult = 1000000; /* no math genius, but this seemed to help avoiding floating point. */
87 }
88
89 LARGE_INTEGER ll;
90 if (QueryPerformanceCounter(&ll))
91 return (ll.QuadPart * uMult) / llFreq.QuadPart;
92 return (uint64_t)GetTickCount() * RT_NS_1MS_64;
93
94#elif defined USE_FILE_TIME
95 /*
96 * This is SystemTime not InterruptTime.
97 */
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
103 /*
104 * Use interrupt time if we can (not possible on NT 3.1).
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.
107 */
108 static PFNRTLGETINTERRUPTTIMEPRECISE s_pfnRtlGetInterruptTimePrecise = NULL;
109 static int volatile s_iCanUseUserSharedData = -1;
110 int iCanUseUserSharedData = s_iCanUseUserSharedData;
111 if (iCanUseUserSharedData != -1)
112 { /* likely */ }
113 else
114 {
115 /* We may be called before g_enmWinVer has been initialized. */
116 if (g_enmWinVer != kRTWinOSType_UNKNOWN)
117 iCanUseUserSharedData = g_enmWinVer > kRTWinOSType_NT310;
118 else
119 {
120 DWORD dwVer = GetVersion();
121 iCanUseUserSharedData = (dwVer & 0xff) != 3 || ((dwVer >> 8) & 0xff) >= 50;
122 }
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;
133 }
134
135 if (iCanUseUserSharedData != 0)
136 {
137 LARGE_INTEGER Time;
138 if (iCanUseUserSharedData == 42)
139 {
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 }
152
153 return (uint64_t)Time.QuadPart * 100;
154 }
155
156 return (uint64_t)GetTickCount() * RT_NS_1MS_64;
157
158#else
159# error "Must select a method bright guy!"
160#endif
161}
162
163
164RTDECL(uint64_t) RTTimeSystemNanoTS(void)
165{
166 return rtTimeGetSystemNanoTS();
167}
168
169
170RTDECL(uint64_t) RTTimeSystemMilliTS(void)
171{
172 return rtTimeGetSystemNanoTS() / RT_NS_1MS;
173}
174
175
176RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
177{
178 uint64_t u64;
179 AssertCompile(sizeof(u64) == sizeof(FILETIME));
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 }
189 return RTTimeSpecSetNtTime(pTime, u64);
190
191}
192
193
194RTDECL(PRTTIMESPEC) RTTimeLocalNow(PRTTIMESPEC pTime)
195{
196 uint64_t u64Local;
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 }
212 return RTTimeSpecSetNtTime(pTime, u64Local);
213}
214
215
216RTDECL(int64_t) RTTimeLocalDeltaNano(void)
217{
218 /*
219 * UTC = local + Tzi.Bias;
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)
225 return -(int64_t)Tzi.Bias * 60 * RT_NS_1SEC_64;
226 return 0;
227}
228
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette