VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTR0ThreadPreemptionDriver.cpp@ 83743

Last change on this file since 83743 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 KB
Line 
1/* $Id: tstRTR0ThreadPreemptionDriver.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT R0 Testcase - Thread Preemption, driver program.
4 */
5
6/*
7 * Copyright (C) 2009-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/initterm.h>
32
33#include <iprt/asm.h>
34#include <iprt/cpuset.h>
35#include <iprt/errcore.h>
36#include <iprt/path.h>
37#include <iprt/param.h>
38#include <iprt/stream.h>
39#include <iprt/string.h>
40#include <iprt/test.h>
41#include <iprt/time.h>
42#include <iprt/thread.h>
43#ifdef VBOX
44# include <VBox/sup.h>
45# include "tstRTR0ThreadPreemption.h"
46#endif
47
48
49/*********************************************************************************************************************************
50* Global Variables *
51*********************************************************************************************************************************/
52static bool volatile g_fTerminate = false;
53
54
55/**
56 * Try make sure all online CPUs will be engaged.
57 */
58static DECLCALLBACK(int) MyThreadProc(RTTHREAD hSelf, void *pvCpuIdx)
59{
60 RT_NOREF1(hSelf);
61 RTCPUSET Affinity;
62 RTCpuSetEmpty(&Affinity);
63 RTCpuSetAddByIndex(&Affinity, (intptr_t)pvCpuIdx);
64 RTThreadSetAffinity(&Affinity); /* ignore return code as it's not supported on all hosts. */
65
66 while (!g_fTerminate)
67 {
68 uint64_t tsStart = RTTimeMilliTS();
69 do
70 {
71 ASMNopPause();
72 } while (RTTimeMilliTS() - tsStart < 8);
73 RTThreadSleep(4);
74 }
75
76 return VINF_SUCCESS;
77}
78
79
80/**
81 * Entry point.
82 */
83extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
84{
85 RT_NOREF3(argc, argv, envp);
86#ifndef VBOX
87 RTPrintf("tstSup: SKIPPED\n");
88 return 0;
89#else
90 /*
91 * Init.
92 */
93 RTTEST hTest;
94 int rc = RTTestInitAndCreate("tstRTR0ThreadPreemption", &hTest);
95 if (rc)
96 return rc;
97 RTTestBanner(hTest);
98
99 PSUPDRVSESSION pSession;
100 rc = SUPR3Init(&pSession);
101 if (RT_FAILURE(rc))
102 {
103 RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
104 return RTTestSummaryAndDestroy(hTest);
105 }
106
107 char szPath[RTPATH_MAX];
108 rc = RTPathExecDir(szPath, sizeof(szPath));
109 if (RT_SUCCESS(rc))
110 rc = RTPathAppend(szPath, sizeof(szPath), "tstRTR0ThreadPreemption.r0");
111 if (RT_FAILURE(rc))
112 {
113 RTTestFailed(hTest, "Failed constructing .r0 filename (rc=%Rrc)", rc);
114 return RTTestSummaryAndDestroy(hTest);
115 }
116
117 void *pvImageBase;
118 rc = SUPR3LoadServiceModule(szPath, "tstRTR0ThreadPreemption",
119 "TSTRTR0ThreadPreemptionSrvReqHandler",
120 &pvImageBase);
121 if (RT_FAILURE(rc))
122 {
123 RTTestFailed(hTest, "SUPR3LoadServiceModule(%s,,,) failed with rc=%Rrc\n", szPath, rc);
124 return RTTestSummaryAndDestroy(hTest);
125 }
126
127 /* test request */
128 struct
129 {
130 SUPR0SERVICEREQHDR Hdr;
131 char szMsg[256];
132 } Req;
133
134 /*
135 * Sanity checks.
136 */
137 RTTestSub(hTest, "Sanity");
138 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
139 Req.Hdr.cbReq = sizeof(Req);
140 Req.szMsg[0] = '\0';
141 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0ThreadPreemption", sizeof("tstRTR0ThreadPreemption") - 1,
142 TSTRTR0THREADPREEMPTION_SANITY_OK, 0, &Req.Hdr), VINF_SUCCESS);
143 if (RT_FAILURE(rc))
144 return RTTestSummaryAndDestroy(hTest);
145 RTTESTI_CHECK_MSG(Req.szMsg[0] == '\0', ("%s", Req.szMsg));
146 if (Req.szMsg[0] != '\0')
147 return RTTestSummaryAndDestroy(hTest);
148
149 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
150 Req.Hdr.cbReq = sizeof(Req);
151 Req.szMsg[0] = '\0';
152 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0ThreadPreemption", sizeof("tstRTR0ThreadPreemption") - 1,
153 TSTRTR0THREADPREEMPTION_SANITY_FAILURE, 0, &Req.Hdr), VINF_SUCCESS);
154 if (RT_FAILURE(rc))
155 return RTTestSummaryAndDestroy(hTest);
156 RTTESTI_CHECK_MSG(!strncmp(Req.szMsg, RT_STR_TUPLE("!42failure42")), ("%s", Req.szMsg));
157 if (strncmp(Req.szMsg, RT_STR_TUPLE("!42failure42")))
158 return RTTestSummaryAndDestroy(hTest);
159
160 /*
161 * Basic tests, bail out on failure.
162 */
163 RTTestSub(hTest, "Basics");
164 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
165 Req.Hdr.cbReq = sizeof(Req);
166 Req.szMsg[0] = '\0';
167 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0ThreadPreemption", sizeof("tstRTR0ThreadPreemption") - 1,
168 TSTRTR0THREADPREEMPTION_BASIC, 0, &Req.Hdr), VINF_SUCCESS);
169 if (RT_FAILURE(rc))
170 return RTTestSummaryAndDestroy(hTest);
171 if (Req.szMsg[0] == '!')
172 {
173 RTTestIFailed("%s", &Req.szMsg[1]);
174 return RTTestSummaryAndDestroy(hTest);
175 }
176 if (Req.szMsg[0])
177 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
178
179 /*
180 * Is it trusty.
181 */
182 RTTestSub(hTest, "RTThreadPreemptIsPendingTrusty");
183 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
184 Req.Hdr.cbReq = sizeof(Req);
185 Req.szMsg[0] = '\0';
186 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0ThreadPreemption", sizeof("tstRTR0ThreadPreemption") - 1,
187 TSTRTR0THREADPREEMPTION_IS_TRUSTY, 0, &Req.Hdr), VINF_SUCCESS);
188 if (RT_FAILURE(rc))
189 return RTTestSummaryAndDestroy(hTest);
190 if (Req.szMsg[0] == '!')
191 RTTestIFailed("%s", &Req.szMsg[1]);
192 else if (Req.szMsg[0])
193 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
194
195 /*
196 * Stay in ring-0 until preemption is pending.
197 */
198 RTTHREAD ahThreads[RTCPUSET_MAX_CPUS];
199 RTCPUSET OnlineSet;
200 RTMpGetOnlineSet(&OnlineSet);
201 for (uint32_t i = 0; i < RT_ELEMENTS(ahThreads); i++)
202 {
203 ahThreads[i] = NIL_RTTHREAD;
204 if (RTCpuSetIsMemberByIndex(&OnlineSet, i))
205 RTThreadCreateF(&ahThreads[i], MyThreadProc, (void *)(uintptr_t)i, 0, RTTHREADTYPE_DEFAULT,
206 RTTHREADFLAGS_WAITABLE, "cpu=%u", i);
207 }
208
209
210 RTTestSub(hTest, "Pending Preemption");
211 RTThreadSleep(250); /** @todo fix GIP initialization? */
212 for (int i = 0; ; i++)
213 {
214 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
215 Req.Hdr.cbReq = sizeof(Req);
216 Req.szMsg[0] = '\0';
217 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0ThreadPreemption", sizeof("tstRTR0ThreadPreemption") - 1,
218 TSTRTR0THREADPREEMPTION_IS_PENDING, 0, &Req.Hdr), VINF_SUCCESS);
219 if ( strcmp(Req.szMsg, "!cLoops=1\n")
220 || i >= 64)
221 {
222 if (Req.szMsg[0] == '!')
223 RTTestIFailed("%s", &Req.szMsg[1]);
224 else if (Req.szMsg[0])
225 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
226 break;
227 }
228 if ((i % 3) == 0)
229 RTThreadYield();
230 else if ((i % 16) == 0)
231 RTThreadSleep(8);
232 }
233
234 ASMAtomicWriteBool(&g_fTerminate, true);
235 for (uint32_t i = 0; i < RT_ELEMENTS(ahThreads); i++)
236 if (ahThreads[i] != NIL_RTTHREAD)
237 RTThreadWait(ahThreads[i], 5000, NULL);
238
239 /*
240 * Test nested RTThreadPreemptDisable calls.
241 */
242 RTTestSub(hTest, "Nested");
243 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
244 Req.Hdr.cbReq = sizeof(Req);
245 Req.szMsg[0] = '\0';
246 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0ThreadPreemption", sizeof("tstRTR0ThreadPreemption") - 1,
247 TSTRTR0THREADPREEMPTION_NESTED, 0, &Req.Hdr), VINF_SUCCESS);
248 if (Req.szMsg[0] == '!')
249 RTTestIFailed("%s", &Req.szMsg[1]);
250 else if (Req.szMsg[0])
251 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
252
253
254 /*
255 * Test thread-context hooks.
256 */
257 RTTestSub(hTest, "RTThreadCtxHooks");
258 uint64_t u64StartTS = RTTimeMilliTS();
259 uint64_t cMsMax = 60000; /* ca. 1 minute timeout. */
260 uint64_t cMsElapsed;
261 for (unsigned i = 0; i < 50; i++)
262 {
263 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
264 Req.Hdr.cbReq = sizeof(Req);
265 Req.szMsg[0] = '\0';
266 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0ThreadPreemption", sizeof("tstRTR0ThreadPreemption") - 1,
267 TSTRTR0THREADPREEMPTION_CTXHOOKS, 0, &Req.Hdr), VINF_SUCCESS);
268 if (RT_FAILURE(rc))
269 return RTTestSummaryAndDestroy(hTest);
270 if (Req.szMsg[0] == '!')
271 RTTestIFailed("%s", &Req.szMsg[1]);
272 else if (Req.szMsg[0])
273 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
274 if (!(i % 10))
275 RTTestIPrintf(RTTESTLVL_ALWAYS, "RTThreadCtxHooks passed %u iteration(s)\n", i);
276
277 /* Check timeout and bail. */
278 cMsElapsed = RTTimeMilliTS() - u64StartTS;
279 if (cMsElapsed > cMsMax)
280 {
281 RTTestIPrintf(RTTESTLVL_INFO, "RTThreadCtxHooks Stopping iterations. %RU64 ms. for %u iterations.\n",
282 cMsElapsed, i);
283 break;
284 }
285 }
286
287 /*
288 * Done.
289 */
290 return RTTestSummaryAndDestroy(hTest);
291#endif
292}
293
294
295#if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
296/**
297 * Main entry point.
298 */
299int main(int argc, char **argv, char **envp)
300{
301 return TrustedMain(argc, argv, envp);
302}
303#endif
304
Note: See TracBrowser for help on using the repository browser.

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