VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstVMM.cpp@ 19466

Last change on this file since 19466 was 19466, checked in by vboxsync, 16 years ago

tstVMM,CFGM: Hacked together a TM testcase in tstVMM.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.7 KB
Line 
1/* $Id: tstVMM.cpp 19466 2009-05-07 00:22:56Z 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.215389.xyz. 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/pdm.h>
30#include <VBox/err.h>
31#include <VBox/log.h>
32#include <iprt/assert.h>
33#include <iprt/initterm.h>
34#include <iprt/semaphore.h>
35#include <iprt/stream.h>
36#include <iprt/test.h>
37#include <iprt/getopt.h>
38#include <iprt/ctype.h>
39
40
41/*******************************************************************************
42* Defined Constants And Macros *
43*******************************************************************************/
44#define TESTCASE "tstVMM"
45
46
47
48/*******************************************************************************
49* Global Variables *
50*******************************************************************************/
51static uint32_t g_cCpus = 1;
52
53
54/*******************************************************************************
55* Internal Functions *
56*******************************************************************************/
57VMMR3DECL(int) VMMDoTest(PVM pVM); /* Linked into VMM, see ../VMMTests.cpp. */
58
59
60/** Dummy timer callback. */
61static DECLCALLBACK(void) tstTMDummyCallback(PVM pVM, PTMTIMER pTimer, void *pvUser)
62{
63 NOREF(pVM);
64 NOREF(pTimer);
65 NOREF(pvUser);
66}
67
68
69/**
70 * This is called on each EMT and will beat TM.
71 *
72 * @returns VINF_SUCCESS, test failure is reported via RTTEST.
73 * @param pVM The VM handle.
74 * @param hTest The test handle.
75 */
76DECLCALLBACK(int) tstTMWorker(PVM pVM, RTTEST hTest)
77{
78 VMCPUID idCpu = VMMGetCpuId(pVM);
79 RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "idCpu=%d STARTING\n", idCpu);
80
81 /*
82 * Create the test set.
83 */
84 int rc;
85 PTMTIMER apTimers[5];
86 for (size_t i = 0; i < RT_ELEMENTS(apTimers); i++)
87 {
88 int rc = TMR3TimerCreateInternal(pVM, i & 1 ? TMCLOCK_VIRTUAL : TMCLOCK_VIRTUAL_SYNC,
89 tstTMDummyCallback, NULL, "test timer", &apTimers[i]);
90 RTTEST_CHECK_RET(hTest, RT_SUCCESS(rc), rc);
91 }
92
93 /*
94 * The run loop.
95 */
96 unsigned uPrevPct = 0;
97 uint32_t const cLoops = 100000;
98 for (uint32_t iLoop = 0; iLoop < cLoops; iLoop++)
99 {
100 size_t cLeft = RT_ELEMENTS(apTimers);
101 unsigned i = iLoop % RT_ELEMENTS(apTimers);
102 while (cLeft-- > 0)
103 {
104 PTMTIMER pTimer = apTimers[i];
105
106 if (cLeft == RT_ELEMENTS(apTimers) / 2
107 && TMTimerIsActive(pTimer))
108 {
109 rc = TMTimerStop(pTimer);
110 RTTEST_CHECK_MSG(hTest, RT_SUCCESS(rc), (hTest, "TMTimerStop: %Rrc\n", rc));
111 }
112 else
113 {
114 rc = TMTimerSetMicro(pTimer, 50 + cLeft);
115 RTTEST_CHECK_MSG(hTest, RT_SUCCESS(rc), (hTest, "TMTimerSetMicro: %Rrc\n", rc));
116 }
117
118 /* next */
119 i = (i + 1) % RT_ELEMENTS(apTimers);
120 }
121
122 if (i % 3)
123 TMR3TimerQueuesDo(pVM);
124
125 /* Progress report. */
126 unsigned uPct = (unsigned)(100.0 * iLoop / cLoops);
127 if (uPct != uPrevPct)
128 {
129 uPrevPct = uPct;
130 if (!(uPct % 10))
131 RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "idCpu=%d - %3u%%\n", idCpu, uPct);
132 }
133 }
134
135 RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "idCpu=%d DONE\n", idCpu);
136 return 0;
137}
138
139
140/** PDMR3LdrEnumModules callback, see FNPDMR3ENUM. */
141static DECLCALLBACK(int)
142tstVMMLdrEnum(PVM pVM, const char *pszFilename, const char *pszName, RTUINTPTR ImageBase, size_t cbImage, bool fGC, void *pvUser)
143{
144 NOREF(pVM); NOREF(pszFilename); NOREF(fGC); NOREF(pvUser);
145 RTPrintf("tstVMM: %RTptr %s\n", ImageBase, pszName);
146 return VINF_SUCCESS;
147}
148
149static DECLCALLBACK(int)
150tstVMMConfigConstructor(PVM pVM, void *pvUser)
151{
152 int rc = CFGMR3ConstructDefaultTree(pVM);
153 if ( RT_SUCCESS(rc)
154 && g_cCpus > 1)
155 {
156 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
157 CFGMR3RemoveValue(pRoot, "NumCPUs");
158 rc = CFGMR3InsertInteger(pRoot, "NumCPUs", g_cCpus);
159 RTTESTI_CHECK_MSG_RET(RT_SUCCESS(rc), ("CFGMR3InsertInteger(pRoot,\"NumCPUs\",) -> %Rrc\n", rc), rc);
160
161 CFGMR3RemoveValue(pRoot, "HwVirtExtForced");
162 rc = CFGMR3InsertInteger(pRoot, "HwVirtExtForced", true);
163 RTTESTI_CHECK_MSG_RET(RT_SUCCESS(rc), ("CFGMR3InsertInteger(pRoot,\"HwVirtExtForced\",) -> %Rrc\n", rc), rc);
164
165 PCFGMNODE pHwVirtExt = CFGMR3GetChild(pRoot, "HWVirtExt");
166 CFGMR3RemoveNode(pHwVirtExt);
167 rc = CFGMR3InsertNode(pRoot, "HWVirtExt", &pHwVirtExt);
168 RTTESTI_CHECK_MSG_RET(RT_SUCCESS(rc), ("CFGMR3InsertNode(pRoot,\"HWVirtExt\",) -> %Rrc\n", rc), rc);
169 rc = CFGMR3InsertInteger(pHwVirtExt, "Enabled", true);
170 RTTESTI_CHECK_MSG_RET(RT_SUCCESS(rc), ("CFGMR3InsertInteger(pHwVirtExt,\"Enabled\",) -> %Rrc\n", rc), rc);
171 rc = CFGMR3InsertInteger(pHwVirtExt, "64bitEnabled", false);
172 RTTESTI_CHECK_MSG_RET(RT_SUCCESS(rc), ("CFGMR3InsertInteger(pHwVirtExt,\"64bitEnabled\",) -> %Rrc\n", rc), rc);
173 }
174 return rc;
175}
176
177
178int main(int argc, char **argv)
179{
180 /*
181 * Init runtime and the test environment.
182 */
183 int rc = RTR3InitAndSUPLib();
184 if (RT_FAILURE(rc))
185 {
186 RTPrintf("tstVMM: RTR3InitAndSUPLib failed: %Rrc\n", rc);
187 return 1;
188 }
189 RTTEST hTest;
190 rc = RTTestCreate("tstVMM", &hTest);
191 if (RT_FAILURE(rc))
192 {
193 RTPrintf("tstVMM: RTTestCreate failed: %Rrc\n", rc);
194 return 1;
195 }
196
197 /*
198 * Parse arguments.
199 */
200 static const RTGETOPTDEF s_aOptions[] =
201 {
202 { "--cpus", 'c', RTGETOPT_REQ_UINT8 },
203 { "--test", 't', RTGETOPT_REQ_STRING },
204 { "--help", 'h', 0 },
205 };
206 enum
207 {
208 kTstVMMTest_VMM, kTstVMMTest_TM
209 } enmTestOpt = kTstVMMTest_VMM;
210
211 int ch;
212 int i = 1;
213 RTGETOPTUNION ValueUnion;
214 RTGETOPTSTATE GetState;
215 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
216 while ((ch = RTGetOpt(&GetState, &ValueUnion)))
217 {
218 switch (ch)
219 {
220 case 'c':
221 g_cCpus = ValueUnion.u8;
222 break;
223
224 case 't':
225 if (!strcmp("vmm", ValueUnion.psz))
226 enmTestOpt = kTstVMMTest_VMM;
227 else if (!strcmp("tm", ValueUnion.psz))
228 enmTestOpt = kTstVMMTest_TM;
229 else
230 {
231 RTPrintf("tstVMM: unknown test: '%s'\n", ValueUnion.psz);
232 return 1;
233 }
234 break;
235
236 case 'h':
237 RTPrintf("usage: tstVMM [--cpus|-c cpus] [--test <vmm|tm>]\n");
238 return 1;
239
240 case VINF_GETOPT_NOT_OPTION:
241 RTPrintf("tstVMM: syntax error: non option '%s'\n", ValueUnion.psz);
242 break;
243
244 default:
245 if (ch > 0)
246 {
247 if (RT_C_IS_GRAPH(ch))
248 RTPrintf("tstVMM: unhandled option: -%c\n", ch);
249 else
250 RTPrintf("tstVMM: unhandled option: %i\n", ch);
251 }
252 else if (ch == VERR_GETOPT_UNKNOWN_OPTION)
253 RTPrintf("tstVMM: unknown option: %s\n", ValueUnion.psz);
254 else if (ValueUnion.pDef)
255 RTPrintf("tstVMM: %s: %Rrs\n", ValueUnion.pDef->pszLong, ch);
256 else
257 RTPrintf("tstVMM: %Rrs\n", ch);
258 return 1;
259 }
260 }
261
262 /*
263 * Create the test VM.
264 */
265 RTPrintf(TESTCASE ": Initializing...\n");
266 PVM pVM;
267 rc = VMR3Create(g_cCpus, NULL, NULL, tstVMMConfigConstructor, NULL, &pVM);
268 if (RT_SUCCESS(rc))
269 {
270 PDMR3LdrEnumModules(pVM, tstVMMLdrEnum, NULL);
271 RTStrmFlush(g_pStdOut);
272 RTThreadSleep(256);
273
274 /*
275 * Do the requested testing.
276 */
277 switch (enmTestOpt)
278 {
279 case kTstVMMTest_VMM:
280 {
281 RTTestSub(hTest, "VMM");
282 PVMREQ pReq1 = NULL;
283 rc = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq1, RT_INDEFINITE_WAIT, (PFNRT)VMMDoTest, 1, pVM);
284 if (RT_FAILURE(rc))
285 RTTestFailed(hTest, "VMR3ReqCall failed: rc=%Rrc\n", rc);
286 else if (RT_FAILURE(pReq1->iStatus))
287 RTTestFailed(hTest, "VMMDoTest failed: rc=%Rrc\n", rc);
288 VMR3ReqFree(pReq1);
289 break;
290 }
291
292 case kTstVMMTest_TM:
293 {
294 RTTestSub(hTest, "TM");
295 for (VMCPUID idCpu = 1; idCpu < g_cCpus; idCpu++)
296 {
297 PVMREQ pReq = NULL;
298 rc = VMR3ReqCallU(pVM->pUVM, idCpu, &pReq, 0, VMREQFLAGS_NO_WAIT, (PFNRT)tstTMWorker, 2, pVM, hTest);
299 if (RT_FAILURE(rc))
300 RTTestFailed(hTest, "VMR3ReqCall failed: rc=%Rrc\n", rc);
301 }
302
303 PVMREQ pReq1 = NULL;
304 rc = VMR3ReqCall(pVM, 0, &pReq1, RT_INDEFINITE_WAIT, (PFNRT)tstTMWorker, 2, pVM, hTest);
305 if (RT_FAILURE(rc))
306 RTTestFailed(hTest, "VMR3ReqCall failed: rc=%Rrc\n", rc);
307 else if (RT_FAILURE(pReq1->iStatus))
308 RTTestFailed(hTest, "VMMDoTest failed: rc=%Rrc\n", rc);
309 VMR3ReqFree(pReq1);
310 break;
311 }
312 }
313
314 STAMR3Dump(pVM, "*");
315
316 /*
317 * Cleanup.
318 */
319 rc = VMR3Destroy(pVM);
320 if (RT_FAILURE(rc))
321 RTTestFailed(hTest, "VMR3Destroy failed: rc=%Rrc\n", rc);
322 }
323 else
324 RTTestFailed(hTest, "VMR3Create failed: rc=%Rrc\n", rc);
325
326 return RTTestSummaryAndDestroy(hTest);
327}
Note: See TracBrowser for help on using the repository browser.

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