VirtualBox

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

Last change on this file since 27410 was 27323, checked in by vboxsync, 15 years ago

AsyncCompletion: Print bandwidth limit in the release log

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