VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstR0ThreadPreemptionDriver.cpp@ 45397

Last change on this file since 45397 was 45264, checked in by vboxsync, 12 years ago

tstR0ThreadPreemptionDriver.cpp: Start a bunch of threads to make sure the CPU will require preemption.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: tstR0ThreadPreemptionDriver.cpp 45264 2013-03-31 12:40:20Z vboxsync $ */
2/** @file
3 * IPRT R0 Testcase - Thread Preemption, driver program.
4 */
5
6/*
7 * Copyright (C) 2009-2010 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* Header Files *
29*******************************************************************************/
30#include <iprt/initterm.h>
31
32#include <iprt/asm.h>
33#include <iprt/cpuset.h>
34#include <iprt/err.h>
35#include <iprt/path.h>
36#include <iprt/param.h>
37#include <iprt/stream.h>
38#include <iprt/string.h>
39#include <iprt/test.h>
40#include <iprt/thread.h>
41#ifdef VBOX
42# include <VBox/sup.h>
43# include "tstR0ThreadPreemption.h"
44#endif
45
46/*******************************************************************************
47* Global Variables *
48*******************************************************************************/
49static bool volatile g_fTerminate = false;
50
51
52/**
53 * Try make sure all online CPUs will be engaged.
54 */
55static DECLCALLBACK(int) MyThreadProc(RTTHREAD hSelf, void *pvCpuIdx)
56{
57 RTCPUSET Affinity;
58 RTCpuSetEmpty(&Affinity);
59 RTCpuSetAddByIndex(&Affinity, (intptr_t)pvCpuIdx);
60 RTThreadSetAffinity(&Affinity); /* ignore return code as it's not supported on all hosts. */
61
62 while (!g_fTerminate)
63 RTThreadSleep(50);
64
65 return VINF_SUCCESS;
66}
67
68
69int main(int argc, char **argv)
70{
71#ifndef VBOX
72 RTPrintf("tstSup: SKIPPED\n");
73 return 0;
74#else
75 /*
76 * Init.
77 */
78 RTTEST hTest;
79 int rc = RTTestInitAndCreate("tstR0ThreadPreemption", &hTest);
80 if (rc)
81 return rc;
82 RTTestBanner(hTest);
83
84 PSUPDRVSESSION pSession;
85 rc = SUPR3Init(&pSession);
86 if (RT_FAILURE(rc))
87 {
88 RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
89 return RTTestSummaryAndDestroy(hTest);
90 }
91
92 char szPath[RTPATH_MAX];
93 rc = RTPathExecDir(szPath, sizeof(szPath));
94 if (RT_SUCCESS(rc))
95 rc = RTPathAppend(szPath, sizeof(szPath), "tstR0ThreadPreemption.r0");
96 if (RT_FAILURE(rc))
97 {
98 RTTestFailed(hTest, "Failed constructing .r0 filename (rc=%Rrc)", rc);
99 return RTTestSummaryAndDestroy(hTest);
100 }
101
102 void *pvImageBase;
103 rc = SUPR3LoadServiceModule(szPath, "tstR0ThreadPreemption",
104 "TSTR0ThreadPreemptionSrvReqHandler",
105 &pvImageBase);
106 if (RT_FAILURE(rc))
107 {
108 RTTestFailed(hTest, "SUPR3LoadServiceModule(%s,,,) failed with rc=%Rrc\n", szPath, rc);
109 return RTTestSummaryAndDestroy(hTest);
110 }
111
112 /* test request */
113 struct
114 {
115 SUPR0SERVICEREQHDR Hdr;
116 char szMsg[256];
117 } Req;
118
119 /*
120 * Sanity checks.
121 */
122 RTTestSub(hTest, "Sanity");
123 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
124 Req.Hdr.cbReq = sizeof(Req);
125 Req.szMsg[0] = '\0';
126 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
127 TSTR0THREADPREMEPTION_SANITY_OK, 0, &Req.Hdr), VINF_SUCCESS);
128 if (RT_FAILURE(rc))
129 return RTTestSummaryAndDestroy(hTest);
130 RTTESTI_CHECK_MSG(Req.szMsg[0] == '\0', ("%s", Req.szMsg));
131 if (Req.szMsg[0] != '\0')
132 return RTTestSummaryAndDestroy(hTest);
133
134 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
135 Req.Hdr.cbReq = sizeof(Req);
136 Req.szMsg[0] = '\0';
137 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
138 TSTR0THREADPREMEPTION_SANITY_FAILURE, 0, &Req.Hdr), VINF_SUCCESS);
139 if (RT_FAILURE(rc))
140 return RTTestSummaryAndDestroy(hTest);
141 RTTESTI_CHECK_MSG(!strncmp(Req.szMsg, "!42failure42", sizeof("!42failure42") - 1), ("%s", Req.szMsg));
142 if (strncmp(Req.szMsg, "!42failure42", sizeof("!42failure42") - 1))
143 return RTTestSummaryAndDestroy(hTest);
144
145 /*
146 * Basic tests, bail out on failure.
147 */
148 RTTestSub(hTest, "Basics");
149 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
150 Req.Hdr.cbReq = sizeof(Req);
151 Req.szMsg[0] = '\0';
152 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
153 TSTR0THREADPREMEPTION_BASIC, 0, &Req.Hdr), VINF_SUCCESS);
154 if (RT_FAILURE(rc))
155 return RTTestSummaryAndDestroy(hTest);
156 if (Req.szMsg[0] == '!')
157 {
158 RTTestIFailed("%s", &Req.szMsg[1]);
159 return RTTestSummaryAndDestroy(hTest);
160 }
161 if (Req.szMsg[0])
162 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
163
164 /*
165 * Stay in ring-0 until preemption is pending.
166 */
167 RTTHREAD ahThreads[RTCPUSET_MAX_CPUS];
168 uint32_t cThreads = RTMpGetCount();
169 RTCPUSET OnlineSet;
170 RTMpGetOnlineSet(&OnlineSet);
171 for (uint32_t i = 0; i < RT_ELEMENTS(ahThreads); i++)
172 {
173 ahThreads[i] = NIL_RTTHREAD;
174 if (RTCpuSetIsMemberByIndex(&OnlineSet, i))
175 RTThreadCreateF(&ahThreads[i], MyThreadProc, (void *)(uintptr_t)i, 0, RTTHREADTYPE_DEFAULT,
176 RTTHREADFLAGS_WAITABLE, "cpu=%u", i);
177 }
178
179RTThreadSleep(250); /** @todo fix GIP initialization? */
180
181 RTTestSub(hTest, "Pending Preemption");
182 for (int i = 0; ; i++)
183 {
184 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
185 Req.Hdr.cbReq = sizeof(Req);
186 Req.szMsg[0] = '\0';
187 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
188 TSTR0THREADPREMEPTION_IS_PENDING, 0, &Req.Hdr), VINF_SUCCESS);
189 if ( strcmp(Req.szMsg, "cLoops=1\n")
190 || i >= 64)
191 {
192 if (Req.szMsg[0] == '!')
193 RTTestIFailed("%s", &Req.szMsg[1]);
194 else if (Req.szMsg[0])
195 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
196 break;
197 }
198 if ((i % 3) == 0)
199 RTThreadYield();
200 }
201
202 ASMAtomicWriteBool(&g_fTerminate, true);
203 for (uint32_t i = 0; i < RT_ELEMENTS(ahThreads); i++)
204 if (ahThreads[i] != NIL_RTTHREAD)
205 RTThreadWait(ahThreads[i], 5000, NULL);
206
207 /*
208 * Test nested RTThreadPreemptDisable calls.
209 */
210 RTTestSub(hTest, "Nested");
211 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
212 Req.Hdr.cbReq = sizeof(Req);
213 Req.szMsg[0] = '\0';
214 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
215 TSTR0THREADPREMEPTION_NESTED, 0, &Req.Hdr), VINF_SUCCESS);
216 if (Req.szMsg[0] == '!')
217 RTTestIFailed("%s", &Req.szMsg[1]);
218 else if (Req.szMsg[0])
219 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
220
221 /*
222 * Done.
223 */
224 return RTTestSummaryAndDestroy(hTest);
225#endif
226}
227
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