PDMAsyncCompletion.cpp revision 22ec733a5e041fcdfe02fce2eafc9faf8b0077dd
/* $Id$ */
/** @file
* PDM Async I/O - Transport data asynchronous in R3 using EMT.
*/
/*
* Copyright (C) 2006-2008 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "PDMInternal.h"
#include <iprt/critsect.h>
#include <VBox/pdmasynccompletion.h>
#include "PDMAsyncCompletionInternal.h"
/**
* Async I/O type.
*/
typedef enum PDMASYNCCOMPLETIONTEMPLATETYPE
{
/** Device . */
/** Driver consumer. */
/** Internal consumer. */
/** Usb consumer. */
/**
* PDM Async I/O template.
*/
typedef struct PDMASYNCCOMPLETIONTEMPLATE
{
/** Pointer to the next template in the list. */
/** Pointer to the previous template in the list. */
/** Type specific data. */
union
{
/** PDMASYNCCOMPLETIONTEMPLATETYPE_DEV */
struct
{
/** Pointer to consumer function. */
/** Pointer to the device instance owning the template. */
} Dev;
/** PDMASYNCCOMPLETIONTEMPLATETYPE_DRV */
struct
{
/** Pointer to consumer function. */
/** Pointer to the driver instance owning the template. */
/** User agument given during template creation.
* This is only here to make things much easier
* for DrVVD. */
void *pvTemplateUser;
} Drv;
/** PDMASYNCCOMPLETIONTEMPLATETYPE_INTERNAL */
struct
{
/** Pointer to consumer function. */
/** Pointer to user data. */
} Int;
/** PDMASYNCCOMPLETIONTEMPLATETYPE_USB */
struct
{
/** Pointer to consumer function. */
/** Pointer to the usb instance owning the template. */
} Usb;
} u;
/** Template type. */
/** Pointer to the VM. */
/** Use count of the template. */
static void pdmR3AsyncCompletionPutTask(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, PPDMASYNCCOMPLETIONTASK pTask);
/**
* Internal worker for the creation apis
*
* @returns VBox status.
* @param pVM VM handle.
* @param ppTemplate Where to store the template handle.
*/
static int pdmR3AsyncCompletionTemplateCreate(PVM pVM, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate, PDMASYNCCOMPLETIONTEMPLATETYPE enmType)
{
int rc = VINF_SUCCESS;
if (ppTemplate == NULL)
{
AssertMsgFailed(("ppTemplate is NULL\n"));
return VERR_INVALID_PARAMETER;
}
rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_ASYNC_COMPLETION, sizeof(PDMASYNCCOMPLETIONTEMPLATE), (void **)&pTemplate);
if (RT_FAILURE(rc))
return rc;
/*
* Initialize fields.
*/
/*
* Add template to the global VM template list.
*/
*ppTemplate = pTemplate;
return VINF_SUCCESS;
}
/**
* Creates a async completion template for a device instance.
*
* The template is used when creating new completion tasks.
*
* @returns VBox status code.
* @param pVM Pointer to the shared VM structure.
* @param pDevIns The device instance.
* @param ppTemplate Where to store the template pointer on success.
* @param pfnCompleted The completion callback routine.
* @param pszDesc Description.
*/
VMMR3DECL(int) PDMR3AsyncCompletionTemplateCreateDevice(PVM pVM, PPDMDEVINS pDevIns, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate, PFNPDMASYNCCOMPLETEDEV pfnCompleted, const char *pszDesc)
{
LogFlow(("%s: pDevIns=%p ppTemplate=%p pfnCompleted=%p pszDesc=%s\n",
/*
* Validate input.
*/
if (!pfnCompleted)
{
AssertMsgFailed(("No completion callback!\n"));
return VERR_INVALID_PARAMETER;
}
if (!ppTemplate)
{
AssertMsgFailed(("Template pointer is NULL!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Create the template.
*/
if (RT_SUCCESS(rc))
{
*ppTemplate = pTemplate;
Log(("PDM: Created device template %p: pfnCompleted=%p pDevIns=%p\n",
}
return rc;
}
/**
* Creates a async completion template for a driver instance.
*
* The template is used when creating new completion tasks.
*
* @returns VBox status code.
* @param pVM Pointer to the shared VM structure.
* @param pDrvIns The driver instance.
* @param ppTemplate Where to store the template pointer on success.
* @param pfnCompleted The completion callback routine.
* @param pvTemplateUser Template user argument
* @param pszDesc Description.
*/
VMMR3DECL(int) PDMR3AsyncCompletionTemplateCreateDriver(PVM pVM, PPDMDRVINS pDrvIns, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate, PFNPDMASYNCCOMPLETEDRV pfnCompleted, void *pvTemplateUser, const char *pszDesc)
{
LogFlow(("%s: pDrvIns=%p ppTemplate=%p pfnCompleted=%p pszDesc=%s\n",
/*
* Validate input.
*/
if (!pfnCompleted)
{
AssertMsgFailed(("No completion callback!\n"));
return VERR_INVALID_PARAMETER;
}
if (!ppTemplate)
{
AssertMsgFailed(("Template pointer is NULL!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Create the template.
*/
if (RT_SUCCESS(rc))
{
*ppTemplate = pTemplate;
Log(("PDM: Created driver template %p: pfnCompleted=%p pDrvIns=%p\n",
}
return rc;
}
/**
* Creates a async completion template for a USB device instance.
*
* The template is used when creating new completion tasks.
*
* @returns VBox status code.
* @param pVM Pointer to the shared VM structure.
* @param pUsbIns The USB device instance.
* @param ppTemplate Where to store the template pointer on success.
* @param pfnCompleted The completion callback routine.
* @param pszDesc Description.
*/
VMMR3DECL(int) PDMR3AsyncCompletionTemplateCreateUsb(PVM pVM, PPDMUSBINS pUsbIns, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate, PFNPDMASYNCCOMPLETEUSB pfnCompleted, const char *pszDesc)
{
LogFlow(("%s: pUsbIns=%p ppTemplate=%p pfnCompleted=%p pszDesc=%s\n",
/*
* Validate input.
*/
if (!pfnCompleted)
{
AssertMsgFailed(("No completion callback!\n"));
return VERR_INVALID_PARAMETER;
}
if (!ppTemplate)
{
AssertMsgFailed(("Template pointer is NULL!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Create the template.
*/
if (RT_SUCCESS(rc))
{
*ppTemplate = pTemplate;
Log(("PDM: Created usb template %p: pfnCompleted=%p pDevIns=%p\n",
}
return rc;
}
/**
* Creates a async completion template for internally by the VMM.
*
* The template is used when creating new completion tasks.
*
* @returns VBox status code.
* @param pVM Pointer to the shared VM structure.
* @param ppTemplate Where to store the template pointer on success.
* @param pfnCompleted The completion callback routine.
* @param pvUser2 The 2nd user argument for the callback.
* @param pszDesc Description.
*/
VMMR3DECL(int) PDMR3AsyncCompletionTemplateCreateInternal(PVM pVM, PPPDMASYNCCOMPLETIONTEMPLATE ppTemplate, PFNPDMASYNCCOMPLETEINT pfnCompleted, void *pvUser2, const char *pszDesc)
{
LogFlow(("%s: ppTemplate=%p pfnCompleted=%p pvUser2=%p pszDesc=%s\n",
/*
* Validate input.
*/
if (!pfnCompleted)
{
AssertMsgFailed(("No completion callback!\n"));
return VERR_INVALID_PARAMETER;
}
if (!ppTemplate)
{
AssertMsgFailed(("Template pointer is NULL!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Create the template.
*/
int rc = pdmR3AsyncCompletionTemplateCreate(pVM, &pTemplate, PDMASYNCCOMPLETIONTEMPLATETYPE_INTERNAL);
if (RT_SUCCESS(rc))
{
*ppTemplate = pTemplate;
Log(("PDM: Created internal template %p: pfnCompleted=%p pvUser2=%p\n",
}
return rc;
}
/**
* Destroys the specified async completion template.
*
* @returns VBox status codes:
* @retval VINF_SUCCESS on success.
* @retval VERR_PDM_ASYNC_TEMPLATE_BUSY if the template is still in use.
*
* @param pTemplate The template in question.
*/
{
if (!pTemplate)
{
AssertMsgFailed(("pTemplate is NULL!\n"));
return VERR_INVALID_PARAMETER;
}
/*
* Check if the template is still used.
*/
{
AssertMsgFailed(("Template is still in use\n"));
return VERR_PDM_ASYNC_TEMPLATE_BUSY;
}
/*
* Unlink the template from the list.
*/
if (pPrev)
else
if (pNext)
/*
* Free the template.
*/
return VINF_SUCCESS;
}
/**
* Destroys all the specified async completion templates for the given device instance.
*
* @returns VBox status codes:
* @retval VINF_SUCCESS on success.
* @retval VERR_PDM_ASYNC_TEMPLATE_BUSY if one or more of the templates are still in use.
*
* @param pVM Pointer to the shared VM structure.
* @param pDevIns The device instance.
*/
{
/*
* Validate input.
*/
if (!pDevIns)
return VERR_INVALID_PARAMETER;
/*
* Unlink it.
*/
while (pTemplate)
{
{
if (RT_FAILURE(rc))
return rc;
}
else
}
return VINF_SUCCESS;
}
/**
* Destroys all the specified async completion templates for the given driver instance.
*
* @returns VBox status codes:
* @retval VINF_SUCCESS on success.
* @retval VERR_PDM_ASYNC_TEMPLATE_BUSY if one or more of the templates are still in use.
*
* @param pVM Pointer to the shared VM structure.
* @param pDrvIns The driver instance.
*/
{
/*
* Validate input.
*/
if (!pDrvIns)
return VERR_INVALID_PARAMETER;
/*
* Unlink it.
*/
while (pTemplate)
{
{
if (RT_FAILURE(rc))
return rc;
}
else
}
return VINF_SUCCESS;
}
/**
* Destroys all the specified async completion templates for the given USB device instance.
*
* @returns VBox status codes:
* @retval VINF_SUCCESS on success.
* @retval VERR_PDM_ASYNC_TEMPLATE_BUSY if one or more of the templates are still in use.
*
* @param pVM Pointer to the shared VM structure.
* @param pUsbIns The USB device instance.
*/
{
/*
* Validate input.
*/
if (!pUsbIns)
return VERR_INVALID_PARAMETER;
/*
* Unlink it.
*/
while (pTemplate)
{
{
if (RT_FAILURE(rc))
return rc;
}
else
}
return VINF_SUCCESS;
}
void pdmR3AsyncCompletionCompleteTask(PPDMASYNCCOMPLETIONTASK pTask, int rc, bool fCallCompletionHandler)
{
LogFlow(("%s: pTask=%#p fCallCompletionHandler=%RTbool\n", __FUNCTION__, pTask, fCallCompletionHandler));
{
{
{
break;
}
{
pTemplate->u.Drv.pfnCompleted(pTemplate->u.Drv.pDrvIns, pTemplate->u.Drv.pvTemplateUser, pTask->pvUser, rc);
break;
}
{
break;
}
{
break;
}
default:
AssertMsgFailed(("Unknown template type!\n"));
}
}
}
/**
* Worker initializing a endpoint class.
*
* @returns VBox statis code.
* @param pVM Pointer to the shared VM instance data.
* @param pEpClass Pointer to the endpoint class structure.
* @param pCfgHandle Pointer to the the CFGM tree.
*/
int pdmR3AsyncCompletionEpClassInit(PVM pVM, PCPDMASYNCCOMPLETIONEPCLASSOPS pEpClassOps, PCFGMNODE pCfgHandle)
{
int rc = VINF_SUCCESS;
/* Validate input. */
if ( !pEpClassOps
/* Allocate global class data. */
(void **)&pEndpointClass);
if (RT_SUCCESS(rc))
{
/* Initialize common data. */
if (RT_SUCCESS(rc))
{
/* Create task cache */
if (RT_SUCCESS(rc))
{
/* Call the specific endpoint class initializer. */
if (RT_SUCCESS(rc))
{
("Endpoint class was already initialized\n"));
return VINF_SUCCESS;
}
}
}
}
return rc;
}
/**
* Worker terminating all endpoint classes.
*
* @returns nothing
* @param pEndpointClass Pointer to the endpoint class to terminate.
*
* @remarks This method ensures that any still open endpoint is closed.
*/
{
int rc = VINF_SUCCESS;
/* Close all still open endpoints. */
while (pEndpointClass->pEndpointsHead)
/* Call the termination callback of the class. */
/* Free the memory of the class finally and clear the entry in the class array. */
}
/**
* Initialize the async completion manager.
*
* @returns VBox status code
* @param pVM Pointer to the shared VM structure.
*/
{
int rc = VINF_SUCCESS;
do
{
/* Allocate array for global class data. */
if (RT_FAILURE(rc))
break;
rc = pdmR3AsyncCompletionEpClassInit(pVM, &g_PDMAsyncCompletionEndpointClassFile, pCfgAsyncCompletion);
if (RT_FAILURE(rc))
break;
/* Put other classes here. */
} while (0);
return rc;
}
/**
* Terminates the async completion manager.
*
* @returns VBox status code
* @param pVM Pointer to the shared VM structure.
*/
{
{
pdmR3AsyncCompletionEpClassTerminate(pVM->pdm.s.papAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE]);
}
return VINF_SUCCESS;
}
/**
* Tries to get a free task from the endpoint or class cache
* allocating the task if it fails.
*
* @returns Pointer to a new and initialized task or NULL
* @param pEndpoint The endpoint the task is for.
* @param pvUser Opaque user data for the task.
*/
static PPDMASYNCCOMPLETIONTASK pdmR3AsyncCompletionGetTask(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, void *pvUser)
{
{
/* Get ID of the task. */
/* Initialize common parts. */
/* Clear list pointers for safety. */
#ifdef VBOX_WITH_STATISTICS
#endif
}
return pTask;
}
/**
* Puts a task in one of the caches.
*
* @returns nothing.
* @param pEndpoint The endpoint the task belongs to.
* @param pTask The task to cache.
*/
static void pdmR3AsyncCompletionPutTask(PPDMASYNCCOMPLETIONENDPOINT pEndpoint, PPDMASYNCCOMPLETIONTASK pTask)
{
#ifdef VBOX_WITH_STATISTICS
if (tsRun < 1000)
{
/* Update nanoseconds statistics */
}
else
{
tsRun /= 1000;
if (tsRun < 1000)
{
/* Update microsecnds statistics */
}
else
{
tsRun /= 1000;
if (tsRun < 1000)
{
/* Update milliseconds statistics */
}
else
{
tsRun /= 1000;
if (tsRun < 1000)
{
/* Update seconds statistics */
}
else
}
}
}
if (tsInterval >= 1000)
{
pEndpoint->cIoOpsCompleted = 0;
}
#endif
}
static PPDMASYNCCOMPLETIONENDPOINT pdmR3AsyncCompletionFindEndpointWithUri(PPDMASYNCCOMPLETIONEPCLASS pEndpointClass,
const char *pszUri)
{
while (pEndpoint)
{
return pEndpoint;
}
return NULL;
}
{
int rc = VINF_SUCCESS;
LogFlowFunc((": ppEndpoint=%p pszFilename=%p{%s} fFlags=%u pTemplate=%p\n",
/* Sanity checks. */
/* Check that the flags are valid. */
PPDMASYNCCOMPLETIONEPCLASS pEndpointClass = pVM->pdm.s.papAsyncCompletionEndpointClass[PDMASYNCCOMPLETIONEPCLASSTYPE_FILE];
/* Search for a already opened endpoint for this file. */
if(!pEndpoint)
{
(void **)&pEndpoint);
if (RT_SUCCESS(rc))
{
/* Initialize common parts. */
pEndpoint->uTaskIdNext = 0;
pEndpoint->fTaskIdWraparound = false;
#ifdef VBOX_WITH_STATISTICS
/* Init the statistics part */
{
"Nanosecond resolution runtime statistics",
"/PDM/AsyncCompletion/File/%s/TaskRun1Ns-%u-%u",
i*100, i*100+100-1);
if (RT_FAILURE(rc))
break;
}
if (RT_SUCCESS(rc))
{
{
"Microsecond resolution runtime statistics",
"/PDM/AsyncCompletion/File/%s/TaskRun2MicroSec-%u-%u",
i*100, i*100+100-1);
if (RT_FAILURE(rc))
break;
}
}
if (RT_SUCCESS(rc))
{
{
"Milliseconds resolution runtime statistics",
"/PDM/AsyncCompletion/File/%s/TaskRun3Ms-%u-%u",
i*100, i*100+100-1);
if (RT_FAILURE(rc))
break;
}
}
if (RT_SUCCESS(rc))
{
{
"Second resolution runtime statistics",
"/PDM/AsyncCompletion/File/%s/TaskRun4Sec-%u-%u",
i*10, i*10+10-1);
if (RT_FAILURE(rc))
break;
}
}
if (RT_SUCCESS(rc))
{
"Tasks which ran more than 100sec",
}
if (RT_SUCCESS(rc))
{
"Processed I/O operations per second",
"/PDM/AsyncCompletion/File/%s/IoOpsPerSec",
}
if (RT_SUCCESS(rc))
{
"Started I/O operations for this endpoint",
"/PDM/AsyncCompletion/File/%s/IoOpsStarted",
}
if (RT_SUCCESS(rc))
{
"Completed I/O operations for this endpoint",
}
#endif
&& RT_SUCCESS(rc))
{
/* Call the initializer for the endpoint. */
if (RT_SUCCESS(rc))
{
/* Link it into the list of endpoints. */
if (pEndpointClass->pEndpointsHead)
/* Reference the template. */
*ppEndpoint = pEndpoint;
return VINF_SUCCESS;
}
}
}
}
else
{
/* Endpoint found. */
*ppEndpoint = pEndpoint;
return VINF_SUCCESS;
}
return rc;
}
{
/* Sanity checks. */
/* If the last user closed the endpoint we will free it. */
{
/* Drop reference from the template. */
/* Unlink the endpoint from the list. */
if (pEndpointPrev)
else
if (pEndpointNext)
#ifdef VBOX_WITH_STATISTICS
/* Deregister the statistics part */
#endif
}
}
{
int rc = VINF_SUCCESS;
if (!pTask)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
}
else
return rc;
}
{
int rc = VINF_SUCCESS;
if (!pTask)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
}
else
return rc;
}
void *pvUser,
{
int rc = VINF_SUCCESS;
if (!pTask)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
}
else
return rc;
}
{
else
return VERR_NOT_SUPPORTED;
}
{
else
return VERR_NOT_SUPPORTED;
}
{
return VERR_NOT_IMPLEMENTED;
}