VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstVMREQ.cpp@ 25517

Last change on this file since 25517 was 23012, checked in by vboxsync, 15 years ago

VMM,Devices,Main: VMR3ReqCall w/ RT_INDEFINITE_WAIT -> VMR3ReqCallWait.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.7 KB
Line 
1/* $Id: tstVMREQ.cpp 23012 2009-09-14 16:38:13Z vboxsync $ */
2/** @file
3 * VMM Testcase.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include <VBox/vm.h>
27#include <VBox/vmm.h>
28#include <VBox/cpum.h>
29#include <VBox/err.h>
30#include <VBox/log.h>
31#include <iprt/assert.h>
32#include <iprt/initterm.h>
33#include <iprt/semaphore.h>
34#include <iprt/stream.h>
35#include <iprt/string.h>
36#include <iprt/thread.h>
37#include <iprt/time.h>
38
39
40/*******************************************************************************
41* Defined Constants And Macros *
42*******************************************************************************/
43#define TESTCASE "tstVMREQ"
44
45/*******************************************************************************
46* Global Variables *
47*******************************************************************************/
48/** the error count. */
49static int g_cErrors = 0;
50
51
52/**
53 * Testings va_list passing in VMSetRuntimeError.
54 */
55static DECLCALLBACK(void) MyAtRuntimeError(PVM pVM, void *pvUser, uint32_t fFlags, const char *pszErrorId, const char *pszFormat, va_list va)
56{
57 if (strcmp((const char *)pvUser, "user argument"))
58 {
59 RTPrintf(TESTCASE ": pvUser=%p:{%s}!\n", pvUser, (const char *)pvUser);
60 g_cErrors++;
61 }
62 if (fFlags)
63 {
64 RTPrintf(TESTCASE ": fFlags=%#x!\n", fFlags);
65 g_cErrors++;
66 }
67 if (strcmp(pszErrorId, "enum"))
68 {
69 RTPrintf(TESTCASE ": pszErrorId=%p:{%s}!\n", pszErrorId, pszErrorId);
70 g_cErrors++;
71 }
72 if (strcmp(pszFormat, "some %s string"))
73 {
74 RTPrintf(TESTCASE ": pszFormat=%p:{%s}!\n", pszFormat, pszFormat);
75 g_cErrors++;
76 }
77
78 char szBuf[1024];
79 RTStrPrintfV(szBuf, sizeof(szBuf), pszFormat, va);
80 if (strcmp(szBuf, "some error string"))
81 {
82 RTPrintf(TESTCASE ": RTStrPrintfV -> '%s'!\n", szBuf);
83 g_cErrors++;
84 }
85}
86
87
88/**
89 * The function PassVA and PassVA2 calls.
90 */
91static DECLCALLBACK(int) PassVACallback(PVM pVM, unsigned u4K, unsigned u1G, const char *pszFormat, va_list *pva)
92{
93 if (u4K != _4K)
94 {
95 RTPrintf(TESTCASE ": u4K=%#x!\n", u4K);
96 g_cErrors++;
97 }
98 if (u1G != _1G)
99 {
100 RTPrintf(TESTCASE ": u1G=%#x!\n", u1G);
101 g_cErrors++;
102 }
103
104 if (strcmp(pszFormat, "hello %s"))
105 {
106 RTPrintf(TESTCASE ": pszFormat=%p:{%s}!\n", pszFormat, pszFormat);
107 g_cErrors++;
108 }
109
110 char szBuf[1024];
111 RTStrPrintfV(szBuf, sizeof(szBuf), pszFormat, *pva);
112 if (strcmp(szBuf, "hello world"))
113 {
114 RTPrintf(TESTCASE ": RTStrPrintfV -> '%s'!\n", szBuf);
115 g_cErrors++;
116 }
117
118 return VINF_SUCCESS;
119}
120
121
122/**
123 * Functions that tests passing a va_list * argument in a request,
124 * similar to VMSetRuntimeError.
125 */
126static void PassVA2(PVM pVM, const char *pszFormat, va_list va)
127{
128#if 0 /** @todo test if this is a GCC problem only or also happens with AMD64+VCC80... */
129 void *pvVA = &va;
130#else
131 va_list va2;
132 va_copy(va2, va);
133 void *pvVA = va2;
134#endif
135
136 int rc = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)PassVACallback, 5, pVM, _4K, _1G, pszFormat, pvVA);
137 NOREF(rc);
138
139#if 1
140 va_end(va2);
141#endif
142}
143
144
145/**
146 * Functions that tests passing a va_list * argument in a request,
147 * similar to VMSetRuntimeError.
148 */
149static void PassVA(PVM pVM, const char *pszFormat, ...)
150{
151 /* 1st test */
152 va_list va1;
153 va_start(va1, pszFormat);
154 int rc = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)PassVACallback, 5, pVM, _4K, _1G, pszFormat, &va1);
155 va_end(va1);
156 NOREF(rc);
157
158 /* 2nd test */
159 va_list va2;
160 va_start(va2, pszFormat);
161 PassVA2(pVM, pszFormat, va2);
162 va_end(va2);
163}
164
165
166/**
167 * Thread function which allocates and frees requests like wildfire.
168 */
169static DECLCALLBACK(int) Thread(RTTHREAD Thread, void *pvUser)
170{
171 int rc = VINF_SUCCESS;
172 PVM pVM = (PVM)pvUser;
173 for (unsigned i = 0; i < 100000; i++)
174 {
175 PVMREQ apReq[17];
176 const unsigned cReqs = i % RT_ELEMENTS(apReq);
177 unsigned iReq;
178 for (iReq = 0; iReq < cReqs; iReq++)
179 {
180 rc = VMR3ReqAlloc(pVM, &apReq[iReq], VMREQTYPE_INTERNAL, VMCPUID_ANY);
181 if (RT_FAILURE(rc))
182 {
183 RTPrintf(TESTCASE ": i=%d iReq=%d cReqs=%d rc=%Rrc (alloc)\n", i, iReq, cReqs, rc);
184 return rc;
185 }
186 apReq[iReq]->iStatus = iReq + i;
187 }
188
189 for (iReq = 0; iReq < cReqs; iReq++)
190 {
191 if (apReq[iReq]->iStatus != (int)(iReq + i))
192 {
193 RTPrintf(TESTCASE ": i=%d iReq=%d cReqs=%d: iStatus=%d != %d\n", i, iReq, cReqs, apReq[iReq]->iStatus, iReq + i);
194 return VERR_GENERAL_FAILURE;
195 }
196 rc = VMR3ReqFree(apReq[iReq]);
197 if (RT_FAILURE(rc))
198 {
199 RTPrintf(TESTCASE ": i=%d iReq=%d cReqs=%d rc=%Rrc (free)\n", i, iReq, cReqs, rc);
200 return rc;
201 }
202 }
203 //if (!(i % 10000))
204 // RTPrintf(TESTCASE ": i=%d\n", i);
205 }
206
207 return VINF_SUCCESS;
208}
209
210
211
212int main(int argc, char **argv)
213{
214 RTR3InitAndSUPLib();
215 RTPrintf(TESTCASE ": TESTING...\n");
216
217 /*
218 * Create empty VM.
219 */
220 PVM pVM;
221 int rc = VMR3Create(1, NULL, NULL, NULL, NULL, &pVM);
222 if (RT_SUCCESS(rc))
223 {
224 /*
225 * Do testing.
226 */
227 uint64_t u64StartTS = RTTimeNanoTS();
228 RTTHREAD Thread0;
229 rc = RTThreadCreate(&Thread0, Thread, pVM, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "REQ1");
230 if (RT_SUCCESS(rc))
231 {
232 RTTHREAD Thread1;
233 rc = RTThreadCreate(&Thread1, Thread, pVM, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "REQ1");
234 if (RT_SUCCESS(rc))
235 {
236 int rcThread1;
237 rc = RTThreadWait(Thread1, RT_INDEFINITE_WAIT, &rcThread1);
238 if (RT_FAILURE(rc))
239 {
240 RTPrintf(TESTCASE ": RTThreadWait(Thread1,,) failed, rc=%Rrc\n", rc);
241 g_cErrors++;
242 }
243 if (RT_FAILURE(rcThread1))
244 g_cErrors++;
245 }
246 else
247 {
248 RTPrintf(TESTCASE ": RTThreadCreate(&Thread1,,,,) failed, rc=%Rrc\n", rc);
249 g_cErrors++;
250 }
251
252 int rcThread0;
253 rc = RTThreadWait(Thread0, RT_INDEFINITE_WAIT, &rcThread0);
254 if (RT_FAILURE(rc))
255 {
256 RTPrintf(TESTCASE ": RTThreadWait(Thread1,,) failed, rc=%Rrc\n", rc);
257 g_cErrors++;
258 }
259 if (RT_FAILURE(rcThread0))
260 g_cErrors++;
261 }
262 else
263 {
264 RTPrintf(TESTCASE ": RTThreadCreate(&Thread0,,,,) failed, rc=%Rrc\n", rc);
265 g_cErrors++;
266 }
267 uint64_t u64ElapsedTS = RTTimeNanoTS() - u64StartTS;
268 RTPrintf(TESTCASE ": %llu ns elapsed\n", u64ElapsedTS);
269
270 /*
271 * Print stats.
272 */
273 STAMR3Print(pVM, "/VM/Req/*");
274
275 /*
276 * Testing va_list fun.
277 */
278 RTPrintf(TESTCASE ": va_list argument test...\n");
279 PassVA(pVM, "hello %s", "world");
280 VMR3AtRuntimeErrorRegister(pVM, MyAtRuntimeError, (void *)"user argument");
281 VMSetRuntimeError(pVM, 0 /*fFlags*/, "enum", "some %s string", "error");
282
283 /*
284 * Cleanup.
285 */
286 rc = VMR3Destroy(pVM);
287 if (!RT_SUCCESS(rc))
288 {
289 RTPrintf(TESTCASE ": error: failed to destroy vm! rc=%Rrc\n", rc);
290 g_cErrors++;
291 }
292 }
293 else
294 {
295 RTPrintf(TESTCASE ": fatal error: failed to create vm! rc=%Rrc\n", rc);
296 g_cErrors++;
297 }
298
299 /*
300 * Summary and return.
301 */
302 if (!g_cErrors)
303 RTPrintf(TESTCASE ": SUCCESS\n");
304 else
305 RTPrintf(TESTCASE ": FAILURE - %d errors\n", g_cErrors);
306
307 return !!g_cErrors;
308}
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