VirtualBox

Changeset 70405 in vbox


Ignore:
Timestamp:
Jan 1, 2018 5:45:14 PM (7 years ago)
Author:
vboxsync
Message:

IPRT/time-win.cpp: Use RtlGetInterruptTimePrecise for RTTimeSystemNanoTS when available (based on RDTSC) and using KUSER_SHARED_DATA from nt.h instead of duplicating it and its address.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/nt/nt.h

    r70347 r70405  
    677677    ULONG                   LangGenerationCount;                        /**< 0x3a4 */
    678678    ULONGLONG               Reserved4;                                  /**< 0x3a8 */
    679     ULONGLONG volatile      InterruptTimeBias;                          /**< 0x3b0 */
     679    ULONGLONG volatile      InterruptTimeBias;                          /**< 0x3b0 - What QueryUnbiasedInterruptTimePrecise
     680                                                                         * subtracts from interrupt time. */
    680681    ULONGLONG volatile      QpcBias;                                    /**< 0x3b8 */
    681682    ULONG volatile          ActiveProcessorCount;                       /**< 0x3c0 */
     
    28132814NTSYSAPI uint64_t NTAPI RtlGetSystemTimePrecise(VOID);
    28142815typedef uint64_t (NTAPI * PFNRTLGETSYSTEMTIMEPRECISE)(VOID);
     2816NTSYSAPI uint64_t NTAPI RtlGetInterruptTimePrecise(uint64_t *puPerfTime);
     2817typedef uint64_t (NTAPI * PFNRTLGETINTERRUPTTIMEPRECISE)(uint64_t *);
     2818NTSYSAPI BOOLEAN NTAPI RtlQueryUnbiasedInterruptTime(uint64_t *puInterruptTime);
     2819typedef BOOLEAN (NTAPI * PFNRTLQUERYUNBIASEDINTERRUPTTIME)(uint64_t *);
    28152820
    28162821RT_C_DECLS_END
  • trunk/src/VBox/Runtime/r3/win/time-win.cpp

    r70402 r70405  
    55
    66/*
    7  * Copyright (C) 2006-2017 Oracle Corporation
     7 * Copyright (C) 2006-2018 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3030*********************************************************************************************************************************/
    3131#define LOG_GROUP RTLOGGROUP_TIME
    32 #include <iprt/win/windows.h>
     32#include <iprt/nt/nt-and-windows.h>
    3333
    3434#include <iprt/time.h>
     
    5353//#endif
    5454
    55 
    56 #ifdef USE_INTERRUPT_TIME
    57 
    58 typedef struct _MY_KSYSTEM_TIME
    59 {
    60     ULONG LowPart;
    61     LONG High1Time;
    62     LONG High2Time;
    63 } MY_KSYSTEM_TIME;
    64 
    65 typedef struct _MY_KUSER_SHARED_DATA
    66 {
    67     ULONG TickCountLowDeprecated;
    68     ULONG TickCountMultiplier;
    69     volatile MY_KSYSTEM_TIME InterruptTime;
    70     /* The rest is not relevant. */
    71 } MY_KUSER_SHARED_DATA, *PMY_KUSER_SHARED_DATA;
    72 
    73 #endif /* USE_INTERRUPT_TIME */
    7455
    7556
     
    11293    /*
    11394     * Use interrupt time if we can (not possible on NT 3.1).
     95     * Note! We cannot entirely depend on g_enmWinVer here as we're likely to
     96     *       get called before IPRT is initialized.  Ditto g_hModNtDll.
    11497     */
    115     /** @todo use RtlGetInterruptTimePrecise if available (W10+). */
    116 
    117     static int volatile g_fCanUseUserSharedData = -1;
    118     int fCanUseUserSharedData = g_fCanUseUserSharedData;
    119     if (fCanUseUserSharedData != -1)
     98    static PFNRTLGETINTERRUPTTIMEPRECISE    s_pfnRtlGetInterruptTimePrecise = NULL;
     99    static int volatile                     s_iCanUseUserSharedData         = -1;
     100    int                                     iCanUseUserSharedData           = s_iCanUseUserSharedData;
     101    if (iCanUseUserSharedData != -1)
    120102    { /* likely */ }
    121103    else
     
    123105        /* We may be called before g_enmWinVer has been initialized. */
    124106        if (g_enmWinVer != kRTWinOSType_UNKNOWN)
    125             fCanUseUserSharedData = g_enmWinVer > kRTWinOSType_NT310;
     107            iCanUseUserSharedData = g_enmWinVer > kRTWinOSType_NT310;
    126108        else
    127109        {
    128110            DWORD dwVer = GetVersion();
    129             fCanUseUserSharedData = (dwVer & 0xff) != 3 || ((dwVer >> 8) & 0xff) >= 50;
     111            iCanUseUserSharedData = (dwVer & 0xff) != 3 || ((dwVer >> 8) & 0xff) >= 50;
    130112        }
    131         g_fCanUseUserSharedData = fCanUseUserSharedData;
     113        if (iCanUseUserSharedData != 0)
     114        {
     115            FARPROC pfn = GetProcAddress(g_hModNtDll ? g_hModNtDll : GetModuleHandleW(L"ntdll"), "RtlGetInterruptTimePrecise");
     116            if (pfn != NULL)
     117            {
     118                ASMAtomicWritePtr(&s_pfnRtlGetInterruptTimePrecise, pfn);
     119                iCanUseUserSharedData = 42;
     120            }
     121        }
     122        s_iCanUseUserSharedData = iCanUseUserSharedData;
    132123    }
    133124
    134     if (fCanUseUserSharedData != 0)
     125    if (iCanUseUserSharedData != 0)
    135126    {
    136         PMY_KUSER_SHARED_DATA pUserSharedData = (PMY_KUSER_SHARED_DATA)(uintptr_t)0x7ffe0000;
    137127        LARGE_INTEGER Time;
    138         do
     128        if (iCanUseUserSharedData == 42)
    139129        {
    140             Time.HighPart = pUserSharedData->InterruptTime.High1Time;
    141             Time.LowPart  = pUserSharedData->InterruptTime.LowPart;
    142         } while (pUserSharedData->InterruptTime.High2Time != Time.HighPart);
     130            uint64_t iIgnored;
     131            Time.QuadPart = s_pfnRtlGetInterruptTimePrecise(&iIgnored);
     132        }
     133        else
     134        {
     135            PKUSER_SHARED_DATA pUserSharedData = (PKUSER_SHARED_DATA)MM_SHARED_USER_DATA_VA;
     136            do
     137            {
     138                Time.HighPart = pUserSharedData->InterruptTime.High1Time;
     139                Time.LowPart  = pUserSharedData->InterruptTime.LowPart;
     140            } while (pUserSharedData->InterruptTime.High2Time != Time.HighPart);
     141        }
    143142
    144143        return (uint64_t)Time.QuadPart * 100;
  • trunk/src/VBox/Runtime/testcase/tstTime-3.cpp

    r69111 r70405  
    2929*   Header Files                                                                                                                 *
    3030*********************************************************************************************************************************/
    31 #ifdef RT_OS_WINDOWS
    32 # include <iprt/win/windows.h>
    33 
    34 #else /* posix */
    35 # include <sys/time.h>
    36 #endif
    37 
    3831#include <iprt/time.h>
    3932#include <iprt/stream.h>
     
    4235#include <iprt/thread.h>
    4336#include <iprt/err.h>
    44 
    45 
    46 DECLINLINE(uint64_t) OSNanoTS(void)
    47 {
    48 #ifdef RT_OS_WINDOWS
    49     uint64_t u64; /* manual say larger integer, should be safe to assume it's the same. */
    50     GetSystemTimeAsFileTime((LPFILETIME)&u64);
    51     return u64 * 100;
    52 
    53 #else /* posix */
    54 
    55     struct timeval tv;
    56     gettimeofday(&tv, NULL);
    57     return (uint64_t)tv.tv_sec  * (uint64_t)(1000 * 1000 * 1000)
    58          + (uint64_t)(tv.tv_usec * 1000);
    59 #endif
    60 }
    6137
    6238
     
    7248    }
    7349
    74     RTPrintf("tstTime-3: Testing difference between RTTimeNanoTS() and OS time...\n");
     50    RTPrintf("tstTime-3: Testing difference between RTTimeNanoTS() and RTTimeSystemNanoTS()...\n");
    7551
    7652    for (int i = 1; i < argc; i++)
     
    8561        RTPrintf("tstTime-3: %d - %RU64 seconds period...\n", i, cSeconds);
    8662
    87         RTTimeNanoTS(); OSNanoTS(); RTThreadSleep(1);
     63        RTTimeNanoTS(); RTTimeSystemNanoTS(); RTThreadSleep(1);
    8864        uint64_t u64RTStartTS = RTTimeNanoTS();
    89         uint64_t u64OSStartTS = OSNanoTS();
     65        uint64_t u64OSStartTS = RTTimeSystemNanoTS();
    9066
    9167        RTThreadSleep(cSeconds * 1000);
     
    10379    return 0;
    10480}
     81
Note: See TracChangeset for help on using the changeset viewer.

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