VirtualBox

source: vbox/trunk/src/VBox/VMM/PDMAsyncCompletionFileNormal.cpp@ 22949

Last change on this file since 22949 was 22851, checked in by vboxsync, 15 years ago

Fixes for async I/O manager

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.0 KB
Line 
1/* $Id: PDMAsyncCompletionFileNormal.cpp 22851 2009-09-08 23:38:47Z vboxsync $ */
2/** @file
3 * PDM Async I/O - Transport data asynchronous in R3 using EMT.
4 * Async File I/O manager.
5 */
6
7/*
8 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22#define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
23#include <iprt/types.h>
24#include <iprt/asm.h>
25#include <iprt/file.h>
26#include <iprt/mem.h>
27#include <iprt/string.h>
28#include <VBox/log.h>
29
30#include "PDMAsyncCompletionFileInternal.h"
31
32/** The update period for the I/O load statistics in ms. */
33#define PDMACEPFILEMGR_LOAD_UPDATE_PERIOD 1000
34/** Maximum number of requests a manager will handle. */
35#define PDMACEPFILEMGR_REQS_MAX 512 /* @todo: Find better solution wrt. the request number*/
36
37int pdmacFileAioMgrNormalInit(PPDMACEPFILEMGR pAioMgr)
38{
39 int rc = VINF_SUCCESS;
40
41 rc = RTFileAioCtxCreate(&pAioMgr->hAioCtx, RTFILEAIO_UNLIMITED_REQS);
42 if (rc == VERR_OUT_OF_RANGE)
43 rc = RTFileAioCtxCreate(&pAioMgr->hAioCtx, PDMACEPFILEMGR_REQS_MAX);
44
45 if (RT_SUCCESS(rc))
46 {
47 /* Initialize request handle array. */
48 pAioMgr->iFreeEntryNext = 0;
49 pAioMgr->iFreeReqNext = 0;
50 pAioMgr->cReqEntries = PDMACEPFILEMGR_REQS_MAX + 1;
51 pAioMgr->pahReqsFree = (RTFILEAIOREQ *)RTMemAllocZ(pAioMgr->cReqEntries * sizeof(RTFILEAIOREQ));
52
53 if (pAioMgr->pahReqsFree)
54 {
55 return VINF_SUCCESS;
56 }
57 else
58 {
59 RTFileAioCtxDestroy(pAioMgr->hAioCtx);
60 rc = VERR_NO_MEMORY;
61 }
62 }
63
64 return rc;
65}
66
67void pdmacFileAioMgrNormalDestroy(PPDMACEPFILEMGR pAioMgr)
68{
69 RTFileAioCtxDestroy(pAioMgr->hAioCtx);
70
71 while (pAioMgr->iFreeReqNext != pAioMgr->iFreeEntryNext)
72 {
73 RTFileAioReqDestroy(pAioMgr->pahReqsFree[pAioMgr->iFreeReqNext]);
74 pAioMgr->iFreeReqNext = (pAioMgr->iFreeReqNext + 1) % pAioMgr->cReqEntries;
75 }
76
77 RTMemFree(pAioMgr->pahReqsFree);
78}
79
80/**
81 * Sorts the endpoint list with insertion sort.
82 */
83static void pdmacFileAioMgrNormalEndpointsSortByLoad(PPDMACEPFILEMGR pAioMgr)
84{
85 PPDMASYNCCOMPLETIONENDPOINTFILE pEpPrev, pEpCurr, pEpNextToSort;
86
87 pEpPrev = pAioMgr->pEndpointsHead;
88 pEpCurr = pEpPrev->AioMgr.pEndpointNext;
89
90 while (pEpCurr)
91 {
92 /* Remember the next element to sort because the list might change. */
93 pEpNextToSort = pEpCurr->AioMgr.pEndpointNext;
94
95 /* Unlink the current element from the list. */
96 PPDMASYNCCOMPLETIONENDPOINTFILE pPrev = pEpCurr->AioMgr.pEndpointPrev;
97 PPDMASYNCCOMPLETIONENDPOINTFILE pNext = pEpCurr->AioMgr.pEndpointNext;
98
99 if (pPrev)
100 pPrev->AioMgr.pEndpointNext = pNext;
101 else
102 pAioMgr->pEndpointsHead = pNext;
103
104 if (pNext)
105 pNext->AioMgr.pEndpointPrev = pPrev;
106
107 /* Go back until we reached the place to insert the current endpoint into. */
108 while (pEpPrev && (pEpPrev->AioMgr.cReqsPerSec < pEpCurr->AioMgr.cReqsPerSec))
109 pEpPrev = pEpPrev->AioMgr.pEndpointPrev;
110
111 /* Link the endpoint into the list. */
112 if (pEpPrev)
113 pNext = pEpPrev->AioMgr.pEndpointNext;
114 else
115 pNext = pAioMgr->pEndpointsHead;
116
117 pEpCurr->AioMgr.pEndpointNext = pNext;
118 pEpCurr->AioMgr.pEndpointPrev = pEpPrev;
119 pNext->AioMgr.pEndpointPrev = pEpCurr;
120 if (pEpPrev)
121 pEpPrev->AioMgr.pEndpointNext = pEpCurr;
122 else
123 pAioMgr->pEndpointsHead = pEpCurr;
124
125 pEpCurr = pEpNextToSort;
126 }
127
128#ifdef DEBUG
129 /* Validate sorting alogrithm */
130 unsigned cEndpoints = 0;
131 pEpCurr = pAioMgr->pEndpointsHead;
132
133 AssertMsg(pEpCurr, ("No endpoint in the list?\n"));
134 AssertMsg(!pEpCurr->AioMgr.pEndpointPrev, ("First element in the list points to previous element\n"));
135
136 while (pEpCurr)
137 {
138 cEndpoints++;
139
140 PPDMASYNCCOMPLETIONENDPOINTFILE pNext = pEpCurr->AioMgr.pEndpointNext;
141 PPDMASYNCCOMPLETIONENDPOINTFILE pPrev = pEpCurr->AioMgr.pEndpointPrev;
142
143 Assert(!pNext || pNext->AioMgr.cReqsPerSec <= pEpCurr->AioMgr.cReqsPerSec);
144 Assert(!pPrev || pPrev->AioMgr.cReqsPerSec >= pEpCurr->AioMgr.cReqsPerSec);
145
146 pEpCurr = pNext;
147 }
148
149 AssertMsg(cEndpoints == pAioMgr->cEndpoints, ("Endpoints lost during sort!\n"));
150
151#endif
152}
153
154/**
155 * Removes an endpoint from the currently assigned manager.
156 *
157 * @returns TRUE if there are still requests pending on the current manager for this endpoint.
158 * FALSE otherwise.
159 * @param pEndpointRemove The endpoint to remove.
160 */
161static bool pdmacFileAioMgrNormalRemoveEndpoint(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointRemove)
162{
163 PPDMASYNCCOMPLETIONENDPOINTFILE pPrev = pEndpointRemove->AioMgr.pEndpointPrev;
164 PPDMASYNCCOMPLETIONENDPOINTFILE pNext = pEndpointRemove->AioMgr.pEndpointNext;
165 PPDMACEPFILEMGR pAioMgr = pEndpointRemove->pAioMgr;
166
167 pAioMgr->cEndpoints--;
168
169 if (pPrev)
170 pPrev->AioMgr.pEndpointNext = pNext;
171 else
172 pAioMgr->pEndpointsHead = pNext;
173
174 if (pNext)
175 pNext->AioMgr.pEndpointPrev = pPrev;
176
177 /* Make sure that there is no request pending on this manager for the endpoint. */
178 if (!pEndpointRemove->AioMgr.cRequestsActive)
179 {
180 Assert(!pEndpointRemove->pFlushReq);
181
182 /* Reopen the file so that the new endpoint can reassociate with the file */
183 RTFileClose(pEndpointRemove->File);
184 int rc = RTFileOpen(&pEndpointRemove->File, pEndpointRemove->Core.pszUri, pEndpointRemove->fFlags);
185 AssertRC(rc);
186 return false;
187 }
188
189 return true;
190}
191
192/**
193 * Creates a new I/O manager and spreads the I/O load of the endpoints
194 * between the given I/O manager and the new one.
195 *
196 * @returns nothing.
197 * @param pAioMgr The I/O manager with high I/O load.
198 */
199static void pdmacFileAioMgrNormalBalanceLoad(PPDMACEPFILEMGR pAioMgr)
200{
201 PPDMACEPFILEMGR pAioMgrNew = NULL;
202 int rc = VINF_SUCCESS;
203
204 /* Splitting can't be done with only one open endpoint. */
205 if (pAioMgr->cEndpoints > 1)
206 {
207 rc = pdmacFileAioMgrCreate((PPDMASYNCCOMPLETIONEPCLASSFILE)pAioMgr->pEndpointsHead->Core.pEpClass,
208 &pAioMgrNew);
209 if (RT_SUCCESS(rc))
210 {
211 /* We will sort the list by request count per second. */
212 pdmacFileAioMgrNormalEndpointsSortByLoad(pAioMgr);
213
214 /* Now move some endpoints to the new manager. */
215 unsigned cReqsHere = pAioMgr->pEndpointsHead->AioMgr.cReqsPerSec;
216 unsigned cReqsOther = 0;
217 PPDMASYNCCOMPLETIONENDPOINTFILE pCurr = pAioMgr->pEndpointsHead->AioMgr.pEndpointNext;
218
219 while (pCurr)
220 {
221 if (cReqsHere <= cReqsOther)
222 {
223 /*
224 * The other manager has more requests to handle now.
225 * We will keep the current endpoint.
226 */
227 Log(("Keeping endpoint %#p{%s} with %u reqs/s\n", pCurr->Core.pszUri, pCurr->AioMgr.cReqsPerSec));
228 cReqsHere += pCurr->AioMgr.cReqsPerSec;
229 pCurr = pCurr->AioMgr.pEndpointNext;
230 }
231 else
232 {
233 /* Move to other endpoint. */
234 Log(("Moving endpoint %#p{%s} with %u reqs/s to other manager\n", pCurr->Core.pszUri, pCurr->AioMgr.cReqsPerSec));
235 cReqsOther += pCurr->AioMgr.cReqsPerSec;
236
237 PPDMASYNCCOMPLETIONENDPOINTFILE pMove = pCurr;
238
239 pCurr = pCurr->AioMgr.pEndpointNext;
240
241 bool fReqsPending = pdmacFileAioMgrNormalRemoveEndpoint(pMove);
242
243 if (fReqsPending)
244 {
245 pMove->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_REMOVING;
246 pMove->AioMgr.fMoving = true;
247 pMove->AioMgr.pAioMgrDst = pAioMgrNew;
248 }
249 else
250 {
251 pMove->AioMgr.fMoving = false;
252 pMove->AioMgr.pAioMgrDst = NULL;
253 pdmacFileAioMgrAddEndpoint(pAioMgrNew, pMove);
254 }
255 }
256 }
257 }
258 else
259 {
260 /* Don't process further but leave a log entry about reduced performance. */
261 LogRel(("AIOMgr: Could not create new I/O manager (rc=%Rrc). Expect reduced performance\n", rc));
262 }
263 }
264}
265
266/**
267 * Error handler which will create the failsafe managers and destroy the failed I/O manager.
268 *
269 * @returns VBox status code
270 * @param pAioMgr The I/O manager the error ocurred on.
271 * @param rc The error code.
272 */
273static int pdmacFileAioMgrNormalErrorHandler(PPDMACEPFILEMGR pAioMgr, int rc, RT_SRC_POS_DECL)
274{
275 LogRel(("AIOMgr: I/O manager %#p encountered a critical error (rc=%Rrc) during operation. Falling back to failsafe mode. Expect reduced performance\n",
276 pAioMgr, rc));
277 LogRel(("AIOMgr: Error happened in %s:(%u){%s}\n", RT_SRC_POS_ARGS));
278 LogRel(("AIOMgr: Please contact the product vendor\n"));
279
280 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pAioMgr->pEndpointsHead->Core.pEpClass;
281
282 pAioMgr->enmState = PDMACEPFILEMGRSTATE_FAULT;
283 ASMAtomicWriteBool(&pEpClassFile->fFailsafe, true);
284
285 AssertMsgFailed(("Implement\n"));
286 return VINF_SUCCESS;
287}
288
289static int pdmacFileAioMgrNormalProcessTaskList(PPDMACTASKFILE pTaskHead,
290 PPDMACEPFILEMGR pAioMgr,
291 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
292{
293 RTFILEAIOREQ apReqs[20];
294 unsigned cRequests = 0;
295 unsigned cMaxRequests = PDMACEPFILEMGR_REQS_MAX - pAioMgr->cRequestsActive;
296 int rc = VINF_SUCCESS;
297 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
298
299 AssertMsg(pEndpoint->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE,
300 ("Trying to process request lists of a non active endpoint!\n"));
301
302 /* Go through the list and queue the requests until we get a flush request */
303 while (pTaskHead && !pEndpoint->pFlushReq && (cMaxRequests > 0))
304 {
305 PPDMACTASKFILE pCurr = pTaskHead;
306
307 pTaskHead = pTaskHead->pNext;
308
309 AssertMsg(VALID_PTR(pCurr->pEndpoint) && (pCurr->pEndpoint == pEndpoint),
310 ("Endpoints do not match\n"));
311
312 switch (pCurr->enmTransferType)
313 {
314 case PDMACTASKFILETRANSFER_FLUSH:
315 {
316 /* If there is no data transfer request this flush request finished immediately. */
317 if (!pEndpoint->AioMgr.cRequestsActive)
318 {
319 pCurr->pfnCompleted(pCurr, pCurr->pvUser);
320 pdmacFileTaskFree(pEndpoint, pCurr);
321 }
322 else
323 {
324 pEndpoint->pFlushReq = pCurr;
325
326 if (pTaskHead)
327 {
328 /* Add the rest of the tasks to the pending list */
329 if (!pEndpoint->AioMgr.pReqsPendingHead)
330 {
331 Assert(!pEndpoint->AioMgr.pReqsPendingTail);
332 pEndpoint->AioMgr.pReqsPendingHead = pTaskHead;
333 }
334 else
335 {
336 Assert(pEndpoint->AioMgr.pReqsPendingTail);
337 pEndpoint->AioMgr.pReqsPendingTail->pNext = pTaskHead;
338 }
339
340 /* Update the tail. */
341 while (pTaskHead->pNext)
342 pTaskHead = pTaskHead->pNext;
343
344 pEndpoint->AioMgr.pReqsPendingTail = pTaskHead;
345 }
346 }
347 break;
348 }
349 case PDMACTASKFILETRANSFER_READ:
350 case PDMACTASKFILETRANSFER_WRITE:
351 {
352 RTFILEAIOREQ hReq = NIL_RTFILEAIOREQ;
353 void *pvBuf = pCurr->DataSeg.pvSeg;
354
355 /* Get a request handle. */
356 if (pAioMgr->iFreeReqNext != pAioMgr->iFreeEntryNext)
357 {
358 hReq = pAioMgr->pahReqsFree[pAioMgr->iFreeReqNext];
359 pAioMgr->pahReqsFree[pAioMgr->iFreeReqNext] = NIL_RTFILEAIOREQ;
360 pAioMgr->iFreeReqNext = (pAioMgr->iFreeReqNext + 1) % pAioMgr->cReqEntries;
361 }
362 else
363 {
364 rc = RTFileAioReqCreate(&hReq);
365 AssertRC(rc);
366 }
367
368 AssertMsg(hReq != NIL_RTFILEAIOREQ, ("Out of request handles\n"));
369
370 /* Check if the alignment requirements are met.
371 * Offset, transfer size and buffer address
372 * need to be on a 512 boundary. */
373 size_t cbToTransfer = RT_ALIGN_Z(pCurr->DataSeg.cbSeg, 512);
374 RTFOFF OffStart = pCurr->Off & ~(RTFOFF)(512-1);
375 PDMACTASKFILETRANSFER enmTransferType = pCurr->enmTransferType;
376
377 AssertMsg(( (pCurr->enmTransferType == PDMACTASKFILETRANSFER_WRITE)
378 || (OffStart + cbToTransfer <= pEndpoint->cbFile)),
379 ("Read exceeds file size OffStart=%RTfoff cbToTransfer=%d cbFile=%llu\n",
380 OffStart, cbToTransfer, pEndpoint->cbFile));
381
382 pCurr->fPrefetch = false;
383
384 if ( RT_UNLIKELY(cbToTransfer != pCurr->DataSeg.cbSeg)
385 || RT_UNLIKELY(OffStart != pCurr->Off)
386 || ((pEpClassFile->uBitmaskAlignment & (RTR3UINTPTR)pvBuf) != (RTR3UINTPTR)pvBuf))
387 {
388 /* Create bounce buffer. */
389 pCurr->fBounceBuffer = true;
390
391 AssertMsg(pCurr->Off >= OffStart, ("Overflow in calculation Off=%llu OffStart=%llu\n",
392 pCurr->Off, OffStart));
393 pCurr->uBounceBufOffset = pCurr->Off - OffStart;
394
395 /** @todo: I think we need something like a RTMemAllocAligned method here.
396 * Current assumption is that the maximum alignment is 4096byte
397 * (GPT disk on Windows)
398 * so we can use RTMemPageAlloc here.
399 */
400 pCurr->pvBounceBuffer = RTMemPageAlloc(cbToTransfer);
401 AssertPtr(pCurr->pvBounceBuffer);
402 pvBuf = pCurr->pvBounceBuffer;
403
404 if (pCurr->enmTransferType == PDMACTASKFILETRANSFER_WRITE)
405 {
406 if ( RT_UNLIKELY(cbToTransfer != pCurr->DataSeg.cbSeg)
407 || RT_UNLIKELY(OffStart != pCurr->Off))
408 {
409 /* We have to fill the buffer first before we can update the data. */
410 pCurr->fPrefetch = true;
411 enmTransferType = PDMACTASKFILETRANSFER_READ;
412 }
413 else
414 memcpy(pvBuf, pCurr->DataSeg.pvSeg, pCurr->DataSeg.cbSeg);
415 }
416 }
417 else
418 pCurr->fBounceBuffer = false;
419
420 AssertMsg((pEpClassFile->uBitmaskAlignment & (RTR3UINTPTR)pvBuf) == (RTR3UINTPTR)pvBuf,
421 ("AIO: Alignment restrictions not met!\n"));
422
423 if (enmTransferType == PDMACTASKFILETRANSFER_WRITE)
424 {
425 /* Grow the file if needed. */
426 if (RT_UNLIKELY((pCurr->Off + pCurr->DataSeg.cbSeg) > pEndpoint->cbFile))
427 {
428 ASMAtomicWriteU64(&pEndpoint->cbFile, pCurr->Off + pCurr->DataSeg.cbSeg);
429 RTFileSetSize(pEndpoint->File, pCurr->Off + pCurr->DataSeg.cbSeg);
430 }
431
432 rc = RTFileAioReqPrepareWrite(hReq, pEndpoint->File,
433 OffStart, pvBuf, cbToTransfer, pCurr);
434 }
435 else
436 rc = RTFileAioReqPrepareRead(hReq, pEndpoint->File,
437 OffStart, pvBuf, cbToTransfer, pCurr);
438 AssertRC(rc);
439
440 apReqs[cRequests] = hReq;
441 pEndpoint->AioMgr.cReqsProcessed++;
442 cMaxRequests--;
443 cRequests++;
444 if (cRequests == RT_ELEMENTS(apReqs))
445 {
446 pAioMgr->cRequestsActive += cRequests;
447 rc = RTFileAioCtxSubmit(pAioMgr->hAioCtx, apReqs, cRequests);
448 if (RT_FAILURE(rc))
449 {
450 /* @todo implement */
451 AssertMsgFailed(("Implement\n"));
452 }
453
454 cRequests = 0;
455 }
456 break;
457 }
458 default:
459 AssertMsgFailed(("Invalid transfer type %d\n", pCurr->enmTransferType));
460 }
461 }
462
463 if (cRequests)
464 {
465 pAioMgr->cRequestsActive += cRequests;
466 rc = RTFileAioCtxSubmit(pAioMgr->hAioCtx, apReqs, cRequests);
467 AssertMsgReturn(RT_SUCCESS(rc), ("Could not submit %u requests %Rrc\n", cRequests, rc), rc);
468 }
469
470 if (RT_UNLIKELY(!cMaxRequests && pTaskHead && !pEndpoint->pFlushReq))
471 {
472 /*
473 * The I/O manager has no room left for more requests
474 * but there are still requests to process.
475 * Create a new I/O manager and let it handle some endpoints.
476 */
477
478 /* Add the rest of the tasks to the pending list first */
479 if (!pEndpoint->AioMgr.pReqsPendingHead)
480 {
481 Assert(!pEndpoint->AioMgr.pReqsPendingTail);
482 pEndpoint->AioMgr.pReqsPendingHead = pTaskHead;
483 }
484 else
485 {
486 Assert(pEndpoint->AioMgr.pReqsPendingTail);
487 pEndpoint->AioMgr.pReqsPendingTail->pNext = pTaskHead;
488 }
489
490 /* Update the tail. */
491 while (pTaskHead->pNext)
492 pTaskHead = pTaskHead->pNext;
493
494 pEndpoint->AioMgr.pReqsPendingTail = pTaskHead;
495
496 pdmacFileAioMgrNormalBalanceLoad(pAioMgr);
497 }
498
499 return rc;
500}
501
502/**
503 * Adds all pending requests for the given endpoint
504 * until a flush request is encountered or there is no
505 * request anymore.
506 *
507 * @returns VBox status code.
508 * @param pAioMgr The async I/O manager for the endpoint
509 * @param pEndpoint The endpoint to get the requests from.
510 */
511static int pdmacFileAioMgrNormalQueueReqs(PPDMACEPFILEMGR pAioMgr,
512 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
513{
514 int rc = VINF_SUCCESS;
515 PPDMACTASKFILE pTasksHead = NULL;
516
517 AssertMsg(pEndpoint->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE,
518 ("Trying to process request lists of a non active endpoint!\n"));
519
520 Assert(!pEndpoint->pFlushReq);
521
522 /* Check the pending list first */
523 if (pEndpoint->AioMgr.pReqsPendingHead)
524 {
525 pTasksHead = pEndpoint->AioMgr.pReqsPendingHead;
526 /*
527 * Clear the list as the processing routine will insert them into the list
528 * again if it gets a flush request.
529 */
530 pEndpoint->AioMgr.pReqsPendingHead = NULL;
531 pEndpoint->AioMgr.pReqsPendingTail = NULL;
532 rc = pdmacFileAioMgrNormalProcessTaskList(pTasksHead, pAioMgr, pEndpoint);
533 AssertRC(rc);
534 }
535
536 if (!pEndpoint->pFlushReq)
537 {
538 /* Now the request queue. */
539 pTasksHead = pdmacFileEpGetNewTasks(pEndpoint);
540 if (pTasksHead)
541 {
542 rc = pdmacFileAioMgrNormalProcessTaskList(pTasksHead, pAioMgr, pEndpoint);
543 AssertRC(rc);
544 }
545 }
546
547 return rc;
548}
549
550static int pdmacFileAioMgrNormalProcessBlockingEvent(PPDMACEPFILEMGR pAioMgr)
551{
552 int rc = VINF_SUCCESS;
553 bool fNotifyWaiter = false;
554
555 Assert(pAioMgr->fBlockingEventPending);
556
557 switch (pAioMgr->enmBlockingEvent)
558 {
559 case PDMACEPFILEAIOMGRBLOCKINGEVENT_ADD_ENDPOINT:
560 {
561 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointNew = (PPDMASYNCCOMPLETIONENDPOINTFILE)ASMAtomicReadPtr((void * volatile *)&pAioMgr->BlockingEventData.AddEndpoint.pEndpoint);
562 AssertMsg(VALID_PTR(pEndpointNew), ("Adding endpoint event without a endpoint to add\n"));
563
564 pEndpointNew->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
565
566 pEndpointNew->AioMgr.pEndpointNext = pAioMgr->pEndpointsHead;
567 pEndpointNew->AioMgr.pEndpointPrev = NULL;
568 if (pAioMgr->pEndpointsHead)
569 pAioMgr->pEndpointsHead->AioMgr.pEndpointPrev = pEndpointNew;
570 pAioMgr->pEndpointsHead = pEndpointNew;
571
572 /* Assign the completion point to this file. */
573 rc = RTFileAioCtxAssociateWithFile(pAioMgr->hAioCtx, pEndpointNew->File);
574 fNotifyWaiter = true;
575 pAioMgr->cEndpoints++;
576 break;
577 }
578 case PDMACEPFILEAIOMGRBLOCKINGEVENT_REMOVE_ENDPOINT:
579 {
580 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointRemove = (PPDMASYNCCOMPLETIONENDPOINTFILE)ASMAtomicReadPtr((void * volatile *)&pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint);
581 AssertMsg(VALID_PTR(pEndpointRemove), ("Removing endpoint event without a endpoint to remove\n"));
582
583 pEndpointRemove->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_REMOVING;
584 fNotifyWaiter = !pdmacFileAioMgrNormalRemoveEndpoint(pEndpointRemove);
585 break;
586 }
587 case PDMACEPFILEAIOMGRBLOCKINGEVENT_CLOSE_ENDPOINT:
588 {
589 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointClose = (PPDMASYNCCOMPLETIONENDPOINTFILE)ASMAtomicReadPtr((void * volatile *)&pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint);
590 AssertMsg(VALID_PTR(pEndpointClose), ("Close endpoint event without a endpoint to close\n"));
591
592 /* Make sure all tasks finished. Process the queues a last time first. */
593 rc = pdmacFileAioMgrNormalQueueReqs(pAioMgr, pEndpointClose);
594 AssertRC(rc);
595
596 pEndpointClose->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_CLOSING;
597 fNotifyWaiter = !pdmacFileAioMgrNormalRemoveEndpoint(pEndpointClose);
598 break;
599 }
600 case PDMACEPFILEAIOMGRBLOCKINGEVENT_SHUTDOWN:
601 {
602 pAioMgr->enmState = PDMACEPFILEMGRSTATE_SHUTDOWN;
603 if (!pAioMgr->cRequestsActive)
604 fNotifyWaiter = true;
605 break;
606 }
607 case PDMACEPFILEAIOMGRBLOCKINGEVENT_SUSPEND:
608 {
609 pAioMgr->enmState = PDMACEPFILEMGRSTATE_SUSPENDING;
610 break;
611 }
612 case PDMACEPFILEAIOMGRBLOCKINGEVENT_RESUME:
613 {
614 pAioMgr->enmState = PDMACEPFILEMGRSTATE_RUNNING;
615 fNotifyWaiter = true;
616 break;
617 }
618 default:
619 AssertReleaseMsgFailed(("Invalid event type %d\n", pAioMgr->enmBlockingEvent));
620 }
621
622 if (fNotifyWaiter)
623 {
624 ASMAtomicWriteBool(&pAioMgr->fBlockingEventPending, false);
625 pAioMgr->enmBlockingEvent = PDMACEPFILEAIOMGRBLOCKINGEVENT_INVALID;
626
627 /* Release the waiting thread. */
628 rc = RTSemEventSignal(pAioMgr->EventSemBlock);
629 AssertRC(rc);
630 }
631
632 return rc;
633}
634
635/** Helper macro for checking for error codes. */
636#define CHECK_RC(pAioMgr, rc) \
637 if (RT_FAILURE(rc)) \
638 {\
639 int rc2 = pdmacFileAioMgrNormalErrorHandler(pAioMgr, rc, RT_SRC_POS);\
640 return rc2;\
641 }
642
643/**
644 * The normal I/O manager using the RTFileAio* API
645 *
646 * @returns VBox status code.
647 * @param ThreadSelf Handle of the thread.
648 * @param pvUser Opaque user data.
649 */
650int pdmacFileAioMgrNormal(RTTHREAD ThreadSelf, void *pvUser)
651{
652 int rc = VINF_SUCCESS;
653 PPDMACEPFILEMGR pAioMgr = (PPDMACEPFILEMGR)pvUser;
654 uint64_t uMillisEnd = RTTimeMilliTS() + PDMACEPFILEMGR_LOAD_UPDATE_PERIOD;
655
656 while ( (pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING)
657 || (pAioMgr->enmState == PDMACEPFILEMGRSTATE_SUSPENDING))
658 {
659 ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, true);
660 if (!ASMAtomicReadBool(&pAioMgr->fWokenUp))
661 rc = RTSemEventWait(pAioMgr->EventSem, RT_INDEFINITE_WAIT);
662 ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, false);
663 AssertRC(rc);
664
665 LogFlow(("Got woken up\n"));
666 ASMAtomicWriteBool(&pAioMgr->fWokenUp, false);
667
668 /* Check for an external blocking event first. */
669 if (pAioMgr->fBlockingEventPending)
670 {
671 rc = pdmacFileAioMgrNormalProcessBlockingEvent(pAioMgr);
672 CHECK_RC(pAioMgr, rc);
673 }
674
675 if (RT_LIKELY(pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING))
676 {
677 /* Check the assigned endpoints for new tasks if there isn't a flush request active at the moment. */
678 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint = pAioMgr->pEndpointsHead;
679
680 while (pEndpoint)
681 {
682 if (!pEndpoint->pFlushReq && (pEndpoint->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE))
683 {
684 rc = pdmacFileAioMgrNormalQueueReqs(pAioMgr, pEndpoint);
685 CHECK_RC(pAioMgr, rc);
686 }
687
688 pEndpoint = pEndpoint->AioMgr.pEndpointNext;
689 }
690
691 while (pAioMgr->cRequestsActive)
692 {
693 RTFILEAIOREQ apReqs[20];
694 uint32_t cReqsCompleted = 0;
695 size_t cReqsWait;
696
697 if (pAioMgr->cRequestsActive > RT_ELEMENTS(apReqs))
698 cReqsWait = RT_ELEMENTS(apReqs);
699 else
700 cReqsWait = pAioMgr->cRequestsActive;
701
702 rc = RTFileAioCtxWait(pAioMgr->hAioCtx,
703 cReqsWait,
704 RT_INDEFINITE_WAIT, apReqs,
705 RT_ELEMENTS(apReqs), &cReqsCompleted);
706 if (RT_FAILURE(rc) && (rc != VERR_INTERRUPTED))
707 CHECK_RC(pAioMgr, rc);
708
709 for (uint32_t i = 0; i < cReqsCompleted; i++)
710 {
711 size_t cbTransfered = 0;
712 int rcReq = RTFileAioReqGetRC(apReqs[i], &cbTransfered);
713 PPDMACTASKFILE pTask = (PPDMACTASKFILE)RTFileAioReqGetUser(apReqs[i]);
714
715 pEndpoint = pTask->pEndpoint;
716
717 AssertMsg( RT_SUCCESS(rcReq)
718 && ( (cbTransfered == pTask->DataSeg.cbSeg)
719 || (pTask->fBounceBuffer)),
720 ("Task didn't completed successfully (rc=%Rrc) or was incomplete (cbTransfered=%u)\n", rc, cbTransfered));
721
722 if (pTask->fPrefetch)
723 {
724 Assert(pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE);
725 Assert(pTask->fBounceBuffer);
726
727 memcpy(((uint8_t *)pTask->pvBounceBuffer) + pTask->uBounceBufOffset,
728 pTask->DataSeg.pvSeg,
729 pTask->DataSeg.cbSeg);
730
731 /* Write it now. */
732 pTask->fPrefetch = false;
733 size_t cbToTransfer = RT_ALIGN_Z(pTask->DataSeg.cbSeg, 512);
734 RTFOFF OffStart = pTask->Off & ~(RTFOFF)(512-1);
735
736 /* Grow the file if needed. */
737 if (RT_UNLIKELY((pTask->Off + pTask->DataSeg.cbSeg) > pEndpoint->cbFile))
738 {
739 ASMAtomicWriteU64(&pEndpoint->cbFile, pTask->Off + pTask->DataSeg.cbSeg);
740 RTFileSetSize(pEndpoint->File, pTask->Off + pTask->DataSeg.cbSeg);
741 }
742
743 rc = RTFileAioReqPrepareWrite(apReqs[i], pEndpoint->File,
744 OffStart, pTask->pvBounceBuffer, cbToTransfer, pTask);
745 AssertRC(rc);
746 rc = RTFileAioCtxSubmit(pAioMgr->hAioCtx, &apReqs[i], 1);
747 AssertRC(rc);
748 }
749 else
750 {
751 if (pTask->fBounceBuffer)
752 {
753 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_READ)
754 memcpy(pTask->DataSeg.pvSeg,
755 ((uint8_t *)pTask->pvBounceBuffer) + pTask->uBounceBufOffset,
756 pTask->DataSeg.cbSeg);
757
758 RTMemPageFree(pTask->pvBounceBuffer);
759 }
760
761 /* Put the entry on the free array */
762 pAioMgr->pahReqsFree[pAioMgr->iFreeEntryNext] = apReqs[i];
763 pAioMgr->iFreeEntryNext = (pAioMgr->iFreeEntryNext + 1) %pAioMgr->cReqEntries;
764
765 pAioMgr->cRequestsActive--;
766 pEndpoint->AioMgr.cReqsProcessed++;
767
768 /* Call completion callback */
769 pTask->pfnCompleted(pTask, pTask->pvUser);
770 pdmacFileTaskFree(pEndpoint, pTask);
771
772 /*
773 * If there is no request left on the endpoint but a flush request is set
774 * it completed now and we notify the owner.
775 * Furthermore we look for new requests and continue.
776 */
777 if (!pEndpoint->AioMgr.cRequestsActive && pEndpoint->pFlushReq)
778 {
779 /* Call completion callback */
780 pTask = pEndpoint->pFlushReq;
781 pEndpoint->pFlushReq = NULL;
782
783 AssertMsg(pTask->pEndpoint == pEndpoint, ("Endpoint of the flush request does not match assigned one\n"));
784
785 pTask->pfnCompleted(pTask, pTask->pvUser);
786 pdmacFileTaskFree(pEndpoint, pTask);
787 }
788 }
789
790 if (pEndpoint->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE)
791 {
792 if (!pEndpoint->pFlushReq)
793 {
794 /* Check if there are events on the endpoint. */
795 rc = pdmacFileAioMgrNormalQueueReqs(pAioMgr, pEndpoint);
796 CHECK_RC(pAioMgr, rc);
797 }
798 }
799 else if (!pEndpoint->AioMgr.cRequestsActive)
800 {
801 /* Reopen the file so that the new endpoint can reassociate with the file */
802 RTFileClose(pEndpoint->File);
803 rc = RTFileOpen(&pEndpoint->File, pEndpoint->Core.pszUri, pEndpoint->fFlags);
804 AssertRC(rc);
805
806 if (pEndpoint->AioMgr.fMoving)
807 {
808 pEndpoint->AioMgr.fMoving = false;
809 pdmacFileAioMgrAddEndpoint(pEndpoint->AioMgr.pAioMgrDst, pEndpoint);
810 }
811 else
812 {
813 Assert(pAioMgr->fBlockingEventPending);
814 ASMAtomicWriteBool(&pAioMgr->fBlockingEventPending, false);
815
816 /* Release the waiting thread. */
817 rc = RTSemEventSignal(pAioMgr->EventSemBlock);
818 AssertRC(rc);
819 }
820 }
821 }
822
823 /* Check for an external blocking event before we go to sleep again. */
824 if (pAioMgr->fBlockingEventPending)
825 {
826 rc = pdmacFileAioMgrNormalProcessBlockingEvent(pAioMgr);
827 CHECK_RC(pAioMgr, rc);
828 }
829
830 /* Update load statistics. */
831 uint64_t uMillisCurr = RTTimeMilliTS();
832 if (uMillisCurr > uMillisEnd)
833 {
834 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointCurr = pAioMgr->pEndpointsHead;
835
836 /* Calculate timespan. */
837 uMillisCurr -= uMillisEnd;
838
839 while (pEndpointCurr)
840 {
841 pEndpointCurr->AioMgr.cReqsPerSec = pEndpointCurr->AioMgr.cReqsProcessed / (uMillisCurr + PDMACEPFILEMGR_LOAD_UPDATE_PERIOD);
842 pEndpointCurr->AioMgr.cReqsProcessed = 0;
843 pEndpointCurr = pEndpointCurr->AioMgr.pEndpointNext;
844 }
845
846 /* Set new update interval */
847 uMillisEnd = RTTimeMilliTS() + PDMACEPFILEMGR_LOAD_UPDATE_PERIOD;
848 }
849 }
850 }
851 }
852
853 return rc;
854}
855
856#undef CHECK_RC
857
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