VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/nt/nttimesources.cpp@ 106815

Last change on this file since 106815 was 106650, checked in by vboxsync, 4 months ago

ValKit: win.arm64 build fixes. jiraref:VBP-1253

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
1/* $Id: nttimesources.cpp 106650 2024-10-24 09:32:31Z vboxsync $ */
2/** @file
3 * Check the various time sources on Windows NT.
4 */
5
6/*
7 * Copyright (C) 2009-2024 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#include <iprt/win/windows.h>
42
43#include <iprt/asm.h>
44#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
45# include <iprt/asm-amd64-x86.h>
46#else
47# include <iprt/asm-arm.h>
48#endif
49#include <iprt/errcore.h>
50#include <iprt/string.h>
51#include <iprt/test.h>
52
53
54/*********************************************************************************************************************************
55* Structures and Typedefs *
56*********************************************************************************************************************************/
57typedef struct _MY_KSYSTEM_TIME
58{
59 ULONG LowPart;
60 LONG High1Time;
61 LONG High2Time;
62} MY_KSYSTEM_TIME;
63
64typedef struct _MY_KUSER_SHARED_DATA
65{
66 ULONG TickCountLowDeprecated;
67 ULONG TickCountMultiplier;
68 volatile MY_KSYSTEM_TIME InterruptTime;
69 volatile MY_KSYSTEM_TIME SystemTime;
70 volatile MY_KSYSTEM_TIME TimeZoneBias;
71 /* The rest is not relevant. */
72} MY_KUSER_SHARED_DATA;
73
74/** The fixed pointer to the user shared data. */
75#define MY_USER_SHARED_DATA ((MY_KUSER_SHARED_DATA *)0x7ffe0000)
76
77/** Spins until GetTickCount() changes. */
78static void SpinUntilTick(void)
79{
80 /* spin till GetTickCount changes. */
81 DWORD dwMsTick = GetTickCount();
82 while (GetTickCount() == dwMsTick)
83 /* nothing */;
84}
85
86/** Delay function that tries to return right after GetTickCount changed. */
87static void DelayMillies(DWORD dwMsStart, DWORD cMillies)
88{
89 /* Delay cMillies - 1. */
90 Sleep(cMillies - 1);
91 while (GetTickCount() - dwMsStart < cMillies - 1U)
92 Sleep(1);
93
94 SpinUntilTick();
95}
96
97
98int main(int argc, char **argv)
99{
100 RT_NOREF1(argv);
101
102 /*
103 * Init, create a test instance and "parse" arguments.
104 */
105 RTTEST hTest;
106 int rc = RTTestInitAndCreate("nttimesources", &hTest);
107 if (rc)
108 return rc;
109 if (argc > 1)
110 {
111 RTTestFailed(hTest, "Syntax error! no arguments expected");
112 return RTTestSummaryAndDestroy(hTest);
113 }
114
115 /*
116 * Guess MHz using GetTickCount.
117 */
118 RTTestSub(hTest, "Guess MHz");
119 DWORD dwTickStart, dwTickEnd, cMsTicks;
120 uint64_t u64TscStart, u64TscEnd, cTscTicks;
121
122 /* get a good start time. */
123 SpinUntilTick();
124 do
125 {
126 dwTickStart = GetTickCount();
127 ASMCompilerBarrier();
128 ASMSerializeInstruction();
129 u64TscStart = ASMReadTSC();
130 ASMCompilerBarrier();
131 } while (GetTickCount() != dwTickStart);
132
133 /* delay a good while. */
134 DelayMillies(dwTickStart, 256);
135
136 /* get a good end time. */
137 do
138 {
139 dwTickEnd = GetTickCount();
140 ASMCompilerBarrier();
141 ASMSerializeInstruction();
142 u64TscEnd = ASMReadTSC();
143 ASMCompilerBarrier();
144 } while (GetTickCount() != dwTickEnd);
145 cMsTicks = dwTickEnd - dwTickStart;
146 cTscTicks = u64TscEnd - u64TscStart;
147
148 /* Calc an approximate TSC frequency:
149 cTscTicks / uTscHz = cMsTicks / 1000
150 1 / uTscHz = (cMsTicks / 1000) / cTscTicks
151 uTscHz = cTscTicks / (cMsTicks / 1000) */
152 uint64_t u64TscHz = (long double)cTscTicks / ((long double)cMsTicks / 1000.0);
153 if ( u64TscHz > _1M*3
154 && u64TscHz < _1T)
155 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "u64TscHz=%'llu", u64TscHz);
156 else
157 {
158 RTTestFailed(hTest, "u64TscHz=%'llu - out of range", u64TscHz);
159 u64TscHz = 0;
160 }
161
162
163 /*
164 * Pit GetTickCount, InterruptTime, Performance Counters and TSC against each other.
165 */
166 LARGE_INTEGER PrfHz;
167 LARGE_INTEGER PrfStart, PrfEnd, cPrfTicks;
168 LARGE_INTEGER IntStart, IntEnd, cIntTicks;
169 for (uint32_t i = 0; i < 7; i++)
170 {
171 RTTestSubF(hTest, "The whole bunch - pass #%u", i + 1);
172
173 if (!QueryPerformanceFrequency(&PrfHz))
174 {
175 RTTestFailed(hTest, "QueryPerformanceFrequency failed (%u)", GetLastError());
176 return RTTestSummaryAndDestroy(hTest);
177 }
178
179 /* get a good start time. */
180 SpinUntilTick();
181 do
182 {
183 IntStart.HighPart = MY_USER_SHARED_DATA->InterruptTime.High1Time;
184 IntStart.LowPart = MY_USER_SHARED_DATA->InterruptTime.LowPart;
185 dwTickStart = GetTickCount();
186 if (!QueryPerformanceCounter(&PrfStart))
187 {
188 RTTestFailed(hTest, "QueryPerformanceCounter failed (%u)", GetLastError());
189 return RTTestSummaryAndDestroy(hTest);
190 }
191 ASMCompilerBarrier();
192 ASMSerializeInstruction();
193 u64TscStart = ASMReadTSC();
194 ASMCompilerBarrier();
195 } while ( MY_USER_SHARED_DATA->InterruptTime.High2Time != IntStart.HighPart
196 || MY_USER_SHARED_DATA->InterruptTime.LowPart != IntStart.LowPart
197 || GetTickCount() != dwTickStart);
198
199 /* delay a good while. */
200 DelayMillies(dwTickStart, 256);
201
202 /* get a good end time. */
203 do
204 {
205 IntEnd.HighPart = MY_USER_SHARED_DATA->InterruptTime.High1Time;
206 IntEnd.LowPart = MY_USER_SHARED_DATA->InterruptTime.LowPart;
207 dwTickEnd = GetTickCount();
208 if (!QueryPerformanceCounter(&PrfEnd))
209 {
210 RTTestFailed(hTest, "QueryPerformanceCounter failed (%u)", GetLastError());
211 return RTTestSummaryAndDestroy(hTest);
212 }
213 ASMCompilerBarrier();
214 ASMSerializeInstruction();
215 u64TscEnd = ASMReadTSC();
216 ASMCompilerBarrier();
217 } while ( MY_USER_SHARED_DATA->InterruptTime.High2Time != IntEnd.HighPart
218 || MY_USER_SHARED_DATA->InterruptTime.LowPart != IntEnd.LowPart
219 || GetTickCount() != dwTickEnd);
220
221 cMsTicks = dwTickEnd - dwTickStart;
222 cTscTicks = u64TscEnd - u64TscStart;
223 cIntTicks.QuadPart = IntEnd.QuadPart - IntStart.QuadPart;
224 cPrfTicks.QuadPart = PrfEnd.QuadPart - PrfStart.QuadPart;
225
226 /* Recalc to micro seconds. */
227 uint64_t u64MicroSecMs = (uint64_t)cMsTicks * 1000;
228 uint64_t u64MicroSecTsc = u64TscHz ? (long double)cTscTicks / (long double)u64TscHz * 1000000 : u64MicroSecMs;
229 uint64_t u64MicroSecPrf = (long double)cPrfTicks.QuadPart / (long double)PrfHz.QuadPart * 1000000;
230 uint64_t u64MicroSecInt = cIntTicks.QuadPart / 10; /* 100ns units*/
231
232 /* check how much they differ using the millisecond tick count as the standard candle. */
233 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, " %9llu / %7lld us - GetTickCount\n", u64MicroSecMs, 0);
234
235 int64_t off = u64MicroSecTsc - u64MicroSecMs;
236 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, " %9llu / %7lld us - TSC\n", u64MicroSecTsc, off);
237 RTTEST_CHECK(hTest, RT_ABS(off) < 50000 /*us*/); /* some extra uncertainty with TSC. */
238
239 off = u64MicroSecInt - u64MicroSecMs;
240 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, " %9llu / %7lld us - InterruptTime\n", u64MicroSecInt, off);
241 RTTEST_CHECK(hTest, RT_ABS(off) < 25000 /*us*/);
242
243 off = u64MicroSecPrf - u64MicroSecMs;
244 RTTestPrintf(hTest, RTTESTLVL_ALWAYS, " %9llu / %7lld us - QueryPerformanceCounter\n", u64MicroSecPrf, off);
245 RTTEST_CHECK(hTest, RT_ABS(off) < 25000 /*us*/);
246 }
247
248 return RTTestSummaryAndDestroy(hTest);
249}
250
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