VirtualBox

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

Last change on this file since 11523 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1/* $Id: timesup.cpp 8245 2008-04-21 17:24:28Z vboxsync $ */
2/** @file
3 * IPRT - Time using SUPLib.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. 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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_TIME
36#include <iprt/time.h>
37#include <iprt/asm.h>
38#include <iprt/assert.h>
39#include <iprt/err.h>
40#include <iprt/log.h>
41#ifndef IN_GUEST
42# include <VBox/sup.h>
43# include <VBox/x86.h>
44#endif
45#include "internal/time.h"
46
47
48/*******************************************************************************
49* Internal Functions *
50*******************************************************************************/
51#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
52static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
53static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData);
54static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData);
55#endif
56
57
58/*******************************************************************************
59* Global Variables *
60*******************************************************************************/
61#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
62/** The previous timestamp value returned by RTTimeNanoTS. */
63static uint64_t g_TimeNanoTSPrev = 0;
64
65/** The RTTimeNanoTS data structure that's passed down to the worker functions. */
66static RTTIMENANOTSDATA g_TimeNanoTSData =
67{
68 /* .pu64Prev = */ &g_TimeNanoTSPrev,
69 /* .pfnBad = */ rtTimeNanoTSInternalBitch,
70 /* .pfnRediscover = */ rtTimeNanoTSInternalRediscover,
71 /* .pvDummy = */ NULL,
72 /* .c1nsSteps = */ 0,
73 /* .cExpired = */ 0,
74 /* .cBadPrev = */ 0,
75 /* .cUpdateRaces = */ 0
76};
77
78/** The index into g_apfnWorkers for the function to use.
79 * This cannot be a pointer because that'll break down in GC due to code relocation. */
80static uint32_t g_iWorker = 0;
81/** Array of rtTimeNanoTSInternal worker functions.
82 * This array is indexed by g_iWorker. */
83static const PFNTIMENANOTSINTERNAL g_apfnWorkers[] =
84{
85#define RTTIMENANO_WORKER_DETECT 0
86 rtTimeNanoTSInternalRediscover,
87#define RTTIMENANO_WORKER_SYNC_CPUID 1
88 RTTimeNanoTSLegacySync,
89#define RTTIMENANO_WORKER_ASYNC_CPUID 2
90 RTTimeNanoTSLegacyAsync,
91#define RTTIMENANO_WORKER_SYNC_LFENCE 3
92 RTTimeNanoTSLFenceSync,
93#define RTTIMENANO_WORKER_ASYNC_LFENCE 4
94 RTTimeNanoTSLFenceAsync,
95#define RTTIMENANO_WORKER_FALLBACK 5
96 rtTimeNanoTSInternalFallback,
97};
98
99
100/**
101 * Helper function that's used by the assembly routines when something goes bust.
102 *
103 * @param pData Pointer to the data structure.
104 * @param u64NanoTS The calculated nano ts.
105 * @param u64DeltaPrev The delta relative to the previously returned timestamp.
106 * @param u64PrevNanoTS The previously returned timestamp (as it was read it).
107 */
108static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS)
109{
110 pData->cBadPrev++;
111 if ((int64_t)u64DeltaPrev < 0)
112 LogRel(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64\n",
113 u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
114 else
115 Log(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 (debugging?)\n",
116 u64DeltaPrev, u64PrevNanoTS, u64NanoTS));
117}
118
119/**
120 * Fallback function.
121 */
122static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalFallback(PRTTIMENANOTSDATA pData)
123{
124 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
125 if ( pGip
126 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
127 && ( pGip->u32Mode == SUPGIPMODE_SYNC_TSC
128 || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
129 return rtTimeNanoTSInternalRediscover(pData);
130 NOREF(pData);
131#if defined(IN_RING3) /** @todo Add ring-0 RTTimeSystemNanoTS to all hosts. */
132 return RTTimeSystemNanoTS();
133#else
134 return 0;
135#endif
136}
137
138
139/**
140 * Called the first time somebody asks for the time or when the GIP
141 * is mapped/unmapped.
142 */
143static DECLCALLBACK(uint64_t) rtTimeNanoTSInternalRediscover(PRTTIMENANOTSDATA pData)
144{
145 uint32_t iWorker;
146 PSUPGLOBALINFOPAGE pGip = g_pSUPGlobalInfoPage;
147 if ( pGip
148 && pGip->u32Magic == SUPGLOBALINFOPAGE_MAGIC
149 && ( pGip->u32Mode == SUPGIPMODE_SYNC_TSC
150 || pGip->u32Mode == SUPGIPMODE_ASYNC_TSC))
151 {
152 if (ASMCpuId_EDX(1) & X86_CPUID_FEATURE_EDX_SSE2)
153 iWorker = pGip->u32Mode == SUPGIPMODE_SYNC_TSC
154 ? RTTIMENANO_WORKER_SYNC_LFENCE
155 : RTTIMENANO_WORKER_ASYNC_LFENCE;
156 else
157 iWorker = pGip->u32Mode == SUPGIPMODE_SYNC_TSC
158 ? RTTIMENANO_WORKER_SYNC_CPUID
159 : RTTIMENANO_WORKER_ASYNC_CPUID;
160 }
161 else
162 iWorker = RTTIMENANO_WORKER_FALLBACK;
163
164 ASMAtomicXchgU32((uint32_t volatile *)&g_iWorker, iWorker);
165 return g_apfnWorkers[iWorker](pData);
166}
167
168#endif /* !IN_GUEST && !RT_NO_GIP */
169
170
171/**
172 * Internal worker for getting the current nanosecond timestamp.
173 */
174DECLINLINE(uint64_t) rtTimeNanoTSInternal(void)
175{
176#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
177 return g_apfnWorkers[g_iWorker](&g_TimeNanoTSData);
178#else
179 return RTTimeSystemNanoTS();
180#endif
181}
182
183
184/**
185 * Gets the current nanosecond timestamp.
186 *
187 * @returns nanosecond timestamp.
188 */
189RTDECL(uint64_t) RTTimeNanoTS(void)
190{
191 return rtTimeNanoTSInternal();
192}
193
194
195/**
196 * Gets the current millisecond timestamp.
197 *
198 * @returns millisecond timestamp.
199 */
200RTDECL(uint64_t) RTTimeMilliTS(void)
201{
202 return rtTimeNanoTSInternal() / 1000000;
203}
204
205
206#if !defined(IN_GUEST) && !defined(RT_NO_GIP)
207/**
208 * Debugging the time api.
209 *
210 * @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
211 */
212RTDECL(uint32_t) RTTimeDbgSteps(void)
213{
214 return g_TimeNanoTSData.c1nsSteps;
215}
216
217
218/**
219 * Debugging the time api.
220 *
221 * @returns the number of times the TSC interval expired RTTimeNanoTS().
222 */
223RTDECL(uint32_t) RTTimeDbgExpired(void)
224{
225 return g_TimeNanoTSData.cExpired;
226}
227
228
229/**
230 * Debugging the time api.
231 *
232 * @returns the number of bad previous values encountered by RTTimeNanoTS().
233 */
234RTDECL(uint32_t) RTTimeDbgBad(void)
235{
236 return g_TimeNanoTSData.cBadPrev;
237}
238
239
240/**
241 * Debugging the time api.
242 *
243 * @returns the number of update races in RTTimeNanoTS().
244 */
245RTDECL(uint32_t) RTTimeDbgRaces(void)
246{
247 return g_TimeNanoTSData.cUpdateRaces;
248}
249#endif /* !IN_GUEST && !RT_NO_GIP */
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