VirtualBox

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

Last change on this file since 42062 was 41862, checked in by vboxsync, 13 years ago

PDMAsyncCompletionFileNormal.cpp: Removed RT_STRICT as it breaks the strict build (added 'temporarily' about a year back, r71670).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 64.4 KB
Line 
1/* $Id: PDMAsyncCompletionFileNormal.cpp 41862 2012-06-21 15:08:02Z vboxsync $ */
2/** @file
3 * PDM Async I/O - Async File I/O manager.
4 */
5
6/*
7 * Copyright (C) 2006-2011 Oracle Corporation
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
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
22#include <iprt/types.h>
23#include <iprt/asm.h>
24#include <iprt/file.h>
25#include <iprt/mem.h>
26#include <iprt/string.h>
27#include <iprt/assert.h>
28#include <VBox/log.h>
29
30#include "PDMAsyncCompletionFileInternal.h"
31
32/** The update period for the I/O load statistics in ms. */
33#define PDMACEPFILEMGR_LOAD_UPDATE_PERIOD 1000
34/** Maximum number of requests a manager will handle. */
35#define PDMACEPFILEMGR_REQS_STEP 512
36
37
38/*******************************************************************************
39* Internal functions *
40*******************************************************************************/
41static int pdmacFileAioMgrNormalProcessTaskList(PPDMACTASKFILE pTaskHead,
42 PPDMACEPFILEMGR pAioMgr,
43 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint);
44
45static PPDMACTASKFILE pdmacFileAioMgrNormalRangeLockFree(PPDMACEPFILEMGR pAioMgr,
46 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
47 PPDMACFILERANGELOCK pRangeLock);
48
49static void pdmacFileAioMgrNormalReqCompleteRc(PPDMACEPFILEMGR pAioMgr, RTFILEAIOREQ hReq,
50 int rc, size_t cbTransfered);
51
52
53int pdmacFileAioMgrNormalInit(PPDMACEPFILEMGR pAioMgr)
54{
55 pAioMgr->cRequestsActiveMax = PDMACEPFILEMGR_REQS_STEP;
56
57 int rc = RTFileAioCtxCreate(&pAioMgr->hAioCtx, RTFILEAIO_UNLIMITED_REQS);
58 if (rc == VERR_OUT_OF_RANGE)
59 rc = RTFileAioCtxCreate(&pAioMgr->hAioCtx, pAioMgr->cRequestsActiveMax);
60
61 if (RT_SUCCESS(rc))
62 {
63 /* Initialize request handle array. */
64 pAioMgr->iFreeEntry = 0;
65 pAioMgr->cReqEntries = pAioMgr->cRequestsActiveMax;
66 pAioMgr->pahReqsFree = (RTFILEAIOREQ *)RTMemAllocZ(pAioMgr->cReqEntries * sizeof(RTFILEAIOREQ));
67
68 if (pAioMgr->pahReqsFree)
69 {
70 /* Create the range lock memcache. */
71 rc = RTMemCacheCreate(&pAioMgr->hMemCacheRangeLocks, sizeof(PDMACFILERANGELOCK),
72 0, UINT32_MAX, NULL, NULL, NULL, 0);
73 if (RT_SUCCESS(rc))
74 return VINF_SUCCESS;
75
76 RTMemFree(pAioMgr->pahReqsFree);
77 }
78 else
79 {
80 RTFileAioCtxDestroy(pAioMgr->hAioCtx);
81 rc = VERR_NO_MEMORY;
82 }
83 }
84
85 return rc;
86}
87
88void pdmacFileAioMgrNormalDestroy(PPDMACEPFILEMGR pAioMgr)
89{
90 RTFileAioCtxDestroy(pAioMgr->hAioCtx);
91
92 while (pAioMgr->iFreeEntry > 0)
93 {
94 pAioMgr->iFreeEntry--;
95 Assert(pAioMgr->pahReqsFree[pAioMgr->iFreeEntry] != NIL_RTFILEAIOREQ);
96 RTFileAioReqDestroy(pAioMgr->pahReqsFree[pAioMgr->iFreeEntry]);
97 }
98
99 RTMemFree(pAioMgr->pahReqsFree);
100 RTMemCacheDestroy(pAioMgr->hMemCacheRangeLocks);
101}
102
103#if 0 /* currently unused */
104/**
105 * Sorts the endpoint list with insertion sort.
106 */
107static void pdmacFileAioMgrNormalEndpointsSortByLoad(PPDMACEPFILEMGR pAioMgr)
108{
109 PPDMASYNCCOMPLETIONENDPOINTFILE pEpPrev, pEpCurr, pEpNextToSort;
110
111 pEpPrev = pAioMgr->pEndpointsHead;
112 pEpCurr = pEpPrev->AioMgr.pEndpointNext;
113
114 while (pEpCurr)
115 {
116 /* Remember the next element to sort because the list might change. */
117 pEpNextToSort = pEpCurr->AioMgr.pEndpointNext;
118
119 /* Unlink the current element from the list. */
120 PPDMASYNCCOMPLETIONENDPOINTFILE pPrev = pEpCurr->AioMgr.pEndpointPrev;
121 PPDMASYNCCOMPLETIONENDPOINTFILE pNext = pEpCurr->AioMgr.pEndpointNext;
122
123 if (pPrev)
124 pPrev->AioMgr.pEndpointNext = pNext;
125 else
126 pAioMgr->pEndpointsHead = pNext;
127
128 if (pNext)
129 pNext->AioMgr.pEndpointPrev = pPrev;
130
131 /* Go back until we reached the place to insert the current endpoint into. */
132 while (pEpPrev && (pEpPrev->AioMgr.cReqsPerSec < pEpCurr->AioMgr.cReqsPerSec))
133 pEpPrev = pEpPrev->AioMgr.pEndpointPrev;
134
135 /* Link the endpoint into the list. */
136 if (pEpPrev)
137 pNext = pEpPrev->AioMgr.pEndpointNext;
138 else
139 pNext = pAioMgr->pEndpointsHead;
140
141 pEpCurr->AioMgr.pEndpointNext = pNext;
142 pEpCurr->AioMgr.pEndpointPrev = pEpPrev;
143
144 if (pNext)
145 pNext->AioMgr.pEndpointPrev = pEpCurr;
146
147 if (pEpPrev)
148 pEpPrev->AioMgr.pEndpointNext = pEpCurr;
149 else
150 pAioMgr->pEndpointsHead = pEpCurr;
151
152 pEpCurr = pEpNextToSort;
153 }
154
155#ifdef DEBUG
156 /* Validate sorting algorithm */
157 unsigned cEndpoints = 0;
158 pEpCurr = pAioMgr->pEndpointsHead;
159
160 AssertMsg(pEpCurr, ("No endpoint in the list?\n"));
161 AssertMsg(!pEpCurr->AioMgr.pEndpointPrev, ("First element in the list points to previous element\n"));
162
163 while (pEpCurr)
164 {
165 cEndpoints++;
166
167 PPDMASYNCCOMPLETIONENDPOINTFILE pNext = pEpCurr->AioMgr.pEndpointNext;
168 PPDMASYNCCOMPLETIONENDPOINTFILE pPrev = pEpCurr->AioMgr.pEndpointPrev;
169
170 Assert(!pNext || pNext->AioMgr.cReqsPerSec <= pEpCurr->AioMgr.cReqsPerSec);
171 Assert(!pPrev || pPrev->AioMgr.cReqsPerSec >= pEpCurr->AioMgr.cReqsPerSec);
172
173 pEpCurr = pNext;
174 }
175
176 AssertMsg(cEndpoints == pAioMgr->cEndpoints, ("Endpoints lost during sort!\n"));
177
178#endif
179}
180#endif /* currently unused */
181
182/**
183 * Removes an endpoint from the currently assigned manager.
184 *
185 * @returns TRUE if there are still requests pending on the current manager for this endpoint.
186 * FALSE otherwise.
187 * @param pEndpointRemove The endpoint to remove.
188 */
189static bool pdmacFileAioMgrNormalRemoveEndpoint(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointRemove)
190{
191 PPDMASYNCCOMPLETIONENDPOINTFILE pPrev = pEndpointRemove->AioMgr.pEndpointPrev;
192 PPDMASYNCCOMPLETIONENDPOINTFILE pNext = pEndpointRemove->AioMgr.pEndpointNext;
193 PPDMACEPFILEMGR pAioMgr = pEndpointRemove->pAioMgr;
194
195 pAioMgr->cEndpoints--;
196
197 if (pPrev)
198 pPrev->AioMgr.pEndpointNext = pNext;
199 else
200 pAioMgr->pEndpointsHead = pNext;
201
202 if (pNext)
203 pNext->AioMgr.pEndpointPrev = pPrev;
204
205 /* Make sure that there is no request pending on this manager for the endpoint. */
206 if (!pEndpointRemove->AioMgr.cRequestsActive)
207 {
208 Assert(!pEndpointRemove->pFlushReq);
209
210 /* Reopen the file so that the new endpoint can re-associate with the file */
211 RTFileClose(pEndpointRemove->hFile);
212 int rc = RTFileOpen(&pEndpointRemove->hFile, pEndpointRemove->Core.pszUri, pEndpointRemove->fFlags);
213 AssertRC(rc);
214 return false;
215 }
216
217 return true;
218}
219
220#if 0 /* currently unused */
221
222static bool pdmacFileAioMgrNormalIsBalancePossible(PPDMACEPFILEMGR pAioMgr)
223{
224 /* Balancing doesn't make sense with only one endpoint. */
225 if (pAioMgr->cEndpoints == 1)
226 return false;
227
228 /* Doesn't make sens to move endpoints if only one produces the whole load */
229 unsigned cEndpointsWithLoad = 0;
230
231 PPDMASYNCCOMPLETIONENDPOINTFILE pCurr = pAioMgr->pEndpointsHead;
232
233 while (pCurr)
234 {
235 if (pCurr->AioMgr.cReqsPerSec)
236 cEndpointsWithLoad++;
237
238 pCurr = pCurr->AioMgr.pEndpointNext;
239 }
240
241 return (cEndpointsWithLoad > 1);
242}
243
244/**
245 * Creates a new I/O manager and spreads the I/O load of the endpoints
246 * between the given I/O manager and the new one.
247 *
248 * @returns nothing.
249 * @param pAioMgr The I/O manager with high I/O load.
250 */
251static void pdmacFileAioMgrNormalBalanceLoad(PPDMACEPFILEMGR pAioMgr)
252{
253 /*
254 * Check if balancing would improve the situation.
255 */
256 if (pdmacFileAioMgrNormalIsBalancePossible(pAioMgr))
257 {
258 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pAioMgr->pEndpointsHead->Core.pEpClass;
259 PPDMACEPFILEMGR pAioMgrNew = NULL;
260
261 int rc = pdmacFileAioMgrCreate(pEpClassFile, &pAioMgrNew, PDMACEPFILEMGRTYPE_ASYNC);
262 if (RT_SUCCESS(rc))
263 {
264 /* We will sort the list by request count per second. */
265 pdmacFileAioMgrNormalEndpointsSortByLoad(pAioMgr);
266
267 /* Now move some endpoints to the new manager. */
268 unsigned cReqsHere = pAioMgr->pEndpointsHead->AioMgr.cReqsPerSec;
269 unsigned cReqsOther = 0;
270 PPDMASYNCCOMPLETIONENDPOINTFILE pCurr = pAioMgr->pEndpointsHead->AioMgr.pEndpointNext;
271
272 while (pCurr)
273 {
274 if (cReqsHere <= cReqsOther)
275 {
276 /*
277 * The other manager has more requests to handle now.
278 * We will keep the current endpoint.
279 */
280 Log(("Keeping endpoint %#p{%s} with %u reqs/s\n", pCurr->Core.pszUri, pCurr->AioMgr.cReqsPerSec));
281 cReqsHere += pCurr->AioMgr.cReqsPerSec;
282 pCurr = pCurr->AioMgr.pEndpointNext;
283 }
284 else
285 {
286 /* Move to other endpoint. */
287 Log(("Moving endpoint %#p{%s} with %u reqs/s to other manager\n", pCurr, pCurr->Core.pszUri, pCurr->AioMgr.cReqsPerSec));
288 cReqsOther += pCurr->AioMgr.cReqsPerSec;
289
290 PPDMASYNCCOMPLETIONENDPOINTFILE pMove = pCurr;
291
292 pCurr = pCurr->AioMgr.pEndpointNext;
293
294 bool fReqsPending = pdmacFileAioMgrNormalRemoveEndpoint(pMove);
295
296 if (fReqsPending)
297 {
298 pMove->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_REMOVING;
299 pMove->AioMgr.fMoving = true;
300 pMove->AioMgr.pAioMgrDst = pAioMgrNew;
301 }
302 else
303 {
304 pMove->AioMgr.fMoving = false;
305 pMove->AioMgr.pAioMgrDst = NULL;
306 pdmacFileAioMgrAddEndpoint(pAioMgrNew, pMove);
307 }
308 }
309 }
310 }
311 else
312 {
313 /* Don't process further but leave a log entry about reduced performance. */
314 LogRel(("AIOMgr: Could not create new I/O manager (rc=%Rrc). Expect reduced performance\n", rc));
315 }
316 }
317 else
318 Log(("AIOMgr: Load balancing would not improve anything\n"));
319}
320
321#endif /* unused */
322
323/**
324 * Increase the maximum number of active requests for the given I/O manager.
325 *
326 * @returns VBox status code.
327 * @param pAioMgr The I/O manager to grow.
328 */
329static int pdmacFileAioMgrNormalGrow(PPDMACEPFILEMGR pAioMgr)
330{
331 LogFlowFunc(("pAioMgr=%#p\n", pAioMgr));
332
333 AssertMsg( pAioMgr->enmState == PDMACEPFILEMGRSTATE_GROWING
334 && !pAioMgr->cRequestsActive,
335 ("Invalid state of the I/O manager\n"));
336
337#ifdef RT_OS_WINDOWS
338 /*
339 * Reopen the files of all assigned endpoints first so we can assign them to the new
340 * I/O context.
341 */
342 PPDMASYNCCOMPLETIONENDPOINTFILE pCurr = pAioMgr->pEndpointsHead;
343
344 while (pCurr)
345 {
346 RTFileClose(pCurr->hFile);
347 int rc2 = RTFileOpen(&pCurr->hFile, pCurr->Core.pszUri, pCurr->fFlags); AssertRC(rc2);
348
349 pCurr = pCurr->AioMgr.pEndpointNext;
350 }
351#endif
352
353 /* Create the new bigger context. */
354 pAioMgr->cRequestsActiveMax += PDMACEPFILEMGR_REQS_STEP;
355
356 RTFILEAIOCTX hAioCtxNew = NIL_RTFILEAIOCTX;
357 int rc = RTFileAioCtxCreate(&hAioCtxNew, RTFILEAIO_UNLIMITED_REQS);
358 if (rc == VERR_OUT_OF_RANGE)
359 rc = RTFileAioCtxCreate(&hAioCtxNew, pAioMgr->cRequestsActiveMax);
360
361 if (RT_SUCCESS(rc))
362 {
363 /* Close the old context. */
364 rc = RTFileAioCtxDestroy(pAioMgr->hAioCtx);
365 AssertRC(rc); /** @todo r=bird: Ignoring error code, will propagate. */
366
367 pAioMgr->hAioCtx = hAioCtxNew;
368
369 /* Create a new I/O task handle array */
370 uint32_t cReqEntriesNew = pAioMgr->cRequestsActiveMax + 1;
371 RTFILEAIOREQ *pahReqNew = (RTFILEAIOREQ *)RTMemAllocZ(cReqEntriesNew * sizeof(RTFILEAIOREQ));
372
373 if (pahReqNew)
374 {
375 /* Copy the cached request handles. */
376 for (uint32_t iReq = 0; iReq < pAioMgr->cReqEntries; iReq++)
377 pahReqNew[iReq] = pAioMgr->pahReqsFree[iReq];
378
379 RTMemFree(pAioMgr->pahReqsFree);
380 pAioMgr->pahReqsFree = pahReqNew;
381 pAioMgr->cReqEntries = cReqEntriesNew;
382 LogFlowFunc(("I/O manager increased to handle a maximum of %u requests\n",
383 pAioMgr->cRequestsActiveMax));
384 }
385 else
386 rc = VERR_NO_MEMORY;
387 }
388
389#ifdef RT_OS_WINDOWS
390 /* Assign the file to the new context. */
391 pCurr = pAioMgr->pEndpointsHead;
392 while (pCurr)
393 {
394 rc = RTFileAioCtxAssociateWithFile(pAioMgr->hAioCtx, pCurr->hFile);
395 AssertRC(rc); /** @todo r=bird: Ignoring error code, will propagate. */
396
397 pCurr = pCurr->AioMgr.pEndpointNext;
398 }
399#endif
400
401 if (RT_FAILURE(rc))
402 {
403 LogFlow(("Increasing size of the I/O manager failed with rc=%Rrc\n", rc));
404 pAioMgr->cRequestsActiveMax -= PDMACEPFILEMGR_REQS_STEP;
405 }
406
407 pAioMgr->enmState = PDMACEPFILEMGRSTATE_RUNNING;
408 LogFlowFunc(("returns rc=%Rrc\n", rc));
409
410 return rc;
411}
412
413/**
414 * Checks if a given status code is fatal.
415 * Non fatal errors can be fixed by migrating the endpoint to a
416 * failsafe manager.
417 *
418 * @returns true If the error is fatal and migrating to a failsafe manager doesn't help
419 * false If the error can be fixed by a migration. (image on NFS disk for example)
420 * @param rcReq The status code to check.
421 */
422DECLINLINE(bool) pdmacFileAioMgrNormalRcIsFatal(int rcReq)
423{
424 return rcReq == VERR_DEV_IO_ERROR
425 || rcReq == VERR_FILE_IO_ERROR
426 || rcReq == VERR_DISK_IO_ERROR
427 || rcReq == VERR_DISK_FULL
428 || rcReq == VERR_FILE_TOO_BIG;
429}
430
431/**
432 * Error handler which will create the failsafe managers and destroy the failed I/O manager.
433 *
434 * @returns VBox status code
435 * @param pAioMgr The I/O manager the error occurred on.
436 * @param rc The error code.
437 */
438static int pdmacFileAioMgrNormalErrorHandler(PPDMACEPFILEMGR pAioMgr, int rc, RT_SRC_POS_DECL)
439{
440 LogRel(("AIOMgr: I/O manager %#p encountered a critical error (rc=%Rrc) during operation. Falling back to failsafe mode. Expect reduced performance\n",
441 pAioMgr, rc));
442 LogRel(("AIOMgr: Error happened in %s:(%u){%s}\n", RT_SRC_POS_ARGS));
443 LogRel(("AIOMgr: Please contact the product vendor\n"));
444
445 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pAioMgr->pEndpointsHead->Core.pEpClass;
446
447 pAioMgr->enmState = PDMACEPFILEMGRSTATE_FAULT;
448 ASMAtomicWriteU32((volatile uint32_t *)&pEpClassFile->enmMgrTypeOverride, PDMACEPFILEMGRTYPE_SIMPLE);
449
450 AssertMsgFailed(("Implement\n"));
451 return VINF_SUCCESS;
452}
453
454/**
455 * Put a list of tasks in the pending request list of an endpoint.
456 */
457DECLINLINE(void) pdmacFileAioMgrEpAddTaskList(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint, PPDMACTASKFILE pTaskHead)
458{
459 /* Add the rest of the tasks to the pending list */
460 if (!pEndpoint->AioMgr.pReqsPendingHead)
461 {
462 Assert(!pEndpoint->AioMgr.pReqsPendingTail);
463 pEndpoint->AioMgr.pReqsPendingHead = pTaskHead;
464 }
465 else
466 {
467 Assert(pEndpoint->AioMgr.pReqsPendingTail);
468 pEndpoint->AioMgr.pReqsPendingTail->pNext = pTaskHead;
469 }
470
471 /* Update the tail. */
472 while (pTaskHead->pNext)
473 pTaskHead = pTaskHead->pNext;
474
475 pEndpoint->AioMgr.pReqsPendingTail = pTaskHead;
476 pTaskHead->pNext = NULL;
477}
478
479/**
480 * Put one task in the pending request list of an endpoint.
481 */
482DECLINLINE(void) pdmacFileAioMgrEpAddTask(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint, PPDMACTASKFILE pTask)
483{
484 /* Add the rest of the tasks to the pending list */
485 if (!pEndpoint->AioMgr.pReqsPendingHead)
486 {
487 Assert(!pEndpoint->AioMgr.pReqsPendingTail);
488 pEndpoint->AioMgr.pReqsPendingHead = pTask;
489 }
490 else
491 {
492 Assert(pEndpoint->AioMgr.pReqsPendingTail);
493 pEndpoint->AioMgr.pReqsPendingTail->pNext = pTask;
494 }
495
496 pEndpoint->AioMgr.pReqsPendingTail = pTask;
497 pTask->pNext = NULL;
498}
499
500/**
501 * Allocates a async I/O request.
502 *
503 * @returns Handle to the request.
504 * @param pAioMgr The I/O manager.
505 */
506static RTFILEAIOREQ pdmacFileAioMgrNormalRequestAlloc(PPDMACEPFILEMGR pAioMgr)
507{
508 /* Get a request handle. */
509 RTFILEAIOREQ hReq;
510 if (pAioMgr->iFreeEntry > 0)
511 {
512 pAioMgr->iFreeEntry--;
513 hReq = pAioMgr->pahReqsFree[pAioMgr->iFreeEntry];
514 pAioMgr->pahReqsFree[pAioMgr->iFreeEntry] = NIL_RTFILEAIOREQ;
515 Assert(hReq != NIL_RTFILEAIOREQ);
516 }
517 else
518 {
519 int rc = RTFileAioReqCreate(&hReq);
520 AssertRCReturn(rc, NIL_RTFILEAIOREQ);
521 }
522
523 return hReq;
524}
525
526/**
527 * Frees a async I/O request handle.
528 *
529 * @returns nothing.
530 * @param pAioMgr The I/O manager.
531 * @param hReq The I/O request handle to free.
532 */
533static void pdmacFileAioMgrNormalRequestFree(PPDMACEPFILEMGR pAioMgr, RTFILEAIOREQ hReq)
534{
535 Assert(pAioMgr->iFreeEntry < pAioMgr->cReqEntries);
536 Assert(pAioMgr->pahReqsFree[pAioMgr->iFreeEntry] == NIL_RTFILEAIOREQ);
537
538 pAioMgr->pahReqsFree[pAioMgr->iFreeEntry] = hReq;
539 pAioMgr->iFreeEntry++;
540}
541
542/**
543 * Wrapper around RTFIleAioCtxSubmit() which is also doing error handling.
544 */
545static int pdmacFileAioMgrNormalReqsEnqueue(PPDMACEPFILEMGR pAioMgr,
546 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
547 PRTFILEAIOREQ pahReqs, unsigned cReqs)
548{
549 pAioMgr->cRequestsActive += cReqs;
550 pEndpoint->AioMgr.cRequestsActive += cReqs;
551
552 LogFlow(("Enqueuing %d requests. I/O manager has a total of %d active requests now\n", cReqs, pAioMgr->cRequestsActive));
553 LogFlow(("Endpoint has a total of %d active requests now\n", pEndpoint->AioMgr.cRequestsActive));
554
555 int rc = RTFileAioCtxSubmit(pAioMgr->hAioCtx, pahReqs, cReqs);
556 if (RT_FAILURE(rc))
557 {
558 if (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES)
559 {
560 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClass = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
561
562 /* Append any not submitted task to the waiting list. */
563 for (size_t i = 0; i < cReqs; i++)
564 {
565 int rcReq = RTFileAioReqGetRC(pahReqs[i], NULL);
566
567 if (rcReq != VERR_FILE_AIO_IN_PROGRESS)
568 {
569 PPDMACTASKFILE pTask = (PPDMACTASKFILE)RTFileAioReqGetUser(pahReqs[i]);
570
571 Assert(pTask->hReq == pahReqs[i]);
572 pdmacFileAioMgrEpAddTask(pEndpoint, pTask);
573 pAioMgr->cRequestsActive--;
574 pEndpoint->AioMgr.cRequestsActive--;
575
576 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_FLUSH)
577 {
578 /* Clear the pending flush */
579 Assert(pEndpoint->pFlushReq == pTask);
580 pEndpoint->pFlushReq = NULL;
581 }
582 }
583 }
584
585 pAioMgr->cRequestsActiveMax = pAioMgr->cRequestsActive;
586
587 /* Print an entry in the release log */
588 if (RT_UNLIKELY(!pEpClass->fOutOfResourcesWarningPrinted))
589 {
590 pEpClass->fOutOfResourcesWarningPrinted = true;
591 LogRel(("AIOMgr: Host limits number of active IO requests to %u. Expect a performance impact.\n",
592 pAioMgr->cRequestsActive));
593 }
594
595 LogFlow(("Removed requests. I/O manager has a total of %u active requests now\n", pAioMgr->cRequestsActive));
596 LogFlow(("Endpoint has a total of %u active requests now\n", pEndpoint->AioMgr.cRequestsActive));
597 rc = VINF_SUCCESS;
598 }
599 else /* Another kind of error happened (full disk, ...) */
600 {
601 /* An error happened. Find out which one caused the error and resubmit all other tasks. */
602 for (size_t i = 0; i < cReqs; i++)
603 {
604 int rcReq = RTFileAioReqGetRC(pahReqs[i], NULL);
605
606 if (rcReq == VERR_FILE_AIO_NOT_SUBMITTED)
607 {
608 /* We call ourself again to do any error handling which might come up now. */
609 rc = pdmacFileAioMgrNormalReqsEnqueue(pAioMgr, pEndpoint, &pahReqs[i], 1);
610 AssertRC(rc);
611 }
612 else if (rcReq != VERR_FILE_AIO_IN_PROGRESS)
613 pdmacFileAioMgrNormalReqCompleteRc(pAioMgr, pahReqs[i], rcReq, 0);
614 }
615
616
617 if ( pEndpoint->pFlushReq
618 && !pAioMgr->cRequestsActive
619 && !pEndpoint->fAsyncFlushSupported)
620 {
621 /*
622 * Complete a pending flush if we don't have requests enqueued and the host doesn't support
623 * the async flush API.
624 * Happens only if this we just noticed that this is not supported
625 * and the only active request was a flush.
626 */
627 PPDMACTASKFILE pFlush = pEndpoint->pFlushReq;
628 pEndpoint->pFlushReq = NULL;
629 pFlush->pfnCompleted(pFlush, pFlush->pvUser, VINF_SUCCESS);
630 pdmacFileTaskFree(pEndpoint, pFlush);
631 }
632 }
633 }
634
635 return VINF_SUCCESS;
636}
637
638static bool pdmacFileAioMgrNormalIsRangeLocked(PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
639 RTFOFF offStart, size_t cbRange,
640 PPDMACTASKFILE pTask)
641{
642 AssertMsg( pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE
643 || pTask->enmTransferType == PDMACTASKFILETRANSFER_READ,
644 ("Invalid task type %d\n", pTask->enmTransferType));
645
646 PPDMACFILERANGELOCK pRangeLock;
647 pRangeLock = (PPDMACFILERANGELOCK)RTAvlrFileOffsetRangeGet(pEndpoint->AioMgr.pTreeRangesLocked, offStart);
648 if (!pRangeLock)
649 {
650 pRangeLock = (PPDMACFILERANGELOCK)RTAvlrFileOffsetGetBestFit(pEndpoint->AioMgr.pTreeRangesLocked, offStart, true);
651 /* Check if we intersect with the range. */
652 if ( !pRangeLock
653 || !( (pRangeLock->Core.Key) <= (offStart + (RTFOFF)cbRange - 1)
654 && (pRangeLock->Core.KeyLast) >= offStart))
655 {
656 pRangeLock = NULL; /* False alarm */
657 }
658 }
659
660 /* Check whether we have one of the situations explained below */
661 if ( pRangeLock
662#if 0 /** @todo later. For now we will just block all requests if they interfere */
663 && ( (pRangeLock->fReadLock && pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE)
664 || (!pRangeLock->fReadLock)
665#endif
666 )
667 {
668 /* Add to the list. */
669 pTask->pNext = NULL;
670
671 if (!pRangeLock->pWaitingTasksHead)
672 {
673 Assert(!pRangeLock->pWaitingTasksTail);
674 pRangeLock->pWaitingTasksHead = pTask;
675 pRangeLock->pWaitingTasksTail = pTask;
676 }
677 else
678 {
679 AssertPtr(pRangeLock->pWaitingTasksTail);
680 pRangeLock->pWaitingTasksTail->pNext = pTask;
681 pRangeLock->pWaitingTasksTail = pTask;
682 }
683 return true;
684 }
685
686 return false;
687}
688
689static int pdmacFileAioMgrNormalRangeLock(PPDMACEPFILEMGR pAioMgr,
690 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
691 RTFOFF offStart, size_t cbRange,
692 PPDMACTASKFILE pTask)
693{
694 AssertMsg(!pdmacFileAioMgrNormalIsRangeLocked(pEndpoint, offStart, cbRange, pTask),
695 ("Range is already locked offStart=%RTfoff cbRange=%u\n",
696 offStart, cbRange));
697
698 PPDMACFILERANGELOCK pRangeLock = (PPDMACFILERANGELOCK)RTMemCacheAlloc(pAioMgr->hMemCacheRangeLocks);
699 if (!pRangeLock)
700 return VERR_NO_MEMORY;
701
702 /* Init the lock. */
703 pRangeLock->Core.Key = offStart;
704 pRangeLock->Core.KeyLast = offStart + cbRange - 1;
705 pRangeLock->cRefs = 1;
706 pRangeLock->fReadLock = pTask->enmTransferType == PDMACTASKFILETRANSFER_READ;
707 pRangeLock->pWaitingTasksHead = NULL;
708 pRangeLock->pWaitingTasksTail = NULL;
709
710 bool fInserted = RTAvlrFileOffsetInsert(pEndpoint->AioMgr.pTreeRangesLocked, &pRangeLock->Core);
711 AssertMsg(fInserted, ("Range lock was not inserted!\n"));
712
713 /* Let the task point to its lock. */
714 pTask->pRangeLock = pRangeLock;
715
716 return VINF_SUCCESS;
717}
718
719static PPDMACTASKFILE pdmacFileAioMgrNormalRangeLockFree(PPDMACEPFILEMGR pAioMgr,
720 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
721 PPDMACFILERANGELOCK pRangeLock)
722{
723 PPDMACTASKFILE pTasksWaitingHead;
724
725 AssertPtr(pRangeLock);
726 Assert(pRangeLock->cRefs == 1);
727
728 RTAvlrFileOffsetRemove(pEndpoint->AioMgr.pTreeRangesLocked, pRangeLock->Core.Key);
729 pTasksWaitingHead = pRangeLock->pWaitingTasksHead;
730 pRangeLock->pWaitingTasksHead = NULL;
731 pRangeLock->pWaitingTasksTail = NULL;
732 RTMemCacheFree(pAioMgr->hMemCacheRangeLocks, pRangeLock);
733
734 return pTasksWaitingHead;
735}
736
737static int pdmacFileAioMgrNormalTaskPrepareBuffered(PPDMACEPFILEMGR pAioMgr,
738 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
739 PPDMACTASKFILE pTask, PRTFILEAIOREQ phReq)
740{
741 AssertMsg( pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE
742 || (uint64_t)(pTask->Off + pTask->DataSeg.cbSeg) <= pEndpoint->cbFile,
743 ("Read exceeds file size offStart=%RTfoff cbToTransfer=%d cbFile=%llu\n",
744 pTask->Off, pTask->DataSeg.cbSeg, pEndpoint->cbFile));
745
746 pTask->fPrefetch = false;
747 pTask->cbBounceBuffer = 0;
748
749 /*
750 * Before we start to setup the request we have to check whether there is a task
751 * already active which range intersects with ours. We have to defer execution
752 * of this task in two cases:
753 * - The pending task is a write and the current is either read or write
754 * - The pending task is a read and the current task is a write task.
755 *
756 * To check whether a range is currently "locked" we use the AVL tree where every pending task
757 * is stored by its file offset range. The current task will be added to the active task
758 * and will be executed when the active one completes. (The method below
759 * which checks whether a range is already used will add the task)
760 *
761 * This is necessary because of the requirement to align all requests to a 512 boundary
762 * which is enforced by the host OS (Linux and Windows atm). It is possible that
763 * we have to process unaligned tasks and need to align them using bounce buffers.
764 * While the data is fetched from the file another request might arrive writing to
765 * the same range. This will result in data corruption if both are executed concurrently.
766 */
767 int rc = VINF_SUCCESS;
768 bool fLocked = pdmacFileAioMgrNormalIsRangeLocked(pEndpoint, pTask->Off, pTask->DataSeg.cbSeg, pTask);
769 if (!fLocked)
770 {
771 /* Get a request handle. */
772 RTFILEAIOREQ hReq = pdmacFileAioMgrNormalRequestAlloc(pAioMgr);
773 AssertMsg(hReq != NIL_RTFILEAIOREQ, ("Out of request handles\n"));
774
775 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE)
776 {
777 /* Grow the file if needed. */
778 if (RT_UNLIKELY((uint64_t)(pTask->Off + pTask->DataSeg.cbSeg) > pEndpoint->cbFile))
779 {
780 ASMAtomicWriteU64(&pEndpoint->cbFile, pTask->Off + pTask->DataSeg.cbSeg);
781 RTFileSetSize(pEndpoint->hFile, pTask->Off + pTask->DataSeg.cbSeg);
782 }
783
784 rc = RTFileAioReqPrepareWrite(hReq, pEndpoint->hFile,
785 pTask->Off, pTask->DataSeg.pvSeg,
786 pTask->DataSeg.cbSeg, pTask);
787 }
788 else
789 rc = RTFileAioReqPrepareRead(hReq, pEndpoint->hFile,
790 pTask->Off, pTask->DataSeg.pvSeg,
791 pTask->DataSeg.cbSeg, pTask);
792 AssertRC(rc);
793
794 rc = pdmacFileAioMgrNormalRangeLock(pAioMgr, pEndpoint, pTask->Off,
795 pTask->DataSeg.cbSeg,
796 pTask);
797
798 if (RT_SUCCESS(rc))
799 {
800 pTask->hReq = hReq;
801 *phReq = hReq;
802 }
803 }
804 else
805 LogFlow(("Task %#p was deferred because the access range is locked\n", pTask));
806
807 return rc;
808}
809
810static int pdmacFileAioMgrNormalTaskPrepareNonBuffered(PPDMACEPFILEMGR pAioMgr,
811 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
812 PPDMACTASKFILE pTask, PRTFILEAIOREQ phReq)
813{
814 /*
815 * Check if the alignment requirements are met.
816 * Offset, transfer size and buffer address
817 * need to be on a 512 boundary.
818 */
819 RTFOFF offStart = pTask->Off & ~(RTFOFF)(512-1);
820 size_t cbToTransfer = RT_ALIGN_Z(pTask->DataSeg.cbSeg + (pTask->Off - offStart), 512);
821 PDMACTASKFILETRANSFER enmTransferType = pTask->enmTransferType;
822
823 AssertMsg( pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE
824 || (uint64_t)(offStart + cbToTransfer) <= pEndpoint->cbFile,
825 ("Read exceeds file size offStart=%RTfoff cbToTransfer=%d cbFile=%llu\n",
826 offStart, cbToTransfer, pEndpoint->cbFile));
827
828 pTask->fPrefetch = false;
829
830 /*
831 * Before we start to setup the request we have to check whether there is a task
832 * already active which range intersects with ours. We have to defer execution
833 * of this task in two cases:
834 * - The pending task is a write and the current is either read or write
835 * - The pending task is a read and the current task is a write task.
836 *
837 * To check whether a range is currently "locked" we use the AVL tree where every pending task
838 * is stored by its file offset range. The current task will be added to the active task
839 * and will be executed when the active one completes. (The method below
840 * which checks whether a range is already used will add the task)
841 *
842 * This is necessary because of the requirement to align all requests to a 512 boundary
843 * which is enforced by the host OS (Linux and Windows atm). It is possible that
844 * we have to process unaligned tasks and need to align them using bounce buffers.
845 * While the data is fetched from the file another request might arrive writing to
846 * the same range. This will result in data corruption if both are executed concurrently.
847 */
848 int rc = VINF_SUCCESS;
849 bool fLocked = pdmacFileAioMgrNormalIsRangeLocked(pEndpoint, offStart, cbToTransfer, pTask);
850 if (!fLocked)
851 {
852 PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
853 void *pvBuf = pTask->DataSeg.pvSeg;
854
855 /* Get a request handle. */
856 RTFILEAIOREQ hReq = pdmacFileAioMgrNormalRequestAlloc(pAioMgr);
857 AssertMsg(hReq != NIL_RTFILEAIOREQ, ("Out of request handles\n"));
858
859 if ( RT_UNLIKELY(cbToTransfer != pTask->DataSeg.cbSeg)
860 || RT_UNLIKELY(offStart != pTask->Off)
861 || ((pEpClassFile->uBitmaskAlignment & (RTR3UINTPTR)pvBuf) != (RTR3UINTPTR)pvBuf))
862 {
863 LogFlow(("Using bounce buffer for task %#p cbToTransfer=%zd cbSeg=%zd offStart=%RTfoff off=%RTfoff\n",
864 pTask, cbToTransfer, pTask->DataSeg.cbSeg, offStart, pTask->Off));
865
866 /* Create bounce buffer. */
867 pTask->cbBounceBuffer = cbToTransfer;
868
869 AssertMsg(pTask->Off >= offStart, ("Overflow in calculation Off=%llu offStart=%llu\n",
870 pTask->Off, offStart));
871 pTask->offBounceBuffer = pTask->Off - offStart;
872
873 /** @todo: I think we need something like a RTMemAllocAligned method here.
874 * Current assumption is that the maximum alignment is 4096byte
875 * (GPT disk on Windows)
876 * so we can use RTMemPageAlloc here.
877 */
878 pTask->pvBounceBuffer = RTMemPageAlloc(cbToTransfer);
879 if (RT_LIKELY(pTask->pvBounceBuffer))
880 {
881 pvBuf = pTask->pvBounceBuffer;
882
883 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE)
884 {
885 if ( RT_UNLIKELY(cbToTransfer != pTask->DataSeg.cbSeg)
886 || RT_UNLIKELY(offStart != pTask->Off))
887 {
888 /* We have to fill the buffer first before we can update the data. */
889 LogFlow(("Prefetching data for task %#p\n", pTask));
890 pTask->fPrefetch = true;
891 enmTransferType = PDMACTASKFILETRANSFER_READ;
892 }
893 else
894 memcpy(pvBuf, pTask->DataSeg.pvSeg, pTask->DataSeg.cbSeg);
895 }
896 }
897 else
898 rc = VERR_NO_MEMORY;
899 }
900 else
901 pTask->cbBounceBuffer = 0;
902
903 if (RT_SUCCESS(rc))
904 {
905 AssertMsg((pEpClassFile->uBitmaskAlignment & (RTR3UINTPTR)pvBuf) == (RTR3UINTPTR)pvBuf,
906 ("AIO: Alignment restrictions not met! pvBuf=%p uBitmaskAlignment=%p\n", pvBuf, pEpClassFile->uBitmaskAlignment));
907
908 if (enmTransferType == PDMACTASKFILETRANSFER_WRITE)
909 {
910 /* Grow the file if needed. */
911 if (RT_UNLIKELY((uint64_t)(pTask->Off + pTask->DataSeg.cbSeg) > pEndpoint->cbFile))
912 {
913 ASMAtomicWriteU64(&pEndpoint->cbFile, pTask->Off + pTask->DataSeg.cbSeg);
914 RTFileSetSize(pEndpoint->hFile, pTask->Off + pTask->DataSeg.cbSeg);
915 }
916
917 rc = RTFileAioReqPrepareWrite(hReq, pEndpoint->hFile,
918 offStart, pvBuf, cbToTransfer, pTask);
919 }
920 else
921 rc = RTFileAioReqPrepareRead(hReq, pEndpoint->hFile,
922 offStart, pvBuf, cbToTransfer, pTask);
923 AssertRC(rc);
924
925 rc = pdmacFileAioMgrNormalRangeLock(pAioMgr, pEndpoint, offStart, cbToTransfer, pTask);
926
927 if (RT_SUCCESS(rc))
928 {
929 pTask->hReq = hReq;
930 *phReq = hReq;
931 }
932 else
933 {
934 /* Cleanup */
935 if (pTask->cbBounceBuffer)
936 RTMemPageFree(pTask->pvBounceBuffer, pTask->cbBounceBuffer);
937 }
938 }
939 }
940 else
941 LogFlow(("Task %#p was deferred because the access range is locked\n", pTask));
942
943 return rc;
944}
945
946static int pdmacFileAioMgrNormalProcessTaskList(PPDMACTASKFILE pTaskHead,
947 PPDMACEPFILEMGR pAioMgr,
948 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
949{
950 RTFILEAIOREQ apReqs[20];
951 unsigned cRequests = 0;
952 int rc = VINF_SUCCESS;
953
954 AssertMsg(pEndpoint->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE,
955 ("Trying to process request lists of a non active endpoint!\n"));
956
957 /* Go through the list and queue the requests until we get a flush request */
958 while ( pTaskHead
959 && !pEndpoint->pFlushReq
960 && (pAioMgr->cRequestsActive + cRequests < pAioMgr->cRequestsActiveMax)
961 && RT_SUCCESS(rc))
962 {
963 RTMSINTERVAL msWhenNext;
964 PPDMACTASKFILE pCurr = pTaskHead;
965
966 if (!pdmacEpIsTransferAllowed(&pEndpoint->Core, (uint32_t)pCurr->DataSeg.cbSeg, &msWhenNext))
967 {
968 pAioMgr->msBwLimitExpired = RT_MIN(pAioMgr->msBwLimitExpired, msWhenNext);
969 break;
970 }
971
972 pTaskHead = pTaskHead->pNext;
973
974 pCurr->pNext = NULL;
975
976 AssertMsg(VALID_PTR(pCurr->pEndpoint) && (pCurr->pEndpoint == pEndpoint),
977 ("Endpoints do not match\n"));
978
979 switch (pCurr->enmTransferType)
980 {
981 case PDMACTASKFILETRANSFER_FLUSH:
982 {
983 /* If there is no data transfer request this flush request finished immediately. */
984 if (pEndpoint->fAsyncFlushSupported)
985 {
986 /* Issue a flush to the host. */
987 RTFILEAIOREQ hReq = pdmacFileAioMgrNormalRequestAlloc(pAioMgr);
988 AssertMsg(hReq != NIL_RTFILEAIOREQ, ("Out of request handles\n"));
989
990 LogFlow(("Flush request %#p\n", hReq));
991
992 rc = RTFileAioReqPrepareFlush(hReq, pEndpoint->hFile, pCurr);
993 if (RT_FAILURE(rc))
994 {
995 pEndpoint->fAsyncFlushSupported = false;
996 pdmacFileAioMgrNormalRequestFree(pAioMgr, hReq);
997 rc = VINF_SUCCESS; /* Fake success */
998 }
999 else
1000 {
1001 pCurr->hReq = hReq;
1002 apReqs[cRequests] = hReq;
1003 pEndpoint->AioMgr.cReqsProcessed++;
1004 cRequests++;
1005 }
1006 }
1007
1008 if ( !pEndpoint->AioMgr.cRequestsActive
1009 && !pEndpoint->fAsyncFlushSupported)
1010 {
1011 pCurr->pfnCompleted(pCurr, pCurr->pvUser, VINF_SUCCESS);
1012 pdmacFileTaskFree(pEndpoint, pCurr);
1013 }
1014 else
1015 {
1016 Assert(!pEndpoint->pFlushReq);
1017 pEndpoint->pFlushReq = pCurr;
1018 }
1019 break;
1020 }
1021 case PDMACTASKFILETRANSFER_READ:
1022 case PDMACTASKFILETRANSFER_WRITE:
1023 {
1024 RTFILEAIOREQ hReq = NIL_RTFILEAIOREQ;
1025
1026 if (pCurr->hReq == NIL_RTFILEAIOREQ)
1027 {
1028 if (pEndpoint->enmBackendType == PDMACFILEEPBACKEND_BUFFERED)
1029 rc = pdmacFileAioMgrNormalTaskPrepareBuffered(pAioMgr, pEndpoint, pCurr, &hReq);
1030 else if (pEndpoint->enmBackendType == PDMACFILEEPBACKEND_NON_BUFFERED)
1031 rc = pdmacFileAioMgrNormalTaskPrepareNonBuffered(pAioMgr, pEndpoint, pCurr, &hReq);
1032 else
1033 AssertMsgFailed(("Invalid backend type %d\n", pEndpoint->enmBackendType));
1034
1035 AssertRC(rc);
1036 }
1037 else
1038 {
1039 LogFlow(("Task %#p has I/O request %#p already\n", pCurr, pCurr->hReq));
1040 hReq = pCurr->hReq;
1041 }
1042
1043 LogFlow(("Read/Write request %#p\n", hReq));
1044
1045 if (hReq != NIL_RTFILEAIOREQ)
1046 {
1047 apReqs[cRequests] = hReq;
1048 cRequests++;
1049 }
1050 break;
1051 }
1052 default:
1053 AssertMsgFailed(("Invalid transfer type %d\n", pCurr->enmTransferType));
1054 } /* switch transfer type */
1055
1056 /* Queue the requests if the array is full. */
1057 if (cRequests == RT_ELEMENTS(apReqs))
1058 {
1059 rc = pdmacFileAioMgrNormalReqsEnqueue(pAioMgr, pEndpoint, apReqs, cRequests);
1060 cRequests = 0;
1061 AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
1062 ("Unexpected return code\n"));
1063 }
1064 }
1065
1066 if (cRequests)
1067 {
1068 rc = pdmacFileAioMgrNormalReqsEnqueue(pAioMgr, pEndpoint, apReqs, cRequests);
1069 AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
1070 ("Unexpected return code rc=%Rrc\n", rc));
1071 }
1072
1073 if (pTaskHead)
1074 {
1075 /* Add the rest of the tasks to the pending list */
1076 pdmacFileAioMgrEpAddTaskList(pEndpoint, pTaskHead);
1077
1078 if (RT_UNLIKELY( pAioMgr->cRequestsActiveMax == pAioMgr->cRequestsActive
1079 && !pEndpoint->pFlushReq))
1080 {
1081#if 0
1082 /*
1083 * The I/O manager has no room left for more requests
1084 * but there are still requests to process.
1085 * Create a new I/O manager and let it handle some endpoints.
1086 */
1087 pdmacFileAioMgrNormalBalanceLoad(pAioMgr);
1088#else
1089 /* Grow the I/O manager */
1090 pAioMgr->enmState = PDMACEPFILEMGRSTATE_GROWING;
1091#endif
1092 }
1093 }
1094
1095 /* Insufficient resources are not fatal. */
1096 if (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES)
1097 rc = VINF_SUCCESS;
1098
1099 return rc;
1100}
1101
1102/**
1103 * Adds all pending requests for the given endpoint
1104 * until a flush request is encountered or there is no
1105 * request anymore.
1106 *
1107 * @returns VBox status code.
1108 * @param pAioMgr The async I/O manager for the endpoint
1109 * @param pEndpoint The endpoint to get the requests from.
1110 */
1111static int pdmacFileAioMgrNormalQueueReqs(PPDMACEPFILEMGR pAioMgr,
1112 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
1113{
1114 int rc = VINF_SUCCESS;
1115 PPDMACTASKFILE pTasksHead = NULL;
1116
1117 AssertMsg(pEndpoint->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE,
1118 ("Trying to process request lists of a non active endpoint!\n"));
1119
1120 Assert(!pEndpoint->pFlushReq);
1121
1122 /* Check the pending list first */
1123 if (pEndpoint->AioMgr.pReqsPendingHead)
1124 {
1125 LogFlow(("Queuing pending requests first\n"));
1126
1127 pTasksHead = pEndpoint->AioMgr.pReqsPendingHead;
1128 /*
1129 * Clear the list as the processing routine will insert them into the list
1130 * again if it gets a flush request.
1131 */
1132 pEndpoint->AioMgr.pReqsPendingHead = NULL;
1133 pEndpoint->AioMgr.pReqsPendingTail = NULL;
1134 rc = pdmacFileAioMgrNormalProcessTaskList(pTasksHead, pAioMgr, pEndpoint);
1135 AssertRC(rc); /** @todo r=bird: status code potentially overwritten. */
1136 }
1137
1138 if (!pEndpoint->pFlushReq && !pEndpoint->AioMgr.pReqsPendingHead)
1139 {
1140 /* Now the request queue. */
1141 pTasksHead = pdmacFileEpGetNewTasks(pEndpoint);
1142 if (pTasksHead)
1143 {
1144 rc = pdmacFileAioMgrNormalProcessTaskList(pTasksHead, pAioMgr, pEndpoint);
1145 AssertRC(rc);
1146 }
1147 }
1148
1149 return rc;
1150}
1151
1152static int pdmacFileAioMgrNormalProcessBlockingEvent(PPDMACEPFILEMGR pAioMgr)
1153{
1154 int rc = VINF_SUCCESS;
1155 bool fNotifyWaiter = false;
1156
1157 LogFlowFunc((": Enter\n"));
1158
1159 Assert(pAioMgr->fBlockingEventPending);
1160
1161 switch (pAioMgr->enmBlockingEvent)
1162 {
1163 case PDMACEPFILEAIOMGRBLOCKINGEVENT_ADD_ENDPOINT:
1164 {
1165 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointNew = ASMAtomicReadPtrT(&pAioMgr->BlockingEventData.AddEndpoint.pEndpoint, PPDMASYNCCOMPLETIONENDPOINTFILE);
1166 AssertMsg(VALID_PTR(pEndpointNew), ("Adding endpoint event without a endpoint to add\n"));
1167
1168 pEndpointNew->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
1169
1170 pEndpointNew->AioMgr.pEndpointNext = pAioMgr->pEndpointsHead;
1171 pEndpointNew->AioMgr.pEndpointPrev = NULL;
1172 if (pAioMgr->pEndpointsHead)
1173 pAioMgr->pEndpointsHead->AioMgr.pEndpointPrev = pEndpointNew;
1174 pAioMgr->pEndpointsHead = pEndpointNew;
1175
1176 /* Assign the completion point to this file. */
1177 rc = RTFileAioCtxAssociateWithFile(pAioMgr->hAioCtx, pEndpointNew->hFile);
1178 fNotifyWaiter = true;
1179 pAioMgr->cEndpoints++;
1180 break;
1181 }
1182 case PDMACEPFILEAIOMGRBLOCKINGEVENT_REMOVE_ENDPOINT:
1183 {
1184 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointRemove = ASMAtomicReadPtrT(&pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint, PPDMASYNCCOMPLETIONENDPOINTFILE);
1185 AssertMsg(VALID_PTR(pEndpointRemove), ("Removing endpoint event without a endpoint to remove\n"));
1186
1187 pEndpointRemove->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_REMOVING;
1188 fNotifyWaiter = !pdmacFileAioMgrNormalRemoveEndpoint(pEndpointRemove);
1189 break;
1190 }
1191 case PDMACEPFILEAIOMGRBLOCKINGEVENT_CLOSE_ENDPOINT:
1192 {
1193 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointClose = ASMAtomicReadPtrT(&pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint, PPDMASYNCCOMPLETIONENDPOINTFILE);
1194 AssertMsg(VALID_PTR(pEndpointClose), ("Close endpoint event without a endpoint to close\n"));
1195
1196 if (pEndpointClose->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE)
1197 {
1198 LogFlowFunc((": Closing endpoint %#p{%s}\n", pEndpointClose, pEndpointClose->Core.pszUri));
1199
1200 /* Make sure all tasks finished. Process the queues a last time first. */
1201 rc = pdmacFileAioMgrNormalQueueReqs(pAioMgr, pEndpointClose);
1202 AssertRC(rc);
1203
1204 pEndpointClose->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_CLOSING;
1205 fNotifyWaiter = !pdmacFileAioMgrNormalRemoveEndpoint(pEndpointClose);
1206 }
1207 else if ( (pEndpointClose->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_CLOSING)
1208 && (!pEndpointClose->AioMgr.cRequestsActive))
1209 fNotifyWaiter = true;
1210 break;
1211 }
1212 case PDMACEPFILEAIOMGRBLOCKINGEVENT_SHUTDOWN:
1213 {
1214 pAioMgr->enmState = PDMACEPFILEMGRSTATE_SHUTDOWN;
1215 if (!pAioMgr->cRequestsActive)
1216 fNotifyWaiter = true;
1217 break;
1218 }
1219 case PDMACEPFILEAIOMGRBLOCKINGEVENT_SUSPEND:
1220 {
1221 pAioMgr->enmState = PDMACEPFILEMGRSTATE_SUSPENDING;
1222 break;
1223 }
1224 case PDMACEPFILEAIOMGRBLOCKINGEVENT_RESUME:
1225 {
1226 pAioMgr->enmState = PDMACEPFILEMGRSTATE_RUNNING;
1227 fNotifyWaiter = true;
1228 break;
1229 }
1230 default:
1231 AssertReleaseMsgFailed(("Invalid event type %d\n", pAioMgr->enmBlockingEvent));
1232 }
1233
1234 if (fNotifyWaiter)
1235 {
1236 ASMAtomicWriteBool(&pAioMgr->fBlockingEventPending, false);
1237 pAioMgr->enmBlockingEvent = PDMACEPFILEAIOMGRBLOCKINGEVENT_INVALID;
1238
1239 /* Release the waiting thread. */
1240 LogFlow(("Signalling waiter\n"));
1241 rc = RTSemEventSignal(pAioMgr->EventSemBlock);
1242 AssertRC(rc);
1243 }
1244
1245 LogFlowFunc((": Leave\n"));
1246 return rc;
1247}
1248
1249/**
1250 * Checks all endpoints for pending events or new requests.
1251 *
1252 * @returns VBox status code.
1253 * @param pAioMgr The I/O manager handle.
1254 */
1255static int pdmacFileAioMgrNormalCheckEndpoints(PPDMACEPFILEMGR pAioMgr)
1256{
1257 /* Check the assigned endpoints for new tasks if there isn't a flush request active at the moment. */
1258 int rc = VINF_SUCCESS;
1259 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint = pAioMgr->pEndpointsHead;
1260
1261 pAioMgr->msBwLimitExpired = RT_INDEFINITE_WAIT;
1262
1263 while (pEndpoint)
1264 {
1265 if (!pEndpoint->pFlushReq
1266 && (pEndpoint->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE)
1267 && !pEndpoint->AioMgr.fMoving)
1268 {
1269 rc = pdmacFileAioMgrNormalQueueReqs(pAioMgr, pEndpoint);
1270 if (RT_FAILURE(rc))
1271 return rc;
1272 }
1273 else if ( !pEndpoint->AioMgr.cRequestsActive
1274 && pEndpoint->enmState != PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE)
1275 {
1276 /* Reopen the file so that the new endpoint can re-associate with the file */
1277 RTFileClose(pEndpoint->hFile);
1278 rc = RTFileOpen(&pEndpoint->hFile, pEndpoint->Core.pszUri, pEndpoint->fFlags);
1279 AssertRC(rc);
1280
1281 if (pEndpoint->AioMgr.fMoving)
1282 {
1283 pEndpoint->AioMgr.fMoving = false;
1284 pdmacFileAioMgrAddEndpoint(pEndpoint->AioMgr.pAioMgrDst, pEndpoint);
1285 }
1286 else
1287 {
1288 Assert(pAioMgr->fBlockingEventPending);
1289 ASMAtomicWriteBool(&pAioMgr->fBlockingEventPending, false);
1290
1291 /* Release the waiting thread. */
1292 LogFlow(("Signalling waiter\n"));
1293 rc = RTSemEventSignal(pAioMgr->EventSemBlock);
1294 AssertRC(rc);
1295 }
1296 }
1297
1298 pEndpoint = pEndpoint->AioMgr.pEndpointNext;
1299 }
1300
1301 return rc;
1302}
1303
1304/**
1305 * Wrapper around pdmacFileAioMgrNormalReqCompleteRc().
1306 */
1307static void pdmacFileAioMgrNormalReqComplete(PPDMACEPFILEMGR pAioMgr, RTFILEAIOREQ hReq)
1308{
1309 size_t cbTransfered = 0;
1310 int rcReq = RTFileAioReqGetRC(hReq, &cbTransfered);
1311
1312 pdmacFileAioMgrNormalReqCompleteRc(pAioMgr, hReq, rcReq, cbTransfered);
1313}
1314
1315static void pdmacFileAioMgrNormalReqCompleteRc(PPDMACEPFILEMGR pAioMgr, RTFILEAIOREQ hReq,
1316 int rcReq, size_t cbTransfered)
1317{
1318 int rc = VINF_SUCCESS;
1319 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint;
1320 PPDMACTASKFILE pTask = (PPDMACTASKFILE)RTFileAioReqGetUser(hReq);
1321 PPDMACTASKFILE pTasksWaiting;
1322
1323 LogFlowFunc(("pAioMgr=%#p hReq=%#p\n", pAioMgr, hReq));
1324
1325 pEndpoint = pTask->pEndpoint;
1326
1327 pTask->hReq = NIL_RTFILEAIOREQ;
1328
1329 pAioMgr->cRequestsActive--;
1330 pEndpoint->AioMgr.cRequestsActive--;
1331 pEndpoint->AioMgr.cReqsProcessed++;
1332
1333 /*
1334 * It is possible that the request failed on Linux with kernels < 2.6.23
1335 * if the passed buffer was allocated with remap_pfn_range or if the file
1336 * is on an NFS endpoint which does not support async and direct I/O at the same time.
1337 * The endpoint will be migrated to a failsafe manager in case a request fails.
1338 */
1339 if (RT_FAILURE(rcReq))
1340 {
1341 /* Free bounce buffers and the IPRT request. */
1342 pdmacFileAioMgrNormalRequestFree(pAioMgr, hReq);
1343
1344 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_FLUSH)
1345 {
1346 LogFlow(("Async flushes are not supported for this endpoint, disabling\n"));
1347 pEndpoint->fAsyncFlushSupported = false;
1348 AssertMsg(pEndpoint->pFlushReq == pTask, ("Failed flush request doesn't match active one\n"));
1349 /* The other method will take over now. */
1350 }
1351 else
1352 {
1353 /* Free the lock and process pending tasks if necessary */
1354 pTasksWaiting = pdmacFileAioMgrNormalRangeLockFree(pAioMgr, pEndpoint, pTask->pRangeLock);
1355 rc = pdmacFileAioMgrNormalProcessTaskList(pTasksWaiting, pAioMgr, pEndpoint);
1356 AssertRC(rc);
1357
1358 if (pTask->cbBounceBuffer)
1359 RTMemPageFree(pTask->pvBounceBuffer, pTask->cbBounceBuffer);
1360
1361 /*
1362 * Fatal errors are reported to the guest and non-fatal errors
1363 * will cause a migration to the failsafe manager in the hope
1364 * that the error disappears.
1365 */
1366 if (!pdmacFileAioMgrNormalRcIsFatal(rcReq))
1367 {
1368 /* Queue the request on the pending list. */
1369 pTask->pNext = pEndpoint->AioMgr.pReqsPendingHead;
1370 pEndpoint->AioMgr.pReqsPendingHead = pTask;
1371
1372 /* Create a new failsafe manager if necessary. */
1373 if (!pEndpoint->AioMgr.fMoving)
1374 {
1375 PPDMACEPFILEMGR pAioMgrFailsafe;
1376
1377 LogRel(("%s: Request %#p failed with rc=%Rrc, migrating endpoint %s to failsafe manager.\n",
1378 RTThreadGetName(pAioMgr->Thread), pTask, rcReq, pEndpoint->Core.pszUri));
1379
1380 pEndpoint->AioMgr.fMoving = true;
1381
1382 rc = pdmacFileAioMgrCreate((PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass,
1383 &pAioMgrFailsafe, PDMACEPFILEMGRTYPE_SIMPLE);
1384 AssertRC(rc);
1385
1386 pEndpoint->AioMgr.pAioMgrDst = pAioMgrFailsafe;
1387
1388 /* Update the flags to open the file with. Disable async I/O and enable the host cache. */
1389 pEndpoint->fFlags &= ~(RTFILE_O_ASYNC_IO | RTFILE_O_NO_CACHE);
1390 }
1391
1392 /* If this was the last request for the endpoint migrate it to the new manager. */
1393 if (!pEndpoint->AioMgr.cRequestsActive)
1394 {
1395 bool fReqsPending = pdmacFileAioMgrNormalRemoveEndpoint(pEndpoint);
1396 Assert(!fReqsPending);
1397
1398 rc = pdmacFileAioMgrAddEndpoint(pEndpoint->AioMgr.pAioMgrDst, pEndpoint);
1399 AssertRC(rc);
1400 }
1401 }
1402 else
1403 {
1404 pTask->pfnCompleted(pTask, pTask->pvUser, rcReq);
1405 pdmacFileTaskFree(pEndpoint, pTask);
1406 }
1407 }
1408 }
1409 else
1410 {
1411 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_FLUSH)
1412 {
1413 /* Clear pending flush */
1414 AssertMsg(pEndpoint->pFlushReq == pTask, ("Completed flush request doesn't match active one\n"));
1415 pEndpoint->pFlushReq = NULL;
1416 pdmacFileAioMgrNormalRequestFree(pAioMgr, hReq);
1417
1418 /* Call completion callback */
1419 LogFlow(("Flush task=%#p completed with %Rrc\n", pTask, rcReq));
1420 pTask->pfnCompleted(pTask, pTask->pvUser, rcReq);
1421 pdmacFileTaskFree(pEndpoint, pTask);
1422 }
1423 else
1424 {
1425 /*
1426 * Restart an incomplete transfer.
1427 * This usually means that the request will return an error now
1428 * but to get the cause of the error (disk full, file too big, I/O error, ...)
1429 * the transfer needs to be continued.
1430 */
1431 if (RT_UNLIKELY( cbTransfered < pTask->DataSeg.cbSeg
1432 || ( pTask->cbBounceBuffer
1433 && cbTransfered < pTask->cbBounceBuffer)))
1434 {
1435 RTFOFF offStart;
1436 size_t cbToTransfer;
1437 uint8_t *pbBuf = NULL;
1438
1439 LogFlow(("Restarting incomplete transfer %#p (%zu bytes transferred)\n",
1440 pTask, cbTransfered));
1441 Assert(cbTransfered % 512 == 0);
1442
1443 if (pTask->cbBounceBuffer)
1444 {
1445 AssertPtr(pTask->pvBounceBuffer);
1446 offStart = (pTask->Off & ~((RTFOFF)512-1)) + cbTransfered;
1447 cbToTransfer = pTask->cbBounceBuffer - cbTransfered;
1448 pbBuf = (uint8_t *)pTask->pvBounceBuffer + cbTransfered;
1449 }
1450 else
1451 {
1452 Assert(!pTask->pvBounceBuffer);
1453 offStart = pTask->Off + cbTransfered;
1454 cbToTransfer = pTask->DataSeg.cbSeg - cbTransfered;
1455 pbBuf = (uint8_t *)pTask->DataSeg.pvSeg + cbTransfered;
1456 }
1457
1458 if (pTask->fPrefetch || pTask->enmTransferType == PDMACTASKFILETRANSFER_READ)
1459 {
1460 rc = RTFileAioReqPrepareRead(hReq, pEndpoint->hFile, offStart,
1461 pbBuf, cbToTransfer, pTask);
1462 }
1463 else
1464 {
1465 AssertMsg(pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE,
1466 ("Invalid transfer type\n"));
1467 rc = RTFileAioReqPrepareWrite(hReq, pEndpoint->hFile, offStart,
1468 pbBuf, cbToTransfer, pTask);
1469 }
1470 AssertRC(rc);
1471
1472 pTask->hReq = hReq;
1473 rc = pdmacFileAioMgrNormalReqsEnqueue(pAioMgr, pEndpoint, &hReq, 1);
1474 AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
1475 ("Unexpected return code rc=%Rrc\n", rc));
1476 }
1477 else if (pTask->fPrefetch)
1478 {
1479 Assert(pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE);
1480 Assert(pTask->cbBounceBuffer);
1481
1482 memcpy(((uint8_t *)pTask->pvBounceBuffer) + pTask->offBounceBuffer,
1483 pTask->DataSeg.pvSeg,
1484 pTask->DataSeg.cbSeg);
1485
1486 /* Write it now. */
1487 pTask->fPrefetch = false;
1488 size_t cbToTransfer = RT_ALIGN_Z(pTask->DataSeg.cbSeg, 512);
1489 RTFOFF offStart = pTask->Off & ~(RTFOFF)(512-1);
1490
1491 /* Grow the file if needed. */
1492 if (RT_UNLIKELY((uint64_t)(pTask->Off + pTask->DataSeg.cbSeg) > pEndpoint->cbFile))
1493 {
1494 ASMAtomicWriteU64(&pEndpoint->cbFile, pTask->Off + pTask->DataSeg.cbSeg);
1495 RTFileSetSize(pEndpoint->hFile, pTask->Off + pTask->DataSeg.cbSeg);
1496 }
1497
1498 rc = RTFileAioReqPrepareWrite(hReq, pEndpoint->hFile,
1499 offStart, pTask->pvBounceBuffer, cbToTransfer, pTask);
1500 AssertRC(rc);
1501 pTask->hReq = hReq;
1502 rc = pdmacFileAioMgrNormalReqsEnqueue(pAioMgr, pEndpoint, &hReq, 1);
1503 AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
1504 ("Unexpected return code rc=%Rrc\n", rc));
1505 }
1506 else
1507 {
1508 if (RT_SUCCESS(rc) && pTask->cbBounceBuffer)
1509 {
1510 if (pTask->enmTransferType == PDMACTASKFILETRANSFER_READ)
1511 memcpy(pTask->DataSeg.pvSeg,
1512 ((uint8_t *)pTask->pvBounceBuffer) + pTask->offBounceBuffer,
1513 pTask->DataSeg.cbSeg);
1514
1515 RTMemPageFree(pTask->pvBounceBuffer, pTask->cbBounceBuffer);
1516 }
1517
1518 pdmacFileAioMgrNormalRequestFree(pAioMgr, hReq);
1519
1520 /* Free the lock and process pending tasks if necessary */
1521 pTasksWaiting = pdmacFileAioMgrNormalRangeLockFree(pAioMgr, pEndpoint, pTask->pRangeLock);
1522 if (pTasksWaiting)
1523 {
1524 rc = pdmacFileAioMgrNormalProcessTaskList(pTasksWaiting, pAioMgr, pEndpoint);
1525 AssertRC(rc);
1526 }
1527
1528 /* Call completion callback */
1529 LogFlow(("Task=%#p completed with %Rrc\n", pTask, rcReq));
1530 pTask->pfnCompleted(pTask, pTask->pvUser, rcReq);
1531 pdmacFileTaskFree(pEndpoint, pTask);
1532
1533 /*
1534 * If there is no request left on the endpoint but a flush request is set
1535 * it completed now and we notify the owner.
1536 * Furthermore we look for new requests and continue.
1537 */
1538 if (!pEndpoint->AioMgr.cRequestsActive && pEndpoint->pFlushReq)
1539 {
1540 /* Call completion callback */
1541 pTask = pEndpoint->pFlushReq;
1542 pEndpoint->pFlushReq = NULL;
1543
1544 AssertMsg(pTask->pEndpoint == pEndpoint, ("Endpoint of the flush request does not match assigned one\n"));
1545
1546 pTask->pfnCompleted(pTask, pTask->pvUser, VINF_SUCCESS);
1547 pdmacFileTaskFree(pEndpoint, pTask);
1548 }
1549 else if (RT_UNLIKELY(!pEndpoint->AioMgr.cRequestsActive && pEndpoint->AioMgr.fMoving))
1550 {
1551 /* If the endpoint is about to be migrated do it now. */
1552 bool fReqsPending = pdmacFileAioMgrNormalRemoveEndpoint(pEndpoint);
1553 Assert(!fReqsPending);
1554
1555 rc = pdmacFileAioMgrAddEndpoint(pEndpoint->AioMgr.pAioMgrDst, pEndpoint);
1556 AssertRC(rc);
1557 }
1558 }
1559 } /* Not a flush request */
1560 } /* request completed successfully */
1561}
1562
1563/** Helper macro for checking for error codes. */
1564#define CHECK_RC(pAioMgr, rc) \
1565 if (RT_FAILURE(rc)) \
1566 {\
1567 int rc2 = pdmacFileAioMgrNormalErrorHandler(pAioMgr, rc, RT_SRC_POS);\
1568 return rc2;\
1569 }
1570
1571/**
1572 * The normal I/O manager using the RTFileAio* API
1573 *
1574 * @returns VBox status code.
1575 * @param hThreadSelf Handle of the thread.
1576 * @param pvUser Opaque user data.
1577 */
1578DECLCALLBACK(int) pdmacFileAioMgrNormal(RTTHREAD hThreadSelf, void *pvUser)
1579{
1580 int rc = VINF_SUCCESS;
1581 PPDMACEPFILEMGR pAioMgr = (PPDMACEPFILEMGR)pvUser;
1582 uint64_t uMillisEnd = RTTimeMilliTS() + PDMACEPFILEMGR_LOAD_UPDATE_PERIOD;
1583 NOREF(hThreadSelf);
1584
1585 while ( pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING
1586 || pAioMgr->enmState == PDMACEPFILEMGRSTATE_SUSPENDING
1587 || pAioMgr->enmState == PDMACEPFILEMGRSTATE_GROWING)
1588 {
1589 if (!pAioMgr->cRequestsActive)
1590 {
1591 ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, true);
1592 if (!ASMAtomicReadBool(&pAioMgr->fWokenUp))
1593 rc = RTSemEventWait(pAioMgr->EventSem, pAioMgr->msBwLimitExpired);
1594 ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, false);
1595 Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
1596
1597 LogFlow(("Got woken up\n"));
1598 ASMAtomicWriteBool(&pAioMgr->fWokenUp, false);
1599 }
1600
1601 /* Check for an external blocking event first. */
1602 if (pAioMgr->fBlockingEventPending)
1603 {
1604 rc = pdmacFileAioMgrNormalProcessBlockingEvent(pAioMgr);
1605 CHECK_RC(pAioMgr, rc);
1606 }
1607
1608 if (RT_LIKELY( pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING
1609 || pAioMgr->enmState == PDMACEPFILEMGRSTATE_GROWING))
1610 {
1611 /* We got woken up because an endpoint issued new requests. Queue them. */
1612 rc = pdmacFileAioMgrNormalCheckEndpoints(pAioMgr);
1613 CHECK_RC(pAioMgr, rc);
1614
1615 while (pAioMgr->cRequestsActive)
1616 {
1617 RTFILEAIOREQ apReqs[20];
1618 uint32_t cReqsCompleted = 0;
1619 size_t cReqsWait;
1620
1621 if (pAioMgr->cRequestsActive > RT_ELEMENTS(apReqs))
1622 cReqsWait = RT_ELEMENTS(apReqs);
1623 else
1624 cReqsWait = pAioMgr->cRequestsActive;
1625
1626 LogFlow(("Waiting for %d of %d tasks to complete\n", 1, cReqsWait));
1627
1628 rc = RTFileAioCtxWait(pAioMgr->hAioCtx,
1629 1,
1630 RT_INDEFINITE_WAIT, apReqs,
1631 cReqsWait, &cReqsCompleted);
1632 if (RT_FAILURE(rc) && (rc != VERR_INTERRUPTED))
1633 CHECK_RC(pAioMgr, rc);
1634
1635 LogFlow(("%d tasks completed\n", cReqsCompleted));
1636
1637 for (uint32_t i = 0; i < cReqsCompleted; i++)
1638 pdmacFileAioMgrNormalReqComplete(pAioMgr, apReqs[i]);
1639
1640 /* Check for an external blocking event before we go to sleep again. */
1641 if (pAioMgr->fBlockingEventPending)
1642 {
1643 rc = pdmacFileAioMgrNormalProcessBlockingEvent(pAioMgr);
1644 CHECK_RC(pAioMgr, rc);
1645 }
1646
1647 /* Update load statistics. */
1648 uint64_t uMillisCurr = RTTimeMilliTS();
1649 if (uMillisCurr > uMillisEnd)
1650 {
1651 PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointCurr = pAioMgr->pEndpointsHead;
1652
1653 /* Calculate timespan. */
1654 uMillisCurr -= uMillisEnd;
1655
1656 while (pEndpointCurr)
1657 {
1658 pEndpointCurr->AioMgr.cReqsPerSec = pEndpointCurr->AioMgr.cReqsProcessed / (uMillisCurr + PDMACEPFILEMGR_LOAD_UPDATE_PERIOD);
1659 pEndpointCurr->AioMgr.cReqsProcessed = 0;
1660 pEndpointCurr = pEndpointCurr->AioMgr.pEndpointNext;
1661 }
1662
1663 /* Set new update interval */
1664 uMillisEnd = RTTimeMilliTS() + PDMACEPFILEMGR_LOAD_UPDATE_PERIOD;
1665 }
1666
1667 /* Check endpoints for new requests. */
1668 if (pAioMgr->enmState != PDMACEPFILEMGRSTATE_GROWING)
1669 {
1670 rc = pdmacFileAioMgrNormalCheckEndpoints(pAioMgr);
1671 CHECK_RC(pAioMgr, rc);
1672 }
1673 } /* while requests are active. */
1674
1675 if (pAioMgr->enmState == PDMACEPFILEMGRSTATE_GROWING)
1676 {
1677 rc = pdmacFileAioMgrNormalGrow(pAioMgr);
1678 AssertRC(rc);
1679 Assert(pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING);
1680
1681 rc = pdmacFileAioMgrNormalCheckEndpoints(pAioMgr);
1682 CHECK_RC(pAioMgr, rc);
1683 }
1684 } /* if still running */
1685 } /* while running */
1686
1687 LogFlowFunc(("rc=%Rrc\n", rc));
1688 return rc;
1689}
1690
1691#undef CHECK_RC
1692
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