VirtualBox

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

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

VMM: %Vrc -> %Rrc, %Vra -> %Rra.

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