VirtualBox

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

Last change on this file since 62596 was 62478, checked in by vboxsync, 8 years ago

(C) 2016

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