VirtualBox

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

Last change on this file since 48216 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

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