VirtualBox

source: vbox/trunk/src/VBox/VMM/testcase/tstPDMAsyncCompletion.cpp@ 24323

Last change on this file since 24323 was 23973, checked in by vboxsync, 15 years ago

*,RTFileOpen: Fixing RTFileOpen flag misdesign: The deny, access and action flags are mandatory now.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette