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