timesup.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
/* $Id$ */
/** @file
* IPRT - Time using SUPLib.
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_TIME
#ifndef IN_GUEST
#endif
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS);
#endif
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The previous timestamp value returned by RTTimeNanoTS. */
static uint64_t g_TimeNanoTSPrev = 0;
/** The RTTimeNanoTS data structure that's passed down to the worker functions. */
static RTTIMENANOTSDATA g_TimeNanoTSData =
{
/* .pu64Prev = */ &g_TimeNanoTSPrev,
/* .pfnBad = */ rtTimeNanoTSInternalBitch,
/* .pfnRediscover = */ rtTimeNanoTSInternalRediscover,
/* .pvDummy = */ NULL,
/* .c1nsSteps = */ 0,
/* .cExpired = */ 0,
/* .cBadPrev = */ 0,
/* .cUpdateRaces = */ 0
};
/** The index into g_apfnWorkers for the function to use.
* This cannot be a pointer because that'll break down in GC due to code relocation. */
/** Array of rtTimeNanoTSInternal worker functions.
* This array is indexed by g_iWorker. */
static const PFNTIMENANOTSINTERNAL g_apfnWorkers[] =
{
#define RTTIMENANO_WORKER_DETECT 0
#define RTTIMENANO_WORKER_SYNC_CPUID 1
#define RTTIMENANO_WORKER_ASYNC_CPUID 2
#define RTTIMENANO_WORKER_SYNC_LFENCE 3
#define RTTIMENANO_WORKER_ASYNC_LFENCE 4
#define RTTIMENANO_WORKER_FALLBACK 5
};
/**
* Helper function that's used by the assembly routines when something goes bust.
*
* @param pData Pointer to the data structure.
* @param u64NanoTS The calculated nano ts.
* @param u64DeltaPrev The delta relative to the previously returned timestamp.
* @param u64PrevNanoTS The previously returned timestamp (as it was read it).
*/
static DECLCALLBACK(void) rtTimeNanoTSInternalBitch(PRTTIMENANOTSDATA pData, uint64_t u64NanoTS, uint64_t u64DeltaPrev, uint64_t u64PrevNanoTS)
{
if ((int64_t)u64DeltaPrev < 0)
LogRel(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64\n",
else
Log(("TM: u64DeltaPrev=%RI64 u64PrevNanoTS=0x%016RX64 u64NanoTS=0x%016RX64 (debugging?)\n",
}
/**
* Fallback function.
*/
{
if ( pGip
return rtTimeNanoTSInternalRediscover(pData);
#if defined(IN_RING3) /** @todo Add ring-0 RTTimeSystemNanoTS to all hosts. */
return RTTimeSystemNanoTS();
#else
return 0;
#endif
}
/**
* Called the first time somebody asks for the time or when the GIP
*/
{
if ( pGip
{
else
}
else
}
#endif /* !IN_GUEST && !RT_NO_GIP */
/**
* Internal worker for getting the current nanosecond timestamp.
*/
{
#else
return RTTimeSystemNanoTS();
#endif
}
/**
* Gets the current nanosecond timestamp.
*
* @returns nanosecond timestamp.
*/
{
return rtTimeNanoTSInternal();
}
/**
* Gets the current millisecond timestamp.
*
* @returns millisecond timestamp.
*/
{
return rtTimeNanoTSInternal() / 1000000;
}
/**
* Debugging the time api.
*
* @returns the number of 1ns steps which has been applied by RTTimeNanoTS().
*/
{
return g_TimeNanoTSData.c1nsSteps;
}
/**
* Debugging the time api.
*
* @returns the number of times the TSC interval expired RTTimeNanoTS().
*/
{
return g_TimeNanoTSData.cExpired;
}
/**
* Debugging the time api.
*
* @returns the number of bad previous values encountered by RTTimeNanoTS().
*/
{
return g_TimeNanoTSData.cBadPrev;
}
/**
* Debugging the time api.
*
* @returns the number of update races in RTTimeNanoTS().
*/
{
return g_TimeNanoTSData.cUpdateRaces;
}
#endif /* !IN_GUEST && !RT_NO_GIP */