VirtualBox

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

Last change on this file since 26944 was 25254, checked in by vboxsync, 15 years ago

tstPDMAsyncCompletion.cpp: -Wshadow

  • 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 25254 2009-12-08 18:28:25Z 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 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, cbLeft;
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 PDMDATASEG 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 PDMDATASEG 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(pVM);
243 AssertMsg(rc == VINF_SUCCESS, ("%s: Destroying VM failed rc=%Rrc!!\n", __FUNCTION__, rc));
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
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