VirtualBox

Ignore:
Timestamp:
May 22, 2023 2:15:10 PM (19 months ago)
Author:
vboxsync
Message:

IPRT: Cleaned up RTThreadGetExecutionTimeMilli and associated testcase.

File:
1 edited

Legend:

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

    r98103 r99901  
    5252*********************************************************************************************************************************/
    5353static RTTEST g_hTest;
    54 static volatile uint64_t g_kernel, g_user;
     54static volatile uint64_t g_cMsKernel;
     55static volatile uint64_t g_cMsUser;
    5556
    5657
    5758static DECLCALLBACK(int) testThread(RTTHREAD hSelf, void *pvUser)
    5859{
    59     RT_NOREF_PV(hSelf); RT_NOREF_PV(pvUser);
     60    RTMSINTERVAL const msWait = *(RTMSINTERVAL const *)pvUser;
     61    RT_NOREF_PV(hSelf);
    6062
    61     uint64_t u64Now = RTTimeMilliTS();
    62     uint64_t kernel, kernelStart, user, userStart;
    63     RTThreadGetExecutionTimeMilli(&kernelStart, &userStart);
    64     while (RTTimeMilliTS() < u64Now + 1000)
    65         ;
    66     RTThreadGetExecutionTimeMilli(&kernel, &user);
    67     RTPrintf("kernel = %4lldms, user = %4lldms\n", kernel - kernelStart, user - userStart);
    68     ASMAtomicAddU64(&g_kernel, kernel);
    69     ASMAtomicAddU64(&g_user, user);
     63    uint64_t const msNow = RTTimeMilliTS();
     64    uint64_t cMsKernelStart, cMsUserStart;
     65    RTTEST_CHECK_RC_RET(g_hTest, RTThreadGetExecutionTimeMilli(&cMsKernelStart, &cMsUserStart), VINF_SUCCESS, rcCheck);
     66
     67    while (RTTimeMilliTS() < msNow + msWait)
     68        ASMNopPause();
     69
     70    uint64_t cMsKernel, cMsUser;
     71    RTTEST_CHECK_RC_RET(g_hTest, RTThreadGetExecutionTimeMilli(&cMsKernel, &cMsUser), VINF_SUCCESS, rcCheck);
     72
     73    cMsKernel -= cMsKernelStart;
     74    cMsUser   -= cMsUserStart;
     75    RTPrintf("kernel = %4lldms, user = %4lldms\n", cMsKernel, cMsUser);
     76    ASMAtomicAddU64(&g_cMsKernel, cMsKernel);
     77    ASMAtomicAddU64(&g_cMsUser,   cMsUser);
    7078
    7179    return VINF_SUCCESS;
     
    7381
    7482
    75 static void test1(void)
     83static void test1(RTMSINTERVAL msWait)
    7684{
    77     RTTestSub(g_hTest, "Interrupt RTThreadSleep");
    78     RTTHREAD hThread[16];
    79     RTMSINTERVAL msWait = 1000;
    80     for (unsigned i = 0; i < RT_ELEMENTS(hThread); i++)
     85    RTTHREAD ahThreads[16];
     86    RTTestSubF(g_hTest, "RTThreadGetExecutionTimeMilli - %zu thread for %u ms", RT_ELEMENTS(ahThreads), (unsigned)msWait);
     87    for (unsigned i = 0; i < RT_ELEMENTS(ahThreads); i++)
     88        RTTESTI_CHECK_RC_RETV(RTThreadCreate(&ahThreads[i], testThread, &msWait, 0, RTTHREADTYPE_DEFAULT,
     89                              RTTHREADFLAGS_WAITABLE, "test"), VINF_SUCCESS);
     90
     91    RTPrintf("Waiting for the threads to complete...\n");
     92    for (unsigned i = 0; i < RT_ELEMENTS(ahThreads); i++)
     93        RTTESTI_CHECK_RC(RTThreadWait(ahThreads[i], msWait * 5, NULL), VINF_SUCCESS);
     94
     95    RTPrintf("sum kernel = %lldms, sum user = %lldms\n", g_cMsKernel, g_cMsUser);
     96}
     97
     98
     99static void test2(void)
     100{
     101    RTTestSub(g_hTest, "RTThreadGetExecutionTimeMilli perf");
     102
     103    /* Run it for ~3 seconds. */
     104    RTThreadYield();
     105    uint64_t       cCalls  = 0;
     106    uint64_t const nsStart = RTTimeNanoTS();
     107    for (;;)
    81108    {
    82         RTTESTI_CHECK_RC_RETV(RTThreadCreate(&hThread[i], testThread, NULL, 0, RTTHREADTYPE_DEFAULT,
    83                               RTTHREADFLAGS_WAITABLE, "test"), VINF_SUCCESS);
     109        uint32_t cLeftBeforeCheck = 16384;
     110        while (cLeftBeforeCheck-- > 0)
     111        {
     112            uint64_t uIgn;
     113            RTThreadGetExecutionTimeMilli(&uIgn, &uIgn);
     114            cCalls++;
     115        }
     116        uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
     117        if (cNsElapsed >= RT_NS_1SEC_64 * 3)
     118        {
     119            RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "%'RU64 calls in %'RU64 ns\n", cCalls, cNsElapsed);
     120            RTTestValue(g_hTest, "RTThreadGetExecutionTimeMilli avg.", cNsElapsed * 1000 / cCalls, RTTESTUNIT_PS_PER_CALL);
     121            return;
     122        }
    84123    }
    85     RTThreadSleep(500);
    86     RTPrintf("Waiting for %dms ...\n", msWait);
    87     RTThreadSleep(msWait);
    88     for (unsigned i = 0; i < RT_ELEMENTS(hThread); i++)
    89         RTTESTI_CHECK_RC(RTThreadWait(hThread[i], RT_INDEFINITE_WAIT, NULL), VINF_SUCCESS);
    90 
    91     RTPrintf("sum kernel = %lldms, sum user = %lldms\n", g_kernel, g_user);
    92124}
    93125
     
    98130    if (rcExit != RTEXITCODE_SUCCESS)
    99131        return rcExit;
    100     test1();
     132
     133    uint64_t uIgn;
     134    int rc = RTThreadGetExecutionTimeMilli(&uIgn, &uIgn);
     135    if (rc == VERR_NOT_IMPLEMENTED)
     136        return RTTestSkipAndDestroy(g_hTest, "VERR_NOT_IMPLEMENTED");
     137
     138    test1(RT_MS_1SEC);
     139    test2();
    101140
    102141    return RTTestSummaryAndDestroy(g_hTest);
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