1
/* $Id: tstPDMAsyncCompletionStress.cpp $ */
3
* PDM Asynchronous Completion Stresstest.
5
* This testcase is for stress testing the async completion interface.
9
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
11
* This file is part of VirtualBox Open Source Edition (OSE), as
12
* available from http://www.virtualbox.org. This file is free software;
13
* you can redistribute it and/or modify it under the terms of the GNU
14
* General Public License (GPL) as published by the Free Software
15
* Foundation, in version 2 as it comes in the "COPYING" file of the
16
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20
* Clara, CA 95054 USA or visit http://www.sun.com if you need
21
* additional information or have any questions.
24
/*******************************************************************************
26
*******************************************************************************/
27
#define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
29
#include "../VMInternal.h" /* UVM */
32
#include <VBox/pdmasynccompletion.h>
34
#include <VBox/cpum.h>
37
#include <VBox/pdmapi.h>
38
#include <VBox/pdmthread.h>
39
#include <iprt/alloc.h>
41
#include <iprt/assert.h>
42
#include <iprt/file.h>
43
#include <iprt/initterm.h>
44
#include <iprt/semaphore.h>
45
#include <iprt/rand.h>
46
#include <iprt/string.h>
47
#include <iprt/path.h>
48
#include <iprt/stream.h>
49
#include <iprt/thread.h>
50
#include <iprt/param.h>
52
#define TESTCASE "tstPDMAsyncCompletionStress"
55
/** Number of simultaneous open endpoints for reading and writing. */
56
#define NR_OPEN_ENDPOINTS 10
57
/** Test pattern size. */
58
#define TEST_PATTERN_SIZE (100*_1M)
59
/** Minimum file size. */
60
#define FILE_SIZE_MIN (100 * _1M)
61
/** Maximum file size. */
62
#define FILE_SIZE_MAX (10000UL * _1M)
63
/** Minimum segment size. */
64
#define SEGMENT_SIZE_MIN (512)
65
/** Maximum segment size. */
66
#define SEGMENT_SIZE_MAX (TEST_PATTERN_SIZE)
67
/** Maximum number of active tasks. */
68
#define TASK_ACTIVE_MAX (1024)
69
/** Maximum size of a transfer. */
70
#define TASK_TRANSFER_SIZE_MAX (10*_1M)
72
/** Number of simultaneous open endpoints for reading and writing. */
73
#define NR_OPEN_ENDPOINTS 5
74
/** Test pattern size. */
75
#define TEST_PATTERN_SIZE (10*_1M)
76
/** Minimum file size. */
77
#define FILE_SIZE_MIN (100 * _1M)
78
/** Maximum file size. */
79
#define FILE_SIZE_MAX (1000UL * _1M)
80
/** Minimum segment size. */
81
#define SEGMENT_SIZE_MIN (512)
82
/** Maximum segment size. */
83
#define SEGMENT_SIZE_MAX (TEST_PATTERN_SIZE)
84
/** Maximum number of active tasks. */
85
#define TASK_ACTIVE_MAX (1)
86
/** Maximum size of a transfer. */
87
#define TASK_TRANSFER_SIZE_MAX (_1M)
91
* Structure defining a file segment.
93
typedef struct PDMACTESTFILESEG
95
/** Start offset in the file. */
97
/** Size of the segment. */
99
/** Pointer to the start of the data in the test pattern used for the segment. */
101
} PDMACTESTFILESEG, *PPDMACTESTFILESEG;
104
* Structure defining a I/O task.
106
typedef struct PDMACTESTFILETASK
108
/** Flag whether the task is currently active. */
110
/** Flag whether this is a write. */
117
PPDMASYNCCOMPLETIONTASK hTask;
118
} PDMACTESTFILETASK, *PPDMACTESTFILETASK;
121
* Structure defining a test file.
123
typedef struct PDMACTESTFILE
125
/** The PDM async completion endpoint handle. */
126
PPDMASYNCCOMPLETIONENDPOINT hEndpoint;
127
/** Template used for this file. */
128
PPDMASYNCCOMPLETIONTEMPLATE pTemplate;
129
/** Maximum size of the file. */
131
/** Current size of the file. */
133
/** Size of a file segment. */
134
size_t cbFileSegment;
135
/** Maximum number of segments. */
137
/** Pointer to the array describing how the file is assembled
138
* of the test pattern. Used for comparing read data to ensure
139
* that no corruption occured.
141
PPDMACTESTFILESEG paSegs;
142
/** Maximum number of active tasks for this endpoint. */
143
uint32_t cTasksActiveMax;
144
/** Number of current active tasks. */
145
volatile uint32_t cTasksActiveCurr;
146
/** Pointer to the array of task. */
147
PPDMACTESTFILETASK paTasks;
148
/** I/O thread handle. */
150
/** Flag whether the thread should terminate. */
152
} PDMACTESTFILE, *PPDMACTESTFILE;
154
/** Buffer storing the random test pattern. */
155
uint8_t *g_pbTestPattern = NULL;
156
/** Size of the test pattern. */
157
size_t g_cbTestPattern;
158
/** Array holding test files. */
159
PDMACTESTFILE g_aTestFiles[NR_OPEN_ENDPOINTS];
161
static void tstPDMACStressTestFileVerify(PPDMACTESTFILE pTestFile, PPDMACTESTFILETASK pTestTask)
163
size_t cbLeft = pTestTask->DataSeg.cbSeg;
164
RTFOFF off = pTestTask->off;
165
uint8_t *pbBuf = (uint8_t *)pTestTask->DataSeg.pvSeg;
170
unsigned iSeg = off / pTestFile->cbFileSegment;
171
PPDMACTESTFILESEG pSeg = &pTestFile->paSegs[iSeg];
172
uint8_t *pbTestPattern;
173
unsigned offSeg = off - pSeg->off;
175
cbCompare = RT_MIN(cbLeft, pSeg->cbSegment - offSeg);
176
pbTestPattern = pSeg->pbData + offSeg;
178
if (memcmp(pbBuf, pbTestPattern, cbCompare))
179
AssertMsgFailed(("Unexpected data for off=%RTfoff size=%u\n", pTestTask->off, pTestTask->DataSeg.cbSeg));
187
static void tstPDMACStressTestFileFillBuffer(PPDMACTESTFILE pTestFile, PPDMACTESTFILETASK pTestTask)
189
uint8_t *pbBuf = (uint8_t *)pTestTask->DataSeg.pvSeg;
190
size_t cbLeft = pTestTask->DataSeg.cbSeg;
191
RTFOFF off = pTestTask->off;
193
Assert(pTestTask->fWrite && pTestTask->fActive);
198
unsigned iSeg = off / pTestFile->cbFileSegment;
199
PPDMACTESTFILESEG pSeg = &pTestFile->paSegs[iSeg];
200
uint8_t *pbTestPattern;
201
unsigned offSeg = off - pSeg->off;
203
cbFill = RT_MIN(cbLeft, pSeg->cbSegment - offSeg);
204
pbTestPattern = pSeg->pbData + offSeg;
206
memcpy(pbBuf, pbTestPattern, cbFill);
214
static int tstPDMACStressTestFileWrite(PPDMACTESTFILE pTestFile, PPDMACTESTFILETASK pTestTask)
216
int rc = VINF_SUCCESS;
218
Assert(!pTestTask->fActive);
220
pTestTask->fActive = true;
221
pTestTask->fWrite = true;
222
pTestTask->DataSeg.cbSeg = RTRandU32Ex(512, TASK_TRANSFER_SIZE_MAX) & ~511;
226
/* Did we reached the maximum file size */
227
if (pTestFile->cbFileCurr < pTestFile->cbFileMax)
229
offMax = (pTestFile->cbFileMax - pTestFile->cbFileCurr) < pTestTask->DataSeg.cbSeg
230
? pTestFile->cbFileMax - pTestTask->DataSeg.cbSeg
231
: pTestFile->cbFileCurr;
234
offMax = pTestFile->cbFileMax - pTestTask->DataSeg.cbSeg;
239
* If we reached the maximum file size write in the whole file
240
* otherwise we will enforce the range for random offsets to let it grow
243
if (pTestFile->cbFileCurr == pTestFile->cbFileMax)
246
offMin = RT_MIN(pTestFile->cbFileCurr, offMax);
249
pTestTask->off = RTRandU64Ex(offMin, offMax) & ~511;
251
/* Set new file size of required */
252
if ((uint64_t)pTestTask->off + pTestTask->DataSeg.cbSeg > pTestFile->cbFileCurr)
253
pTestFile->cbFileCurr = pTestTask->off + pTestTask->DataSeg.cbSeg;
255
AssertMsg(pTestFile->cbFileCurr <= pTestFile->cbFileMax,
256
("Current file size (%llu) exceeds final size (%llu)\n",
257
pTestFile->cbFileCurr, pTestFile->cbFileMax));
259
/* Allocate data buffer. */
260
pTestTask->DataSeg.pvSeg = RTMemAlloc(pTestTask->DataSeg.cbSeg);
261
if (!pTestTask->DataSeg.pvSeg)
262
return VERR_NO_MEMORY;
264
/* Fill data into buffer. */
265
tstPDMACStressTestFileFillBuffer(pTestFile, pTestTask);
268
rc = PDMR3AsyncCompletionEpWrite(pTestFile->hEndpoint, pTestTask->off,
269
&pTestTask->DataSeg, 1,
270
pTestTask->DataSeg.cbSeg,
277
static int tstPDMACStressTestFileRead(PPDMACTESTFILE pTestFile, PPDMACTESTFILETASK pTestTask)
279
int rc = VINF_SUCCESS;
281
Assert(!pTestTask->fActive);
283
pTestTask->fActive = true;
284
pTestTask->fWrite = false;
285
pTestTask->DataSeg.cbSeg = RTRandU32Ex(1, RT_MIN(pTestFile->cbFileCurr, TASK_TRANSFER_SIZE_MAX));
287
AssertMsg(pTestFile->cbFileCurr >= pTestTask->DataSeg.cbSeg, ("Impossible\n"));
288
pTestTask->off = RTRandU64Ex(0, pTestFile->cbFileCurr - pTestTask->DataSeg.cbSeg);
290
/* Allocate data buffer. */
291
pTestTask->DataSeg.pvSeg = RTMemAlloc(pTestTask->DataSeg.cbSeg);
292
if (!pTestTask->DataSeg.pvSeg)
293
return VERR_NO_MEMORY;
296
rc = PDMR3AsyncCompletionEpRead(pTestFile->hEndpoint, pTestTask->off,
297
&pTestTask->DataSeg, 1,
298
pTestTask->DataSeg.cbSeg,
306
* Returns true with the given chance in percent.
308
* @returns true or false
309
* @param iPercentage The percentage of the chance to return true.
311
static bool tstPDMACTestIsTrue(int iPercentage)
313
int uRnd = RTRandU32Ex(0, 100);
315
return (uRnd < iPercentage); /* This should be enough for our purpose */
318
static int tstPDMACTestFileThread(PVM pVM, PPDMTHREAD pThread)
320
PPDMACTESTFILE pTestFile = (PPDMACTESTFILE)pThread->pvUser;
321
int iWriteChance = 100; /* Chance to get a write task in percent. */
322
uint32_t cTasksStarted = 0;
323
int rc = VINF_SUCCESS;
325
if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
328
while (pTestFile->fRunning)
330
unsigned iTaskCurr = 0;
334
while ( (pTestFile->cTasksActiveCurr < pTestFile->cTasksActiveMax)
335
&& (iTaskCurr < pTestFile->cTasksActiveMax))
337
PPDMACTESTFILETASK pTask = &pTestFile->paTasks[iTaskCurr];
341
/* Read or write task? */
342
bool fWrite = tstPDMACTestIsTrue(iWriteChance);
344
ASMAtomicIncU32(&pTestFile->cTasksActiveCurr);
347
rc = tstPDMACStressTestFileWrite(pTestFile, pTask);
349
rc = tstPDMACStressTestFileRead(pTestFile, pTask);
360
* Recalc write chance. The bigger the file the lower the chance to have a write.
361
* The minimum chance is 33 percent.
363
iWriteChance = 100 - (int)(((float)100.0 / pTestFile->cbFileMax) * (float)pTestFile->cbFileCurr);
364
iWriteChance = RT_MAX(33, iWriteChance);
366
/* Wait a random amount of time. (1ms - 100ms) */
367
RTThreadSleep(RTRandU32Ex(1, 100));
370
/* Wait for the rest to complete. */
371
while (pTestFile->cTasksActiveCurr)
374
RTPrintf("Thread exiting: processed %u tasks\n", cTasksStarted);
378
static void tstPDMACStressTestFileTaskCompleted(PVM pVM, void *pvUser, void *pvUser2)
380
PPDMACTESTFILE pTestFile = (PPDMACTESTFILE)pvUser2;
381
PPDMACTESTFILETASK pTestTask = (PPDMACTESTFILETASK)pvUser;
383
if (pTestTask->fWrite)
385
/* @todo Do something sensible here. */
389
tstPDMACStressTestFileVerify(pTestFile, pTestTask); /* Will assert if it fails */
392
RTMemFree(pTestTask->DataSeg.pvSeg);
393
pTestTask->fActive = false;
394
AssertMsg(pTestFile->cTasksActiveCurr > 0, ("Trying to complete a non active task\n"));
395
ASMAtomicDecU32(&pTestFile->cTasksActiveCurr);
399
* Sets up a test file creating the I/O thread.
401
* @returns VBox status code.
402
* @param pVM Pointer to the shared VM instance structure.
403
* @param pTestFile Pointer to the uninitialized test file structure.
404
* @param iTestId Unique test id.
406
static int tstPDMACStressTestFileOpen(PVM pVM, PPDMACTESTFILE pTestFile, unsigned iTestId)
408
int rc = VERR_NO_MEMORY;
410
/* Size is a multiple of 512 */
411
pTestFile->cbFileMax = RTRandU64Ex(FILE_SIZE_MIN, FILE_SIZE_MAX) & ~(511UL);
412
pTestFile->cbFileCurr = 0;
413
pTestFile->cbFileSegment = RTRandU32Ex(SEGMENT_SIZE_MIN, RT_MIN(pTestFile->cbFileMax, SEGMENT_SIZE_MAX)) & ~((size_t)511);
415
Assert(pTestFile->cbFileMax >= pTestFile->cbFileSegment);
417
/* Set up the segments array. */
418
pTestFile->cSegments = pTestFile->cbFileMax / pTestFile->cbFileSegment;
419
pTestFile->cSegments += ((pTestFile->cbFileMax % pTestFile->cbFileSegment) > 0) ? 1 : 0;
421
pTestFile->paSegs = (PPDMACTESTFILESEG)RTMemAllocZ(pTestFile->cSegments * sizeof(PDMACTESTFILESEG));
422
if (pTestFile->paSegs)
424
/* Init the segments */
425
for (unsigned i = 0; i < pTestFile->cSegments; i++)
427
PPDMACTESTFILESEG pSeg = &pTestFile->paSegs[i];
429
pSeg->off = (RTFOFF)i * pTestFile->cbFileSegment;
430
pSeg->cbSegment = pTestFile->cbFileSegment;
432
/* Let the buffer point to a random position in the test pattern. */
433
uint32_t offTestPattern = RTRandU64Ex(0, g_cbTestPattern - pSeg->cbSegment);
435
pSeg->pbData = g_pbTestPattern + offTestPattern;
438
/* Init task array. */
439
pTestFile->cTasksActiveMax = RTRandU32Ex(1, TASK_ACTIVE_MAX);
440
pTestFile->paTasks = (PPDMACTESTFILETASK)RTMemAllocZ(pTestFile->cTasksActiveMax * sizeof(PDMACTESTFILETASK));
441
if (pTestFile->paTasks)
443
/* Create the template */
446
RTStrPrintf(szDesc, sizeof(szDesc), "Template-%d", iTestId);
447
rc = PDMR3AsyncCompletionTemplateCreateInternal(pVM, &pTestFile->pTemplate, tstPDMACStressTestFileTaskCompleted, pTestFile, szDesc);
450
/* Open the endpoint now. Because async completion endpoints cannot create files we have to do it before. */
451
char szFile[RTPATH_MAX];
453
RTStrPrintf(szFile, sizeof(szFile), "tstPDMAsyncCompletionStress-%d.tmp", iTestId);
456
rc = RTFileOpen(&FileTmp, szFile, RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE);
459
RTFileClose(FileTmp);
461
rc = PDMR3AsyncCompletionEpCreateForFile(&pTestFile->hEndpoint, szFile, PDMACEP_FILE_FLAGS_CACHING, pTestFile->pTemplate);
464
char szThreadDesc[256];
466
pTestFile->fRunning = true;
468
/* Create the thread creating the I/O for the given file. */
469
RTStrPrintf(szThreadDesc, sizeof(szThreadDesc), "PDMACThread-%d", iTestId);
470
rc = PDMR3ThreadCreate(pVM, &pTestFile->hThread, pTestFile, tstPDMACTestFileThread,
471
NULL, 0, RTTHREADTYPE_IO, szThreadDesc);
474
rc = PDMR3ThreadResume(pTestFile->hThread);
477
RTPrintf(TESTCASE ": Created test file %s cbFileMax=%llu cbFileSegment=%u cSegments=%u cTasksActiveMax=%u\n",
478
szFile, pTestFile->cbFileMax, pTestFile->cbFileSegment, pTestFile->cSegments, pTestFile->cTasksActiveMax);
482
PDMR3AsyncCompletionEpClose(pTestFile->hEndpoint);
485
RTFileDelete(szFile);
488
PDMR3AsyncCompletionTemplateDestroy(pTestFile->pTemplate);
491
RTMemFree(pTestFile->paTasks);
496
RTMemFree(pTestFile->paSegs);
501
RTPrintf(TESTCASE ": Opening test file with id %d failed rc=%Rrc\n", iTestId, rc);
507
* Closes a test file.
510
* @param pTestFile Pointer to the test file.
512
static void tstPDMACStressTestFileClose(PPDMACTESTFILE pTestFile)
517
RTPrintf("Terminating I/O thread, please wait...\n");
519
/* Let the thread know that it should terminate. */
520
pTestFile->fRunning = false;
522
/* Wait for the thread to terminate. */
523
rc = PDMR3ThreadDestroy(pTestFile->hThread, &rcThread);
525
RTPrintf("Thread terminated with status code rc=%Rrc\n", rcThread);
528
RTMemFree(pTestFile->paTasks);
529
RTMemFree(pTestFile->paSegs);
530
PDMR3AsyncCompletionEpClose(pTestFile->hEndpoint);
531
PDMR3AsyncCompletionTemplateDestroy(pTestFile->pTemplate);
535
* Inits the test pattern.
537
* @returns VBox status code.
539
static int tstPDMACStressTestPatternInit(void)
541
RTPrintf(TESTCASE ": Creating test pattern. Please wait...\n");
542
g_cbTestPattern = TEST_PATTERN_SIZE;
543
g_pbTestPattern = (uint8_t *)RTMemAlloc(g_cbTestPattern);
544
if (!g_pbTestPattern)
545
return VERR_NO_MEMORY;
547
RTRandBytes(g_pbTestPattern, g_cbTestPattern);
551
static void tstPDMACStressTestPatternDestroy(void)
553
RTPrintf(TESTCASE ": Destroying test pattern\n");
554
RTMemFree(g_pbTestPattern);
557
int main(int argc, char *argv[])
559
int rcRet = 0; /* error count */
560
int rc = VINF_SUCCESS;
565
rc = VMR3Create(1, NULL, NULL, NULL, NULL, &pVM);
569
* Little hack to avoid the VM_ASSERT_EMT assertion.
571
RTTlsSet(pVM->pUVM->vm.s.idxTLS, &pVM->pUVM->aCpus[0]);
572
pVM->pUVM->aCpus[0].pUVM = pVM->pUVM;
573
pVM->pUVM->aCpus[0].vm.s.NativeThreadEMT = RTThreadNativeSelf();
575
rc = tstPDMACStressTestPatternInit();
578
unsigned cFilesOpened = 0;
580
/* Open the endpoints. */
581
for (cFilesOpened = 0; cFilesOpened < NR_OPEN_ENDPOINTS; cFilesOpened++)
583
rc = tstPDMACStressTestFileOpen(pVM, &g_aTestFiles[cFilesOpened], cFilesOpened);
590
/* Tests are running now. */
591
RTPrintf(TESTCASE ": Successfully opened all files. Running tests forever now or until an error is hit :)\n");
592
RTThreadSleep(RT_INDEFINITE_WAIT);
595
/* Close opened endpoints. */
596
for (unsigned i = 0; i < cFilesOpened; i++)
597
tstPDMACStressTestFileClose(&g_aTestFiles[i]);
599
tstPDMACStressTestPatternDestroy();
603
RTPrintf(TESTCASE ": failed to init test pattern!! rc=%Rrc\n", rc);
607
rc = VMR3Destroy(pVM);
608
AssertMsg(rc == VINF_SUCCESS, ("%s: Destroying VM failed rc=%Rrc!!\n", __FUNCTION__, rc));
612
RTPrintf(TESTCASE ": failed to create VM!! rc=%Rrc\n", rc);