VirtualBox

source: vbox/trunk/src/VBox/Storage/testcase/VDIoBackendMem.cpp@ 44227

Last change on this file since 44227 was 41783, checked in by vboxsync, 12 years ago

Doxygen, comment typos.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 KB
Line 
1/** $Id: VDIoBackendMem.cpp 41783 2012-06-16 19:24:15Z vboxsync $ */
2/** @file
3 *
4 * VBox HDD container test utility, async I/O memory backend
5 */
6
7/*
8 * Copyright (C) 2011 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18#define LOGGROUP LOGGROUP_DEFAULT /** @todo: Log group */
19#include <iprt/err.h>
20#include <iprt/log.h>
21#include <iprt/assert.h>
22#include <iprt/asm.h>
23#include <iprt/mem.h>
24#include <iprt/thread.h>
25#include <iprt/circbuf.h>
26#include <iprt/semaphore.h>
27
28#include "VDMemDisk.h"
29#include "VDIoBackendMem.h"
30
31#define VDMEMIOBACKEND_REQS 1024
32
33/**
34 * Memory I/O request.
35 */
36typedef struct VDIOBACKENDREQ
37{
38 /** I/O request direction. */
39 VDIOTXDIR enmTxDir;
40 /** Memory disk handle. */
41 PVDMEMDISK pMemDisk;
42 /** Start offset. */
43 uint64_t off;
44 /** Size of the transfer. */
45 size_t cbTransfer;
46 /** Number of segments in the array. */
47 unsigned cSegs;
48 /** Completion handler to call. */
49 PFNVDIOCOMPLETE pfnComplete;
50 /** Opaque user data. */
51 void *pvUser;
52 /** Segment array - variable in size */
53 RTSGSEG aSegs[1];
54} VDIOBACKENDREQ, *PVDIOBACKENDREQ;
55
56typedef PVDIOBACKENDREQ *PPVDIOBACKENDREQ;
57
58/**
59 * I/O memory backend
60 */
61typedef struct VDIOBACKENDMEM
62{
63 /** Thread handle for the backend. */
64 RTTHREAD hThreadIo;
65 /** Circular buffer used for submitting requests. */
66 PRTCIRCBUF pRequestRing;
67 /** Size of the buffer in request items. */
68 unsigned cReqsRing;
69 /** Event semaphore the thread waits on for more work. */
70 RTSEMEVENT EventSem;
71 /** Flag whether the server should be still running. */
72 volatile bool fRunning;
73 /** Number of requests waiting in the request buffer. */
74 volatile uint32_t cReqsWaiting;
75} VDIOBACKENDMEM;
76
77static int vdIoBackendMemThread(RTTHREAD hThread, void *pvUser);
78
79/**
80 * Pokes the I/O thread that something interesting happened.
81 *
82 * @returns IPRT status code.
83 *
84 * @param pIoBackend The backend to poke.
85 */
86static int vdIoBackendMemThreadPoke(PVDIOBACKENDMEM pIoBackend)
87{
88 return RTSemEventSignal(pIoBackend->EventSem);
89}
90
91int VDIoBackendMemCreate(PPVDIOBACKENDMEM ppIoBackend)
92{
93 int rc = VINF_SUCCESS;
94 PVDIOBACKENDMEM pIoBackend = NULL;
95
96 pIoBackend = (PVDIOBACKENDMEM)RTMemAllocZ(sizeof(VDIOBACKENDMEM));
97 if (pIoBackend)
98 {
99 rc = RTCircBufCreate(&pIoBackend->pRequestRing, VDMEMIOBACKEND_REQS * sizeof(PVDIOBACKENDREQ));
100 if (RT_SUCCESS(rc))
101 {
102 pIoBackend->cReqsRing = VDMEMIOBACKEND_REQS * sizeof(VDIOBACKENDREQ);
103 pIoBackend->fRunning = true;
104
105 rc = RTSemEventCreate(&pIoBackend->EventSem);
106 if (RT_SUCCESS(rc))
107 {
108 rc = RTThreadCreate(&pIoBackend->hThreadIo, vdIoBackendMemThread, pIoBackend, 0, RTTHREADTYPE_IO,
109 RTTHREADFLAGS_WAITABLE, "MemIo");
110 if (RT_SUCCESS(rc))
111 {
112 *ppIoBackend = pIoBackend;
113
114 LogFlowFunc(("returns success\n"));
115 return VINF_SUCCESS;
116 }
117 RTSemEventDestroy(pIoBackend->EventSem);
118 }
119
120 RTCircBufDestroy(pIoBackend->pRequestRing);
121 }
122
123 RTMemFree(pIoBackend);
124 }
125 else
126 rc = VERR_NO_MEMORY;
127
128 return rc;
129}
130
131int VDIoBackendMemDestroy(PVDIOBACKENDMEM pIoBackend)
132{
133 ASMAtomicXchgBool(&pIoBackend->fRunning, false);
134 vdIoBackendMemThreadPoke(pIoBackend);
135
136 RTThreadWait(pIoBackend->hThreadIo, RT_INDEFINITE_WAIT, NULL);
137 RTSemEventDestroy(pIoBackend->EventSem);
138 RTCircBufDestroy(pIoBackend->pRequestRing);
139 RTMemFree(pIoBackend);
140
141 return VINF_SUCCESS;
142}
143
144int VDIoBackendMemTransfer(PVDIOBACKENDMEM pIoBackend, PVDMEMDISK pMemDisk,
145 VDIOTXDIR enmTxDir, uint64_t off, size_t cbTransfer, PCRTSGSEG paSegs,
146 unsigned cSegs, PFNVDIOCOMPLETE pfnComplete, void *pvUser)
147{
148 PVDIOBACKENDREQ pReq = NULL;
149 PPVDIOBACKENDREQ ppReq = NULL;
150 size_t cbData;
151
152 LogFlowFunc(("Queuing request\n"));
153
154 pReq = (PVDIOBACKENDREQ)RTMemAlloc(RT_OFFSETOF(VDIOBACKENDREQ, aSegs[cSegs]));
155 if (!pReq)
156 return VERR_NO_MEMORY;
157
158 RTCircBufAcquireWriteBlock(pIoBackend->pRequestRing, sizeof(PVDIOBACKENDREQ), (void **)&ppReq, &cbData);
159 if (!pReq)
160 {
161 RTMemFree(pReq);
162 return VERR_NO_MEMORY;
163 }
164
165 Assert(cbData == sizeof(PVDIOBACKENDREQ));
166 pReq->enmTxDir = enmTxDir;
167 pReq->cbTransfer = cbTransfer;
168 pReq->off = off;
169 pReq->pMemDisk = pMemDisk;
170 pReq->cSegs = cSegs;
171 pReq->pfnComplete = pfnComplete;
172 pReq->pvUser = pvUser;
173 for (unsigned i = 0; i < cSegs; i++)
174 {
175 pReq->aSegs[i].pvSeg = paSegs[i].pvSeg;
176 pReq->aSegs[i].cbSeg = paSegs[i].cbSeg;
177 }
178
179 *ppReq = pReq;
180 RTCircBufReleaseWriteBlock(pIoBackend->pRequestRing, sizeof(PVDIOBACKENDREQ));
181 uint32_t cReqsWaiting = ASMAtomicIncU32(&pIoBackend->cReqsWaiting);
182 if (cReqsWaiting == 1)
183 vdIoBackendMemThreadPoke(pIoBackend);
184
185 return VINF_SUCCESS;
186}
187
188/**
189 * I/O thread for the memory backend.
190 *
191 * @returns IPRT status code.
192 *
193 * @param hThread The thread handle.
194 * @param pvUser Opaque user data.
195 */
196static int vdIoBackendMemThread(RTTHREAD hThread, void *pvUser)
197{
198 PVDIOBACKENDMEM pIoBackend = (PVDIOBACKENDMEM)pvUser;
199
200 while (pIoBackend->fRunning)
201 {
202 int rc = RTSemEventWait(pIoBackend->EventSem, RT_INDEFINITE_WAIT);
203 if (RT_FAILURE(rc) || !pIoBackend->fRunning)
204 break;
205
206 PVDIOBACKENDREQ pReq;
207 PPVDIOBACKENDREQ ppReq;
208 size_t cbData;
209 uint32_t cReqsWaiting = ASMAtomicXchgU32(&pIoBackend->cReqsWaiting, 0);
210
211 while (cReqsWaiting)
212 {
213 int rcReq = VINF_SUCCESS;
214
215 /* Do we have another request? */
216 RTCircBufAcquireReadBlock(pIoBackend->pRequestRing, sizeof(PVDIOBACKENDREQ), (void **)&ppReq, &cbData);
217 Assert(!ppReq || cbData == sizeof(PVDIOBACKENDREQ));
218 RTCircBufReleaseReadBlock(pIoBackend->pRequestRing, cbData);
219
220 pReq = *ppReq;
221 cReqsWaiting--;
222
223 LogFlowFunc(("Processing request\n"));
224 switch (pReq->enmTxDir)
225 {
226 case VDIOTXDIR_READ:
227 {
228 RTSGBUF SgBuf;
229 RTSgBufInit(&SgBuf, pReq->aSegs, pReq->cSegs);
230 rcReq = VDMemDiskRead(pReq->pMemDisk, pReq->off, pReq->cbTransfer, &SgBuf);
231 break;
232 }
233 case VDIOTXDIR_WRITE:
234 {
235 RTSGBUF SgBuf;
236 RTSgBufInit(&SgBuf, pReq->aSegs, pReq->cSegs);
237 rcReq = VDMemDiskWrite(pReq->pMemDisk, pReq->off, pReq->cbTransfer, &SgBuf);
238 break;
239 }
240 case VDIOTXDIR_FLUSH:
241 break;
242 default:
243 AssertMsgFailed(("Invalid TX direction!\n"));
244 }
245
246 /* Notify completion. */
247 pReq->pfnComplete(pReq->pvUser, rcReq);
248 RTMemFree(pReq);
249 }
250 }
251
252 return VINF_SUCCESS;
253}
254
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