VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/STAM.cpp@ 46447

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

VMM: Optimize STAM enumeration. Multi pattern expressions are not optimized yet.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 96.7 KB
Line 
1/* $Id: STAM.cpp 46447 2013-06-07 19:09:25Z vboxsync $ */
2/** @file
3 * STAM - The Statistics Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/** @page pg_stam STAM - The Statistics Manager
19 *
20 * The purpose for the statistics manager is to present the rest of the system
21 * with a somewhat uniform way of accessing VMM statistics. STAM sports a
22 * couple of different APIs for accessing them: STAMR3EnumU, STAMR3SnapshotU,
23 * STAMR3DumpU, STAMR3DumpToReleaseLogU and the debugger. Main is exposing the
24 * XML based one, STAMR3SnapshotU.
25 *
26 * The rest of the VMM together with the devices and drivers registers their
27 * statistics with STAM giving them a name. The name is hierarchical, the
28 * components separated by slashes ('/') and must start with a slash.
29 *
30 * Each item registered with STAM - also, half incorrectly, called a sample -
31 * has a type, unit, visibility, data pointer and description associated with it
32 * in addition to the name (described above). The type tells STAM what kind of
33 * structure the pointer is pointing to. The visibility allows unused
34 * statistics from cluttering the output or showing up in the GUI. All the bits
35 * together makes STAM able to present the items in a sensible way to the user.
36 * Some types also allows STAM to reset the data, which is very convenient when
37 * digging into specific operations and such.
38 *
39 * PS. The VirtualBox Debugger GUI has a viewer for inspecting the statistics
40 * STAM provides. You will also find statistics in the release and debug logs.
41 * And as mentioned in the introduction, the debugger console features a couple
42 * of command: .stats and .statsreset.
43 *
44 * @see grp_stam
45 */
46
47/*******************************************************************************
48* Header Files *
49*******************************************************************************/
50#define LOG_GROUP LOG_GROUP_STAM
51#include <VBox/vmm/stam.h>
52#include "STAMInternal.h"
53#include <VBox/vmm/vm.h>
54#include <VBox/vmm/uvm.h>
55#include <VBox/err.h>
56#include <VBox/dbg.h>
57#include <VBox/log.h>
58
59#include <iprt/assert.h>
60#include <iprt/asm.h>
61#include <iprt/mem.h>
62#include <iprt/stream.h>
63#include <iprt/string.h>
64
65
66/*******************************************************************************
67* Structures and Typedefs *
68*******************************************************************************/
69/**
70 * Argument structure for stamR3PrintOne().
71 */
72typedef struct STAMR3PRINTONEARGS
73{
74 PUVM pUVM;
75 void *pvArg;
76 DECLCALLBACKMEMBER(void, pfnPrintf)(struct STAMR3PRINTONEARGS *pvArg, const char *pszFormat, ...);
77} STAMR3PRINTONEARGS, *PSTAMR3PRINTONEARGS;
78
79
80/**
81 * Argument structure to stamR3EnumOne().
82 */
83typedef struct STAMR3ENUMONEARGS
84{
85 PVM pVM;
86 PFNSTAMR3ENUM pfnEnum;
87 void *pvUser;
88} STAMR3ENUMONEARGS, *PSTAMR3ENUMONEARGS;
89
90
91/**
92 * The snapshot status structure.
93 * Argument package passed to stamR3SnapshotOne, stamR3SnapshotPrintf and stamR3SnapshotOutput.
94 */
95typedef struct STAMR3SNAPSHOTONE
96{
97 /** Pointer to the buffer start. */
98 char *pszStart;
99 /** Pointer to the buffer end. */
100 char *pszEnd;
101 /** Pointer to the current buffer position. */
102 char *psz;
103 /** Pointer to the VM. */
104 PVM pVM;
105 /** The number of bytes allocated. */
106 size_t cbAllocated;
107 /** The status code. */
108 int rc;
109 /** Whether to include the description strings. */
110 bool fWithDesc;
111} STAMR3SNAPSHOTONE, *PSTAMR3SNAPSHOTONE;
112
113
114/**
115 * Init record for a ring-0 statistic sample.
116 */
117typedef struct STAMR0SAMPLE
118{
119 /** The GVMMSTATS structure offset of the variable. */
120 unsigned offVar;
121 /** The type. */
122 STAMTYPE enmType;
123 /** The unit. */
124 STAMUNIT enmUnit;
125 /** The name. */
126 const char *pszName;
127 /** The description. */
128 const char *pszDesc;
129} STAMR0SAMPLE;
130
131
132/*******************************************************************************
133* Internal Functions *
134*******************************************************************************/
135#ifdef STAM_WITH_LOOKUP_TREE
136static void stamR3LookupDestroyTree(PSTAMLOOKUP pRoot);
137#endif
138static int stamR3RegisterU(PUVM pUVM, void *pvSample, PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
139 STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc);
140static int stamR3ResetOne(PSTAMDESC pDesc, void *pvArg);
141static DECLCALLBACK(void) stamR3EnumLogPrintf(PSTAMR3PRINTONEARGS pvArg, const char *pszFormat, ...);
142static DECLCALLBACK(void) stamR3EnumRelLogPrintf(PSTAMR3PRINTONEARGS pvArg, const char *pszFormat, ...);
143static DECLCALLBACK(void) stamR3EnumPrintf(PSTAMR3PRINTONEARGS pvArg, const char *pszFormat, ...);
144static int stamR3SnapshotOne(PSTAMDESC pDesc, void *pvArg);
145static int stamR3SnapshotPrintf(PSTAMR3SNAPSHOTONE pThis, const char *pszFormat, ...);
146static int stamR3PrintOne(PSTAMDESC pDesc, void *pvArg);
147static int stamR3EnumOne(PSTAMDESC pDesc, void *pvArg);
148static bool stamR3MultiMatch(const char * const *papszExpressions, unsigned cExpressions, unsigned *piExpression, const char *pszName);
149static char ** stamR3SplitPattern(const char *pszPat, unsigned *pcExpressions, char **ppszCopy);
150static int stamR3EnumU(PUVM pUVM, const char *pszPat, bool fUpdateRing0, int (pfnCallback)(PSTAMDESC pDesc, void *pvArg), void *pvArg);
151static void stamR3Ring0StatsRegisterU(PUVM pUVM);
152static void stamR3Ring0StatsUpdateU(PUVM pUVM, const char *pszPat);
153static void stamR3Ring0StatsUpdateMultiU(PUVM pUVM, const char * const *papszExpressions, unsigned cExpressions);
154
155#ifdef VBOX_WITH_DEBUGGER
156static FNDBGCCMD stamR3CmdStats;
157static DECLCALLBACK(void) stamR3EnumDbgfPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...);
158static FNDBGCCMD stamR3CmdStatsReset;
159#endif
160
161
162/*******************************************************************************
163* Global Variables *
164*******************************************************************************/
165#ifdef VBOX_WITH_DEBUGGER
166/** Pattern argument. */
167static const DBGCVARDESC g_aArgPat[] =
168{
169 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
170 { 0, 1, DBGCVAR_CAT_STRING, 0, "pattern", "Which samples the command shall be applied to. Use '*' as wildcard. Use ';' to separate expression." }
171};
172
173/** Command descriptors. */
174static const DBGCCMD g_aCmds[] =
175{
176 /* pszCmd, cArgsMin, cArgsMax, paArgDesc, cArgDescs, fFlags, pfnHandler pszSyntax, ....pszDescription */
177 { "stats", 0, 1, &g_aArgPat[0], RT_ELEMENTS(g_aArgPat), 0, stamR3CmdStats, "[pattern]", "Display statistics." },
178 { "statsreset", 0, 1, &g_aArgPat[0], RT_ELEMENTS(g_aArgPat), 0, stamR3CmdStatsReset,"[pattern]", "Resets statistics." }
179};
180#endif
181
182
183/**
184 * The GVMM mapping records - sans the host cpus.
185 */
186static const STAMR0SAMPLE g_aGVMMStats[] =
187{
188 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltCalls", "The number of calls to GVMMR0SchedHalt." },
189 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltBlocking), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltBlocking", "The number of times we did go to sleep in GVMMR0SchedHalt." },
190 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltTimeouts), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltTimeouts", "The number of times we timed out in GVMMR0SchedHalt." },
191 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltNotBlocking), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltNotBlocking", "The number of times we didn't go to sleep in GVMMR0SchedHalt." },
192 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltWakeUps", "The number of wake ups done during GVMMR0SchedHalt." },
193 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cWakeUpCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/WakeUpCalls", "The number of calls to GVMMR0WakeUp." },
194 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cWakeUpNotHalted), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/WakeUpNotHalted", "The number of times the EMT thread wasn't actually halted when GVMMR0WakeUp was called." },
195 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cWakeUpWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/WakeUpWakeUps", "The number of wake ups done during GVMMR0WakeUp (not counting the explicit one)." },
196 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cPokeCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/PokeCalls", "The number of calls to GVMMR0Poke." },
197 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cPokeNotBusy), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/PokeNotBusy", "The number of times the EMT thread wasn't actually busy when GVMMR0Poke was called." },
198 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cPollCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/PollCalls", "The number of calls to GVMMR0SchedPoll." },
199 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cPollHalts), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/PollHalts", "The number of times the EMT has halted in a GVMMR0SchedPoll call." },
200 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cPollWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/PollWakeUps", "The number of wake ups done during GVMMR0SchedPoll." },
201
202 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltCalls", "The number of calls to GVMMR0SchedHalt." },
203 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltBlocking), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltBlocking", "The number of times we did go to sleep in GVMMR0SchedHalt." },
204 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltTimeouts), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltTimeouts", "The number of times we timed out in GVMMR0SchedHalt." },
205 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltNotBlocking), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltNotBlocking", "The number of times we didn't go to sleep in GVMMR0SchedHalt." },
206 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltWakeUps", "The number of wake ups done during GVMMR0SchedHalt." },
207 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cWakeUpCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/WakeUpCalls", "The number of calls to GVMMR0WakeUp." },
208 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cWakeUpNotHalted), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/WakeUpNotHalted", "The number of times the EMT thread wasn't actually halted when GVMMR0WakeUp was called." },
209 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cWakeUpWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/WakeUpWakeUps", "The number of wake ups done during GVMMR0WakeUp (not counting the explicit one)." },
210 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cPokeCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/PokeCalls", "The number of calls to GVMMR0Poke." },
211 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cPokeNotBusy), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/PokeNotBusy", "The number of times the EMT thread wasn't actually busy when GVMMR0Poke was called." },
212 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cPollCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/PollCalls", "The number of calls to GVMMR0SchedPoll." },
213 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cPollHalts), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/PollHalts", "The number of times the EMT has halted in a GVMMR0SchedPoll call." },
214 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cPollWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/PollWakeUps", "The number of wake ups done during GVMMR0SchedPoll." },
215
216 { RT_UOFFSETOF(GVMMSTATS, cVMs), STAMTYPE_U32, STAMUNIT_CALLS, "/GVMM/VMs", "The number of VMs accessible to the caller." },
217 { RT_UOFFSETOF(GVMMSTATS, cEMTs), STAMTYPE_U32, STAMUNIT_CALLS, "/GVMM/EMTs", "The number of emulation threads." },
218 { RT_UOFFSETOF(GVMMSTATS, cHostCpus), STAMTYPE_U32, STAMUNIT_CALLS, "/GVMM/HostCPUs", "The number of host CPUs." },
219};
220
221
222/**
223 * The GMM mapping records.
224 */
225static const STAMR0SAMPLE g_aGMMStats[] =
226{
227 { RT_UOFFSETOF(GMMSTATS, cMaxPages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/cMaxPages", "The maximum number of pages GMM is allowed to allocate." },
228 { RT_UOFFSETOF(GMMSTATS, cReservedPages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/cReservedPages", "The number of pages that has been reserved." },
229 { RT_UOFFSETOF(GMMSTATS, cOverCommittedPages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/cOverCommittedPages", "The number of pages that we have over-committed in reservations." },
230 { RT_UOFFSETOF(GMMSTATS, cAllocatedPages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/cAllocatedPages", "The number of actually allocated (committed if you like) pages." },
231 { RT_UOFFSETOF(GMMSTATS, cSharedPages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/cSharedPages", "The number of pages that are shared. A subset of cAllocatedPages." },
232 { RT_UOFFSETOF(GMMSTATS, cDuplicatePages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/cDuplicatePages", "The number of pages that are actually shared between VMs." },
233 { RT_UOFFSETOF(GMMSTATS, cLeftBehindSharedPages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/cLeftBehindSharedPages", "The number of pages that are shared that has been left behind by VMs not doing proper cleanups." },
234 { RT_UOFFSETOF(GMMSTATS, cBalloonedPages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/cBalloonedPages", "The number of current ballooned pages." },
235 { RT_UOFFSETOF(GMMSTATS, cChunks), STAMTYPE_U32, STAMUNIT_COUNT, "/GMM/cChunks", "The number of allocation chunks." },
236 { RT_UOFFSETOF(GMMSTATS, cFreedChunks), STAMTYPE_U32, STAMUNIT_COUNT, "/GMM/cFreedChunks", "The number of freed chunks ever." },
237 { RT_UOFFSETOF(GMMSTATS, cShareableModules), STAMTYPE_U32, STAMUNIT_COUNT, "/GMM/cShareableModules", "The number of shareable modules." },
238 { RT_UOFFSETOF(GMMSTATS, VMStats.Reserved.cBasePages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/VM/Reserved/cBasePages", "The amount of base memory (RAM, ROM, ++) reserved by the VM." },
239 { RT_UOFFSETOF(GMMSTATS, VMStats.Reserved.cShadowPages), STAMTYPE_U32, STAMUNIT_PAGES, "/GMM/VM/Reserved/cShadowPages", "The amount of memory reserved for shadow/nested page tables." },
240 { RT_UOFFSETOF(GMMSTATS, VMStats.Reserved.cFixedPages), STAMTYPE_U32, STAMUNIT_PAGES, "/GMM/VM/Reserved/cFixedPages", "The amount of memory reserved for fixed allocations like MMIO2 and the hyper heap." },
241 { RT_UOFFSETOF(GMMSTATS, VMStats.Allocated.cBasePages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/VM/Allocated/cBasePages", "The amount of base memory (RAM, ROM, ++) allocated by the VM." },
242 { RT_UOFFSETOF(GMMSTATS, VMStats.Allocated.cShadowPages), STAMTYPE_U32, STAMUNIT_PAGES, "/GMM/VM/Allocated/cShadowPages", "The amount of memory allocated for shadow/nested page tables." },
243 { RT_UOFFSETOF(GMMSTATS, VMStats.Allocated.cFixedPages), STAMTYPE_U32, STAMUNIT_PAGES, "/GMM/VM/Allocated/cFixedPages", "The amount of memory allocated for fixed allocations like MMIO2 and the hyper heap." },
244 { RT_UOFFSETOF(GMMSTATS, VMStats.cPrivatePages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/VM/cPrivatePages", "The current number of private pages." },
245 { RT_UOFFSETOF(GMMSTATS, VMStats.cSharedPages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/VM/cSharedPages", "The current number of shared pages." },
246 { RT_UOFFSETOF(GMMSTATS, VMStats.cBalloonedPages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/VM/cBalloonedPages", "The current number of ballooned pages." },
247 { RT_UOFFSETOF(GMMSTATS, VMStats.cMaxBalloonedPages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/VM/cMaxBalloonedPages", "The max number of pages that can be ballooned." },
248 { RT_UOFFSETOF(GMMSTATS, VMStats.cReqBalloonedPages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/VM/cReqBalloonedPages", "The number of pages we've currently requested the guest to give us." },
249 { RT_UOFFSETOF(GMMSTATS, VMStats.cReqActuallyBalloonedPages),STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/VM/cReqActuallyBalloonedPages","The number of pages the guest has given us in response to the request." },
250 { RT_UOFFSETOF(GMMSTATS, VMStats.cReqDeflatePages), STAMTYPE_U64, STAMUNIT_PAGES, "/GMM/VM/cReqDeflatePages", "The number of pages we've currently requested the guest to take back." },
251 { RT_UOFFSETOF(GMMSTATS, VMStats.cShareableModules), STAMTYPE_U32, STAMUNIT_COUNT, "/GMM/VM/cShareableModules", "The number of shareable modules traced by the VM." },
252 { RT_UOFFSETOF(GMMSTATS, VMStats.enmPolicy), STAMTYPE_U32, STAMUNIT_NONE, "/GMM/VM/enmPolicy", "The current over-commit policy." },
253 { RT_UOFFSETOF(GMMSTATS, VMStats.enmPriority), STAMTYPE_U32, STAMUNIT_NONE, "/GMM/VM/enmPriority", "The VM priority for arbitrating VMs in low and out of memory situation." },
254 { RT_UOFFSETOF(GMMSTATS, VMStats.fBallooningEnabled), STAMTYPE_BOOL, STAMUNIT_NONE, "/GMM/VM/fBallooningEnabled", "Whether ballooning is enabled or not." },
255 { RT_UOFFSETOF(GMMSTATS, VMStats.fBallooningEnabled), STAMTYPE_BOOL, STAMUNIT_NONE, "/GMM/VM/fSharedPagingEnabled", "Whether shared paging is enabled or not." },
256 { RT_UOFFSETOF(GMMSTATS, VMStats.fBallooningEnabled), STAMTYPE_BOOL, STAMUNIT_NONE, "/GMM/VM/fMayAllocate", "Whether the VM is allowed to allocate memory or not." },
257};
258
259
260/**
261 * Initializes the STAM.
262 *
263 * @returns VBox status code.
264 * @param pVM Pointer to the VM.
265 */
266VMMR3DECL(int) STAMR3InitUVM(PUVM pUVM)
267{
268 LogFlow(("STAMR3Init\n"));
269
270 /*
271 * Assert alignment and sizes.
272 */
273 AssertCompile(sizeof(pUVM->stam.s) <= sizeof(pUVM->stam.padding));
274 AssertRelease(sizeof(pUVM->stam.s) <= sizeof(pUVM->stam.padding));
275
276 /*
277 * Initialize the read/write lock and list.
278 */
279 int rc = RTSemRWCreate(&pUVM->stam.s.RWSem);
280 AssertRCReturn(rc, rc);
281
282 RTListInit(&pUVM->stam.s.List);
283
284#ifdef STAM_WITH_LOOKUP_TREE
285 /*
286 * Initialize the root node.
287 */
288 PSTAMLOOKUP pRoot = (PSTAMLOOKUP)RTMemAlloc(sizeof(STAMLOOKUP));
289 if (!pRoot)
290 {
291 RTSemRWDestroy(pUVM->stam.s.RWSem);
292 pUVM->stam.s.RWSem = NIL_RTSEMRW;
293 return VERR_NO_MEMORY;
294 }
295 pRoot->pParent = NULL;
296 pRoot->papChildren = NULL;
297 pRoot->pDesc = NULL;
298 pRoot->cDescsInTree = 0;
299 pRoot->cChildren = 0;
300 pRoot->iParent = UINT16_MAX;
301 pRoot->off = 0;
302 pRoot->cch = 0;
303 pRoot->szName[0] = '\0';
304
305 pUVM->stam.s.pRoot = pRoot;
306#endif
307
308
309 /*
310 * Register the ring-0 statistics (GVMM/GMM).
311 */
312 stamR3Ring0StatsRegisterU(pUVM);
313
314#ifdef VBOX_WITH_DEBUGGER
315 /*
316 * Register debugger commands.
317 */
318 static bool fRegisteredCmds = false;
319 if (!fRegisteredCmds)
320 {
321 rc = DBGCRegisterCommands(&g_aCmds[0], RT_ELEMENTS(g_aCmds));
322 if (RT_SUCCESS(rc))
323 fRegisteredCmds = true;
324 }
325#endif
326
327 return VINF_SUCCESS;
328}
329
330
331/**
332 * Terminates the STAM.
333 *
334 * @param pUVM Pointer to the user mode VM structure.
335 */
336VMMR3DECL(void) STAMR3TermUVM(PUVM pUVM)
337{
338 /*
339 * Free used memory and the RWLock.
340 */
341 PSTAMDESC pCur, pNext;
342 RTListForEachSafe(&pUVM->stam.s.List, pCur, pNext, STAMDESC, ListEntry)
343 {
344#ifdef STAM_WITH_LOOKUP_TREE
345 pCur->pLookup->pDesc = NULL;
346#endif
347 RTMemFree(pCur);
348 }
349
350#ifdef STAM_WITH_LOOKUP_TREE
351 stamR3LookupDestroyTree(pUVM->stam.s.pRoot);
352 pUVM->stam.s.pRoot = NULL;
353#endif
354
355 Assert(pUVM->stam.s.RWSem != NIL_RTSEMRW);
356 RTSemRWDestroy(pUVM->stam.s.RWSem);
357 pUVM->stam.s.RWSem = NIL_RTSEMRW;
358}
359
360
361/**
362 * Registers a sample with the statistics manager.
363 *
364 * Statistics are maintained on a per VM basis and is normally registered
365 * during the VM init stage, but there is nothing preventing you from
366 * register them at runtime.
367 *
368 * Use STAMR3Deregister() to deregister statistics at runtime, however do
369 * not bother calling at termination time.
370 *
371 * It is not possible to register the same sample twice.
372 *
373 * @returns VBox status.
374 * @param pUVM Pointer to the user mode VM structure.
375 * @param pvSample Pointer to the sample.
376 * @param enmType Sample type. This indicates what pvSample is pointing at.
377 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
378 * @param pszName Sample name. The name is on this form "/<component>/<sample>".
379 * Further nesting is possible.
380 * @param enmUnit Sample unit.
381 * @param pszDesc Sample description.
382 */
383VMMR3DECL(int) STAMR3RegisterU(PUVM pUVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
384{
385 AssertReturn(enmType != STAMTYPE_CALLBACK, VERR_INVALID_PARAMETER);
386 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
387 return stamR3RegisterU(pUVM, pvSample, NULL, NULL, enmType, enmVisibility, pszName, enmUnit, pszDesc);
388}
389
390
391/**
392 * Registers a sample with the statistics manager.
393 *
394 * Statistics are maintained on a per VM basis and is normally registered
395 * during the VM init stage, but there is nothing preventing you from
396 * register them at runtime.
397 *
398 * Use STAMR3Deregister() to deregister statistics at runtime, however do
399 * not bother calling at termination time.
400 *
401 * It is not possible to register the same sample twice.
402 *
403 * @returns VBox status.
404 * @param pVM Pointer to the VM.
405 * @param pvSample Pointer to the sample.
406 * @param enmType Sample type. This indicates what pvSample is pointing at.
407 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
408 * @param pszName Sample name. The name is on this form "/<component>/<sample>".
409 * Further nesting is possible.
410 * @param enmUnit Sample unit.
411 * @param pszDesc Sample description.
412 */
413VMMR3DECL(int) STAMR3Register(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
414{
415 AssertReturn(enmType != STAMTYPE_CALLBACK, VERR_INVALID_PARAMETER);
416 return stamR3RegisterU(pVM->pUVM, pvSample, NULL, NULL, enmType, enmVisibility, pszName, enmUnit, pszDesc);
417}
418
419
420/**
421 * Same as STAMR3RegisterU except that the name is specified in a
422 * RTStrPrintf like fashion.
423 *
424 * @returns VBox status.
425 * @param pUVM Pointer to the user mode VM structure.
426 * @param pvSample Pointer to the sample.
427 * @param enmType Sample type. This indicates what pvSample is pointing at.
428 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
429 * @param enmUnit Sample unit.
430 * @param pszDesc Sample description.
431 * @param pszName The sample name format string.
432 * @param ... Arguments to the format string.
433 */
434VMMR3DECL(int) STAMR3RegisterFU(PUVM pUVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
435 const char *pszDesc, const char *pszName, ...)
436{
437 va_list args;
438 va_start(args, pszName);
439 int rc = STAMR3RegisterVU(pUVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
440 va_end(args);
441 return rc;
442}
443
444
445/**
446 * Same as STAMR3Register except that the name is specified in a
447 * RTStrPrintf like fashion.
448 *
449 * @returns VBox status.
450 * @param pVM Pointer to the VM.
451 * @param pvSample Pointer to the sample.
452 * @param enmType Sample type. This indicates what pvSample is pointing at.
453 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
454 * @param enmUnit Sample unit.
455 * @param pszDesc Sample description.
456 * @param pszName The sample name format string.
457 * @param ... Arguments to the format string.
458 */
459VMMR3DECL(int) STAMR3RegisterF(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
460 const char *pszDesc, const char *pszName, ...)
461{
462 va_list args;
463 va_start(args, pszName);
464 int rc = STAMR3RegisterVU(pVM->pUVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
465 va_end(args);
466 return rc;
467}
468
469
470/**
471 * Same as STAMR3Register except that the name is specified in a
472 * RTStrPrintfV like fashion.
473 *
474 * @returns VBox status.
475 * @param pVM Pointer to the VM.
476 * @param pvSample Pointer to the sample.
477 * @param enmType Sample type. This indicates what pvSample is pointing at.
478 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
479 * @param enmUnit Sample unit.
480 * @param pszDesc Sample description.
481 * @param pszName The sample name format string.
482 * @param args Arguments to the format string.
483 */
484VMMR3DECL(int) STAMR3RegisterVU(PUVM pUVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
485 const char *pszDesc, const char *pszName, va_list args)
486{
487 AssertReturn(enmType != STAMTYPE_CALLBACK, VERR_INVALID_PARAMETER);
488
489 char *pszFormattedName;
490 RTStrAPrintfV(&pszFormattedName, pszName, args);
491 if (!pszFormattedName)
492 return VERR_NO_MEMORY;
493
494 int rc = STAMR3RegisterU(pUVM, pvSample, enmType, enmVisibility, pszFormattedName, enmUnit, pszDesc);
495 RTStrFree(pszFormattedName);
496 return rc;
497}
498
499
500/**
501 * Same as STAMR3Register except that the name is specified in a
502 * RTStrPrintfV like fashion.
503 *
504 * @returns VBox status.
505 * @param pVM Pointer to the VM.
506 * @param pvSample Pointer to the sample.
507 * @param enmType Sample type. This indicates what pvSample is pointing at.
508 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
509 * @param enmUnit Sample unit.
510 * @param pszDesc Sample description.
511 * @param pszName The sample name format string.
512 * @param args Arguments to the format string.
513 */
514VMMR3DECL(int) STAMR3RegisterV(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
515 const char *pszDesc, const char *pszName, va_list args)
516{
517 return STAMR3RegisterVU(pVM->pUVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
518}
519
520
521/**
522 * Similar to STAMR3Register except for the two callbacks, the implied type (STAMTYPE_CALLBACK),
523 * and name given in an RTStrPrintf like fashion.
524 *
525 * @returns VBox status.
526 * @param pVM Pointer to the VM.
527 * @param pvSample Pointer to the sample.
528 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
529 * @param enmUnit Sample unit.
530 * @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
531 * @param pfnPrint Print the sample.
532 * @param pszDesc Sample description.
533 * @param pszName The sample name format string.
534 * @param ... Arguments to the format string.
535 * @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
536 */
537VMMR3DECL(int) STAMR3RegisterCallback(PVM pVM, void *pvSample, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
538 PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
539 const char *pszDesc, const char *pszName, ...)
540{
541 va_list args;
542 va_start(args, pszName);
543 int rc = STAMR3RegisterCallbackV(pVM, pvSample, enmVisibility, enmUnit, pfnReset, pfnPrint, pszDesc, pszName, args);
544 va_end(args);
545 return rc;
546}
547
548
549/**
550 * Same as STAMR3RegisterCallback() except for the ellipsis which is a va_list here.
551 *
552 * @returns VBox status.
553 * @param pVM Pointer to the VM.
554 * @param pvSample Pointer to the sample.
555 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
556 * @param enmUnit Sample unit.
557 * @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
558 * @param pfnPrint Print the sample.
559 * @param pszDesc Sample description.
560 * @param pszName The sample name format string.
561 * @param args Arguments to the format string.
562 * @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
563 */
564VMMR3DECL(int) STAMR3RegisterCallbackV(PVM pVM, void *pvSample, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
565 PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
566 const char *pszDesc, const char *pszName, va_list args)
567{
568 char *pszFormattedName;
569 RTStrAPrintfV(&pszFormattedName, pszName, args);
570 if (!pszFormattedName)
571 return VERR_NO_MEMORY;
572
573 int rc = stamR3RegisterU(pVM->pUVM, pvSample, pfnReset, pfnPrint, STAMTYPE_CALLBACK, enmVisibility, pszFormattedName, enmUnit, pszDesc);
574 RTStrFree(pszFormattedName);
575 return rc;
576}
577
578
579#ifdef VBOX_STRICT
580/**
581 * Divide the strings into sub-strings using '/' as delimiter
582 * and then compare them in strcmp fashion.
583 *
584 * @returns Difference.
585 * @retval 0 if equal.
586 * @retval < 0 if psz1 is less than psz2.
587 * @retval > 0 if psz1 greater than psz2.
588 *
589 * @param psz1 The first string.
590 * @param psz2 The second string.
591 */
592static int stamR3SlashCompare(const char *psz1, const char *psz2)
593{
594 for (;;)
595 {
596 unsigned int ch1 = *psz1++;
597 unsigned int ch2 = *psz2++;
598 if (ch1 != ch2)
599 {
600 /* slash is end-of-sub-string, so it trumps everything but '\0'. */
601 if (ch1 == '/')
602 return ch2 ? -1 : 1;
603 if (ch2 == '/')
604 return ch1 ? 1 : -1;
605 return ch1 - ch2;
606 }
607
608 /* done? */
609 if (ch1 == '\0')
610 return 0;
611 }
612}
613#endif /* VBOX_STRICT */
614
615
616#ifdef STAM_WITH_LOOKUP_TREE
617
618/**
619 * Compares a lookup node with a name.
620 *
621 * @returns like strcmp and memcmp.
622 * @param pNode The lookup node.
623 * @param pchName The name, not necessarily terminated.
624 * @param cchName The length of the name.
625 */
626DECL_FORCE_INLINE(int) stamR3LookupCmp(PSTAMLOOKUP pNode, const char *pchName, uint32_t cchName)
627{
628 uint32_t cchComp = RT_MIN(pNode->cch, cchName);
629 int iDiff = memcmp(pNode->szName, pchName, cchComp);
630 if (!iDiff && pNode->cch != cchName)
631 iDiff = pNode->cch > cchName ? 2 : -2;
632 return iDiff;
633}
634
635
636/**
637 * Creates a new lookup child node.
638 *
639 * @returns Pointer to the newly created lookup node.
640 * @param pParent The parent node.
641 * @param pchName The name (not necessarily terminated).
642 * @param cchName The length of the name.
643 * @param offName The offset of the node in a path.
644 * @param iChild Child index of a node that's before the one
645 * we're inserting (returned by
646 * stamR3LookupFindChild).
647 */
648static PSTAMLOOKUP stamR3LookupNewChild(PSTAMLOOKUP pParent, const char *pchName, uint32_t cchName, uint32_t offName,
649 uint32_t iChild)
650{
651 Assert(cchName <= UINT8_MAX);
652 Assert(offName <= UINT8_MAX);
653 Assert(iChild < UINT16_MAX);
654
655 /*
656 * Allocate a new entry.
657 */
658 PSTAMLOOKUP pNew = (PSTAMLOOKUP)RTMemAlloc(RT_OFFSETOF(STAMLOOKUP, szName[cchName + 1]));
659 if (!pNew)
660 return NULL;
661 pNew->pParent = pParent;
662 pNew->papChildren = NULL;
663 pNew->pDesc = NULL;
664 pNew->cDescsInTree = 0;
665 pNew->cChildren = 0;
666 pNew->cch = (uint16_t)cchName;
667 pNew->off = (uint16_t)offName;
668 memcpy(pNew->szName, pchName, cchName);
669 pNew->szName[cchName] = '\0';
670
671 /*
672 * Reallocate the array?
673 */
674 if (RT_IS_POWER_OF_TWO(pParent->cChildren))
675 {
676 uint32_t cNew = pParent->cChildren ? (uint32_t)pParent->cChildren * 2 : 8;
677 AssertReturnStmt(cNew <= 0x8000, RTMemFree(pNew), NULL);
678 void *pvNew = RTMemRealloc(pParent->papChildren, cNew * sizeof(pParent->papChildren[0]));
679 if (!pvNew)
680 {
681 RTMemFree(pNew);
682 return NULL;
683 }
684 pParent->papChildren = (PSTAMLOOKUP *)pvNew;
685 }
686
687 /*
688 * Find the exact insertion point using iChild as a very good clue from
689 * the find function.
690 */
691 if (!pParent->cChildren)
692 iChild = 0;
693 else
694 {
695 if (iChild >= pParent->cChildren)
696 iChild = pParent->cChildren - 1;
697 while ( iChild < pParent->cChildren
698 && stamR3LookupCmp(pParent->papChildren[iChild], pchName, cchName) < 0)
699 iChild++;
700 }
701
702 /*
703 * Insert it.
704 */
705 if (iChild < pParent->cChildren)
706 {
707 /* Do shift. */
708 uint32_t i = pParent->cChildren;
709 while (i > iChild)
710 {
711 PSTAMLOOKUP pNode = pParent->papChildren[i - 1];
712 pParent->papChildren[i] = pNode;
713 pNode->iParent = i;
714 i--;
715 }
716 }
717
718 pNew->iParent = iChild;
719 pParent->papChildren[iChild] = pNew;
720 pParent->cChildren++;
721
722 return pNew;
723}
724
725
726/**
727 * Looks up a child.
728 *
729 * @returns Pointer to child node if found, NULL if not.
730 * @param pParent The parent node.
731 * @param pchName The name (not necessarily terminated).
732 * @param cchName The length of the name.
733 * @param piChild Where to store a child index suitable for
734 * passing to stamR3LookupNewChild when NULL is
735 * returned.
736 */
737static PSTAMLOOKUP stamR3LookupFindChild(PSTAMLOOKUP pParent, const char *pchName, uint32_t cchName, uint32_t *piChild)
738{
739 uint32_t iChild = pParent->cChildren;
740 if (iChild > 4)
741 {
742 uint32_t iFirst = 0;
743 uint32_t iEnd = iChild;
744 iChild /= 2;
745 for (;;)
746 {
747 int iDiff = stamR3LookupCmp(pParent->papChildren[iChild], pchName, cchName);
748 if (!iDiff)
749 {
750 if (piChild)
751 *piChild = iChild;
752 return pParent->papChildren[iChild];
753 }
754
755 /* Split. */
756 if (iDiff < 0)
757 {
758 iFirst = iChild + 1;
759 if (iFirst >= iEnd)
760 {
761 if (piChild)
762 *piChild = iChild;
763 break;
764 }
765 }
766 else
767 {
768 if (iChild == iFirst)
769 {
770 if (piChild)
771 *piChild = iChild ? iChild - 1 : 0;
772 break;
773 }
774 iEnd = iChild;
775 }
776
777 /* Calc next child. */
778 iChild = (iEnd - iFirst) / 2 + iFirst;
779 }
780 return NULL;
781 }
782
783 /*
784 * Linear search.
785 */
786 while (iChild-- > 0)
787 {
788 int iDiff = stamR3LookupCmp(pParent->papChildren[iChild], pchName, cchName);
789 if (iDiff <= 0)
790 {
791 if (piChild)
792 *piChild = iChild;
793 return !iDiff ? pParent->papChildren[iChild] : NULL;
794 }
795 }
796 if (piChild)
797 *piChild = 0;
798 return NULL;
799}
800
801
802/**
803 * Find the next sample descriptor node.
804 *
805 * This is for use with insertion in the big list and pattern range lookups.
806 *
807 * @returns Pointer to the next sample descriptor. NULL if not found (i.e.
808 * we're at the end of the list).
809 * @param pLookup The current node.
810 */
811static PSTAMDESC stamR3LookupFindNextWithDesc(PSTAMLOOKUP pLookup)
812{
813 Assert(!pLookup->pDesc);
814 PSTAMLOOKUP pCur = pLookup;
815 uint32_t iCur = 0;
816 for (;;)
817 {
818 /*
819 * Check all children.
820 */
821 uint32_t cChildren = pCur->cChildren;
822 if (iCur < cChildren)
823 {
824 PSTAMLOOKUP *papChildren = pCur->papChildren;
825 do
826 {
827 PSTAMLOOKUP pChild = pCur->papChildren[iCur];
828 if (pChild->pDesc)
829 return pChild->pDesc;
830
831 if (pChild->cChildren > 0)
832 {
833 /* One level down. */
834 iCur = 0;
835 pCur = pChild;
836 break;
837 }
838 } while (++iCur < cChildren);
839 }
840 else
841 {
842 /*
843 * One level up, resuming after the current.
844 */
845 iCur = pCur->iParent + 1;
846 pCur = pCur->pParent;
847 if (!pCur)
848 return NULL;
849 }
850 }
851}
852
853
854/**
855 * Look up a sample descriptor by name.
856 *
857 * @returns Pointer to a sample descriptor.
858 * @param pRoot The root node.
859 * @param pszName The name to lookup.
860 */
861static PSTAMDESC stamR3LookupFindDesc(PSTAMLOOKUP pRoot, const char *pszName)
862{
863 Assert(!pRoot->pParent);
864 while (*pszName++ == '/')
865 {
866 const char *pszEnd = strchr(pszName, '/');
867 uint32_t cch = pszEnd ? pszEnd - pszName : strlen(pszName);
868 PSTAMLOOKUP pChild = stamR3LookupFindChild(pRoot, pszName, cch, NULL);
869 if (!pChild)
870 break;
871 if (!pszEnd)
872 return pChild->pDesc;
873 pszName = pszEnd;
874 pRoot = pChild;
875 }
876
877 return NULL;
878}
879
880
881/**
882 * Finds the first sample descriptor for a given lookup range.
883 *
884 * This is for pattern range lookups.
885 *
886 * @returns Pointer to the first descriptor.
887 * @param pFirst The first node in the range.
888 * @param pLast The last node in the range.
889 */
890static PSTAMDESC stamR3LookupFindFirstDescForRange(PSTAMLOOKUP pFirst, PSTAMLOOKUP pLast)
891{
892 if (pFirst->pDesc)
893 return pFirst->pDesc;
894
895 PSTAMLOOKUP pCur = pFirst;
896 uint32_t iCur = 0;
897 for (;;)
898 {
899 uint32_t cChildren = pCur->cChildren;
900 if (iCur < pCur->cChildren)
901 {
902 /*
903 * Check all children.
904 */
905 PSTAMLOOKUP *papChildren = pCur->papChildren;
906 do
907 {
908 PSTAMLOOKUP pChild = pCur->papChildren[iCur];
909 if (pChild->pDesc)
910 return pChild->pDesc;
911 if (pChild->cChildren > 0)
912 {
913 /* One level down. */
914 iCur = 0;
915 pCur = pChild;
916 break;
917 }
918 if (pChild == pLast)
919 return NULL;
920 } while (++iCur < cChildren);
921 }
922 else
923 {
924 /*
925 * One level up, checking current and its 'older' sibilings.
926 */
927 if (pCur == pLast)
928 return NULL;
929 iCur = pCur->iParent + 1;
930 pCur = pCur->pParent;
931 if (!pCur)
932 break;
933 }
934 }
935
936 return NULL;
937}
938
939
940/**
941 * Finds the first sample descriptor for a given lookup range.
942 *
943 * This is for pattern range lookups.
944 *
945 * @returns Pointer to the first descriptor.
946 * @param pFirst The first node in the range.
947 * @param pLast The last node in the range.
948 */
949static PSTAMDESC stamR3LookupFindLastDescForRange(PSTAMLOOKUP pFirst, PSTAMLOOKUP pLast)
950{
951 PSTAMLOOKUP pCur = pLast;
952 uint32_t iCur = pCur->cChildren - 1;
953 for (;;)
954 {
955 if (iCur < pCur->cChildren)
956 {
957 /*
958 * Check children backwards, depth first.
959 */
960 PSTAMLOOKUP *papChildren = pCur->papChildren;
961 do
962 {
963 PSTAMLOOKUP pChild = pCur->papChildren[iCur];
964 if (pChild->cChildren > 0)
965 {
966 /* One level down. */
967 iCur = pChild->cChildren - 1;
968 pCur = pChild;
969 break;
970 }
971
972 if (pChild->pDesc)
973 return pChild->pDesc;
974 if (pChild == pFirst)
975 return NULL;
976 } while (iCur-- > 0); /* (underflow handled above) */
977 }
978 else
979 {
980 /*
981 * One level up, checking current and its 'older' sibilings.
982 */
983 if (pCur->pDesc)
984 return pCur->pDesc;
985 if (pCur == pFirst)
986 return NULL;
987 iCur = pCur->iParent - 1; /* (underflow handled above) */
988 pCur = pCur->pParent;
989 if (!pCur)
990 break;
991 }
992 }
993
994 return NULL;
995}
996
997
998/**
999 * Look up the first and last descriptors for a (single) pattern expression.
1000 *
1001 * This is used to optimize pattern enumerations and doesn't have to return 100%
1002 * accurate results if that costs too much.
1003 *
1004 * @returns Pointer to the first descriptor in the range.
1005 * @param pRoot The root node.
1006 * @param pList The descriptor list anchor.
1007 * @param pszPat The name patter to lookup.
1008 * @param ppLastDesc Where to store the address of the last
1009 * descriptor (approximate).
1010 */
1011static PSTAMDESC stamR3LookupFindPatternDescRange(PSTAMLOOKUP pRoot, PRTLISTANCHOR pList, const char *pszPat,
1012 PSTAMDESC *ppLastDesc)
1013{
1014 Assert(!pRoot->pParent);
1015
1016 /*
1017 * If there is an early enough wildcard, the whole list needs to be searched.
1018 */
1019 if ( pszPat[0] == '*' || pszPat[0] == '?'
1020 || pszPat[1] == '*' || pszPat[1] == '?')
1021 {
1022 *ppLastDesc = RTListGetLast(pList, STAMDESC, ListEntry);
1023 return RTListGetFirst(pList, STAMDESC, ListEntry);
1024 }
1025
1026 /*
1027 * All statistics starts with a slash.
1028 */
1029 while ( *pszPat++ == '/'
1030 && pRoot->cDescsInTree > 0
1031 && pRoot->cChildren > 0)
1032 {
1033 const char *pszEnd = strchr(pszPat, '/');
1034 uint32_t cch = pszEnd ? pszEnd - pszPat : strlen(pszPat);
1035 if (!cch)
1036 break;
1037
1038 const char *pszPat1 = (const char *)memchr(pszPat, '*', cch);
1039 const char *pszPat2 = (const char *)memchr(pszPat, '?', cch);
1040 if (pszPat1 || pszPat2)
1041 {
1042 /* We've narrowed it down to a sub-tree now. */
1043 PSTAMLOOKUP pFirst = pRoot->papChildren[0];
1044 PSTAMLOOKUP pLast = pRoot->papChildren[pRoot->cChildren - 1];
1045 /** @todo narrow the range further if both pszPat1/2 != pszPat. */
1046
1047 *ppLastDesc = stamR3LookupFindLastDescForRange(pFirst, pLast);
1048 return stamR3LookupFindFirstDescForRange(pFirst, pLast);
1049 }
1050
1051 PSTAMLOOKUP pChild = stamR3LookupFindChild(pRoot, pszPat, cch, NULL);
1052 if (!pChild)
1053 break;
1054
1055 /* Advance */
1056 if (!pszEnd)
1057 return *ppLastDesc = pChild->pDesc;
1058 pszPat = pszEnd;
1059 pRoot = pChild;
1060 }
1061
1062 /* No match. */
1063 *ppLastDesc = NULL;
1064 return NULL;
1065}
1066
1067
1068/**
1069 * Increments the cDescInTree member of the given node an all ancestors.
1070 *
1071 * @param pLookup The lookup node.
1072 */
1073static void stamR3LookupIncUsage(PSTAMLOOKUP pLookup)
1074{
1075 Assert(pLookup->pDesc);
1076
1077 PSTAMLOOKUP pCur = pLookup;
1078 while (pCur != NULL)
1079 {
1080 pCur->cDescsInTree++;
1081 pCur = pCur->pParent;
1082 }
1083}
1084
1085
1086/**
1087 * Descrements the cDescInTree member of the given node an all ancestors.
1088 *
1089 * @param pLookup The lookup node.
1090 */
1091static void stamR3LookupDecUsage(PSTAMLOOKUP pLookup)
1092{
1093 Assert(!pLookup->pDesc);
1094
1095 PSTAMLOOKUP pCur = pLookup;
1096 while (pCur != NULL)
1097 {
1098 Assert(pCur->cDescsInTree > 0);
1099 pCur->cDescsInTree--;
1100 pCur = pCur->pParent;
1101 }
1102}
1103
1104
1105/**
1106 * Frees empty lookup nodes if it's worth it.
1107 *
1108 * @param pLookup The lookup node.
1109 */
1110static void stamR3LookupMaybeFree(PSTAMLOOKUP pLookup)
1111{
1112 Assert(!pLookup->pDesc);
1113
1114 /*
1115 * Free between two and three levels of nodes. Freeing too much most
1116 * likely wasted effort since we're either going to repopluate the tree
1117 * or quit the whole thing.
1118 */
1119 if (pLookup->cDescsInTree > 0)
1120 return;
1121
1122 PSTAMLOOKUP pCur = pLookup->pParent;
1123 if (!pCur)
1124 return;
1125 if (pCur->cDescsInTree > 0)
1126 return;
1127 PSTAMLOOKUP pParent = pCur->pParent;
1128 if (pParent)
1129 return;
1130
1131 if (pParent->cDescsInTree == 0 && pParent->pParent)
1132 {
1133 pCur = pParent;
1134 pParent = pCur->pParent;
1135 }
1136
1137 /*
1138 * Remove pCur from pParent.
1139 */
1140 PSTAMLOOKUP *papChildren = pParent->papChildren;
1141 uint32_t cChildren = --pParent->cChildren;
1142 for (uint32_t i = pCur->iParent; i < cChildren; i++)
1143 {
1144 PSTAMLOOKUP pChild = papChildren[i + 1];
1145 pChild->iParent = i;
1146 papChildren[i] = pChild;
1147 }
1148 pCur->pParent = NULL;
1149
1150 /*
1151 * Destroy pCur.
1152 */
1153 stamR3LookupDestroyTree(pCur);
1154}
1155
1156
1157/**
1158 * Destroys a lookup tree.
1159 *
1160 * This is used by STAMR3Term as well as stamR3LookupMaybeFree.
1161 *
1162 * @param pRoot The root of the tree (must have no parent).
1163 */
1164static void stamR3LookupDestroyTree(PSTAMLOOKUP pRoot)
1165{
1166 Assert(pRoot); Assert(!pRoot->pParent);
1167 PSTAMLOOKUP pLookup = pRoot;
1168 for (;;)
1169 {
1170 uint32_t i = pLookup->cChildren;
1171 if (i > 0)
1172 {
1173 /*
1174 * Push child (with leaf optimization).
1175 */
1176 PSTAMLOOKUP pChild = pLookup->papChildren[--i];
1177 if (pChild->cChildren != 0)
1178 pLookup = pChild;
1179 else
1180 {
1181 /* free leaves. */
1182 for (;;)
1183 {
1184 if (pChild->papChildren)
1185 {
1186 RTMemFree(pChild->papChildren);
1187 pChild->papChildren = NULL;
1188 }
1189 RTMemFree(pChild);
1190
1191 /* next */
1192 if (i == 0)
1193 {
1194 pLookup->papChildren = 0;
1195 break;
1196 }
1197 pChild = pLookup->papChildren[--i];
1198 if (pChild->cChildren != 0)
1199 {
1200 pLookup->cChildren = i + 1;
1201 pLookup = pChild;
1202 break;
1203 }
1204 }
1205 }
1206 }
1207 else
1208 {
1209 /*
1210 * Pop and free current.
1211 */
1212 Assert(!pLookup->pDesc);
1213
1214 PSTAMLOOKUP pParent = pLookup->pParent;
1215 RTMemFree(pLookup->papChildren);
1216 pLookup->papChildren = NULL;
1217 RTMemFree(pLookup);
1218
1219 pLookup = pParent;
1220 if (!pLookup)
1221 break;
1222 pLookup->cChildren--;
1223 }
1224 }
1225}
1226
1227#endif /* STAM_WITH_LOOKUP_TREE */
1228
1229
1230
1231/**
1232 * Internal worker for the different register calls.
1233 *
1234 * @returns VBox status.
1235 * @param pUVM Pointer to the user mode VM structure.
1236 * @param pvSample Pointer to the sample.
1237 * @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
1238 * @param pfnPrint Print the sample.
1239 * @param enmType Sample type. This indicates what pvSample is pointing at.
1240 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
1241 * @param enmUnit Sample unit.
1242 * @param pszDesc Sample description.
1243 * @param pszName The sample name format string.
1244 * @param args Arguments to the format string.
1245 * @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
1246 */
1247static int stamR3RegisterU(PUVM pUVM, void *pvSample, PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
1248 STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
1249{
1250 AssertReturn(pszName[0] == '/', VERR_INVALID_NAME);
1251 AssertReturn(pszName[1] != '/' && pszName[1], VERR_INVALID_NAME);
1252 uint32_t const cchName = (uint32_t)strlen(pszName);
1253 AssertReturn(cchName < 256, VERR_OUT_OF_RANGE);
1254 AssertReturn(pszName[cchName - 1] != '/', VERR_INVALID_NAME);
1255 AssertReturn(memchr(pszName, '\\', cchName) == NULL, VERR_INVALID_NAME);
1256
1257 STAM_LOCK_WR(pUVM);
1258
1259 /*
1260 * Look up the tree location, populating the lookup tree as we walk it.
1261 */
1262#ifdef STAM_WITH_LOOKUP_TREE
1263 PSTAMLOOKUP pLookup = pUVM->stam.s.pRoot; Assert(pLookup);
1264 uint32_t offName = 1;
1265 for (;;)
1266 {
1267 /* Get the next part of the path. */
1268 const char *pszStart = &pszName[offName];
1269 const char *pszEnd = strchr(pszStart, '/');
1270 uint32_t cch = pszEnd ? (uint32_t)(pszEnd - pszStart) : cchName - offName;
1271 if (cch == 0)
1272 {
1273 STAM_UNLOCK_WR(pUVM);
1274 AssertMsgFailed(("No double or trailing slashes are allowed: '%s'\n", pszName));
1275 return VERR_INVALID_NAME;
1276 }
1277
1278 /* Do the looking up. */
1279 uint32_t iChild = 0;
1280 PSTAMLOOKUP pChild = stamR3LookupFindChild(pLookup, pszStart, cch, &iChild);
1281 if (!pChild)
1282 {
1283 pChild = stamR3LookupNewChild(pLookup, pszStart, cch, offName, iChild);
1284 if (!pChild)
1285 {
1286 STAM_UNLOCK_WR(pUVM);
1287 return VERR_NO_MEMORY;
1288 }
1289 }
1290
1291 /* Advance. */
1292 pLookup = pChild;
1293 if (!pszEnd)
1294 break;
1295 offName += cch + 1;
1296 }
1297 if (pLookup->pDesc)
1298 {
1299 STAM_UNLOCK_WR(pUVM);
1300 AssertMsgFailed(("Duplicate sample name: %s\n", pszName));
1301 return VERR_ALREADY_EXISTS;
1302 }
1303
1304 PSTAMDESC pCur = stamR3LookupFindNextWithDesc(pLookup);
1305
1306#else
1307 PSTAMDESC pCur;
1308 RTListForEach(&pUVM->stam.s.List, pCur, STAMDESC, ListEntry)
1309 {
1310 int iDiff = strcmp(pCur->pszName, pszName);
1311 /* passed it */
1312 if (iDiff > 0)
1313 break;
1314 /* found it. */
1315 if (!iDiff)
1316 {
1317 STAM_UNLOCK_WR(pUVM);
1318 AssertMsgFailed(("Duplicate sample name: %s\n", pszName));
1319 return VERR_ALREADY_EXISTS;
1320 }
1321 }
1322#endif
1323
1324 /*
1325 * Check that the name doesn't screw up sorting order when taking
1326 * slashes into account. The QT4 GUI makes some assumptions.
1327 * Problematic chars are: !"#$%&'()*+,-.
1328 */
1329#ifdef VBOX_STRICT
1330 Assert(pszName[0] == '/');
1331 PSTAMDESC pPrev = pCur
1332 ? RTListGetPrev(&pUVM->stam.s.List, pCur, STAMDESC, ListEntry)
1333 : RTListGetLast(&pUVM->stam.s.List, STAMDESC, ListEntry);
1334 Assert(!pPrev || strcmp(pszName, pPrev->pszName) > 0);
1335 Assert(!pCur || strcmp(pszName, pCur->pszName) < 0);
1336 Assert(!pPrev || stamR3SlashCompare(pPrev->pszName, pszName) < 0);
1337 Assert(!pCur || stamR3SlashCompare(pCur->pszName, pszName) > 0);
1338
1339 /*
1340 * Check alignment requirements.
1341 */
1342 switch (enmType)
1343 {
1344 /* 8 byte / 64-bit */
1345 case STAMTYPE_U64:
1346 case STAMTYPE_U64_RESET:
1347 case STAMTYPE_X64:
1348 case STAMTYPE_X64_RESET:
1349 case STAMTYPE_COUNTER:
1350 case STAMTYPE_PROFILE:
1351 case STAMTYPE_PROFILE_ADV:
1352 AssertMsg(!((uintptr_t)pvSample & 7), ("%p - %s\n", pvSample, pszName));
1353 break;
1354
1355 /* 4 byte / 32-bit */
1356 case STAMTYPE_RATIO_U32:
1357 case STAMTYPE_RATIO_U32_RESET:
1358 case STAMTYPE_U32:
1359 case STAMTYPE_U32_RESET:
1360 case STAMTYPE_X32:
1361 case STAMTYPE_X32_RESET:
1362 AssertMsg(!((uintptr_t)pvSample & 3), ("%p - %s\n", pvSample, pszName));
1363 break;
1364
1365 /* 2 byte / 32-bit */
1366 case STAMTYPE_U16:
1367 case STAMTYPE_U16_RESET:
1368 case STAMTYPE_X16:
1369 case STAMTYPE_X16_RESET:
1370 AssertMsg(!((uintptr_t)pvSample & 1), ("%p - %s\n", pvSample, pszName));
1371 break;
1372
1373 /* 1 byte / 8-bit / unaligned */
1374 case STAMTYPE_U8:
1375 case STAMTYPE_U8_RESET:
1376 case STAMTYPE_X8:
1377 case STAMTYPE_X8_RESET:
1378 case STAMTYPE_BOOL:
1379 case STAMTYPE_BOOL_RESET:
1380 case STAMTYPE_CALLBACK:
1381 break;
1382
1383 default:
1384 AssertMsgFailed(("%d\n", enmType));
1385 break;
1386 }
1387#endif /* VBOX_STRICT */
1388
1389 /*
1390 * Create a new node and insert it at the current location.
1391 */
1392 int rc;
1393 size_t cbDesc = pszDesc ? strlen(pszDesc) + 1 : 0;
1394 PSTAMDESC pNew = (PSTAMDESC)RTMemAlloc(sizeof(*pNew) + cchName + 1 + cbDesc);
1395 if (pNew)
1396 {
1397 pNew->pszName = (char *)memcpy((char *)(pNew + 1), pszName, cchName + 1);
1398 pNew->enmType = enmType;
1399 pNew->enmVisibility = enmVisibility;
1400 if (enmType != STAMTYPE_CALLBACK)
1401 pNew->u.pv = pvSample;
1402 else
1403 {
1404 pNew->u.Callback.pvSample = pvSample;
1405 pNew->u.Callback.pfnReset = pfnReset;
1406 pNew->u.Callback.pfnPrint = pfnPrint;
1407 }
1408 pNew->enmUnit = enmUnit;
1409 pNew->pszDesc = NULL;
1410 if (pszDesc)
1411 pNew->pszDesc = (char *)memcpy((char *)(pNew + 1) + cchName + 1, pszDesc, cbDesc);
1412
1413 if (pCur)
1414 RTListNodeInsertBefore(&pCur->ListEntry, &pNew->ListEntry);
1415 else
1416 RTListAppend(&pUVM->stam.s.List, &pNew->ListEntry);
1417
1418#ifdef STAM_WITH_LOOKUP_TREE
1419 pNew->pLookup = pLookup;
1420 pLookup->pDesc = pNew;
1421 stamR3LookupIncUsage(pLookup);
1422#endif
1423
1424 stamR3ResetOne(pNew, pUVM->pVM);
1425 rc = VINF_SUCCESS;
1426 }
1427 else
1428 rc = VERR_NO_MEMORY;
1429
1430 STAM_UNLOCK_WR(pUVM);
1431 return rc;
1432}
1433
1434
1435/**
1436 * Deregisters a sample previously registered by STAR3Register().
1437 *
1438 * This is intended used for devices which can be unplugged and for
1439 * temporary samples.
1440 *
1441 * @returns VBox status.
1442 * @param pUVM Pointer to the user mode VM structure.
1443 * @param pvSample Pointer to the sample registered with STAMR3Register().
1444 */
1445VMMR3DECL(int) STAMR3DeregisterU(PUVM pUVM, void *pvSample)
1446{
1447 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1448
1449 STAM_LOCK_WR(pUVM);
1450
1451 /*
1452 * Search for it.
1453 */
1454 int rc = VERR_INVALID_HANDLE;
1455 PSTAMDESC pCur, pNext;
1456 RTListForEachSafe(&pUVM->stam.s.List, pCur, pNext, STAMDESC, ListEntry)
1457 {
1458 if (pCur->u.pv == pvSample)
1459 {
1460 RTListNodeRemove(&pCur->ListEntry);
1461#ifdef STAM_WITH_LOOKUP_TREE
1462 pCur->pLookup->pDesc = NULL; /** @todo free lookup nodes once it's working. */
1463 stamR3LookupDecUsage(pCur->pLookup);
1464 stamR3LookupMaybeFree(pCur->pLookup);
1465#endif
1466 RTMemFree(pCur);
1467 rc = VINF_SUCCESS;
1468 }
1469 }
1470
1471 STAM_UNLOCK_WR(pUVM);
1472 return rc;
1473}
1474
1475
1476/**
1477 * Deregisters a sample previously registered by STAR3Register().
1478 *
1479 * This is intended used for devices which can be unplugged and for
1480 * temporary samples.
1481 *
1482 * @returns VBox status.
1483 * @param pVM Pointer to the VM.
1484 * @param pvSample Pointer to the sample registered with STAMR3Register().
1485 */
1486VMMR3DECL(int) STAMR3Deregister(PVM pVM, void *pvSample)
1487{
1488 return STAMR3DeregisterU(pVM->pUVM, pvSample);
1489}
1490
1491
1492/**
1493 * Resets statistics for the specified VM.
1494 * It's possible to select a subset of the samples.
1495 *
1496 * @returns VBox status. (Basically, it cannot fail.)
1497 * @param pUVM The user mode VM handle.
1498 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
1499 * If NULL all samples are reset.
1500 * @remarks Don't confuse this with the other 'XYZR3Reset' methods, it's not called at VM reset.
1501 */
1502VMMR3DECL(int) STAMR3Reset(PUVM pUVM, const char *pszPat)
1503{
1504 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1505 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
1506
1507 int rc = VINF_SUCCESS;
1508
1509 /* ring-0 */
1510 GVMMRESETSTATISTICSSREQ GVMMReq;
1511 GMMRESETSTATISTICSSREQ GMMReq;
1512 bool fGVMMMatched = !pszPat || !*pszPat;
1513 bool fGMMMatched = fGVMMMatched;
1514 if (fGVMMMatched)
1515 {
1516 memset(&GVMMReq.Stats, 0xff, sizeof(GVMMReq.Stats));
1517 memset(&GMMReq.Stats, 0xff, sizeof(GMMReq.Stats));
1518 }
1519 else
1520 {
1521 char *pszCopy;
1522 unsigned cExpressions;
1523 char **papszExpressions = stamR3SplitPattern(pszPat, &cExpressions, &pszCopy);
1524 if (!papszExpressions)
1525 return VERR_NO_MEMORY;
1526
1527 /* GVMM */
1528 RT_ZERO(GVMMReq.Stats);
1529 for (unsigned i = 0; i < RT_ELEMENTS(g_aGVMMStats); i++)
1530 if (stamR3MultiMatch(papszExpressions, cExpressions, NULL, g_aGVMMStats[i].pszName))
1531 {
1532 *((uint8_t *)&GVMMReq.Stats + g_aGVMMStats[i].offVar) = 0xff;
1533 fGVMMMatched = true;
1534 }
1535 if (!fGVMMMatched)
1536 {
1537 /** @todo match cpu leaves some rainy day. */
1538 }
1539
1540 /* GMM */
1541 RT_ZERO(GMMReq.Stats);
1542 for (unsigned i = 0; i < RT_ELEMENTS(g_aGMMStats); i++)
1543 if (stamR3MultiMatch(papszExpressions, cExpressions, NULL, g_aGMMStats[i].pszName))
1544 {
1545 *((uint8_t *)&GMMReq.Stats + g_aGMMStats[i].offVar) = 0xff;
1546 fGMMMatched = true;
1547 }
1548
1549 RTMemTmpFree(papszExpressions);
1550 RTStrFree(pszCopy);
1551 }
1552
1553 STAM_LOCK_WR(pUVM);
1554
1555 if (fGVMMMatched)
1556 {
1557 PVM pVM = pUVM->pVM;
1558 GVMMReq.Hdr.cbReq = sizeof(GVMMReq);
1559 GVMMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
1560 GVMMReq.pSession = pVM->pSession;
1561 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, NIL_VMCPUID, VMMR0_DO_GVMM_RESET_STATISTICS, 0, &GVMMReq.Hdr);
1562 }
1563
1564 if (fGMMMatched)
1565 {
1566 PVM pVM = pUVM->pVM;
1567 GMMReq.Hdr.cbReq = sizeof(GMMReq);
1568 GMMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
1569 GMMReq.pSession = pVM->pSession;
1570 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, NIL_VMCPUID, VMMR0_DO_GMM_RESET_STATISTICS, 0, &GMMReq.Hdr);
1571 }
1572
1573 /* and the reset */
1574 stamR3EnumU(pUVM, pszPat, false /* fUpdateRing0 */, stamR3ResetOne, pUVM->pVM);
1575
1576 STAM_UNLOCK_WR(pUVM);
1577 return rc;
1578}
1579
1580
1581/**
1582 * Resets one statistics sample.
1583 * Callback for stamR3EnumU().
1584 *
1585 * @returns VINF_SUCCESS
1586 * @param pDesc Pointer to the current descriptor.
1587 * @param pvArg User argument - Pointer to the VM.
1588 */
1589static int stamR3ResetOne(PSTAMDESC pDesc, void *pvArg)
1590{
1591 switch (pDesc->enmType)
1592 {
1593 case STAMTYPE_COUNTER:
1594 ASMAtomicXchgU64(&pDesc->u.pCounter->c, 0);
1595 break;
1596
1597 case STAMTYPE_PROFILE:
1598 case STAMTYPE_PROFILE_ADV:
1599 ASMAtomicXchgU64(&pDesc->u.pProfile->cPeriods, 0);
1600 ASMAtomicXchgU64(&pDesc->u.pProfile->cTicks, 0);
1601 ASMAtomicXchgU64(&pDesc->u.pProfile->cTicksMax, 0);
1602 ASMAtomicXchgU64(&pDesc->u.pProfile->cTicksMin, ~0);
1603 break;
1604
1605 case STAMTYPE_RATIO_U32_RESET:
1606 ASMAtomicXchgU32(&pDesc->u.pRatioU32->u32A, 0);
1607 ASMAtomicXchgU32(&pDesc->u.pRatioU32->u32B, 0);
1608 break;
1609
1610 case STAMTYPE_CALLBACK:
1611 if (pDesc->u.Callback.pfnReset)
1612 pDesc->u.Callback.pfnReset((PVM)pvArg, pDesc->u.Callback.pvSample);
1613 break;
1614
1615 case STAMTYPE_U8_RESET:
1616 case STAMTYPE_X8_RESET:
1617 ASMAtomicXchgU8(pDesc->u.pu8, 0);
1618 break;
1619
1620 case STAMTYPE_U16_RESET:
1621 case STAMTYPE_X16_RESET:
1622 ASMAtomicXchgU16(pDesc->u.pu16, 0);
1623 break;
1624
1625 case STAMTYPE_U32_RESET:
1626 case STAMTYPE_X32_RESET:
1627 ASMAtomicXchgU32(pDesc->u.pu32, 0);
1628 break;
1629
1630 case STAMTYPE_U64_RESET:
1631 case STAMTYPE_X64_RESET:
1632 ASMAtomicXchgU64(pDesc->u.pu64, 0);
1633 break;
1634
1635 case STAMTYPE_BOOL_RESET:
1636 ASMAtomicXchgBool(pDesc->u.pf, false);
1637 break;
1638
1639 /* These are custom and will not be touched. */
1640 case STAMTYPE_U8:
1641 case STAMTYPE_X8:
1642 case STAMTYPE_U16:
1643 case STAMTYPE_X16:
1644 case STAMTYPE_U32:
1645 case STAMTYPE_X32:
1646 case STAMTYPE_U64:
1647 case STAMTYPE_X64:
1648 case STAMTYPE_RATIO_U32:
1649 case STAMTYPE_BOOL:
1650 break;
1651
1652 default:
1653 AssertMsgFailed(("enmType=%d\n", pDesc->enmType));
1654 break;
1655 }
1656 NOREF(pvArg);
1657 return VINF_SUCCESS;
1658}
1659
1660
1661/**
1662 * Get a snapshot of the statistics.
1663 * It's possible to select a subset of the samples.
1664 *
1665 * @returns VBox status. (Basically, it cannot fail.)
1666 * @param pUVM The user mode VM handle.
1667 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
1668 * If NULL all samples are reset.
1669 * @param fWithDesc Whether to include the descriptions.
1670 * @param ppszSnapshot Where to store the pointer to the snapshot data.
1671 * The format of the snapshot should be XML, but that will have to be discussed
1672 * when this function is implemented.
1673 * The returned pointer must be freed by calling STAMR3SnapshotFree().
1674 * @param pcchSnapshot Where to store the size of the snapshot data. (Excluding the trailing '\0')
1675 */
1676VMMR3DECL(int) STAMR3Snapshot(PUVM pUVM, const char *pszPat, char **ppszSnapshot, size_t *pcchSnapshot, bool fWithDesc)
1677{
1678 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1679 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
1680
1681 STAMR3SNAPSHOTONE State = { NULL, NULL, NULL, pUVM->pVM, 0, VINF_SUCCESS, fWithDesc };
1682
1683 /*
1684 * Write the XML header.
1685 */
1686 /** @todo Make this proper & valid XML. */
1687 stamR3SnapshotPrintf(&State, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
1688
1689 /*
1690 * Write the content.
1691 */
1692 stamR3SnapshotPrintf(&State, "<Statistics>\n");
1693 int rc = stamR3EnumU(pUVM, pszPat, true /* fUpdateRing0 */, stamR3SnapshotOne, &State);
1694 stamR3SnapshotPrintf(&State, "</Statistics>\n");
1695
1696 if (RT_SUCCESS(rc))
1697 rc = State.rc;
1698 else
1699 {
1700 RTMemFree(State.pszStart);
1701 State.pszStart = State.pszEnd = State.psz = NULL;
1702 State.cbAllocated = 0;
1703 }
1704
1705 /*
1706 * Done.
1707 */
1708 *ppszSnapshot = State.pszStart;
1709 if (pcchSnapshot)
1710 *pcchSnapshot = State.psz - State.pszStart;
1711 return rc;
1712}
1713
1714
1715/**
1716 * stamR3EnumU callback employed by STAMR3Snapshot.
1717 *
1718 * @returns VBox status code, but it's interpreted as 0 == success / !0 == failure by enmR3Enum.
1719 * @param pDesc The sample.
1720 * @param pvArg The snapshot status structure.
1721 */
1722static int stamR3SnapshotOne(PSTAMDESC pDesc, void *pvArg)
1723{
1724 PSTAMR3SNAPSHOTONE pThis = (PSTAMR3SNAPSHOTONE)pvArg;
1725
1726 switch (pDesc->enmType)
1727 {
1728 case STAMTYPE_COUNTER:
1729 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pCounter->c == 0)
1730 return VINF_SUCCESS;
1731 stamR3SnapshotPrintf(pThis, "<Counter c=\"%lld\"", pDesc->u.pCounter->c);
1732 break;
1733
1734 case STAMTYPE_PROFILE:
1735 case STAMTYPE_PROFILE_ADV:
1736 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pProfile->cPeriods == 0)
1737 return VINF_SUCCESS;
1738 stamR3SnapshotPrintf(pThis, "<Profile cPeriods=\"%lld\" cTicks=\"%lld\" cTicksMin=\"%lld\" cTicksMax=\"%lld\"",
1739 pDesc->u.pProfile->cPeriods, pDesc->u.pProfile->cTicks, pDesc->u.pProfile->cTicksMin,
1740 pDesc->u.pProfile->cTicksMax);
1741 break;
1742
1743 case STAMTYPE_RATIO_U32:
1744 case STAMTYPE_RATIO_U32_RESET:
1745 if (pDesc->enmVisibility == STAMVISIBILITY_USED && !pDesc->u.pRatioU32->u32A && !pDesc->u.pRatioU32->u32B)
1746 return VINF_SUCCESS;
1747 stamR3SnapshotPrintf(pThis, "<Ratio32 u32A=\"%lld\" u32B=\"%lld\"",
1748 pDesc->u.pRatioU32->u32A, pDesc->u.pRatioU32->u32B);
1749 break;
1750
1751 case STAMTYPE_CALLBACK:
1752 {
1753 char szBuf[512];
1754 pDesc->u.Callback.pfnPrint(pThis->pVM, pDesc->u.Callback.pvSample, szBuf, sizeof(szBuf));
1755 stamR3SnapshotPrintf(pThis, "<Callback val=\"%s\"", szBuf);
1756 break;
1757 }
1758
1759 case STAMTYPE_U8:
1760 case STAMTYPE_U8_RESET:
1761 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
1762 return VINF_SUCCESS;
1763 stamR3SnapshotPrintf(pThis, "<U8 val=\"%u\"", *pDesc->u.pu8);
1764 break;
1765
1766 case STAMTYPE_X8:
1767 case STAMTYPE_X8_RESET:
1768 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
1769 return VINF_SUCCESS;
1770 stamR3SnapshotPrintf(pThis, "<X8 val=\"%#x\"", *pDesc->u.pu8);
1771 break;
1772
1773 case STAMTYPE_U16:
1774 case STAMTYPE_U16_RESET:
1775 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
1776 return VINF_SUCCESS;
1777 stamR3SnapshotPrintf(pThis, "<U16 val=\"%u\"", *pDesc->u.pu16);
1778 break;
1779
1780 case STAMTYPE_X16:
1781 case STAMTYPE_X16_RESET:
1782 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
1783 return VINF_SUCCESS;
1784 stamR3SnapshotPrintf(pThis, "<X16 val=\"%#x\"", *pDesc->u.pu16);
1785 break;
1786
1787 case STAMTYPE_U32:
1788 case STAMTYPE_U32_RESET:
1789 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
1790 return VINF_SUCCESS;
1791 stamR3SnapshotPrintf(pThis, "<U32 val=\"%u\"", *pDesc->u.pu32);
1792 break;
1793
1794 case STAMTYPE_X32:
1795 case STAMTYPE_X32_RESET:
1796 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
1797 return VINF_SUCCESS;
1798 stamR3SnapshotPrintf(pThis, "<X32 val=\"%#x\"", *pDesc->u.pu32);
1799 break;
1800
1801 case STAMTYPE_U64:
1802 case STAMTYPE_U64_RESET:
1803 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
1804 return VINF_SUCCESS;
1805 stamR3SnapshotPrintf(pThis, "<U64 val=\"%llu\"", *pDesc->u.pu64);
1806 break;
1807
1808 case STAMTYPE_X64:
1809 case STAMTYPE_X64_RESET:
1810 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
1811 return VINF_SUCCESS;
1812 stamR3SnapshotPrintf(pThis, "<X64 val=\"%#llx\"", *pDesc->u.pu64);
1813 break;
1814
1815 case STAMTYPE_BOOL:
1816 case STAMTYPE_BOOL_RESET:
1817 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pf == false)
1818 return VINF_SUCCESS;
1819 stamR3SnapshotPrintf(pThis, "<BOOL val=\"%RTbool\"", *pDesc->u.pf);
1820 break;
1821
1822 default:
1823 AssertMsgFailed(("%d\n", pDesc->enmType));
1824 return 0;
1825 }
1826
1827 stamR3SnapshotPrintf(pThis, " unit=\"%s\"", STAMR3GetUnit(pDesc->enmUnit));
1828
1829 switch (pDesc->enmVisibility)
1830 {
1831 default:
1832 case STAMVISIBILITY_ALWAYS:
1833 break;
1834 case STAMVISIBILITY_USED:
1835 stamR3SnapshotPrintf(pThis, " vis=\"used\"");
1836 break;
1837 case STAMVISIBILITY_NOT_GUI:
1838 stamR3SnapshotPrintf(pThis, " vis=\"not-gui\"");
1839 break;
1840 }
1841
1842 stamR3SnapshotPrintf(pThis, " name=\"%s\"", pDesc->pszName);
1843
1844 if (pThis->fWithDesc && pDesc->pszDesc)
1845 {
1846 /*
1847 * The description is a bit tricky as it may include chars that
1848 * xml requires to be escaped.
1849 */
1850 const char *pszBadChar = strpbrk(pDesc->pszDesc, "&<>\"'");
1851 if (!pszBadChar)
1852 return stamR3SnapshotPrintf(pThis, " desc=\"%s\"/>\n", pDesc->pszDesc);
1853
1854 stamR3SnapshotPrintf(pThis, " desc=\"");
1855 const char *pszCur = pDesc->pszDesc;
1856 do
1857 {
1858 stamR3SnapshotPrintf(pThis, "%.*s", pszBadChar - pszCur, pszCur);
1859 switch (*pszBadChar)
1860 {
1861 case '&': stamR3SnapshotPrintf(pThis, "&amp;"); break;
1862 case '<': stamR3SnapshotPrintf(pThis, "&lt;"); break;
1863 case '>': stamR3SnapshotPrintf(pThis, "&gt;"); break;
1864 case '"': stamR3SnapshotPrintf(pThis, "&quot;"); break;
1865 case '\'': stamR3SnapshotPrintf(pThis, "&apos;"); break;
1866 default: AssertMsgFailed(("%c", *pszBadChar)); break;
1867 }
1868 pszCur = pszBadChar + 1;
1869 pszBadChar = strpbrk(pszCur, "&<>\"'");
1870 } while (pszBadChar);
1871 return stamR3SnapshotPrintf(pThis, "%s\"/>\n", pszCur);
1872 }
1873 return stamR3SnapshotPrintf(pThis, "/>\n");
1874}
1875
1876
1877/**
1878 * Output callback for stamR3SnapshotPrintf.
1879 *
1880 * @returns number of bytes written.
1881 * @param pvArg The snapshot status structure.
1882 * @param pach Pointer to an array of characters (bytes).
1883 * @param cch The number or chars (bytes) to write from the array.
1884 */
1885static DECLCALLBACK(size_t) stamR3SnapshotOutput(void *pvArg, const char *pach, size_t cch)
1886{
1887 PSTAMR3SNAPSHOTONE pThis = (PSTAMR3SNAPSHOTONE)pvArg;
1888
1889 /*
1890 * Make sure we've got space for it.
1891 */
1892 if (RT_UNLIKELY((uintptr_t)pThis->pszEnd - (uintptr_t)pThis->psz < cch + 1))
1893 {
1894 if (RT_FAILURE(pThis->rc))
1895 return 0;
1896
1897 size_t cbNewSize = pThis->cbAllocated;
1898 if (cbNewSize > cch)
1899 cbNewSize *= 2;
1900 else
1901 cbNewSize += RT_ALIGN(cch + 1, 0x1000);
1902 char *pszNew = (char *)RTMemRealloc(pThis->pszStart, cbNewSize);
1903 if (!pszNew)
1904 {
1905 /*
1906 * Free up immediately, out-of-memory is bad news and this
1907 * isn't an important allocations / API.
1908 */
1909 pThis->rc = VERR_NO_MEMORY;
1910 RTMemFree(pThis->pszStart);
1911 pThis->pszStart = pThis->pszEnd = pThis->psz = NULL;
1912 pThis->cbAllocated = 0;
1913 return 0;
1914 }
1915
1916 pThis->psz = pszNew + (pThis->psz - pThis->pszStart);
1917 pThis->pszStart = pszNew;
1918 pThis->pszEnd = pszNew + cbNewSize;
1919 pThis->cbAllocated = cbNewSize;
1920 }
1921
1922 /*
1923 * Copy the chars to the buffer and terminate it.
1924 */
1925 memcpy(pThis->psz, pach, cch);
1926 pThis->psz += cch;
1927 *pThis->psz = '\0';
1928 return cch;
1929}
1930
1931
1932/**
1933 * Wrapper around RTStrFormatV for use by the snapshot API.
1934 *
1935 * @returns VBox status code.
1936 * @param pThis The snapshot status structure.
1937 * @param pszFormat The format string.
1938 * @param ... Optional arguments.
1939 */
1940static int stamR3SnapshotPrintf(PSTAMR3SNAPSHOTONE pThis, const char *pszFormat, ...)
1941{
1942 va_list va;
1943 va_start(va, pszFormat);
1944 RTStrFormatV(stamR3SnapshotOutput, pThis, NULL, NULL, pszFormat, va);
1945 va_end(va);
1946 return pThis->rc;
1947}
1948
1949
1950/**
1951 * Releases a statistics snapshot returned by STAMR3Snapshot().
1952 *
1953 * @returns VBox status.
1954 * @param pUVM The user mode VM handle.
1955 * @param pszSnapshot The snapshot data pointer returned by STAMR3Snapshot().
1956 * NULL is allowed.
1957 */
1958VMMR3DECL(int) STAMR3SnapshotFree(PUVM pUVM, char *pszSnapshot)
1959{
1960 if (!pszSnapshot)
1961 RTMemFree(pszSnapshot);
1962 NOREF(pUVM);
1963 return VINF_SUCCESS;
1964}
1965
1966
1967/**
1968 * Dumps the selected statistics to the log.
1969 *
1970 * @returns VBox status.
1971 * @param pUVM Pointer to the user mode VM structure.
1972 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
1973 * If NULL all samples are written to the log.
1974 */
1975VMMR3DECL(int) STAMR3Dump(PUVM pUVM, const char *pszPat)
1976{
1977 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
1978 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
1979
1980 STAMR3PRINTONEARGS Args;
1981 Args.pUVM = pUVM;
1982 Args.pvArg = NULL;
1983 Args.pfnPrintf = stamR3EnumLogPrintf;
1984
1985 stamR3EnumU(pUVM, pszPat, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
1986 return VINF_SUCCESS;
1987}
1988
1989
1990/**
1991 * Prints to the log.
1992 *
1993 * @param pArgs Pointer to the print one argument structure.
1994 * @param pszFormat Format string.
1995 * @param ... Format arguments.
1996 */
1997static DECLCALLBACK(void) stamR3EnumLogPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
1998{
1999 va_list va;
2000 va_start(va, pszFormat);
2001 RTLogPrintfV(pszFormat, va);
2002 va_end(va);
2003 NOREF(pArgs);
2004}
2005
2006
2007/**
2008 * Dumps the selected statistics to the release log.
2009 *
2010 * @returns VBox status.
2011 * @param pUVM Pointer to the user mode VM structure.
2012 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
2013 * If NULL all samples are written to the log.
2014 */
2015VMMR3DECL(int) STAMR3DumpToReleaseLog(PUVM pUVM, const char *pszPat)
2016{
2017 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2018 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2019
2020 STAMR3PRINTONEARGS Args;
2021 Args.pUVM = pUVM;
2022 Args.pvArg = NULL;
2023 Args.pfnPrintf = stamR3EnumRelLogPrintf;
2024
2025 stamR3EnumU(pUVM, pszPat, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
2026 return VINF_SUCCESS;
2027}
2028
2029/**
2030 * Prints to the release log.
2031 *
2032 * @param pArgs Pointer to the print one argument structure.
2033 * @param pszFormat Format string.
2034 * @param ... Format arguments.
2035 */
2036static DECLCALLBACK(void) stamR3EnumRelLogPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
2037{
2038 va_list va;
2039 va_start(va, pszFormat);
2040 RTLogRelPrintfV(pszFormat, va);
2041 va_end(va);
2042 NOREF(pArgs);
2043}
2044
2045
2046/**
2047 * Prints the selected statistics to standard out.
2048 *
2049 * @returns VBox status.
2050 * @param pUVM The user mode VM handle.
2051 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
2052 * If NULL all samples are reset.
2053 */
2054VMMR3DECL(int) STAMR3Print(PUVM pUVM, const char *pszPat)
2055{
2056 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2057 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2058
2059 STAMR3PRINTONEARGS Args;
2060 Args.pUVM = pUVM;
2061 Args.pvArg = NULL;
2062 Args.pfnPrintf = stamR3EnumPrintf;
2063
2064 stamR3EnumU(pUVM, pszPat, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
2065 return VINF_SUCCESS;
2066}
2067
2068
2069/**
2070 * Prints to stdout.
2071 *
2072 * @param pArgs Pointer to the print one argument structure.
2073 * @param pszFormat Format string.
2074 * @param ... Format arguments.
2075 */
2076static DECLCALLBACK(void) stamR3EnumPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
2077{
2078 va_list va;
2079 va_start(va, pszFormat);
2080 RTPrintfV(pszFormat, va);
2081 va_end(va);
2082 NOREF(pArgs);
2083}
2084
2085
2086/**
2087 * Prints one sample.
2088 * Callback for stamR3EnumU().
2089 *
2090 * @returns VINF_SUCCESS
2091 * @param pDesc Pointer to the current descriptor.
2092 * @param pvArg User argument - STAMR3PRINTONEARGS.
2093 */
2094static int stamR3PrintOne(PSTAMDESC pDesc, void *pvArg)
2095{
2096 PSTAMR3PRINTONEARGS pArgs = (PSTAMR3PRINTONEARGS)pvArg;
2097
2098 switch (pDesc->enmType)
2099 {
2100 case STAMTYPE_COUNTER:
2101 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pCounter->c == 0)
2102 return VINF_SUCCESS;
2103
2104 pArgs->pfnPrintf(pArgs, "%-32s %8llu %s\n", pDesc->pszName, pDesc->u.pCounter->c, STAMR3GetUnit(pDesc->enmUnit));
2105 break;
2106
2107 case STAMTYPE_PROFILE:
2108 case STAMTYPE_PROFILE_ADV:
2109 {
2110 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pProfile->cPeriods == 0)
2111 return VINF_SUCCESS;
2112
2113 uint64_t u64 = pDesc->u.pProfile->cPeriods ? pDesc->u.pProfile->cPeriods : 1;
2114 pArgs->pfnPrintf(pArgs, "%-32s %8llu %s (%12llu ticks, %7llu times, max %9llu, min %7lld)\n", pDesc->pszName,
2115 pDesc->u.pProfile->cTicks / u64, STAMR3GetUnit(pDesc->enmUnit),
2116 pDesc->u.pProfile->cTicks, pDesc->u.pProfile->cPeriods, pDesc->u.pProfile->cTicksMax, pDesc->u.pProfile->cTicksMin);
2117 break;
2118 }
2119
2120 case STAMTYPE_RATIO_U32:
2121 case STAMTYPE_RATIO_U32_RESET:
2122 if (pDesc->enmVisibility == STAMVISIBILITY_USED && !pDesc->u.pRatioU32->u32A && !pDesc->u.pRatioU32->u32B)
2123 return VINF_SUCCESS;
2124 pArgs->pfnPrintf(pArgs, "%-32s %8u:%-8u %s\n", pDesc->pszName,
2125 pDesc->u.pRatioU32->u32A, pDesc->u.pRatioU32->u32B, STAMR3GetUnit(pDesc->enmUnit));
2126 break;
2127
2128 case STAMTYPE_CALLBACK:
2129 {
2130 char szBuf[512];
2131 pDesc->u.Callback.pfnPrint(pArgs->pUVM->pVM, pDesc->u.Callback.pvSample, szBuf, sizeof(szBuf));
2132 pArgs->pfnPrintf(pArgs, "%-32s %s %s\n", pDesc->pszName, szBuf, STAMR3GetUnit(pDesc->enmUnit));
2133 break;
2134 }
2135
2136 case STAMTYPE_U8:
2137 case STAMTYPE_U8_RESET:
2138 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
2139 return VINF_SUCCESS;
2140 pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu8, STAMR3GetUnit(pDesc->enmUnit));
2141 break;
2142
2143 case STAMTYPE_X8:
2144 case STAMTYPE_X8_RESET:
2145 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
2146 return VINF_SUCCESS;
2147 pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu8, STAMR3GetUnit(pDesc->enmUnit));
2148 break;
2149
2150 case STAMTYPE_U16:
2151 case STAMTYPE_U16_RESET:
2152 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
2153 return VINF_SUCCESS;
2154 pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu16, STAMR3GetUnit(pDesc->enmUnit));
2155 break;
2156
2157 case STAMTYPE_X16:
2158 case STAMTYPE_X16_RESET:
2159 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
2160 return VINF_SUCCESS;
2161 pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu16, STAMR3GetUnit(pDesc->enmUnit));
2162 break;
2163
2164 case STAMTYPE_U32:
2165 case STAMTYPE_U32_RESET:
2166 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
2167 return VINF_SUCCESS;
2168 pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu32, STAMR3GetUnit(pDesc->enmUnit));
2169 break;
2170
2171 case STAMTYPE_X32:
2172 case STAMTYPE_X32_RESET:
2173 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
2174 return VINF_SUCCESS;
2175 pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu32, STAMR3GetUnit(pDesc->enmUnit));
2176 break;
2177
2178 case STAMTYPE_U64:
2179 case STAMTYPE_U64_RESET:
2180 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
2181 return VINF_SUCCESS;
2182 pArgs->pfnPrintf(pArgs, "%-32s %8llu %s\n", pDesc->pszName, *pDesc->u.pu64, STAMR3GetUnit(pDesc->enmUnit));
2183 break;
2184
2185 case STAMTYPE_X64:
2186 case STAMTYPE_X64_RESET:
2187 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
2188 return VINF_SUCCESS;
2189 pArgs->pfnPrintf(pArgs, "%-32s %8llx %s\n", pDesc->pszName, *pDesc->u.pu64, STAMR3GetUnit(pDesc->enmUnit));
2190 break;
2191
2192 case STAMTYPE_BOOL:
2193 case STAMTYPE_BOOL_RESET:
2194 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pf == false)
2195 return VINF_SUCCESS;
2196 pArgs->pfnPrintf(pArgs, "%-32s %s %s\n", pDesc->pszName, *pDesc->u.pf ? "true " : "false ", STAMR3GetUnit(pDesc->enmUnit));
2197 break;
2198
2199 default:
2200 AssertMsgFailed(("enmType=%d\n", pDesc->enmType));
2201 break;
2202 }
2203 NOREF(pvArg);
2204 return VINF_SUCCESS;
2205}
2206
2207
2208/**
2209 * Enumerate the statistics by the means of a callback function.
2210 *
2211 * @returns Whatever the callback returns.
2212 *
2213 * @param pUVM The user mode VM handle.
2214 * @param pszPat The pattern to match samples.
2215 * @param pfnEnum The callback function.
2216 * @param pvUser The pvUser argument of the callback function.
2217 */
2218VMMR3DECL(int) STAMR3Enum(PUVM pUVM, const char *pszPat, PFNSTAMR3ENUM pfnEnum, void *pvUser)
2219{
2220 UVM_ASSERT_VALID_EXT_RETURN(pUVM, VERR_INVALID_VM_HANDLE);
2221 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, VERR_INVALID_VM_HANDLE);
2222
2223 STAMR3ENUMONEARGS Args;
2224 Args.pVM = pUVM->pVM;
2225 Args.pfnEnum = pfnEnum;
2226 Args.pvUser = pvUser;
2227
2228 return stamR3EnumU(pUVM, pszPat, true /* fUpdateRing0 */, stamR3EnumOne, &Args);
2229}
2230
2231
2232/**
2233 * Callback function for STARTR3Enum().
2234 *
2235 * @returns whatever the callback returns.
2236 * @param pDesc Pointer to the current descriptor.
2237 * @param pvArg Points to a STAMR3ENUMONEARGS structure.
2238 */
2239static int stamR3EnumOne(PSTAMDESC pDesc, void *pvArg)
2240{
2241 PSTAMR3ENUMONEARGS pArgs = (PSTAMR3ENUMONEARGS)pvArg;
2242 int rc;
2243 if (pDesc->enmType == STAMTYPE_CALLBACK)
2244 {
2245 /* Give the enumerator something useful. */
2246 char szBuf[512];
2247 pDesc->u.Callback.pfnPrint(pArgs->pVM, pDesc->u.Callback.pvSample, szBuf, sizeof(szBuf));
2248 rc = pArgs->pfnEnum(pDesc->pszName, pDesc->enmType, szBuf, pDesc->enmUnit,
2249 pDesc->enmVisibility, pDesc->pszDesc, pArgs->pvUser);
2250 }
2251 else
2252 rc = pArgs->pfnEnum(pDesc->pszName, pDesc->enmType, pDesc->u.pv, pDesc->enmUnit,
2253 pDesc->enmVisibility, pDesc->pszDesc, pArgs->pvUser);
2254 return rc;
2255}
2256
2257
2258/**
2259 * Checks if the string contains a pattern expression or not.
2260 *
2261 * @returns true / false.
2262 * @param pszPat The potential pattern.
2263 */
2264static bool stamR3IsPattern(const char *pszPat)
2265{
2266 return strchr(pszPat, '*') != NULL
2267 || strchr(pszPat, '?') != NULL;
2268}
2269
2270
2271/**
2272 * Match a name against an array of patterns.
2273 *
2274 * @returns true if it matches, false if it doesn't match.
2275 * @param papszExpressions The array of pattern expressions.
2276 * @param cExpressions The number of array entries.
2277 * @param piExpression Where to read/store the current skip index. Optional.
2278 * @param pszName The name to match.
2279 */
2280static bool stamR3MultiMatch(const char * const *papszExpressions, unsigned cExpressions,
2281 unsigned *piExpression, const char *pszName)
2282{
2283 for (unsigned i = piExpression ? *piExpression : 0; i < cExpressions; i++)
2284 {
2285 const char *pszPat = papszExpressions[i];
2286 if (RTStrSimplePatternMatch(pszPat, pszName))
2287 {
2288 /* later:
2289 if (piExpression && i > *piExpression)
2290 {
2291 check if we can skip some expressions
2292 }*/
2293 return true;
2294 }
2295 }
2296 return false;
2297}
2298
2299
2300/**
2301 * Splits a multi pattern into single ones.
2302 *
2303 * @returns Pointer to an array of single patterns. Free it with RTMemTmpFree.
2304 * @param pszPat The pattern to split.
2305 * @param pcExpressions The number of array elements.
2306 * @param pszCopy The pattern copy to free using RTStrFree.
2307 */
2308static char **stamR3SplitPattern(const char *pszPat, unsigned *pcExpressions, char **ppszCopy)
2309{
2310 Assert(pszPat && *pszPat);
2311
2312 char *pszCopy = RTStrDup(pszPat);
2313 if (!pszCopy)
2314 return NULL;
2315
2316 /* count them & allocate array. */
2317 char *psz = pszCopy;
2318 unsigned cExpressions = 1;
2319 while ((psz = strchr(psz, '|')) != NULL)
2320 cExpressions++, psz++;
2321
2322 char **papszExpressions = (char **)RTMemTmpAllocZ((cExpressions + 1) * sizeof(char *));
2323 if (!papszExpressions)
2324 {
2325 RTStrFree(pszCopy);
2326 return NULL;
2327 }
2328
2329 /* split */
2330 psz = pszCopy;
2331 for (unsigned i = 0;;)
2332 {
2333 papszExpressions[i] = psz;
2334 if (++i >= cExpressions)
2335 break;
2336 psz = strchr(psz, '|');
2337 *psz++ = '\0';
2338 }
2339
2340 /* sort the array, putting '*' last. */
2341 /** @todo sort it... */
2342
2343 *pcExpressions = cExpressions;
2344 *ppszCopy = pszCopy;
2345 return papszExpressions;
2346}
2347
2348
2349/**
2350 * Enumerates the nodes selected by a pattern or all nodes if no pattern
2351 * is specified.
2352 *
2353 * The call may lock STAM for writing before calling this function, however do
2354 * not lock it for reading as this function may need to write lock STAM.
2355 *
2356 * @returns The rc from the callback.
2357 * @param pUVM Pointer to the user mode VM structure.
2358 * @param pszPat Pattern.
2359 * @param fUpdateRing0 Update the ring-0 .
2360 * @param pfnCallback Callback function which shall be called for matching nodes.
2361 * If it returns anything but VINF_SUCCESS the enumeration is
2362 * terminated and the status code returned to the caller.
2363 * @param pvArg User parameter for the callback.
2364 */
2365static int stamR3EnumU(PUVM pUVM, const char *pszPat, bool fUpdateRing0,
2366 int (*pfnCallback)(PSTAMDESC pDesc, void *pvArg), void *pvArg)
2367{
2368 int rc = VINF_SUCCESS;
2369 PSTAMDESC pCur;
2370
2371 /*
2372 * All.
2373 */
2374 if (!pszPat || !*pszPat || !strcmp(pszPat, "*"))
2375 {
2376 if (fUpdateRing0)
2377 stamR3Ring0StatsUpdateU(pUVM, "*");
2378
2379 STAM_LOCK_RD(pUVM);
2380 RTListForEach(&pUVM->stam.s.List, pCur, STAMDESC, ListEntry)
2381 {
2382 rc = pfnCallback(pCur, pvArg);
2383 if (rc)
2384 break;
2385 }
2386 STAM_UNLOCK_RD(pUVM);
2387 }
2388
2389 /*
2390 * Single expression pattern.
2391 */
2392 else if (!strchr(pszPat, '|'))
2393 {
2394 if (fUpdateRing0)
2395 stamR3Ring0StatsUpdateU(pUVM, pszPat);
2396
2397 STAM_LOCK_RD(pUVM);
2398#ifdef STAM_WITH_LOOKUP_TREE
2399 if (!stamR3IsPattern(pszPat))
2400 {
2401 pCur = stamR3LookupFindDesc(pUVM->stam.s.pRoot, pszPat);
2402 if (pCur)
2403 rc = pfnCallback(pCur, pvArg);
2404 }
2405 else
2406 {
2407 PSTAMDESC pLast;
2408 pCur = stamR3LookupFindPatternDescRange(pUVM->stam.s.pRoot, &pUVM->stam.s.List, pszPat, &pLast);
2409 if (pCur)
2410 {
2411 for (;;)
2412 {
2413 if (RTStrSimplePatternMatch(pszPat, pCur->pszName))
2414 {
2415 rc = pfnCallback(pCur, pvArg);
2416 if (rc)
2417 break;
2418 }
2419 if (pCur == pLast)
2420 break;
2421 pCur = RTListNodeGetNext(&pCur->ListEntry, STAMDESC, ListEntry);
2422 }
2423 Assert(pLast);
2424 }
2425 else
2426 Assert(!pLast);
2427
2428 }
2429#else
2430 RTListForEach(&pUVM->stam.s.List, pCur, STAMDESC, ListEntry)
2431 {
2432 if (RTStrSimplePatternMatch(pszPat, pCur->pszName))
2433 {
2434 rc = pfnCallback(pCur, pvArg);
2435 if (rc)
2436 break;
2437 }
2438 }
2439#endif
2440 STAM_UNLOCK_RD(pUVM);
2441 }
2442
2443 /*
2444 * Multi expression pattern.
2445 */
2446 else
2447 {
2448 /*
2449 * Split up the pattern first.
2450 */
2451 char *pszCopy;
2452 unsigned cExpressions;
2453 char **papszExpressions = stamR3SplitPattern(pszPat, &cExpressions, &pszCopy);
2454 if (!papszExpressions)
2455 return VERR_NO_MEMORY;
2456
2457 /*
2458 * Perform the enumeration.
2459 */
2460 if (fUpdateRing0)
2461 stamR3Ring0StatsUpdateMultiU(pUVM, papszExpressions, cExpressions);
2462
2463 STAM_LOCK_RD(pUVM);
2464 unsigned iExpression = 0;
2465 RTListForEach(&pUVM->stam.s.List, pCur, STAMDESC, ListEntry)
2466 {
2467 if (stamR3MultiMatch(papszExpressions, cExpressions, &iExpression, pCur->pszName))
2468 {
2469 rc = pfnCallback(pCur, pvArg);
2470 if (rc)
2471 break;
2472 }
2473 }
2474 STAM_UNLOCK_RD(pUVM);
2475
2476 RTMemTmpFree(papszExpressions);
2477 RTStrFree(pszCopy);
2478 }
2479
2480 return rc;
2481}
2482
2483
2484/**
2485 * Registers the ring-0 statistics.
2486 *
2487 * @param pUVM Pointer to the user mode VM structure.
2488 */
2489static void stamR3Ring0StatsRegisterU(PUVM pUVM)
2490{
2491 /* GVMM */
2492 for (unsigned i = 0; i < RT_ELEMENTS(g_aGVMMStats); i++)
2493 stamR3RegisterU(pUVM, (uint8_t *)&pUVM->stam.s.GVMMStats + g_aGVMMStats[i].offVar, NULL, NULL,
2494 g_aGVMMStats[i].enmType, STAMVISIBILITY_ALWAYS, g_aGVMMStats[i].pszName,
2495 g_aGVMMStats[i].enmUnit, g_aGVMMStats[i].pszDesc);
2496 pUVM->stam.s.cRegisteredHostCpus = 0;
2497
2498 /* GMM */
2499 for (unsigned i = 0; i < RT_ELEMENTS(g_aGMMStats); i++)
2500 stamR3RegisterU(pUVM, (uint8_t *)&pUVM->stam.s.GMMStats + g_aGMMStats[i].offVar, NULL, NULL,
2501 g_aGMMStats[i].enmType, STAMVISIBILITY_ALWAYS, g_aGMMStats[i].pszName,
2502 g_aGMMStats[i].enmUnit, g_aGMMStats[i].pszDesc);
2503}
2504
2505
2506/**
2507 * Updates the ring-0 statistics (the copy).
2508 *
2509 * @param pUVM Pointer to the user mode VM structure.
2510 * @param pszPat The pattern.
2511 */
2512static void stamR3Ring0StatsUpdateU(PUVM pUVM, const char *pszPat)
2513{
2514 stamR3Ring0StatsUpdateMultiU(pUVM, &pszPat, 1);
2515}
2516
2517
2518/**
2519 * Updates the ring-0 statistics.
2520 *
2521 * The ring-0 statistics aren't directly addressable from ring-3 and must be
2522 * copied when needed.
2523 *
2524 * @param pUVM Pointer to the user mode VM structure.
2525 * @param pszPat The pattern (for knowing when to skip).
2526 */
2527static void stamR3Ring0StatsUpdateMultiU(PUVM pUVM, const char * const *papszExpressions, unsigned cExpressions)
2528{
2529 PVM pVM = pUVM->pVM;
2530 if (!pVM || !pVM->pSession)
2531 return;
2532
2533 /*
2534 * GVMM
2535 */
2536 bool fUpdate = false;
2537 for (unsigned i = 0; i < RT_ELEMENTS(g_aGVMMStats); i++)
2538 if (stamR3MultiMatch(papszExpressions, cExpressions, NULL, g_aGVMMStats[i].pszName))
2539 {
2540 fUpdate = true;
2541 break;
2542 }
2543 if (!fUpdate)
2544 {
2545 /** @todo check the cpu leaves - rainy day. */
2546 }
2547 if (fUpdate)
2548 {
2549 GVMMQUERYSTATISTICSSREQ Req;
2550 Req.Hdr.cbReq = sizeof(Req);
2551 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
2552 Req.pSession = pVM->pSession;
2553 int rc = SUPR3CallVMMR0Ex(pVM->pVMR0, NIL_VMCPUID, VMMR0_DO_GVMM_QUERY_STATISTICS, 0, &Req.Hdr);
2554 if (RT_SUCCESS(rc))
2555 {
2556 pUVM->stam.s.GVMMStats = Req.Stats;
2557
2558 /*
2559 * Check if the number of host CPUs has changed (it will the first
2560 * time around and normally never again).
2561 */
2562 if (RT_UNLIKELY(pUVM->stam.s.GVMMStats.cHostCpus > pUVM->stam.s.cRegisteredHostCpus))
2563 {
2564 STAM_LOCK_WR(pUVM);
2565 if (RT_UNLIKELY(pUVM->stam.s.GVMMStats.cHostCpus > pUVM->stam.s.cRegisteredHostCpus))
2566 {
2567 uint32_t cCpus = pUVM->stam.s.GVMMStats.cHostCpus;
2568 for (uint32_t iCpu = pUVM->stam.s.cRegisteredHostCpus; iCpu < cCpus; iCpu++)
2569 {
2570 char szName[120];
2571 size_t cchBase = RTStrPrintf(szName, sizeof(szName), "/GVMM/HostCpus/%u", iCpu);
2572 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].idCpu, NULL, NULL,
2573 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_NONE, "Host CPU ID");
2574 strcpy(&szName[cchBase], "/idxCpuSet");
2575 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].idxCpuSet, NULL, NULL,
2576 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_NONE, "CPU Set index");
2577 strcpy(&szName[cchBase], "/DesiredHz");
2578 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].uDesiredHz, NULL, NULL,
2579 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_HZ, "The desired frequency");
2580 strcpy(&szName[cchBase], "/CurTimerHz");
2581 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].uTimerHz, NULL, NULL,
2582 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_HZ, "The current timer frequency");
2583 strcpy(&szName[cchBase], "/PPTChanges");
2584 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].cChanges, NULL, NULL,
2585 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_OCCURENCES, "RTTimerChangeInterval calls");
2586 strcpy(&szName[cchBase], "/PPTStarts");
2587 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].cStarts, NULL, NULL,
2588 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_OCCURENCES, "RTTimerStart calls");
2589 }
2590 pUVM->stam.s.cRegisteredHostCpus = cCpus;
2591 }
2592 STAM_UNLOCK_WR(pUVM);
2593 }
2594 }
2595 }
2596
2597 /*
2598 * GMM
2599 */
2600 fUpdate = false;
2601 for (unsigned i = 0; i < RT_ELEMENTS(g_aGMMStats); i++)
2602 if (stamR3MultiMatch(papszExpressions, cExpressions, NULL, g_aGMMStats[i].pszName))
2603 {
2604 fUpdate = true;
2605 break;
2606 }
2607 if (fUpdate)
2608 {
2609 GMMQUERYSTATISTICSSREQ Req;
2610 Req.Hdr.cbReq = sizeof(Req);
2611 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
2612 Req.pSession = pVM->pSession;
2613 int rc = SUPR3CallVMMR0Ex(pVM->pVMR0, NIL_VMCPUID, VMMR0_DO_GMM_QUERY_STATISTICS, 0, &Req.Hdr);
2614 if (RT_SUCCESS(rc))
2615 pUVM->stam.s.GMMStats = Req.Stats;
2616 }
2617}
2618
2619
2620/**
2621 * Get the unit string.
2622 *
2623 * @returns Pointer to read only unit string.
2624 * @param enmUnit The unit.
2625 */
2626VMMR3DECL(const char *) STAMR3GetUnit(STAMUNIT enmUnit)
2627{
2628 switch (enmUnit)
2629 {
2630 case STAMUNIT_NONE: return "";
2631 case STAMUNIT_CALLS: return "calls";
2632 case STAMUNIT_COUNT: return "count";
2633 case STAMUNIT_BYTES: return "bytes";
2634 case STAMUNIT_PAGES: return "pages";
2635 case STAMUNIT_ERRORS: return "errors";
2636 case STAMUNIT_OCCURENCES: return "times";
2637 case STAMUNIT_TICKS: return "ticks";
2638 case STAMUNIT_TICKS_PER_CALL: return "ticks/call";
2639 case STAMUNIT_TICKS_PER_OCCURENCE: return "ticks/time";
2640 case STAMUNIT_GOOD_BAD: return "good:bad";
2641 case STAMUNIT_MEGABYTES: return "megabytes";
2642 case STAMUNIT_KILOBYTES: return "kilobytes";
2643 case STAMUNIT_NS: return "ns";
2644 case STAMUNIT_NS_PER_CALL: return "ns/call";
2645 case STAMUNIT_NS_PER_OCCURENCE: return "ns/time";
2646 case STAMUNIT_PCT: return "%";
2647 case STAMUNIT_HZ: return "Hz";
2648
2649 default:
2650 AssertMsgFailed(("Unknown unit %d\n", enmUnit));
2651 return "(?unit?)";
2652 }
2653}
2654
2655#ifdef VBOX_WITH_DEBUGGER
2656
2657/**
2658 * @callback_method_impl{FNDBGCCMD, The '.stats' command.}
2659 */
2660static DECLCALLBACK(int) stamR3CmdStats(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2661{
2662 /*
2663 * Validate input.
2664 */
2665 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2666 if (RTListIsEmpty(&pUVM->stam.s.List))
2667 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No statistics present");
2668
2669 /*
2670 * Do the printing.
2671 */
2672 STAMR3PRINTONEARGS Args;
2673 Args.pUVM = pUVM;
2674 Args.pvArg = pCmdHlp;
2675 Args.pfnPrintf = stamR3EnumDbgfPrintf;
2676
2677 return stamR3EnumU(pUVM, cArgs ? paArgs[0].u.pszString : NULL, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
2678}
2679
2680
2681/**
2682 * Display one sample in the debugger.
2683 *
2684 * @param pArgs Pointer to the print one argument structure.
2685 * @param pszFormat Format string.
2686 * @param ... Format arguments.
2687 */
2688static DECLCALLBACK(void) stamR3EnumDbgfPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
2689{
2690 PDBGCCMDHLP pCmdHlp = (PDBGCCMDHLP)pArgs->pvArg;
2691
2692 va_list va;
2693 va_start(va, pszFormat);
2694 pCmdHlp->pfnPrintfV(pCmdHlp, NULL, pszFormat, va);
2695 va_end(va);
2696 NOREF(pArgs);
2697}
2698
2699
2700/**
2701 * @callback_method_impl{FNDBGCCMD, The '.statsreset' command.}
2702 */
2703static DECLCALLBACK(int) stamR3CmdStatsReset(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PUVM pUVM, PCDBGCVAR paArgs, unsigned cArgs)
2704{
2705 /*
2706 * Validate input.
2707 */
2708 DBGC_CMDHLP_REQ_UVM_RET(pCmdHlp, pCmd, pUVM);
2709 if (RTListIsEmpty(&pUVM->stam.s.List))
2710 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No statistics present");
2711
2712 /*
2713 * Execute reset.
2714 */
2715 int rc = STAMR3Reset(pUVM, cArgs ? paArgs[0].u.pszString : NULL);
2716 if (RT_SUCCESS(rc))
2717 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "STAMR3ResetU");
2718 return DBGCCmdHlpPrintf(pCmdHlp, "Statistics have been reset.\n");
2719}
2720
2721#endif /* VBOX_WITH_DEBUGGER */
2722
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