VirtualBox

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

Last change on this file since 42640 was 41800, checked in by vboxsync, 13 years ago

Doxygen.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 53.1 KB
Line 
1/* $Id: PDMAsyncCompletion.cpp 41800 2012-06-17 16:18:26Z vboxsync $ */
2/** @file
3 * PDM Async I/O - Transport data asynchronous in R3 using EMT.
4 */
5
6/*
7 * Copyright (C) 2006-2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
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 pUVM->pdm.s.apAsyncCompletionEndpointClass[pEpClassOps->enmClassType] = pEndpointClass;
851 LogFlowFunc((": Initialized endpoint class \"%s\" rc=%Rrc\n", pEpClassOps->pcszName, rc));
852 return VINF_SUCCESS;
853 }
854 }
855 RTMemCacheDestroy(pEndpointClass->hMemCacheTasks);
856 }
857 RTCritSectDelete(&pEndpointClass->CritSect);
858 }
859 MMR3HeapFree(pEndpointClass);
860 }
861
862 LogFlowFunc((": Failed to initialize endpoint class rc=%Rrc\n", rc));
863
864 return rc;
865}
866
867/**
868 * Worker terminating all endpoint classes.
869 *
870 * @returns nothing
871 * @param pEndpointClass Pointer to the endpoint class to terminate.
872 *
873 * @remarks This method ensures that any still open endpoint is closed.
874 */
875static void pdmR3AsyncCompletionEpClassTerminate(PPDMASYNCCOMPLETIONEPCLASS pEndpointClass)
876{
877 PVM pVM = pEndpointClass->pVM;
878
879 /* Close all still open endpoints. */
880 while (pEndpointClass->pEndpointsHead)
881 PDMR3AsyncCompletionEpClose(pEndpointClass->pEndpointsHead);
882
883 /* Destroy the bandwidth managers. */
884 PPDMACBWMGR pBwMgr = pEndpointClass->pBwMgrsHead;
885 while (pBwMgr)
886 {
887 PPDMACBWMGR pFree = pBwMgr;
888 pBwMgr = pBwMgr->pNext;
889 MMR3HeapFree(pFree);
890 }
891
892 /* Call the termination callback of the class. */
893 pEndpointClass->pEndpointOps->pfnTerminate(pEndpointClass);
894
895 RTMemCacheDestroy(pEndpointClass->hMemCacheTasks);
896 RTCritSectDelete(&pEndpointClass->CritSect);
897
898 /* Free the memory of the class finally and clear the entry in the class array. */
899 pVM->pUVM->pdm.s.apAsyncCompletionEndpointClass[pEndpointClass->pEndpointOps->enmClassType] = NULL;
900 MMR3HeapFree(pEndpointClass);
901}
902
903/**
904 * Initialize the async completion manager.
905 *
906 * @returns VBox status code
907 * @param pVM Pointer to the VM.
908 */
909int pdmR3AsyncCompletionInit(PVM pVM)
910{
911 LogFlowFunc((": pVM=%p\n", pVM));
912
913 VM_ASSERT_EMT(pVM);
914
915 PCFGMNODE pCfgRoot = CFGMR3GetRoot(pVM);
916 PCFGMNODE pCfgAsyncCompletion = CFGMR3GetChild(CFGMR3GetChild(pCfgRoot, "PDM"), "AsyncCompletion");
917
918 int rc = pdmR3AsyncCompletionEpClassInit(pVM, &g_PDMAsyncCompletionEndpointClassFile, pCfgAsyncCompletion);
919 LogFlowFunc((": pVM=%p rc=%Rrc\n", pVM, rc));
920 return rc;
921}
922
923/**
924 * Terminates the async completion manager.
925 *
926 * @returns VBox status code
927 * @param pVM Pointer to the VM.
928 */
929int pdmR3AsyncCompletionTerm(PVM pVM)
930{
931 LogFlowFunc((": pVM=%p\n", pVM));
932 PUVM pUVM = pVM->pUVM;
933
934 for (size_t i = 0; i < RT_ELEMENTS(pUVM->pdm.s.apAsyncCompletionEndpointClass); i++)
935 if (pUVM->pdm.s.apAsyncCompletionEndpointClass[i])
936 pdmR3AsyncCompletionEpClassTerminate(pUVM->pdm.s.apAsyncCompletionEndpointClass[i]);
937
938 return VINF_SUCCESS;
939}
940
941/**
942 * Resume worker for the async completion manager.
943 *
944 * @returns nothing.
945 * @param pVM Pointer to the VM.
946 */
947void pdmR3AsyncCompletionResume(PVM pVM)
948{
949 LogFlowFunc((": pVM=%p\n", pVM));
950 PUVM pUVM = pVM->pUVM;
951
952 /* Log the bandwidth groups and all assigned endpoints. */
953 for (size_t i = 0; i < RT_ELEMENTS(pUVM->pdm.s.apAsyncCompletionEndpointClass); i++)
954 if (pUVM->pdm.s.apAsyncCompletionEndpointClass[i])
955 {
956 PPDMASYNCCOMPLETIONEPCLASS pEpClass = pUVM->pdm.s.apAsyncCompletionEndpointClass[i];
957 PPDMACBWMGR pBwMgr = pEpClass->pBwMgrsHead;
958 PPDMASYNCCOMPLETIONENDPOINT pEp;
959
960 if (pBwMgr)
961 LogRel(("AIOMgr: Bandwidth groups for class '%s'\n", i == PDMASYNCCOMPLETIONEPCLASSTYPE_FILE
962 ? "File" : "<Unknown>"));
963
964 while (pBwMgr)
965 {
966 LogRel(("AIOMgr: Id: %s\n", pBwMgr->pszId));
967 LogRel(("AIOMgr: Max: %u B/s\n", pBwMgr->cbTransferPerSecMax));
968 LogRel(("AIOMgr: Start: %u B/s\n", pBwMgr->cbTransferPerSecStart));
969 LogRel(("AIOMgr: Step: %u B/s\n", pBwMgr->cbTransferPerSecStep));
970 LogRel(("AIOMgr: Endpoints:\n"));
971
972 pEp = pEpClass->pEndpointsHead;
973 while (pEp)
974 {
975 if (pEp->pBwMgr == pBwMgr)
976 LogRel(("AIOMgr: %s\n", pEp->pszUri));
977
978 pEp = pEp->pNext;
979 }
980
981 pBwMgr = pBwMgr->pNext;
982 }
983
984 /* Print all endpoints without assigned bandwidth groups. */
985 pEp = pEpClass->pEndpointsHead;
986 if (pEp)
987 LogRel(("AIOMgr: Endpoints without assigned bandwidth groups:\n"));
988
989 while (pEp)
990 {
991 if (!pEp->pBwMgr)
992 LogRel(("AIOMgr: %s\n", pEp->pszUri));
993
994 pEp = pEp->pNext;
995 }
996 }
997}
998
999/**
1000 * Tries to get a free task from the endpoint or class cache
1001 * allocating the task if it fails.
1002 *
1003 * @returns Pointer to a new and initialized task or NULL
1004 * @param pEndpoint The endpoint the task is for.
1005 * @param pvUser Opaque user data for the task.
1006 */
1007static PPDMASYNCCOMPLETIONTASK pdmR3AsyncCompletionGetTask(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, void *pvUser)
1008{
1009 PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = pEndpoint->pEpClass;
1010 PPDMASYNCCOMPLETIONTASK pTask = (PPDMASYNCCOMPLETIONTASK)RTMemCacheAlloc(pEndpointClass->hMemCacheTasks);
1011 if (RT_LIKELY(pTask))
1012 {
1013 /* Initialize common parts. */
1014 pTask->pvUser = pvUser;
1015 pTask->pEndpoint = pEndpoint;
1016 /* Clear list pointers for safety. */
1017 pTask->pPrev = NULL;
1018 pTask->pNext = NULL;
1019 pTask->tsNsStart = RTTimeNanoTS();
1020#ifdef VBOX_WITH_STATISTICS
1021 STAM_COUNTER_INC(&pEndpoint->StatIoOpsStarted);
1022#endif
1023 }
1024
1025 return pTask;
1026}
1027
1028/**
1029 * Puts a task in one of the caches.
1030 *
1031 * @returns nothing.
1032 * @param pEndpoint The endpoint the task belongs to.
1033 * @param pTask The task to cache.
1034 */
1035static void pdmR3AsyncCompletionPutTask(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, PPDMASYNCCOMPLETIONTASK pTask)
1036{
1037 PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = pEndpoint->pEpClass;
1038 uint64_t cNsRun = RTTimeNanoTS() - pTask->tsNsStart;
1039
1040 if (RT_UNLIKELY(cNsRun >= RT_NS_10SEC))
1041 LogRel(("AsyncCompletion: Task %#p completed after %llu seconds\n", pTask, cNsRun / RT_NS_1SEC));
1042
1043#ifdef VBOX_WITH_STATISTICS
1044 PSTAMCOUNTER pStatCounter;
1045 if (cNsRun < RT_NS_1US)
1046 pStatCounter = &pEndpoint->StatTaskRunTimesNs[cNsRun / (RT_NS_1US / 10)];
1047 else if (cNsRun < RT_NS_1MS)
1048 pStatCounter = &pEndpoint->StatTaskRunTimesUs[cNsRun / (RT_NS_1MS / 10)];
1049 else if (cNsRun < RT_NS_1SEC)
1050 pStatCounter = &pEndpoint->StatTaskRunTimesMs[cNsRun / (RT_NS_1SEC / 10)];
1051 else if (cNsRun < RT_NS_1SEC_64*100)
1052 pStatCounter = &pEndpoint->StatTaskRunTimesSec[cNsRun / (RT_NS_1SEC_64*100 / 10)];
1053 else
1054 pStatCounter = &pEndpoint->StatTaskRunOver100Sec;
1055 STAM_COUNTER_INC(pStatCounter);
1056
1057 STAM_COUNTER_INC(&pEndpoint->StatIoOpsCompleted);
1058 pEndpoint->cIoOpsCompleted++;
1059 uint64_t tsMsCur = RTTimeMilliTS();
1060 uint64_t tsInterval = tsMsCur - pEndpoint->tsIntervalStartMs;
1061 if (tsInterval >= 1000)
1062 {
1063 pEndpoint->StatIoOpsPerSec.c = pEndpoint->cIoOpsCompleted / (tsInterval / 1000);
1064 pEndpoint->tsIntervalStartMs = tsMsCur;
1065 pEndpoint->cIoOpsCompleted = 0;
1066 }
1067#endif /* VBOX_WITH_STATISTICS */
1068
1069 RTMemCacheFree(pEndpointClass->hMemCacheTasks, pTask);
1070}
1071
1072static PPDMASYNCCOMPLETIONENDPOINT pdmR3AsyncCompletionFindEndpointWithUri(PPDMASYNCCOMPLETIONEPCLASS pEndpointClass,
1073 const char *pszUri)
1074{
1075 PPDMASYNCCOMPLETIONENDPOINT pEndpoint = pEndpointClass->pEndpointsHead;
1076
1077 while (pEndpoint)
1078 {
1079 if (!RTStrCmp(pEndpoint->pszUri, pszUri))
1080 return pEndpoint;
1081
1082 pEndpoint = pEndpoint->pNext;
1083 }
1084
1085 return NULL;
1086}
1087
1088VMMR3DECL(int) PDMR3AsyncCompletionEpCreateForFile(PPPDMASYNCCOMPLETIONENDPOINT ppEndpoint,
1089 const char *pszFilename, uint32_t fFlags,
1090 PPDMASYNCCOMPLETIONTEMPLATE pTemplate)
1091{
1092 LogFlowFunc((": ppEndpoint=%p pszFilename=%p{%s} fFlags=%u pTemplate=%p\n",
1093 ppEndpoint, pszFilename, pszFilename, fFlags, pTemplate));
1094
1095 /* Sanity checks. */
1096 AssertPtrReturn(ppEndpoint, VERR_INVALID_POINTER);
1097 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
1098 AssertPtrReturn(pTemplate, VERR_INVALID_POINTER);
1099
1100 /* Check that the flags are valid. */
1101 AssertReturn(((~(PDMACEP_FILE_FLAGS_READ_ONLY | PDMACEP_FILE_FLAGS_DONT_LOCK | PDMACEP_FILE_FLAGS_HOST_CACHE_ENABLED) & fFlags) == 0),
1102 VERR_INVALID_PARAMETER);
1103
1104 PVM pVM = pTemplate->pVM;
1105 PUVM pUVM = pVM->pUVM;
1106 PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = pUVM->pdm.s.apAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
1107 PPDMASYNCCOMPLETIONENDPOINT pEndpoint = NULL;
1108
1109 AssertMsg(pEndpointClass, ("File endpoint class was not initialized\n"));
1110
1111 /* Search for a already opened endpoint for this file. */
1112 pEndpoint = pdmR3AsyncCompletionFindEndpointWithUri(pEndpointClass, pszFilename);
1113 if (pEndpoint)
1114 {
1115 /* Endpoint found. */
1116 pEndpoint->cUsers++;
1117
1118 *ppEndpoint = pEndpoint;
1119 return VINF_SUCCESS;
1120 }
1121
1122 /* Create an endpoint. */
1123 int rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_ASYNC_COMPLETION,
1124 pEndpointClass->pEndpointOps->cbEndpoint,
1125 (void **)&pEndpoint);
1126 if (RT_SUCCESS(rc))
1127 {
1128
1129 /* Initialize common parts. */
1130 pEndpoint->pNext = NULL;
1131 pEndpoint->pPrev = NULL;
1132 pEndpoint->pEpClass = pEndpointClass;
1133 pEndpoint->pTemplate = pTemplate;
1134 pEndpoint->pszUri = RTStrDup(pszFilename);
1135 pEndpoint->cUsers = 1;
1136 pEndpoint->pBwMgr = NULL;
1137
1138 if ( pEndpoint->pszUri
1139 && RT_SUCCESS(rc))
1140 {
1141 /* Call the initializer for the endpoint. */
1142 rc = pEndpointClass->pEndpointOps->pfnEpInitialize(pEndpoint, pszFilename, fFlags);
1143 if (RT_SUCCESS(rc))
1144 {
1145 /* Link it into the list of endpoints. */
1146 rc = RTCritSectEnter(&pEndpointClass->CritSect);
1147 AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
1148
1149 pEndpoint->pNext = pEndpointClass->pEndpointsHead;
1150 if (pEndpointClass->pEndpointsHead)
1151 pEndpointClass->pEndpointsHead->pPrev = pEndpoint;
1152
1153 pEndpointClass->pEndpointsHead = pEndpoint;
1154 pEndpointClass->cEndpoints++;
1155
1156 rc = RTCritSectLeave(&pEndpointClass->CritSect);
1157 AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
1158
1159 /* Reference the template. */
1160 ASMAtomicIncU32(&pTemplate->cUsed);
1161
1162#ifdef VBOX_WITH_STATISTICS
1163 /* Init the statistics part */
1164 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesNs); i++)
1165 {
1166 rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesNs[i], STAMTYPE_COUNTER,
1167 STAMVISIBILITY_USED,
1168 STAMUNIT_OCCURENCES,
1169 "Nanosecond resolution runtime statistics",
1170 "/PDM/AsyncCompletion/File/%s/TaskRun1Ns-%u-%u",
1171 RTPathFilename(pEndpoint->pszUri),
1172 i*100, i*100+100-1);
1173 if (RT_FAILURE(rc))
1174 break;
1175 }
1176
1177 if (RT_SUCCESS(rc))
1178 {
1179 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesUs); i++)
1180 {
1181 rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesUs[i], STAMTYPE_COUNTER,
1182 STAMVISIBILITY_USED,
1183 STAMUNIT_OCCURENCES,
1184 "Microsecond resolution runtime statistics",
1185 "/PDM/AsyncCompletion/File/%s/TaskRun2MicroSec-%u-%u",
1186 RTPathFilename(pEndpoint->pszUri),
1187 i*100, i*100+100-1);
1188 if (RT_FAILURE(rc))
1189 break;
1190 }
1191 }
1192
1193 if (RT_SUCCESS(rc))
1194 {
1195 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMs); i++)
1196 {
1197 rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesMs[i], STAMTYPE_COUNTER,
1198 STAMVISIBILITY_USED,
1199 STAMUNIT_OCCURENCES,
1200 "Milliseconds resolution runtime statistics",
1201 "/PDM/AsyncCompletion/File/%s/TaskRun3Ms-%u-%u",
1202 RTPathFilename(pEndpoint->pszUri),
1203 i*100, i*100+100-1);
1204 if (RT_FAILURE(rc))
1205 break;
1206 }
1207 }
1208
1209 if (RT_SUCCESS(rc))
1210 {
1211 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMs); i++)
1212 {
1213 rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunTimesSec[i], STAMTYPE_COUNTER,
1214 STAMVISIBILITY_USED,
1215 STAMUNIT_OCCURENCES,
1216 "Second resolution runtime statistics",
1217 "/PDM/AsyncCompletion/File/%s/TaskRun4Sec-%u-%u",
1218 RTPathFilename(pEndpoint->pszUri),
1219 i*10, i*10+10-1);
1220 if (RT_FAILURE(rc))
1221 break;
1222 }
1223 }
1224
1225 if (RT_SUCCESS(rc))
1226 {
1227 rc = STAMR3RegisterF(pVM, &pEndpoint->StatTaskRunOver100Sec, STAMTYPE_COUNTER,
1228 STAMVISIBILITY_USED,
1229 STAMUNIT_OCCURENCES,
1230 "Tasks which ran more than 100sec",
1231 "/PDM/AsyncCompletion/File/%s/TaskRunSecGreater100Sec",
1232 RTPathFilename(pEndpoint->pszUri));
1233 }
1234
1235 if (RT_SUCCESS(rc))
1236 {
1237 rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsPerSec, STAMTYPE_COUNTER,
1238 STAMVISIBILITY_ALWAYS,
1239 STAMUNIT_OCCURENCES,
1240 "Processed I/O operations per second",
1241 "/PDM/AsyncCompletion/File/%s/IoOpsPerSec",
1242 RTPathFilename(pEndpoint->pszUri));
1243 }
1244
1245 if (RT_SUCCESS(rc))
1246 {
1247 rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsStarted, STAMTYPE_COUNTER,
1248 STAMVISIBILITY_ALWAYS,
1249 STAMUNIT_OCCURENCES,
1250 "Started I/O operations for this endpoint",
1251 "/PDM/AsyncCompletion/File/%s/IoOpsStarted",
1252 RTPathFilename(pEndpoint->pszUri));
1253 }
1254
1255 if (RT_SUCCESS(rc))
1256 {
1257 rc = STAMR3RegisterF(pVM, &pEndpoint->StatIoOpsCompleted, STAMTYPE_COUNTER,
1258 STAMVISIBILITY_ALWAYS,
1259 STAMUNIT_OCCURENCES,
1260 "Completed I/O operations for this endpoint",
1261 "/PDM/AsyncCompletion/File/%s/IoOpsCompleted",
1262 RTPathFilename(pEndpoint->pszUri));
1263 }
1264 /** @todo why bother maintaing rc when it's just ignored /
1265 logged and not returned? */
1266
1267 pEndpoint->tsIntervalStartMs = RTTimeMilliTS();
1268#endif
1269
1270 *ppEndpoint = pEndpoint;
1271
1272 LogFlowFunc((": Created endpoint for %s: rc=%Rrc\n", pszFilename, rc));
1273 return VINF_SUCCESS;
1274 }
1275 RTStrFree(pEndpoint->pszUri);
1276 }
1277 MMR3HeapFree(pEndpoint);
1278 }
1279
1280 LogFlowFunc((": Creation of endpoint for %s failed: rc=%Rrc\n", pszFilename, rc));
1281 return rc;
1282}
1283
1284VMMR3DECL(void) PDMR3AsyncCompletionEpClose(PPDMASYNCCOMPLETIONENDPOINT pEndpoint)
1285{
1286 LogFlowFunc((": pEndpoint=%p\n", pEndpoint));
1287
1288 /* Sanity checks. */
1289 AssertReturnVoid(VALID_PTR(pEndpoint));
1290
1291 pEndpoint->cUsers--;
1292
1293 /* If the last user closed the endpoint we will free it. */
1294 if (!pEndpoint->cUsers)
1295 {
1296 PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = pEndpoint->pEpClass;
1297 pEndpointClass->pEndpointOps->pfnEpClose(pEndpoint);
1298
1299 /* Drop reference from the template. */
1300 ASMAtomicDecU32(&pEndpoint->pTemplate->cUsed);
1301
1302 /* Unlink the endpoint from the list. */
1303 int rc = RTCritSectEnter(&pEndpointClass->CritSect);
1304 AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
1305
1306 PPDMASYNCCOMPLETIONENDPOINT pEndpointNext = pEndpoint->pNext;
1307 PPDMASYNCCOMPLETIONENDPOINT pEndpointPrev = pEndpoint->pPrev;
1308
1309 if (pEndpointPrev)
1310 pEndpointPrev->pNext = pEndpointNext;
1311 else
1312 pEndpointClass->pEndpointsHead = pEndpointNext;
1313 if (pEndpointNext)
1314 pEndpointNext->pPrev = pEndpointPrev;
1315
1316 pEndpointClass->cEndpoints--;
1317
1318 rc = RTCritSectLeave(&pEndpointClass->CritSect);
1319 AssertMsg(RT_SUCCESS(rc), ("Failed to enter critical section rc=%Rrc\n", rc));
1320
1321#ifdef VBOX_WITH_STATISTICS
1322 /* Deregister the statistics part */
1323 PVM pVM = pEndpointClass->pVM;
1324
1325 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesNs); i++)
1326 STAMR3Deregister(pVM, &pEndpoint->StatTaskRunTimesNs[i]);
1327 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesUs); i++)
1328 STAMR3Deregister(pVM, &pEndpoint->StatTaskRunTimesUs[i]);
1329 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMs); i++)
1330 STAMR3Deregister(pVM, &pEndpoint->StatTaskRunTimesMs[i]);
1331 for (unsigned i = 0; i < RT_ELEMENTS(pEndpoint->StatTaskRunTimesMs); i++)
1332 STAMR3Deregister(pVM, &pEndpoint->StatTaskRunTimesSec[i]);
1333
1334 STAMR3Deregister(pVM, &pEndpoint->StatTaskRunOver100Sec);
1335 STAMR3Deregister(pVM, &pEndpoint->StatIoOpsPerSec);
1336 STAMR3Deregister(pVM, &pEndpoint->StatIoOpsStarted);
1337 STAMR3Deregister(pVM, &pEndpoint->StatIoOpsCompleted);
1338#endif
1339
1340 RTStrFree(pEndpoint->pszUri);
1341 MMR3HeapFree(pEndpoint);
1342 }
1343}
1344
1345VMMR3DECL(int) PDMR3AsyncCompletionEpRead(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1346 PCRTSGSEG paSegments, unsigned cSegments,
1347 size_t cbRead, void *pvUser,
1348 PPPDMASYNCCOMPLETIONTASK ppTask)
1349{
1350 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1351 AssertPtrReturn(paSegments, VERR_INVALID_POINTER);
1352 AssertPtrReturn(ppTask, VERR_INVALID_POINTER);
1353 AssertReturn(cSegments > 0, VERR_INVALID_PARAMETER);
1354 AssertReturn(cbRead > 0, VERR_INVALID_PARAMETER);
1355 AssertReturn(off >= 0, VERR_INVALID_PARAMETER);
1356
1357 PPDMASYNCCOMPLETIONTASK pTask;
1358
1359 pTask = pdmR3AsyncCompletionGetTask(pEndpoint, pvUser);
1360 if (!pTask)
1361 return VERR_NO_MEMORY;
1362
1363 int rc = pEndpoint->pEpClass->pEndpointOps->pfnEpRead(pTask, pEndpoint, off,
1364 paSegments, cSegments, cbRead);
1365 if (RT_SUCCESS(rc))
1366 *ppTask = pTask;
1367 else
1368 pdmR3AsyncCompletionPutTask(pEndpoint, pTask);
1369
1370 return rc;
1371}
1372
1373VMMR3DECL(int) PDMR3AsyncCompletionEpWrite(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, RTFOFF off,
1374 PCRTSGSEG paSegments, unsigned cSegments,
1375 size_t cbWrite, void *pvUser,
1376 PPPDMASYNCCOMPLETIONTASK ppTask)
1377{
1378 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1379 AssertPtrReturn(paSegments, VERR_INVALID_POINTER);
1380 AssertPtrReturn(ppTask, VERR_INVALID_POINTER);
1381 AssertReturn(cSegments > 0, VERR_INVALID_PARAMETER);
1382 AssertReturn(cbWrite > 0, VERR_INVALID_PARAMETER);
1383 AssertReturn(off >= 0, VERR_INVALID_PARAMETER);
1384
1385 PPDMASYNCCOMPLETIONTASK pTask;
1386
1387 pTask = pdmR3AsyncCompletionGetTask(pEndpoint, pvUser);
1388 if (!pTask)
1389 return VERR_NO_MEMORY;
1390
1391 int rc = pEndpoint->pEpClass->pEndpointOps->pfnEpWrite(pTask, pEndpoint, off,
1392 paSegments, cSegments, cbWrite);
1393 if (RT_SUCCESS(rc))
1394 {
1395 *ppTask = pTask;
1396 }
1397 else
1398 pdmR3AsyncCompletionPutTask(pEndpoint, pTask);
1399
1400 return rc;
1401}
1402
1403VMMR3DECL(int) PDMR3AsyncCompletionEpFlush(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
1404 void *pvUser,
1405 PPPDMASYNCCOMPLETIONTASK ppTask)
1406{
1407 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1408 AssertPtrReturn(ppTask, VERR_INVALID_POINTER);
1409
1410 PPDMASYNCCOMPLETIONTASK pTask;
1411
1412 pTask = pdmR3AsyncCompletionGetTask(pEndpoint, pvUser);
1413 if (!pTask)
1414 return VERR_NO_MEMORY;
1415
1416 int rc = pEndpoint->pEpClass->pEndpointOps->pfnEpFlush(pTask, pEndpoint);
1417 if (RT_SUCCESS(rc))
1418 *ppTask = pTask;
1419 else
1420 pdmR3AsyncCompletionPutTask(pEndpoint, pTask);
1421
1422 return rc;
1423}
1424
1425VMMR3DECL(int) PDMR3AsyncCompletionEpGetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
1426 uint64_t *pcbSize)
1427{
1428 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1429 AssertPtrReturn(pcbSize, VERR_INVALID_POINTER);
1430
1431 if (pEndpoint->pEpClass->pEndpointOps->pfnEpGetSize)
1432 return pEndpoint->pEpClass->pEndpointOps->pfnEpGetSize(pEndpoint, pcbSize);
1433 return VERR_NOT_SUPPORTED;
1434}
1435
1436VMMR3DECL(int) PDMR3AsyncCompletionEpSetSize(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
1437 uint64_t cbSize)
1438{
1439 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1440
1441 if (pEndpoint->pEpClass->pEndpointOps->pfnEpSetSize)
1442 return pEndpoint->pEpClass->pEndpointOps->pfnEpSetSize(pEndpoint, cbSize);
1443 return VERR_NOT_SUPPORTED;
1444}
1445
1446VMMR3DECL(int) PDMR3AsyncCompletionEpSetBwMgr(PPDMASYNCCOMPLETIONENDPOINT pEndpoint,
1447 const char *pcszBwMgr)
1448{
1449 AssertPtrReturn(pEndpoint, VERR_INVALID_POINTER);
1450 PPDMACBWMGR pBwMgrOld = NULL;
1451 PPDMACBWMGR pBwMgrNew = NULL;
1452
1453 int rc = VINF_SUCCESS;
1454 if (pcszBwMgr)
1455 {
1456 pBwMgrNew = pdmacBwMgrFindById(pEndpoint->pEpClass, pcszBwMgr);
1457 if (pBwMgrNew)
1458 pdmacBwMgrRef(pBwMgrNew);
1459 else
1460 rc = VERR_NOT_FOUND;
1461 }
1462
1463 if (RT_SUCCESS(rc))
1464 {
1465 pBwMgrOld = ASMAtomicXchgPtrT(&pEndpoint->pBwMgr, pBwMgrNew, PPDMACBWMGR);
1466 if (pBwMgrOld)
1467 pdmacBwMgrUnref(pBwMgrOld);
1468 }
1469
1470 return rc;
1471}
1472
1473VMMR3DECL(int) PDMR3AsyncCompletionTaskCancel(PPDMASYNCCOMPLETIONTASK pTask)
1474{
1475 NOREF(pTask);
1476 return VERR_NOT_IMPLEMENTED;
1477}
1478
1479VMMR3DECL(int) PDMR3AsyncCompletionBwMgrSetMaxForFile(PVM pVM, const char *pcszBwMgr, uint32_t cbMaxNew)
1480{
1481 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
1482 AssertPtrReturn(pcszBwMgr, VERR_INVALID_POINTER);
1483
1484 int rc = VINF_SUCCESS;
1485 PPDMASYNCCOMPLETIONEPCLASS pEpClass = pVM->pUVM->pdm.s.apAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
1486 PPDMACBWMGR pBwMgr = pdmacBwMgrFindById(pEpClass, pcszBwMgr);
1487 if (pBwMgr)
1488 {
1489 /*
1490 * Set the new value for the start and max value to let the manager pick up
1491 * the new limit immediately.
1492 */
1493 ASMAtomicWriteU32(&pBwMgr->cbTransferPerSecMax, cbMaxNew);
1494 ASMAtomicWriteU32(&pBwMgr->cbTransferPerSecStart, cbMaxNew);
1495 }
1496 else
1497 rc = VERR_NOT_FOUND;
1498
1499 return rc;
1500}
1501
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