VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTMemCache.cpp@ 28260

Last change on this file since 28260 was 27302, checked in by vboxsync, 15 years ago

IPRT: RIP RTCache*, RTMemCache* is way better...

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.5 KB
Line 
1/* $Id: tstRTMemCache.cpp 27302 2010-03-11 20:11:17Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTMemCache.
4 */
5
6/*
7 * Copyright (C) 2010 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/memcache.h>
35
36#include <iprt/asm.h>
37#include <iprt/err.h>
38#include <iprt/initterm.h>
39#include <iprt/mem.h>
40#include <iprt/param.h>
41#include <iprt/rand.h>
42#include <iprt/string.h>
43#include <iprt/semaphore.h>
44#include <iprt/test.h>
45#include <iprt/time.h>
46#include <iprt/thread.h>
47
48
49/*******************************************************************************
50* Structures and Typedefs *
51*******************************************************************************/
52typedef struct TST3THREAD
53{
54 RTTHREAD hThread;
55 RTSEMEVENTMULTI hEvt;
56 uint64_t volatile cIterations;
57 uint32_t cbObject;
58 bool fUseCache;
59} TST3THREAD, *PTST3THREAD;
60
61
62/*******************************************************************************
63* Global Variables *
64*******************************************************************************/
65/** The test handle */
66static RTTEST g_hTest;
67/** Global mem cache handle for use in some of the testcases. */
68static RTMEMCACHE g_hMemCache;
69/** Stop indicator for tst3 threads. */
70static bool volatile g_fTst3Stop;
71
72
73/**
74 * Basic API checks.
75 * We'll return if any of these fails.
76 */
77static void tst1(void)
78{
79 RTTestISub("Basics");
80
81 /* Create one without constructor or destructor. */
82 uint32_t const cObjects = PAGE_SIZE * 2 / 256;
83 RTMEMCACHE hMemCache;
84 RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&hMemCache, 256, cObjects, 32, NULL, NULL, NULL, 0 /*fFlags*/), VINF_SUCCESS);
85 RTTESTI_CHECK_RETV(hMemCache != NIL_RTMEMCACHE);
86
87 /* Allocate a bit and free it again. */
88 void *pv = NULL;
89 RTTESTI_CHECK_RC_RETV(RTMemCacheAllocEx(hMemCache, &pv), VINF_SUCCESS);
90 RTTESTI_CHECK_RETV(pv != NULL);
91 RTTESTI_CHECK_RETV(RT_ALIGN_P(pv, 32) == pv);
92 RTMemCacheFree(hMemCache, pv);
93
94 RTTESTI_CHECK((pv = RTMemCacheAlloc(hMemCache)) != NULL);
95 RTMemCacheFree(hMemCache, pv);
96
97 /* Allocate everything and free it again, checking size constraints. */
98 for (uint32_t iLoop = 0; iLoop < 20; iLoop++)
99 {
100 /* Allocate everything. */
101 void *apv[cObjects];
102 for (uint32_t i = 0; i < cObjects; i++)
103 {
104 apv[i] = NULL;
105 RTTESTI_CHECK_RC(RTMemCacheAllocEx(hMemCache, &apv[i]), VINF_SUCCESS);
106 }
107
108 /* Check that we've got it all. */
109 int rc;
110 RTTESTI_CHECK_RC(rc = RTMemCacheAllocEx(hMemCache, &pv), VERR_MEM_CACHE_MAX_SIZE);
111 if (RT_SUCCESS(rc))
112 RTMemCacheFree(hMemCache, pv);
113
114 RTTESTI_CHECK((pv = RTMemCacheAlloc(hMemCache)) == NULL);
115 RTMemCacheFree(hMemCache, pv);
116
117 /* Free all the allocations. */
118 for (uint32_t i = 0; i < cObjects; i++)
119 {
120 RTMemCacheFree(hMemCache, apv[i]);
121
122 RTTESTI_CHECK((pv = RTMemCacheAlloc(hMemCache)) != NULL);
123 RTMemCacheFree(hMemCache, pv);
124 }
125 }
126
127 /* Destroy it. */
128 RTTESTI_CHECK_RC(RTMemCacheDestroy(hMemCache), VINF_SUCCESS);
129 RTTESTI_CHECK_RC(RTMemCacheDestroy(NIL_RTMEMCACHE), VINF_SUCCESS);
130}
131
132
133
134/** Constructor for tst2. */
135static DECLCALLBACK(int) tst2Ctor(RTMEMCACHE hMemCache, void *pvObj, void *pvUser)
136{
137 RTTESTI_CHECK(hMemCache == g_hMemCache);
138 RTTESTI_CHECK(ASMMemIsAll8(pvObj, 256, 0) == NULL);
139
140 if (*(bool *)pvUser)
141 return VERR_RESOURCE_BUSY;
142
143 strcat((char *)pvObj, "ctor was called\n");
144 return VINF_SUCCESS;
145}
146
147
148/** Destructor for tst2. Checks that it was constructed and used twice. */
149static DECLCALLBACK(void) tst2Dtor(RTMEMCACHE hMemCache, void *pvObj, void *pvUser)
150{
151 RTTESTI_CHECK(!strcmp((char *)pvObj, "ctor was called\nused\nused\n"));
152 strcat((char *)pvObj, "dtor was called\n");
153}
154
155/**
156 * Test constructor / destructor.
157 */
158static void tst2(void)
159{
160 RTTestISub("Ctor/Dtor");
161
162 /* Create one without constructor or destructor. */
163 bool fFail = false;
164 uint32_t const cObjects = PAGE_SIZE * 2 / 256;
165 RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&g_hMemCache, 256, cObjects, 32, tst2Ctor, tst2Dtor, &fFail, 0 /*fFlags*/), VINF_SUCCESS);
166
167 /* A failure run first. */
168 fFail = true;
169 void *pv = (void *)0x42;
170 RTTESTI_CHECK_RC_RETV(RTMemCacheAllocEx(g_hMemCache, &pv), VERR_RESOURCE_BUSY);
171 RTTESTI_CHECK(pv == (void *)0x42);
172 fFail = false;
173
174 /* To two rounds where we allocate all the objects and free them again. */
175 for (uint32_t iLoop = 0; iLoop < 2; iLoop++)
176 {
177 void *apv[cObjects];
178 for (uint32_t i = 0; i < cObjects; i++)
179 {
180 apv[i] = NULL;
181 RTTESTI_CHECK_RC_RETV(RTMemCacheAllocEx(g_hMemCache, &apv[i]), VINF_SUCCESS);
182 if (iLoop == 0)
183 RTTESTI_CHECK(!strcmp((char *)apv[i], "ctor was called\n"));
184 else
185 RTTESTI_CHECK(!strcmp((char *)apv[i], "ctor was called\nused\n"));
186 strcat((char *)apv[i], "used\n");
187 }
188
189 RTTESTI_CHECK_RETV((pv = RTMemCacheAlloc(g_hMemCache)) == NULL);
190 RTMemCacheFree(g_hMemCache, pv);
191
192 for (uint32_t i = 0; i < cObjects; i++)
193 RTMemCacheFree(g_hMemCache, apv[i]);
194 }
195
196 /* Cone, destroy the cache. */
197 RTTESTI_CHECK_RC(RTMemCacheDestroy(g_hMemCache), VINF_SUCCESS);
198}
199
200
201/**
202 * Thread that allocates
203 * @returns
204 * @param hThreadSelf The thread.
205 * @param pvArg Pointer to fUseCache.
206 */
207static DECLCALLBACK(int) tst3Thread(RTTHREAD hThreadSelf, void *pvArg)
208{
209 PTST3THREAD pThread = (PTST3THREAD)(pvArg);
210 size_t cbObject = pThread->cbObject;
211 uint64_t cIterations = 0;
212
213 /* wait for the kick-off */
214 RTTEST_CHECK_RC_OK(g_hTest, RTSemEventMultiWait(pThread->hEvt, RT_INDEFINITE_WAIT));
215
216 /* allocate and free loop */
217 if (pThread->fUseCache)
218 {
219 while (!g_fTst3Stop)
220 {
221 void *apv[64];
222 for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
223 {
224 apv[i] = RTMemCacheAlloc(g_hMemCache);
225 RTTEST_CHECK(g_hTest, apv[i] != NULL);
226 }
227 for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
228 RTMemCacheFree(g_hMemCache, apv[i]);
229
230 cIterations += RT_ELEMENTS(apv);
231 }
232 }
233 else
234 {
235 while (!g_fTst3Stop)
236 {
237 void *apv[64];
238
239 for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
240 {
241 apv[i] = RTMemAlloc(cbObject);
242 RTTEST_CHECK(g_hTest, apv[i] != NULL);
243 }
244
245 for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
246 RTMemFree(apv[i]);
247
248 cIterations += RT_ELEMENTS(apv);
249 }
250 }
251
252 /* report back the status */
253 pThread->cIterations = cIterations;
254 return VINF_SUCCESS;
255}
256
257/**
258 * Time constrained test with and unlimited N threads.
259 */
260static void tst3(uint32_t cThreads, uint32_t cbObject, int iMethod, uint32_t cSecs)
261{
262 RTTestISubF("Benchmark - %u threads, %u bytes, %u secs, %s", cThreads, cbObject, cSecs,
263 iMethod == 0 ? "RTMemCache"
264 : "RTMemAlloc");
265
266 /*
267 * Create a cache with unlimited space, a start semaphore and line up
268 * the threads.
269 */
270 RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&g_hMemCache, cbObject, 0 /*cbAlignment*/, UINT32_MAX, NULL, NULL, NULL, 0 /*fFlags*/), VINF_SUCCESS);
271
272 RTSEMEVENTMULTI hEvt;
273 RTTESTI_CHECK_RC_OK_RETV(RTSemEventMultiCreate(&hEvt));
274
275 TST3THREAD aThreads[64];
276 RTTESTI_CHECK_RETV(cThreads < RT_ELEMENTS(aThreads));
277
278 ASMAtomicWriteBool(&g_fTst3Stop, false);
279 for (uint32_t i = 0; i < cThreads; i++)
280 {
281 aThreads[i].hThread = NIL_RTTHREAD;
282 aThreads[i].cIterations = 0;
283 aThreads[i].fUseCache = iMethod == 0;
284 aThreads[i].cbObject = cbObject;
285 aThreads[i].hEvt = hEvt;
286 RTTESTI_CHECK_RC_OK_RETV(RTThreadCreateF(&aThreads[i].hThread, tst3Thread, &aThreads[i], 0,
287 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tst3-%u", i));
288 }
289
290 /*
291 * Start the race.
292 */
293 RTTimeNanoTS(); /* warmup */
294
295 uint64_t uStartTS = RTTimeNanoTS();
296 RTTESTI_CHECK_RC_OK_RETV(RTSemEventMultiSignal(hEvt));
297 RTThreadSleep(cSecs * 1000);
298 ASMAtomicWriteBool(&g_fTst3Stop, true);
299 for (uint32_t i = 0; i < cThreads; i++)
300 RTTESTI_CHECK_RC_OK_RETV(RTThreadWait(aThreads[i].hThread, 60*1000, NULL));
301 uint64_t cElapsedNS = RTTimeNanoTS() - uStartTS;
302
303 /*
304 * Sum up the counts.
305 */
306 uint64_t cIterations = 0;
307 for (uint32_t i = 0; i < cThreads; i++)
308 cIterations += aThreads[i].cIterations;
309
310 RTTestIPrintf(RTTESTLVL_ALWAYS, "%'8u iterations per second, %'llu ns on avg\n",
311 (unsigned)((long double)cIterations * 1000000000.0 / cElapsedNS),
312 cElapsedNS / cIterations);
313
314 /* clean up */
315 RTTESTI_CHECK_RC(RTMemCacheDestroy(g_hMemCache), VINF_SUCCESS);
316 RTTESTI_CHECK_RC_OK(RTSemEventMultiDestroy(hEvt));
317}
318
319static void tst3AllMethods(uint32_t cThreads, uint32_t cbObject, uint32_t cSecs)
320{
321 tst3(cThreads, cbObject, 0, cSecs);
322 tst3(cThreads, cbObject, 1, cSecs);
323}
324
325
326int main(int argc, char **argv)
327{
328 RTTEST hTest;
329 int rc = RTTestInitAndCreate("tstRTMemCache", &hTest);
330 if (rc)
331 return rc;
332 RTTestBanner(hTest);
333 g_hTest = hTest;
334
335 tst1();
336 tst2();
337 if (RTTestIErrorCount() == 0)
338 {
339 uint32_t cSecs = argc == 1 ? 5 : 2;
340 /* threads, cbObj, cSecs */
341 tst3AllMethods( 1, 256, cSecs);
342 tst3AllMethods( 1, 32, cSecs);
343 tst3AllMethods( 1, 8, cSecs);
344 tst3AllMethods( 1, 2, cSecs);
345 tst3AllMethods( 1, 1, cSecs);
346
347 tst3AllMethods( 3, 256, cSecs);
348 tst3AllMethods( 3, 128, cSecs);
349 tst3AllMethods( 3, 64, cSecs);
350 tst3AllMethods( 3, 32, cSecs);
351 tst3AllMethods( 3, 2, cSecs);
352 tst3AllMethods( 3, 1, cSecs);
353
354 tst3AllMethods( 16, 32, cSecs);
355 }
356
357 /*
358 * Summary.
359 */
360 return RTTestSummaryAndDestroy(hTest);
361}
362
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