VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/testcase/tstSupSem.cpp@ 33032

Last change on this file since 33032 was 33032, checked in by vboxsync, 14 years ago

tstSupSem.cpp: Some more tests.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.0 KB
Line 
1/* $Id: tstSupSem.cpp 33032 2010-10-11 09:30:34Z vboxsync $ */
2/** @file
3 * Support Library Testcase - Ring-3 Semaphore interface.
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/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <VBox/sup.h>
32
33#include <VBox/param.h>
34#include <iprt/err.h>
35#include <iprt/initterm.h>
36#include <iprt/stream.h>
37#include <iprt/test.h>
38#include <iprt/thread.h>
39#include <iprt/process.h>
40#include <iprt/env.h>
41#include <iprt/string.h>
42#include <iprt/time.h>
43
44
45/*******************************************************************************
46* Structures and Typedefs *
47*******************************************************************************/
48static PSUPDRVSESSION g_pSession;
49static RTTEST g_hTest;
50static uint32_t g_cMillies; /* Used by the interruptible tests. */
51
52
53
54static DECLCALLBACK(int) tstSupSemInterruptibleSRE(RTTHREAD hSelf, void *pvUser)
55{
56 SUPSEMEVENT hEvent = (SUPSEMEVENT)pvUser;
57 RTThreadUserSignal(hSelf);
58 return SUPSemEventWaitNoResume(g_pSession, hEvent, g_cMillies);
59}
60
61
62static DECLCALLBACK(int) tstSupSemInterruptibleMRE(RTTHREAD hSelf, void *pvUser)
63{
64 SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)pvUser;
65 RTThreadUserSignal(hSelf);
66 return SUPSemEventMultiWaitNoResume(g_pSession, hEventMulti, g_cMillies);
67}
68
69
70int main(int argc, char **argv)
71{
72 /*
73 * Init.
74 */
75 int rc = RTR3InitAndSUPLib();
76 if (RT_FAILURE(rc))
77 {
78 RTPrintf("tstSupSem: fatal error: RTR3InitAndSUPLib failed with rc=%Rrc\n", rc);
79 return 1;
80 }
81
82 if (argc == 2 && !strcmp(argv[1], "child"))
83 {
84 RTThreadSleep(300);
85 return 0;
86 }
87
88 RTTEST hTest;
89 rc = RTTestCreate("tstSupSem", &hTest);
90 if (RT_FAILURE(rc))
91 {
92 RTPrintf("tstSupSem: fatal error: RTTestCreate failed with rc=%Rrc\n", rc);
93 return 1;
94 }
95 g_hTest = hTest;
96
97 PSUPDRVSESSION pSession;
98 rc = SUPR3Init(&pSession);
99 if (RT_FAILURE(rc))
100 {
101 RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
102 return RTTestSummaryAndDestroy(hTest);
103 }
104 g_pSession = pSession;
105 RTTestBanner(hTest);
106
107 /*
108 * Basic API checks.
109 */
110 RTTestSub(hTest, "Single Release Event (SRE) API");
111 SUPSEMEVENT hEvent = NIL_SUPSEMEVENT;
112 RTTESTI_CHECK_RC(SUPSemEventCreate(pSession, &hEvent), VINF_SUCCESS);
113 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 0), VERR_TIMEOUT);
114 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 1), VERR_TIMEOUT);
115 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 2), VERR_TIMEOUT);
116 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 8), VERR_TIMEOUT);
117 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent,20), VERR_TIMEOUT);
118 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
119 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 0), VINF_SUCCESS);
120 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
121 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 1), VINF_SUCCESS);
122 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
123 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 2), VINF_SUCCESS);
124 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
125 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 8), VINF_SUCCESS);
126 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
127 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 20), VINF_SUCCESS);
128 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
129 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent,1000),VINF_SUCCESS);
130 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
131 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
132 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 0), VINF_SUCCESS);
133 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 0), VERR_TIMEOUT);
134 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 1), VERR_TIMEOUT);
135 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 2), VERR_TIMEOUT);
136 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 8), VERR_TIMEOUT);
137 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent,20), VERR_TIMEOUT);
138 RTTESTI_CHECK_RC(SUPSemEventClose(pSession, hEvent), VINF_OBJECT_DESTROYED);
139 RTTESTI_CHECK_RC(SUPSemEventClose(pSession, hEvent), VERR_INVALID_HANDLE);
140 RTTESTI_CHECK_RC(SUPSemEventClose(pSession, NIL_SUPSEMEVENT), VINF_SUCCESS);
141
142 RTTestSub(hTest, "Multiple Release Event (MRE) API");
143 SUPSEMEVENTMULTI hEventMulti = NIL_SUPSEMEVENT;
144 RTTESTI_CHECK_RC(SUPSemEventMultiCreate(pSession, &hEventMulti), VINF_SUCCESS);
145 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VERR_TIMEOUT);
146 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 1), VERR_TIMEOUT);
147 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 2), VERR_TIMEOUT);
148 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 8), VERR_TIMEOUT);
149 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti,20), VERR_TIMEOUT);
150 RTTESTI_CHECK_RC(SUPSemEventMultiSignal(pSession, hEventMulti), VINF_SUCCESS);
151 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VINF_SUCCESS);
152 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VINF_SUCCESS);
153 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VINF_SUCCESS);
154 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 1), VINF_SUCCESS);
155 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 2), VINF_SUCCESS);
156 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 8), VINF_SUCCESS);
157 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti,20), VINF_SUCCESS);
158 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti,1000), VINF_SUCCESS);
159 RTTESTI_CHECK_RC(SUPSemEventMultiSignal(pSession, hEventMulti), VINF_SUCCESS);
160 RTTESTI_CHECK_RC(SUPSemEventMultiSignal(pSession, hEventMulti), VINF_SUCCESS);
161 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VINF_SUCCESS);
162 RTTESTI_CHECK_RC(SUPSemEventMultiReset(pSession, hEventMulti), VINF_SUCCESS);
163 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VERR_TIMEOUT);
164 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 1), VERR_TIMEOUT);
165 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 2), VERR_TIMEOUT);
166 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 8), VERR_TIMEOUT);
167 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti,20), VERR_TIMEOUT);
168 RTTESTI_CHECK_RC(SUPSemEventMultiSignal(pSession, hEventMulti), VINF_SUCCESS);
169 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VINF_SUCCESS);
170 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 1), VINF_SUCCESS);
171 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 2), VINF_SUCCESS);
172 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 8), VINF_SUCCESS);
173 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 20), VINF_SUCCESS);
174 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti,1000), VINF_SUCCESS);
175 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, hEventMulti), VINF_OBJECT_DESTROYED);
176 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, hEventMulti), VERR_INVALID_HANDLE);
177 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, NIL_SUPSEMEVENTMULTI), VINF_SUCCESS);
178
179#if !defined(RT_OS_OS2) && !defined(RT_OS_WINDOWS)
180 RTTestSub(hTest, "SRE Interruptibility");
181 RTTESTI_CHECK_RC(SUPSemEventCreate(pSession, &hEvent), VINF_SUCCESS);
182 g_cMillies = RT_INDEFINITE_WAIT;
183 RTTHREAD hThread = NIL_RTTHREAD;
184 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemInterruptibleSRE, (void *)hEvent, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "IntSRE"), VINF_SUCCESS);
185 RTTESTI_CHECK_RC(RTThreadUserWait(hThread, 60*1000), VINF_SUCCESS);
186 RTThreadSleep(120);
187 RTThreadPoke(hThread);
188 int rcThread = VINF_SUCCESS;
189 RTTESTI_CHECK_RC(RTThreadWait(hThread, 60*1000, &rcThread), VINF_SUCCESS);
190 RTTESTI_CHECK_RC(rcThread, VERR_INTERRUPTED);
191 RTTESTI_CHECK_RC(SUPSemEventClose(pSession, hEvent), VINF_OBJECT_DESTROYED);
192
193 RTTESTI_CHECK_RC(SUPSemEventCreate(pSession, &hEvent), VINF_SUCCESS);
194 g_cMillies = 120*1000;
195 hThread = NIL_RTTHREAD;
196 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemInterruptibleSRE, (void *)hEvent, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "IntSRE"), VINF_SUCCESS);
197 RTTESTI_CHECK_RC(RTThreadUserWait(hThread, 60*1000), VINF_SUCCESS);
198 RTThreadSleep(120);
199 RTThreadPoke(hThread);
200 rcThread = VINF_SUCCESS;
201 RTTESTI_CHECK_RC(RTThreadWait(hThread, 60*1000, &rcThread), VINF_SUCCESS);
202 RTTESTI_CHECK_RC(rcThread, VERR_INTERRUPTED);
203 RTTESTI_CHECK_RC(SUPSemEventClose(pSession, hEvent), VINF_OBJECT_DESTROYED);
204
205
206 RTTestSub(hTest, "MRE Interruptibility");
207 RTTESTI_CHECK_RC(SUPSemEventMultiCreate(pSession, &hEventMulti), VINF_SUCCESS);
208 g_cMillies = RT_INDEFINITE_WAIT;
209 hThread = NIL_RTTHREAD;
210 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemInterruptibleMRE, (void *)hEventMulti, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "IntMRE"), VINF_SUCCESS);
211 RTTESTI_CHECK_RC(RTThreadUserWait(hThread, 60*1000), VINF_SUCCESS);
212 RTThreadSleep(120);
213 RTThreadPoke(hThread);
214 rcThread = VINF_SUCCESS;
215 RTTESTI_CHECK_RC(RTThreadWait(hThread, 60*1000, &rcThread), VINF_SUCCESS);
216 RTTESTI_CHECK_RC(rcThread, VERR_INTERRUPTED);
217 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, hEventMulti), VINF_OBJECT_DESTROYED);
218
219 RTTESTI_CHECK_RC(SUPSemEventMultiCreate(pSession, &hEventMulti), VINF_SUCCESS);
220 g_cMillies = 120*1000;
221 hThread = NIL_RTTHREAD;
222 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemInterruptibleMRE, (void *)hEventMulti, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "IntMRE"), VINF_SUCCESS);
223 RTTESTI_CHECK_RC(RTThreadUserWait(hThread, 60*1000), VINF_SUCCESS);
224 RTThreadSleep(120);
225 RTThreadPoke(hThread);
226 rcThread = VINF_SUCCESS;
227 RTTESTI_CHECK_RC(RTThreadWait(hThread, 60*1000, &rcThread), VINF_SUCCESS);
228 RTTESTI_CHECK_RC(rcThread, VERR_INTERRUPTED);
229 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, hEventMulti), VINF_OBJECT_DESTROYED);
230
231 /*
232 * Fork test.
233 * Spawn a thread waiting for an event, then spawn a new child process (of
234 * ourselves) and make sure that this does not alter the intended behaviour
235 * of our event semaphore implementation (see #5090).
236 */
237 RTTestSub(hTest, "SRE Process Spawn");
238 hThread = NIL_RTTHREAD;
239 g_cMillies = 120*1000;
240 RTTESTI_CHECK_RC(SUPSemEventCreate(pSession, &hEvent), VINF_SUCCESS);
241 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemInterruptibleSRE, (void *)hEvent, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "IntSRE"), VINF_SUCCESS);
242
243 const char *apszArgs[3] = { argv[0], "child", NULL };
244 RTPROCESS Process = NIL_RTPROCESS;
245 RTThreadSleep(250);
246 RTTESTI_CHECK_RC(RTProcCreate(apszArgs[0], apszArgs, RTENV_DEFAULT, 0, &Process), VINF_SUCCESS);
247
248 RTThreadSleep(250);
249 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
250
251 rcThread = VERR_GENERAL_FAILURE;
252 RTTESTI_CHECK_RC(RTThreadWait(hThread, 120*1000, &rcThread), VINF_SUCCESS);
253 RTTESTI_CHECK_RC(rcThread, VINF_SUCCESS);
254 RTTESTI_CHECK_RC(SUPSemEventClose(pSession, hEvent), VINF_OBJECT_DESTROYED);
255
256
257 RTTestSub(hTest, "MRE Process Spawn");
258 hThread = NIL_RTTHREAD;
259 g_cMillies = 120*1000;
260 RTTESTI_CHECK_RC(SUPSemEventMultiCreate(pSession, &hEvent), VINF_SUCCESS);
261 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemInterruptibleMRE, (void *)hEvent, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "IntSRE"), VINF_SUCCESS);
262
263 RTTHREAD hThread2 = NIL_RTTHREAD;
264 RTTESTI_CHECK_RC(RTThreadCreate(&hThread2, tstSupSemInterruptibleMRE, (void *)hEvent, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "IntSRE"), VINF_SUCCESS);
265
266 Process = NIL_RTPROCESS;
267 RTThreadSleep(250);
268 RTTESTI_CHECK_RC(RTProcCreate(apszArgs[0], apszArgs, RTENV_DEFAULT, 0, &Process), VINF_SUCCESS);
269
270 RTThreadSleep(250);
271 RTTESTI_CHECK_RC(SUPSemEventMultiSignal(pSession, hEvent), VINF_SUCCESS);
272
273 rcThread = VERR_GENERAL_FAILURE;
274 RTTESTI_CHECK_RC(RTThreadWait(hThread, 120*1000, &rcThread), VINF_SUCCESS);
275 RTTESTI_CHECK_RC(rcThread, VINF_SUCCESS);
276
277 int rcThread2 = VERR_GENERAL_FAILURE;
278 RTTESTI_CHECK_RC(RTThreadWait(hThread2, 120*1000, &rcThread2), VINF_SUCCESS);
279 RTTESTI_CHECK_RC(rcThread2, VINF_SUCCESS);
280
281 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, hEvent), VINF_OBJECT_DESTROYED);
282
283#endif /* !OS2 && !WINDOWS */
284
285 if (RTTestErrorCount(hTest) == 0)
286 {
287 RTTestSub(hTest, "MRE Timeout Accuracy");
288 RTTESTI_CHECK_RC(SUPSemEventMultiCreate(pSession, &hEvent), VINF_SUCCESS);
289
290 static unsigned const s_acMsIntervals[] = { 0, 1, 2, 3, 4, 8, 10, 16, 32 };
291 for (unsigned i = 0; i < RT_ELEMENTS(s_acMsIntervals); i++)
292 {
293 uint64_t cMs = s_acMsIntervals[i];
294 uint64_t cNsMinSys = UINT64_MAX;
295 uint64_t cNsMin = UINT64_MAX;
296 uint64_t cNsTotalSys= 0;
297 uint64_t cNsTotal = 0;
298 for (unsigned j = 0; j < 10; j++)
299 {
300 uint64_t u64StartSys = RTTimeSystemNanoTS();
301 uint64_t u64Start = RTTimeNanoTS();
302 int rcX = SUPSemEventMultiWaitNoResume(pSession, hEvent, cMs);
303 if (rc == VERR_TIMEOUT)
304 RTTestFailed(hTest, "%Rrc j=%u cMs=%u", rcX, j, cMs);
305 uint64_t cNsElapsedSys = RTTimeSystemNanoTS() - u64StartSys;
306 uint64_t cNsElapsed = RTTimeNanoTS() - u64Start;
307 if (cNsElapsedSys < cNsMinSys)
308 cNsMinSys = cNsElapsedSys;
309 if (cNsElapsed < cNsMin)
310 cNsMin = cNsElapsed;
311 cNsTotalSys += cNsElapsedSys;
312 cNsTotal += cNsElapsed;
313 }
314 RTTestValueF(hTest, cNsMinSys, RTTESTUNIT_NS, "%u ms min (clock=sys)", cMs);
315 RTTestValueF(hTest, cNsTotalSys / 10, RTTESTUNIT_NS, "%u ms avg - (clock=sys)", cMs);
316 RTTestValueF(hTest, cNsMin, RTTESTUNIT_NS, "%u ms min (clock=gip)", cMs);
317 RTTestValueF(hTest, cNsTotal / 10, RTTESTUNIT_NS, "%u ms avg - (clock=gip)", cMs);
318 }
319
320 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, hEvent), VINF_OBJECT_DESTROYED);
321 }
322
323
324 /*
325 * Done.
326 */
327 return RTTestSummaryAndDestroy(hTest);
328}
329
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