VirtualBox

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

Last change on this file since 62722 was 62721, checked in by vboxsync, 9 years ago

IPRT/testcases: warnings

  • 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 62721 2016-07-29 22:31:28Z vboxsync $ */
2/** @file
3 * IPRT R0 Testcase - Thread Preemption, driver program.
4 */
5
6/*
7 * Copyright (C) 2009-2016 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/err.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 uint32_t cThreads = RTMpGetCount();
200 RTCPUSET OnlineSet;
201 RTMpGetOnlineSet(&OnlineSet);
202 for (uint32_t i = 0; i < RT_ELEMENTS(ahThreads); i++)
203 {
204 ahThreads[i] = NIL_RTTHREAD;
205 if (RTCpuSetIsMemberByIndex(&OnlineSet, i))
206 RTThreadCreateF(&ahThreads[i], MyThreadProc, (void *)(uintptr_t)i, 0, RTTHREADTYPE_DEFAULT,
207 RTTHREADFLAGS_WAITABLE, "cpu=%u", i);
208 }
209
210
211 RTTestSub(hTest, "Pending Preemption");
212 RTThreadSleep(250); /** @todo fix GIP initialization? */
213 for (int i = 0; ; i++)
214 {
215 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
216 Req.Hdr.cbReq = sizeof(Req);
217 Req.szMsg[0] = '\0';
218 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0ThreadPreemption", sizeof("tstRTR0ThreadPreemption") - 1,
219 TSTRTR0THREADPREEMPTION_IS_PENDING, 0, &Req.Hdr), VINF_SUCCESS);
220 if ( strcmp(Req.szMsg, "!cLoops=1\n")
221 || i >= 64)
222 {
223 if (Req.szMsg[0] == '!')
224 RTTestIFailed("%s", &Req.szMsg[1]);
225 else if (Req.szMsg[0])
226 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
227 break;
228 }
229 if ((i % 3) == 0)
230 RTThreadYield();
231 else if ((i % 16) == 0)
232 RTThreadSleep(8);
233 }
234
235 ASMAtomicWriteBool(&g_fTerminate, true);
236 for (uint32_t i = 0; i < RT_ELEMENTS(ahThreads); i++)
237 if (ahThreads[i] != NIL_RTTHREAD)
238 RTThreadWait(ahThreads[i], 5000, NULL);
239
240 /*
241 * Test nested RTThreadPreemptDisable calls.
242 */
243 RTTestSub(hTest, "Nested");
244 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
245 Req.Hdr.cbReq = sizeof(Req);
246 Req.szMsg[0] = '\0';
247 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0ThreadPreemption", sizeof("tstRTR0ThreadPreemption") - 1,
248 TSTRTR0THREADPREEMPTION_NESTED, 0, &Req.Hdr), VINF_SUCCESS);
249 if (Req.szMsg[0] == '!')
250 RTTestIFailed("%s", &Req.szMsg[1]);
251 else if (Req.szMsg[0])
252 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
253
254
255 /*
256 * Test thread-context hooks.
257 */
258 RTTestSub(hTest, "RTThreadCtxHooks");
259 uint64_t u64StartTS = RTTimeMilliTS();
260 uint64_t cMsMax = 60000; /* ca. 1 minute timeout. */
261 uint64_t cMsElapsed;
262 for (unsigned i = 0; i < 50; i++)
263 {
264 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
265 Req.Hdr.cbReq = sizeof(Req);
266 Req.szMsg[0] = '\0';
267 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0ThreadPreemption", sizeof("tstRTR0ThreadPreemption") - 1,
268 TSTRTR0THREADPREEMPTION_CTXHOOKS, 0, &Req.Hdr), VINF_SUCCESS);
269 if (RT_FAILURE(rc))
270 return RTTestSummaryAndDestroy(hTest);
271 if (Req.szMsg[0] == '!')
272 RTTestIFailed("%s", &Req.szMsg[1]);
273 else if (Req.szMsg[0])
274 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
275 if (!(i % 10))
276 RTTestIPrintf(RTTESTLVL_ALWAYS, "RTThreadCtxHooks passed %u iteration(s)\n", i);
277
278 /* Check timeout and bail. */
279 cMsElapsed = RTTimeMilliTS() - u64StartTS;
280 if (cMsElapsed > cMsMax)
281 {
282 RTTestIPrintf(RTTESTLVL_INFO, "RTThreadCtxHooks Stopping iterations. %RU64 ms. for %u iterations.\n",
283 cMsElapsed, i);
284 break;
285 }
286 }
287
288 /*
289 * Done.
290 */
291 return RTTestSummaryAndDestroy(hTest);
292#endif
293}
294
295
296#if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
297/**
298 * Main entry point.
299 */
300int main(int argc, char **argv, char **envp)
301{
302 return TrustedMain(argc, argv, envp);
303}
304#endif
305
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