1 | /* $Id: tstPDMAsyncCompletion.cpp 56287 2015-06-09 11:15:22Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM Asynchronous Completion Testcase.
|
---|
4 | *
|
---|
5 | * This testcase is for testing the async completion interface.
|
---|
6 | * It implements a file copy program which uses the interface to copy the data.
|
---|
7 | *
|
---|
8 | * Use: ./tstPDMAsyncCompletion <source> <destination>
|
---|
9 | */
|
---|
10 |
|
---|
11 | /*
|
---|
12 | * Copyright (C) 2008-2015 Oracle Corporation
|
---|
13 | *
|
---|
14 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
15 | * available from http://www.virtualbox.org. This file is free software;
|
---|
16 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
17 | * General Public License (GPL) as published by the Free Software
|
---|
18 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
19 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
20 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
21 | */
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
|
---|
27 |
|
---|
28 | #include "VMInternal.h" /* UVM */
|
---|
29 | #include <VBox/vmm/vm.h>
|
---|
30 | #include <VBox/vmm/uvm.h>
|
---|
31 | #include <VBox/vmm/pdmasynccompletion.h>
|
---|
32 | #include <VBox/vmm/vmm.h>
|
---|
33 | #include <VBox/vmm/cpum.h>
|
---|
34 | #include <VBox/err.h>
|
---|
35 | #include <VBox/log.h>
|
---|
36 | #include <VBox/vmm/pdmapi.h>
|
---|
37 | #include <iprt/alloc.h>
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/file.h>
|
---|
41 | #include <iprt/initterm.h>
|
---|
42 | #include <iprt/semaphore.h>
|
---|
43 | #include <iprt/stream.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include <iprt/thread.h>
|
---|
46 |
|
---|
47 | #define TESTCASE "tstPDMAsyncCompletion"
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * Number of simultaneous active tasks.
|
---|
51 | */
|
---|
52 | #define NR_TASKS 80
|
---|
53 | #define BUFFER_SIZE (64*_1K)
|
---|
54 |
|
---|
55 | /* Buffers to store data in .*/
|
---|
56 | uint8_t *g_AsyncCompletionTasksBuffer[NR_TASKS];
|
---|
57 | PPDMASYNCCOMPLETIONTASK g_AsyncCompletionTasks[NR_TASKS];
|
---|
58 | volatile uint32_t g_cTasksLeft;
|
---|
59 | RTSEMEVENT g_FinishedEventSem;
|
---|
60 |
|
---|
61 | void pfnAsyncTaskCompleted(PVM pVM, void *pvUser, void *pvUser2, int rc)
|
---|
62 | {
|
---|
63 | LogFlow((TESTCASE ": %s: pVM=%p pvUser=%p pvUser2=%p\n", __FUNCTION__, pVM, pvUser, pvUser2));
|
---|
64 | NOREF(rc);
|
---|
65 |
|
---|
66 | uint32_t cTasksStillLeft = ASMAtomicDecU32(&g_cTasksLeft);
|
---|
67 |
|
---|
68 | if (!cTasksStillLeft)
|
---|
69 | {
|
---|
70 | /* All tasks processed. Wakeup main. */
|
---|
71 | RTSemEventSignal(g_FinishedEventSem);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Entry point.
|
---|
77 | */
|
---|
78 | extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
|
---|
79 | {
|
---|
80 | int rcRet = 0; /* error count */
|
---|
81 | PPDMASYNCCOMPLETIONENDPOINT pEndpointSrc, pEndpointDst;
|
---|
82 |
|
---|
83 | RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
|
---|
84 |
|
---|
85 | if (argc != 3)
|
---|
86 | {
|
---|
87 | RTPrintf(TESTCASE ": Usage is ./tstPDMAsyncCompletion <source> <dest>\n");
|
---|
88 | return 1;
|
---|
89 | }
|
---|
90 |
|
---|
91 | PVM pVM;
|
---|
92 | PUVM pUVM;
|
---|
93 | int rc = VMR3Create(1, NULL, NULL, NULL, NULL, NULL, &pVM, &pUVM);
|
---|
94 | if (RT_SUCCESS(rc))
|
---|
95 | {
|
---|
96 | /*
|
---|
97 | * Little hack to avoid the VM_ASSERT_EMT assertion.
|
---|
98 | */
|
---|
99 | RTTlsSet(pVM->pUVM->vm.s.idxTLS, &pVM->pUVM->aCpus[0]);
|
---|
100 | pVM->pUVM->aCpus[0].pUVM = pVM->pUVM;
|
---|
101 | pVM->pUVM->aCpus[0].vm.s.NativeThreadEMT = RTThreadNativeSelf();
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * Create the template.
|
---|
105 | */
|
---|
106 | PPDMASYNCCOMPLETIONTEMPLATE pTemplate;
|
---|
107 | rc = PDMR3AsyncCompletionTemplateCreateInternal(pVM, &pTemplate, pfnAsyncTaskCompleted, NULL, "Test");
|
---|
108 | if (RT_FAILURE(rc))
|
---|
109 | {
|
---|
110 | RTPrintf(TESTCASE ": Error while creating the template!! rc=%d\n", rc);
|
---|
111 | return 1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * Create event semaphore.
|
---|
116 | */
|
---|
117 | rc = RTSemEventCreate(&g_FinishedEventSem);
|
---|
118 | AssertRC(rc);
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * Create the temporary buffers.
|
---|
122 | */
|
---|
123 | for (unsigned i=0; i < NR_TASKS; i++)
|
---|
124 | {
|
---|
125 | g_AsyncCompletionTasksBuffer[i] = (uint8_t *)RTMemAllocZ(BUFFER_SIZE);
|
---|
126 | if (!g_AsyncCompletionTasksBuffer[i])
|
---|
127 | {
|
---|
128 | RTPrintf(TESTCASE ": out of memory!\n");
|
---|
129 | return ++rcRet;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /* Create the destination as the async completion API can't do this. */
|
---|
134 | RTFILE FileTmp;
|
---|
135 | rc = RTFileOpen(&FileTmp, argv[2], RTFILE_O_READWRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_DENY_NONE);
|
---|
136 | if (RT_FAILURE(rc))
|
---|
137 | {
|
---|
138 | RTPrintf(TESTCASE ": Error while creating the destination!! rc=%d\n", rc);
|
---|
139 | return ++rcRet;
|
---|
140 | }
|
---|
141 | RTFileClose(FileTmp);
|
---|
142 |
|
---|
143 | /* Create our file endpoint */
|
---|
144 | rc = PDMR3AsyncCompletionEpCreateForFile(&pEndpointSrc, argv[1], 0, pTemplate);
|
---|
145 | if (RT_SUCCESS(rc))
|
---|
146 | {
|
---|
147 | rc = PDMR3AsyncCompletionEpCreateForFile(&pEndpointDst, argv[2], 0, pTemplate);
|
---|
148 | if (RT_SUCCESS(rc))
|
---|
149 | {
|
---|
150 | PDMR3PowerOn(pVM);
|
---|
151 |
|
---|
152 | /* Wait for all threads to finish initialization. */
|
---|
153 | RTThreadSleep(100);
|
---|
154 |
|
---|
155 | int fReadPass = true;
|
---|
156 | uint64_t cbSrc;
|
---|
157 | size_t offSrc = 0;
|
---|
158 | size_t offDst = 0;
|
---|
159 | uint32_t cTasksUsed = 0;
|
---|
160 |
|
---|
161 | rc = PDMR3AsyncCompletionEpGetSize(pEndpointSrc, &cbSrc);
|
---|
162 | if (RT_SUCCESS(rc))
|
---|
163 | {
|
---|
164 | /* Copy the data. */
|
---|
165 | for (;;)
|
---|
166 | {
|
---|
167 | if (fReadPass)
|
---|
168 | {
|
---|
169 | cTasksUsed = (BUFFER_SIZE * NR_TASKS) <= (cbSrc - offSrc)
|
---|
170 | ? NR_TASKS
|
---|
171 | : ((cbSrc - offSrc) / BUFFER_SIZE)
|
---|
172 | + ((cbSrc - offSrc) % BUFFER_SIZE) > 0
|
---|
173 | ? 1
|
---|
174 | : 0;
|
---|
175 |
|
---|
176 | g_cTasksLeft = cTasksUsed;
|
---|
177 |
|
---|
178 | for (uint32_t i = 0; i < cTasksUsed; i++)
|
---|
179 | {
|
---|
180 | size_t cbRead = ((size_t)offSrc + BUFFER_SIZE) <= cbSrc ? BUFFER_SIZE : cbSrc - offSrc;
|
---|
181 | RTSGSEG DataSeg;
|
---|
182 |
|
---|
183 | DataSeg.pvSeg = g_AsyncCompletionTasksBuffer[i];
|
---|
184 | DataSeg.cbSeg = cbRead;
|
---|
185 |
|
---|
186 | rc = PDMR3AsyncCompletionEpRead(pEndpointSrc, offSrc, &DataSeg, 1, cbRead, NULL,
|
---|
187 | &g_AsyncCompletionTasks[i]);
|
---|
188 | AssertRC(rc);
|
---|
189 | offSrc += cbRead;
|
---|
190 | if (offSrc == cbSrc)
|
---|
191 | break;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | else
|
---|
195 | {
|
---|
196 | g_cTasksLeft = cTasksUsed;
|
---|
197 |
|
---|
198 | for (uint32_t i = 0; i < cTasksUsed; i++)
|
---|
199 | {
|
---|
200 | size_t cbWrite = (offDst + BUFFER_SIZE) <= cbSrc ? BUFFER_SIZE : cbSrc - offDst;
|
---|
201 | RTSGSEG DataSeg;
|
---|
202 |
|
---|
203 | DataSeg.pvSeg = g_AsyncCompletionTasksBuffer[i];
|
---|
204 | DataSeg.cbSeg = cbWrite;
|
---|
205 |
|
---|
206 | rc = PDMR3AsyncCompletionEpWrite(pEndpointDst, offDst, &DataSeg, 1, cbWrite, NULL,
|
---|
207 | &g_AsyncCompletionTasks[i]);
|
---|
208 | AssertRC(rc);
|
---|
209 | offDst += cbWrite;
|
---|
210 | if (offDst == cbSrc)
|
---|
211 | break;
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | rc = RTSemEventWait(g_FinishedEventSem, RT_INDEFINITE_WAIT);
|
---|
216 | AssertRC(rc);
|
---|
217 |
|
---|
218 | if (!fReadPass && (offDst == cbSrc))
|
---|
219 | break;
|
---|
220 | else if (fReadPass)
|
---|
221 | fReadPass = false;
|
---|
222 | else
|
---|
223 | {
|
---|
224 | cTasksUsed = 0;
|
---|
225 | fReadPass = true;
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 | else
|
---|
230 | {
|
---|
231 | RTPrintf(TESTCASE ": Error querying size of the endpoint!! rc=%d\n", rc);
|
---|
232 | rcRet++;
|
---|
233 | }
|
---|
234 |
|
---|
235 | PDMR3PowerOff(pVM);
|
---|
236 | PDMR3AsyncCompletionEpClose(pEndpointDst);
|
---|
237 | }
|
---|
238 | PDMR3AsyncCompletionEpClose(pEndpointSrc);
|
---|
239 | }
|
---|
240 |
|
---|
241 | rc = VMR3Destroy(pUVM);
|
---|
242 | AssertMsg(rc == VINF_SUCCESS, ("%s: Destroying VM failed rc=%Rrc!!\n", __FUNCTION__, rc));
|
---|
243 | VMR3ReleaseUVM(pUVM);
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * Clean up.
|
---|
247 | */
|
---|
248 | for (uint32_t i = 0; i < NR_TASKS; i++)
|
---|
249 | {
|
---|
250 | RTMemFree(g_AsyncCompletionTasksBuffer[i]);
|
---|
251 | }
|
---|
252 | }
|
---|
253 | else
|
---|
254 | {
|
---|
255 | RTPrintf(TESTCASE ": failed to create VM!! rc=%Rrc\n", rc);
|
---|
256 | rcRet++;
|
---|
257 | }
|
---|
258 |
|
---|
259 | return rcRet;
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | #if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
|
---|
264 | /**
|
---|
265 | * Main entry point.
|
---|
266 | */
|
---|
267 | int main(int argc, char **argv, char **envp)
|
---|
268 | {
|
---|
269 | return TrustedMain(argc, argv, envp);
|
---|
270 | }
|
---|
271 | #endif
|
---|
272 |
|
---|