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