1 | /* $Id: PDMAsyncCompletionFileNormal.cpp 43725 2012-10-24 11:43:51Z 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 | *******************************************************************************/
|
---|
41 | static int pdmacFileAioMgrNormalProcessTaskList(PPDMACTASKFILE pTaskHead,
|
---|
42 | PPDMACEPFILEMGR pAioMgr,
|
---|
43 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint);
|
---|
44 |
|
---|
45 | static PPDMACTASKFILE pdmacFileAioMgrNormalRangeLockFree(PPDMACEPFILEMGR pAioMgr,
|
---|
46 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
|
---|
47 | PPDMACFILERANGELOCK pRangeLock);
|
---|
48 |
|
---|
49 | static void pdmacFileAioMgrNormalReqCompleteRc(PPDMACEPFILEMGR pAioMgr, RTFILEAIOREQ hReq,
|
---|
50 | int rc, size_t cbTransfered);
|
---|
51 |
|
---|
52 |
|
---|
53 | int 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 |
|
---|
88 | void 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 | */
|
---|
107 | static 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 | */
|
---|
189 | static 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 |
|
---|
222 | static 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 | */
|
---|
251 | static 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 | */
|
---|
329 | static 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 | */
|
---|
422 | DECLINLINE(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 | */
|
---|
438 | static 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 | */
|
---|
457 | DECLINLINE(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 | */
|
---|
482 | DECLINLINE(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 | */
|
---|
506 | static 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 | */
|
---|
533 | static 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 | */
|
---|
545 | static 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 |
|
---|
638 | static 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 |
|
---|
689 | static int pdmacFileAioMgrNormalRangeLock(PPDMACEPFILEMGR pAioMgr,
|
---|
690 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
|
---|
691 | RTFOFF offStart, size_t cbRange,
|
---|
692 | PPDMACTASKFILE pTask)
|
---|
693 | {
|
---|
694 | LogFlowFunc(("pAioMgr=%#p pEndpoint=%#p offStart=%RTfoff cbRange=%zu pTask=%#p\n",
|
---|
695 | pAioMgr, pEndpoint, offStart, cbRange, pTask));
|
---|
696 |
|
---|
697 | AssertMsg(!pdmacFileAioMgrNormalIsRangeLocked(pEndpoint, offStart, cbRange, pTask),
|
---|
698 | ("Range is already locked offStart=%RTfoff cbRange=%u\n",
|
---|
699 | offStart, cbRange));
|
---|
700 |
|
---|
701 | PPDMACFILERANGELOCK pRangeLock = (PPDMACFILERANGELOCK)RTMemCacheAlloc(pAioMgr->hMemCacheRangeLocks);
|
---|
702 | if (!pRangeLock)
|
---|
703 | return VERR_NO_MEMORY;
|
---|
704 |
|
---|
705 | /* Init the lock. */
|
---|
706 | pRangeLock->Core.Key = offStart;
|
---|
707 | pRangeLock->Core.KeyLast = offStart + cbRange - 1;
|
---|
708 | pRangeLock->cRefs = 1;
|
---|
709 | pRangeLock->fReadLock = pTask->enmTransferType == PDMACTASKFILETRANSFER_READ;
|
---|
710 | pRangeLock->pWaitingTasksHead = NULL;
|
---|
711 | pRangeLock->pWaitingTasksTail = NULL;
|
---|
712 |
|
---|
713 | bool fInserted = RTAvlrFileOffsetInsert(pEndpoint->AioMgr.pTreeRangesLocked, &pRangeLock->Core);
|
---|
714 | AssertMsg(fInserted, ("Range lock was not inserted!\n"));
|
---|
715 |
|
---|
716 | /* Let the task point to its lock. */
|
---|
717 | pTask->pRangeLock = pRangeLock;
|
---|
718 |
|
---|
719 | return VINF_SUCCESS;
|
---|
720 | }
|
---|
721 |
|
---|
722 | static PPDMACTASKFILE pdmacFileAioMgrNormalRangeLockFree(PPDMACEPFILEMGR pAioMgr,
|
---|
723 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
|
---|
724 | PPDMACFILERANGELOCK pRangeLock)
|
---|
725 | {
|
---|
726 | PPDMACTASKFILE pTasksWaitingHead;
|
---|
727 |
|
---|
728 | LogFlowFunc(("pAioMgr=%#p pEndpoint=%#p pRangeLock=%#p\n",
|
---|
729 | pAioMgr, pEndpoint, pRangeLock));
|
---|
730 |
|
---|
731 | AssertPtr(pRangeLock);
|
---|
732 | Assert(pRangeLock->cRefs == 1);
|
---|
733 |
|
---|
734 | RTAvlrFileOffsetRemove(pEndpoint->AioMgr.pTreeRangesLocked, pRangeLock->Core.Key);
|
---|
735 | pTasksWaitingHead = pRangeLock->pWaitingTasksHead;
|
---|
736 | pRangeLock->pWaitingTasksHead = NULL;
|
---|
737 | pRangeLock->pWaitingTasksTail = NULL;
|
---|
738 | RTMemCacheFree(pAioMgr->hMemCacheRangeLocks, pRangeLock);
|
---|
739 |
|
---|
740 | return pTasksWaitingHead;
|
---|
741 | }
|
---|
742 |
|
---|
743 | static int pdmacFileAioMgrNormalTaskPrepareBuffered(PPDMACEPFILEMGR pAioMgr,
|
---|
744 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
|
---|
745 | PPDMACTASKFILE pTask, PRTFILEAIOREQ phReq)
|
---|
746 | {
|
---|
747 | AssertMsg( pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE
|
---|
748 | || (uint64_t)(pTask->Off + pTask->DataSeg.cbSeg) <= pEndpoint->cbFile,
|
---|
749 | ("Read exceeds file size offStart=%RTfoff cbToTransfer=%d cbFile=%llu\n",
|
---|
750 | pTask->Off, pTask->DataSeg.cbSeg, pEndpoint->cbFile));
|
---|
751 |
|
---|
752 | pTask->fPrefetch = false;
|
---|
753 | pTask->cbBounceBuffer = 0;
|
---|
754 |
|
---|
755 | /*
|
---|
756 | * Before we start to setup the request we have to check whether there is a task
|
---|
757 | * already active which range intersects with ours. We have to defer execution
|
---|
758 | * of this task in two cases:
|
---|
759 | * - The pending task is a write and the current is either read or write
|
---|
760 | * - The pending task is a read and the current task is a write task.
|
---|
761 | *
|
---|
762 | * To check whether a range is currently "locked" we use the AVL tree where every pending task
|
---|
763 | * is stored by its file offset range. The current task will be added to the active task
|
---|
764 | * and will be executed when the active one completes. (The method below
|
---|
765 | * which checks whether a range is already used will add the task)
|
---|
766 | *
|
---|
767 | * This is necessary because of the requirement to align all requests to a 512 boundary
|
---|
768 | * which is enforced by the host OS (Linux and Windows atm). It is possible that
|
---|
769 | * we have to process unaligned tasks and need to align them using bounce buffers.
|
---|
770 | * While the data is fetched from the file another request might arrive writing to
|
---|
771 | * the same range. This will result in data corruption if both are executed concurrently.
|
---|
772 | */
|
---|
773 | int rc = VINF_SUCCESS;
|
---|
774 | bool fLocked = pdmacFileAioMgrNormalIsRangeLocked(pEndpoint, pTask->Off, pTask->DataSeg.cbSeg, pTask);
|
---|
775 | if (!fLocked)
|
---|
776 | {
|
---|
777 | /* Get a request handle. */
|
---|
778 | RTFILEAIOREQ hReq = pdmacFileAioMgrNormalRequestAlloc(pAioMgr);
|
---|
779 | AssertMsg(hReq != NIL_RTFILEAIOREQ, ("Out of request handles\n"));
|
---|
780 |
|
---|
781 | if (pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE)
|
---|
782 | {
|
---|
783 | /* Grow the file if needed. */
|
---|
784 | if (RT_UNLIKELY((uint64_t)(pTask->Off + pTask->DataSeg.cbSeg) > pEndpoint->cbFile))
|
---|
785 | {
|
---|
786 | ASMAtomicWriteU64(&pEndpoint->cbFile, pTask->Off + pTask->DataSeg.cbSeg);
|
---|
787 | RTFileSetSize(pEndpoint->hFile, pTask->Off + pTask->DataSeg.cbSeg);
|
---|
788 | }
|
---|
789 |
|
---|
790 | rc = RTFileAioReqPrepareWrite(hReq, pEndpoint->hFile,
|
---|
791 | pTask->Off, pTask->DataSeg.pvSeg,
|
---|
792 | pTask->DataSeg.cbSeg, pTask);
|
---|
793 | }
|
---|
794 | else
|
---|
795 | rc = RTFileAioReqPrepareRead(hReq, pEndpoint->hFile,
|
---|
796 | pTask->Off, pTask->DataSeg.pvSeg,
|
---|
797 | pTask->DataSeg.cbSeg, pTask);
|
---|
798 | AssertRC(rc);
|
---|
799 |
|
---|
800 | rc = pdmacFileAioMgrNormalRangeLock(pAioMgr, pEndpoint, pTask->Off,
|
---|
801 | pTask->DataSeg.cbSeg,
|
---|
802 | pTask);
|
---|
803 |
|
---|
804 | if (RT_SUCCESS(rc))
|
---|
805 | {
|
---|
806 | pTask->hReq = hReq;
|
---|
807 | *phReq = hReq;
|
---|
808 | }
|
---|
809 | }
|
---|
810 | else
|
---|
811 | LogFlow(("Task %#p was deferred because the access range is locked\n", pTask));
|
---|
812 |
|
---|
813 | return rc;
|
---|
814 | }
|
---|
815 |
|
---|
816 | static int pdmacFileAioMgrNormalTaskPrepareNonBuffered(PPDMACEPFILEMGR pAioMgr,
|
---|
817 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint,
|
---|
818 | PPDMACTASKFILE pTask, PRTFILEAIOREQ phReq)
|
---|
819 | {
|
---|
820 | /*
|
---|
821 | * Check if the alignment requirements are met.
|
---|
822 | * Offset, transfer size and buffer address
|
---|
823 | * need to be on a 512 boundary.
|
---|
824 | */
|
---|
825 | RTFOFF offStart = pTask->Off & ~(RTFOFF)(512-1);
|
---|
826 | size_t cbToTransfer = RT_ALIGN_Z(pTask->DataSeg.cbSeg + (pTask->Off - offStart), 512);
|
---|
827 | PDMACTASKFILETRANSFER enmTransferType = pTask->enmTransferType;
|
---|
828 |
|
---|
829 | AssertMsg( pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE
|
---|
830 | || (uint64_t)(offStart + cbToTransfer) <= pEndpoint->cbFile,
|
---|
831 | ("Read exceeds file size offStart=%RTfoff cbToTransfer=%d cbFile=%llu\n",
|
---|
832 | offStart, cbToTransfer, pEndpoint->cbFile));
|
---|
833 |
|
---|
834 | pTask->fPrefetch = false;
|
---|
835 |
|
---|
836 | /*
|
---|
837 | * Before we start to setup the request we have to check whether there is a task
|
---|
838 | * already active which range intersects with ours. We have to defer execution
|
---|
839 | * of this task in two cases:
|
---|
840 | * - The pending task is a write and the current is either read or write
|
---|
841 | * - The pending task is a read and the current task is a write task.
|
---|
842 | *
|
---|
843 | * To check whether a range is currently "locked" we use the AVL tree where every pending task
|
---|
844 | * is stored by its file offset range. The current task will be added to the active task
|
---|
845 | * and will be executed when the active one completes. (The method below
|
---|
846 | * which checks whether a range is already used will add the task)
|
---|
847 | *
|
---|
848 | * This is necessary because of the requirement to align all requests to a 512 boundary
|
---|
849 | * which is enforced by the host OS (Linux and Windows atm). It is possible that
|
---|
850 | * we have to process unaligned tasks and need to align them using bounce buffers.
|
---|
851 | * While the data is fetched from the file another request might arrive writing to
|
---|
852 | * the same range. This will result in data corruption if both are executed concurrently.
|
---|
853 | */
|
---|
854 | int rc = VINF_SUCCESS;
|
---|
855 | bool fLocked = pdmacFileAioMgrNormalIsRangeLocked(pEndpoint, offStart, cbToTransfer, pTask);
|
---|
856 | if (!fLocked)
|
---|
857 | {
|
---|
858 | PPDMASYNCCOMPLETIONEPCLASSFILE pEpClassFile = (PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass;
|
---|
859 | void *pvBuf = pTask->DataSeg.pvSeg;
|
---|
860 |
|
---|
861 | /* Get a request handle. */
|
---|
862 | RTFILEAIOREQ hReq = pdmacFileAioMgrNormalRequestAlloc(pAioMgr);
|
---|
863 | AssertMsg(hReq != NIL_RTFILEAIOREQ, ("Out of request handles\n"));
|
---|
864 |
|
---|
865 | if ( RT_UNLIKELY(cbToTransfer != pTask->DataSeg.cbSeg)
|
---|
866 | || RT_UNLIKELY(offStart != pTask->Off)
|
---|
867 | || ((pEpClassFile->uBitmaskAlignment & (RTR3UINTPTR)pvBuf) != (RTR3UINTPTR)pvBuf))
|
---|
868 | {
|
---|
869 | LogFlow(("Using bounce buffer for task %#p cbToTransfer=%zd cbSeg=%zd offStart=%RTfoff off=%RTfoff\n",
|
---|
870 | pTask, cbToTransfer, pTask->DataSeg.cbSeg, offStart, pTask->Off));
|
---|
871 |
|
---|
872 | /* Create bounce buffer. */
|
---|
873 | pTask->cbBounceBuffer = cbToTransfer;
|
---|
874 |
|
---|
875 | AssertMsg(pTask->Off >= offStart, ("Overflow in calculation Off=%llu offStart=%llu\n",
|
---|
876 | pTask->Off, offStart));
|
---|
877 | pTask->offBounceBuffer = pTask->Off - offStart;
|
---|
878 |
|
---|
879 | /** @todo: I think we need something like a RTMemAllocAligned method here.
|
---|
880 | * Current assumption is that the maximum alignment is 4096byte
|
---|
881 | * (GPT disk on Windows)
|
---|
882 | * so we can use RTMemPageAlloc here.
|
---|
883 | */
|
---|
884 | pTask->pvBounceBuffer = RTMemPageAlloc(cbToTransfer);
|
---|
885 | if (RT_LIKELY(pTask->pvBounceBuffer))
|
---|
886 | {
|
---|
887 | pvBuf = pTask->pvBounceBuffer;
|
---|
888 |
|
---|
889 | if (pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE)
|
---|
890 | {
|
---|
891 | if ( RT_UNLIKELY(cbToTransfer != pTask->DataSeg.cbSeg)
|
---|
892 | || RT_UNLIKELY(offStart != pTask->Off))
|
---|
893 | {
|
---|
894 | /* We have to fill the buffer first before we can update the data. */
|
---|
895 | LogFlow(("Prefetching data for task %#p\n", pTask));
|
---|
896 | pTask->fPrefetch = true;
|
---|
897 | enmTransferType = PDMACTASKFILETRANSFER_READ;
|
---|
898 | }
|
---|
899 | else
|
---|
900 | memcpy(pvBuf, pTask->DataSeg.pvSeg, pTask->DataSeg.cbSeg);
|
---|
901 | }
|
---|
902 | }
|
---|
903 | else
|
---|
904 | rc = VERR_NO_MEMORY;
|
---|
905 | }
|
---|
906 | else
|
---|
907 | pTask->cbBounceBuffer = 0;
|
---|
908 |
|
---|
909 | if (RT_SUCCESS(rc))
|
---|
910 | {
|
---|
911 | AssertMsg((pEpClassFile->uBitmaskAlignment & (RTR3UINTPTR)pvBuf) == (RTR3UINTPTR)pvBuf,
|
---|
912 | ("AIO: Alignment restrictions not met! pvBuf=%p uBitmaskAlignment=%p\n", pvBuf, pEpClassFile->uBitmaskAlignment));
|
---|
913 |
|
---|
914 | if (enmTransferType == PDMACTASKFILETRANSFER_WRITE)
|
---|
915 | {
|
---|
916 | /* Grow the file if needed. */
|
---|
917 | if (RT_UNLIKELY((uint64_t)(pTask->Off + pTask->DataSeg.cbSeg) > pEndpoint->cbFile))
|
---|
918 | {
|
---|
919 | ASMAtomicWriteU64(&pEndpoint->cbFile, pTask->Off + pTask->DataSeg.cbSeg);
|
---|
920 | RTFileSetSize(pEndpoint->hFile, pTask->Off + pTask->DataSeg.cbSeg);
|
---|
921 | }
|
---|
922 |
|
---|
923 | rc = RTFileAioReqPrepareWrite(hReq, pEndpoint->hFile,
|
---|
924 | offStart, pvBuf, cbToTransfer, pTask);
|
---|
925 | }
|
---|
926 | else
|
---|
927 | rc = RTFileAioReqPrepareRead(hReq, pEndpoint->hFile,
|
---|
928 | offStart, pvBuf, cbToTransfer, pTask);
|
---|
929 | AssertRC(rc);
|
---|
930 |
|
---|
931 | rc = pdmacFileAioMgrNormalRangeLock(pAioMgr, pEndpoint, offStart, cbToTransfer, pTask);
|
---|
932 |
|
---|
933 | if (RT_SUCCESS(rc))
|
---|
934 | {
|
---|
935 | pTask->hReq = hReq;
|
---|
936 | *phReq = hReq;
|
---|
937 | }
|
---|
938 | else
|
---|
939 | {
|
---|
940 | /* Cleanup */
|
---|
941 | if (pTask->cbBounceBuffer)
|
---|
942 | RTMemPageFree(pTask->pvBounceBuffer, pTask->cbBounceBuffer);
|
---|
943 | }
|
---|
944 | }
|
---|
945 | }
|
---|
946 | else
|
---|
947 | LogFlow(("Task %#p was deferred because the access range is locked\n", pTask));
|
---|
948 |
|
---|
949 | return rc;
|
---|
950 | }
|
---|
951 |
|
---|
952 | static int pdmacFileAioMgrNormalProcessTaskList(PPDMACTASKFILE pTaskHead,
|
---|
953 | PPDMACEPFILEMGR pAioMgr,
|
---|
954 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
|
---|
955 | {
|
---|
956 | RTFILEAIOREQ apReqs[20];
|
---|
957 | unsigned cRequests = 0;
|
---|
958 | int rc = VINF_SUCCESS;
|
---|
959 |
|
---|
960 | AssertMsg(pEndpoint->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE,
|
---|
961 | ("Trying to process request lists of a non active endpoint!\n"));
|
---|
962 |
|
---|
963 | /* Go through the list and queue the requests until we get a flush request */
|
---|
964 | while ( pTaskHead
|
---|
965 | && !pEndpoint->pFlushReq
|
---|
966 | && (pAioMgr->cRequestsActive + cRequests < pAioMgr->cRequestsActiveMax)
|
---|
967 | && RT_SUCCESS(rc))
|
---|
968 | {
|
---|
969 | RTMSINTERVAL msWhenNext;
|
---|
970 | PPDMACTASKFILE pCurr = pTaskHead;
|
---|
971 |
|
---|
972 | if (!pdmacEpIsTransferAllowed(&pEndpoint->Core, (uint32_t)pCurr->DataSeg.cbSeg, &msWhenNext))
|
---|
973 | {
|
---|
974 | pAioMgr->msBwLimitExpired = RT_MIN(pAioMgr->msBwLimitExpired, msWhenNext);
|
---|
975 | break;
|
---|
976 | }
|
---|
977 |
|
---|
978 | pTaskHead = pTaskHead->pNext;
|
---|
979 |
|
---|
980 | pCurr->pNext = NULL;
|
---|
981 |
|
---|
982 | AssertMsg(VALID_PTR(pCurr->pEndpoint) && (pCurr->pEndpoint == pEndpoint),
|
---|
983 | ("Endpoints do not match\n"));
|
---|
984 |
|
---|
985 | switch (pCurr->enmTransferType)
|
---|
986 | {
|
---|
987 | case PDMACTASKFILETRANSFER_FLUSH:
|
---|
988 | {
|
---|
989 | /* If there is no data transfer request this flush request finished immediately. */
|
---|
990 | if (pEndpoint->fAsyncFlushSupported)
|
---|
991 | {
|
---|
992 | /* Issue a flush to the host. */
|
---|
993 | RTFILEAIOREQ hReq = pdmacFileAioMgrNormalRequestAlloc(pAioMgr);
|
---|
994 | AssertMsg(hReq != NIL_RTFILEAIOREQ, ("Out of request handles\n"));
|
---|
995 |
|
---|
996 | LogFlow(("Flush request %#p\n", hReq));
|
---|
997 |
|
---|
998 | rc = RTFileAioReqPrepareFlush(hReq, pEndpoint->hFile, pCurr);
|
---|
999 | if (RT_FAILURE(rc))
|
---|
1000 | {
|
---|
1001 | pEndpoint->fAsyncFlushSupported = false;
|
---|
1002 | pdmacFileAioMgrNormalRequestFree(pAioMgr, hReq);
|
---|
1003 | rc = VINF_SUCCESS; /* Fake success */
|
---|
1004 | }
|
---|
1005 | else
|
---|
1006 | {
|
---|
1007 | pCurr->hReq = hReq;
|
---|
1008 | apReqs[cRequests] = hReq;
|
---|
1009 | pEndpoint->AioMgr.cReqsProcessed++;
|
---|
1010 | cRequests++;
|
---|
1011 | }
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | if ( !pEndpoint->AioMgr.cRequestsActive
|
---|
1015 | && !pEndpoint->fAsyncFlushSupported)
|
---|
1016 | {
|
---|
1017 | pCurr->pfnCompleted(pCurr, pCurr->pvUser, VINF_SUCCESS);
|
---|
1018 | pdmacFileTaskFree(pEndpoint, pCurr);
|
---|
1019 | }
|
---|
1020 | else
|
---|
1021 | {
|
---|
1022 | Assert(!pEndpoint->pFlushReq);
|
---|
1023 | pEndpoint->pFlushReq = pCurr;
|
---|
1024 | }
|
---|
1025 | break;
|
---|
1026 | }
|
---|
1027 | case PDMACTASKFILETRANSFER_READ:
|
---|
1028 | case PDMACTASKFILETRANSFER_WRITE:
|
---|
1029 | {
|
---|
1030 | RTFILEAIOREQ hReq = NIL_RTFILEAIOREQ;
|
---|
1031 |
|
---|
1032 | if (pCurr->hReq == NIL_RTFILEAIOREQ)
|
---|
1033 | {
|
---|
1034 | if (pEndpoint->enmBackendType == PDMACFILEEPBACKEND_BUFFERED)
|
---|
1035 | rc = pdmacFileAioMgrNormalTaskPrepareBuffered(pAioMgr, pEndpoint, pCurr, &hReq);
|
---|
1036 | else if (pEndpoint->enmBackendType == PDMACFILEEPBACKEND_NON_BUFFERED)
|
---|
1037 | rc = pdmacFileAioMgrNormalTaskPrepareNonBuffered(pAioMgr, pEndpoint, pCurr, &hReq);
|
---|
1038 | else
|
---|
1039 | AssertMsgFailed(("Invalid backend type %d\n", pEndpoint->enmBackendType));
|
---|
1040 |
|
---|
1041 | AssertRC(rc);
|
---|
1042 | }
|
---|
1043 | else
|
---|
1044 | {
|
---|
1045 | LogFlow(("Task %#p has I/O request %#p already\n", pCurr, pCurr->hReq));
|
---|
1046 | hReq = pCurr->hReq;
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | LogFlow(("Read/Write request %#p\n", hReq));
|
---|
1050 |
|
---|
1051 | if (hReq != NIL_RTFILEAIOREQ)
|
---|
1052 | {
|
---|
1053 | apReqs[cRequests] = hReq;
|
---|
1054 | cRequests++;
|
---|
1055 | }
|
---|
1056 | break;
|
---|
1057 | }
|
---|
1058 | default:
|
---|
1059 | AssertMsgFailed(("Invalid transfer type %d\n", pCurr->enmTransferType));
|
---|
1060 | } /* switch transfer type */
|
---|
1061 |
|
---|
1062 | /* Queue the requests if the array is full. */
|
---|
1063 | if (cRequests == RT_ELEMENTS(apReqs))
|
---|
1064 | {
|
---|
1065 | rc = pdmacFileAioMgrNormalReqsEnqueue(pAioMgr, pEndpoint, apReqs, cRequests);
|
---|
1066 | cRequests = 0;
|
---|
1067 | AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
|
---|
1068 | ("Unexpected return code\n"));
|
---|
1069 | }
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 | if (cRequests)
|
---|
1073 | {
|
---|
1074 | rc = pdmacFileAioMgrNormalReqsEnqueue(pAioMgr, pEndpoint, apReqs, cRequests);
|
---|
1075 | AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
|
---|
1076 | ("Unexpected return code rc=%Rrc\n", rc));
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | if (pTaskHead)
|
---|
1080 | {
|
---|
1081 | /* Add the rest of the tasks to the pending list */
|
---|
1082 | pdmacFileAioMgrEpAddTaskList(pEndpoint, pTaskHead);
|
---|
1083 |
|
---|
1084 | if (RT_UNLIKELY( pAioMgr->cRequestsActiveMax == pAioMgr->cRequestsActive
|
---|
1085 | && !pEndpoint->pFlushReq))
|
---|
1086 | {
|
---|
1087 | #if 0
|
---|
1088 | /*
|
---|
1089 | * The I/O manager has no room left for more requests
|
---|
1090 | * but there are still requests to process.
|
---|
1091 | * Create a new I/O manager and let it handle some endpoints.
|
---|
1092 | */
|
---|
1093 | pdmacFileAioMgrNormalBalanceLoad(pAioMgr);
|
---|
1094 | #else
|
---|
1095 | /* Grow the I/O manager */
|
---|
1096 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_GROWING;
|
---|
1097 | #endif
|
---|
1098 | }
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | /* Insufficient resources are not fatal. */
|
---|
1102 | if (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES)
|
---|
1103 | rc = VINF_SUCCESS;
|
---|
1104 |
|
---|
1105 | return rc;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | /**
|
---|
1109 | * Adds all pending requests for the given endpoint
|
---|
1110 | * until a flush request is encountered or there is no
|
---|
1111 | * request anymore.
|
---|
1112 | *
|
---|
1113 | * @returns VBox status code.
|
---|
1114 | * @param pAioMgr The async I/O manager for the endpoint
|
---|
1115 | * @param pEndpoint The endpoint to get the requests from.
|
---|
1116 | */
|
---|
1117 | static int pdmacFileAioMgrNormalQueueReqs(PPDMACEPFILEMGR pAioMgr,
|
---|
1118 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint)
|
---|
1119 | {
|
---|
1120 | int rc = VINF_SUCCESS;
|
---|
1121 | PPDMACTASKFILE pTasksHead = NULL;
|
---|
1122 |
|
---|
1123 | AssertMsg(pEndpoint->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE,
|
---|
1124 | ("Trying to process request lists of a non active endpoint!\n"));
|
---|
1125 |
|
---|
1126 | Assert(!pEndpoint->pFlushReq);
|
---|
1127 |
|
---|
1128 | /* Check the pending list first */
|
---|
1129 | if (pEndpoint->AioMgr.pReqsPendingHead)
|
---|
1130 | {
|
---|
1131 | LogFlow(("Queuing pending requests first\n"));
|
---|
1132 |
|
---|
1133 | pTasksHead = pEndpoint->AioMgr.pReqsPendingHead;
|
---|
1134 | /*
|
---|
1135 | * Clear the list as the processing routine will insert them into the list
|
---|
1136 | * again if it gets a flush request.
|
---|
1137 | */
|
---|
1138 | pEndpoint->AioMgr.pReqsPendingHead = NULL;
|
---|
1139 | pEndpoint->AioMgr.pReqsPendingTail = NULL;
|
---|
1140 | rc = pdmacFileAioMgrNormalProcessTaskList(pTasksHead, pAioMgr, pEndpoint);
|
---|
1141 | AssertRC(rc); /** @todo r=bird: status code potentially overwritten. */
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | if (!pEndpoint->pFlushReq && !pEndpoint->AioMgr.pReqsPendingHead)
|
---|
1145 | {
|
---|
1146 | /* Now the request queue. */
|
---|
1147 | pTasksHead = pdmacFileEpGetNewTasks(pEndpoint);
|
---|
1148 | if (pTasksHead)
|
---|
1149 | {
|
---|
1150 | rc = pdmacFileAioMgrNormalProcessTaskList(pTasksHead, pAioMgr, pEndpoint);
|
---|
1151 | AssertRC(rc);
|
---|
1152 | }
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | return rc;
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | static int pdmacFileAioMgrNormalProcessBlockingEvent(PPDMACEPFILEMGR pAioMgr)
|
---|
1159 | {
|
---|
1160 | int rc = VINF_SUCCESS;
|
---|
1161 | bool fNotifyWaiter = false;
|
---|
1162 |
|
---|
1163 | LogFlowFunc((": Enter\n"));
|
---|
1164 |
|
---|
1165 | Assert(pAioMgr->fBlockingEventPending);
|
---|
1166 |
|
---|
1167 | switch (pAioMgr->enmBlockingEvent)
|
---|
1168 | {
|
---|
1169 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_ADD_ENDPOINT:
|
---|
1170 | {
|
---|
1171 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointNew = ASMAtomicReadPtrT(&pAioMgr->BlockingEventData.AddEndpoint.pEndpoint, PPDMASYNCCOMPLETIONENDPOINTFILE);
|
---|
1172 | AssertMsg(VALID_PTR(pEndpointNew), ("Adding endpoint event without a endpoint to add\n"));
|
---|
1173 |
|
---|
1174 | pEndpointNew->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE;
|
---|
1175 |
|
---|
1176 | pEndpointNew->AioMgr.pEndpointNext = pAioMgr->pEndpointsHead;
|
---|
1177 | pEndpointNew->AioMgr.pEndpointPrev = NULL;
|
---|
1178 | if (pAioMgr->pEndpointsHead)
|
---|
1179 | pAioMgr->pEndpointsHead->AioMgr.pEndpointPrev = pEndpointNew;
|
---|
1180 | pAioMgr->pEndpointsHead = pEndpointNew;
|
---|
1181 |
|
---|
1182 | /* Assign the completion point to this file. */
|
---|
1183 | rc = RTFileAioCtxAssociateWithFile(pAioMgr->hAioCtx, pEndpointNew->hFile);
|
---|
1184 | fNotifyWaiter = true;
|
---|
1185 | pAioMgr->cEndpoints++;
|
---|
1186 | break;
|
---|
1187 | }
|
---|
1188 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_REMOVE_ENDPOINT:
|
---|
1189 | {
|
---|
1190 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointRemove = ASMAtomicReadPtrT(&pAioMgr->BlockingEventData.RemoveEndpoint.pEndpoint, PPDMASYNCCOMPLETIONENDPOINTFILE);
|
---|
1191 | AssertMsg(VALID_PTR(pEndpointRemove), ("Removing endpoint event without a endpoint to remove\n"));
|
---|
1192 |
|
---|
1193 | pEndpointRemove->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_REMOVING;
|
---|
1194 | fNotifyWaiter = !pdmacFileAioMgrNormalRemoveEndpoint(pEndpointRemove);
|
---|
1195 | break;
|
---|
1196 | }
|
---|
1197 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_CLOSE_ENDPOINT:
|
---|
1198 | {
|
---|
1199 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointClose = ASMAtomicReadPtrT(&pAioMgr->BlockingEventData.CloseEndpoint.pEndpoint, PPDMASYNCCOMPLETIONENDPOINTFILE);
|
---|
1200 | AssertMsg(VALID_PTR(pEndpointClose), ("Close endpoint event without a endpoint to close\n"));
|
---|
1201 |
|
---|
1202 | if (pEndpointClose->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE)
|
---|
1203 | {
|
---|
1204 | LogFlowFunc((": Closing endpoint %#p{%s}\n", pEndpointClose, pEndpointClose->Core.pszUri));
|
---|
1205 |
|
---|
1206 | /* Make sure all tasks finished. Process the queues a last time first. */
|
---|
1207 | rc = pdmacFileAioMgrNormalQueueReqs(pAioMgr, pEndpointClose);
|
---|
1208 | AssertRC(rc);
|
---|
1209 |
|
---|
1210 | pEndpointClose->enmState = PDMASYNCCOMPLETIONENDPOINTFILESTATE_CLOSING;
|
---|
1211 | fNotifyWaiter = !pdmacFileAioMgrNormalRemoveEndpoint(pEndpointClose);
|
---|
1212 | }
|
---|
1213 | else if ( (pEndpointClose->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_CLOSING)
|
---|
1214 | && (!pEndpointClose->AioMgr.cRequestsActive))
|
---|
1215 | fNotifyWaiter = true;
|
---|
1216 | break;
|
---|
1217 | }
|
---|
1218 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_SHUTDOWN:
|
---|
1219 | {
|
---|
1220 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_SHUTDOWN;
|
---|
1221 | if (!pAioMgr->cRequestsActive)
|
---|
1222 | fNotifyWaiter = true;
|
---|
1223 | break;
|
---|
1224 | }
|
---|
1225 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_SUSPEND:
|
---|
1226 | {
|
---|
1227 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_SUSPENDING;
|
---|
1228 | break;
|
---|
1229 | }
|
---|
1230 | case PDMACEPFILEAIOMGRBLOCKINGEVENT_RESUME:
|
---|
1231 | {
|
---|
1232 | pAioMgr->enmState = PDMACEPFILEMGRSTATE_RUNNING;
|
---|
1233 | fNotifyWaiter = true;
|
---|
1234 | break;
|
---|
1235 | }
|
---|
1236 | default:
|
---|
1237 | AssertReleaseMsgFailed(("Invalid event type %d\n", pAioMgr->enmBlockingEvent));
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | if (fNotifyWaiter)
|
---|
1241 | {
|
---|
1242 | ASMAtomicWriteBool(&pAioMgr->fBlockingEventPending, false);
|
---|
1243 | pAioMgr->enmBlockingEvent = PDMACEPFILEAIOMGRBLOCKINGEVENT_INVALID;
|
---|
1244 |
|
---|
1245 | /* Release the waiting thread. */
|
---|
1246 | LogFlow(("Signalling waiter\n"));
|
---|
1247 | rc = RTSemEventSignal(pAioMgr->EventSemBlock);
|
---|
1248 | AssertRC(rc);
|
---|
1249 | }
|
---|
1250 |
|
---|
1251 | LogFlowFunc((": Leave\n"));
|
---|
1252 | return rc;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | /**
|
---|
1256 | * Checks all endpoints for pending events or new requests.
|
---|
1257 | *
|
---|
1258 | * @returns VBox status code.
|
---|
1259 | * @param pAioMgr The I/O manager handle.
|
---|
1260 | */
|
---|
1261 | static int pdmacFileAioMgrNormalCheckEndpoints(PPDMACEPFILEMGR pAioMgr)
|
---|
1262 | {
|
---|
1263 | /* Check the assigned endpoints for new tasks if there isn't a flush request active at the moment. */
|
---|
1264 | int rc = VINF_SUCCESS;
|
---|
1265 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint = pAioMgr->pEndpointsHead;
|
---|
1266 |
|
---|
1267 | pAioMgr->msBwLimitExpired = RT_INDEFINITE_WAIT;
|
---|
1268 |
|
---|
1269 | while (pEndpoint)
|
---|
1270 | {
|
---|
1271 | if (!pEndpoint->pFlushReq
|
---|
1272 | && (pEndpoint->enmState == PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE)
|
---|
1273 | && !pEndpoint->AioMgr.fMoving)
|
---|
1274 | {
|
---|
1275 | rc = pdmacFileAioMgrNormalQueueReqs(pAioMgr, pEndpoint);
|
---|
1276 | if (RT_FAILURE(rc))
|
---|
1277 | return rc;
|
---|
1278 | }
|
---|
1279 | else if ( !pEndpoint->AioMgr.cRequestsActive
|
---|
1280 | && pEndpoint->enmState != PDMASYNCCOMPLETIONENDPOINTFILESTATE_ACTIVE)
|
---|
1281 | {
|
---|
1282 | /* Reopen the file so that the new endpoint can re-associate with the file */
|
---|
1283 | RTFileClose(pEndpoint->hFile);
|
---|
1284 | rc = RTFileOpen(&pEndpoint->hFile, pEndpoint->Core.pszUri, pEndpoint->fFlags);
|
---|
1285 | AssertRC(rc);
|
---|
1286 |
|
---|
1287 | if (pEndpoint->AioMgr.fMoving)
|
---|
1288 | {
|
---|
1289 | pEndpoint->AioMgr.fMoving = false;
|
---|
1290 | pdmacFileAioMgrAddEndpoint(pEndpoint->AioMgr.pAioMgrDst, pEndpoint);
|
---|
1291 | }
|
---|
1292 | else
|
---|
1293 | {
|
---|
1294 | Assert(pAioMgr->fBlockingEventPending);
|
---|
1295 | ASMAtomicWriteBool(&pAioMgr->fBlockingEventPending, false);
|
---|
1296 |
|
---|
1297 | /* Release the waiting thread. */
|
---|
1298 | LogFlow(("Signalling waiter\n"));
|
---|
1299 | rc = RTSemEventSignal(pAioMgr->EventSemBlock);
|
---|
1300 | AssertRC(rc);
|
---|
1301 | }
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | pEndpoint = pEndpoint->AioMgr.pEndpointNext;
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | return rc;
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | /**
|
---|
1311 | * Wrapper around pdmacFileAioMgrNormalReqCompleteRc().
|
---|
1312 | */
|
---|
1313 | static void pdmacFileAioMgrNormalReqComplete(PPDMACEPFILEMGR pAioMgr, RTFILEAIOREQ hReq)
|
---|
1314 | {
|
---|
1315 | size_t cbTransfered = 0;
|
---|
1316 | int rcReq = RTFileAioReqGetRC(hReq, &cbTransfered);
|
---|
1317 |
|
---|
1318 | pdmacFileAioMgrNormalReqCompleteRc(pAioMgr, hReq, rcReq, cbTransfered);
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | static void pdmacFileAioMgrNormalReqCompleteRc(PPDMACEPFILEMGR pAioMgr, RTFILEAIOREQ hReq,
|
---|
1322 | int rcReq, size_t cbTransfered)
|
---|
1323 | {
|
---|
1324 | int rc = VINF_SUCCESS;
|
---|
1325 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpoint;
|
---|
1326 | PPDMACTASKFILE pTask = (PPDMACTASKFILE)RTFileAioReqGetUser(hReq);
|
---|
1327 | PPDMACTASKFILE pTasksWaiting;
|
---|
1328 |
|
---|
1329 | LogFlowFunc(("pAioMgr=%#p hReq=%#p\n", pAioMgr, hReq));
|
---|
1330 |
|
---|
1331 | pEndpoint = pTask->pEndpoint;
|
---|
1332 |
|
---|
1333 | pTask->hReq = NIL_RTFILEAIOREQ;
|
---|
1334 |
|
---|
1335 | pAioMgr->cRequestsActive--;
|
---|
1336 | pEndpoint->AioMgr.cRequestsActive--;
|
---|
1337 | pEndpoint->AioMgr.cReqsProcessed++;
|
---|
1338 |
|
---|
1339 | /*
|
---|
1340 | * It is possible that the request failed on Linux with kernels < 2.6.23
|
---|
1341 | * if the passed buffer was allocated with remap_pfn_range or if the file
|
---|
1342 | * is on an NFS endpoint which does not support async and direct I/O at the same time.
|
---|
1343 | * The endpoint will be migrated to a failsafe manager in case a request fails.
|
---|
1344 | */
|
---|
1345 | if (RT_FAILURE(rcReq))
|
---|
1346 | {
|
---|
1347 | /* Free bounce buffers and the IPRT request. */
|
---|
1348 | pdmacFileAioMgrNormalRequestFree(pAioMgr, hReq);
|
---|
1349 |
|
---|
1350 | if (pTask->enmTransferType == PDMACTASKFILETRANSFER_FLUSH)
|
---|
1351 | {
|
---|
1352 | LogFlow(("Async flushes are not supported for this endpoint, disabling\n"));
|
---|
1353 | pEndpoint->fAsyncFlushSupported = false;
|
---|
1354 | AssertMsg(pEndpoint->pFlushReq == pTask, ("Failed flush request doesn't match active one\n"));
|
---|
1355 | /* The other method will take over now. */
|
---|
1356 |
|
---|
1357 | pEndpoint->pFlushReq = NULL;
|
---|
1358 | /* Call completion callback */
|
---|
1359 | LogFlow(("Flush task=%#p completed with %Rrc\n", pTask, VINF_SUCCESS));
|
---|
1360 | pTask->pfnCompleted(pTask, pTask->pvUser, VINF_SUCCESS);
|
---|
1361 | pdmacFileTaskFree(pEndpoint, pTask);
|
---|
1362 | }
|
---|
1363 | else
|
---|
1364 | {
|
---|
1365 | /* Free the lock and process pending tasks if necessary */
|
---|
1366 | pTasksWaiting = pdmacFileAioMgrNormalRangeLockFree(pAioMgr, pEndpoint, pTask->pRangeLock);
|
---|
1367 | rc = pdmacFileAioMgrNormalProcessTaskList(pTasksWaiting, pAioMgr, pEndpoint);
|
---|
1368 | AssertRC(rc);
|
---|
1369 |
|
---|
1370 | if (pTask->cbBounceBuffer)
|
---|
1371 | RTMemPageFree(pTask->pvBounceBuffer, pTask->cbBounceBuffer);
|
---|
1372 |
|
---|
1373 | /*
|
---|
1374 | * Fatal errors are reported to the guest and non-fatal errors
|
---|
1375 | * will cause a migration to the failsafe manager in the hope
|
---|
1376 | * that the error disappears.
|
---|
1377 | */
|
---|
1378 | if (!pdmacFileAioMgrNormalRcIsFatal(rcReq))
|
---|
1379 | {
|
---|
1380 | /* Queue the request on the pending list. */
|
---|
1381 | pTask->pNext = pEndpoint->AioMgr.pReqsPendingHead;
|
---|
1382 | pEndpoint->AioMgr.pReqsPendingHead = pTask;
|
---|
1383 |
|
---|
1384 | /* Create a new failsafe manager if necessary. */
|
---|
1385 | if (!pEndpoint->AioMgr.fMoving)
|
---|
1386 | {
|
---|
1387 | PPDMACEPFILEMGR pAioMgrFailsafe;
|
---|
1388 |
|
---|
1389 | LogRel(("%s: Request %#p failed with rc=%Rrc, migrating endpoint %s to failsafe manager.\n",
|
---|
1390 | RTThreadGetName(pAioMgr->Thread), pTask, rcReq, pEndpoint->Core.pszUri));
|
---|
1391 |
|
---|
1392 | pEndpoint->AioMgr.fMoving = true;
|
---|
1393 |
|
---|
1394 | rc = pdmacFileAioMgrCreate((PPDMASYNCCOMPLETIONEPCLASSFILE)pEndpoint->Core.pEpClass,
|
---|
1395 | &pAioMgrFailsafe, PDMACEPFILEMGRTYPE_SIMPLE);
|
---|
1396 | AssertRC(rc);
|
---|
1397 |
|
---|
1398 | pEndpoint->AioMgr.pAioMgrDst = pAioMgrFailsafe;
|
---|
1399 |
|
---|
1400 | /* Update the flags to open the file with. Disable async I/O and enable the host cache. */
|
---|
1401 | pEndpoint->fFlags &= ~(RTFILE_O_ASYNC_IO | RTFILE_O_NO_CACHE);
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 | /* If this was the last request for the endpoint migrate it to the new manager. */
|
---|
1405 | if (!pEndpoint->AioMgr.cRequestsActive)
|
---|
1406 | {
|
---|
1407 | bool fReqsPending = pdmacFileAioMgrNormalRemoveEndpoint(pEndpoint);
|
---|
1408 | Assert(!fReqsPending);
|
---|
1409 |
|
---|
1410 | rc = pdmacFileAioMgrAddEndpoint(pEndpoint->AioMgr.pAioMgrDst, pEndpoint);
|
---|
1411 | AssertRC(rc);
|
---|
1412 | }
|
---|
1413 | }
|
---|
1414 | else
|
---|
1415 | {
|
---|
1416 | pTask->pfnCompleted(pTask, pTask->pvUser, rcReq);
|
---|
1417 | pdmacFileTaskFree(pEndpoint, pTask);
|
---|
1418 | }
|
---|
1419 | }
|
---|
1420 | }
|
---|
1421 | else
|
---|
1422 | {
|
---|
1423 | if (pTask->enmTransferType == PDMACTASKFILETRANSFER_FLUSH)
|
---|
1424 | {
|
---|
1425 | /* Clear pending flush */
|
---|
1426 | AssertMsg(pEndpoint->pFlushReq == pTask, ("Completed flush request doesn't match active one\n"));
|
---|
1427 | pEndpoint->pFlushReq = NULL;
|
---|
1428 | pdmacFileAioMgrNormalRequestFree(pAioMgr, hReq);
|
---|
1429 |
|
---|
1430 | /* Call completion callback */
|
---|
1431 | LogFlow(("Flush task=%#p completed with %Rrc\n", pTask, rcReq));
|
---|
1432 | pTask->pfnCompleted(pTask, pTask->pvUser, rcReq);
|
---|
1433 | pdmacFileTaskFree(pEndpoint, pTask);
|
---|
1434 | }
|
---|
1435 | else
|
---|
1436 | {
|
---|
1437 | /*
|
---|
1438 | * Restart an incomplete transfer.
|
---|
1439 | * This usually means that the request will return an error now
|
---|
1440 | * but to get the cause of the error (disk full, file too big, I/O error, ...)
|
---|
1441 | * the transfer needs to be continued.
|
---|
1442 | */
|
---|
1443 | if (RT_UNLIKELY( cbTransfered < pTask->DataSeg.cbSeg
|
---|
1444 | || ( pTask->cbBounceBuffer
|
---|
1445 | && cbTransfered < pTask->cbBounceBuffer)))
|
---|
1446 | {
|
---|
1447 | RTFOFF offStart;
|
---|
1448 | size_t cbToTransfer;
|
---|
1449 | uint8_t *pbBuf = NULL;
|
---|
1450 |
|
---|
1451 | LogFlow(("Restarting incomplete transfer %#p (%zu bytes transferred)\n",
|
---|
1452 | pTask, cbTransfered));
|
---|
1453 | Assert(cbTransfered % 512 == 0);
|
---|
1454 |
|
---|
1455 | if (pTask->cbBounceBuffer)
|
---|
1456 | {
|
---|
1457 | AssertPtr(pTask->pvBounceBuffer);
|
---|
1458 | offStart = (pTask->Off & ~((RTFOFF)512-1)) + cbTransfered;
|
---|
1459 | cbToTransfer = pTask->cbBounceBuffer - cbTransfered;
|
---|
1460 | pbBuf = (uint8_t *)pTask->pvBounceBuffer + cbTransfered;
|
---|
1461 | }
|
---|
1462 | else
|
---|
1463 | {
|
---|
1464 | Assert(!pTask->pvBounceBuffer);
|
---|
1465 | offStart = pTask->Off + cbTransfered;
|
---|
1466 | cbToTransfer = pTask->DataSeg.cbSeg - cbTransfered;
|
---|
1467 | pbBuf = (uint8_t *)pTask->DataSeg.pvSeg + cbTransfered;
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | if (pTask->fPrefetch || pTask->enmTransferType == PDMACTASKFILETRANSFER_READ)
|
---|
1471 | {
|
---|
1472 | rc = RTFileAioReqPrepareRead(hReq, pEndpoint->hFile, offStart,
|
---|
1473 | pbBuf, cbToTransfer, pTask);
|
---|
1474 | }
|
---|
1475 | else
|
---|
1476 | {
|
---|
1477 | AssertMsg(pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE,
|
---|
1478 | ("Invalid transfer type\n"));
|
---|
1479 | rc = RTFileAioReqPrepareWrite(hReq, pEndpoint->hFile, offStart,
|
---|
1480 | pbBuf, cbToTransfer, pTask);
|
---|
1481 | }
|
---|
1482 | AssertRC(rc);
|
---|
1483 |
|
---|
1484 | pTask->hReq = hReq;
|
---|
1485 | rc = pdmacFileAioMgrNormalReqsEnqueue(pAioMgr, pEndpoint, &hReq, 1);
|
---|
1486 | AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
|
---|
1487 | ("Unexpected return code rc=%Rrc\n", rc));
|
---|
1488 | }
|
---|
1489 | else if (pTask->fPrefetch)
|
---|
1490 | {
|
---|
1491 | Assert(pTask->enmTransferType == PDMACTASKFILETRANSFER_WRITE);
|
---|
1492 | Assert(pTask->cbBounceBuffer);
|
---|
1493 |
|
---|
1494 | memcpy(((uint8_t *)pTask->pvBounceBuffer) + pTask->offBounceBuffer,
|
---|
1495 | pTask->DataSeg.pvSeg,
|
---|
1496 | pTask->DataSeg.cbSeg);
|
---|
1497 |
|
---|
1498 | /* Write it now. */
|
---|
1499 | pTask->fPrefetch = false;
|
---|
1500 | size_t cbToTransfer = RT_ALIGN_Z(pTask->DataSeg.cbSeg, 512);
|
---|
1501 | RTFOFF offStart = pTask->Off & ~(RTFOFF)(512-1);
|
---|
1502 |
|
---|
1503 | /* Grow the file if needed. */
|
---|
1504 | if (RT_UNLIKELY((uint64_t)(pTask->Off + pTask->DataSeg.cbSeg) > pEndpoint->cbFile))
|
---|
1505 | {
|
---|
1506 | ASMAtomicWriteU64(&pEndpoint->cbFile, pTask->Off + pTask->DataSeg.cbSeg);
|
---|
1507 | RTFileSetSize(pEndpoint->hFile, pTask->Off + pTask->DataSeg.cbSeg);
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | rc = RTFileAioReqPrepareWrite(hReq, pEndpoint->hFile,
|
---|
1511 | offStart, pTask->pvBounceBuffer, cbToTransfer, pTask);
|
---|
1512 | AssertRC(rc);
|
---|
1513 | pTask->hReq = hReq;
|
---|
1514 | rc = pdmacFileAioMgrNormalReqsEnqueue(pAioMgr, pEndpoint, &hReq, 1);
|
---|
1515 | AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
|
---|
1516 | ("Unexpected return code rc=%Rrc\n", rc));
|
---|
1517 | }
|
---|
1518 | else
|
---|
1519 | {
|
---|
1520 | if (RT_SUCCESS(rc) && pTask->cbBounceBuffer)
|
---|
1521 | {
|
---|
1522 | if (pTask->enmTransferType == PDMACTASKFILETRANSFER_READ)
|
---|
1523 | memcpy(pTask->DataSeg.pvSeg,
|
---|
1524 | ((uint8_t *)pTask->pvBounceBuffer) + pTask->offBounceBuffer,
|
---|
1525 | pTask->DataSeg.cbSeg);
|
---|
1526 |
|
---|
1527 | RTMemPageFree(pTask->pvBounceBuffer, pTask->cbBounceBuffer);
|
---|
1528 | }
|
---|
1529 |
|
---|
1530 | pdmacFileAioMgrNormalRequestFree(pAioMgr, hReq);
|
---|
1531 |
|
---|
1532 | /* Free the lock and process pending tasks if necessary */
|
---|
1533 | pTasksWaiting = pdmacFileAioMgrNormalRangeLockFree(pAioMgr, pEndpoint, pTask->pRangeLock);
|
---|
1534 | if (pTasksWaiting)
|
---|
1535 | {
|
---|
1536 | rc = pdmacFileAioMgrNormalProcessTaskList(pTasksWaiting, pAioMgr, pEndpoint);
|
---|
1537 | AssertRC(rc);
|
---|
1538 | }
|
---|
1539 |
|
---|
1540 | /* Call completion callback */
|
---|
1541 | LogFlow(("Task=%#p completed with %Rrc\n", pTask, rcReq));
|
---|
1542 | pTask->pfnCompleted(pTask, pTask->pvUser, rcReq);
|
---|
1543 | pdmacFileTaskFree(pEndpoint, pTask);
|
---|
1544 |
|
---|
1545 | /*
|
---|
1546 | * If there is no request left on the endpoint but a flush request is set
|
---|
1547 | * it completed now and we notify the owner.
|
---|
1548 | * Furthermore we look for new requests and continue.
|
---|
1549 | */
|
---|
1550 | if (!pEndpoint->AioMgr.cRequestsActive && pEndpoint->pFlushReq)
|
---|
1551 | {
|
---|
1552 | /* Call completion callback */
|
---|
1553 | pTask = pEndpoint->pFlushReq;
|
---|
1554 | pEndpoint->pFlushReq = NULL;
|
---|
1555 |
|
---|
1556 | AssertMsg(pTask->pEndpoint == pEndpoint, ("Endpoint of the flush request does not match assigned one\n"));
|
---|
1557 |
|
---|
1558 | pTask->pfnCompleted(pTask, pTask->pvUser, VINF_SUCCESS);
|
---|
1559 | pdmacFileTaskFree(pEndpoint, pTask);
|
---|
1560 | }
|
---|
1561 | else if (RT_UNLIKELY(!pEndpoint->AioMgr.cRequestsActive && pEndpoint->AioMgr.fMoving))
|
---|
1562 | {
|
---|
1563 | /* If the endpoint is about to be migrated do it now. */
|
---|
1564 | bool fReqsPending = pdmacFileAioMgrNormalRemoveEndpoint(pEndpoint);
|
---|
1565 | Assert(!fReqsPending);
|
---|
1566 |
|
---|
1567 | rc = pdmacFileAioMgrAddEndpoint(pEndpoint->AioMgr.pAioMgrDst, pEndpoint);
|
---|
1568 | AssertRC(rc);
|
---|
1569 | }
|
---|
1570 | }
|
---|
1571 | } /* Not a flush request */
|
---|
1572 | } /* request completed successfully */
|
---|
1573 | }
|
---|
1574 |
|
---|
1575 | /** Helper macro for checking for error codes. */
|
---|
1576 | #define CHECK_RC(pAioMgr, rc) \
|
---|
1577 | if (RT_FAILURE(rc)) \
|
---|
1578 | {\
|
---|
1579 | int rc2 = pdmacFileAioMgrNormalErrorHandler(pAioMgr, rc, RT_SRC_POS);\
|
---|
1580 | return rc2;\
|
---|
1581 | }
|
---|
1582 |
|
---|
1583 | /**
|
---|
1584 | * The normal I/O manager using the RTFileAio* API
|
---|
1585 | *
|
---|
1586 | * @returns VBox status code.
|
---|
1587 | * @param hThreadSelf Handle of the thread.
|
---|
1588 | * @param pvUser Opaque user data.
|
---|
1589 | */
|
---|
1590 | DECLCALLBACK(int) pdmacFileAioMgrNormal(RTTHREAD hThreadSelf, void *pvUser)
|
---|
1591 | {
|
---|
1592 | int rc = VINF_SUCCESS;
|
---|
1593 | PPDMACEPFILEMGR pAioMgr = (PPDMACEPFILEMGR)pvUser;
|
---|
1594 | uint64_t uMillisEnd = RTTimeMilliTS() + PDMACEPFILEMGR_LOAD_UPDATE_PERIOD;
|
---|
1595 | NOREF(hThreadSelf);
|
---|
1596 |
|
---|
1597 | while ( pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING
|
---|
1598 | || pAioMgr->enmState == PDMACEPFILEMGRSTATE_SUSPENDING
|
---|
1599 | || pAioMgr->enmState == PDMACEPFILEMGRSTATE_GROWING)
|
---|
1600 | {
|
---|
1601 | if (!pAioMgr->cRequestsActive)
|
---|
1602 | {
|
---|
1603 | ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, true);
|
---|
1604 | if (!ASMAtomicReadBool(&pAioMgr->fWokenUp))
|
---|
1605 | rc = RTSemEventWait(pAioMgr->EventSem, pAioMgr->msBwLimitExpired);
|
---|
1606 | ASMAtomicWriteBool(&pAioMgr->fWaitingEventSem, false);
|
---|
1607 | Assert(RT_SUCCESS(rc) || rc == VERR_TIMEOUT);
|
---|
1608 |
|
---|
1609 | LogFlow(("Got woken up\n"));
|
---|
1610 | ASMAtomicWriteBool(&pAioMgr->fWokenUp, false);
|
---|
1611 | }
|
---|
1612 |
|
---|
1613 | /* Check for an external blocking event first. */
|
---|
1614 | if (pAioMgr->fBlockingEventPending)
|
---|
1615 | {
|
---|
1616 | rc = pdmacFileAioMgrNormalProcessBlockingEvent(pAioMgr);
|
---|
1617 | CHECK_RC(pAioMgr, rc);
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 | if (RT_LIKELY( pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING
|
---|
1621 | || pAioMgr->enmState == PDMACEPFILEMGRSTATE_GROWING))
|
---|
1622 | {
|
---|
1623 | /* We got woken up because an endpoint issued new requests. Queue them. */
|
---|
1624 | rc = pdmacFileAioMgrNormalCheckEndpoints(pAioMgr);
|
---|
1625 | CHECK_RC(pAioMgr, rc);
|
---|
1626 |
|
---|
1627 | while (pAioMgr->cRequestsActive)
|
---|
1628 | {
|
---|
1629 | RTFILEAIOREQ apReqs[20];
|
---|
1630 | uint32_t cReqsCompleted = 0;
|
---|
1631 | size_t cReqsWait;
|
---|
1632 |
|
---|
1633 | if (pAioMgr->cRequestsActive > RT_ELEMENTS(apReqs))
|
---|
1634 | cReqsWait = RT_ELEMENTS(apReqs);
|
---|
1635 | else
|
---|
1636 | cReqsWait = pAioMgr->cRequestsActive;
|
---|
1637 |
|
---|
1638 | LogFlow(("Waiting for %d of %d tasks to complete\n", 1, cReqsWait));
|
---|
1639 |
|
---|
1640 | rc = RTFileAioCtxWait(pAioMgr->hAioCtx,
|
---|
1641 | 1,
|
---|
1642 | RT_INDEFINITE_WAIT, apReqs,
|
---|
1643 | cReqsWait, &cReqsCompleted);
|
---|
1644 | if (RT_FAILURE(rc) && (rc != VERR_INTERRUPTED))
|
---|
1645 | CHECK_RC(pAioMgr, rc);
|
---|
1646 |
|
---|
1647 | LogFlow(("%d tasks completed\n", cReqsCompleted));
|
---|
1648 |
|
---|
1649 | for (uint32_t i = 0; i < cReqsCompleted; i++)
|
---|
1650 | pdmacFileAioMgrNormalReqComplete(pAioMgr, apReqs[i]);
|
---|
1651 |
|
---|
1652 | /* Check for an external blocking event before we go to sleep again. */
|
---|
1653 | if (pAioMgr->fBlockingEventPending)
|
---|
1654 | {
|
---|
1655 | rc = pdmacFileAioMgrNormalProcessBlockingEvent(pAioMgr);
|
---|
1656 | CHECK_RC(pAioMgr, rc);
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | /* Update load statistics. */
|
---|
1660 | uint64_t uMillisCurr = RTTimeMilliTS();
|
---|
1661 | if (uMillisCurr > uMillisEnd)
|
---|
1662 | {
|
---|
1663 | PPDMASYNCCOMPLETIONENDPOINTFILE pEndpointCurr = pAioMgr->pEndpointsHead;
|
---|
1664 |
|
---|
1665 | /* Calculate timespan. */
|
---|
1666 | uMillisCurr -= uMillisEnd;
|
---|
1667 |
|
---|
1668 | while (pEndpointCurr)
|
---|
1669 | {
|
---|
1670 | pEndpointCurr->AioMgr.cReqsPerSec = pEndpointCurr->AioMgr.cReqsProcessed / (uMillisCurr + PDMACEPFILEMGR_LOAD_UPDATE_PERIOD);
|
---|
1671 | pEndpointCurr->AioMgr.cReqsProcessed = 0;
|
---|
1672 | pEndpointCurr = pEndpointCurr->AioMgr.pEndpointNext;
|
---|
1673 | }
|
---|
1674 |
|
---|
1675 | /* Set new update interval */
|
---|
1676 | uMillisEnd = RTTimeMilliTS() + PDMACEPFILEMGR_LOAD_UPDATE_PERIOD;
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | /* Check endpoints for new requests. */
|
---|
1680 | if (pAioMgr->enmState != PDMACEPFILEMGRSTATE_GROWING)
|
---|
1681 | {
|
---|
1682 | rc = pdmacFileAioMgrNormalCheckEndpoints(pAioMgr);
|
---|
1683 | CHECK_RC(pAioMgr, rc);
|
---|
1684 | }
|
---|
1685 | } /* while requests are active. */
|
---|
1686 |
|
---|
1687 | if (pAioMgr->enmState == PDMACEPFILEMGRSTATE_GROWING)
|
---|
1688 | {
|
---|
1689 | rc = pdmacFileAioMgrNormalGrow(pAioMgr);
|
---|
1690 | AssertRC(rc);
|
---|
1691 | Assert(pAioMgr->enmState == PDMACEPFILEMGRSTATE_RUNNING);
|
---|
1692 |
|
---|
1693 | rc = pdmacFileAioMgrNormalCheckEndpoints(pAioMgr);
|
---|
1694 | CHECK_RC(pAioMgr, rc);
|
---|
1695 | }
|
---|
1696 | } /* if still running */
|
---|
1697 | } /* while running */
|
---|
1698 |
|
---|
1699 | LogFlowFunc(("rc=%Rrc\n", rc));
|
---|
1700 | return rc;
|
---|
1701 | }
|
---|
1702 |
|
---|
1703 | #undef CHECK_RC
|
---|
1704 |
|
---|