STAM.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/** @file
*
* STAM - The Statistics Manager.
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* 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 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.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_STAM
#include "STAMInternal.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Argument structure for stamR3PrintOne().
*/
typedef struct STAMR3PRINTONEARGS
{
void *pvArg;
/**
* Argument structure to stamR3EnumOne().
*/
typedef struct STAMR3ENUMONEARGS
{
void *pvUser;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int stamR3Register(PVM pVM, void *pvSample, PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc);
static DECLCALLBACK(void) stamR3EnumLogPrintf(PSTAMR3PRINTONEARGS pvArg, const char *pszFormat, ...);
static DECLCALLBACK(void) stamR3EnumRelLogPrintf(PSTAMR3PRINTONEARGS pvArg, const char *pszFormat, ...);
static int stamR3Enum(PVM pVM, const char *pszPat, int (pfnCallback)(PSTAMDESC pDesc, void *pvArg), void *pvArg);
#ifdef VBOX_WITH_DEBUGGER
static DECLCALLBACK(int) stamR3CmdStats(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
static DECLCALLBACK(void) stamR3EnumDbgfPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...);
static DECLCALLBACK(int) stamR3CmdStatsReset(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
#endif
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifdef VBOX_WITH_DEBUGGER
/** Pattern argument. */
static const DBGCVARDESC g_aArgPat[] =
{
/* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
{ 0, 1, DBGCVAR_CAT_STRING, 0, "pattern", "Which samples the command shall be applied to. Use '*' as wildcard. Use ';' to separate expression." }
};
/** Command descriptors. */
{
/* pszCmd, cArgsMin, cArgsMax, paArgDesc, cArgDescs, pResultDesc, fFlags, pfnHandler pszSyntax, ....pszDescription */
{ "stats", 0, 1, &g_aArgPat[0], ELEMENTS(g_aArgPat), NULL, 0, stamR3CmdStats, "[pattern]", "Display statistics." },
{ "statsreset", 0, 1, &g_aArgPat[0], ELEMENTS(g_aArgPat), NULL, 0, stamR3CmdStatsReset,"[pattern]", "Resets statistics." }
};
#endif
/**
* Initializes the STAM.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
LogFlow(("STAMR3Init\n"));
/*
* Assert alignment and sizes.
*/
/*
* Setup any fixed pointers and offsets.
*/
if (VBOX_FAILURE(rc))
return rc;
#ifdef VBOX_WITH_DEBUGGER
/*
* Register debugger commands.
*/
static bool fRegisteredCmds = false;
if (!fRegisteredCmds)
{
if (VBOX_SUCCESS(rc))
fRegisteredCmds = true;
}
#endif
return VINF_SUCCESS;
}
/**
* Applies relocations to data and code managed by this
* component. This function will be called at init and
* whenever the VMM need to relocate it self inside the GC.
*
* @param pVM The VM.
*/
{
LogFlow(("STAMR3Relocate\n"));
}
/**
* Terminates the STAM.
*
* Termination means cleaning up and freeing all resources,
* the VM it self is at this point powered off or suspended.
*
* @returns VBox status code.
* @param pVM The VM to operate on.
*/
{
/*
* Free used memory and RWLock.
*/
while (pCur)
{
}
return VINF_SUCCESS;
}
/**
* Registers a sample with the statistics mamanger.
*
* Statistics are maintained on a per VM basis and should therefore
* be registered during the VM init stage. However, there is not problem
* registering temporary samples or samples for hotpluggable devices. Samples
* can be deregisterd using the STAMR3Deregister() function, but note that
* this is only necessary for temporary samples or hotpluggable devices.
*
* It is not possible to register the same sample twice.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pvSample Pointer to the sample.
* @param enmType Sample type. This indicates what pvSample is pointing at.
* @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
* @param pszName Sample name. The name is on this form "/<component>/<sample>".
* Further nesting is possible.
* @param enmUnit Sample unit.
* @param pszDesc Sample description.
*/
STAMR3DECL(int) STAMR3Register(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
{
return stamR3Register(pVM, pvSample, NULL, NULL, enmType, enmVisibility, pszName, enmUnit, pszDesc);
}
/**
* Same as STAMR3Register except that the name is specified in a
* RTStrPrintf like fashion.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pvSample Pointer to the sample.
* @param enmType Sample type. This indicates what pvSample is pointing at.
* @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
* @param enmUnit Sample unit.
* @param pszDesc Sample description.
* @param pszName The sample name format string.
* @param ... Arguments to the format string.
*/
STAMR3DECL(int) STAMR3RegisterF(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
{
return rc;
}
/**
* Same as STAMR3Register except that the name is specified in a
* RTStrPrintfV like fashion.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pvSample Pointer to the sample.
* @param enmType Sample type. This indicates what pvSample is pointing at.
* @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
* @param enmUnit Sample unit.
* @param pszDesc Sample description.
* @param pszName The sample name format string.
* @param args Arguments to the format string.
*/
STAMR3DECL(int) STAMR3RegisterV(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
{
char *pszFormattedName;
if (!pszFormattedName)
return VERR_NO_MEMORY;
return rc;
}
/**
* Similar to STAMR3Register except for the two callbacks, the implied type (STAMTYPE_CALLBACK),
* and name given in an RTStrPrintf like fashion.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pvSample Pointer to the sample.
* @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
* @param enmUnit Sample unit.
* @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
* @param pfnPrint Print the sample.
* @param pszDesc Sample description.
* @param pszName The sample name format string.
* @param ... Arguments to the format string.
* @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
*/
STAMR3DECL(int) STAMR3RegisterCallback(PVM pVM, void *pvSample, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
{
int rc = STAMR3RegisterCallbackV(pVM, pvSample, enmVisibility, enmUnit, pfnReset, pfnPrint, pszDesc, pszName, args);
return rc;
}
/**
* Same as STAMR3RegisterCallback() except for the ellipsis which is a va_list here.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pvSample Pointer to the sample.
* @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
* @param enmUnit Sample unit.
* @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
* @param pfnPrint Print the sample.
* @param pszDesc Sample description.
* @param pszName The sample name format string.
* @param args Arguments to the format string.
* @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
*/
STAMR3DECL(int) STAMR3RegisterCallbackV(PVM pVM, void *pvSample, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
{
char *pszFormattedName;
if (!pszFormattedName)
return VERR_NO_MEMORY;
int rc = stamR3Register(pVM, pvSample, pfnReset, pfnPrint, STAMTYPE_CALLBACK, enmVisibility, pszFormattedName, enmUnit, pszDesc);
return rc;
}
/**
* Internal worker for the different register calls.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pvSample Pointer to the sample.
* @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
* @param pfnPrint Print the sample.
* @param enmType Sample type. This indicates what pvSample is pointing at.
* @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
* @param enmUnit Sample unit.
* @param pszDesc Sample description.
* @param pszName The sample name format string.
* @param args Arguments to the format string.
* @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
*/
static int stamR3Register(PVM pVM, void *pvSample, PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
{
/*
* Check if exists.
*/
while (pCur)
{
/* passed it */
if (iDiff > 0)
break;
/* found it. */
if (!iDiff)
{
return VERR_ALREADY_EXISTS;
}
/* next */
}
/*
* Create a new node and insert it at the current location.
*/
int rc;
if (pNew)
{
if (enmType != STAMTYPE_CALLBACK)
else
{
}
if (pszDesc)
if (pPrev)
else
rc = VINF_SUCCESS;
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Deregisters a sample previously registered by STAR3Register().
*
* This is intended used for devices which can be unplugged and for
* temporary samples.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pvSample Pointer to the sample registered with STAMR3Register().
*/
{
/*
* Search for it.
*/
int rc = VERR_INVALID_HANDLE;
while (pCur)
{
{
if (pPrev)
else
rc = VINF_SUCCESS;
continue;
}
/* next */
}
return rc;
}
/**
* Resets statistics for the specified VM.
* It's possible to select a subset of the samples.
*
* @returns VBox status. (Basically, it cannot fail.)
* @param pVM The VM handle.
* @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
* If NULL all samples are reset.
*/
{
return VINF_SUCCESS;
}
/**
* Resets one statistics sample.
* Callback for stamR3Enum().
*
* @returns VINF_SUCCESS
* @param pDesc Pointer to the current descriptor.
* @param pvArg User argument - The VM handle.
*/
{
{
case STAMTYPE_COUNTER:
break;
case STAMTYPE_PROFILE:
case STAMTYPE_PROFILE_ADV:
break;
case STAMTYPE_RATIO_U32_RESET:
break;
case STAMTYPE_CALLBACK:
break;
case STAMTYPE_U8_RESET:
case STAMTYPE_X8_RESET:
break;
case STAMTYPE_U16_RESET:
case STAMTYPE_X16_RESET:
break;
case STAMTYPE_U32_RESET:
case STAMTYPE_X32_RESET:
break;
case STAMTYPE_U64_RESET:
case STAMTYPE_X64_RESET:
break;
/* These are custom and will not be touched. */
case STAMTYPE_U8:
case STAMTYPE_X8:
case STAMTYPE_U16:
case STAMTYPE_X16:
case STAMTYPE_U32:
case STAMTYPE_X32:
case STAMTYPE_U64:
case STAMTYPE_X64:
case STAMTYPE_RATIO_U32:
break;
default:
break;
}
return VINF_SUCCESS;
}
/**
* Get a snapshot of the statistics.
* It's possible to select a subset of the samples.
*
* @returns VBox status. (Basically, it cannot fail.)
* @param pVM The VM handle.
* @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
* If NULL all samples are reset.
* @param ppszSnapshot Where to store the pointer to the snapshot data.
* The format of the snapshot should be XML, but that will have to be discussed
* when this function is implemented.
* The returned pointer must be freed by calling STAMR3SnapshotFree().
* @param pcchSnapshot Where to store the size of the snapshot data. (Excluding the trailing '\0')
*/
STAMR3DECL(int) STAMR3Snapshot(PVM pVM, const char *pszPat, char **ppszSnapshot, size_t *pcchSnapshot)
{
AssertMsgFailed(("not implemented yet\n"));
return VERR_NOT_IMPLEMENTED;
}
/**
* Releases a statistics snapshot returned by STAMR3Snapshot().
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pszSnapshot The snapshot data pointer returned by STAMR3Snapshot().
* NULL is allowed.
*/
{
if (!pszSnapshot)
return VINF_SUCCESS;
}
/**
* Dumps the selected statistics to the log.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
* If NULL all samples are written to the log.
*/
{
return VINF_SUCCESS;
}
/**
* Prints to the log.
*
* @param pArgs Pointer to the print one argument structure.
* @param pszFormat Format string.
* @param ... Format arguments.
*/
static DECLCALLBACK(void) stamR3EnumLogPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
{
}
/**
* Dumps the selected statistics to the release log.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
* If NULL all samples are written to the log.
*/
{
return VINF_SUCCESS;
}
/**
* Prints to the release log.
*
* @param pArgs Pointer to the print one argument structure.
* @param pszFormat Format string.
* @param ... Format arguments.
*/
static DECLCALLBACK(void) stamR3EnumRelLogPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
{
}
/**
* Prints the selected statistics to standard out.
*
* @returns VBox status.
* @param pVM The VM handle.
* @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
* If NULL all samples are reset.
*/
{
return VINF_SUCCESS;
}
/**
* Prints to stdout.
*
* @param pArgs Pointer to the print one argument structure.
* @param pszFormat Format string.
* @param ... Format arguments.
*/
{
}
/**
* Prints one sample.
* Callback for stamR3Enum().
*
* @returns VINF_SUCCESS
* @param pDesc Pointer to the current descriptor.
* @param pvArg User argument - STAMR3PRINTONEARGS.
*/
{
{
case STAMTYPE_COUNTER:
return VINF_SUCCESS;
pArgs->pfnPrintf(pArgs, "%-32s %8llu %s\n", pDesc->pszName, pDesc->u.pCounter->c, STAMR3GetUnit(pDesc->enmUnit));
break;
case STAMTYPE_PROFILE:
case STAMTYPE_PROFILE_ADV:
{
return VINF_SUCCESS;
pArgs->pfnPrintf(pArgs, "%-32s %8llu %s (%12llu ticks, %7llu times, max %9llu, min %7lld)\n", pDesc->pszName,
pDesc->u.pProfile->cTicks, pDesc->u.pProfile->cPeriods, pDesc->u.pProfile->cTicksMax, pDesc->u.pProfile->cTicksMin);
break;
}
case STAMTYPE_RATIO_U32:
case STAMTYPE_RATIO_U32_RESET:
if (pDesc->enmVisibility == STAMVISIBILITY_USED && !pDesc->u.pRatioU32->u32A && !pDesc->u.pRatioU32->u32B)
return VINF_SUCCESS;
break;
case STAMTYPE_CALLBACK:
{
char szBuf[512];
break;
}
case STAMTYPE_U8:
case STAMTYPE_U8_RESET:
return VINF_SUCCESS;
pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu8, STAMR3GetUnit(pDesc->enmUnit));
break;
case STAMTYPE_X8:
case STAMTYPE_X8_RESET:
return VINF_SUCCESS;
pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu8, STAMR3GetUnit(pDesc->enmUnit));
break;
case STAMTYPE_U16:
case STAMTYPE_U16_RESET:
return VINF_SUCCESS;
pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu16, STAMR3GetUnit(pDesc->enmUnit));
break;
case STAMTYPE_X16:
case STAMTYPE_X16_RESET:
return VINF_SUCCESS;
pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu16, STAMR3GetUnit(pDesc->enmUnit));
break;
case STAMTYPE_U32:
case STAMTYPE_U32_RESET:
return VINF_SUCCESS;
pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu32, STAMR3GetUnit(pDesc->enmUnit));
break;
case STAMTYPE_X32:
case STAMTYPE_X32_RESET:
return VINF_SUCCESS;
pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu32, STAMR3GetUnit(pDesc->enmUnit));
break;
case STAMTYPE_U64:
case STAMTYPE_U64_RESET:
return VINF_SUCCESS;
pArgs->pfnPrintf(pArgs, "%-32s %8llu %s\n", pDesc->pszName, *pDesc->u.pu64, STAMR3GetUnit(pDesc->enmUnit));
break;
case STAMTYPE_X64:
case STAMTYPE_X64_RESET:
return VINF_SUCCESS;
pArgs->pfnPrintf(pArgs, "%-32s %8llx %s\n", pDesc->pszName, *pDesc->u.pu64, STAMR3GetUnit(pDesc->enmUnit));
break;
default:
break;
}
return VINF_SUCCESS;
}
/**
* Enumerate the statistics by the means of a callback function.
*
* @returns Whatever the callback returns.
*
* @param pVM The VM handle.
* @param pszPat The pattern to match samples.
* @param pfnEnum The callback function.
* @param pvUser The pvUser argument of the callback function.
*/
{
return rc;
}
/**
* Callback function for STARTR3Enum().
*
* @returns whatever the callback returns.
* @param pDesc Pointer to the current descriptor.
* @param pvArg Points to a STAMR3ENUMONEARGS structure.
*/
{
int rc;
{
/* Give the enumerator something useful. */
char szBuf[512];
}
else
return rc;
}
/**
* Matches a sample name against a pattern.
*
* @returns True if matches, false if not.
* @param pszPat Pattern.
* @param pszName Name to match against the pattern.
*/
{
if (!pszPat)
return true;
/* ASSUMES ASCII */
for (;;)
{
switch (chPat)
{
case '\0':
return !*pszName;
case '*':
{
/* nothing */;
for (;;)
{
&& ( !chPat
return true;
if (!ch)
return false;
}
/* won't ever get here */
break;
}
case '?':
if (!*pszName)
return false;
break;
default:
return false;
break;
}
pszName++;
pszPat++;
}
return true;
}
/**
* Enumerates the nodes selected by a pattern or all nodes if no pattern
* is specified.
*
* The call must own at least a read lock to the STAM data.
*
* @returns The rc from the callback.
* @param pVM VM handle
* @param pszPat Pattern.
* @param pfnCallback Callback function which shall be called for matching nodes.
* If it returns anything but VINF_SUCCESS the enumeration is
* terminated and the status code returned to the caller.
* @param pvArg User parameter for the callback.
*/
static int stamR3Enum(PVM pVM, const char *pszPat, int (*pfnCallback)(PSTAMDESC pDesc, void *pvArg), void *pvArg)
{
/*
* Search for it.
*/
int rc = VINF_SUCCESS;
while (pCur)
{
{
if (rc)
break;
}
/* next */
}
return rc;
}
/**
* Get the unit string.
*
* @returns Pointer to read only unit string.
* @param enmUnit The unit.
*/
{
switch (enmUnit)
{
case STAMUNIT_NONE: return "";
case STAMUNIT_CALLS: return "calls";
case STAMUNIT_COUNT: return "count";
case STAMUNIT_BYTES: return "bytes";
case STAMUNIT_PAGES: return "pages";
case STAMUNIT_ERRORS: return "errors";
case STAMUNIT_OCCURENCES: return "times";
case STAMUNIT_TICKS_PER_CALL: return "ticks/call";
case STAMUNIT_TICKS_PER_OCCURENCE: return "ticks/time";
case STAMUNIT_GOOD_BAD: return "good:bad";
case STAMUNIT_MEGABYTES: return "MBs";
default:
return "(?unit?)";
}
}
#ifdef VBOX_WITH_DEBUGGER
/**
* The '.stats' command.
*
* @returns VBox status.
* @param pCmd Pointer to the command descriptor (as registered).
* @param pCmdHlp Pointer to command helper functions.
* @param pVM Pointer to the current VM (if any).
* @param paArgs Pointer to (readonly) array of arguments.
* @param cArgs Number of arguments in the array.
*/
static DECLCALLBACK(int) stamR3CmdStats(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
{
/*
* Validate input.
*/
if (!pVM)
/*
* Do the printing.
*/
return rc;
}
/**
* Display one sample in the debugger.
*
* @param pArgs Pointer to the print one argument structure.
* @param pszFormat Format string.
* @param ... Format arguments.
*/
static DECLCALLBACK(void) stamR3EnumDbgfPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
{
}
/**
* The '.statsreset' command.
*
* @returns VBox status.
* @param pCmd Pointer to the command descriptor (as registered).
* @param pCmdHlp Pointer to command helper functions.
* @param pVM Pointer to the current VM (if any).
* @param paArgs Pointer to (readonly) array of arguments.
* @param cArgs Number of arguments in the array.
*/
static DECLCALLBACK(int) stamR3CmdStatsReset(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
{
/*
* Validate input.
*/
if (!pVM)
/*
* Execute reset.
*/
if (VBOX_SUCCESS(rc))
}
#endif