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