VirtualBox

Ignore:
Timestamp:
Feb 20, 2023 8:58:45 AM (22 months ago)
Author:
vboxsync
Message:

IPRT/tstRTTime: Try avoid false failures due to preemption between RTTimeNanoTS and RTTimeSystemNanoTS calls.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/tstRTTime.cpp

    r98103 r98637  
    6363     */
    6464
     65    /* Take down the start time of both sources, try get them w/o being rescheduled. */
    6566    RTTimeSystemNanoTS(); RTTimeNanoTS(); RTThreadYield();
    6667    uint64_t u64RTStartTS = RTTimeNanoTS();
    6768    uint64_t u64OSStartTS = RTTimeSystemNanoTS();
     69    uint64_t const cNsMaxTolerance = 256 * RT_NS_1US;
     70    for (unsigned cTries = 0; cTries < 32 && RTTimeNanoTS() - u64RTStartTS > cNsMaxTolerance; cTries++)
     71    {
     72        RTThreadYield();
     73        u64RTStartTS = RTTimeNanoTS();
     74        u64OSStartTS = RTTimeSystemNanoTS();
     75    }
    6876
     77    /* Test loop. */
    6978    uint32_t i;
    7079    uint64_t u64Prev = RTTimeNanoTS();
     
    98107    }
    99108
     109    /* Take down the stop time of both sources, again try get them w/o being rescheduled. */
    100110    RTTimeSystemNanoTS(); RTTimeNanoTS(); RTThreadYield();
    101111    uint64_t u64RTElapsedTS = RTTimeNanoTS();
    102112    uint64_t u64OSElapsedTS = RTTimeSystemNanoTS();
     113    for (unsigned cTries = 0; cTries < 32 && RTTimeNanoTS() - u64RTElapsedTS > cNsMaxTolerance; cTries++)
     114    {
     115        RTThreadYield();
     116        u64RTElapsedTS = RTTimeNanoTS();
     117        u64OSElapsedTS = RTTimeSystemNanoTS();
     118    }
    103119    u64RTElapsedTS -= u64RTStartTS;
    104120    u64OSElapsedTS -= u64OSStartTS;
     121
     122    /* Check the runtime difference between the two sources. */
    105123    int64_t i64Diff = u64OSElapsedTS >= u64RTElapsedTS ? u64OSElapsedTS - u64RTElapsedTS : u64RTElapsedTS - u64OSElapsedTS;
    106     if (i64Diff > (int64_t)(u64OSElapsedTS / 1000))
     124    if (i64Diff > (int64_t)(RT_MAX(u64OSElapsedTS / 1000, cNsMaxTolerance)))
    107125        RTTestFailed(hTest, "total time differs too much! u64OSElapsedTS=%#llx u64RTElapsedTS=%#llx delta=%lld\n",
    108126                     u64OSElapsedTS, u64RTElapsedTS, u64OSElapsedTS - u64RTElapsedTS);
     
    117135    }
    118136
     137    /* Report debug details: */
    119138#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86) /** @todo This isn't really x86 or AMD64 specific... */
    120139    RTTestValue(hTest, "RTTimeDbgSteps",        RTTimeDbgSteps(),                           RTTESTUNIT_OCCURRENCES);
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