VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTR0SemMutexDriver.cpp@ 28472

最後變更 在這個檔案從28472是 28472,由 vboxsync 提交於 15 年 前

tstRTR0SemMutex: More tests.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 11.0 KB
 
1/* $Id: tstRTR0SemMutexDriver.cpp 28472 2010-04-19 14:59:33Z vboxsync $ */
2/** @file
3 * IPRT R0 Testcase - Thread Preemption, driver program.
4 */
5
6/*
7 * Copyright (C) 2009-2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/initterm.h>
35
36#include <iprt/err.h>
37#include <iprt/path.h>
38#include <iprt/param.h>
39#include <iprt/stream.h>
40#include <iprt/string.h>
41#include <iprt/test.h>
42#include <iprt/thread.h>
43#ifdef VBOX
44# include <VBox/sup.h>
45# include "tstRTR0SemMutex.h"
46#endif
47
48/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51typedef struct TSTRTR0SEMMUTEXREQ
52{
53 SUPR0SERVICEREQHDR Hdr;
54 char szMsg[256];
55} TSTRTR0SEMMUTEXREQ;
56typedef TSTRTR0SEMMUTEXREQ *PTSTRTR0SEMMUTEXREQ;
57
58
59/*******************************************************************************
60* Global Variables *
61*******************************************************************************/
62static RTTEST g_hTest;
63
64
65/**
66 * Thread function employed by tstDoThreadedTest.
67 *
68 * @returns IPRT status code.
69 * @param hThreadSelf The thread.
70 * @param pvUser The operation to perform.
71 */
72static DECLCALLBACK(int) tstThreadFn(RTTHREAD hThreadSelf, void *pvUser)
73{
74 uint32_t u32 = (uint32_t)(uintptr_t)pvUser;
75 TSTRTR0SEMMUTEX enmDo = (TSTRTR0SEMMUTEX)RT_LOWORD(u32);
76 uint32_t cSecs = RT_HIWORD(u32);
77 TSTRTR0SEMMUTEXREQ Req;
78 RT_ZERO(Req);
79 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
80 Req.Hdr.cbReq = sizeof(Req);
81 Req.szMsg[0] = '\0';
82 RTTEST_CHECK_RC_RET(g_hTest, SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
83 enmDo, cSecs, &Req.Hdr),
84 VINF_SUCCESS,
85 rcCheck);
86 return VINF_SUCCESS;
87}
88
89/**
90 * Performs a threaded test.
91 *
92 * @returns true on succes, false on failure.
93 * @param enmSetup The setup operation number.
94 * @param enmDo The do-it operation number.
95 * @param enmCleanup The cleanup operation number.
96 * @param cThreads The number of threads.
97 * @param pReq The request structure.
98 * @param pszTest The test name.
99 */
100static bool tstDoThreadedTest(TSTRTR0SEMMUTEX enmSetup, TSTRTR0SEMMUTEX enmDo, TSTRTR0SEMMUTEX enmCleanup,
101 unsigned cThreads, unsigned cSecs, PTSTRTR0SEMMUTEXREQ pReq, const char *pszTest)
102{
103 RTTestSubF(g_hTest, "%s - %u threads", pszTest, cThreads);
104 RTTHREAD ahThreads[32];
105 RTTESTI_CHECK_RET(cThreads <= RT_ELEMENTS(ahThreads), false);
106
107 /*
108 * Setup.
109 */
110 pReq->Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
111 pReq->Hdr.cbReq = sizeof(*pReq);
112 pReq->szMsg[0] = '\0';
113 RTTESTI_CHECK_RC_RET(SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1, enmSetup, 0, &pReq->Hdr),
114 VINF_SUCCESS, false);
115 if (pReq->szMsg[0] == '!')
116 return RTTestIFailedRc(false, "%s", &pReq->szMsg[1]);
117 if (pReq->szMsg[0])
118 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", pReq->szMsg);
119
120 /*
121 * Test execution.
122 */
123 for (unsigned i = 0; i < RT_ELEMENTS(ahThreads); i++)
124 ahThreads[i] = NIL_RTTHREAD;
125
126 int rc = VINF_SUCCESS;
127 for (unsigned i = 0; i < cThreads && RT_SUCCESS(rc); i++)
128 rc = RTThreadCreateF(&ahThreads[i], tstThreadFn, (void *)(uintptr_t)RT_MAKE_U32(enmDo, cSecs), 0, RTTHREADTYPE_DEFAULT,
129 RTTHREADFLAGS_WAITABLE, "test-%u", i);
130
131 for (unsigned i = 0; i < cThreads; i++)
132 if (ahThreads[i] != NIL_RTTHREAD)
133 {
134 int rcThread = VINF_SUCCESS;
135 int rc2 = RTThreadWait(ahThreads[i], 3600*1000, &rcThread);
136 if (RT_SUCCESS(rc2))
137 {
138 ahThreads[i] = NIL_RTTHREAD;
139 if (RT_FAILURE(rcThread) && RT_SUCCESS(rc2))
140 rc = rcThread;
141 }
142 else if (RT_SUCCESS(rc))
143 rc = rc2;
144 }
145
146 /*
147 * Cleanup.
148 */
149 pReq->Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
150 pReq->Hdr.cbReq = sizeof(*pReq);
151 pReq->szMsg[0] = '\0';
152 RTTESTI_CHECK_RC_RET(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1, enmCleanup, 0, &pReq->Hdr),
153 VINF_SUCCESS, false);
154 if (pReq->szMsg[0] == '!')
155 return RTTestIFailedRc(false, "%s", &pReq->szMsg[1]);
156 if (pReq->szMsg[0])
157 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", pReq->szMsg);
158
159 if (RT_FAILURE(rc))
160 for (unsigned i = 0; i < cThreads; i++)
161 if (ahThreads[i] != NIL_RTTHREAD)
162 RTThreadWait(ahThreads[i], 1000, NULL);
163
164 return RT_SUCCESS(rc);
165}
166
167
168int main(int argc, char **argv)
169{
170#ifndef VBOX
171 RTPrintf("tstSup: SKIPPED\n");
172 return 0;
173#else
174 /*
175 * Init.
176 */
177 RTTEST hTest;
178 int rc = RTTestInitAndCreate("tstRTR0SemMutex", &hTest);
179 if (rc)
180 return rc;
181 RTTestBanner(hTest);
182
183 PSUPDRVSESSION pSession;
184 rc = SUPR3Init(&pSession);
185 if (RT_FAILURE(rc))
186 {
187 RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
188 return RTTestSummaryAndDestroy(hTest);
189 }
190
191 char szPath[RTPATH_MAX];
192 rc = RTPathExecDir(szPath, sizeof(szPath));
193 if (RT_SUCCESS(rc))
194 rc = RTPathAppend(szPath, sizeof(szPath), "tstRTR0SemMutex.r0");
195 if (RT_FAILURE(rc))
196 {
197 RTTestFailed(hTest, "Failed constructing .r0 filename (rc=%Rrc)", rc);
198 return RTTestSummaryAndDestroy(hTest);
199 }
200
201 void *pvImageBase;
202 rc = SUPR3LoadServiceModule(szPath, "tstRTR0SemMutex",
203 "TSTRTR0SemMutexSrvReqHandler",
204 &pvImageBase);
205 if (RT_FAILURE(rc))
206 {
207 RTTestFailed(hTest, "SUPR3LoadServiceModule(%s,,,) failed with rc=%Rrc\n", szPath, rc);
208 return RTTestSummaryAndDestroy(hTest);
209 }
210
211 /* test request */
212 TSTRTR0SEMMUTEXREQ Req;
213
214 /*
215 * Sanity checks.
216 */
217 RTTestSub(hTest, "Sanity");
218 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
219 Req.Hdr.cbReq = sizeof(Req);
220 Req.szMsg[0] = '\0';
221 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
222 TSTRTR0SEMMUTEX_SANITY_OK, 0, &Req.Hdr), VINF_SUCCESS);
223 if (RT_FAILURE(rc))
224 return RTTestSummaryAndDestroy(hTest);
225 RTTESTI_CHECK_MSG(Req.szMsg[0] == '\0', ("%s", Req.szMsg));
226 if (Req.szMsg[0] != '\0')
227 return RTTestSummaryAndDestroy(hTest);
228
229 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
230 Req.Hdr.cbReq = sizeof(Req);
231 Req.szMsg[0] = '\0';
232 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
233 TSTRTR0SEMMUTEX_SANITY_FAILURE, 0, &Req.Hdr), VINF_SUCCESS);
234 if (RT_FAILURE(rc))
235 return RTTestSummaryAndDestroy(hTest);
236 RTTESTI_CHECK_MSG(!strncmp(Req.szMsg, "!42failure42", sizeof("!42failure42") - 1), ("%s", Req.szMsg));
237 if (strncmp(Req.szMsg, "!42failure42", sizeof("!42failure42") - 1))
238 return RTTestSummaryAndDestroy(hTest);
239
240 /*
241 * Basic tests, bail out on failure.
242 */
243 RTTestSub(hTest, "Basics");
244 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
245 Req.Hdr.cbReq = sizeof(Req);
246 Req.szMsg[0] = '\0';
247 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0SemMutex", sizeof("tstRTR0SemMutex") - 1,
248 TSTRTR0SEMMUTEX_BASIC, 0, &Req.Hdr), VINF_SUCCESS);
249 if (RT_FAILURE(rc))
250 return RTTestSummaryAndDestroy(hTest);
251 if (Req.szMsg[0] == '!')
252 {
253 RTTestIFailed("%s", &Req.szMsg[1]);
254 return RTTestSummaryAndDestroy(hTest);
255 }
256 if (Req.szMsg[0])
257 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
258
259 /*
260 * Tests with multiple threads for bugs in the contention part of the code.
261 * Test #2: Try to hold the semaphore for 1 ms.
262 * Test #3: Grab and release immediately.
263 * Test #4: Timeout checks. Try grab it for 0-32 ms and hold it for 1 s.
264 */
265 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 1, 1, &Req, "test #2");
266 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 2, 3, &Req, "test #2");
267 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 3, 3, &Req, "test #2");
268 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST2_SETUP, TSTRTR0SEMMUTEX_TEST2_DO, TSTRTR0SEMMUTEX_TEST2_CLEANUP, 9, 3, &Req, "test #2");
269
270 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 1, 1, &Req, "test #3");
271 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 2, 3, &Req, "test #3");
272 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 3, 3, &Req, "test #3");
273 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST3_SETUP, TSTRTR0SEMMUTEX_TEST3_DO, TSTRTR0SEMMUTEX_TEST3_CLEANUP, 9, 3, &Req, "test #3");
274
275 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 1, 1, &Req, "test #4");
276 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 2, 3, &Req, "test #4");
277 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 3, 3, &Req, "test #4");
278 tstDoThreadedTest(TSTRTR0SEMMUTEX_TEST4_SETUP, TSTRTR0SEMMUTEX_TEST4_DO, TSTRTR0SEMMUTEX_TEST4_CLEANUP, 9, 3, &Req, "test #4");
279
280 /*
281 * Done.
282 */
283 return RTTestSummaryAndDestroy(hTest);
284#endif
285}
286
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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