VirtualBox

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

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

memcache: some quick tuning.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.7 KB
Line 
1/* $Id: tstRTMemCache.cpp 26420 2010-02-11 02:55:04Z 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), 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), 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 bool fUseCache = pThread->fUseCache;
211 size_t cbObject = pThread->cbObject;
212 uint64_t cIterations = 0;
213
214 /* wait for the kick-off */
215 RTTEST_CHECK_RC_OK(g_hTest, RTSemEventMultiWait(pThread->hEvt, RT_INDEFINITE_WAIT));
216
217 /* allocate and free loop */
218 while (!g_fTst3Stop)
219 {
220 void *apv[64];
221
222 if (fUseCache)
223 {
224 for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
225 {
226 apv[i] = RTMemCacheAlloc(g_hMemCache);
227 RTTEST_CHECK(g_hTest, apv[i] != NULL);
228 }
229 for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
230 RTMemCacheFree(g_hMemCache, apv[i]);
231 }
232 else
233 {
234 for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
235 {
236 apv[i] = RTMemAlloc(cbObject);
237 RTTEST_CHECK(g_hTest, apv[i] != NULL);
238 }
239
240 for (unsigned i = 0; i < RT_ELEMENTS(apv); i++)
241 RTMemFree(apv[i]);
242 }
243 cIterations += RT_ELEMENTS(apv);
244 }
245
246 /* report back the status */
247 pThread->cIterations = cIterations;
248 return VINF_SUCCESS;
249}
250
251/**
252 * Time constrained test with and unlimited N threads.
253 */
254static void tst3(uint32_t cThreads, uint32_t cbObject, bool fUseCache, uint32_t cSecs)
255{
256 RTTestISubF("Benchmark - %u threads, %u bytes, %u secs, %s", cThreads, cbObject, cSecs, fUseCache ? "RTMemCache" : "RTMemAlloc");
257
258 /*
259 * Create a cache with unlimited space, a start semaphore and line up
260 * the threads.
261 */
262 RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&g_hMemCache, cbObject, 0 /*cbAlignment*/, UINT32_MAX, NULL, NULL, NULL), VINF_SUCCESS);
263
264 RTSEMEVENTMULTI hEvt;
265 RTTESTI_CHECK_RC_OK_RETV(RTSemEventMultiCreate(&hEvt));
266
267 TST3THREAD aThreads[64];
268 RTTESTI_CHECK_RETV(cThreads < RT_ELEMENTS(aThreads));
269
270 ASMAtomicWriteBool(&g_fTst3Stop, false);
271 for (uint32_t i = 0; i < cThreads; i++)
272 {
273 aThreads[i].hThread = NIL_RTTHREAD;
274 aThreads[i].cIterations = 0;
275 aThreads[i].fUseCache = fUseCache;
276 aThreads[i].cbObject = cbObject;
277 aThreads[i].hEvt = hEvt;
278 RTTESTI_CHECK_RC_OK_RETV(RTThreadCreateF(&aThreads[i].hThread, tst3Thread, &aThreads[i], 0,
279 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tst3-%u", i));
280 }
281
282 /*
283 * Start the race.
284 */
285 RTTimeNanoTS(); /* warmup */
286
287 uint64_t uStartTS = RTTimeNanoTS();
288 RTTESTI_CHECK_RC_OK_RETV(RTSemEventMultiSignal(hEvt));
289 RTThreadSleep(cSecs * 1000);
290 ASMAtomicWriteBool(&g_fTst3Stop, true);
291 for (uint32_t i = 0; i < cThreads; i++)
292 RTTESTI_CHECK_RC_OK_RETV(RTThreadWait(aThreads[i].hThread, 60*1000, NULL));
293 uint64_t cElapsedNS = RTTimeNanoTS() - uStartTS;
294
295 /*
296 * Sum up the counts.
297 */
298 uint64_t cIterations = 0;
299 for (uint32_t i = 0; i < cThreads; i++)
300 cIterations += aThreads[i].cIterations;
301
302 RTTestIPrintf(RTTESTLVL_ALWAYS, "%'8u iterations per second, %'llu ns on avg\n",
303 (unsigned)((long double)cIterations * 1000000000.0 / cElapsedNS),
304 cElapsedNS / cIterations);
305
306 /* clean up */
307 RTTESTI_CHECK_RC(RTMemCacheDestroy(g_hMemCache), VINF_SUCCESS);
308 RTTESTI_CHECK_RC_OK(RTSemEventMultiDestroy(hEvt));
309}
310
311int main()
312{
313 RTTEST hTest;
314 int rc = RTTestInitAndCreate("tstRTMemCache", &hTest);
315 if (rc)
316 return rc;
317 RTTestBanner(hTest);
318 g_hTest = hTest;
319
320 tst1();
321 tst2();
322 if (RTTestIErrorCount() == 0)
323 {
324 /* threads, cbObj, fUseCache, cSecs */
325 tst3( 1, 256, true, 5);
326 tst3( 1, 256, false, 5);
327 tst3( 1, 32, true, 5);
328 tst3( 1, 32, false, 5);
329 tst3( 1, 8, true, 5);
330 tst3( 1, 8, false, 5);
331 tst3( 1, 2, true, 5);
332 tst3( 1, 2, false, 5);
333 tst3( 1, 1, true, 5);
334 tst3( 1, 1, false, 5);
335
336 tst3( 3, 256, true, 5);
337 tst3( 3, 256, false, 5);
338 tst3( 3, 128, true, 5);
339 tst3( 3, 128, false, 5);
340 tst3( 3, 64, true, 5);
341 tst3( 3, 64, false, 5);
342 tst3( 3, 32, true, 5);
343 tst3( 3, 32, false, 5);
344 tst3( 3, 2, true, 5);
345 tst3( 3, 2, false, 5);
346 tst3( 3, 1, true, 5);
347 tst3( 3, 1, false, 5);
348
349 tst3( 16, 32, true, 5);
350 tst3( 16, 32, false, 5);
351 }
352
353 /*
354 * Summary.
355 */
356 return RTTestSummaryAndDestroy(hTest);
357}
358
359
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