VirtualBox

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

Last change on this file since 24285 was 24278, checked in by vboxsync, 15 years ago

AsyncCompletion: Cleanup in the cache and more statistics (more to come)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.7 KB
Line 
1/* $Id: PDMAsyncCompletionFile.cpp 24278 2009-11-02 20:26:07Z 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#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);
324 }
325 else
326 {
327 uint32_t uOld = ASMAtomicSubU32(&pTaskFile->cbTransferLeft, pTask->DataSeg.cbSeg);
328
329 if (!(uOld - pTask->DataSeg.cbSeg)
330 && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
331 pdmR3AsyncCompletionCompleteTask(&pTaskFile->Core);
332 }
333}
334
335int pdmacFileEpTaskInitiate(PPDMASYNCCOMPLETIONTASK pTask,
336 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
337 PCPDMDATASEG paSegments, size_t cSegments,
338 size_t cbTransfer, PDMACTASKFILETRANSFER enmTransfer)
339{
340 int rc = VINF_SUCCESS;
341 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
342 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
343 PPDMACEPFILEMGR pAioMgr = pEpFile->pAioMgr;
344
345 Assert( (enmTransfer == PDMACTASKFILETRANSFER_READ)
346 || (enmTransfer == PDMACTASKFILETRANSFER_WRITE));
347
348 ASMAtomicWriteS32(&pTaskFile->cbTransferLeft, cbTransfer);
349 ASMAtomicWriteBool(&pTaskFile->fCompleted, false);
350
351 for (unsigned i = 0; i < cSegments; i++)
352 {
353 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
354 AssertPtr(pIoTask);
355
356 pIoTask->pEndpoint = pEpFile;
357 pIoTask->enmTransferType = enmTransfer;
358 pIoTask->Off = off;
359 pIoTask->DataSeg.cbSeg = paSegments[i].cbSeg;
360 pIoTask->DataSeg.pvSeg = paSegments[i].pvSeg;
361 pIoTask->pvUser = pTaskFile;
362 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
363
364 /* Send it off to the I/O manager. */
365 pdmacFileEpAddTask(pEpFile, pIoTask);
366 off += paSegments[i].cbSeg;
367 cbTransfer -= paSegments[i].cbSeg;
368 }
369
370 AssertMsg(!cbTransfer, ("Incomplete transfer %u bytes left\n", cbTransfer));
371
372 if (ASMAtomicReadS32(&pTaskFile->cbTransferLeft) == 0
373 && !ASMAtomicXchgBool(&pTaskFile->fCompleted, true))
374 pdmR3AsyncCompletionCompleteTask(pTask);
375
376 return VINF_SUCCESS;
377}
378
379/**
380 * Creates a new async I/O manager.
381 *
382 * @returns VBox status code.
383 * @param pEpClass Pointer to the endpoint class data.
384 * @param ppAioMgr Where to store the pointer to the new async I/O manager on success.
385 * @param fFailsafe Flag to force a failsafe manager even if the global flag is not set.
386 */
387int pdmacFileAioMgrCreate(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass, PPPDMACEPFILEMGR ppAioMgr, bool fFailsafe)
388{
389 int rc = VINF_SUCCESS;
390 PPDMACEPFILEMGR pAioMgrNew;
391
392 LogFlowFunc((": Entered\n"));
393
394 rc = MMR3HeapAllocZEx(pEpClass->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION, sizeof(PDMACEPFILEMGR), (void **)&pAioMgrNew);
395 if (RT_SUCCESS(rc))
396 {
397 pAioMgrNew->fFailsafe = fFailsafe ? true : pEpClass->fFailsafe;
398
399 rc = RTSemEventCreate(&pAioMgrNew->EventSem);
400 if (RT_SUCCESS(rc))
401 {
402 rc = RTSemEventCreate(&pAioMgrNew->EventSemBlock);
403 if (RT_SUCCESS(rc))
404 {
405 rc = RTCritSectInit(&pAioMgrNew->CritSectBlockingEvent);
406 if (RT_SUCCESS(rc))
407 {
408 /* Init the rest of the manager. */
409 if (!pAioMgrNew->fFailsafe)
410 rc = pdmacFileAioMgrNormalInit(pAioMgrNew);
411
412 if (RT_SUCCESS(rc))
413 {
414 pAioMgrNew->enmState = PDMACEPFILEMGRSTATE_RUNNING;
415
416 rc = RTThreadCreateF(&pAioMgrNew->Thread,
417 pAioMgrNew->fFailsafe
418 ? pdmacFileAioMgrFailsafe
419 : pdmacFileAioMgrNormal,
420 pAioMgrNew,
421 0,
422 RTTHREADTYPE_IO,
423 0,
424 "AioMgr%d-%s", pEpClass->cAioMgrs,
425 pAioMgrNew->fFailsafe
426 ? "F"
427 : "N");
428 if (RT_SUCCESS(rc))
429 {
430 /* Link it into the list. */
431 RTCritSectEnter(&pEpClass->CritSect);
432 pAioMgrNew->pNext = pEpClass->pAioMgrHead;
433 if (pEpClass->pAioMgrHead)
434 pEpClass->pAioMgrHead->pPrev = pAioMgrNew;
435 pEpClass->pAioMgrHead = pAioMgrNew;
436 pEpClass->cAioMgrs++;
437 RTCritSectLeave(&pEpClass->CritSect);
438
439 *ppAioMgr = pAioMgrNew;
440
441 Log(("PDMAC: Successfully created new file AIO Mgr {%s}\n", RTThreadGetName(pAioMgrNew->Thread)));
442 return VINF_SUCCESS;
443 }
444 pdmacFileAioMgrNormalDestroy(pAioMgrNew);
445 }
446 RTCritSectDelete(&pAioMgrNew->CritSectBlockingEvent);
447 }
448 RTSemEventDestroy(pAioMgrNew->EventSem);
449 }
450 RTSemEventDestroy(pAioMgrNew->EventSemBlock);
451 }
452 MMR3HeapFree(pAioMgrNew);
453 }
454
455 LogFlowFunc((": Leave rc=%Rrc\n", rc));
456
457 return rc;
458}
459
460/**
461 * Destroys a async I/O manager.
462 *
463 * @returns nothing.
464 * @param pAioMgr The async I/O manager to destroy.
465 */
466static void pdmacFileAioMgrDestroy(PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile, PPDMACEPFILEMGR pAioMgr)
467{
468 int rc = pdmacFileAioMgrShutdown(pAioMgr);
469 AssertRC(rc);
470
471 /* Unlink from the list. */
472 rc = RTCritSectEnter(&pEpClassFile->CritSect);
473 AssertRC(rc);
474
475 PPDMACEPFILEMGR pPrev = pAioMgr->pPrev;
476 PPDMACEPFILEMGR pNext = pAioMgr->pNext;
477
478 if (pPrev)
479 pPrev->pNext = pNext;
480 else
481 pEpClassFile->pAioMgrHead = pNext;
482
483 if (pNext)
484 pNext->pPrev = pPrev;
485
486 pEpClassFile->cAioMgrs--;
487
488 rc = RTCritSectLeave(&pEpClassFile->CritSect);
489 AssertRC(rc);
490
491 /* Free the ressources. */
492 RTCritSectDelete(&pAioMgr->CritSectBlockingEvent);
493 RTSemEventDestroy(pAioMgr->EventSem);
494 if (!pAioMgr->fFailsafe)
495 pdmacFileAioMgrNormalDestroy(pAioMgr);
496
497 MMR3HeapFree(pAioMgr);
498}
499
500static int pdmacFileInitialize(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals, PCFGMNODE pCfgNode)
501{
502 int rc = VINF_SUCCESS;
503 RTFILEAIOLIMITS AioLimits; /** < Async I/O limitations. */
504
505 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
506
507 rc = RTFileAioGetLimits(&AioLimits);
508#ifdef DEBUG
509 if (RT_SUCCESS(rc) && RTEnvExist("VBOX_ASYNC_IO_FAILBACK"))
510 rc = VERR_ENV_VAR_NOT_FOUND;
511#endif
512 if (RT_FAILURE(rc))
513 {
514 LogRel(("AIO: Async I/O manager not supported (rc=%Rrc). Falling back to failsafe manager\n",
515 rc));
516 pEpClassFile->fFailsafe = true;
517 }
518 else
519 {
520 pEpClassFile->uBitmaskAlignment = AioLimits.cbBufferAlignment ? ~((RTR3UINTPTR)AioLimits.cbBufferAlignment - 1) : RTR3UINTPTR_MAX;
521 pEpClassFile->cReqsOutstandingMax = AioLimits.cReqsOutstandingMax;
522 pEpClassFile->fFailsafe = false;
523 }
524
525 /* Init critical section. */
526 rc = RTCritSectInit(&pEpClassFile->CritSect);
527 if (RT_SUCCESS(rc))
528 {
529 /* Init cache structure */
530 rc = pdmacFileCacheInit(pEpClassFile, pCfgNode);
531 if (RT_FAILURE(rc))
532 RTCritSectDelete(&pEpClassFile->CritSect);
533 }
534
535 return rc;
536}
537
538static void pdmacFileTerminate(PPDMASYNCCOMPLETIONEPCLASS pClassGlobals)
539{
540 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pClassGlobals;
541
542 /* All endpoints should be closed at this point. */
543 AssertMsg(!pEpClassFile->Core.pEndpointsHead, ("There are still endpoints left\n"));
544
545 /* Destroy all left async I/O managers. */
546 while (pEpClassFile->pAioMgrHead)
547 pdmacFileAioMgrDestroy(pEpClassFile, pEpClassFile->pAioMgrHead);
548
549 RTCritSectDelete(&pEpClassFile->CritSect);
550}
551
552static int pdmacFileEpInitialize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
553 const char *pszUri, uint32_t fFlags)
554{
555 int rc = VINF_SUCCESS;
556 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
557 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
558 bool fUseFailsafeManager = pEpClassFile->fFailsafe;
559
560 AssertMsgReturn((fFlags & ~(PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_CACHING)) == 0,
561 ("PDMAsyncCompletion: Invalid flag specified\n"), VERR_INVALID_PARAMETER);
562
563 unsigned fFileFlags = fFlags & PDMACEP_FILE_FLAGS_READ_ONLY
564 ? RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE
565 : RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE;
566
567 if (!pEpClassFile->fFailsafe)
568 {
569 fFileFlags |= (RTFILE_O_ASYNC_IO | RTFILE_O_WRITE_THROUGH);
570
571 /*
572 * We only disable the cache if the size of the file is a multiple of 512.
573 * Certain hosts like Windows, Linux and Solaris require that transfer sizes
574 * are aligned to the volume sector size.
575 * If not we just make sure that the data is written to disk with RTFILE_O_WRITE_THROUGH
576 * which will trash the host cache but ensures that the host cache will not
577 * contain dirty buffers.
578 */
579 RTFILE File = NIL_RTFILE;
580
581 rc = RTFileOpen(&File, pszUri, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
582 if (RT_SUCCESS(rc))
583 {
584 uint64_t cbSize;
585
586 rc = RTFileGetSize(File, &cbSize);
587 if (RT_SUCCESS(rc) && ((cbSize % 512) == 0))
588 {
589 fFileFlags &= ~RTFILE_O_WRITE_THROUGH;
590 fFileFlags |= RTFILE_O_NO_CACHE;
591 }
592
593 pEpFile->cbFile = cbSize;
594
595 RTFileClose(File);
596 }
597 }
598
599 /* Open with final flags. */
600 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
601 if ((rc == VERR_INVALID_FUNCTION) || (rc == VERR_INVALID_PARAMETER))
602 {
603 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed with %Rrc\n",
604 pszUri, fFileFlags, rc));
605 /*
606 * Solaris doesn't support directio on ZFS so far. :-\
607 * Trying to enable it returns VERR_INVALID_FUNCTION
608 * (ENOTTY). Remove it and hope for the best.
609 * ZFS supports write throttling in case applications
610 * write more data than can be synced to the disk
611 * without blocking the whole application.
612 *
613 * On Linux we have the same problem with cifs.
614 * Have to disable async I/O here too because it requires O_DIRECT.
615 */
616 fFileFlags &= ~RTFILE_O_NO_CACHE;
617
618#ifdef RT_OS_LINUX
619 fFileFlags &= ~RTFILE_O_ASYNC_IO;
620 fUseFailsafeManager = true;
621#endif
622
623 /* Open again. */
624 rc = RTFileOpen(&pEpFile->File, pszUri, fFileFlags);
625
626 if (RT_FAILURE(rc))
627 {
628 LogRel(("pdmacFileEpInitialize: RTFileOpen %s / %08x failed AGAIN(!) with %Rrc\n",
629 pszUri, fFileFlags, rc));
630 }
631 }
632
633 if (RT_SUCCESS(rc))
634 {
635 pEpFile->fFlags = fFileFlags;
636
637 rc = RTFileGetSize(pEpFile->File, (uint64_t *)&pEpFile->cbFile);
638 if (RT_SUCCESS(rc))
639 {
640 /* Initialize the segment cache */
641 rc = MMR3HeapAllocZEx(pEpClassFile->Core.pVM, MM_TAG_PDM_ASYNC_COMPLETION,
642 sizeof(PDMACTASKFILE),
643 (void **)&pEpFile->pTasksFreeHead);
644 if (RT_SUCCESS(rc))
645 {
646 PPDMACEPFILEMGR pAioMgr = NULL;
647
648 pEpFile->pTasksFreeTail = pEpFile->pTasksFreeHead;
649 pEpFile->cTasksCached = 0;
650
651 if (fUseFailsafeManager)
652 {
653 /* Safe mode. Every file has its own async I/O manager. */
654 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, true);
655 AssertRC(rc);
656 }
657 else
658 {
659 if (fFlags & PDMACEP_FILE_FLAGS_CACHING)
660 {
661 pEpFile->fCaching = true;
662 rc = pdmacFileEpCacheInit(pEpFile, pEpClassFile);
663 if (RT_FAILURE(rc))
664 {
665 LogRel(("AIOMgr: Endpoint for \"%s\" was opened with caching but initializing cache failed. Disabled caching\n", pszUri));
666 pEpFile->fCaching = false;
667 }
668 }
669
670 pAioMgr = pEpClassFile->pAioMgrHead;
671
672 /* Check for an idling not failsafe one or create new if not found */
673 while (pAioMgr && pAioMgr->fFailsafe)
674 pAioMgr = pAioMgr->pNext;
675
676 if (!pAioMgr)
677 {
678 rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgr, false);
679 AssertRC(rc);
680 }
681 }
682
683 pEpFile->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
684
685 /* Assign the endpoint to the thread. */
686 rc = pdmacFileAioMgrAddEndpoint(pAioMgr, pEpFile);
687 if (RT_FAILURE(rc))
688 MMR3HeapFree(pEpFile->pTasksFreeHead);
689 }
690 }
691
692 if (RT_FAILURE(rc))
693 RTFileClose(pEpFile->File);
694 }
695
696#ifdef VBOX_WITH_STATISTICS
697 if (RT_SUCCESS(rc))
698 {
699 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatRead,
700 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
701 STAMUNIT_TICKS_PER_CALL, "Time taken to read from the endpoint",
702 "/PDM/AsyncCompletion/File/%s/Read", RTPathFilename(pEpFile->Core.pszUri));
703
704 STAMR3RegisterF(pEpClassFile->Core.pVM, &pEpFile->StatWrite,
705 STAMTYPE_PROFILE_ADV, STAMVISIBILITY_ALWAYS,
706 STAMUNIT_TICKS_PER_CALL, "Time taken to write to the endpoint",
707 "/PDM/AsyncCompletion/File/%s/Write", RTPathFilename(pEpFile->Core.pszUri));
708 }
709#endif
710
711 return rc;
712}
713
714static int pdmacFileEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
715{
716 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
717 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->pEpClass;
718
719 /* Make sure that all tasks finished for this endpoint. */
720 int rc = pdmacFileAioMgrCloseEndpoint(pEpFile->pAioMgr, pEpFile);
721 AssertRC(rc);
722
723 /*
724 * If the async I/O manager is in failsafe mode this is the only endpoint
725 * he processes and thus can be destroyed now.
726 */
727 if (pEpFile->pAioMgr->fFailsafe)
728 pdmacFileAioMgrDestroy(pEpClassFile, pEpFile->pAioMgr);
729
730 /* Free cached tasks. */
731 PPDMACTASKFILE pTask = pEpFile->pTasksFreeHead;
732
733 while (pTask)
734 {
735 PPDMACTASKFILE pTaskFree = pTask;
736 pTask = pTask->pNext;
737 MMR3HeapFree(pTaskFree);
738 }
739
740 /* Free the cached data. */
741 if (pEpFile->fCaching)
742 pdmacFileEpCacheDestroy(pEpFile);
743
744 RTFileClose(pEpFile->File);
745
746#ifdef VBOX_WITH_STATISTICS
747 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatRead);
748 STAMR3Deregister(pEpClassFile->Core.pVM, &pEpFile->StatWrite);
749#endif
750
751 return VINF_SUCCESS;
752}
753
754static int pdmacFileEpRead(PPDMASYNCCOMPLETIONTASK pTask,
755 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
756 PCPDMDATASEG paSegments, size_t cSegments,
757 size_t cbRead)
758{
759 int rc = VINF_SUCCESS;
760 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
761
762 STAM_PROFILE_ADV_START(&pEpFile->StatRead, Read);
763
764 if (pEpFile->fCaching)
765 rc = pdmacFileEpCacheRead(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
766 off, paSegments, cSegments, cbRead);
767 else
768 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbRead,
769 PDMACTASKFILETRANSFER_READ);
770
771 STAM_PROFILE_ADV_STOP(&pEpFile->StatRead, Read);
772
773 return rc;
774}
775
776static int pdmacFileEpWrite(PPDMASYNCCOMPLETIONTASK pTask,
777 PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
778 PCPDMDATASEG paSegments, size_t cSegments,
779 size_t cbWrite)
780{
781 int rc = VINF_SUCCESS;
782 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
783
784 if (RT_UNLIKELY(pEpFile->fReadonly))
785 return VERR_NOT_SUPPORTED;
786
787 STAM_PROFILE_ADV_START(&pEpFile->StatWrite, Write);
788
789 if (pEpFile->fCaching)
790 rc = pdmacFileEpCacheWrite(pEpFile, (PPDMASYNCCOMPLETIONTASKFILE)pTask,
791 off, paSegments, cSegments, cbWrite);
792 else
793 rc = pdmacFileEpTaskInitiate(pTask, pEndpoint, off, paSegments, cSegments, cbWrite,
794 PDMACTASKFILETRANSFER_WRITE);
795
796 STAM_PROFILE_ADV_STOP(&pEpFile->StatWrite, Write);
797
798 return rc;
799}
800
801static int pdmacFileEpFlush(PPDMASYNCCOMPLETIONTASK pTask,
802 PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
803{
804 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
805 PPDMASYNCCOMPLETIONTASKFILE pTaskFile = (PPDMASYNCCOMPLETIONTASKFILE)pTask;
806
807 if (RT_UNLIKELY(pEpFile->fReadonly))
808 return VERR_NOT_SUPPORTED;
809
810 pTaskFile->cbTransferLeft = 0;
811
812 PPDMACTASKFILE pIoTask = pdmacFileTaskAlloc(pEpFile);
813 AssertPtr(pIoTask);
814
815 pIoTask->pEndpoint = pEpFile;
816 pIoTask->enmTransferType = PDMACTASKFILETRANSFER_FLUSH;
817 pIoTask->pvUser = pTaskFile;
818 pIoTask->pfnCompleted = pdmacFileEpTaskCompleted;
819 pdmacFileEpAddTask(pEpFile, pIoTask);
820
821 return VINF_SUCCESS;
822}
823
824static int pdmacFileEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t *pcbSize)
825{
826 PPDMASYNCCOMPLETIONENDPOINTFILE pEpFile = (PPDMASYNCCOMPLETIONENDPOINTFILE)pEndpoint;
827
828 *pcbSize = ASMAtomicReadU64(&pEpFile->cbFile);
829
830 return VINF_SUCCESS;
831}
832
833const PDMASYNCCOMPLETIONEPCLASSOPS g_PDMAsyncCompletionEndpointClassFile =
834{
835 /* u32Version */
836 PDMAC_EPCLASS_OPS_VERSION,
837 /* pcszName */
838 "File",
839 /* enmClassType */
840 PDMASYNCCOMPLETIONEPCLASSTYPE_FILE,
841 /* cbEndpointClassGlobal */
842 sizeof(PDMASYNCCOMPLETIONEPCLASSFILE),
843 /* cbEndpoint */
844 sizeof(PDMASYNCCOMPLETIONENDPOINTFILE),
845 /* cbTask */
846 sizeof(PDMASYNCCOMPLETIONTASKFILE),
847 /* pfnInitialize */
848 pdmacFileInitialize,
849 /* pfnTerminate */
850 pdmacFileTerminate,
851 /* pfnEpInitialize. */
852 pdmacFileEpInitialize,
853 /* pfnEpClose */
854 pdmacFileEpClose,
855 /* pfnEpRead */
856 pdmacFileEpRead,
857 /* pfnEpWrite */
858 pdmacFileEpWrite,
859 /* pfnEpFlush */
860 pdmacFileEpFlush,
861 /* pfnEpGetSize */
862 pdmacFileEpGetSize,
863 /* u32VersionEnd */
864 PDMAC_EPCLASS_OPS_VERSION
865};
866
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