1 | /* $Id: tstPDMAsyncCompletionStress.cpp 28065 2010-04-07 20:54:34Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM Asynchronous Completion Stresstest.
|
---|
4 | *
|
---|
5 | * This testcase is for stress testing the async completion interface.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008-2009 Sun Microsystems, Inc.
|
---|
10 | *
|
---|
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.
|
---|
18 | *
|
---|
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.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
|
---|
28 |
|
---|
29 | #include "../VMInternal.h" /* UVM */
|
---|
30 | #include <VBox/vm.h>
|
---|
31 | #include <VBox/uvm.h>
|
---|
32 | #include <VBox/pdmasynccompletion.h>
|
---|
33 | #include <VBox/vmm.h>
|
---|
34 | #include <VBox/cpum.h>
|
---|
35 | #include <VBox/err.h>
|
---|
36 | #include <VBox/log.h>
|
---|
37 | #include <VBox/pdmapi.h>
|
---|
38 | #include <VBox/pdmthread.h>
|
---|
39 | #include <iprt/alloc.h>
|
---|
40 | #include <iprt/asm.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>
|
---|
51 | #include <iprt/message.h>
|
---|
52 |
|
---|
53 | #define TESTCASE "tstPDMAsyncCompletionStress"
|
---|
54 |
|
---|
55 | #if 0
|
---|
56 | /** Number of simultaneous open endpoints for reading and writing. */
|
---|
57 | #define NR_OPEN_ENDPOINTS 10
|
---|
58 | /** Test pattern size. */
|
---|
59 | #define TEST_PATTERN_SIZE (100*_1M)
|
---|
60 | /** Minimum file size. */
|
---|
61 | #define FILE_SIZE_MIN (100 * _1M)
|
---|
62 | /** Maximum file size. */
|
---|
63 | #define FILE_SIZE_MAX (10000UL * _1M)
|
---|
64 | /** Minimum segment size. */
|
---|
65 | #define SEGMENT_SIZE_MIN (512)
|
---|
66 | /** Maximum segment size. */
|
---|
67 | #define SEGMENT_SIZE_MAX (TEST_PATTERN_SIZE)
|
---|
68 | /** Maximum number of active tasks. */
|
---|
69 | #define TASK_ACTIVE_MAX (1024)
|
---|
70 | /** Maximum size of a transfer. */
|
---|
71 | #define TASK_TRANSFER_SIZE_MAX (10*_1M)
|
---|
72 | #else
|
---|
73 | /** Number of simultaneous open endpoints for reading and writing. */
|
---|
74 | #define NR_OPEN_ENDPOINTS 5
|
---|
75 | /** Test pattern size. */
|
---|
76 | #define TEST_PATTERN_SIZE (10*_1M)
|
---|
77 | /** Minimum file size. */
|
---|
78 | #define FILE_SIZE_MIN (100 * _1M)
|
---|
79 | /** Maximum file size. */
|
---|
80 | #define FILE_SIZE_MAX (1000UL * _1M)
|
---|
81 | /** Minimum segment size. */
|
---|
82 | #define SEGMENT_SIZE_MIN (512)
|
---|
83 | /** Maximum segment size. */
|
---|
84 | #define SEGMENT_SIZE_MAX (TEST_PATTERN_SIZE)
|
---|
85 | /** Maximum number of active tasks. */
|
---|
86 | #define TASK_ACTIVE_MAX (1)
|
---|
87 | /** Maximum size of a transfer. */
|
---|
88 | #define TASK_TRANSFER_SIZE_MAX (_1M)
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Structure defining a file segment.
|
---|
93 | */
|
---|
94 | typedef struct PDMACTESTFILESEG
|
---|
95 | {
|
---|
96 | /** Start offset in the file. */
|
---|
97 | RTFOFF off;
|
---|
98 | /** Size of the segment. */
|
---|
99 | size_t cbSegment;
|
---|
100 | /** Pointer to the start of the data in the test pattern used for the segment. */
|
---|
101 | uint8_t *pbData;
|
---|
102 | } PDMACTESTFILESEG, *PPDMACTESTFILESEG;
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Structure defining a I/O task.
|
---|
106 | */
|
---|
107 | typedef struct PDMACTESTFILETASK
|
---|
108 | {
|
---|
109 | /** Flag whether the task is currently active. */
|
---|
110 | bool fActive;
|
---|
111 | /** Flag whether this is a write. */
|
---|
112 | bool fWrite;
|
---|
113 | /** Start offset. */
|
---|
114 | RTFOFF off;
|
---|
115 | /** Data segment */
|
---|
116 | RTSGSEG DataSeg;
|
---|
117 | /** Task handle. */
|
---|
118 | PPDMASYNCCOMPLETIONTASK hTask;
|
---|
119 | } PDMACTESTFILETASK, *PPDMACTESTFILETASK;
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Structure defining a test file.
|
---|
123 | */
|
---|
124 | typedef struct PDMACTESTFILE
|
---|
125 | {
|
---|
126 | /** The PDM async completion endpoint handle. */
|
---|
127 | PPDMASYNCCOMPLETIONENDPOINT hEndpoint;
|
---|
128 | /** Template used for this file. */
|
---|
129 | PPDMASYNCCOMPLETIONTEMPLATE pTemplate;
|
---|
130 | /** Maximum size of the file. */
|
---|
131 | uint64_t cbFileMax;
|
---|
132 | /** Current size of the file. */
|
---|
133 | uint64_t cbFileCurr;
|
---|
134 | /** Size of a file segment. */
|
---|
135 | size_t cbFileSegment;
|
---|
136 | /** Maximum number of segments. */
|
---|
137 | unsigned cSegments;
|
---|
138 | /** Pointer to the array describing how the file is assembled
|
---|
139 | * of the test pattern. Used for comparing read data to ensure
|
---|
140 | * that no corruption occured.
|
---|
141 | */
|
---|
142 | PPDMACTESTFILESEG paSegs;
|
---|
143 | /** Maximum number of active tasks for this endpoint. */
|
---|
144 | uint32_t cTasksActiveMax;
|
---|
145 | /** Number of current active tasks. */
|
---|
146 | volatile uint32_t cTasksActiveCurr;
|
---|
147 | /** Pointer to the array of task. */
|
---|
148 | PPDMACTESTFILETASK paTasks;
|
---|
149 | /** I/O thread handle. */
|
---|
150 | PPDMTHREAD hThread;
|
---|
151 | /** Flag whether the thread should terminate. */
|
---|
152 | bool fRunning;
|
---|
153 | } PDMACTESTFILE, *PPDMACTESTFILE;
|
---|
154 |
|
---|
155 | /** Buffer storing the random test pattern. */
|
---|
156 | uint8_t *g_pbTestPattern = NULL;
|
---|
157 | /** Size of the test pattern. */
|
---|
158 | size_t g_cbTestPattern;
|
---|
159 | /** Array holding test files. */
|
---|
160 | PDMACTESTFILE g_aTestFiles[NR_OPEN_ENDPOINTS];
|
---|
161 |
|
---|
162 | static void tstPDMACStressTestFileTaskCompleted(PVM pVM, void *pvUser, void *pvUser2, int rcReq);
|
---|
163 |
|
---|
164 | static void tstPDMACStressTestFileVerify(PPDMACTESTFILE pTestFile, PPDMACTESTFILETASK pTestTask)
|
---|
165 | {
|
---|
166 | size_t cbLeft = pTestTask->DataSeg.cbSeg;
|
---|
167 | RTFOFF off = pTestTask->off;
|
---|
168 | uint8_t *pbBuf = (uint8_t *)pTestTask->DataSeg.pvSeg;
|
---|
169 |
|
---|
170 | while (cbLeft)
|
---|
171 | {
|
---|
172 | size_t cbCompare;
|
---|
173 | unsigned iSeg = off / pTestFile->cbFileSegment;
|
---|
174 | PPDMACTESTFILESEG pSeg = &pTestFile->paSegs[iSeg];
|
---|
175 | uint8_t *pbTestPattern;
|
---|
176 | unsigned offSeg = off - pSeg->off;
|
---|
177 |
|
---|
178 | cbCompare = RT_MIN(cbLeft, pSeg->cbSegment - offSeg);
|
---|
179 | pbTestPattern = pSeg->pbData + offSeg;
|
---|
180 |
|
---|
181 | if (memcmp(pbBuf, pbTestPattern, cbCompare))
|
---|
182 | {
|
---|
183 | unsigned idx = 0;
|
---|
184 |
|
---|
185 | while ( (pbBuf[idx] == pbTestPattern[idx])
|
---|
186 | && (idx < cbCompare))
|
---|
187 | idx++;
|
---|
188 |
|
---|
189 | RTMsgError("Unexpected data for off=%RTfoff size=%u\n"
|
---|
190 | "Expected %c got %c\n",
|
---|
191 | pTestTask->off + idx, pTestTask->DataSeg.cbSeg,
|
---|
192 | pbTestPattern[idx], pbBuf[idx]);
|
---|
193 | RTAssertDebugBreak();
|
---|
194 | }
|
---|
195 |
|
---|
196 | pbBuf += cbCompare;
|
---|
197 | off += cbCompare;
|
---|
198 | cbLeft -= cbCompare;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | static void tstPDMACStressTestFileFillBuffer(PPDMACTESTFILE pTestFile, PPDMACTESTFILETASK pTestTask)
|
---|
203 | {
|
---|
204 | uint8_t *pbBuf = (uint8_t *)pTestTask->DataSeg.pvSeg;
|
---|
205 | size_t cbLeft = pTestTask->DataSeg.cbSeg;
|
---|
206 | RTFOFF off = pTestTask->off;
|
---|
207 |
|
---|
208 | Assert(pTestTask->fWrite && pTestTask->fActive);
|
---|
209 |
|
---|
210 | while (cbLeft)
|
---|
211 | {
|
---|
212 | size_t cbFill;
|
---|
213 | unsigned iSeg = off / pTestFile->cbFileSegment;
|
---|
214 | PPDMACTESTFILESEG pSeg = &pTestFile->paSegs[iSeg];
|
---|
215 | uint8_t *pbTestPattern;
|
---|
216 | unsigned offSeg = off - pSeg->off;
|
---|
217 |
|
---|
218 | cbFill = RT_MIN(cbLeft, pSeg->cbSegment - offSeg);
|
---|
219 | pbTestPattern = pSeg->pbData + offSeg;
|
---|
220 |
|
---|
221 | memcpy(pbBuf, pbTestPattern, cbFill);
|
---|
222 |
|
---|
223 | pbBuf += cbFill;
|
---|
224 | off += cbFill;
|
---|
225 | cbLeft -= cbFill;
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | static int tstPDMACStressTestFileWrite(PPDMACTESTFILE pTestFile, PPDMACTESTFILETASK pTestTask)
|
---|
230 | {
|
---|
231 | int rc = VINF_SUCCESS;
|
---|
232 |
|
---|
233 | Assert(!pTestTask->fActive);
|
---|
234 |
|
---|
235 | pTestTask->fActive = true;
|
---|
236 | pTestTask->fWrite = true;
|
---|
237 | pTestTask->DataSeg.cbSeg = RTRandU32Ex(512, TASK_TRANSFER_SIZE_MAX) & ~511;
|
---|
238 |
|
---|
239 | uint64_t offMax;
|
---|
240 |
|
---|
241 | /* Did we reached the maximum file size */
|
---|
242 | if (pTestFile->cbFileCurr < pTestFile->cbFileMax)
|
---|
243 | {
|
---|
244 | offMax = (pTestFile->cbFileMax - pTestFile->cbFileCurr) < pTestTask->DataSeg.cbSeg
|
---|
245 | ? pTestFile->cbFileMax - pTestTask->DataSeg.cbSeg
|
---|
246 | : pTestFile->cbFileCurr;
|
---|
247 | }
|
---|
248 | else
|
---|
249 | offMax = pTestFile->cbFileMax - pTestTask->DataSeg.cbSeg;
|
---|
250 |
|
---|
251 | uint64_t offMin;
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * If we reached the maximum file size write in the whole file
|
---|
255 | * otherwise we will enforce the range for random offsets to let it grow
|
---|
256 | * more quickly.
|
---|
257 | */
|
---|
258 | if (pTestFile->cbFileCurr == pTestFile->cbFileMax)
|
---|
259 | offMin = 0;
|
---|
260 | else
|
---|
261 | offMin = RT_MIN(pTestFile->cbFileCurr, offMax);
|
---|
262 |
|
---|
263 |
|
---|
264 | pTestTask->off = RTRandU64Ex(offMin, offMax) & ~511;
|
---|
265 |
|
---|
266 | /* Set new file size of required */
|
---|
267 | if ((uint64_t)pTestTask->off + pTestTask->DataSeg.cbSeg > pTestFile->cbFileCurr)
|
---|
268 | pTestFile->cbFileCurr = pTestTask->off + pTestTask->DataSeg.cbSeg;
|
---|
269 |
|
---|
270 | AssertMsg(pTestFile->cbFileCurr <= pTestFile->cbFileMax,
|
---|
271 | ("Current file size (%llu) exceeds final size (%llu)\n",
|
---|
272 | pTestFile->cbFileCurr, pTestFile->cbFileMax));
|
---|
273 |
|
---|
274 | /* Allocate data buffer. */
|
---|
275 | pTestTask->DataSeg.pvSeg = RTMemAlloc(pTestTask->DataSeg.cbSeg);
|
---|
276 | if (!pTestTask->DataSeg.pvSeg)
|
---|
277 | return VERR_NO_MEMORY;
|
---|
278 |
|
---|
279 | /* Fill data into buffer. */
|
---|
280 | tstPDMACStressTestFileFillBuffer(pTestFile, pTestTask);
|
---|
281 |
|
---|
282 | /* Engage */
|
---|
283 | rc = PDMR3AsyncCompletionEpWrite(pTestFile->hEndpoint, pTestTask->off,
|
---|
284 | &pTestTask->DataSeg, 1,
|
---|
285 | pTestTask->DataSeg.cbSeg,
|
---|
286 | pTestTask,
|
---|
287 | &pTestTask->hTask);
|
---|
288 |
|
---|
289 | return rc;
|
---|
290 | }
|
---|
291 |
|
---|
292 | static int tstPDMACStressTestFileRead(PPDMACTESTFILE pTestFile, PPDMACTESTFILETASK pTestTask)
|
---|
293 | {
|
---|
294 | int rc = VINF_SUCCESS;
|
---|
295 |
|
---|
296 | Assert(!pTestTask->fActive);
|
---|
297 |
|
---|
298 | pTestTask->fActive = true;
|
---|
299 | pTestTask->fWrite = false;
|
---|
300 | pTestTask->DataSeg.cbSeg = RTRandU32Ex(1, RT_MIN(pTestFile->cbFileCurr, TASK_TRANSFER_SIZE_MAX));
|
---|
301 |
|
---|
302 | AssertMsg(pTestFile->cbFileCurr >= pTestTask->DataSeg.cbSeg, ("Impossible\n"));
|
---|
303 | pTestTask->off = RTRandU64Ex(0, pTestFile->cbFileCurr - pTestTask->DataSeg.cbSeg);
|
---|
304 |
|
---|
305 | /* Allocate data buffer. */
|
---|
306 | pTestTask->DataSeg.pvSeg = RTMemAlloc(pTestTask->DataSeg.cbSeg);
|
---|
307 | if (!pTestTask->DataSeg.pvSeg)
|
---|
308 | return VERR_NO_MEMORY;
|
---|
309 |
|
---|
310 | /* Engage */
|
---|
311 | rc = PDMR3AsyncCompletionEpRead(pTestFile->hEndpoint, pTestTask->off,
|
---|
312 | &pTestTask->DataSeg, 1,
|
---|
313 | pTestTask->DataSeg.cbSeg,
|
---|
314 | pTestTask,
|
---|
315 | &pTestTask->hTask);
|
---|
316 |
|
---|
317 | return rc;
|
---|
318 | }
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Returns true with the given chance in percent.
|
---|
322 | *
|
---|
323 | * @returns true or false
|
---|
324 | * @param iPercentage The percentage of the chance to return true.
|
---|
325 | */
|
---|
326 | static bool tstPDMACTestIsTrue(int iPercentage)
|
---|
327 | {
|
---|
328 | int uRnd = RTRandU32Ex(0, 100);
|
---|
329 |
|
---|
330 | return (uRnd <= iPercentage); /* This should be enough for our purpose */
|
---|
331 | }
|
---|
332 |
|
---|
333 | static int tstPDMACTestFileThread(PVM pVM, PPDMTHREAD pThread)
|
---|
334 | {
|
---|
335 | PPDMACTESTFILE pTestFile = (PPDMACTESTFILE)pThread->pvUser;
|
---|
336 | int iWriteChance = 100; /* Chance to get a write task in percent. */
|
---|
337 | uint32_t cTasksStarted = 0;
|
---|
338 | int rc = VINF_SUCCESS;
|
---|
339 |
|
---|
340 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
341 | return VINF_SUCCESS;
|
---|
342 |
|
---|
343 | while (pTestFile->fRunning)
|
---|
344 | {
|
---|
345 | unsigned iTaskCurr = 0;
|
---|
346 |
|
---|
347 |
|
---|
348 | /* Fill all tasks */
|
---|
349 | while ( (pTestFile->cTasksActiveCurr < pTestFile->cTasksActiveMax)
|
---|
350 | && (iTaskCurr < pTestFile->cTasksActiveMax))
|
---|
351 | {
|
---|
352 | PPDMACTESTFILETASK pTask = &pTestFile->paTasks[iTaskCurr];
|
---|
353 |
|
---|
354 | if (!pTask->fActive)
|
---|
355 | {
|
---|
356 | /* Read or write task? */
|
---|
357 | bool fWrite = tstPDMACTestIsTrue(iWriteChance);
|
---|
358 |
|
---|
359 | ASMAtomicIncU32(&pTestFile->cTasksActiveCurr);
|
---|
360 |
|
---|
361 | if (fWrite)
|
---|
362 | rc = tstPDMACStressTestFileWrite(pTestFile, pTask);
|
---|
363 | else
|
---|
364 | rc = tstPDMACStressTestFileRead(pTestFile, pTask);
|
---|
365 |
|
---|
366 | if (rc != VINF_AIO_TASK_PENDING)
|
---|
367 | tstPDMACStressTestFileTaskCompleted(pVM, pTask, pTestFile, rc);
|
---|
368 |
|
---|
369 | cTasksStarted++;
|
---|
370 | }
|
---|
371 |
|
---|
372 | iTaskCurr++;
|
---|
373 | }
|
---|
374 |
|
---|
375 | /*
|
---|
376 | * Recalc write chance. The bigger the file the lower the chance to have a write.
|
---|
377 | * The minimum chance is 33 percent.
|
---|
378 | */
|
---|
379 | iWriteChance = 100 - (int)(((float)100.0 / pTestFile->cbFileMax) * (float)pTestFile->cbFileCurr);
|
---|
380 | iWriteChance = RT_MAX(33, iWriteChance);
|
---|
381 |
|
---|
382 | /* Wait a random amount of time. (1ms - 100ms) */
|
---|
383 | RTThreadSleep(RTRandU32Ex(1, 100));
|
---|
384 | }
|
---|
385 |
|
---|
386 | /* Wait for the rest to complete. */
|
---|
387 | while (pTestFile->cTasksActiveCurr)
|
---|
388 | RTThreadSleep(250);
|
---|
389 |
|
---|
390 | RTPrintf("Thread exiting: processed %u tasks\n", cTasksStarted);
|
---|
391 | return rc;
|
---|
392 | }
|
---|
393 |
|
---|
394 | static void tstPDMACStressTestFileTaskCompleted(PVM pVM, void *pvUser, void *pvUser2, int rcReq)
|
---|
395 | {
|
---|
396 | PPDMACTESTFILE pTestFile = (PPDMACTESTFILE)pvUser2;
|
---|
397 | PPDMACTESTFILETASK pTestTask = (PPDMACTESTFILETASK)pvUser;
|
---|
398 |
|
---|
399 | if (pTestTask->fWrite)
|
---|
400 | {
|
---|
401 | /* @todo Do something sensible here. */
|
---|
402 | }
|
---|
403 | else
|
---|
404 | {
|
---|
405 | tstPDMACStressTestFileVerify(pTestFile, pTestTask); /* Will assert if it fails */
|
---|
406 | }
|
---|
407 |
|
---|
408 | RTMemFree(pTestTask->DataSeg.pvSeg);
|
---|
409 | pTestTask->fActive = false;
|
---|
410 | AssertMsg(pTestFile->cTasksActiveCurr > 0, ("Trying to complete a non active task\n"));
|
---|
411 | ASMAtomicDecU32(&pTestFile->cTasksActiveCurr);
|
---|
412 | }
|
---|
413 |
|
---|
414 | /**
|
---|
415 | * Sets up a test file creating the I/O thread.
|
---|
416 | *
|
---|
417 | * @returns VBox status code.
|
---|
418 | * @param pVM Pointer to the shared VM instance structure.
|
---|
419 | * @param pTestFile Pointer to the uninitialized test file structure.
|
---|
420 | * @param iTestId Unique test id.
|
---|
421 | */
|
---|
422 | static int tstPDMACStressTestFileOpen(PVM pVM, PPDMACTESTFILE pTestFile, unsigned iTestId)
|
---|
423 | {
|
---|
424 | int rc = VERR_NO_MEMORY;
|
---|
425 |
|
---|
426 | /* Size is a multiple of 512 */
|
---|
427 | pTestFile->cbFileMax = RTRandU64Ex(FILE_SIZE_MIN, FILE_SIZE_MAX) & ~(511UL);
|
---|
428 | pTestFile->cbFileCurr = 0;
|
---|
429 | pTestFile->cbFileSegment = RTRandU32Ex(SEGMENT_SIZE_MIN, RT_MIN(pTestFile->cbFileMax, SEGMENT_SIZE_MAX)) & ~((size_t)511);
|
---|
430 |
|
---|
431 | Assert(pTestFile->cbFileMax >= pTestFile->cbFileSegment);
|
---|
432 |
|
---|
433 | /* Set up the segments array. */
|
---|
434 | pTestFile->cSegments = pTestFile->cbFileMax / pTestFile->cbFileSegment;
|
---|
435 | pTestFile->cSegments += ((pTestFile->cbFileMax % pTestFile->cbFileSegment) > 0) ? 1 : 0;
|
---|
436 |
|
---|
437 | pTestFile->paSegs = (PPDMACTESTFILESEG)RTMemAllocZ(pTestFile->cSegments * sizeof(PDMACTESTFILESEG));
|
---|
438 | if (pTestFile->paSegs)
|
---|
439 | {
|
---|
440 | /* Init the segments */
|
---|
441 | for (unsigned i = 0; i < pTestFile->cSegments; i++)
|
---|
442 | {
|
---|
443 | PPDMACTESTFILESEG pSeg = &pTestFile->paSegs[i];
|
---|
444 |
|
---|
445 | pSeg->off = (RTFOFF)i * pTestFile->cbFileSegment;
|
---|
446 | pSeg->cbSegment = pTestFile->cbFileSegment;
|
---|
447 |
|
---|
448 | /* Let the buffer point to a random position in the test pattern. */
|
---|
449 | uint32_t offTestPattern = RTRandU64Ex(0, g_cbTestPattern - pSeg->cbSegment);
|
---|
450 |
|
---|
451 | pSeg->pbData = g_pbTestPattern + offTestPattern;
|
---|
452 | }
|
---|
453 |
|
---|
454 | /* Init task array. */
|
---|
455 | pTestFile->cTasksActiveMax = RTRandU32Ex(1, TASK_ACTIVE_MAX);
|
---|
456 | pTestFile->paTasks = (PPDMACTESTFILETASK)RTMemAllocZ(pTestFile->cTasksActiveMax * sizeof(PDMACTESTFILETASK));
|
---|
457 | if (pTestFile->paTasks)
|
---|
458 | {
|
---|
459 | /* Create the template */
|
---|
460 | char szDesc[256];
|
---|
461 |
|
---|
462 | RTStrPrintf(szDesc, sizeof(szDesc), "Template-%d", iTestId);
|
---|
463 | rc = PDMR3AsyncCompletionTemplateCreateInternal(pVM, &pTestFile->pTemplate, tstPDMACStressTestFileTaskCompleted, pTestFile, szDesc);
|
---|
464 | if (RT_SUCCESS(rc))
|
---|
465 | {
|
---|
466 | /* Open the endpoint now. Because async completion endpoints cannot create files we have to do it before. */
|
---|
467 | char szFile[RTPATH_MAX];
|
---|
468 |
|
---|
469 | RTStrPrintf(szFile, sizeof(szFile), "tstPDMAsyncCompletionStress-%d.tmp", iTestId);
|
---|
470 |
|
---|
471 | RTFILE FileTmp;
|
---|
472 | rc = RTFileOpen(&FileTmp, szFile, RTFILE_O_READWRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE);
|
---|
473 | if (RT_SUCCESS(rc))
|
---|
474 | {
|
---|
475 | RTFileClose(FileTmp);
|
---|
476 |
|
---|
477 | rc = PDMR3AsyncCompletionEpCreateForFile(&pTestFile->hEndpoint, szFile, PDMACEP_FILE_FLAGS_CACHING, pTestFile->pTemplate);
|
---|
478 | if (RT_SUCCESS(rc))
|
---|
479 | {
|
---|
480 | char szThreadDesc[256];
|
---|
481 |
|
---|
482 | pTestFile->fRunning = true;
|
---|
483 |
|
---|
484 | /* Create the thread creating the I/O for the given file. */
|
---|
485 | RTStrPrintf(szThreadDesc, sizeof(szThreadDesc), "PDMACThread-%d", iTestId);
|
---|
486 | rc = PDMR3ThreadCreate(pVM, &pTestFile->hThread, pTestFile, tstPDMACTestFileThread,
|
---|
487 | NULL, 0, RTTHREADTYPE_IO, szThreadDesc);
|
---|
488 | if (RT_SUCCESS(rc))
|
---|
489 | {
|
---|
490 | rc = PDMR3ThreadResume(pTestFile->hThread);
|
---|
491 | AssertRC(rc);
|
---|
492 |
|
---|
493 | RTPrintf(TESTCASE ": Created test file %s cbFileMax=%llu cbFileSegment=%u cSegments=%u cTasksActiveMax=%u\n",
|
---|
494 | szFile, pTestFile->cbFileMax, pTestFile->cbFileSegment, pTestFile->cSegments, pTestFile->cTasksActiveMax);
|
---|
495 | return VINF_SUCCESS;
|
---|
496 | }
|
---|
497 |
|
---|
498 | PDMR3AsyncCompletionEpClose(pTestFile->hEndpoint);
|
---|
499 | }
|
---|
500 |
|
---|
501 | RTFileDelete(szFile);
|
---|
502 | }
|
---|
503 |
|
---|
504 | PDMR3AsyncCompletionTemplateDestroy(pTestFile->pTemplate);
|
---|
505 | }
|
---|
506 |
|
---|
507 | RTMemFree(pTestFile->paTasks);
|
---|
508 | }
|
---|
509 | else
|
---|
510 | rc = VERR_NO_MEMORY;
|
---|
511 |
|
---|
512 | RTMemFree(pTestFile->paSegs);
|
---|
513 | }
|
---|
514 | else
|
---|
515 | rc = VERR_NO_MEMORY;
|
---|
516 |
|
---|
517 | RTPrintf(TESTCASE ": Opening test file with id %d failed rc=%Rrc\n", iTestId, rc);
|
---|
518 |
|
---|
519 | return rc;
|
---|
520 | }
|
---|
521 |
|
---|
522 | /**
|
---|
523 | * Closes a test file.
|
---|
524 | *
|
---|
525 | * @returns nothing.
|
---|
526 | * @param pTestFile Pointer to the test file.
|
---|
527 | */
|
---|
528 | static void tstPDMACStressTestFileClose(PPDMACTESTFILE pTestFile)
|
---|
529 | {
|
---|
530 | int rcThread;
|
---|
531 | int rc;
|
---|
532 |
|
---|
533 | RTPrintf("Terminating I/O thread, please wait...\n");
|
---|
534 |
|
---|
535 | /* Let the thread know that it should terminate. */
|
---|
536 | pTestFile->fRunning = false;
|
---|
537 |
|
---|
538 | /* Wait for the thread to terminate. */
|
---|
539 | rc = PDMR3ThreadDestroy(pTestFile->hThread, &rcThread);
|
---|
540 |
|
---|
541 | RTPrintf("Thread terminated with status code rc=%Rrc\n", rcThread);
|
---|
542 |
|
---|
543 | /* Free resources */
|
---|
544 | RTMemFree(pTestFile->paTasks);
|
---|
545 | RTMemFree(pTestFile->paSegs);
|
---|
546 | PDMR3AsyncCompletionEpClose(pTestFile->hEndpoint);
|
---|
547 | PDMR3AsyncCompletionTemplateDestroy(pTestFile->pTemplate);
|
---|
548 | }
|
---|
549 |
|
---|
550 | /**
|
---|
551 | * Inits the test pattern.
|
---|
552 | *
|
---|
553 | * @returns VBox status code.
|
---|
554 | */
|
---|
555 | static int tstPDMACStressTestPatternInit(void)
|
---|
556 | {
|
---|
557 | RTPrintf(TESTCASE ": Creating test pattern. Please wait...\n");
|
---|
558 | g_cbTestPattern = TEST_PATTERN_SIZE;
|
---|
559 | g_pbTestPattern = (uint8_t *)RTMemAlloc(g_cbTestPattern);
|
---|
560 | if (!g_pbTestPattern)
|
---|
561 | return VERR_NO_MEMORY;
|
---|
562 |
|
---|
563 | RTRandBytes(g_pbTestPattern, g_cbTestPattern);
|
---|
564 | return VINF_SUCCESS;
|
---|
565 | }
|
---|
566 |
|
---|
567 | static void tstPDMACStressTestPatternDestroy(void)
|
---|
568 | {
|
---|
569 | RTPrintf(TESTCASE ": Destroying test pattern\n");
|
---|
570 | RTMemFree(g_pbTestPattern);
|
---|
571 | }
|
---|
572 |
|
---|
573 | int main(int argc, char *argv[])
|
---|
574 | {
|
---|
575 | int rcRet = 0; /* error count */
|
---|
576 | int rc = VINF_SUCCESS;
|
---|
577 |
|
---|
578 | RTR3InitAndSUPLib();
|
---|
579 |
|
---|
580 | PVM pVM;
|
---|
581 | rc = VMR3Create(1, NULL, NULL, NULL, NULL, &pVM);
|
---|
582 | if (RT_SUCCESS(rc))
|
---|
583 | {
|
---|
584 | /*
|
---|
585 | * Little hack to avoid the VM_ASSERT_EMT assertion.
|
---|
586 | */
|
---|
587 | RTTlsSet(pVM->pUVM->vm.s.idxTLS, &pVM->pUVM->aCpus[0]);
|
---|
588 | pVM->pUVM->aCpus[0].pUVM = pVM->pUVM;
|
---|
589 | pVM->pUVM->aCpus[0].vm.s.NativeThreadEMT = RTThreadNativeSelf();
|
---|
590 |
|
---|
591 | rc = tstPDMACStressTestPatternInit();
|
---|
592 | if (RT_SUCCESS(rc))
|
---|
593 | {
|
---|
594 | unsigned cFilesOpened = 0;
|
---|
595 |
|
---|
596 | /* Open the endpoints. */
|
---|
597 | for (cFilesOpened = 0; cFilesOpened < NR_OPEN_ENDPOINTS; cFilesOpened++)
|
---|
598 | {
|
---|
599 | rc = tstPDMACStressTestFileOpen(pVM, &g_aTestFiles[cFilesOpened], cFilesOpened);
|
---|
600 | if (RT_FAILURE(rc))
|
---|
601 | break;
|
---|
602 | }
|
---|
603 |
|
---|
604 | if (RT_SUCCESS(rc))
|
---|
605 | {
|
---|
606 | /* Tests are running now. */
|
---|
607 | RTPrintf(TESTCASE ": Successfully opened all files. Running tests forever now or until an error is hit :)\n");
|
---|
608 | RTThreadSleep(RT_INDEFINITE_WAIT);
|
---|
609 | }
|
---|
610 |
|
---|
611 | /* Close opened endpoints. */
|
---|
612 | for (unsigned i = 0; i < cFilesOpened; i++)
|
---|
613 | tstPDMACStressTestFileClose(&g_aTestFiles[i]);
|
---|
614 |
|
---|
615 | tstPDMACStressTestPatternDestroy();
|
---|
616 | }
|
---|
617 | else
|
---|
618 | {
|
---|
619 | RTPrintf(TESTCASE ": failed to init test pattern!! rc=%Rrc\n", rc);
|
---|
620 | rcRet++;
|
---|
621 | }
|
---|
622 |
|
---|
623 | rc = VMR3Destroy(pVM);
|
---|
624 | AssertMsg(rc == VINF_SUCCESS, ("%s: Destroying VM failed rc=%Rrc!!\n", __FUNCTION__, rc));
|
---|
625 | }
|
---|
626 | else
|
---|
627 | {
|
---|
628 | RTPrintf(TESTCASE ": failed to create VM!! rc=%Rrc\n", rc);
|
---|
629 | rcRet++;
|
---|
630 | }
|
---|
631 |
|
---|
632 | return rcRet;
|
---|
633 | }
|
---|
634 |
|
---|