1 | /* $Id: tstRTThreadExecutionTime.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTThreadGetExecution.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/thread.h>
|
---|
42 |
|
---|
43 | #include <iprt/asm.h>
|
---|
44 | #include <iprt/test.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 | #include <iprt/stream.h>
|
---|
47 | #include <iprt/time.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Global Variables *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | static RTTEST g_hTest;
|
---|
54 | static volatile uint64_t g_cMsKernel;
|
---|
55 | static volatile uint64_t g_cMsUser;
|
---|
56 |
|
---|
57 |
|
---|
58 | static DECLCALLBACK(int) testThread(RTTHREAD hSelf, void *pvUser)
|
---|
59 | {
|
---|
60 | RTMSINTERVAL const msWait = *(RTMSINTERVAL const *)pvUser;
|
---|
61 | RT_NOREF_PV(hSelf);
|
---|
62 |
|
---|
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);
|
---|
78 |
|
---|
79 | return VINF_SUCCESS;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | static void test1(RTMSINTERVAL msWait)
|
---|
84 | {
|
---|
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 |
|
---|
99 | static 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 (;;)
|
---|
108 | {
|
---|
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 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | int main()
|
---|
128 | {
|
---|
129 | RTEXITCODE rcExit = RTTestInitAndCreate("tstRTThreadExecutionTime", &g_hTest);
|
---|
130 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
131 | return rcExit;
|
---|
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();
|
---|
140 |
|
---|
141 | return RTTestSummaryAndDestroy(g_hTest);
|
---|
142 | }
|
---|