VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/time/timesup.cpp@ 54215

Last change on this file since 54215 was 54215, checked in by vboxsync, 10 years ago

Runtime,TM: Use GIP's fTscDeltasAreRoughlyInSync.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.1 KB
Line 
1/* $Id: timesup.cpp 54215 2015-02-16 12:33:36Z vboxsync $ */
2/** @file
3 * IPRT - Time using SUPLib.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP RTLOGGROUP_TIME
32#include <iprt/time.h>
33#include "internal/iprt.h"
34
35#include <iprt/assert.h>
36#include <iprt/err.h>
37#include <iprt/log.h>
38#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
39# include <iprt/asm.h>
40# include <iprt/asm-amd64-x86.h>
41# include <iprt/x86.h>
42# include <VBox/sup.h>
43#endif
44#include "internal/time.h"
45
46
47/*******************************************************************************
48* Internal Functions *
49*******************************************************************************/
50#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
51static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
52static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData);
53static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData);
54#endif
55
56
57/*******************************************************************************
58* Global Variables *
59*******************************************************************************/
60#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
61/** The previous timestamp value returned by RTTimeNanoTS. */
62static uint64_t g_TimeNanoTSPrev = 0;
63
64/** The RTTimeNanoTS data structure that's passed down to the worker functions. */
65static RTTIMENANOTSDATA g_TimeNanoTSData =
66{
67 /* .pu64Prev = */ &g_TimeNanoTSPrev,
68 /* .pfnBad = */ rtTimeNanoTSInternalBitch,
69 /* .pfnRediscover = */ rtTimeNanoTSInternalRediscover,
70 /* .pvDummy = */ NULL,
71 /* .c1nsSteps = */ 0,
72 /* .cExpired = */ 0,
73 /* .cBadPrev = */ 0,
74 /* .cUpdateRaces = */ 0
75};
76
77/** The index into g_apfnWorkers for the function to use.
78 * This cannot be a pointer because that'll break down in GC due to code relocation. */
79static uint32_t g_iWorker = 0;
80/** Array of rtTimeNanoTSInternal worker functions.
81 * This array is indexed by g_iWorker. */
82static const PFNTIMENANOTSINTERNAL g_apfnWorkers[] =
83{
84# define RTTIMENANO_WORKER_DETECT 0
85 rtTimeNanoTSInternalRediscover,
86
87# define RTTIMENANO_WORKER_LEGACY_SYNC_NO_DELTA 1
88 RTTimeNanoTSLegacySyncNoDelta,
89# define RTTIMENANO_WORKER_LEGACY_SYNC_WITH_DELTA 2
90 RTTimeNanoTSLegacySyncWithDelta,
91# define RTTIMENANO_WORKER_LEGACY_ASYNC 3
92 RTTimeNanoTSLegacyAsync,
93# define RTTIMENANO_WORKER_LEGACY_INVAR_NO_DELTA 4
94 RTTimeNanoTSLFenceInvariantNoDelta,
95# define RTTIMENANO_WORKER_LEGACY_INVAR_WITH_DELTA 5
96 RTTimeNanoTSLFenceInvariantWithDelta,
97
98# define RTTIMENANO_WORKER_LFENCE_SYNC_NO_DELTA 6
99 RTTimeNanoTSLFenceSyncNoDelta,
100# define RTTIMENANO_WORKER_LFENCE_SYNC_WITH_DELTA 7
101 RTTimeNanoTSLFenceSyncWithDelta,
102# define RTTIMENANO_WORKER_LFENCE_ASYNC 8
103 RTTimeNanoTSLFenceAsync,
104# define RTTIMENANO_WORKER_LFENCE_INVAR_NO_DELTA 9
105 RTTimeNanoTSLFenceInvariantNoDelta,
106# define RTTIMENANO_WORKER_LFENCE_INVAR_WITH_DELTA 10
107 RTTimeNanoTSLFenceInvariantWithDelta,
108
109# define RTTIMENANO_WORKER_FALLBACK 11
110 rtTimeNanoTSInternalFallback,
111};
112
113
114/**
115 * Helper function that's used by the assembly routines when something goes bust.
116 *
117 * @param pData Pointer to the data structure.
118 * @param u64NanoTS The calculated nano ts.
119 * @param u64DeltaPrev The delta relative to the previously returned timestamp.
120 * @param u64PrevNanoTS The previously returned timestamp (as it was read it).
121 */
122static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS)
123{
124 pData->cBadPrev++;
125 if ((int64_t)u64DeltaPrev < 0)
126 LogRel(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64\n",
127 u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
128 else
129 Log(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 (debugging?)\n",
130 u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
131}
132
133/**
134 * Fallback function.
135 */
136static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData)
137{
138 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
139 if ( pGip
140 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
141 && ( pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
142 || pGip->u32Mode == SUPGIPMODE_SYNC_TSC
143 || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
144 return rtTimeNanoTSInternalRediscover(pData);
145 NOREF(pData);
146# ifndef IN_RC
147 return RTTimeSystemNanoTS();
148# else
149 return 0;
150# endif
151}
152
153
154/**
155 * Checks if we really need to apply the delta values.
156 *
157 * Getting the delta for a CPU is _very_ expensive, it more than doubles the
158 * execution time for RTTimeNanoTS.
159 *
160 * @returns true if deltas needs to be applied, false if not.
161 * @param pGip The GIP.
162 *
163 * @remarks If you change this, make sure to also change tmR3ReallyNeedDeltas().
164 */
165static bool rtTimeNanoTsInternalReallyNeedDeltas(PSUPGLOBALINFOPAGE pGip)
166{
167 return !pGip->fOsTscDeltasInSync && !pGip->fTscDeltasRoughlyInSync;
168#if 0
169 if (!pGip->fOsTscDeltasInSync)
170 {
171 uint32_t i = pGip->cCpus;
172 while (i-- > 0)
173 if ( pGip->aCPUs[i].enmState == SUPGIPCPUSTATE_ONLINE
174 && ( pGip->aCPUs[i].i64TSCDelta > 384
175 || pGip->aCPUs[i].i64TSCDelta < -384) )
176 return true;
177 }
178 return false;
179#endif
180}
181
182
183/**
184 * Called the first time somebody asks for the time or when the GIP
185 * is mapped/unmapped.
186 */
187static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData)
188{
189 uint32_t iWorker;
190 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
191 if ( pGip
192 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
193 && ( pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
194 || pGip->u32Mode == SUPGIPMODE_SYNC_TSC
195 || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
196 {
197 if (ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2)
198 iWorker = pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
199 ? rtTimeNanoTsInternalReallyNeedDeltas(pGip)
200 ? RTTIMENANO_WORKER_LFENCE_INVAR_WITH_DELTA : RTTIMENANO_WORKER_LFENCE_INVAR_NO_DELTA
201 : pGip->u32Mode == SUPGIPMODE_SYNC_TSC
202 ? false /** @todo !rtTimeNanoTsInternalReallyNeedDeltas(pGip) */
203 ? RTTIMENANO_WORKER_LFENCE_SYNC_WITH_DELTA : RTTIMENANO_WORKER_LFENCE_SYNC_NO_DELTA
204 : RTTIMENANO_WORKER_LFENCE_ASYNC;
205 else
206 iWorker = pGip->u32Mode == SUPGIPMODE_INVARIANT_TSC
207 ? rtTimeNanoTsInternalReallyNeedDeltas(pGip)
208 ? RTTIMENANO_WORKER_LEGACY_INVAR_WITH_DELTA : RTTIMENANO_WORKER_LEGACY_INVAR_NO_DELTA
209 : pGip->u32Mode == SUPGIPMODE_SYNC_TSC
210 ? false /** @todo rtTimeNanoTsInternalReallyNeedDeltas(pGip) */
211 ? RTTIMENANO_WORKER_LEGACY_SYNC_WITH_DELTA : RTTIMENANO_WORKER_LEGACY_SYNC_NO_DELTA
212 : RTTIMENANO_WORKER_LEGACY_ASYNC;
213 }
214 else
215 iWorker = RTTIMENANO_WORKER_FALLBACK;
216
217 ASMAtomicWriteU32((uint32_t volatile *)&g_iWorker, iWorker);
218 return g_apfnWorkers[iWorker](pData);
219}
220
221#endif /* !IN_GUEST && !RT_NO_GIP */
222
223
224/**
225 * Internal worker for getting the current nanosecond timestamp.
226 */
227DECLINLINE(uint64_t) rtTimeNanoTSInternal(void)
228{
229#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
230 return g_apfnWorkers[g_iWorker](&g_TimeNanoTSData);
231#else
232 return RTTimeSystemNanoTS();
233#endif
234}
235
236
237/**
238 * Gets the current nanosecond timestamp.
239 *
240 * @returns nanosecond timestamp.
241 */
242RTDECL(uint64_t) RTTimeNanoTS(void)
243{
244 return rtTimeNanoTSInternal();
245}
246RT_EXPORT_SYMBOL(RTTimeNanoTS);
247
248
249/**
250 * Gets the current millisecond timestamp.
251 *
252 * @returns millisecond timestamp.
253 */
254RTDECL(uint64_t) RTTimeMilliTS(void)
255{
256 return rtTimeNanoTSInternal() / 1000000;
257}
258RT_EXPORT_SYMBOL(RTTimeMilliTS);
259
260
261#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
262/**
263 * Debugging the time api.
264 *
265 * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
266 */
267RTDECL(uint32_t) RTTimeDbgSteps(void)
268{
269 return g_TimeNanoTSData.c1nsSteps;
270}
271RT_EXPORT_SYMBOL(RTTimeDbgSteps);
272
273
274/**
275 * Debugging the time api.
276 *
277 * @returns the number of times the TSC interval expired RTTimeNanoTS().
278 */
279RTDECL(uint32_t) RTTimeDbgExpired(void)
280{
281 return g_TimeNanoTSData.cExpired;
282}
283RT_EXPORT_SYMBOL(RTTimeDbgExpired);
284
285
286/**
287 * Debugging the time api.
288 *
289 * @returns the number of bad previous values encountered by RTTimeNanoTS().
290 */
291RTDECL(uint32_t) RTTimeDbgBad(void)
292{
293 return g_TimeNanoTSData.cBadPrev;
294}
295RT_EXPORT_SYMBOL(RTTimeDbgBad);
296
297
298/**
299 * Debugging the time api.
300 *
301 * @returns the number of update races in RTTimeNanoTS().
302 */
303RTDECL(uint32_t) RTTimeDbgRaces(void)
304{
305 return g_TimeNanoTSData.cUpdateRaces;
306}
307RT_EXPORT_SYMBOL(RTTimeDbgRaces);
308#endif /* !IN_GUEST && !RT_NO_GIP */
309
Note: See TracBrowser for help on using the repository browser.

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