VirtualBox

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

Last change on this file since 43530 was 43530, checked in by vboxsync, 12 years ago

AsyncCompletion: STAT_* => STAT_REL_*

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