VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PDMAsyncCompletion.cpp@ 55036

Last change on this file since 55036 was 46493, checked in by vboxsync, 11 years ago

STAMR3Deregister* optimizations. Relevant for both startup and shutdown times.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 65.2 KB
Line 
1/* $Id: PDMAsyncCompletion.cpp 46493 2013-06-11 13:34:40Z vboxsync $ */
2/** @file
3 * PDM Async I/O - Transport data asynchronous in R3 using EMT.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_PDM_ASYNC_COMPLETION
23#include "PDMInternal.h"
24#include <VBox/vmm/pdm.h>
25#include <VBox/vmm/mm.h>
26#ifdef VBOX_WITH_REM
27# include <VBox/vmm/rem.h>
28#endif
29#include <VBox/vmm/vm.h>
30#include <VBox/vmm/uvm.h>
31#include <VBox/err.h>
32
33#include <VBox/log.h>
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/thread.h>
37#include <iprt/mem.h>
38#include <iprt/critsect.h>
39#include <iprt/tcp.h>
40#include <iprt/path.h>
41#include <iprt/string.h>
42
43#include <VBox/vmm/pdmasynccompletion.h>
44#include "PDMAsyncCompletionInternal.h"
45
46
47/*******************************************************************************
48* Structures and Typedefs *
49*******************************************************************************/
50/**
51 * Async I/O type.
52 */
53typedef enum PDMASYNCCOMPLETIONTEMPLATETYPE
54{
55 /** Device . */
56 PDMASYNCCOMPLETIONTEMPLATETYPE_DEV = 1,
57 /** Driver consumer. */
58 PDMASYNCCOMPLETIONTEMPLATETYPE_DRV,
59 /** Internal consumer. */
60 PDMASYNCCOMPLETIONTEMPLATETYPE_INTERNAL,
61 /** Usb consumer. */
62 PDMASYNCCOMPLETIONTEMPLATETYPE_USB
63} PDMASYNCTEMPLATETYPE;
64
65/**
66 * PDM Async I/O template.
67 */
68typedef struct PDMASYNCCOMPLETIONTEMPLATE
69{
70 /** Pointer to the next template in the list. */
71 R3PTRTYPE(PPDMASYNCCOMPLETIONTEMPLATE) pNext;
72 /** Pointer to the previous template in the list. */
73 R3PTRTYPE(PPDMASYNCCOMPLETIONTEMPLATE) pPrev;
74 /** Type specific data. */
75 union
76 {
77 /** PDMASYNCCOMPLETIONTEMPLATETYPE_DEV */
78 struct
79 {
80 /** Pointer to consumer function. */
81 R3PTRTYPE(PFNPDMASYNCCOMPLETEDEV) pfnCompleted;
82 /** Pointer to the device instance owning the template. */
83 R3PTRTYPE(PPDMDEVINS) pDevIns;
84 } Dev;
85 /** PDMASYNCCOMPLETIONTEMPLATETYPE_DRV */
86 struct
87 {
88 /** Pointer to consumer function. */
89 R3PTRTYPE(PFNPDMASYNCCOMPLETEDRV) pfnCompleted;
90 /** Pointer to the driver instance owning the template. */
91 R3PTRTYPE(PPDMDRVINS) pDrvIns;
92 /** User argument given during template creation.
93 * This is only here to make things much easier
94 * for DrVVD. */
95 void *pvTemplateUser;
96 } Drv;
97 /** PDMASYNCCOMPLETIONTEMPLATETYPE_INTERNAL */
98 struct
99 {
100 /** Pointer to consumer function. */
101 R3PTRTYPE(PFNPDMASYNCCOMPLETEINT) pfnCompleted;
102 /** Pointer to user data. */
103 R3PTRTYPE(void *) pvUser;
104 } Int;
105 /** PDMASYNCCOMPLETIONTEMPLATETYPE_USB */
106 struct
107 {
108 /** Pointer to consumer function. */
109 R3PTRTYPE(PFNPDMASYNCCOMPLETEUSB) pfnCompleted;
110 /** Pointer to the usb instance owning the template. */
111 R3PTRTYPE(PPDMUSBINS) pUsbIns;
112 } Usb;
113 } u;
114 /** Template type. */
115 PDMASYNCCOMPLETIONTEMPLATETYPE enmType;
116 /** Pointer to the VM. */
117 R3PTRTYPE(PVM) pVM;
118 /** Use count of the template. */
119 volatile uint32_t cUsed;
120} PDMASYNCCOMPLETIONTEMPLATE;
121
122/**
123 * Bandwidth control manager instance data
124 */
125typedef struct PDMACBWMGR
126{
127 /** Pointer to the next manager in the list. */
128 struct PDMACBWMGR *pNext;
129 /** Pointer to the shared UVM structure. */
130 PPDMASYNCCOMPLETIONEPCLASS pEpClass;
131 /** Identifier of the manager. */
132 char *pszId;
133 /** Maximum number of bytes the endpoints are allowed to transfer (Max is 4GB/s currently) */
134 volatile uint32_t cbTransferPerSecMax;
135 /** Number of bytes we start with */
136 volatile uint32_t cbTransferPerSecStart;
137 /** Step after each update */
138 volatile uint32_t cbTransferPerSecStep;
139 /** Number of bytes we are allowed to transfer till the next update.
140 * Reset by the refresh timer. */
141 volatile uint32_t cbTransferAllowed;
142 /** Timestamp of the last update */
143 volatile uint64_t tsUpdatedLast;
144 /** Reference counter - How many endpoints are associated with this manager. */
145 volatile uint32_t cRefs;
146} PDMACBWMGR;
147/** Pointer to a bandwidth control manager pointer. */
148typedef PPDMACBWMGR *PPPDMACBWMGR;
149
150
151/*******************************************************************************
152* Internal Functions *
153*******************************************************************************/
154static void pdmR3AsyncCompletionPutTask(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, PPDMASYNCCOMPLETIONTASK pTask);
155
156
157/**
158 * Internal worker for the creation apis
159 *
160 * @returns VBox status.
161 * @param pVM Pointer to the VM.
162 * @param ppTemplate Where to store the template handle.
163 */
164static int pdmR3AsyncCompletionTemplateCreate(PVM pVM, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate,
165 PDMASYNCCOMPLETIONTEMPLATETYPE enmType)
166{
167 PUVM pUVM = pVM->pUVM;
168
169 AssertPtrReturn(ppTemplate, VERR_INVALID_POINTER);
170
171 PPDMASYNCCOMPLETIONTEMPLATE pTemplate;
172 int rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_ASYNC_COMPLETION, sizeof(PDMASYNCCOMPLETIONTEMPLATE), (void **)&pTemplate);
173 if (RT_FAILURE(rc))
174 return rc;
175
176 /*
177 * Initialize fields.
178 */
179 pTemplate->pVM = pVM;
180 pTemplate->cUsed = 0;
181 pTemplate->enmType = enmType;
182
183 /*
184 * Add template to the global VM template list.
185 */
186 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
187 pTemplate->pNext = pUVM->pdm.s.pAsyncCompletionTemplates;
188 if (pUVM->pdm.s.pAsyncCompletionTemplates)
189 pUVM->pdm.s.pAsyncCompletionTemplates->pPrev = pTemplate;
190 pUVM->pdm.s.pAsyncCompletionTemplates = pTemplate;
191 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
192
193 *ppTemplate = pTemplate;
194 return VINF_SUCCESS;
195}
196
197
198#ifdef SOME_UNUSED_FUNCTION
199/**
200 * Creates a async completion template for a device instance.
201 *
202 * The template is used when creating new completion tasks.
203 *
204 * @returns VBox status code.
205 * @param pVM Pointer to the VM.
206 * @param pDevIns The device instance.
207 * @param ppTemplate Where to store the template pointer on success.
208 * @param pfnCompleted The completion callback routine.
209 * @param pszDesc Description.
210 */
211int pdmR3AsyncCompletionTemplateCreateDevice(PVM pVM, PPDMDEVINS pDevIns, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate,
212 PFNPDMASYNCCOMPLETEDEV pfnCompleted, const char *pszDesc)
213{
214 LogFlow(("%s: pDevIns=%p ppTemplate=%p pfnCompleted=%p pszDesc=%s\n",
215 __FUNCTION__, pDevIns, ppTemplate, pfnCompleted, pszDesc));
216
217 /*
218 * Validate input.
219 */
220 VM_ASSERT_EMT(pVM);
221 AssertPtrReturn(pfnCompleted, VERR_INVALID_POINTER);
222 AssertPtrReturn(ppTemplate, VERR_INVALID_POINTER);
223
224 /*
225 * Create the template.
226 */
227 PPDMASYNCCOMPLETIONTEMPLATE pTemplate;
228 int rc = pdmR3AsyncCompletionTemplateCreate(pVM, &pTemplate, PDMASYNCCOMPLETIONTEMPLATETYPE_DEV);
229 if (RT_SUCCESS(rc))
230 {
231 pTemplate->u.Dev.pDevIns = pDevIns;
232 pTemplate->u.Dev.pfnCompleted = pfnCompleted;
233
234 *ppTemplate = pTemplate;
235 Log(("PDM: Created device template %p: pfnCompleted=%p pDevIns=%p\n",
236 pTemplate, pfnCompleted, pDevIns));
237 }
238
239 return rc;
240}
241#endif /* SOME_UNUSED_FUNCTION */
242
243
244/**
245 * Creates a async completion template for a driver instance.
246 *
247 * The template is used when creating new completion tasks.
248 *
249 * @returns VBox status code.
250 * @param pVM Pointer to the VM.
251 * @param pDrvIns The driver instance.
252 * @param ppTemplate Where to store the template pointer on success.
253 * @param pfnCompleted The completion callback routine.
254 * @param pvTemplateUser Template user argument
255 * @param pszDesc Description.
256 */
257int pdmR3AsyncCompletionTemplateCreateDriver(PVM pVM, PPDMDRVINS pDrvIns, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate,
258 PFNPDMASYNCCOMPLETEDRV pfnCompleted, void *pvTemplateUser,
259 const char *pszDesc)
260{
261 LogFlow(("PDMR3AsyncCompletionTemplateCreateDriver: pDrvIns=%p ppTemplate=%p pfnCompleted=%p pszDesc=%s\n", pDrvIns, ppTemplate, pfnCompleted, pszDesc));
262
263 /*
264 * Validate input.
265 */
266 AssertPtrReturn(pfnCompleted, VERR_INVALID_POINTER);
267 AssertPtrReturn(ppTemplate, VERR_INVALID_POINTER);
268
269 /*
270 * Create the template.
271 */
272 PPDMASYNCCOMPLETIONTEMPLATE pTemplate;
273 int rc = pdmR3AsyncCompletionTemplateCreate(pVM, &pTemplate, PDMASYNCCOMPLETIONTEMPLATETYPE_DRV);
274 if (RT_SUCCESS(rc))
275 {
276 pTemplate->u.Drv.pDrvIns = pDrvIns;
277 pTemplate->u.Drv.pfnCompleted = pfnCompleted;
278 pTemplate->u.Drv.pvTemplateUser = pvTemplateUser;
279
280 *ppTemplate = pTemplate;
281 Log(("PDM: Created driver template %p: pfnCompleted=%p pDrvIns=%p\n",
282 pTemplate, pfnCompleted, pDrvIns));
283 }
284
285 return rc;
286}
287
288
289#ifdef SOME_UNUSED_FUNCTION
290/**
291 * Creates a async completion template for a USB device instance.
292 *
293 * The template is used when creating new completion tasks.
294 *
295 * @returns VBox status code.
296 * @param pVM Pointer to the VM.
297 * @param pUsbIns The USB device instance.
298 * @param ppTemplate Where to store the template pointer on success.
299 * @param pfnCompleted The completion callback routine.
300 * @param pszDesc Description.
301 */
302int pdmR3AsyncCompletionTemplateCreateUsb(PVM pVM, PPDMUSBINS pUsbIns, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate,
303 PFNPDMASYNCCOMPLETEUSB pfnCompleted, const char *pszDesc)
304{
305 LogFlow(("pdmR3AsyncCompletionTemplateCreateUsb: pUsbIns=%p ppTemplate=%p pfnCompleted=%p pszDesc=%s\n", pUsbIns, ppTemplate, pfnCompleted, pszDesc));
306
307 /*
308 * Validate input.
309 */
310 VM_ASSERT_EMT(pVM);
311 AssertPtrReturn(pfnCompleted, VERR_INVALID_POINTER);
312 AssertPtrReturn(ppTemplate, VERR_INVALID_POINTER);
313
314 /*
315 * Create the template.
316 */
317 PPDMASYNCCOMPLETIONTEMPLATE pTemplate;
318 int rc = pdmR3AsyncCompletionTemplateCreate(pVM, &pTemplate, PDMASYNCCOMPLETIONTEMPLATETYPE_USB);
319 if (RT_SUCCESS(rc))
320 {
321 pTemplate->u.Usb.pUsbIns = pUsbIns;
322 pTemplate->u.Usb.pfnCompleted = pfnCompleted;
323
324 *ppTemplate = pTemplate;
325 Log(("PDM: Created usb template %p: pfnCompleted=%p pDevIns=%p\n",
326 pTemplate, pfnCompleted, pUsbIns));
327 }
328
329 return rc;
330}
331#endif
332
333
334/**
335 * Creates a async completion template for internally by the VMM.
336 *
337 * The template is used when creating new completion tasks.
338 *
339 * @returns VBox status code.
340 * @param pVM Pointer to the VM.
341 * @param ppTemplate Where to store the template pointer on success.
342 * @param pfnCompleted The completion callback routine.
343 * @param pvUser2 The 2nd user argument for the callback.
344 * @param pszDesc Description.
345 * @internal
346 */
347VMMR3DECL(int) PDMR3AsyncCompletionTemplateCreateInternal(PVM pVM, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate, PFNPDMASYNCCOMPLETEINT pfnCompleted, void *pvUser2, const char *pszDesc)
348{
349 LogFlow(("%s: ppTemplate=%p pfnCompleted=%p pvUser2=%p pszDesc=%s\n",
350 __FUNCTION__, ppTemplate, pfnCompleted, pvUser2, pszDesc));
351
352 /*
353 * Validate input.
354 */
355 VM_ASSERT_EMT(pVM);
356 AssertPtrReturn(pfnCompleted, VERR_INVALID_POINTER);
357 AssertPtrReturn(ppTemplate, VERR_INVALID_POINTER);
358
359 /*
360 * Create the template.
361 */
362 PPDMASYNCCOMPLETIONTEMPLATE pTemplate;
363 int rc = pdmR3AsyncCompletionTemplateCreate(pVM, &pTemplate, PDMASYNCCOMPLETIONTEMPLATETYPE_INTERNAL);
364 if (RT_SUCCESS(rc))
365 {
366 pTemplate->u.Int.pvUser = pvUser2;
367 pTemplate->u.Int.pfnCompleted = pfnCompleted;
368
369 *ppTemplate = pTemplate;
370 Log(("PDM: Created internal template %p: pfnCompleted=%p pvUser2=%p\n",
371 pTemplate, pfnCompleted, pvUser2));
372 }
373
374 return rc;
375}
376
377
378/**
379 * Destroys the specified async completion template.
380 *
381 * @returns VBox status codes:
382 * @retval VINF_SUCCESS on success.
383 * @retval VERR_PDM_ASYNC_TEMPLATE_BUSY if the template is still in use.
384 *
385 * @param pTemplate The template in question.
386 */
387VMMR3DECL(int) PDMR3AsyncCompletionTemplateDestroy(PPDMASYNCCOMPLETIONTEMPLATE pTemplate)
388{
389 LogFlow(("%s: pTemplate=%p\n", __FUNCTION__, pTemplate));
390
391 if (!pTemplate)
392 {
393 AssertMsgFailed(("pTemplate is NULL!\n"));
394 return VERR_INVALID_PARAMETER;
395 }
396
397 /*
398 * Check if the template is still used.
399 */
400 if (pTemplate->cUsed > 0)
401 {
402 AssertMsgFailed(("Template is still in use\n"));
403 return VERR_PDM_ASYNC_TEMPLATE_BUSY;
404 }
405
406 /*
407 * Unlink the template from the list.
408 */
409 PUVM pUVM = pTemplate->pVM->pUVM;
410 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
411
412 PPDMASYNCCOMPLETIONTEMPLATE pPrev = pTemplate->pPrev;
413 PPDMASYNCCOMPLETIONTEMPLATE pNext = pTemplate->pNext;
414
415 if (pPrev)
416 pPrev->pNext = pNext;
417 else
418 pUVM->pdm.s.pAsyncCompletionTemplates = pNext;
419
420 if (pNext)
421 pNext->pPrev = pPrev;
422
423 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
424
425 /*
426 * Free the template.
427 */
428 MMR3HeapFree(pTemplate);
429
430 return VINF_SUCCESS;
431}
432
433
434/**
435 * Destroys all the specified async completion templates for the given device instance.
436 *
437 * @returns VBox status codes:
438 * @retval VINF_SUCCESS on success.
439 * @retval VERR_PDM_ASYNC_TEMPLATE_BUSY if one or more of the templates are still in use.
440 *
441 * @param pVM Pointer to the VM.
442 * @param pDevIns The device instance.
443 */
444int pdmR3AsyncCompletionTemplateDestroyDevice(PVM pVM, PPDMDEVINS pDevIns)
445{
446 LogFlow(("pdmR3AsyncCompletionTemplateDestroyDevice: pDevIns=%p\n", pDevIns));
447
448 /*
449 * Validate input.
450 */
451 if (!pDevIns)
452 return VERR_INVALID_PARAMETER;
453 VM_ASSERT_EMT(pVM);
454
455 /*
456 * Unlink it.
457 */
458 PUVM pUVM = pVM->pUVM;
459 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
460 PPDMASYNCCOMPLETIONTEMPLATE pTemplate = pUVM->pdm.s.pAsyncCompletionTemplates;
461 while (pTemplate)
462 {
463 if ( pTemplate->enmType == PDMASYNCCOMPLETIONTEMPLATETYPE_DEV
464 && pTemplate->u.Dev.pDevIns == pDevIns)
465 {
466 PPDMASYNCCOMPLETIONTEMPLATE pTemplateDestroy = pTemplate;
467 pTemplate = pTemplate->pNext;
468 int rc = PDMR3AsyncCompletionTemplateDestroy(pTemplateDestroy);
469 if (RT_FAILURE(rc))
470 {
471 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
472 return rc;
473 }
474 }
475 else
476 pTemplate = pTemplate->pNext;
477 }
478
479 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
480 return VINF_SUCCESS;
481}
482
483
484/**
485 * Destroys all the specified async completion templates for the given driver instance.
486 *
487 * @returns VBox status codes:
488 * @retval VINF_SUCCESS on success.
489 * @retval VERR_PDM_ASYNC_TEMPLATE_BUSY if one or more of the templates are still in use.
490 *
491 * @param pVM Pointer to the VM.
492 * @param pDrvIns The driver instance.
493 */
494int pdmR3AsyncCompletionTemplateDestroyDriver(PVM pVM, PPDMDRVINS pDrvIns)
495{
496 LogFlow(("pdmR3AsyncCompletionTemplateDestroyDriver: pDevIns=%p\n", pDrvIns));
497
498 /*
499 * Validate input.
500 */
501 if (!pDrvIns)
502 return VERR_INVALID_PARAMETER;
503 VM_ASSERT_EMT(pVM);
504
505 /*
506 * Unlink it.
507 */
508 PUVM pUVM = pVM->pUVM;
509 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
510 PPDMASYNCCOMPLETIONTEMPLATE pTemplate = pUVM->pdm.s.pAsyncCompletionTemplates;
511 while (pTemplate)
512 {
513 if ( pTemplate->enmType == PDMASYNCCOMPLETIONTEMPLATETYPE_DRV
514 && pTemplate->u.Drv.pDrvIns == pDrvIns)
515 {
516 PPDMASYNCCOMPLETIONTEMPLATE pTemplateDestroy = pTemplate;
517 pTemplate = pTemplate->pNext;
518 int rc = PDMR3AsyncCompletionTemplateDestroy(pTemplateDestroy);
519 if (RT_FAILURE(rc))
520 {
521 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
522 return rc;
523 }
524 }
525 else
526 pTemplate = pTemplate->pNext;
527 }
528
529 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
530 return VINF_SUCCESS;
531}
532
533
534/**
535 * Destroys all the specified async completion templates for the given USB device instance.
536 *
537 * @returns VBox status codes:
538 * @retval VINF_SUCCESS on success.
539 * @retval VERR_PDM_ASYNC_TEMPLATE_BUSY if one or more of the templates are still in use.
540 *
541 * @param pVM Pointer to the VM.
542 * @param pUsbIns The USB device instance.
543 */
544int pdmR3AsyncCompletionTemplateDestroyUsb(PVM pVM, PPDMUSBINS pUsbIns)
545{
546 LogFlow(("pdmR3AsyncCompletionTemplateDestroyUsb: pUsbIns=%p\n", pUsbIns));
547
548 /*
549 * Validate input.
550 */
551 if (!pUsbIns)
552 return VERR_INVALID_PARAMETER;
553 VM_ASSERT_EMT(pVM);
554
555 /*
556 * Unlink it.
557 */
558 PUVM pUVM = pVM->pUVM;
559 RTCritSectEnter(&pUVM->pdm.s.ListCritSect);
560 PPDMASYNCCOMPLETIONTEMPLATE pTemplate = pUVM->pdm.s.pAsyncCompletionTemplates;
561 while (pTemplate)
562 {
563 if ( pTemplate->enmType == PDMASYNCCOMPLETIONTEMPLATETYPE_USB
564 && pTemplate->u.Usb.pUsbIns == pUsbIns)
565 {
566 PPDMASYNCCOMPLETIONTEMPLATE pTemplateDestroy = pTemplate;
567 pTemplate = pTemplate->pNext;
568 int rc = PDMR3AsyncCompletionTemplateDestroy(pTemplateDestroy);
569 if (RT_FAILURE(rc))
570 {
571 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
572 return rc;
573 }
574 }
575 else
576 pTemplate = pTemplate->pNext;
577 }
578
579 RTCritSectLeave(&pUVM->pdm.s.ListCritSect);
580 return VINF_SUCCESS;
581}
582
583
584/** Lazy coder. */
585static PPDMACBWMGR pdmacBwMgrFindById(PPDMASYNCCOMPLETIONEPCLASS pEpClass, const char *pszId)
586{
587 PPDMACBWMGR pBwMgr = NULL;
588
589 if (pszId)
590 {
591 int rc = RTCritSectEnter(&pEpClass->CritSect); AssertRC(rc);
592
593 pBwMgr = pEpClass->pBwMgrsHead;
594 while ( pBwMgr
595 && RTStrCmp(pBwMgr->pszId, pszId))
596 pBwMgr = pBwMgr->pNext;
597
598 rc = RTCritSectLeave(&pEpClass->CritSect); AssertRC(rc);
599 }
600
601 return pBwMgr;
602}
603
604
605/** Lazy coder. */
606static void pdmacBwMgrLink(PPDMACBWMGR pBwMgr)
607{
608 PPDMASYNCCOMPLETIONEPCLASS pEpClass = pBwMgr->pEpClass;
609 int rc = RTCritSectEnter(&pEpClass->CritSect); AssertRC(rc);
610
611 pBwMgr->pNext = pEpClass->pBwMgrsHead;
612 pEpClass->pBwMgrsHead = pBwMgr;
613
614 rc = RTCritSectLeave(&pEpClass->CritSect); AssertRC(rc);
615}
616
617
618#ifdef SOME_UNUSED_FUNCTION
619/** Lazy coder. */
620static void pdmacBwMgrUnlink(PPDMACBWMGR pBwMgr)
621{
622 PPDMASYNCCOMPLETIONEPCLASS pEpClass = pBwMgr->pEpClass;
623 int rc = RTCritSectEnter(&pEpClass->CritSect); AssertRC(rc);
624
625 if (pBwMgr == pEpClass->pBwMgrsHead)
626 pEpClass->pBwMgrsHead = pBwMgr->pNext;
627 else
628 {
629 PPDMACBWMGR pPrev = pEpClass->pBwMgrsHead;
630 while ( pPrev
631 && pPrev->pNext != pBwMgr)
632 pPrev = pPrev->pNext;
633
634 AssertPtr(pPrev);
635 pPrev->pNext = pBwMgr->pNext;
636 }
637
638 rc = RTCritSectLeave(&pEpClass->CritSect); AssertRC(rc);
639}
640#endif /* SOME_UNUSED_FUNCTION */
641
642
643/** Lazy coder. */
644static int pdmacAsyncCompletionBwMgrCreate(PPDMASYNCCOMPLETIONEPCLASS pEpClass, const char *pszBwMgr, uint32_t cbTransferPerSecMax,
645 uint32_t cbTransferPerSecStart, uint32_t cbTransferPerSecStep)
646{
647 LogFlowFunc(("pEpClass=%#p pszBwMgr=%#p{%s} cbTransferPerSecMax=%u cbTransferPerSecStart=%u cbTransferPerSecStep=%u\n",
648 pEpClass, pszBwMgr, cbTransferPerSecMax, cbTransferPerSecStart, cbTransferPerSecStep));
649
650 AssertPtrReturn(pEpClass, VERR_INVALID_POINTER);
651 AssertPtrReturn(pszBwMgr, VERR_INVALID_POINTER);
652 AssertReturn(*pszBwMgr != '\0', VERR_INVALID_PARAMETER);
653
654 int rc;
655 PPDMACBWMGR pBwMgr = pdmacBwMgrFindById(pEpClass, pszBwMgr);
656 if (!pBwMgr)
657 {
658 rc = MMR3HeapAllocZEx(pEpClass->pVM, MM_TAG_PDM_ASYNC_COMPLETION,
659 sizeof(PDMACBWMGR),
660 (void **)&pBwMgr);
661 if (RT_SUCCESS(rc))
662 {
663 pBwMgr->pszId = RTStrDup(pszBwMgr);
664 if (pBwMgr->pszId)
665 {
666 pBwMgr->pEpClass = pEpClass;
667 pBwMgr->cRefs = 0;
668
669 /* Init I/O flow control. */
670 pBwMgr->cbTransferPerSecMax = cbTransferPerSecMax;
671 pBwMgr->cbTransferPerSecStart = cbTransferPerSecStart;
672 pBwMgr->cbTransferPerSecStep = cbTransferPerSecStep;
673
674 pBwMgr->cbTransferAllowed = pBwMgr->cbTransferPerSecStart;
675 pBwMgr->tsUpdatedLast = RTTimeSystemNanoTS();
676
677 pdmacBwMgrLink(pBwMgr);
678 rc = VINF_SUCCESS;
679 }
680 else
681 {
682 rc = VERR_NO_MEMORY;
683 MMR3HeapFree(pBwMgr);
684 }
685 }
686 }
687 else
688 rc = VERR_ALREADY_EXISTS;
689
690 LogFlowFunc(("returns rc=%Rrc\n", rc));
691 return rc;
692}
693
694
695/** Lazy coder. */
696DECLINLINE(void) pdmacBwMgrRetain(PPDMACBWMGR pBwMgr)
697{
698 ASMAtomicIncU32(&pBwMgr->cRefs);
699}
700
701
702/** Lazy coder. */
703DECLINLINE(void) pdmacBwMgrRelease(PPDMACBWMGR pBwMgr)
704{
705 Assert(pBwMgr->cRefs > 0);
706 ASMAtomicDecU32(&pBwMgr->cRefs);
707}
708
709
710/**
711 * Checks if the endpoint is allowed to transfer the given amount of bytes.
712 *
713 * @returns true if the endpoint is allowed to transfer the data.
714 * false otherwise
715 * @param pEndpoint The endpoint.
716 * @param cbTransfer The number of bytes to transfer.
717 * @param pmsWhenNext Where to store the number of milliseconds
718 * until the bandwidth is refreshed.
719 * Only set if false is returned.
720 */
721bool pdmacEpIsTransferAllowed(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint32_t cbTransfer, RTMSINTERVAL *pmsWhenNext)
722{
723 bool fAllowed = true;
724 PPDMACBWMGR pBwMgr = ASMAtomicReadPtrT(&pEndpoint->pBwMgr, PPDMACBWMGR);
725
726 LogFlowFunc(("pEndpoint=%p pBwMgr=%p cbTransfer=%u\n", pEndpoint, pBwMgr, cbTransfer));
727
728 if (pBwMgr)
729 {
730 uint32_t cbOld = ASMAtomicSubU32(&pBwMgr->cbTransferAllowed, cbTransfer);
731 if (RT_LIKELY(cbOld >= cbTransfer))
732 fAllowed = true;
733 else
734 {
735 fAllowed = false;
736
737 /* We are out of resources Check if we can update again. */
738 uint64_t tsNow = RTTimeSystemNanoTS();
739 uint64_t tsUpdatedLast = ASMAtomicUoReadU64(&pBwMgr->tsUpdatedLast);
740
741 if (tsNow - tsUpdatedLast >= (1000*1000*1000))
742 {
743 if (ASMAtomicCmpXchgU64(&pBwMgr->tsUpdatedLast, tsNow, tsUpdatedLast))
744 {
745 if (pBwMgr->cbTransferPerSecStart < pBwMgr->cbTransferPerSecMax)
746 {
747 pBwMgr->cbTransferPerSecStart = RT_MIN(pBwMgr->cbTransferPerSecMax, pBwMgr->cbTransferPerSecStart + pBwMgr->cbTransferPerSecStep);
748 LogFlow(("AIOMgr: Increasing maximum bandwidth to %u bytes/sec\n", pBwMgr->cbTransferPerSecStart));
749 }
750
751 /* Update */
752 ASMAtomicWriteU32(&pBwMgr->cbTransferAllowed, pBwMgr->cbTransferPerSecStart - cbTransfer);
753 fAllowed = true;
754 LogFlow(("AIOMgr: Refreshed bandwidth\n"));
755 }
756 }
757 else
758 {
759 ASMAtomicAddU32(&pBwMgr->cbTransferAllowed, cbTransfer);
760 *pmsWhenNext = ((1000*1000*1000) - (tsNow - tsUpdatedLast)) / (1000*1000);
761 }
762 }
763 }
764
765 LogFlowFunc(("fAllowed=%RTbool\n", fAllowed));
766 return fAllowed;
767}
768
769
770/**
771 * Called by the endpoint if a task has finished.
772 *
773 * @returns nothing
774 * @param pTask Pointer to the finished task.
775 * @param rc Status code of the completed request.
776 * @param fCallCompletionHandler Flag whether the completion handler should be called to
777 * inform the owner of the task that it has completed.
778 */
779void pdmR3AsyncCompletionCompleteTask(PPDMASYNCCOMPLETIONTASK pTask, int rc, bool fCallCompletionHandler)
780{
781 LogFlow(("%s: pTask=%#p fCallCompletionHandler=%RTbool\n", __FUNCTION__, pTask, fCallCompletionHandler));
782
783 if (fCallCompletionHandler)
784 {
785 PPDMASYNCCOMPLETIONTEMPLATE pTemplate = pTask->pEndpoint->pTemplate;
786
787 switch (pTemplate->enmType)
788 {
789 case PDMASYNCCOMPLETIONTEMPLATETYPE_DEV:
790 pTemplate->u.Dev.pfnCompleted(pTemplate->u.Dev.pDevIns, pTask->pvUser, rc);
791 break;
792
793 case PDMASYNCCOMPLETIONTEMPLATETYPE_DRV:
794 pTemplate->u.Drv.pfnCompleted(pTemplate->u.Drv.pDrvIns, pTemplate->u.Drv.pvTemplateUser, pTask->pvUser, rc);
795 break;
796
797 case PDMASYNCCOMPLETIONTEMPLATETYPE_USB:
798 pTemplate->u.Usb.pfnCompleted(pTemplate->u.Usb.pUsbIns, pTask->pvUser, rc);
799 break;
800
801 case PDMASYNCCOMPLETIONTEMPLATETYPE_INTERNAL:
802 pTemplate->u.Int.pfnCompleted(pTemplate->pVM, pTask->pvUser, pTemplate->u.Int.pvUser, rc);
803 break;
804
805 default:
806 AssertMsgFailed(("Unknown template type!\n"));
807 }
808 }
809
810 pdmR3AsyncCompletionPutTask(pTask->pEndpoint, pTask);
811}
812
813
814/**
815 * Worker initializing a endpoint class.
816 *
817 * @returns VBox status code.
818 * @param pVM Pointer to the shared VM instance data.
819 * @param pEpClass Pointer to the endpoint class structure.
820 * @param pCfgHandle Pointer to the CFGM tree.
821 */
822int pdmR3AsyncCompletionEpClassInit(PVM pVM, PCPDMASYNCCOMPLETIONEPCLASSOPS pEpClassOps, PCFGMNODE pCfgHandle)
823{
824 /* Validate input. */
825 AssertPtrReturn(pEpClassOps, VERR_INVALID_POINTER);
826 AssertReturn(pEpClassOps->u32Version == PDMAC_EPCLASS_OPS_VERSION, VERR_VERSION_MISMATCH);
827 AssertReturn(pEpClassOps->u32VersionEnd == PDMAC_EPCLASS_OPS_VERSION, VERR_VERSION_MISMATCH);
828
829 LogFlow(("pdmR3AsyncCompletionEpClassInit: pVM=%p pEpClassOps=%p{%s}\n", pVM, pEpClassOps, pEpClassOps->pszName));
830
831 /* Allocate global class data. */
832 PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = NULL;
833
834 int rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_ASYNC_COMPLETION,
835 pEpClassOps->cbEndpointClassGlobal,
836 (void **)&pEndpointClass);
837 if (RT_SUCCESS(rc))
838 {
839 /* Initialize common data. */
840 pEndpointClass->pVM = pVM;
841 pEndpointClass->pEndpointOps = pEpClassOps;
842
843 rc = RTCritSectInit(&pEndpointClass->CritSect);
844 if (RT_SUCCESS(rc))
845 {
846 PCFGMNODE pCfgNodeClass = CFGMR3GetChild(pCfgHandle, pEpClassOps->pszName);
847
848 /* Create task cache */
849 rc = RTMemCacheCreate(&pEndpointClass->hMemCacheTasks, pEpClassOps->cbTask,
850 0, UINT32_MAX, NULL, NULL, NULL, 0);
851 if (RT_SUCCESS(rc))
852 {
853 /* Call the specific endpoint class initializer. */
854 rc = pEpClassOps->pfnInitialize(pEndpointClass, pCfgNodeClass);
855 if (RT_SUCCESS(rc))
856 {
857 /* Create all bandwidth groups for resource control. */
858 PCFGMNODE pCfgBwGrp = CFGMR3GetChild(pCfgNodeClass, "BwGroups");
859
860 if (pCfgBwGrp)
861 {
862 for (PCFGMNODE pCur = CFGMR3GetFirstChild(pCfgBwGrp); pCur; pCur = CFGMR3GetNextChild(pCur))
863 {
864 uint32_t cbMax, cbStart, cbStep;
865 size_t cchName = CFGMR3GetNameLen(pCur) + 1;
866 char *pszBwGrpId = (char *)RTMemAllocZ(cchName);
867
868 if (!pszBwGrpId)
869 {
870 rc = VERR_NO_MEMORY;
871 break;
872 }
873
874 rc = CFGMR3GetName(pCur, pszBwGrpId, cchName);
875 AssertRC(rc);
876
877 if (RT_SUCCESS(rc))
878 rc = CFGMR3QueryU32(pCur, "Max", &cbMax);
879 if (RT_SUCCESS(rc))
880 rc = CFGMR3QueryU32Def(pCur, "Start", &cbStart, cbMax);
881 if (RT_SUCCESS(rc))
882 rc = CFGMR3QueryU32Def(pCur, "Step", &cbStep, 0);
883 if (RT_SUCCESS(rc))
884 rc = pdmacAsyncCompletionBwMgrCreate(pEndpointClass, pszBwGrpId, cbMax, cbStart, cbStep);
885
886 RTMemFree(pszBwGrpId);
887
888 if (RT_FAILURE(rc))
889 break;
890 }
891 }
892
893 if (RT_SUCCESS(rc))
894 {
895 PUVM pUVM = pVM->pUVM;
896 AssertMsg(!pUVM->pdm.s.apAsyncCompletionEndpointClass[pEpClassOps->enmClassType],
897 ("Endpoint class was already initialized\n"));
898
899#ifdef VBOX_WITH_STATISTICS
900 CFGMR3QueryBoolDef(pCfgNodeClass, "AdvancedStatistics", &pEndpointClass->fGatherAdvancedStatistics, true);
901#else
902 CFGMR3QueryBoolDef(pCfgNodeClass, "AdvancedStatistics", &pEndpointClass->fGatherAdvancedStatistics, false);
903#endif
904
905 pUVM->pdm.s.apAsyncCompletionEndpointClass[pEpClassOps->enmClassType] = pEndpointClass;
906 LogFlowFunc((": Initialized endpoint class \"%s\" rc=%Rrc\n", pEpClassOps->pszName, rc));
907 return VINF_SUCCESS;
908 }
909 }
910 RTMemCacheDestroy(pEndpointClass->hMemCacheTasks);
911 }
912 RTCritSectDelete(&pEndpointClass->CritSect);
913 }
914 MMR3HeapFree(pEndpointClass);
915 }
916
917 LogFlowFunc((": Failed to initialize endpoint class rc=%Rrc\n", rc));
918
919 return rc;
920}
921
922
923/**
924 * Worker terminating all endpoint classes.
925 *
926 * @returns nothing
927 * @param pEndpointClass Pointer to the endpoint class to terminate.
928 *
929 * @remarks This method ensures that any still open endpoint is closed.
930 */
931static void pdmR3AsyncCompletionEpClassTerminate(PPDMASYNCCOMPLETIONEPCLASS pEndpointClass)
932{
933 PVM pVM = pEndpointClass->pVM;
934
935 /* Close all still open endpoints. */
936 while (pEndpointClass->pEndpointsHead)
937 PDMR3AsyncCompletionEpClose(pEndpointClass->pEndpointsHead);
938
939 /* Destroy the bandwidth managers. */
940 PPDMACBWMGR pBwMgr = pEndpointClass->pBwMgrsHead;
941 while (pBwMgr)
942 {
943 PPDMACBWMGR pFree = pBwMgr;
944 pBwMgr = pBwMgr->pNext;
945 MMR3HeapFree(pFree);
946 }
947
948 /* Call the termination callback of the class. */
949 pEndpointClass->pEndpointOps->pfnTerminate(pEndpointClass);
950
951 RTMemCacheDestroy(pEndpointClass->hMemCacheTasks);
952 RTCritSectDelete(&pEndpointClass->CritSect);
953
954 /* Free the memory of the class finally and clear the entry in the class array. */
955 pVM->pUVM->pdm.s.apAsyncCompletionEndpointClass[pEndpointClass->pEndpointOps->enmClassType] = NULL;
956 MMR3HeapFree(pEndpointClass);
957}
958
959
960/**
961 * Records the size of the request in the statistics.
962 *
963 * @returns nothing.
964 * @param pEndpoint The endpoint to register the request size for.
965 * @param cbReq Size of the request.
966 */
967static void pdmR3AsyncCompletionStatisticsRecordSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, size_t cbReq)
968{
969 if (cbReq < 512)
970 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSizeSmaller512);
971 else if (cbReq < _1K)
972 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSize512To1K);
973 else if (cbReq < _2K)
974 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSize1KTo2K);
975 else if (cbReq < _4K)
976 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSize2KTo4K);
977 else if (cbReq < _8K)
978 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSize4KTo8K);
979 else if (cbReq < _16K)
980 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSize8KTo16K);
981 else if (cbReq < _32K)
982 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSize16KTo32K);
983 else if (cbReq < _64K)
984 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSize32KTo64K);
985 else if (cbReq < _128K)
986 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSize64KTo128K);
987 else if (cbReq < _256K)
988 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSize128KTo256K);
989 else if (cbReq < _512K)
990 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSize256KTo512K);
991 else
992 STAM_REL_COUNTER_INC(&pEndpoint->StatReqSizeOver512K);
993
994 if (cbReq & ((size_t)512 - 1))
995 STAM_REL_COUNTER_INC(&pEndpoint->StatReqsUnaligned512);
996 else if (cbReq & ((size_t)_4K - 1))
997 STAM_REL_COUNTER_INC(&pEndpoint->StatReqsUnaligned4K);
998 else if (cbReq & ((size_t)_8K - 1))
999 STAM_REL_COUNTER_INC(&pEndpoint->StatReqsUnaligned8K);
1000}
1001
1002
1003/**
1004 * Records the required processing time of a request.
1005 *
1006 * @returns nothing.
1007 * @param pEndpoint The endpoint.
1008 * @param cNsRun The request time in nanoseconds.
1009 */
1010static void pdmR3AsyncCompletionStatisticsRecordCompletionTime(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t cNsRun)
1011{
1012 PSTAMCOUNTER pStatCounter;
1013 if (cNsRun < RT_NS_1US)
1014 pStatCounter = &pEndpoint->StatTaskRunTimesNs[cNsRun / (RT_NS_1US / 10)];
1015 else if (cNsRun < RT_NS_1MS)
1016 pStatCounter = &pEndpoint->StatTaskRunTimesUs[cNsRun / (RT_NS_1MS / 10)];
1017 else if (cNsRun < RT_NS_1SEC)
1018 pStatCounter = &pEndpoint->StatTaskRunTimesMs[cNsRun / (RT_NS_1SEC / 10)];
1019 else if (cNsRun < RT_NS_1SEC_64*100)
1020 pStatCounter = &pEndpoint->StatTaskRunTimesSec[cNsRun / (RT_NS_1SEC_64*100 / 10)];
1021 else
1022 pStatCounter = &pEndpoint->StatTaskRunOver100Sec;
1023 STAM_REL_COUNTER_INC(pStatCounter);
1024
1025 STAM_REL_COUNTER_INC(&pEndpoint->StatIoOpsCompleted);
1026 pEndpoint->cIoOpsCompleted++;
1027 uint64_t tsMsCur = RTTimeMilliTS();
1028 uint64_t tsInterval = tsMsCur - pEndpoint->tsIntervalStartMs;
1029 if (tsInterval >= 1000)
1030 {
1031 pEndpoint->StatIoOpsPerSec.c = pEndpoint->cIoOpsCompleted / (tsInterval / 1000);
1032 pEndpoint->tsIntervalStartMs = tsMsCur;
1033 pEndpoint->cIoOpsCompleted = 0;
1034 }
1035}
1036
1037
1038/**
1039 * Registers advanced statistics for the given endpoint.
1040 *
1041 * @returns VBox status code.
1042 * @param pEndpoint The endpoint to register the advanced statistics for.
1043 */
1044static int pdmR3AsyncCompletionStatisticsRegister(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1045{
1046 int rc = VINF_SUCCESS;
1047 PVM pVM = pEndpoint->pEpClass->pVM;
1048
1049 pEndpoint->tsIntervalStartMs = RTTimeMilliTS();
1050
1051 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesNs) && RT_SUCCESS(rc); i++)
1052 rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesNs[i], STAMTYPE_COUNTER,
1053 STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
1054 "Nanosecond resolution runtime statistics",
1055 "/PDM/AsyncCompletion/File/%s/TaskRun1Ns-%u-%u",
1056 RTPathFilename(pEndpoint->pszUri), i*100, i*100+100-1);
1057
1058 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesUs) && RT_SUCCESS(rc); i++)
1059 rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesUs[i], STAMTYPE_COUNTER,
1060 STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
1061 "Microsecond resolution runtime statistics",
1062 "/PDM/AsyncCompletion/File/%s/TaskRun2MicroSec-%u-%u",
1063 RTPathFilename(pEndpoint->pszUri), i*100, i*100+100-1);
1064
1065 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMs) && RT_SUCCESS(rc); i++)
1066 rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesMs[i], STAMTYPE_COUNTER,
1067 STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
1068 "Milliseconds resolution runtime statistics",
1069 "/PDM/AsyncCompletion/File/%s/TaskRun3Ms-%u-%u",
1070 RTPathFilename(pEndpoint->pszUri), i*100, i*100+100-1);
1071
1072 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMs) && RT_SUCCESS(rc); i++)
1073 rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesSec[i], STAMTYPE_COUNTER,
1074 STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
1075 "Second resolution runtime statistics",
1076 "/PDM/AsyncCompletion/File/%s/TaskRun4Sec-%u-%u",
1077 RTPathFilename(pEndpoint->pszUri), i*10, i*10+10-1);
1078
1079 if (RT_SUCCESS(rc))
1080 rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunOver100Sec, STAMTYPE_COUNTER,
1081 STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
1082 "Tasks which ran more than 100sec",
1083 "/PDM/AsyncCompletion/File/%s/TaskRunSecGreater100Sec",
1084 RTPathFilename(pEndpoint->pszUri));
1085
1086 if (RT_SUCCESS(rc))
1087 rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsPerSec, STAMTYPE_COUNTER,
1088 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1089 "Processed I/O operations per second",
1090 "/PDM/AsyncCompletion/File/%s/IoOpsPerSec",
1091 RTPathFilename(pEndpoint->pszUri));
1092
1093 if (RT_SUCCESS(rc))
1094 rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsStarted, STAMTYPE_COUNTER,
1095 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1096 "Started I/O operations for this endpoint",
1097 "/PDM/AsyncCompletion/File/%s/IoOpsStarted",
1098 RTPathFilename(pEndpoint->pszUri));
1099
1100 if (RT_SUCCESS(rc))
1101 rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsCompleted, STAMTYPE_COUNTER,
1102 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1103 "Completed I/O operations for this endpoint",
1104 "/PDM/AsyncCompletion/File/%s/IoOpsCompleted",
1105 RTPathFilename(pEndpoint->pszUri));
1106
1107 if (RT_SUCCESS(rc))
1108 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSizeSmaller512, STAMTYPE_COUNTER,
1109 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1110 "Number of requests with a size smaller than 512 bytes",
1111 "/PDM/AsyncCompletion/File/%s/ReqSizeSmaller512",
1112 RTPathFilename(pEndpoint->pszUri));
1113
1114 if (RT_SUCCESS(rc))
1115 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSize512To1K, STAMTYPE_COUNTER,
1116 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1117 "Number of requests with a size between 512 bytes and 1KB",
1118 "/PDM/AsyncCompletion/File/%s/ReqSize512To1K",
1119 RTPathFilename(pEndpoint->pszUri));
1120
1121 if (RT_SUCCESS(rc))
1122 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSize1KTo2K, STAMTYPE_COUNTER,
1123 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1124 "Number of requests with a size between 1KB and 2KB",
1125 "/PDM/AsyncCompletion/File/%s/ReqSize1KTo2K",
1126 RTPathFilename(pEndpoint->pszUri));
1127
1128 if (RT_SUCCESS(rc))
1129 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSize2KTo4K, STAMTYPE_COUNTER,
1130 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1131 "Number of requests with a size between 2KB and 4KB",
1132 "/PDM/AsyncCompletion/File/%s/ReqSize2KTo4K",
1133 RTPathFilename(pEndpoint->pszUri));
1134
1135 if (RT_SUCCESS(rc))
1136 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSize4KTo8K, STAMTYPE_COUNTER,
1137 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1138 "Number of requests with a size between 4KB and 8KB",
1139 "/PDM/AsyncCompletion/File/%s/ReqSize4KTo8K",
1140 RTPathFilename(pEndpoint->pszUri));
1141
1142 if (RT_SUCCESS(rc))
1143 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSize8KTo16K, STAMTYPE_COUNTER,
1144 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1145 "Number of requests with a size between 8KB and 16KB",
1146 "/PDM/AsyncCompletion/File/%s/ReqSize8KTo16K",
1147 RTPathFilename(pEndpoint->pszUri));
1148
1149 if (RT_SUCCESS(rc))
1150 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSize16KTo32K, STAMTYPE_COUNTER,
1151 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1152 "Number of requests with a size between 16KB and 32KB",
1153 "/PDM/AsyncCompletion/File/%s/ReqSize16KTo32K",
1154 RTPathFilename(pEndpoint->pszUri));
1155
1156 if (RT_SUCCESS(rc))
1157 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSize32KTo64K, STAMTYPE_COUNTER,
1158 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1159 "Number of requests with a size between 32KB and 64KB",
1160 "/PDM/AsyncCompletion/File/%s/ReqSize32KTo64K",
1161 RTPathFilename(pEndpoint->pszUri));
1162
1163 if (RT_SUCCESS(rc))
1164 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSize64KTo128K, STAMTYPE_COUNTER,
1165 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1166 "Number of requests with a size between 64KB and 128KB",
1167 "/PDM/AsyncCompletion/File/%s/ReqSize64KTo128K",
1168 RTPathFilename(pEndpoint->pszUri));
1169
1170 if (RT_SUCCESS(rc))
1171 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSize128KTo256K, STAMTYPE_COUNTER,
1172 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1173 "Number of requests with a size between 128KB and 256KB",
1174 "/PDM/AsyncCompletion/File/%s/ReqSize128KTo256K",
1175 RTPathFilename(pEndpoint->pszUri));
1176
1177 if (RT_SUCCESS(rc))
1178 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSize256KTo512K, STAMTYPE_COUNTER,
1179 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1180 "Number of requests with a size between 256KB and 512KB",
1181 "/PDM/AsyncCompletion/File/%s/ReqSize256KTo512K",
1182 RTPathFilename(pEndpoint->pszUri));
1183
1184 if (RT_SUCCESS(rc))
1185 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqSizeOver512K, STAMTYPE_COUNTER,
1186 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1187 "Number of requests with a size over 512KB",
1188 "/PDM/AsyncCompletion/File/%s/ReqSizeOver512K",
1189 RTPathFilename(pEndpoint->pszUri));
1190
1191 if (RT_SUCCESS(rc))
1192 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqsUnaligned512, STAMTYPE_COUNTER,
1193 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1194 "Number of requests which size is not aligned to 512 bytes",
1195 "/PDM/AsyncCompletion/File/%s/ReqsUnaligned512",
1196 RTPathFilename(pEndpoint->pszUri));
1197
1198 if (RT_SUCCESS(rc))
1199 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqsUnaligned4K, STAMTYPE_COUNTER,
1200 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1201 "Number of requests which size is not aligned to 4KB",
1202 "/PDM/AsyncCompletion/File/%s/ReqsUnaligned4K",
1203 RTPathFilename(pEndpoint->pszUri));
1204
1205 if (RT_SUCCESS(rc))
1206 rc = STAMR3RegisterF(pVM, &pEndpoint->StatReqsUnaligned8K, STAMTYPE_COUNTER,
1207 STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
1208 "Number of requests which size is not aligned to 8KB",
1209 "/PDM/AsyncCompletion/File/%s/ReqsUnaligned8K",
1210 RTPathFilename(pEndpoint->pszUri));
1211
1212 return rc;
1213}
1214
1215
1216/**
1217 * Deregisters advanced statistics for one endpoint.
1218 *
1219 * @returns nothing.
1220 * @param pEndpoint The endpoint to deregister the advanced statistics for.
1221 */
1222static void pdmR3AsyncCompletionStatisticsDeregister(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1223{
1224 /* I hope this doesn't remove too much... */
1225 STAMR3DeregisterF(pEndpoint->pEpClass->pVM->pUVM, "/PDM/AsyncCompletion/File/%s/*", RTPathFilename(pEndpoint->pszUri));
1226}
1227
1228
1229/**
1230 * Initialize the async completion manager.
1231 *
1232 * @returns VBox status code
1233 * @param pVM Pointer to the VM.
1234 */
1235int pdmR3AsyncCompletionInit(PVM pVM)
1236{
1237 LogFlowFunc((": pVM=%p\n", pVM));
1238
1239 VM_ASSERT_EMT(pVM);
1240
1241 PCFGMNODE pCfgRoot = CFGMR3GetRoot(pVM);
1242 PCFGMNODE pCfgAsyncCompletion = CFGMR3GetChild(CFGMR3GetChild(pCfgRoot, "PDM"), "AsyncCompletion");
1243
1244 int rc = pdmR3AsyncCompletionEpClassInit(pVM, &g_PDMAsyncCompletionEndpointClassFile, pCfgAsyncCompletion);
1245 LogFlowFunc((": pVM=%p rc=%Rrc\n", pVM, rc));
1246 return rc;
1247}
1248
1249
1250/**
1251 * Terminates the async completion manager.
1252 *
1253 * @returns VBox status code
1254 * @param pVM Pointer to the VM.
1255 */
1256int pdmR3AsyncCompletionTerm(PVM pVM)
1257{
1258 LogFlowFunc((": pVM=%p\n", pVM));
1259 PUVM pUVM = pVM->pUVM;
1260
1261 for (size_t i = 0; i < RT_ELEMENTS(pUVM->pdm.s.apAsyncCompletionEndpointClass); i++)
1262 if (pUVM->pdm.s.apAsyncCompletionEndpointClass[i])
1263 pdmR3AsyncCompletionEpClassTerminate(pUVM->pdm.s.apAsyncCompletionEndpointClass[i]);
1264
1265 return VINF_SUCCESS;
1266}
1267
1268
1269/**
1270 * Resume worker for the async completion manager.
1271 *
1272 * @returns nothing.
1273 * @param pVM Pointer to the VM.
1274 */
1275void pdmR3AsyncCompletionResume(PVM pVM)
1276{
1277 LogFlowFunc((": pVM=%p\n", pVM));
1278 PUVM pUVM = pVM->pUVM;
1279
1280 /* Log the bandwidth groups and all assigned endpoints. */
1281 for (size_t i = 0; i < RT_ELEMENTS(pUVM->pdm.s.apAsyncCompletionEndpointClass); i++)
1282 if (pUVM->pdm.s.apAsyncCompletionEndpointClass[i])
1283 {
1284 PPDMASYNCCOMPLETIONEPCLASS pEpClass = pUVM->pdm.s.apAsyncCompletionEndpointClass[i];
1285 PPDMACBWMGR pBwMgr = pEpClass->pBwMgrsHead;
1286 PPDMASYNCCOMPLETIONENDPOINT pEp;
1287
1288 if (pBwMgr)
1289 LogRel(("AIOMgr: Bandwidth groups for class '%s'\n", i == PDMASYNCCOMPLETIONEPCLASSTYPE_FILE
1290 ? "File" : "<Unknown>"));
1291
1292 while (pBwMgr)
1293 {
1294 LogRel(("AIOMgr: Id: %s\n", pBwMgr->pszId));
1295 LogRel(("AIOMgr: Max: %u B/s\n", pBwMgr->cbTransferPerSecMax));
1296 LogRel(("AIOMgr: Start: %u B/s\n", pBwMgr->cbTransferPerSecStart));
1297 LogRel(("AIOMgr: Step: %u B/s\n", pBwMgr->cbTransferPerSecStep));
1298 LogRel(("AIOMgr: Endpoints:\n"));
1299
1300 pEp = pEpClass->pEndpointsHead;
1301 while (pEp)
1302 {
1303 if (pEp->pBwMgr == pBwMgr)
1304 LogRel(("AIOMgr: %s\n", pEp->pszUri));
1305
1306 pEp = pEp->pNext;
1307 }
1308
1309 pBwMgr = pBwMgr->pNext;
1310 }
1311
1312 /* Print all endpoints without assigned bandwidth groups. */
1313 pEp = pEpClass->pEndpointsHead;
1314 if (pEp)
1315 LogRel(("AIOMgr: Endpoints without assigned bandwidth groups:\n"));
1316
1317 while (pEp)
1318 {
1319 if (!pEp->pBwMgr)
1320 LogRel(("AIOMgr: %s\n", pEp->pszUri));
1321
1322 pEp = pEp->pNext;
1323 }
1324 }
1325}
1326
1327
1328/**
1329 * Tries to get a free task from the endpoint or class cache
1330 * allocating the task if it fails.
1331 *
1332 * @returns Pointer to a new and initialized task or NULL
1333 * @param pEndpoint The endpoint the task is for.
1334 * @param pvUser Opaque user data for the task.
1335 */
1336static PPDMASYNCCOMPLETIONTASK pdmR3AsyncCompletionGetTask(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, void *pvUser)
1337{
1338 PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = pEndpoint->pEpClass;
1339 PPDMASYNCCOMPLETIONTASK pTask = (PPDMASYNCCOMPLETIONTASK)RTMemCacheAlloc(pEndpointClass->hMemCacheTasks);
1340 if (RT_LIKELY(pTask))
1341 {
1342 /* Initialize common parts. */
1343 pTask->pvUser = pvUser;
1344 pTask->pEndpoint = pEndpoint;
1345 /* Clear list pointers for safety. */
1346 pTask->pPrev = NULL;
1347 pTask->pNext = NULL;
1348 pTask->tsNsStart = RTTimeNanoTS();
1349 STAM_REL_COUNTER_INC(&pEndpoint->StatIoOpsStarted);
1350 }
1351
1352 return pTask;
1353}
1354
1355
1356/**
1357 * Puts a task in one of the caches.
1358 *
1359 * @returns nothing.
1360 * @param pEndpoint The endpoint the task belongs to.
1361 * @param pTask The task to cache.
1362 */
1363static void pdmR3AsyncCompletionPutTask(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, PPDMASYNCCOMPLETIONTASK pTask)
1364{
1365 PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = pEndpoint->pEpClass;
1366 uint64_t cNsRun = RTTimeNanoTS() - pTask->tsNsStart;
1367
1368 if (RT_UNLIKELY(cNsRun >= RT_NS_10SEC))
1369 LogRel(("AsyncCompletion: Task %#p completed after %llu seconds\n", pTask, cNsRun / RT_NS_1SEC));
1370
1371 if (pEndpointClass->fGatherAdvancedStatistics)
1372 pdmR3AsyncCompletionStatisticsRecordCompletionTime(pEndpoint, cNsRun);
1373
1374 RTMemCacheFree(pEndpointClass->hMemCacheTasks, pTask);
1375}
1376
1377
1378static PPDMASYNCCOMPLETIONENDPOINT
1379pdmR3AsyncCompletionFindEndpointWithUri(PPDMASYNCCOMPLETIONEPCLASS pEndpointClass, const char *pszUri)
1380{
1381 PPDMASYNCCOMPLETIONENDPOINT pEndpoint = pEndpointClass->pEndpointsHead;
1382
1383 while (pEndpoint)
1384 {
1385 if (!RTStrCmp(pEndpoint->pszUri, pszUri))
1386 return pEndpoint;
1387
1388 pEndpoint = pEndpoint->pNext;
1389 }
1390
1391 return NULL;
1392}
1393
1394
1395/**
1396 * Opens a file as an async completion endpoint.
1397 *
1398 * @returns VBox status code.
1399 * @param ppEndpoint Where to store the opaque endpoint handle on success.
1400 * @param pszFilename Path to the file which is to be opened. (UTF-8)
1401 * @param fFlags Open flags, see grp_pdmacep_file_flags.
1402 * @param pTemplate Handle to the completion callback template to use
1403 * for this end point.
1404 */
1405VMMR3DECL(int) PDMR3AsyncCompletionEpCreateForFile(PPPDMASYNCCOMPLETIONENDPOINT ppEndpoint,
1406 const char *pszFilename, uint32_t fFlags,
1407 PPDMASYNCCOMPLETIONTEMPLATE pTemplate)
1408{
1409 LogFlowFunc((": ppEndpoint=%p pszFilename=%p{%s} fFlags=%u pTemplate=%p\n",
1410 ppEndpoint, pszFilename, pszFilename, fFlags, pTemplate));
1411
1412 /* Sanity checks. */
1413 AssertPtrReturn(ppEndpoint, VERR_INVALID_POINTER);
1414 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1415 AssertPtrReturn(pTemplate, VERR_INVALID_POINTER);
1416
1417 /* Check that the flags are valid. */
1418 AssertReturn(((~(PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_DONT_LOCK | PDMACEP_FILE_FLAGS_HOST_CACHE_ENABLED) & fFlags) == 0),
1419 VERR_INVALID_PARAMETER);
1420
1421 PVM pVM = pTemplate->pVM;
1422 PUVM pUVM = pVM->pUVM;
1423 PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = pUVM->pdm.s.apAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
1424 PPDMASYNCCOMPLETIONENDPOINT pEndpoint = NULL;
1425
1426 AssertMsg(pEndpointClass, ("File endpoint class was not initialized\n"));
1427
1428 /* Search for a already opened endpoint for this file. */
1429 pEndpoint = pdmR3AsyncCompletionFindEndpointWithUri(pEndpointClass, pszFilename);
1430 if (pEndpoint)
1431 {
1432 /* Endpoint found. */
1433 pEndpoint->cUsers++;
1434
1435 *ppEndpoint = pEndpoint;
1436 return VINF_SUCCESS;
1437 }
1438
1439 /* Create an endpoint. */
1440 int rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_ASYNC_COMPLETION,
1441 pEndpointClass->pEndpointOps->cbEndpoint,
1442 (void **)&pEndpoint);
1443 if (RT_SUCCESS(rc))
1444 {
1445 /* Initialize common parts. */
1446 pEndpoint->pNext = NULL;
1447 pEndpoint->pPrev = NULL;
1448 pEndpoint->pEpClass = pEndpointClass;
1449 pEndpoint->pTemplate = pTemplate;
1450 pEndpoint->pszUri = RTStrDup(pszFilename);
1451 pEndpoint->cUsers = 1;
1452 pEndpoint->pBwMgr = NULL;
1453
1454 if ( pEndpoint->pszUri
1455 && RT_SUCCESS(rc))
1456 {
1457 /* Call the initializer for the endpoint. */
1458 rc = pEndpointClass->pEndpointOps->pfnEpInitialize(pEndpoint, pszFilename, fFlags);
1459 if (RT_SUCCESS(rc))
1460 {
1461 if (pEndpointClass->fGatherAdvancedStatistics)
1462 rc = pdmR3AsyncCompletionStatisticsRegister(pEndpoint);
1463
1464 if (RT_SUCCESS(rc))
1465 {
1466 /* Link it into the list of endpoints. */
1467 rc = RTCritSectEnter(&pEndpointClass->CritSect);
1468 AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
1469
1470 pEndpoint->pNext = pEndpointClass->pEndpointsHead;
1471 if (pEndpointClass->pEndpointsHead)
1472 pEndpointClass->pEndpointsHead->pPrev = pEndpoint;
1473
1474 pEndpointClass->pEndpointsHead = pEndpoint;
1475 pEndpointClass->cEndpoints++;
1476
1477 rc = RTCritSectLeave(&pEndpointClass->CritSect);
1478 AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
1479
1480 /* Reference the template. */
1481 ASMAtomicIncU32(&pTemplate->cUsed);
1482
1483 *ppEndpoint = pEndpoint;
1484 LogFlowFunc((": Created endpoint for %s\n", pszFilename));
1485 return VINF_SUCCESS;
1486 }
1487
1488 if (pEndpointClass->fGatherAdvancedStatistics)
1489 pdmR3AsyncCompletionStatisticsDeregister(pEndpoint);
1490 }
1491 RTStrFree(pEndpoint->pszUri);
1492 }
1493 MMR3HeapFree(pEndpoint);
1494 }
1495
1496 LogFlowFunc((": Creation of endpoint for %s failed: rc=%Rrc\n", pszFilename, rc));
1497 return rc;
1498}
1499
1500
1501/**
1502 * Closes a endpoint waiting for any pending tasks to finish.
1503 *
1504 * @returns nothing.
1505 * @param pEndpoint Handle of the endpoint.
1506 */
1507VMMR3DECL(void) PDMR3AsyncCompletionEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1508{
1509 LogFlowFunc((": pEndpoint=%p\n", pEndpoint));
1510
1511 /* Sanity checks. */
1512 AssertReturnVoid(VALID_PTR(pEndpoint));
1513
1514 pEndpoint->cUsers--;
1515
1516 /* If the last user closed the endpoint we will free it. */
1517 if (!pEndpoint->cUsers)
1518 {
1519 PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = pEndpoint->pEpClass;
1520 PVM pVM = pEndpointClass->pVM;
1521
1522 pEndpointClass->pEndpointOps->pfnEpClose(pEndpoint);
1523
1524 /* Drop reference from the template. */
1525 ASMAtomicDecU32(&pEndpoint->pTemplate->cUsed);
1526
1527 /* Unlink the endpoint from the list. */
1528 int rc = RTCritSectEnter(&pEndpointClass->CritSect);
1529 AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
1530
1531 PPDMASYNCCOMPLETIONENDPOINT pEndpointNext = pEndpoint->pNext;
1532 PPDMASYNCCOMPLETIONENDPOINT pEndpointPrev = pEndpoint->pPrev;
1533
1534 if (pEndpointPrev)
1535 pEndpointPrev->pNext = pEndpointNext;
1536 else
1537 pEndpointClass->pEndpointsHead = pEndpointNext;
1538 if (pEndpointNext)
1539 pEndpointNext->pPrev = pEndpointPrev;
1540
1541 pEndpointClass->cEndpoints--;
1542
1543 rc = RTCritSectLeave(&pEndpointClass->CritSect);
1544 AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
1545
1546 if (pEndpointClass->fGatherAdvancedStatistics)
1547 pdmR3AsyncCompletionStatisticsDeregister(pEndpoint);
1548
1549 RTStrFree(pEndpoint->pszUri);
1550 MMR3HeapFree(pEndpoint);
1551 }
1552}
1553
1554
1555/**
1556 * Creates a read task on the given endpoint.
1557 *
1558 * @returns VBox status code.
1559 * @param pEndpoint The file endpoint to read from.
1560 * @param off Where to start reading from.
1561 * @param paSegments Scatter gather list to store the data in.
1562 * @param cSegments Number of segments in the list.
1563 * @param cbRead The overall number of bytes to read.
1564 * @param pvUser Opaque user data returned in the completion callback
1565 * upon completion of the task.
1566 * @param ppTask Where to store the task handle on success.
1567 */
1568VMMR3DECL(int) PDMR3AsyncCompletionEpRead(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1569 PCRTSGSEG paSegments, unsigned cSegments,
1570 size_t cbRead, void *pvUser,
1571 PPPDMASYNCCOMPLETIONTASK ppTask)
1572{
1573 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1574 AssertPtrReturn(paSegments, VERR_INVALID_POINTER);
1575 AssertPtrReturn(ppTask, VERR_INVALID_POINTER);
1576 AssertReturn(cSegments > 0, VERR_INVALID_PARAMETER);
1577 AssertReturn(cbRead > 0, VERR_INVALID_PARAMETER);
1578 AssertReturn(off >= 0, VERR_INVALID_PARAMETER);
1579
1580 PPDMASYNCCOMPLETIONTASK pTask;
1581
1582 pTask = pdmR3AsyncCompletionGetTask(pEndpoint, pvUser);
1583 if (!pTask)
1584 return VERR_NO_MEMORY;
1585
1586 int rc = pEndpoint->pEpClass->pEndpointOps->pfnEpRead(pTask, pEndpoint, off,
1587 paSegments, cSegments, cbRead);
1588 if (RT_SUCCESS(rc))
1589 {
1590 if (pEndpoint->pEpClass->fGatherAdvancedStatistics)
1591 pdmR3AsyncCompletionStatisticsRecordSize(pEndpoint, cbRead);
1592
1593 *ppTask = pTask;
1594 }
1595 else
1596 pdmR3AsyncCompletionPutTask(pEndpoint, pTask);
1597
1598 return rc;
1599}
1600
1601
1602/**
1603 * Creates a write task on the given endpoint.
1604 *
1605 * @returns VBox status code.
1606 * @param pEndpoint The file endpoint to write to.
1607 * @param off Where to start writing at.
1608 * @param paSegments Scatter gather list of the data to write.
1609 * @param cSegments Number of segments in the list.
1610 * @param cbWrite The overall number of bytes to write.
1611 * @param pvUser Opaque user data returned in the completion callback
1612 * upon completion of the task.
1613 * @param ppTask Where to store the task handle on success.
1614 */
1615VMMR3DECL(int) PDMR3AsyncCompletionEpWrite(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1616 PCRTSGSEG paSegments, unsigned cSegments,
1617 size_t cbWrite, void *pvUser,
1618 PPPDMASYNCCOMPLETIONTASK ppTask)
1619{
1620 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1621 AssertPtrReturn(paSegments, VERR_INVALID_POINTER);
1622 AssertPtrReturn(ppTask, VERR_INVALID_POINTER);
1623 AssertReturn(cSegments > 0, VERR_INVALID_PARAMETER);
1624 AssertReturn(cbWrite > 0, VERR_INVALID_PARAMETER);
1625 AssertReturn(off >= 0, VERR_INVALID_PARAMETER);
1626
1627 PPDMASYNCCOMPLETIONTASK pTask;
1628
1629 pTask = pdmR3AsyncCompletionGetTask(pEndpoint, pvUser);
1630 if (!pTask)
1631 return VERR_NO_MEMORY;
1632
1633 int rc = pEndpoint->pEpClass->pEndpointOps->pfnEpWrite(pTask, pEndpoint, off,
1634 paSegments, cSegments, cbWrite);
1635 if (RT_SUCCESS(rc))
1636 {
1637 if (pEndpoint->pEpClass->fGatherAdvancedStatistics)
1638 pdmR3AsyncCompletionStatisticsRecordSize(pEndpoint, cbWrite);
1639
1640 *ppTask = pTask;
1641 }
1642 else
1643 pdmR3AsyncCompletionPutTask(pEndpoint, pTask);
1644
1645 return rc;
1646}
1647
1648
1649/**
1650 * Creates a flush task on the given endpoint.
1651 *
1652 * Every read and write task initiated before the flush task is
1653 * finished upon completion of this task.
1654 *
1655 * @returns VBox status code.
1656 * @param pEndpoint The file endpoint to flush.
1657 * @param pvUser Opaque user data returned in the completion callback
1658 * upon completion of the task.
1659 * @param ppTask Where to store the task handle on success.
1660 */
1661VMMR3DECL(int) PDMR3AsyncCompletionEpFlush(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, void *pvUser, PPPDMASYNCCOMPLETIONTASK ppTask)
1662{
1663 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1664 AssertPtrReturn(ppTask, VERR_INVALID_POINTER);
1665
1666 PPDMASYNCCOMPLETIONTASK pTask;
1667
1668 pTask = pdmR3AsyncCompletionGetTask(pEndpoint, pvUser);
1669 if (!pTask)
1670 return VERR_NO_MEMORY;
1671
1672 int rc = pEndpoint->pEpClass->pEndpointOps->pfnEpFlush(pTask, pEndpoint);
1673 if (RT_SUCCESS(rc))
1674 *ppTask = pTask;
1675 else
1676 pdmR3AsyncCompletionPutTask(pEndpoint, pTask);
1677
1678 return rc;
1679}
1680
1681
1682/**
1683 * Queries the size of an endpoint.
1684 *
1685 * Not that some endpoints may not support this and will return an error
1686 * (sockets for example).
1687 *
1688 * @returns VBox status code.
1689 * @retval VERR_NOT_SUPPORTED if the endpoint does not support this operation.
1690 * @param pEndpoint The file endpoint.
1691 * @param pcbSize Where to store the size of the endpoint.
1692 */
1693VMMR3DECL(int) PDMR3AsyncCompletionEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
1694 uint64_t *pcbSize)
1695{
1696 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1697 AssertPtrReturn(pcbSize, VERR_INVALID_POINTER);
1698
1699 if (pEndpoint->pEpClass->pEndpointOps->pfnEpGetSize)
1700 return pEndpoint->pEpClass->pEndpointOps->pfnEpGetSize(pEndpoint, pcbSize);
1701 return VERR_NOT_SUPPORTED;
1702}
1703
1704
1705/**
1706 * Sets the size of an endpoint.
1707 *
1708 * Not that some endpoints may not support this and will return an error
1709 * (sockets for example).
1710 *
1711 * @returns VBox status code.
1712 * @retval VERR_NOT_SUPPORTED if the endpoint does not support this operation.
1713 * @param pEndpoint The file endpoint.
1714 * @param cbSize The size to set.
1715 *
1716 * @note PDMR3AsyncCompletionEpFlush should be called before this operation is executed.
1717 */
1718VMMR3DECL(int) PDMR3AsyncCompletionEpSetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, uint64_t cbSize)
1719{
1720 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1721
1722 if (pEndpoint->pEpClass->pEndpointOps->pfnEpSetSize)
1723 return pEndpoint->pEpClass->pEndpointOps->pfnEpSetSize(pEndpoint, cbSize);
1724 return VERR_NOT_SUPPORTED;
1725}
1726
1727
1728/**
1729 * Assigns or removes a bandwidth control manager to/from the endpoint.
1730 *
1731 * @returns VBox status code.
1732 * @param pEndpoint The endpoint.
1733 * @param pszBwMgr The identifer of the new bandwidth manager to assign
1734 * or NULL to remove the current one.
1735 */
1736VMMR3DECL(int) PDMR3AsyncCompletionEpSetBwMgr(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, const char *pszBwMgr)
1737{
1738 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1739 PPDMACBWMGR pBwMgrOld = NULL;
1740 PPDMACBWMGR pBwMgrNew = NULL;
1741
1742 int rc = VINF_SUCCESS;
1743 if (pszBwMgr)
1744 {
1745 pBwMgrNew = pdmacBwMgrFindById(pEndpoint->pEpClass, pszBwMgr);
1746 if (pBwMgrNew)
1747 pdmacBwMgrRetain(pBwMgrNew);
1748 else
1749 rc = VERR_NOT_FOUND;
1750 }
1751
1752 if (RT_SUCCESS(rc))
1753 {
1754 pBwMgrOld = ASMAtomicXchgPtrT(&pEndpoint->pBwMgr, pBwMgrNew, PPDMACBWMGR);
1755 if (pBwMgrOld)
1756 pdmacBwMgrRelease(pBwMgrOld);
1757 }
1758
1759 return rc;
1760}
1761
1762
1763/**
1764 * Cancels an async completion task.
1765 *
1766 * If you want to use this method, you have to take great create to make sure
1767 * you will never attempt cancel a task which has been completed. Since there is
1768 * no reference counting or anything on the task it self, you have to serialize
1769 * the cancelation and completion paths such that the aren't racing one another.
1770 *
1771 * @returns VBox status code
1772 * @param pTask The Task to cancel.
1773 */
1774VMMR3DECL(int) PDMR3AsyncCompletionTaskCancel(PPDMASYNCCOMPLETIONTASK pTask)
1775{
1776 NOREF(pTask);
1777 return VERR_NOT_IMPLEMENTED;
1778}
1779
1780
1781/**
1782 * Changes the limit of a bandwidth manager for file endpoints to the given value.
1783 *
1784 * @returns VBox status code.
1785 * @param pUVM The user mode VM handle.
1786 * @param pszBwMgr The identifer of the bandwidth manager to change.
1787 * @param cbMaxNew The new maximum for the bandwidth manager in bytes/sec.
1788 */
1789VMMR3DECL(int) PDMR3AsyncCompletionBwMgrSetMaxForFile(PUVM pUVM, const char *pszBwMgr, uint32_t cbMaxNew)
1790{
1791 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1792 PVM pVM = pUVM->pVM;
1793 VM_ASSERT_VALID_EXT_RETURN(pVM, VERR_INVALID_VM_HANDLE);
1794 AssertPtrReturn(pszBwMgr, VERR_INVALID_POINTER);
1795
1796 int rc = VINF_SUCCESS;
1797 PPDMASYNCCOMPLETIONEPCLASS pEpClass = pVM->pUVM->pdm.s.apAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
1798 PPDMACBWMGR pBwMgr = pdmacBwMgrFindById(pEpClass, pszBwMgr);
1799 if (pBwMgr)
1800 {
1801 /*
1802 * Set the new value for the start and max value to let the manager pick up
1803 * the new limit immediately.
1804 */
1805 ASMAtomicWriteU32(&pBwMgr->cbTransferPerSecMax, cbMaxNew);
1806 ASMAtomicWriteU32(&pBwMgr->cbTransferPerSecStart, cbMaxNew);
1807 }
1808 else
1809 rc = VERR_NOT_FOUND;
1810
1811 return rc;
1812}
1813
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