VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/lib/testcase/tstVbglR0PhysHeap-1.cpp@ 97929

Last change on this file since 97929 was 97926, checked in by vboxsync, 2 years ago

Add/VBoxGuestR0LibPhysHeap.cpp: Restrict the number of free list entries we consider during allocation to prevent pointless search for a perfect match in a busy and fragmented heap.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.1 KB
Line 
1/* $Id: tstVbglR0PhysHeap-1.cpp 97926 2022-12-30 23:40:02Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Offset Based Heap.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/assert.h>
42#include <iprt/errcore.h>
43#include <iprt/initterm.h>
44#include <iprt/log.h>
45#include <iprt/mem.h>
46#include <iprt/rand.h>
47#include <iprt/stream.h>
48#include <iprt/string.h>
49#include <iprt/param.h>
50#include <iprt/test.h>
51#include <iprt/time.h>
52
53#define IN_TESTCASE
54#define IN_RING0 /* pretend we're in ring-0 so we get access to the functions */
55#include "../VBoxGuestR0LibInternal.h"
56
57
58/*********************************************************************************************************************************
59* Structures and Typedefs *
60*********************************************************************************************************************************/
61typedef struct
62{
63 uint32_t cb;
64 void *pv;
65} TSTHISTORYENTRY;
66
67
68/*********************************************************************************************************************************
69* Global Variables *
70*********************************************************************************************************************************/
71VBGLDATA g_vbgldata;
72
73int g_cChunks = 0;
74size_t g_cbChunks = 0;
75
76/** Drop-in replacement for RTMemContAlloc */
77static void *tstMemContAlloc(PRTCCPHYS pPhys, size_t cb)
78{
79 RTTESTI_CHECK(cb > 0);
80
81#define TST_MAX_CHUNKS 24
82 if (g_cChunks < TST_MAX_CHUNKS)
83 {
84 void *pvRet = RTMemAlloc(cb);
85 if (pvRet)
86 {
87 g_cChunks++;
88 g_cbChunks += cb;
89 *pPhys = (uint32_t)(uintptr_t)pvRet ^ (UINT32_C(0xf0f0f0f0) & ~(uint32_t)PAGE_OFFSET_MASK);
90
91 /* Avoid problematic values that won't happen in real life: */
92 if (!*pPhys)
93 *pPhys = 4U << PAGE_SHIFT;
94 if (UINT32_MAX - *pPhys < cb)
95 *pPhys -= RT_ALIGN_32(cb, PAGE_SIZE);
96
97 return pvRet;
98 }
99 }
100
101 *pPhys = NIL_RTCCPHYS;
102 return NULL;
103}
104
105
106/** Drop-in replacement for RTMemContFree */
107static void tstMemContFree(void *pv, size_t cb)
108{
109 RTTESTI_CHECK(RT_VALID_PTR(pv));
110 RTTESTI_CHECK(cb > 0);
111 RTTESTI_CHECK(g_cChunks > 0);
112 RTMemFree(pv);
113 g_cChunks--;
114 g_cbChunks -= cb;
115}
116
117
118#define RTMemContAlloc tstMemContAlloc
119#define RTMemContFree tstMemContFree
120#include "../VBoxGuestR0LibPhysHeap.cpp"
121
122
123static void PrintStats(TSTHISTORYENTRY const *paHistory, size_t cHistory, const char *pszDesc)
124{
125 size_t cbAllocated = 0;
126 unsigned cLargeBlocks = 0;
127 unsigned cAllocated = 0;
128 for (size_t i = 0; i < cHistory; i++)
129 if (paHistory[i].pv)
130 {
131 cAllocated += 1;
132 cbAllocated += paHistory[i].cb;
133 cLargeBlocks += paHistory[i].cb > _1K;
134 }
135
136 size_t const cbOverhead = g_cChunks * sizeof(VBGLPHYSHEAPCHUNK) + cAllocated * sizeof(VBGLPHYSHEAPBLOCK);
137 size_t const cbFragmentation = g_cbChunks - cbOverhead - cbAllocated;
138 RTTestIPrintf(RTTESTLVL_ALWAYS,
139 "%s: %'9zu bytes in %2d chunks; %'9zu bytes in %4u blocks (%2u large)\n"
140 " => int-frag %'9zu (%2zu.%1zu%%) overhead %'9zu (%1zu.%02zu%%)\n",
141 pszDesc,
142 g_cbChunks, g_cChunks,
143 cbAllocated, cAllocated, cLargeBlocks,
144 cbFragmentation, cbFragmentation * 100 / g_cbChunks, (cbFragmentation * 1000 / g_cbChunks) % 10,
145 cbOverhead, cbOverhead * 100 / g_cbChunks, (cbOverhead * 10000 / g_cbChunks) % 100);
146}
147
148
149int main(int argc, char **argv)
150{
151 RT_NOREF_PV(argc); RT_NOREF_PV(argv);
152
153 /*
154 * Init runtime.
155 */
156 RTTEST hTest;
157 int rc = RTTestInitAndCreate("tstVbglR0PhysHeap-1", &hTest);
158 if (rc)
159 return rc;
160 RTTestBanner(hTest);
161
162 /*
163 * Arguments are taken to be random seeding.
164 */
165 uint64_t uRandSeed = RTTimeNanoTS();
166 for (int i = 1; i < argc; i++)
167 {
168 rc = RTStrToUInt64Full(argv[i], 0, &uRandSeed);
169 if (rc != VINF_SUCCESS)
170 {
171 RTTestIFailed("Invalid parameter: %Rrc: %s\n", rc, argv[i]);
172 return RTTestSummaryAndDestroy(hTest);
173 }
174 }
175
176 /*
177 * Create a heap.
178 */
179 RTTestSub(hTest, "Basics");
180 RTTESTI_CHECK_RC(rc = VbglR0PhysHeapInit(), VINF_SUCCESS);
181 if (RT_FAILURE(rc))
182 return RTTestSummaryAndDestroy(hTest);
183
184#define CHECK_PHYS_ADDR(a_pv) do { \
185 uint32_t const uPhys = VbglR0PhysHeapGetPhysAddr(a_pv); \
186 if (uPhys == 0 || uPhys == UINT32_MAX || (uPhys & PAGE_OFFSET_MASK) != ((uintptr_t)(a_pv) & PAGE_OFFSET_MASK)) \
187 RTTestIFailed("line %u: %s=%p: uPhys=%#x\n", __LINE__, #a_pv, (a_pv), uPhys); \
188 } while (0)
189
190 /*
191 * Try allocate.
192 */
193 static struct TstPhysHeapOps
194 {
195 uint32_t cb;
196 unsigned iFreeOrder;
197 void *pvAlloc;
198 } s_aOps[] =
199 {
200 { 16, 0, NULL }, // 0
201 { 16, 1, NULL },
202 { 16, 2, NULL },
203 { 16, 5, NULL },
204 { 16, 4, NULL },
205 { 32, 3, NULL }, // 5
206 { 31, 6, NULL },
207 { 1024, 8, NULL },
208 { 1024, 10, NULL },
209 { 1024, 12, NULL },
210 { PAGE_SIZE, 13, NULL }, // 10
211 { 1024, 9, NULL },
212 { PAGE_SIZE, 11, NULL },
213 { PAGE_SIZE, 14, NULL },
214 { 16, 15, NULL },
215 { 9, 7, NULL }, // 15
216 { 16, 7, NULL },
217 { 36, 7, NULL },
218 { 16, 7, NULL },
219 { 12344, 7, NULL },
220 { 50, 7, NULL }, // 20
221 { 16, 7, NULL },
222 };
223 uint32_t i;
224 //RTHeapOffsetDump(Heap, (PFNRTHEAPOFFSETPRINTF)(uintptr_t)RTPrintf); /** @todo Add some detail info output with a signature identical to RTPrintf. */
225 //size_t cbBefore = VbglR0PhysHeapGetFreeSize();
226 static char const s_szFill[] = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
227
228 /* allocate */
229 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
230 {
231 s_aOps[i].pvAlloc = VbglR0PhysHeapAlloc(s_aOps[i].cb);
232 RTTESTI_CHECK_MSG(s_aOps[i].pvAlloc, ("VbglR0PhysHeapAlloc(%#x) -> NULL i=%d\n", s_aOps[i].cb, i));
233 if (!s_aOps[i].pvAlloc)
234 return RTTestSummaryAndDestroy(hTest);
235
236 memset(s_aOps[i].pvAlloc, s_szFill[i], s_aOps[i].cb);
237 RTTESTI_CHECK_MSG(RT_ALIGN_P(s_aOps[i].pvAlloc, sizeof(void *)) == s_aOps[i].pvAlloc,
238 ("VbglR0PhysHeapAlloc(%#x) -> %p\n", s_aOps[i].cb, i));
239
240 CHECK_PHYS_ADDR(s_aOps[i].pvAlloc);
241 }
242
243 /* free and allocate the same node again. */
244 for (i = 0; i < RT_ELEMENTS(s_aOps); i++)
245 {
246 if (!s_aOps[i].pvAlloc)
247 continue;
248 //RTPrintf("debug: i=%d pv=%#x cb=%#zx align=%#zx cbReal=%#zx\n", i, s_aOps[i].pvAlloc,
249 // s_aOps[i].cb, s_aOps[i].uAlignment, RTHeapOffsetSize(Heap, s_aOps[i].pvAlloc));
250 size_t cbBeforeSub = VbglR0PhysHeapGetFreeSize();
251 VbglR0PhysHeapFree(s_aOps[i].pvAlloc);
252 size_t cbAfterSubFree = VbglR0PhysHeapGetFreeSize();
253
254 void *pv;
255 pv = VbglR0PhysHeapAlloc(s_aOps[i].cb);
256 RTTESTI_CHECK_MSG(pv, ("VbglR0PhysHeapAlloc(%#x) -> NULL i=%d\n", s_aOps[i].cb, i));
257 if (!pv)
258 return RTTestSummaryAndDestroy(hTest);
259 CHECK_PHYS_ADDR(pv);
260
261 //RTPrintf("debug: i=%d pv=%p cbReal=%#zx cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx \n", i, pv, RTHeapOffsetSize(Heap, pv),
262 // cbBeforeSub, cbAfterSubFree, VbglR0PhysHeapGetFreeSize());
263
264 if (pv != s_aOps[i].pvAlloc)
265 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: Free+Alloc returned different address. new=%p old=%p i=%d\n", pv, s_aOps[i].pvAlloc, i);
266 s_aOps[i].pvAlloc = pv;
267 size_t cbAfterSubAlloc = VbglR0PhysHeapGetFreeSize();
268 if (cbBeforeSub != cbAfterSubAlloc)
269 {
270 RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: cbBeforeSub=%#zx cbAfterSubFree=%#zx cbAfterSubAlloc=%#zx. i=%d\n",
271 cbBeforeSub, cbAfterSubFree, cbAfterSubAlloc, i);
272 //return 1; - won't work correctly until we start creating free block instead of donating memory on alignment.
273 }
274 }
275
276 VbglR0PhysHeapTerminate();
277 RTTESTI_CHECK_MSG(g_cChunks == 0, ("g_cChunks=%d\n", g_cChunks));
278
279
280 /*
281 * Use random allocation pattern
282 */
283 RTTestSub(hTest, "Random Test");
284 RTTESTI_CHECK_RC(rc = VbglR0PhysHeapInit(), VINF_SUCCESS);
285 if (RT_FAILURE(rc))
286 return RTTestSummaryAndDestroy(hTest);
287
288 RTRAND hRand;
289 RTTESTI_CHECK_RC(rc = RTRandAdvCreateParkMiller(&hRand), VINF_SUCCESS);
290 if (RT_FAILURE(rc))
291 return RTTestSummaryAndDestroy(hTest);
292 RTRandAdvSeed(hRand, uRandSeed);
293 RTTestValue(hTest, "RandSeed", uRandSeed, RTTESTUNIT_NONE);
294
295 static TSTHISTORYENTRY s_aHistory[3072];
296 RT_ZERO(s_aHistory);
297
298 for (unsigned iTest = 0; iTest < 131072; iTest++)
299 {
300 i = RTRandAdvU32Ex(hRand, 0, RT_ELEMENTS(s_aHistory) - 1);
301 if (!s_aHistory[i].pv)
302 {
303 s_aHistory[i].cb = RTRandAdvU32Ex(hRand, 8, 1024);
304 s_aHistory[i].pv = VbglR0PhysHeapAlloc(s_aHistory[i].cb);
305 if (!s_aHistory[i].pv)
306 {
307 s_aHistory[i].cb = 9;
308 s_aHistory[i].pv = VbglR0PhysHeapAlloc(s_aHistory[i].cb);
309 }
310 if (s_aHistory[i].pv)
311 {
312 memset(s_aHistory[i].pv, 0xbb, s_aHistory[i].cb);
313 CHECK_PHYS_ADDR(s_aHistory[i].pv);
314 }
315 }
316 else
317 {
318 VbglR0PhysHeapFree(s_aHistory[i].pv);
319 s_aHistory[i].pv = NULL;
320 }
321
322#if 1
323 /* Check heap integrity: */
324 RTTESTI_CHECK_RC_OK(VbglR0PhysHeapCheck(NULL));
325 int cChunks = 0;
326 for (VBGLPHYSHEAPCHUNK *pCurChunk = g_vbgldata.pChunkHead; pCurChunk; pCurChunk = pCurChunk->pNext)
327 cChunks++;
328 RTTESTI_CHECK_MSG(cChunks == g_cChunks, ("g_cChunks=%u, but only %u chunks in the list!\n", g_cChunks, cChunks));
329#endif
330
331 if ((iTest % 7777) == 7776)
332 {
333 /* exhaust the heap */
334 PrintStats(s_aHistory, RT_ELEMENTS(s_aHistory), "Exhaust-pre ");
335
336 for (i = 0; i < RT_ELEMENTS(s_aHistory) && (VbglR0PhysHeapGetFreeSize() >= 256 || g_cChunks < TST_MAX_CHUNKS); i++)
337 if (!s_aHistory[i].pv)
338 {
339 s_aHistory[i].cb = RTRandAdvU32Ex(hRand, VBGL_PH_CHUNKSIZE / 8, VBGL_PH_CHUNKSIZE / 2 + VBGL_PH_CHUNKSIZE / 4);
340 s_aHistory[i].pv = VbglR0PhysHeapAlloc(s_aHistory[i].cb);
341 if (s_aHistory[i].pv)
342 {
343 memset(s_aHistory[i].pv, 0x55, s_aHistory[i].cb);
344 CHECK_PHYS_ADDR(s_aHistory[i].pv);
345 }
346 }
347
348 size_t cbFree = VbglR0PhysHeapGetFreeSize();
349 if (cbFree)
350 for (i = 0; i < RT_ELEMENTS(s_aHistory); i++)
351 if (!s_aHistory[i].pv)
352 {
353 s_aHistory[i].cb = RTRandAdvU32Ex(hRand, 1, (uint32_t)cbFree);
354 s_aHistory[i].pv = VbglR0PhysHeapAlloc(s_aHistory[i].cb);
355 while (s_aHistory[i].pv == NULL && s_aHistory[i].cb > 2)
356 {
357 s_aHistory[i].cb >>= 1;
358 s_aHistory[i].pv = VbglR0PhysHeapAlloc(s_aHistory[i].cb);
359 }
360 if (s_aHistory[i].pv)
361 {
362 memset(s_aHistory[i].pv, 0x55, s_aHistory[i].cb);
363 CHECK_PHYS_ADDR(s_aHistory[i].pv);
364 }
365
366 cbFree = VbglR0PhysHeapGetFreeSize();
367 if (!cbFree)
368 break;
369 }
370
371 RTTESTI_CHECK_MSG(VbglR0PhysHeapGetFreeSize() == 0, ("%zu\n", VbglR0PhysHeapGetFreeSize()));
372 PrintStats(s_aHistory, RT_ELEMENTS(s_aHistory), "Exhaust-post");
373 }
374 else if ((iTest % 7777) == 1111)
375 {
376 /* free all */
377 RTTestIPrintf(RTTESTLVL_ALWAYS, "Free-all-pre: cFreeBlocks=%u cAllocedBlocks=%u in %u chunk(s)\n",
378 g_vbgldata.acBlocks[0], g_vbgldata.acBlocks[1], g_cChunks);
379 for (i = 0; i < RT_ELEMENTS(s_aHistory); i++)
380 {
381 VbglR0PhysHeapFree(s_aHistory[i].pv);
382 s_aHistory[i].pv = NULL;
383 }
384 RTTestIPrintf(RTTESTLVL_ALWAYS, "Free-all-post: cFreeBlocks=%u in %u chunk(s)\n", g_vbgldata.acBlocks[0], g_cChunks);
385 RTTESTI_CHECK_MSG(g_cChunks == 1, ("g_cChunks=%d\n", g_cChunks));
386 RTTESTI_CHECK_MSG(g_vbgldata.acBlocks[1] == 0, ("g_vbgldata.acBlocks[1]=%d\n", g_vbgldata.acBlocks[0]));
387
388 //size_t cbAfterRand = VbglR0PhysHeapGetFreeSize();
389 //RTTESTI_CHECK_MSG(cbAfterRand == cbAfter, ("cbAfterRand=%zu cbAfter=%zu\n", cbAfterRand, cbAfter));
390 }
391 }
392
393 /* free the rest. */
394 for (i = 0; i < RT_ELEMENTS(s_aHistory); i++)
395 {
396 VbglR0PhysHeapFree(s_aHistory[i].pv);
397 s_aHistory[i].pv = NULL;
398 }
399
400 RTTESTI_CHECK_MSG(g_cChunks == 1, ("g_cChunks=%d\n", g_cChunks));
401
402 VbglR0PhysHeapTerminate();
403 RTTESTI_CHECK_MSG(g_cChunks == 0, ("g_cChunks=%d\n", g_cChunks));
404
405 RTTESTI_CHECK_RC(rc = RTRandAdvDestroy(hRand), VINF_SUCCESS);
406 return RTTestSummaryAndDestroy(hTest);
407}
408
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