VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstMMHyperHeap.cpp@ 22147

Last change on this file since 22147 was 20864, checked in by vboxsync, 15 years ago

SUP,*: API cleanup.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1/* $Id: tstMMHyperHeap.cpp 20864 2009-06-23 19:19:42Z vboxsync $ */
2/** @file
3 * MM Hypervisor Heap 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* Header Files *
24*******************************************************************************/
25#include <VBox/mm.h>
26#include <VBox/stam.h>
27#include <VBox/vm.h>
28#include <VBox/uvm.h>
29#include <VBox/sup.h>
30#include <VBox/param.h>
31#include <VBox/err.h>
32
33#include <VBox/log.h>
34#include <iprt/initterm.h>
35#include <iprt/mem.h>
36#include <iprt/assert.h>
37#include <iprt/stream.h>
38#include <iprt/string.h>
39
40/* does not work for more CPUs as SUPR3LowAlloc() would refuse to allocate more pages */
41#define NUM_CPUS 16
42
43
44int main(int argc, char **argv)
45{
46
47 /*
48 * Init runtime.
49 */
50 RTR3Init();
51
52 /*
53 * Create empty VM structure and call MMR3Init().
54 */
55 PVM pVM;
56 RTR0PTR pvR0;
57 SUPPAGE aPages[RT_ALIGN_Z(sizeof(*pVM) + NUM_CPUS * sizeof(VMCPU), PAGE_SIZE) >> PAGE_SHIFT];
58 int rc = SUPR3Init(NULL);
59 if (RT_SUCCESS(rc))
60 rc = SUPR3LowAlloc(RT_ELEMENTS(aPages), (void **)&pVM, &pvR0, &aPages[0]);
61 if (RT_FAILURE(rc))
62 {
63 RTPrintf("Fatal error: SUP Failure! rc=%Rrc\n", rc);
64 return 1;
65 }
66 memset(pVM, 0, sizeof(*pVM)); /* wtf? */
67 pVM->paVMPagesR3 = aPages;
68 pVM->pVMR0 = pvR0;
69
70 static UVM s_UVM;
71 PUVM pUVM = &s_UVM;
72 pUVM->pVM = pVM;
73 pVM->pUVM = pUVM;
74
75 pVM->cCPUs = NUM_CPUS;
76 pVM->cbSelf = RT_UOFFSETOF(VM, aCpus[pVM->cCPUs]);
77
78 rc = STAMR3InitUVM(pUVM);
79 if (RT_FAILURE(rc))
80 {
81 RTPrintf("FAILURE: STAMR3Init failed. rc=%Rrc\n", rc);
82 return 1;
83 }
84
85 rc = MMR3InitUVM(pUVM);
86 if (RT_FAILURE(rc))
87 {
88 RTPrintf("FAILURE: STAMR3Init failed. rc=%Rrc\n", rc);
89 return 1;
90 }
91
92 rc = MMR3Init(pVM);
93 if (RT_FAILURE(rc))
94 {
95 RTPrintf("Fatal error: MMR3Init failed! rc=%Rrc\n", rc);
96 return 1;
97 }
98
99 /*
100 * Try allocate.
101 */
102 static struct
103 {
104 size_t cb;
105 unsigned uAlignment;
106 void *pvAlloc;
107 unsigned iFreeOrder;
108 } aOps[] =
109 {
110 { 16, 0, NULL, 0 },
111 { 16, 4, NULL, 1 },
112 { 16, 8, NULL, 2 },
113 { 16, 16, NULL, 5 },
114 { 16, 32, NULL, 4 },
115 { 32, 0, NULL, 3 },
116 { 31, 0, NULL, 6 },
117 { 1024, 0, NULL, 8 },
118 { 1024, 32, NULL, 10 },
119 { 1024, 32, NULL, 12 },
120 { PAGE_SIZE, PAGE_SIZE, NULL, 13 },
121 { 1024, 32, NULL, 9 },
122 { PAGE_SIZE, 32, NULL, 11 },
123 { PAGE_SIZE, PAGE_SIZE, NULL, 14 },
124 { 16, 0, NULL, 15 },
125 { 9, 0, NULL, 7 },
126 { 16, 0, NULL, 7 },
127 { 36, 0, NULL, 7 },
128 { 16, 0, NULL, 7 },
129 { 12344, 0, NULL, 7 },
130 { 50, 0, NULL, 7 },
131 { 16, 0, NULL, 7 },
132 };
133 unsigned i;
134#ifdef DEBUG
135 MMHyperHeapDump(pVM);
136#endif
137 size_t cbBefore = MMHyperHeapGetFreeSize(pVM);
138 static char szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
139
140 /* allocate */
141 for (i = 0; i < RT_ELEMENTS(aOps); i++)
142 {
143 rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM, &aOps[i].pvAlloc);
144 if (RT_FAILURE(rc))
145 {
146 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
147 return 1;
148 }
149 memset(aOps[i].pvAlloc, szFill[i], aOps[i].cb);
150 if (RT_ALIGN_P(aOps[i].pvAlloc, (aOps[i].uAlignment ? aOps[i].uAlignment : 8)) != aOps[i].pvAlloc)
151 {
152 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %p, invalid alignment!\n", aOps[i].cb, aOps[i].uAlignment, aOps[i].pvAlloc);
153 return 1;
154 }
155 }
156
157 /* free and allocate the same node again. */
158 for (i = 0; i < RT_ELEMENTS(aOps); i++)
159 {
160 if ( !aOps[i].pvAlloc
161 || aOps[i].uAlignment == PAGE_SIZE)
162 continue;
163 //size_t cbBeforeSub = MMHyperHeapGetFreeSize(pVM);
164 rc = MMHyperFree(pVM, aOps[i].pvAlloc);
165 if (RT_FAILURE(rc))
166 {
167 RTPrintf("Failure: MMHyperFree(, %p,) -> %d i=%d\n", aOps[i].pvAlloc, rc, i);
168 return 1;
169 }
170 //RTPrintf("debug: i=%d cbBeforeSub=%d now=%d\n", i, cbBeforeSub, MMHyperHeapGetFreeSize(pVM));
171 void *pv;
172 rc = MMHyperAlloc(pVM, aOps[i].cb, aOps[i].uAlignment, MM_TAG_VM_REQ, &pv);
173 if (RT_FAILURE(rc))
174 {
175 RTPrintf("Failure: MMHyperAlloc(, %#x, %#x,) -> %d i=%d\n", aOps[i].cb, aOps[i].uAlignment, rc, i);
176 return 1;
177 }
178 if (pv != aOps[i].pvAlloc)
179 {
180 RTPrintf("Failure: Free+Alloc returned different address. new=%p old=%p i=%d (doesn't work with delayed free)\n", pv, aOps[i].pvAlloc, i);
181 //return 1;
182 }
183 aOps[i].pvAlloc = pv;
184 #if 0 /* won't work :/ */
185 size_t cbAfterSub = MMHyperHeapGetFreeSize(pVM);
186 if (cbBeforeSub != cbAfterSub)
187 {
188 RTPrintf("Failure: cbBeforeSub=%d cbAfterSub=%d. i=%d\n", cbBeforeSub, cbAfterSub, i);
189 return 1;
190 }
191 #endif
192 }
193
194 /* free it in a specific order. */
195 int cFreed = 0;
196 for (i = 0; i < RT_ELEMENTS(aOps); i++)
197 {
198 unsigned j;
199 for (j = 0; j < RT_ELEMENTS(aOps); j++)
200 {
201 if ( aOps[j].iFreeOrder != i
202 || !aOps[j].pvAlloc)
203 continue;
204 RTPrintf("j=%d i=%d free=%d cb=%d pv=%p\n", j, i, MMHyperHeapGetFreeSize(pVM), aOps[j].cb, aOps[j].pvAlloc);
205 if (aOps[j].uAlignment == PAGE_SIZE)
206 cbBefore -= aOps[j].cb;
207 else
208 {
209 rc = MMHyperFree(pVM, aOps[j].pvAlloc);
210 if (RT_FAILURE(rc))
211 {
212 RTPrintf("Failure: MMHyperFree(, %p,) -> %d j=%d i=%d\n", aOps[j].pvAlloc, rc, i, j);
213 return 1;
214 }
215 }
216 aOps[j].pvAlloc = NULL;
217 cFreed++;
218 }
219 }
220 Assert(cFreed == RT_ELEMENTS(aOps));
221 RTPrintf("i=done free=%d\n", MMHyperHeapGetFreeSize(pVM));
222
223 /* check that we're back at the right amount of free memory. */
224 size_t cbAfter = MMHyperHeapGetFreeSize(pVM);
225 if (cbBefore != cbAfter)
226 {
227 RTPrintf("Warning: Either we've split out an alignment chunk at the start, or we've got\n"
228 " an alloc/free accounting bug: cbBefore=%d cbAfter=%d\n", cbBefore, cbAfter);
229#ifdef DEBUG
230 MMHyperHeapDump(pVM);
231#endif
232 }
233
234 RTPrintf("tstMMHyperHeap: Success\n");
235#ifdef LOG_ENABLED
236 RTLogFlush(NULL);
237#endif
238 return 0;
239}
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