VirtualBox

source: vbox/trunk/src/VBox/VMM/PDMAsyncCompletionFile.cpp@ 26713

Last change on this file since 26713 was 26689, checked in by vboxsync, 15 years ago

AsyncCompletion: Do not use TM to refresh the bandwidth limit. Expired timers are not executed during suspend leading to a hang because of insufficient bandwidth. Let the I/O managers update them instead

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.2 KB
Line 
1/* $Id: PDMAsyncCompletionFile.cpp 26689 2010-02-22 22:11:38Z vboxsync $ */
2/** @file
3 * PDM Async I/O - Transport data asynchronous in R3 using EMT.
4 */
5
6/*
7 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
27#define RT_STRICT
28//#define DEBUG
29#include "PDMInternal.h"
30#include <VBox/pdm.h>
31#include <VBox/mm.h>
32#include <VBox/vm.h>
33#include <VBox/err.h>
34#include <VBox/log.h>
35
36#include <iprt/asm.h>
37#include <iprt/assert.h>
38#include <iprt/critsect.h>
39#include <iprt/env.h>
40#include <iprt/file.h>
41#include <iprt/mem.h>
42#include <iprt/semaphore.h>
43#include <iprt/string.h>
44#include <iprt/thread.h>
45#include <iprt/path.h>
46
47#include "PDMAsyncCompletionFileInternal.h"
48
49/**
50 * Frees a task.
51 *
52 * @returns nothing.
53 * @param pEndpoint Pointer to the endpoint the segment was for.
54 * @param pTask The task to free.
55 */
56void pdmacFileTaskFree(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
57 PPDMACTASKFILE pTask)
58{
59 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
60
61 LogFlowFunc((": pEndpoint=%p pTask=%p\n", pEndpoint, pTask));
62
63 /* Try the per endpoint cache first. */
64 if (pEndpoint->cTasksCached < pEpClass->cTasksCacheMax)
65 {
66 /* Add it to the list. */
67 pEndpoint->pTasksFreeTail->pNext = pTask;
68 pEndpoint->pTasksFreeTail = pTask;
69 ASMAtomicIncU32(&pEndpoint->cTasksCached);
70 }
71 else if (false)
72 {
73 /* Bigger class cache */
74 }
75 else
76 {
77 Log(("Freeing task %p because all caches are full\n", pTask));
78 MMR3HeapFree(pTask);
79 }
80}
81
82/**
83 * Allocates a task segment
84 *
85 * @returns Pointer to the new task segment or NULL
86 * @param pEndpoint Pointer to the endpoint
87 */
88PPDMACTASKFILE pdmacFileTaskAlloc(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
89{
90 PPDMACTASKFILE pTask = NULL;
91
92 /* Try the small per endpoint cache first. */
93 if (pEndpoint->pTasksFreeHead == pEndpoint->pTasksFreeTail)
94 {
95 /* Try the bigger endpoint class cache. */
96 PPDMASYNCCOMPLETIONEPCLASSFILE pEndpointClass = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
97
98#if 0
99 /* We start with the assigned slot id to distribute the load when allocating new tasks. */
100 unsigned iSlot = pEndpoint->iSlotStart;
101 do
102 {
103 pTask = (PPDMASYNCCOMPLETIONTASK)ASMAtomicXchgPtr((void * volatile *)&pEndpointClass->apTaskCache[iSlot], NULL);
104 if (pTask)
105 break;
106
107 iSlot = (iSlot + 1) % RT_ELEMENTS(pEndpointClass->apTaskCache);
108 } while (iSlot != pEndpoint->iSlotStart);
109#endif
110 if (!pTask)
111 {
112 /*
113 * Allocate completely new.
114 * If this fails we return NULL.
115 */
116 int rc = MMR3HeapAllocZEx(pEndpointClass->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
117 sizeof(PDMACTASKFILE),
118 (void **)&pTask);
119 if (RT_FAILURE(rc))
120 pTask = NULL;
121
122 LogFlow(("Allocated task %p\n", pTask));
123 }
124#if 0
125 else
126 {
127 /* Remove the first element and put the rest into the slot again. */
128 PPDMASYNCCOMPLETIONTASK pTaskHeadNew = pTask->pNext;
129
130 pTaskHeadNew->pPrev = NULL;
131
132 /* Put back into the list adding any new tasks. */
133 while (true)
134 {
135 bool fChanged = ASMAtomicCmpXchgPtr((void * volatile *)&pEndpointClass->apTaskCache[iSlot], pTaskHeadNew, NULL);
136
137 if (fChanged)
138 break;
139
140 PPDMASYNCCOMPLETIONTASK pTaskHead = (PPDMASYNCCOMPLETIONTASK)ASMAtomicXchgPtr((void * volatile *)&pEndpointClass->apTaskCache[iSlot], NULL);
141
142 /* The new task could be taken inbetween */
143 if (pTaskHead)
144 {
145 /* Go to the end of the probably much shorter new list. */
146 PPDMASYNCCOMPLETIONTASK pTaskTail = pTaskHead;
147 while (pTaskTail->pNext)
148 pTaskTail = pTaskTail->pNext;
149
150 /* Concatenate */
151 pTaskTail->pNext = pTaskHeadNew;
152
153 pTaskHeadNew = pTaskHead;
154 }
155 /* Another round trying to change the list. */
156 }
157 /* We got a task from the global cache so decrement the counter */
158 ASMAtomicDecU32(&pEndpointClass->cTasksCached);
159 }
160#endif
161 }
162 else
163 {
164 /* Grab a free task from the head. */
165 AssertMsg(pEndpoint->cTasksCached > 0, ("No tasks cached but list contains more than one element\n"));
166
167 pTask = pEndpoint->pTasksFreeHead;
168 pEndpoint->pTasksFreeHead = pTask->pNext;
169 ASMAtomicDecU32(&pEndpoint->cTasksCached);
170 }
171
172 pTask->pNext = NULL;
173
174 return pTask;
175}
176
177PPDMACTASKFILE pdmacFileEpGetNewTasks(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
178{
179 PPDMACTASKFILE pTasks = NULL;
180
181 /*
182 * Get pending tasks.
183 */
184 pTasks = (PPDMACTASKFILE)ASMAtomicXchgPtr((void * volatile *)&pEndpoint->pTasksNewHead, NULL);
185
186 /* Reverse the list to process in FIFO order. */
187 if (pTasks)
188 {
189 PPDMACTASKFILE pTask = pTasks;
190
191 pTasks = NULL;
192
193 while (pTask)
194 {
195 PPDMACTASKFILE pCur = pTask;
196 pTask = pTask->pNext;
197 pCur->pNext = pTasks;
198 pTasks = pCur;
199 }
200 }
201
202 return pTasks;
203}
204
205static void pdmacFileAioMgrWakeup(PPDMACEPFILEMGR pAioMgr)
206{
207 bool fWokenUp = ASMAtomicXchgBool(&pAioMgr->fWokenUp, true);
208
209 if (!fWokenUp)
210 {
211 int rc = VINF_SUCCESS;
212 bool fWaitingEventSem = ASMAtomicReadBool(&pAioMgr->fWaitingEventSem);
213
214 if (fWaitingEventSem)
215 rc = RTSemEventSignal(pAioMgr->EventSem);
216
217 AssertRC(rc);
218 }
219}
220
221static int pdmacFileAioMgrWaitForBlockingEvent(PPDMACEPFILEMGR pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT enmEvent)
222{
223 int rc = VINF_SUCCESS;
224
225 ASMAtomicWriteU32((volatile uint32_t *)&pAioMgr->enmBlockingEvent, enmEvent);
226 Assert(!pAioMgr->fBlockingEventPending);
227 ASMAtomicXchgBool(&pAioMgr->fBlockingEventPending, true);
228
229 /* Wakeup the async I/O manager */
230 pdmacFileAioMgrWakeup(pAioMgr);
231
232 /* Wait for completion. */
233 rc = RTSemEventWait(pAioMgr->EventSemBlock, RT_INDEFINITE_WAIT);
234 AssertRC(rc);
235
236 ASMAtomicXchgBool(&pAioMgr->fBlockingEventPending, false);
237 ASMAtomicWriteU32((volatile uint32_t *)&pAioMgr->enmBlockingEvent, PDMACEPFILEAIOMGRBLOCKINGEVENT_INVALID);
238
239 return rc;
240}
241
242int pdmacFileAioMgrAddEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
243{
244 int rc;
245
246 rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
247 AssertRCReturn(rc, rc);
248
249 ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.AddEndpoint.pEndpoint, pEndpoint);
250 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_ADD_ENDPOINT);
251
252 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
253
254 if (RT_SUCCESS(rc))
255 ASMAtomicWritePtr((void * volatile *)&pEndpoint->pAioMgr, pAioMgr);
256
257 return rc;
258}
259
260static int pdmacFileAioMgrRemoveEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
261{
262 int rc;
263
264 rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
265 AssertRCReturn(rc, rc);
266
267 ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint, pEndpoint);
268 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_REMOVE_ENDPOINT);
269
270 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
271
272 return rc;
273}
274
275static int pdmacFileAioMgrCloseEndpoint(PPDMACEPFILEMGR pAioMgr, PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
276{
277 int rc;
278
279 rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
280 AssertRCReturn(rc, rc);
281
282 ASMAtomicWritePtr((void * volatile *)&pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint, pEndpoint);
283 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_CLOSE_ENDPOINT);
284
285 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
286
287 return rc;
288}
289
290static int pdmacFileAioMgrShutdown(PPDMACEPFILEMGR pAioMgr)
291{
292 int rc;
293
294 rc = RTCritSectEnter(&pAioMgr->CritSectBlockingEvent);
295 AssertRCReturn(rc, rc);
296
297 rc = pdmacFileAioMgrWaitForBlockingEvent(pAioMgr, PDMACEPFILEAIOMGRBLOCKINGEVENT_SHUTDOWN);
298
299 RTCritSectLeave(&pAioMgr->CritSectBlockingEvent);
300
301 return rc;
302}
303
304int pdmacFileEpAddTask(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint, PPDMACTASKFILE pTask)
305{
306 PPDMACTASKFILE pNext;
307 do
308 {
309 pNext = pEndpoint->pTasksNewHead;
310 pTask->pNext = pNext;
311 } while (!ASMAtomicCmpXchgPtr((void * volatile *)&pEndpoint->pTasksNewHead, (void *)pTask, (void *)pNext));
312
313 pdmacFileAioMgrWakeup((PPDMACEPFILEMGR)ASMAtomicReadPtr((void * volatile *)&pEndpoint->pAioMgr));
314
315 return VINF_SUCCESS;
316}
317
318void pdmacFileEpTaskCompleted(PPDMACTASKFILE pTask, void *pvUser)
319{
320 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pvUser;
321
322 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_FLUSH)
323 {
324 pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core, true);
325 }
326 else
327 {
328 Assert((uint32_t)pTask->DataSeg.cbSeg == pTask->DataSeg.cbSeg && (int32_t)pTask->DataSeg.cbSeg >= 0);
329 uint32_t uOld = ASMAtomicSubS32(&pTaskFile->cbTransferLeft, (int32_t)pTask->DataSeg.cbSeg);
330
331 if (!(uOld - pTask->DataSeg.cbSeg)
332 && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
333 pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core, true);
334 }
335}
336
337int pdmacFileEpTaskInitiate(PPDMASYNCCOMPLETIONTASK pTask,
338 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
339 PCPDMDATASEG paSegments, size_t cSegments,
340 size_t cbTransfer, PDMACTASKFILETRANSFER enmTransfer)
341{
342 int rc = VINF_SUCCESS;
343 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
344 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
345 PPDMACEPFILEMGR pAioMgr = pEpFile->pAioMgr;
346
347 Assert( (enmTransfer == PDMACTASKFILETRANSFER_READ)
348 || (enmTransfer == PDMACTASKFILETRANSFER_WRITE));
349
350 Assert((uint32_t)cbTransfer == cbTransfer && (int32_t)cbTransfer >= 0);
351 ASMAtomicWriteS32(&pTaskFile->cbTransferLeft, (int32_t)cbTransfer);
352 ASMAtomicWriteBool(&pTaskFile->fCompleted, false);
353
354 for (unsigned i = 0; i < cSegments; i++)
355 {
356 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
357 AssertPtr(pIoTask);
358
359 pIoTask->pEndpoint = pEpFile;
360 pIoTask->enmTransferType = enmTransfer;
361 pIoTask->Off = off;
362 pIoTask->DataSeg.cbSeg = paSegments[i].cbSeg;
363 pIoTask->DataSeg.pvSeg = paSegments[i].pvSeg;
364 pIoTask->pvUser = pTaskFile;
365 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
366
367 /* Send it off to the I/O manager. */
368 pdmacFileEpAddTask(pEpFile, pIoTask);
369 off += paSegments[i].cbSeg;
370 cbTransfer -= paSegments[i].cbSeg;
371 }
372
373 AssertMsg(!cbTransfer, ("Incomplete transfer %u bytes left\n", cbTransfer));
374
375 if (ASMAtomicReadS32(&pTaskFile->cbTransferLeft) == 0
376 && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
377 pdmR3AsyncCompletionCompleteTask(pTask, false);
378 else
379 rc = VINF_AIO_TASK_PENDING;
380
381 return rc;
382}
383
384/**
385 * Creates a new async I/O manager.
386 *
387 * @returns VBox status code.
388 * @param pEpClass Pointer to the endpoint class data.
389 * @param ppAioMgr Where to store the pointer to the new async I/O manager on success.
390 * @param fFailsafe Flag to force a failsafe manager even if the global flag is not set.
391 */
392int pdmacFileAioMgrCreate(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass, PPPDMACEPFILEMGR ppAioMgr, bool fFailsafe)
393{
394 int rc = VINF_SUCCESS;
395 PPDMACEPFILEMGR pAioMgrNew;
396
397 LogFlowFunc((": Entered\n"));
398
399 rc = MMR3HeapAllocZEx(pEpClass->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION, sizeof(PDMACEPFILEMGR), (void **)&pAioMgrNew);
400 if (RT_SUCCESS(rc))
401 {
402 pAioMgrNew->fFailsafe = fFailsafe || pEpClass->fFailsafe;
403
404 rc = RTSemEventCreate(&pAioMgrNew->EventSem);
405 if (RT_SUCCESS(rc))
406 {
407 rc = RTSemEventCreate(&pAioMgrNew->EventSemBlock);
408 if (RT_SUCCESS(rc))
409 {
410 rc = RTCritSectInit(&pAioMgrNew->CritSectBlockingEvent);
411 if (RT_SUCCESS(rc))
412 {
413 /* Init the rest of the manager. */
414 if (!pAioMgrNew->fFailsafe)
415 rc = pdmacFileAioMgrNormalInit(pAioMgrNew);
416
417 if (RT_SUCCESS(rc))
418 {
419 pAioMgrNew->enmState = PDMACEPFILEMGRSTATE_RUNNING;
420
421 rc = RTThreadCreateF(&pAioMgrNew->Thread,
422 pAioMgrNew->fFailsafe
423 ? pdmacFileAioMgrFailsafe
424 : pdmacFileAioMgrNormal,
425 pAioMgrNew,
426 0,
427 RTTHREADTYPE_IO,
428 0,
429 "AioMgr%d-%s", pEpClass->cAioMgrs,
430 pAioMgrNew->fFailsafe
431 ? "F"
432 : "N");
433 if (RT_SUCCESS(rc))
434 {
435 /* Link it into the list. */
436 RTCritSectEnter(&pEpClass->CritSect);
437 pAioMgrNew->pNext = pEpClass->pAioMgrHead;
438 if (pEpClass->pAioMgrHead)
439 pEpClass->pAioMgrHead->pPrev = pAioMgrNew;
440 pEpClass->pAioMgrHead = pAioMgrNew;
441 pEpClass->cAioMgrs++;
442 RTCritSectLeave(&pEpClass->CritSect);
443
444 *ppAioMgr = pAioMgrNew;
445
446 Log(("PDMAC: Successfully created new file AIO Mgr {%s}\n", RTThreadGetName(pAioMgrNew->Thread)));
447 return VINF_SUCCESS;
448 }
449 pdmacFileAioMgrNormalDestroy(pAioMgrNew);
450 }
451 RTCritSectDelete(&pAioMgrNew->CritSectBlockingEvent);
452 }
453 RTSemEventDestroy(pAioMgrNew->EventSem);
454 }
455 RTSemEventDestroy(pAioMgrNew->EventSemBlock);
456 }
457 MMR3HeapFree(pAioMgrNew);
458 }
459
460 LogFlowFunc((": Leave rc=%Rrc\n", rc));
461
462 return rc;
463}
464
465/**
466 * Destroys a async I/O manager.
467 *
468 * @returns nothing.
469 * @param pAioMgr The async I/O manager to destroy.
470 */
471static void pdmacFileAioMgrDestroy(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile, PPDMACEPFILEMGR pAioMgr)
472{
473 int rc = pdmacFileAioMgrShutdown(pAioMgr);
474 AssertRC(rc);
475
476 /* Unlink from the list. */
477 rc = RTCritSectEnter(&pEpClassFile->CritSect);
478 AssertRC(rc);
479
480 PPDMACEPFILEMGR pPrev = pAioMgr->pPrev;
481 PPDMACEPFILEMGR pNext = pAioMgr->pNext;
482
483 if (pPrev)
484 pPrev->pNext = pNext;
485 else
486 pEpClassFile->pAioMgrHead = pNext;
487
488 if (pNext)
489 pNext->pPrev = pPrev;
490
491 pEpClassFile->cAioMgrs--;
492 rc = RTCritSectLeave(&pEpClassFile->CritSect);
493 AssertRC(rc);
494
495 /* Free the ressources. */
496 RTCritSectDelete(&pAioMgr->CritSectBlockingEvent);
497 RTSemEventDestroy(pAioMgr->EventSem);
498 if (!pAioMgr->fFailsafe)
499 pdmacFileAioMgrNormalDestroy(pAioMgr);
500
501 MMR3HeapFree(pAioMgr);
502}
503
504static int pdmacFileBwMgrInitialize(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile,
505 PCFGMNODE pCfgNode, PPPDMACFILEBWMGR ppBwMgr)
506{
507 int rc = VINF_SUCCESS;
508 PPDMACFILEBWMGR pBwMgr = NULL;
509
510 rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
511 sizeof(PDMACFILEBWMGR),
512 (void **)&pBwMgr);
513 if (RT_SUCCESS(rc))
514 {
515 /* Init I/O flow control. */
516 rc = CFGMR3QueryU32Def(pCfgNode, "VMTransferPerSecMax", &pBwMgr->cbVMTransferPerSecMax, UINT32_MAX);
517 AssertLogRelRCReturn(rc, rc);
518 rc = CFGMR3QueryU32Def(pCfgNode, "VMTransferPerSecStart", &pBwMgr->cbVMTransferPerSecStart, _1M);
519 AssertLogRelRCReturn(rc, rc);
520 rc = CFGMR3QueryU32Def(pCfgNode, "VMTransferPerSecStep", &pBwMgr->cbVMTransferPerSecStep, _1M);
521 AssertLogRelRCReturn(rc, rc);
522
523 pBwMgr->cbVMTransferAllowed = pBwMgr->cbVMTransferPerSecStart;
524 pBwMgr->tsUpdatedLast = RTTimeSystemNanoTS();
525
526 *ppBwMgr = pBwMgr;
527 }
528
529 return rc;
530}
531
532static void pdmacFileBwMgrDestroy(PPDMACFILEBWMGR pBwMgr)
533{
534 MMR3HeapFree(pBwMgr);
535}
536
537static void pdmacFileBwRef(PPDMACFILEBWMGR pBwMgr)
538{
539 pBwMgr->cRefs++;
540}
541
542static void pdmacFileBwUnref(PPDMACFILEBWMGR pBwMgr)
543{
544 Assert(pBwMgr->cRefs > 0);
545 pBwMgr->cRefs--;
546}
547
548bool pdmacFileBwMgrIsTransferAllowed(PPDMACFILEBWMGR pBwMgr, uint32_t cbTransfer)
549{
550 bool fAllowed = false;
551
552 LogFlowFunc(("pBwMgr=%p cbTransfer=%u\n", pBwMgr, cbTransfer));
553
554 uint32_t cbOld = ASMAtomicSubU32(&pBwMgr->cbVMTransferAllowed, cbTransfer);
555 if (RT_LIKELY(cbOld >= cbTransfer))
556 fAllowed = true;
557 else
558 {
559 /* We are out of ressources Check if we can update again. */
560 uint64_t tsNow = RTTimeSystemNanoTS();
561 uint64_t tsUpdatedLast = ASMAtomicUoReadU64(&pBwMgr->tsUpdatedLast);
562
563 if (tsNow - tsUpdatedLast >= (1000*1000*1000))
564 {
565 if (ASMAtomicCmpXchgU64(&pBwMgr->tsUpdatedLast, tsNow, tsUpdatedLast))
566 {
567 if (pBwMgr->cbVMTransferPerSecStart < pBwMgr->cbVMTransferPerSecMax)
568 {
569 pBwMgr->cbVMTransferPerSecStart = RT_MIN(pBwMgr->cbVMTransferPerSecMax, pBwMgr->cbVMTransferPerSecStart + pBwMgr->cbVMTransferPerSecStep);
570 LogFlow(("AIOMgr: Increasing maximum bandwidth to %u bytes/sec\n", pBwMgr->cbVMTransferPerSecStart));
571 }
572
573 /* Update */
574 ASMAtomicWriteU32(&pBwMgr->cbVMTransferAllowed, pBwMgr->cbVMTransferPerSecStart - cbTransfer);
575 fAllowed = true;
576 LogFlow(("AIOMgr: Refreshed bandwidth\n"));
577 }
578 }
579 else
580 ASMAtomicAddU32(&pBwMgr->cbVMTransferAllowed, cbTransfer);
581 }
582
583 LogFlowFunc(("fAllowed=%RTbool\n", fAllowed));
584
585 return fAllowed;
586}
587
588static int pdmacFileInitialize(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals, PCFGMNODE pCfgNode)
589{
590 int rc = VINF_SUCCESS;
591 RTFILEAIOLIMITS AioLimits; /** < Async I/O limitations. */
592
593 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
594
595 rc = RTFileAioGetLimits(&AioLimits);
596#ifdef DEBUG
597 if (RT_SUCCESS(rc) && RTEnvExist("VBOX_ASYNC_IO_FAILBACK"))
598 rc = VERR_ENV_VAR_NOT_FOUND;
599#endif
600 if (RT_FAILURE(rc))
601 {
602 LogRel(("AIO: Async I/O manager not supported (rc=%Rrc). Falling back to failsafe manager\n",
603 rc));
604 pEpClassFile->fFailsafe = true;
605 }
606 else
607 {
608 pEpClassFile->uBitmaskAlignment = AioLimits.cbBufferAlignment ? ~((RTR3UINTPTR)AioLimits.cbBufferAlignment - 1) : RTR3UINTPTR_MAX;
609 pEpClassFile->cReqsOutstandingMax = AioLimits.cReqsOutstandingMax;
610
611 /* The user can force the failsafe manager. */
612 rc = CFGMR3QueryBoolDef(pCfgNode, "UseFailsafeIo", &pEpClassFile->fFailsafe, false);
613 AssertLogRelRCReturn(rc, rc);
614
615 if (pEpClassFile->fFailsafe)
616 LogRel(("AIOMgr: Failsafe I/O was requested by user\n"));
617 }
618
619 /* Init critical section. */
620 rc = RTCritSectInit(&pEpClassFile->CritSect);
621 if (RT_SUCCESS(rc))
622 {
623 /* Check if the host cache should be used too. */
624#ifndef RT_OS_LINUX
625 rc = CFGMR3QueryBoolDef(pCfgNode, "HostCacheEnabled", &pEpClassFile->fHostCacheEnabled, false);
626 AssertLogRelRCReturn(rc, rc);
627#else
628 /*
629 * Host cache + async I/O is not supported on Linux. Check if the user enabled the cache,
630 * leave a warning and disable it always.
631 */
632 bool fDummy;
633 rc = CFGMR3QueryBool(pCfgNode, "HostCacheEnabled", &fDummy);
634 if (RT_SUCCESS(rc))
635 LogRel(("AIOMgr: The host cache is not supported with async I/O on Linux\n"));
636
637 pEpClassFile->fHostCacheEnabled = false;
638#endif
639
640 /* Check if the cache was disabled by the user. */
641 rc = CFGMR3QueryBoolDef(pCfgNode, "CacheEnabled", &pEpClassFile->fCacheEnabled, true);
642 AssertLogRelRCReturn(rc, rc);
643
644 if (pEpClassFile->fCacheEnabled)
645 {
646 /* Init cache structure */
647 rc = pdmacFileCacheInit(pEpClassFile, pCfgNode);
648 if (RT_FAILURE(rc))
649 {
650 RTCritSectDelete(&pEpClassFile->CritSect);
651 pEpClassFile->fCacheEnabled = false;
652 LogRel(("AIOMgr: Failed to initialise the cache (rc=%Rrc), disabled caching\n"));
653 }
654 }
655 else
656 LogRel(("AIOMgr: Cache was globally disabled\n"));
657
658 rc = pdmacFileBwMgrInitialize(pEpClassFile, pCfgNode, &pEpClassFile->pBwMgr);
659 if (RT_FAILURE(rc))
660 RTCritSectDelete(&pEpClassFile->CritSect);
661 }
662
663 return rc;
664}
665
666static void pdmacFileTerminate(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals)
667{
668 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
669
670 /* All endpoints should be closed at this point. */
671 AssertMsg(!pEpClassFile->Core.pEndpointsHead, ("There are still endpoints left\n"));
672
673 /* Destroy all left async I/O managers. */
674 while (pEpClassFile->pAioMgrHead)
675 pdmacFileAioMgrDestroy(pEpClassFile, pEpClassFile->pAioMgrHead);
676
677 /* Destroy the cache. */
678 if (pEpClassFile->fCacheEnabled)
679 pdmacFileCacheDestroy(pEpClassFile);
680
681 RTCritSectDelete(&pEpClassFile->CritSect);
682 pdmacFileBwMgrDestroy(pEpClassFile->pBwMgr);
683}
684
685static int pdmacFileEpInitialize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
686 const char *pszUri, uint32_t fFlags)
687{
688 int rc = VINF_SUCCESS;
689 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
690 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
691 bool fUseFailsafeManager = pEpClassFile->fFailsafe;
692
693 AssertMsgReturn((fFlags & ~(PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_CACHING)) == 0,
694 ("PDMAsyncCompletion: Invalid flag specified\n"), VERR_INVALID_PARAMETER);
695
696 unsigned fFileFlags = fFlags & PDMACEP_FILE_FLAGS_READ_ONLY
697 ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
698 : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE;
699
700 if (!pEpClassFile->fFailsafe)
701 {
702 fFileFlags |= (RTFILE_O_ASYNC_IO | RTFILE_O_WRITE_THROUGH);
703
704 /*
705 * We only disable the cache if the size of the file is a multiple of 512.
706 * Certain hosts like Windows, Linux and Solaris require that transfer sizes
707 * are aligned to the volume sector size.
708 * If not we just make sure that the data is written to disk with RTFILE_O_WRITE_THROUGH
709 * which will trash the host cache but ensures that the host cache will not
710 * contain dirty buffers.
711 */
712 RTFILE File = NIL_RTFILE;
713
714 rc = RTFileOpen(&File, pszUri, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
715 if (RT_SUCCESS(rc))
716 {
717 uint64_t cbSize;
718
719 rc = RTFileGetSize(File, &cbSize);
720 if (RT_SUCCESS(rc) && ((cbSize % 512) == 0))
721 {
722 fFileFlags &= ~RTFILE_O_WRITE_THROUGH;
723
724#if defined(RT_OS_LINUX)
725 AssertMsg(!pEpClassFile->fHostCacheEnabled, ("Host cache + async I/O is not supported on Linux\n"));
726 fFileFlags |= RTFILE_O_NO_CACHE;
727#else
728 if (!pEpClassFile->fHostCacheEnabled)
729 fFileFlags |= RTFILE_O_NO_CACHE;
730#endif
731 }
732
733 pEpFile->cbFile = cbSize;
734
735 RTFileClose(File);
736 }
737 }
738
739 /* Open with final flags. */
740 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
741 if ((rc == VERR_INVALID_FUNCTION) || (rc == VERR_INVALID_PARAMETER))
742 {
743 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed with %Rrc\n",
744 pszUri, fFileFlags, rc));
745 /*
746 * Solaris doesn't support directio on ZFS so far. :-\
747 * Trying to enable it returns VERR_INVALID_FUNCTION
748 * (ENOTTY). Remove it and hope for the best.
749 * ZFS supports write throttling in case applications
750 * write more data than can be synced to the disk
751 * without blocking the whole application.
752 *
753 * On Linux we have the same problem with cifs.
754 * Have to disable async I/O here too because it requires O_DIRECT.
755 */
756 fFileFlags &= ~RTFILE_O_NO_CACHE;
757
758#ifdef RT_OS_LINUX
759 fFileFlags &= ~RTFILE_O_ASYNC_IO;
760 fUseFailsafeManager = true;
761#endif
762
763 /* Open again. */
764 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
765
766 if (RT_FAILURE(rc))
767 {
768 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed AGAIN(!) with %Rrc\n",
769 pszUri, fFileFlags, rc));
770 }
771 }
772
773 if (RT_SUCCESS(rc))
774 {
775 pEpFile->fFlags = fFileFlags;
776
777 rc = RTFileGetSize(pEpFile->File, (uint64_t *)&pEpFile->cbFile);
778 if (RT_SUCCESS(rc) && (pEpFile->cbFile == 0))
779 {
780 /* Could be a block device */
781 rc = RTFileSeek(pEpFile->File, 0, RTFILE_SEEK_END, (uint64_t *)&pEpFile->cbFile);
782 }
783
784 if (RT_SUCCESS(rc))
785 {
786 /* Initialize the segment cache */
787 rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
788 sizeof(PDMACTASKFILE),
789 (void **)&pEpFile->pTasksFreeHead);
790 if (RT_SUCCESS(rc))
791 {
792 PPDMACEPFILEMGR pAioMgr = NULL;
793
794 pEpFile->pTasksFreeTail = pEpFile->pTasksFreeHead;
795 pEpFile->cTasksCached = 0;
796 pEpFile->pBwMgr = pEpClassFile->pBwMgr;
797 pdmacFileBwRef(pEpFile->pBwMgr);
798
799 if (fUseFailsafeManager)
800 {
801 /* Safe mode. Every file has its own async I/O manager. */
802 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, true);
803 AssertRC(rc);
804 }
805 else
806 {
807 if ( (fFlags & PDMACEP_FILE_FLAGS_CACHING)
808 && (pEpClassFile->fCacheEnabled))
809 {
810 pEpFile->fCaching = true;
811 rc = pdmacFileEpCacheInit(pEpFile, pEpClassFile);
812 if (RT_FAILURE(rc))
813 {
814 LogRel(("AIOMgr: Endpoint for \"%s\" was opened with caching but initializing cache failed. Disabled caching\n", pszUri));
815 pEpFile->fCaching = false;
816 }
817 }
818
819 pAioMgr = pEpClassFile->pAioMgrHead;
820
821 /* Check for an idling not failsafe one or create new if not found */
822 while (pAioMgr && pAioMgr->fFailsafe)
823 pAioMgr = pAioMgr->pNext;
824
825 if (!pAioMgr)
826 {
827 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, false);
828 AssertRC(rc);
829 }
830 }
831
832 pEpFile->AioMgr.pTreeRangesLocked = (PAVLRFOFFTREE)RTMemAllocZ(sizeof(AVLRFOFFTREE));
833 if (!pEpFile->AioMgr.pTreeRangesLocked)
834 rc = VERR_NO_MEMORY;
835 else
836 {
837 pEpFile->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
838
839 /* Assign the endpoint to the thread. */
840 rc = pdmacFileAioMgrAddEndpoint(pAioMgr, pEpFile);
841 if (RT_FAILURE(rc))
842 {
843 RTMemFree(pEpFile->AioMgr.pTreeRangesLocked);
844 MMR3HeapFree(pEpFile->pTasksFreeHead);
845 pdmacFileBwUnref(pEpFile->pBwMgr);
846 }
847 }
848 }
849 }
850
851 if (RT_FAILURE(rc))
852 RTFileClose(pEpFile->File);
853 }
854
855#ifdef VBOX_WITH_STATISTICS
856 if (RT_SUCCESS(rc))
857 {
858 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatRead,
859 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
860 STAMUNIT_TICKS_PER_CALL, "Time taken to read from the endpoint",
861 "/PDM/AsyncCompletion/File/%s/Read", RTPathFilename(pEpFile->Core.pszUri));
862
863 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatWrite,
864 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
865 STAMUNIT_TICKS_PER_CALL, "Time taken to write to the endpoint",
866 "/PDM/AsyncCompletion/File/%s/Write", RTPathFilename(pEpFile->Core.pszUri));
867 }
868#endif
869
870 return rc;
871}
872
873static int pdmacFileEpRangesLockedDestroy(PAVLRFOFFNODECORE pNode, void *pvUser)
874{
875 AssertMsgFailed(("The locked ranges tree should be empty at that point\n"));
876 return VINF_SUCCESS;
877}
878
879static int pdmacFileEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
880{
881 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
882 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
883
884 /* Make sure that all tasks finished for this endpoint. */
885 int rc = pdmacFileAioMgrCloseEndpoint(pEpFile->pAioMgr, pEpFile);
886 AssertRC(rc);
887
888 /*
889 * If the async I/O manager is in failsafe mode this is the only endpoint
890 * he processes and thus can be destroyed now.
891 */
892 if (pEpFile->pAioMgr->fFailsafe)
893 pdmacFileAioMgrDestroy(pEpClassFile, pEpFile->pAioMgr);
894
895 /* Free cached tasks. */
896 PPDMACTASKFILE pTask = pEpFile->pTasksFreeHead;
897
898 while (pTask)
899 {
900 PPDMACTASKFILE pTaskFree = pTask;
901 pTask = pTask->pNext;
902 MMR3HeapFree(pTaskFree);
903 }
904
905 /* Free the cached data. */
906 if (pEpFile->fCaching)
907 pdmacFileEpCacheDestroy(pEpFile);
908
909 /* Remove from the bandwidth manager */
910 pdmacFileBwUnref(pEpFile->pBwMgr);
911
912 /* Destroy the locked ranges tree now. */
913 RTAvlrFileOffsetDestroy(pEpFile->AioMgr.pTreeRangesLocked, pdmacFileEpRangesLockedDestroy, NULL);
914
915 RTFileClose(pEpFile->File);
916
917#ifdef VBOX_WITH_STATISTICS
918 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatRead);
919 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatWrite);
920#endif
921
922 return VINF_SUCCESS;
923}
924
925static int pdmacFileEpRead(PPDMASYNCCOMPLETIONTASK pTask,
926 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
927 PCPDMDATASEG paSegments, size_t cSegments,
928 size_t cbRead)
929{
930 int rc = VINF_SUCCESS;
931 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
932
933 STAM_PROFILE_ADV_START(&pEpFile->StatRead, Read);
934
935 if (pEpFile->fCaching)
936 rc = pdmacFileEpCacheRead(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
937 off, paSegments, cSegments, cbRead);
938 else
939 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbRead,
940 PDMACTASKFILETRANSFER_READ);
941
942 STAM_PROFILE_ADV_STOP(&pEpFile->StatRead, Read);
943
944 return rc;
945}
946
947static int pdmacFileEpWrite(PPDMASYNCCOMPLETIONTASK pTask,
948 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
949 PCPDMDATASEG paSegments, size_t cSegments,
950 size_t cbWrite)
951{
952 int rc = VINF_SUCCESS;
953 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
954
955 if (RT_UNLIKELY(pEpFile->fReadonly))
956 return VERR_NOT_SUPPORTED;
957
958 STAM_PROFILE_ADV_START(&pEpFile->StatWrite, Write);
959
960 if (pEpFile->fCaching)
961 rc = pdmacFileEpCacheWrite(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
962 off, paSegments, cSegments, cbWrite);
963 else
964 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbWrite,
965 PDMACTASKFILETRANSFER_WRITE);
966
967 STAM_PROFILE_ADV_STOP(&pEpFile->StatWrite, Write);
968
969 return rc;
970}
971
972static int pdmacFileEpFlush(PPDMASYNCCOMPLETIONTASK pTask,
973 PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
974{
975 int rc = VINF_SUCCESS;
976 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
977 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
978
979 if (RT_UNLIKELY(pEpFile->fReadonly))
980 return VERR_NOT_SUPPORTED;
981
982 pTaskFile->cbTransferLeft = 0;
983
984 if (pEpFile->fCaching)
985 rc = pdmacFileEpCacheFlush(pEpFile, pTaskFile);
986 else
987 {
988 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
989 AssertPtr(pIoTask);
990
991 pIoTask->pEndpoint = pEpFile;
992 pIoTask->enmTransferType = PDMACTASKFILETRANSFER_FLUSH;
993 pIoTask->pvUser = pTaskFile;
994 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
995 pdmacFileEpAddTask(pEpFile, pIoTask);
996 rc = VINF_AIO_TASK_PENDING;
997 }
998
999 return rc;
1000}
1001
1002static int pdmacFileEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t *pcbSize)
1003{
1004 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
1005
1006 *pcbSize = ASMAtomicReadU64(&pEpFile->cbFile);
1007
1008 return VINF_SUCCESS;
1009}
1010
1011const PDMASYNCCOMPLETIONEPCLASSOPS g_PDMAsyncCompletionEndpointClassFile =
1012{
1013 /* u32Version */
1014 PDMAC_EPCLASS_OPS_VERSION,
1015 /* pcszName */
1016 "File",
1017 /* enmClassType */
1018 PDMASYNCCOMPLETIONEPCLASSTYPE_FILE,
1019 /* cbEndpointClassGlobal */
1020 sizeof(PDMASYNCCOMPLETIONEPCLASSFILE),
1021 /* cbEndpoint */
1022 sizeof(PDMASYNCCOMPLETIONENDPOINTFILE),
1023 /* cbTask */
1024 sizeof(PDMASYNCCOMPLETIONTASKFILE),
1025 /* pfnInitialize */
1026 pdmacFileInitialize,
1027 /* pfnTerminate */
1028 pdmacFileTerminate,
1029 /* pfnEpInitialize. */
1030 pdmacFileEpInitialize,
1031 /* pfnEpClose */
1032 pdmacFileEpClose,
1033 /* pfnEpRead */
1034 pdmacFileEpRead,
1035 /* pfnEpWrite */
1036 pdmacFileEpWrite,
1037 /* pfnEpFlush */
1038 pdmacFileEpFlush,
1039 /* pfnEpGetSize */
1040 pdmacFileEpGetSize,
1041 /* u32VersionEnd */
1042 PDMAC_EPCLASS_OPS_VERSION
1043};
1044
Note: See TracBrowser for help on using the repository browser.

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