VirtualBox

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

Last change on this file since 39870 was 39078, checked in by vboxsync, 13 years ago

VMM: -Wunused-parameter

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 72.2 KB
Line 
1/* $Id: STAM.cpp 39078 2011-10-21 14:18:22Z vboxsync $ */
2/** @file
3 * STAM - The Statistics Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/** @page pg_stam STAM - The Statistics Manager
19 *
20 * The purpose for the statistics manager is to present the rest of the system
21 * with a somewhat uniform way of accessing VMM statistics. STAM sports a
22 * couple of different APIs for accessing them: STAMR3EnumU, STAMR3SnapshotU,
23 * STAMR3DumpU, STAMR3DumpToReleaseLogU and the debugger. Main is exposing the
24 * XML based one, STAMR3SnapshotU.
25 *
26 * The rest of the VMM together with the devices and drivers registers their
27 * statistics with STAM giving them a name. The name is hierarchical, the
28 * components separated by slashes ('/') and must start with a slash.
29 *
30 * Each item registered with STAM - also, half incorrectly, called a sample -
31 * has a type, unit, visibility, data pointer and description associated with it
32 * in addition to the name (described above). The type tells STAM what kind of
33 * structure the pointer is pointing to. The visibility allows unused
34 * statistics from cluttering the output or showing up in the GUI. All the bits
35 * together makes STAM able to present the items in a sensible way to the user.
36 * Some types also allows STAM to reset the data, which is very convenient when
37 * digging into specific operations and such.
38 *
39 * PS. The VirtualBox Debugger GUI has a viewer for inspecting the statistics
40 * STAM provides. You will also find statistics in the release and debug logs.
41 * And as mentioned in the introduction, the debugger console features a couple
42 * of command: .stats and .statsreset.
43 *
44 * @see grp_stam
45 */
46
47/*******************************************************************************
48* Header Files *
49*******************************************************************************/
50#define LOG_GROUP LOG_GROUP_STAM
51#include <VBox/vmm/stam.h>
52#include "STAMInternal.h"
53#include <VBox/vmm/vm.h>
54#include <VBox/vmm/uvm.h>
55#include <VBox/err.h>
56#include <VBox/dbg.h>
57#include <VBox/log.h>
58
59#include <iprt/assert.h>
60#include <iprt/asm.h>
61#include <iprt/alloc.h>
62#include <iprt/stream.h>
63#include <iprt/string.h>
64
65
66/*******************************************************************************
67* Structures and Typedefs *
68*******************************************************************************/
69/**
70 * Argument structure for stamR3PrintOne().
71 */
72typedef struct STAMR3PRINTONEARGS
73{
74 PVM pVM;
75 void *pvArg;
76 DECLCALLBACKMEMBER(void, pfnPrintf)(struct STAMR3PRINTONEARGS *pvArg, const char *pszFormat, ...);
77} STAMR3PRINTONEARGS, *PSTAMR3PRINTONEARGS;
78
79
80/**
81 * Argument structure to stamR3EnumOne().
82 */
83typedef struct STAMR3ENUMONEARGS
84{
85 PVM pVM;
86 PFNSTAMR3ENUM pfnEnum;
87 void *pvUser;
88} STAMR3ENUMONEARGS, *PSTAMR3ENUMONEARGS;
89
90
91/**
92 * The snapshot status structure.
93 * Argument package passed to stamR3SnapshotOne, stamR3SnapshotPrintf and stamR3SnapshotOutput.
94 */
95typedef struct STAMR3SNAPSHOTONE
96{
97 /** Pointer to the buffer start. */
98 char *pszStart;
99 /** Pointer to the buffer end. */
100 char *pszEnd;
101 /** Pointer to the current buffer position. */
102 char *psz;
103 /** The VM handle. */
104 PVM pVM;
105 /** The number of bytes allocated. */
106 size_t cbAllocated;
107 /** The status code. */
108 int rc;
109 /** Whether to include the description strings. */
110 bool fWithDesc;
111} STAMR3SNAPSHOTONE, *PSTAMR3SNAPSHOTONE;
112
113
114/**
115 * Init record for a ring-0 statistic sample.
116 */
117typedef struct STAMR0SAMPLE
118{
119 /** The GVMMSTATS structure offset of the variable. */
120 unsigned offVar;
121 /** The type. */
122 STAMTYPE enmType;
123 /** The unit. */
124 STAMUNIT enmUnit;
125 /** The name. */
126 const char *pszName;
127 /** The description. */
128 const char *pszDesc;
129} STAMR0SAMPLE;
130
131
132/*******************************************************************************
133* Internal Functions *
134*******************************************************************************/
135static int stamR3RegisterU(PUVM pUVM, void *pvSample, PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
136 STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc);
137static int stamR3ResetOne(PSTAMDESC pDesc, void *pvArg);
138static DECLCALLBACK(void) stamR3EnumLogPrintf(PSTAMR3PRINTONEARGS pvArg, const char *pszFormat, ...);
139static DECLCALLBACK(void) stamR3EnumRelLogPrintf(PSTAMR3PRINTONEARGS pvArg, const char *pszFormat, ...);
140static DECLCALLBACK(void) stamR3EnumPrintf(PSTAMR3PRINTONEARGS pvArg, const char *pszFormat, ...);
141static int stamR3SnapshotOne(PSTAMDESC pDesc, void *pvArg);
142static int stamR3SnapshotPrintf(PSTAMR3SNAPSHOTONE pThis, const char *pszFormat, ...);
143static int stamR3PrintOne(PSTAMDESC pDesc, void *pvArg);
144static int stamR3EnumOne(PSTAMDESC pDesc, void *pvArg);
145static bool stamR3MultiMatch(const char * const *papszExpressions, unsigned cExpressions, unsigned *piExpression, const char *pszName);
146static char ** stamR3SplitPattern(const char *pszPat, unsigned *pcExpressions, char **ppszCopy);
147static int stamR3EnumU(PUVM pUVM, const char *pszPat, bool fUpdateRing0, int (pfnCallback)(PSTAMDESC pDesc, void *pvArg), void *pvArg);
148static void stamR3Ring0StatsRegisterU(PUVM pUVM);
149static void stamR3Ring0StatsUpdateU(PUVM pUVM, const char *pszPat);
150static void stamR3Ring0StatsUpdateMultiU(PUVM pUVM, const char * const *papszExpressions, unsigned cExpressions);
151
152#ifdef VBOX_WITH_DEBUGGER
153static DECLCALLBACK(int) stamR3CmdStats(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
154static DECLCALLBACK(void) stamR3EnumDbgfPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...);
155static DECLCALLBACK(int) stamR3CmdStatsReset(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs);
156#endif
157
158
159/*******************************************************************************
160* Global Variables *
161*******************************************************************************/
162#ifdef VBOX_WITH_DEBUGGER
163/** Pattern argument. */
164static const DBGCVARDESC g_aArgPat[] =
165{
166 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
167 { 0, 1, DBGCVAR_CAT_STRING, 0, "pattern", "Which samples the command shall be applied to. Use '*' as wildcard. Use ';' to separate expression." }
168};
169
170/** Command descriptors. */
171static const DBGCCMD g_aCmds[] =
172{
173 /* pszCmd, cArgsMin, cArgsMax, paArgDesc, cArgDescs, fFlags, pfnHandler pszSyntax, ....pszDescription */
174 { "stats", 0, 1, &g_aArgPat[0], RT_ELEMENTS(g_aArgPat), 0, stamR3CmdStats, "[pattern]", "Display statistics." },
175 { "statsreset", 0, 1, &g_aArgPat[0], RT_ELEMENTS(g_aArgPat), 0, stamR3CmdStatsReset,"[pattern]", "Resets statistics." }
176};
177#endif
178
179
180/**
181 * The GVMM mapping records - sans the host cpus.
182 */
183static const STAMR0SAMPLE g_aGVMMStats[] =
184{
185 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltCalls", "The number of calls to GVMMR0SchedHalt." },
186 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltBlocking), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltBlocking", "The number of times we did go to sleep in GVMMR0SchedHalt." },
187 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltTimeouts), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltTimeouts", "The number of times we timed out in GVMMR0SchedHalt." },
188 { 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." },
189 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltWakeUps", "The number of wake ups done during GVMMR0SchedHalt." },
190 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cWakeUpCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/WakeUpCalls", "The number of calls to GVMMR0WakeUp." },
191 { 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." },
192 { 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)." },
193 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cPokeCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/PokeCalls", "The number of calls to GVMMR0Poke." },
194 { 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." },
195 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cPollCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/PollCalls", "The number of calls to GVMMR0SchedPoll." },
196 { 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." },
197 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cPollWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/PollWakeUps", "The number of wake ups done during GVMMR0SchedPoll." },
198
199 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltCalls", "The number of calls to GVMMR0SchedHalt." },
200 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltBlocking), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltBlocking", "The number of times we did go to sleep in GVMMR0SchedHalt." },
201 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltTimeouts), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltTimeouts", "The number of times we timed out in GVMMR0SchedHalt." },
202 { 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." },
203 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltWakeUps", "The number of wake ups done during GVMMR0SchedHalt." },
204 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cWakeUpCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/WakeUpCalls", "The number of calls to GVMMR0WakeUp." },
205 { 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." },
206 { 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)." },
207 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cPokeCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/PokeCalls", "The number of calls to GVMMR0Poke." },
208 { 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." },
209 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cPollCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/PollCalls", "The number of calls to GVMMR0SchedPoll." },
210 { 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." },
211 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cPollWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/PollWakeUps", "The number of wake ups done during GVMMR0SchedPoll." },
212
213 { RT_UOFFSETOF(GVMMSTATS, cVMs), STAMTYPE_U32, STAMUNIT_CALLS, "/GVMM/VMs", "The number of VMs accessible to the caller." },
214 { RT_UOFFSETOF(GVMMSTATS, cEMTs), STAMTYPE_U32, STAMUNIT_CALLS, "/GVMM/EMTs", "The number of emulation threads." },
215 { RT_UOFFSETOF(GVMMSTATS, cHostCpus), STAMTYPE_U32, STAMUNIT_CALLS, "/GVMM/HostCPUs", "The number of host CPUs." },
216};
217
218
219/**
220 * Initializes the STAM.
221 *
222 * @returns VBox status code.
223 * @param pVM The VM to operate on.
224 */
225VMMR3DECL(int) STAMR3InitUVM(PUVM pUVM)
226{
227 LogFlow(("STAMR3Init\n"));
228
229 /*
230 * Assert alignment and sizes.
231 */
232 AssertCompile(sizeof(pUVM->stam.s) <= sizeof(pUVM->stam.padding));
233 AssertRelease(sizeof(pUVM->stam.s) <= sizeof(pUVM->stam.padding));
234
235 /*
236 * Setup any fixed pointers and offsets.
237 */
238 int rc = RTSemRWCreate(&pUVM->stam.s.RWSem);
239 AssertRCReturn(rc, rc);
240
241 /*
242 * Register the ring-0 statistics (GVMM/GMM).
243 */
244 stamR3Ring0StatsRegisterU(pUVM);
245
246#ifdef VBOX_WITH_DEBUGGER
247 /*
248 * Register debugger commands.
249 */
250 static bool fRegisteredCmds = false;
251 if (!fRegisteredCmds)
252 {
253 rc = DBGCRegisterCommands(&g_aCmds[0], RT_ELEMENTS(g_aCmds));
254 if (RT_SUCCESS(rc))
255 fRegisteredCmds = true;
256 }
257#endif
258
259 return VINF_SUCCESS;
260}
261
262
263/**
264 * Terminates the STAM.
265 *
266 * @param pUVM Pointer to the user mode VM structure.
267 */
268VMMR3DECL(void) STAMR3TermUVM(PUVM pUVM)
269{
270 /*
271 * Free used memory and the RWLock.
272 */
273 PSTAMDESC pCur = pUVM->stam.s.pHead;
274 while (pCur)
275 {
276 void *pvFree = pCur;
277 pCur = pCur->pNext;
278 RTMemFree(pvFree);
279 }
280 pUVM->stam.s.pHead = NULL;
281
282 Assert(pUVM->stam.s.RWSem != NIL_RTSEMRW);
283 RTSemRWDestroy(pUVM->stam.s.RWSem);
284 pUVM->stam.s.RWSem = NIL_RTSEMRW;
285}
286
287
288/**
289 * Registers a sample with the statistics manager.
290 *
291 * Statistics are maintained on a per VM basis and is normally registered
292 * during the VM init stage, but there is nothing preventing you from
293 * register them at runtime.
294 *
295 * Use STAMR3Deregister() to deregister statistics at runtime, however do
296 * not bother calling at termination time.
297 *
298 * It is not possible to register the same sample twice.
299 *
300 * @returns VBox status.
301 * @param pUVM Pointer to the user mode VM structure.
302 * @param pvSample Pointer to the sample.
303 * @param enmType Sample type. This indicates what pvSample is pointing at.
304 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
305 * @param pszName Sample name. The name is on this form "/<component>/<sample>".
306 * Further nesting is possible.
307 * @param enmUnit Sample unit.
308 * @param pszDesc Sample description.
309 */
310VMMR3DECL(int) STAMR3RegisterU(PUVM pUVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
311{
312 AssertReturn(enmType != STAMTYPE_CALLBACK, VERR_INVALID_PARAMETER);
313 return stamR3RegisterU(pUVM, pvSample, NULL, NULL, enmType, enmVisibility, pszName, enmUnit, pszDesc);
314}
315
316
317/**
318 * Registers a sample with the statistics manager.
319 *
320 * Statistics are maintained on a per VM basis and is normally registered
321 * during the VM init stage, but there is nothing preventing you from
322 * register them at runtime.
323 *
324 * Use STAMR3Deregister() to deregister statistics at runtime, however do
325 * not bother calling at termination time.
326 *
327 * It is not possible to register the same sample twice.
328 *
329 * @returns VBox status.
330 * @param pVM The VM handle.
331 * @param pvSample Pointer to the sample.
332 * @param enmType Sample type. This indicates what pvSample is pointing at.
333 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
334 * @param pszName Sample name. The name is on this form "/<component>/<sample>".
335 * Further nesting is possible.
336 * @param enmUnit Sample unit.
337 * @param pszDesc Sample description.
338 */
339VMMR3DECL(int) STAMR3Register(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
340{
341 AssertReturn(enmType != STAMTYPE_CALLBACK, VERR_INVALID_PARAMETER);
342 return stamR3RegisterU(pVM->pUVM, pvSample, NULL, NULL, enmType, enmVisibility, pszName, enmUnit, pszDesc);
343}
344
345
346/**
347 * Same as STAMR3RegisterU except that the name is specified in a
348 * RTStrPrintf like fashion.
349 *
350 * @returns VBox status.
351 * @param pUVM Pointer to the user mode VM structure.
352 * @param pvSample Pointer to the sample.
353 * @param enmType Sample type. This indicates what pvSample is pointing at.
354 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
355 * @param enmUnit Sample unit.
356 * @param pszDesc Sample description.
357 * @param pszName The sample name format string.
358 * @param ... Arguments to the format string.
359 */
360VMMR3DECL(int) STAMR3RegisterFU(PUVM pUVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
361 const char *pszDesc, const char *pszName, ...)
362{
363 va_list args;
364 va_start(args, pszName);
365 int rc = STAMR3RegisterVU(pUVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
366 va_end(args);
367 return rc;
368}
369
370
371/**
372 * Same as STAMR3Register except that the name is specified in a
373 * RTStrPrintf like fashion.
374 *
375 * @returns VBox status.
376 * @param pVM The VM handle.
377 * @param pvSample Pointer to the sample.
378 * @param enmType Sample type. This indicates what pvSample is pointing at.
379 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
380 * @param enmUnit Sample unit.
381 * @param pszDesc Sample description.
382 * @param pszName The sample name format string.
383 * @param ... Arguments to the format string.
384 */
385VMMR3DECL(int) STAMR3RegisterF(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
386 const char *pszDesc, const char *pszName, ...)
387{
388 va_list args;
389 va_start(args, pszName);
390 int rc = STAMR3RegisterVU(pVM->pUVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
391 va_end(args);
392 return rc;
393}
394
395
396/**
397 * Same as STAMR3Register except that the name is specified in a
398 * RTStrPrintfV like fashion.
399 *
400 * @returns VBox status.
401 * @param pVM The VM handle.
402 * @param pvSample Pointer to the sample.
403 * @param enmType Sample type. This indicates what pvSample is pointing at.
404 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
405 * @param enmUnit Sample unit.
406 * @param pszDesc Sample description.
407 * @param pszName The sample name format string.
408 * @param args Arguments to the format string.
409 */
410VMMR3DECL(int) STAMR3RegisterVU(PUVM pUVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
411 const char *pszDesc, const char *pszName, va_list args)
412{
413 AssertReturn(enmType != STAMTYPE_CALLBACK, VERR_INVALID_PARAMETER);
414
415 char *pszFormattedName;
416 RTStrAPrintfV(&pszFormattedName, pszName, args);
417 if (!pszFormattedName)
418 return VERR_NO_MEMORY;
419
420 int rc = STAMR3RegisterU(pUVM, pvSample, enmType, enmVisibility, pszFormattedName, enmUnit, pszDesc);
421 RTStrFree(pszFormattedName);
422 return rc;
423}
424
425
426/**
427 * Same as STAMR3Register except that the name is specified in a
428 * RTStrPrintfV like fashion.
429 *
430 * @returns VBox status.
431 * @param pVM The VM handle.
432 * @param pvSample Pointer to the sample.
433 * @param enmType Sample type. This indicates what pvSample is pointing at.
434 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
435 * @param enmUnit Sample unit.
436 * @param pszDesc Sample description.
437 * @param pszName The sample name format string.
438 * @param args Arguments to the format string.
439 */
440VMMR3DECL(int) STAMR3RegisterV(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
441 const char *pszDesc, const char *pszName, va_list args)
442{
443 return STAMR3RegisterVU(pVM->pUVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
444}
445
446
447/**
448 * Similar to STAMR3Register except for the two callbacks, the implied type (STAMTYPE_CALLBACK),
449 * and name given in an RTStrPrintf like fashion.
450 *
451 * @returns VBox status.
452 * @param pVM The VM handle.
453 * @param pvSample Pointer to the sample.
454 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
455 * @param enmUnit Sample unit.
456 * @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
457 * @param pfnPrint Print the sample.
458 * @param pszDesc Sample description.
459 * @param pszName The sample name format string.
460 * @param ... Arguments to the format string.
461 * @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
462 */
463VMMR3DECL(int) STAMR3RegisterCallback(PVM pVM, void *pvSample, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
464 PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
465 const char *pszDesc, const char *pszName, ...)
466{
467 va_list args;
468 va_start(args, pszName);
469 int rc = STAMR3RegisterCallbackV(pVM, pvSample, enmVisibility, enmUnit, pfnReset, pfnPrint, pszDesc, pszName, args);
470 va_end(args);
471 return rc;
472}
473
474
475/**
476 * Same as STAMR3RegisterCallback() except for the ellipsis which is a va_list here.
477 *
478 * @returns VBox status.
479 * @param pVM The VM handle.
480 * @param pvSample Pointer to the sample.
481 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
482 * @param enmUnit Sample unit.
483 * @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
484 * @param pfnPrint Print the sample.
485 * @param pszDesc Sample description.
486 * @param pszName The sample name format string.
487 * @param args Arguments to the format string.
488 * @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
489 */
490VMMR3DECL(int) STAMR3RegisterCallbackV(PVM pVM, void *pvSample, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
491 PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
492 const char *pszDesc, const char *pszName, va_list args)
493{
494 char *pszFormattedName;
495 RTStrAPrintfV(&pszFormattedName, pszName, args);
496 if (!pszFormattedName)
497 return VERR_NO_MEMORY;
498
499 int rc = stamR3RegisterU(pVM->pUVM, pvSample, pfnReset, pfnPrint, STAMTYPE_CALLBACK, enmVisibility, pszFormattedName, enmUnit, pszDesc);
500 RTStrFree(pszFormattedName);
501 return rc;
502}
503
504
505#ifdef VBOX_STRICT
506/**
507 * Divide the strings into sub-strings using '/' as delimiter
508 * and then compare them in strcmp fashion.
509 *
510 * @returns Difference.
511 * @retval 0 if equal.
512 * @retval < 0 if psz1 is less than psz2.
513 * @retval > 0 if psz1 greater than psz2.
514 *
515 * @param psz1 The first string.
516 * @param psz2 The second string.
517 */
518static int stamR3SlashCompare(const char *psz1, const char *psz2)
519{
520 for (;;)
521 {
522 unsigned int ch1 = *psz1++;
523 unsigned int ch2 = *psz2++;
524 if (ch1 != ch2)
525 {
526 /* slash is end-of-sub-string, so it trumps everything but '\0'. */
527 if (ch1 == '/')
528 return ch2 ? -1 : 1;
529 if (ch2 == '/')
530 return ch1 ? 1 : -1;
531 return ch1 - ch2;
532 }
533
534 /* done? */
535 if (ch1 == '\0')
536 return 0;
537 }
538}
539#endif /* VBOX_STRICT */
540
541
542/**
543 * Internal worker for the different register calls.
544 *
545 * @returns VBox status.
546 * @param pUVM Pointer to the user mode VM structure.
547 * @param pvSample Pointer to the sample.
548 * @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
549 * @param pfnPrint Print the sample.
550 * @param enmType Sample type. This indicates what pvSample is pointing at.
551 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
552 * @param enmUnit Sample unit.
553 * @param pszDesc Sample description.
554 * @param pszName The sample name format string.
555 * @param args Arguments to the format string.
556 * @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
557 */
558static int stamR3RegisterU(PUVM pUVM, void *pvSample, PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
559 STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
560{
561 STAM_LOCK_WR(pUVM);
562
563 /*
564 * Check if exists.
565 */
566 PSTAMDESC pPrev = NULL;
567 PSTAMDESC pCur = pUVM->stam.s.pHead;
568 while (pCur)
569 {
570 int iDiff = strcmp(pCur->pszName, pszName);
571 /* passed it */
572 if (iDiff > 0)
573 break;
574 /* found it. */
575 if (!iDiff)
576 {
577 STAM_UNLOCK_WR(pUVM);
578 AssertMsgFailed(("Duplicate sample name: %s\n", pszName));
579 return VERR_ALREADY_EXISTS;
580 }
581
582 /* next */
583 pPrev = pCur;
584 pCur = pCur->pNext;
585 }
586
587 /*
588 * Check that the name doesn't screw up sorting order when taking
589 * slashes into account. The QT4 GUI makes some assumptions.
590 * Problematic chars are: !"#$%&'()*+,-.
591 */
592 Assert(pszName[0] == '/');
593 if (pPrev)
594 Assert(stamR3SlashCompare(pPrev->pszName, pszName) < 0);
595 if (pCur)
596 Assert(stamR3SlashCompare(pCur->pszName, pszName) > 0);
597
598#ifdef VBOX_STRICT
599 /*
600 * Check alignment requirements.
601 */
602 switch (enmType)
603 {
604 /* 8 byte / 64-bit */
605 case STAMTYPE_U64:
606 case STAMTYPE_U64_RESET:
607 case STAMTYPE_X64:
608 case STAMTYPE_X64_RESET:
609 case STAMTYPE_COUNTER:
610 case STAMTYPE_PROFILE:
611 case STAMTYPE_PROFILE_ADV:
612 AssertMsg(!((uintptr_t)pvSample & 7), ("%p - %s\n", pvSample, pszName));
613 break;
614
615 /* 4 byte / 32-bit */
616 case STAMTYPE_RATIO_U32:
617 case STAMTYPE_RATIO_U32_RESET:
618 case STAMTYPE_U32:
619 case STAMTYPE_U32_RESET:
620 case STAMTYPE_X32:
621 case STAMTYPE_X32_RESET:
622 AssertMsg(!((uintptr_t)pvSample & 3), ("%p - %s\n", pvSample, pszName));
623 break;
624
625 /* 2 byte / 32-bit */
626 case STAMTYPE_U16:
627 case STAMTYPE_U16_RESET:
628 case STAMTYPE_X16:
629 case STAMTYPE_X16_RESET:
630 AssertMsg(!((uintptr_t)pvSample & 1), ("%p - %s\n", pvSample, pszName));
631 break;
632
633 /* 1 byte / 8-bit / unaligned */
634 case STAMTYPE_U8:
635 case STAMTYPE_U8_RESET:
636 case STAMTYPE_X8:
637 case STAMTYPE_X8_RESET:
638 case STAMTYPE_CALLBACK:
639 break;
640
641 default:
642 AssertMsgFailed(("%d\n", enmType));
643 break;
644 }
645#endif /* VBOX_STRICT */
646
647 /*
648 * Create a new node and insert it at the current location.
649 */
650 int rc;
651 size_t cchName = strlen(pszName) + 1;
652 size_t cchDesc = pszDesc ? strlen(pszDesc) + 1 : 0;
653 PSTAMDESC pNew = (PSTAMDESC)RTMemAlloc(sizeof(*pNew) + cchName + cchDesc);
654 if (pNew)
655 {
656 pNew->pszName = (char *)memcpy((char *)(pNew + 1), pszName, cchName);
657 pNew->enmType = enmType;
658 pNew->enmVisibility = enmVisibility;
659 if (enmType != STAMTYPE_CALLBACK)
660 pNew->u.pv = pvSample;
661 else
662 {
663 pNew->u.Callback.pvSample = pvSample;
664 pNew->u.Callback.pfnReset = pfnReset;
665 pNew->u.Callback.pfnPrint = pfnPrint;
666 }
667 pNew->enmUnit = enmUnit;
668 pNew->pszDesc = NULL;
669 if (pszDesc)
670 pNew->pszDesc = (char *)memcpy((char *)(pNew + 1) + cchName, pszDesc, cchDesc);
671
672 pNew->pNext = pCur;
673 if (pPrev)
674 pPrev->pNext = pNew;
675 else
676 pUVM->stam.s.pHead = pNew;
677
678 stamR3ResetOne(pNew, pUVM->pVM);
679 rc = VINF_SUCCESS;
680 }
681 else
682 rc = VERR_NO_MEMORY;
683
684 STAM_UNLOCK_WR(pUVM);
685 return rc;
686}
687
688
689/**
690 * Deregisters a sample previously registered by STAR3Register().
691 *
692 * This is intended used for devices which can be unplugged and for
693 * temporary samples.
694 *
695 * @returns VBox status.
696 * @param pUVM Pointer to the user mode VM structure.
697 * @param pvSample Pointer to the sample registered with STAMR3Register().
698 */
699VMMR3DECL(int) STAMR3DeregisterU(PUVM pUVM, void *pvSample)
700{
701 STAM_LOCK_WR(pUVM);
702
703 /*
704 * Search for it.
705 */
706 int rc = VERR_INVALID_HANDLE;
707 PSTAMDESC pPrev = NULL;
708 PSTAMDESC pCur = pUVM->stam.s.pHead;
709 while (pCur)
710 {
711 if (pCur->u.pv == pvSample)
712 {
713 void *pvFree = pCur;
714 pCur = pCur->pNext;
715 if (pPrev)
716 pPrev->pNext = pCur;
717 else
718 pUVM->stam.s.pHead = pCur;
719
720 RTMemFree(pvFree);
721 rc = VINF_SUCCESS;
722 continue;
723 }
724
725 /* next */
726 pPrev = pCur;
727 pCur = pCur->pNext;
728 }
729
730 STAM_UNLOCK_WR(pUVM);
731 return rc;
732}
733
734
735/**
736 * Deregisters a sample previously registered by STAR3Register().
737 *
738 * This is intended used for devices which can be unplugged and for
739 * temporary samples.
740 *
741 * @returns VBox status.
742 * @param pVM The VM handle.
743 * @param pvSample Pointer to the sample registered with STAMR3Register().
744 */
745VMMR3DECL(int) STAMR3Deregister(PVM pVM, void *pvSample)
746{
747 return STAMR3DeregisterU(pVM->pUVM, pvSample);
748}
749
750
751/**
752 * Resets statistics for the specified VM.
753 * It's possible to select a subset of the samples.
754 *
755 * @returns VBox status. (Basically, it cannot fail.)
756 * @param pVM The VM handle.
757 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
758 * If NULL all samples are reset.
759 * @remarks Don't confuse this with the other 'XYZR3Reset' methods, it's not called at VM reset.
760 */
761VMMR3DECL(int) STAMR3ResetU(PUVM pUVM, const char *pszPat)
762{
763 int rc = VINF_SUCCESS;
764
765 /* ring-0 */
766 GVMMRESETSTATISTICSSREQ GVMMReq;
767 //GMMRESETSTATISTICSSREQ GMMReq;
768 bool fGVMMMatched = !pszPat || !*pszPat;
769 //bool fGMMMatched = fGVMMMatched;
770 if (fGVMMMatched)
771 memset(&GVMMReq.Stats, 0xff, sizeof(GVMMReq.Stats));
772 else
773 {
774 char *pszCopy;
775 unsigned cExpressions;
776 char **papszExpressions = stamR3SplitPattern(pszPat, &cExpressions, &pszCopy);
777 if (!papszExpressions)
778 return VERR_NO_MEMORY;
779
780 /* GVMM */
781 memset(&GVMMReq.Stats, 0, sizeof(GVMMReq.Stats));
782 for (unsigned i = 0; i < RT_ELEMENTS(g_aGVMMStats); i++)
783 if (stamR3MultiMatch(papszExpressions, cExpressions, NULL, g_aGVMMStats[i].pszName))
784 {
785 *((uint8_t *)&GVMMReq.Stats + g_aGVMMStats[i].offVar) = 0xff;
786 fGVMMMatched = true;
787 }
788 if (!fGVMMMatched)
789 {
790 /** @todo match cpu leaves some rainy day. */
791 }
792
793 /* GMM */
794// memset(&GMMReq.Stats, 0, sizeof(GMMReq.Stats));
795// for (unsigned i = 0; i < RT_ELEMENTS(g_aGMMStats); i++)
796// if (stamR3MultiMatch(papszExpressions, cExpressions, NULL, g_aGMMStats[i].pszName))
797// {
798// *((uint8_t *)&GMMReq.Stats + g_aGMMStats[i].offVar) = 0xff;
799// fGMMMatched = true;
800// }
801
802 RTMemTmpFree(papszExpressions);
803 RTStrFree(pszCopy);
804 }
805
806 STAM_LOCK_WR(pUVM);
807 if (fGVMMMatched)
808 {
809 PVM pVM = pUVM->pVM;
810 GVMMReq.Hdr.cbReq = sizeof(GVMMReq);
811 GVMMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
812 GVMMReq.pSession = pVM->pSession;
813 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, NIL_VMCPUID, VMMR0_DO_GVMM_RESET_STATISTICS, 0, &GVMMReq.Hdr);
814 }
815
816// if (fGMMMatched)
817// {
818// PVM pVM = pUVM->pVM;
819// GMMReq.Hdr.cbReq = sizeof(Req);
820// GMMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
821// GMMReq.pSession = pVM->pSession;
822// rc = SUPR3CallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_RESET_STATISTICS, 0, &Req.Hdr);
823// }
824
825 /* and the reset */
826 stamR3EnumU(pUVM, pszPat, false /* fUpdateRing0 */, stamR3ResetOne, pUVM->pVM);
827
828 STAM_UNLOCK_WR(pUVM);
829 return rc;
830}
831
832/**
833 * Resets statistics for the specified VM.
834 * It's possible to select a subset of the samples.
835 *
836 * @returns VBox status. (Basically, it cannot fail.)
837 * @param pVM The VM handle.
838 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
839 * If NULL all samples are reset.
840 * @remarks Don't confuse this with the other 'XYZR3Reset' methods, it's not called at VM reset.
841 */
842VMMR3DECL(int) STAMR3Reset(PVM pVM, const char *pszPat)
843{
844 return STAMR3ResetU(pVM->pUVM, pszPat);
845}
846
847
848/**
849 * Resets one statistics sample.
850 * Callback for stamR3EnumU().
851 *
852 * @returns VINF_SUCCESS
853 * @param pDesc Pointer to the current descriptor.
854 * @param pvArg User argument - The VM handle.
855 */
856static int stamR3ResetOne(PSTAMDESC pDesc, void *pvArg)
857{
858 switch (pDesc->enmType)
859 {
860 case STAMTYPE_COUNTER:
861 ASMAtomicXchgU64(&pDesc->u.pCounter->c, 0);
862 break;
863
864 case STAMTYPE_PROFILE:
865 case STAMTYPE_PROFILE_ADV:
866 ASMAtomicXchgU64(&pDesc->u.pProfile->cPeriods, 0);
867 ASMAtomicXchgU64(&pDesc->u.pProfile->cTicks, 0);
868 ASMAtomicXchgU64(&pDesc->u.pProfile->cTicksMax, 0);
869 ASMAtomicXchgU64(&pDesc->u.pProfile->cTicksMin, ~0);
870 break;
871
872 case STAMTYPE_RATIO_U32_RESET:
873 ASMAtomicXchgU32(&pDesc->u.pRatioU32->u32A, 0);
874 ASMAtomicXchgU32(&pDesc->u.pRatioU32->u32B, 0);
875 break;
876
877 case STAMTYPE_CALLBACK:
878 if (pDesc->u.Callback.pfnReset)
879 pDesc->u.Callback.pfnReset((PVM)pvArg, pDesc->u.Callback.pvSample);
880 break;
881
882 case STAMTYPE_U8_RESET:
883 case STAMTYPE_X8_RESET:
884 ASMAtomicXchgU8(pDesc->u.pu8, 0);
885 break;
886
887 case STAMTYPE_U16_RESET:
888 case STAMTYPE_X16_RESET:
889 ASMAtomicXchgU16(pDesc->u.pu16, 0);
890 break;
891
892 case STAMTYPE_U32_RESET:
893 case STAMTYPE_X32_RESET:
894 ASMAtomicXchgU32(pDesc->u.pu32, 0);
895 break;
896
897 case STAMTYPE_U64_RESET:
898 case STAMTYPE_X64_RESET:
899 ASMAtomicXchgU64(pDesc->u.pu64, 0);
900 break;
901
902 /* These are custom and will not be touched. */
903 case STAMTYPE_U8:
904 case STAMTYPE_X8:
905 case STAMTYPE_U16:
906 case STAMTYPE_X16:
907 case STAMTYPE_U32:
908 case STAMTYPE_X32:
909 case STAMTYPE_U64:
910 case STAMTYPE_X64:
911 case STAMTYPE_RATIO_U32:
912 break;
913
914 default:
915 AssertMsgFailed(("enmType=%d\n", pDesc->enmType));
916 break;
917 }
918 NOREF(pvArg);
919 return VINF_SUCCESS;
920}
921
922
923/**
924 * Get a snapshot of the statistics.
925 * It's possible to select a subset of the samples.
926 *
927 * @returns VBox status. (Basically, it cannot fail.)
928 * @param pUVM Pointer to the user mode VM structure.
929 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
930 * If NULL all samples are reset.
931 * @param fWithDesc Whether to include the descriptions.
932 * @param ppszSnapshot Where to store the pointer to the snapshot data.
933 * The format of the snapshot should be XML, but that will have to be discussed
934 * when this function is implemented.
935 * The returned pointer must be freed by calling STAMR3SnapshotFree().
936 * @param pcchSnapshot Where to store the size of the snapshot data. (Excluding the trailing '\0')
937 */
938VMMR3DECL(int) STAMR3SnapshotU(PUVM pUVM, const char *pszPat, char **ppszSnapshot, size_t *pcchSnapshot, bool fWithDesc)
939{
940 STAMR3SNAPSHOTONE State = { NULL, NULL, NULL, pUVM->pVM, 0, VINF_SUCCESS, fWithDesc };
941
942 /*
943 * Write the XML header.
944 */
945 /** @todo Make this proper & valid XML. */
946 stamR3SnapshotPrintf(&State, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
947
948 /*
949 * Write the content.
950 */
951 stamR3SnapshotPrintf(&State, "<Statistics>\n");
952 int rc = stamR3EnumU(pUVM, pszPat, true /* fUpdateRing0 */, stamR3SnapshotOne, &State);
953 stamR3SnapshotPrintf(&State, "</Statistics>\n");
954
955 if (RT_SUCCESS(rc))
956 rc = State.rc;
957 else
958 {
959 RTMemFree(State.pszStart);
960 State.pszStart = State.pszEnd = State.psz = NULL;
961 State.cbAllocated = 0;
962 }
963
964 /*
965 * Done.
966 */
967 *ppszSnapshot = State.pszStart;
968 if (pcchSnapshot)
969 *pcchSnapshot = State.psz - State.pszStart;
970 return rc;
971}
972
973
974/**
975 * Get a snapshot of the statistics.
976 * It's possible to select a subset of the samples.
977 *
978 * @returns VBox status. (Basically, it cannot fail.)
979 * @param pVM The VM handle.
980 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
981 * If NULL all samples are reset.
982 * @param fWithDesc Whether to include the descriptions.
983 * @param ppszSnapshot Where to store the pointer to the snapshot data.
984 * The format of the snapshot should be XML, but that will have to be discussed
985 * when this function is implemented.
986 * The returned pointer must be freed by calling STAMR3SnapshotFree().
987 * @param pcchSnapshot Where to store the size of the snapshot data.
988 * (Excluding the trailing '\\0')
989 */
990VMMR3DECL(int) STAMR3Snapshot(PVM pVM, const char *pszPat, char **ppszSnapshot, size_t *pcchSnapshot, bool fWithDesc)
991{
992 return STAMR3SnapshotU(pVM->pUVM, pszPat, ppszSnapshot, pcchSnapshot, fWithDesc);
993}
994
995
996/**
997 * stamR3EnumU callback employed by STAMR3Snapshot.
998 *
999 * @returns VBox status code, but it's interpreted as 0 == success / !0 == failure by enmR3Enum.
1000 * @param pDesc The sample.
1001 * @param pvArg The snapshot status structure.
1002 */
1003static int stamR3SnapshotOne(PSTAMDESC pDesc, void *pvArg)
1004{
1005 PSTAMR3SNAPSHOTONE pThis = (PSTAMR3SNAPSHOTONE)pvArg;
1006
1007 switch (pDesc->enmType)
1008 {
1009 case STAMTYPE_COUNTER:
1010 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pCounter->c == 0)
1011 return VINF_SUCCESS;
1012 stamR3SnapshotPrintf(pThis, "<Counter c=\"%lld\"", pDesc->u.pCounter->c);
1013 break;
1014
1015 case STAMTYPE_PROFILE:
1016 case STAMTYPE_PROFILE_ADV:
1017 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pProfile->cPeriods == 0)
1018 return VINF_SUCCESS;
1019 stamR3SnapshotPrintf(pThis, "<Profile cPeriods=\"%lld\" cTicks=\"%lld\" cTicksMin=\"%lld\" cTicksMax=\"%lld\"",
1020 pDesc->u.pProfile->cPeriods, pDesc->u.pProfile->cTicks, pDesc->u.pProfile->cTicksMin,
1021 pDesc->u.pProfile->cTicksMax);
1022 break;
1023
1024 case STAMTYPE_RATIO_U32:
1025 case STAMTYPE_RATIO_U32_RESET:
1026 if (pDesc->enmVisibility == STAMVISIBILITY_USED && !pDesc->u.pRatioU32->u32A && !pDesc->u.pRatioU32->u32B)
1027 return VINF_SUCCESS;
1028 stamR3SnapshotPrintf(pThis, "<Ratio32 u32A=\"%lld\" u32B=\"%lld\"",
1029 pDesc->u.pRatioU32->u32A, pDesc->u.pRatioU32->u32B);
1030 break;
1031
1032 case STAMTYPE_CALLBACK:
1033 {
1034 char szBuf[512];
1035 pDesc->u.Callback.pfnPrint(pThis->pVM, pDesc->u.Callback.pvSample, szBuf, sizeof(szBuf));
1036 stamR3SnapshotPrintf(pThis, "<Callback val=\"%s\"", szBuf);
1037 break;
1038 }
1039
1040 case STAMTYPE_U8:
1041 case STAMTYPE_U8_RESET:
1042 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
1043 return VINF_SUCCESS;
1044 stamR3SnapshotPrintf(pThis, "<U8 val=\"%u\"", *pDesc->u.pu8);
1045 break;
1046
1047 case STAMTYPE_X8:
1048 case STAMTYPE_X8_RESET:
1049 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
1050 return VINF_SUCCESS;
1051 stamR3SnapshotPrintf(pThis, "<X8 val=\"%#x\"", *pDesc->u.pu8);
1052 break;
1053
1054 case STAMTYPE_U16:
1055 case STAMTYPE_U16_RESET:
1056 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
1057 return VINF_SUCCESS;
1058 stamR3SnapshotPrintf(pThis, "<U16 val=\"%u\"", *pDesc->u.pu16);
1059 break;
1060
1061 case STAMTYPE_X16:
1062 case STAMTYPE_X16_RESET:
1063 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
1064 return VINF_SUCCESS;
1065 stamR3SnapshotPrintf(pThis, "<X16 val=\"%#x\"", *pDesc->u.pu16);
1066 break;
1067
1068 case STAMTYPE_U32:
1069 case STAMTYPE_U32_RESET:
1070 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
1071 return VINF_SUCCESS;
1072 stamR3SnapshotPrintf(pThis, "<U32 val=\"%u\"", *pDesc->u.pu32);
1073 break;
1074
1075 case STAMTYPE_X32:
1076 case STAMTYPE_X32_RESET:
1077 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
1078 return VINF_SUCCESS;
1079 stamR3SnapshotPrintf(pThis, "<X32 val=\"%#x\"", *pDesc->u.pu32);
1080 break;
1081
1082 case STAMTYPE_U64:
1083 case STAMTYPE_U64_RESET:
1084 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
1085 return VINF_SUCCESS;
1086 stamR3SnapshotPrintf(pThis, "<U64 val=\"%llu\"", *pDesc->u.pu64);
1087 break;
1088
1089 case STAMTYPE_X64:
1090 case STAMTYPE_X64_RESET:
1091 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
1092 return VINF_SUCCESS;
1093 stamR3SnapshotPrintf(pThis, "<X64 val=\"%#llx\"", *pDesc->u.pu64);
1094 break;
1095
1096 default:
1097 AssertMsgFailed(("%d\n", pDesc->enmType));
1098 return 0;
1099 }
1100
1101 stamR3SnapshotPrintf(pThis, " unit=\"%s\"", STAMR3GetUnit(pDesc->enmUnit));
1102
1103 switch (pDesc->enmVisibility)
1104 {
1105 default:
1106 case STAMVISIBILITY_ALWAYS:
1107 break;
1108 case STAMVISIBILITY_USED:
1109 stamR3SnapshotPrintf(pThis, " vis=\"used\"");
1110 break;
1111 case STAMVISIBILITY_NOT_GUI:
1112 stamR3SnapshotPrintf(pThis, " vis=\"not-gui\"");
1113 break;
1114 }
1115
1116 stamR3SnapshotPrintf(pThis, " name=\"%s\"", pDesc->pszName);
1117
1118 if (pThis->fWithDesc && pDesc->pszDesc)
1119 {
1120 /*
1121 * The description is a bit tricky as it may include chars that
1122 * xml requires to be escaped.
1123 */
1124 const char *pszBadChar = strpbrk(pDesc->pszDesc, "&<>\"'");
1125 if (!pszBadChar)
1126 return stamR3SnapshotPrintf(pThis, " desc=\"%s\"/>\n", pDesc->pszDesc);
1127
1128 stamR3SnapshotPrintf(pThis, " desc=\"");
1129 const char *pszCur = pDesc->pszDesc;
1130 do
1131 {
1132 stamR3SnapshotPrintf(pThis, "%.*s", pszBadChar - pszCur, pszCur);
1133 switch (*pszBadChar)
1134 {
1135 case '&': stamR3SnapshotPrintf(pThis, "&amp;"); break;
1136 case '<': stamR3SnapshotPrintf(pThis, "&lt;"); break;
1137 case '>': stamR3SnapshotPrintf(pThis, "&gt;"); break;
1138 case '"': stamR3SnapshotPrintf(pThis, "&quot;"); break;
1139 case '\'': stamR3SnapshotPrintf(pThis, "&apos;"); break;
1140 default: AssertMsgFailed(("%c", *pszBadChar)); break;
1141 }
1142 pszCur = pszBadChar + 1;
1143 pszBadChar = strpbrk(pszCur, "&<>\"'");
1144 } while (pszBadChar);
1145 return stamR3SnapshotPrintf(pThis, "%s\"/>\n", pszCur);
1146 }
1147 return stamR3SnapshotPrintf(pThis, "/>\n");
1148}
1149
1150
1151/**
1152 * Output callback for stamR3SnapshotPrintf.
1153 *
1154 * @returns number of bytes written.
1155 * @param pvArg The snapshot status structure.
1156 * @param pach Pointer to an array of characters (bytes).
1157 * @param cch The number or chars (bytes) to write from the array.
1158 */
1159static DECLCALLBACK(size_t) stamR3SnapshotOutput(void *pvArg, const char *pach, size_t cch)
1160{
1161 PSTAMR3SNAPSHOTONE pThis = (PSTAMR3SNAPSHOTONE)pvArg;
1162
1163 /*
1164 * Make sure we've got space for it.
1165 */
1166 if (RT_UNLIKELY((uintptr_t)pThis->pszEnd - (uintptr_t)pThis->psz < cch + 1))
1167 {
1168 if (RT_FAILURE(pThis->rc))
1169 return 0;
1170
1171 size_t cbNewSize = pThis->cbAllocated;
1172 if (cbNewSize > cch)
1173 cbNewSize *= 2;
1174 else
1175 cbNewSize += RT_ALIGN(cch + 1, 0x1000);
1176 char *pszNew = (char *)RTMemRealloc(pThis->pszStart, cbNewSize);
1177 if (!pszNew)
1178 {
1179 /*
1180 * Free up immediately, out-of-memory is bad news and this
1181 * isn't an important allocations / API.
1182 */
1183 pThis->rc = VERR_NO_MEMORY;
1184 RTMemFree(pThis->pszStart);
1185 pThis->pszStart = pThis->pszEnd = pThis->psz = NULL;
1186 pThis->cbAllocated = 0;
1187 return 0;
1188 }
1189
1190 pThis->psz = pszNew + (pThis->psz - pThis->pszStart);
1191 pThis->pszStart = pszNew;
1192 pThis->pszEnd = pszNew + cbNewSize;
1193 pThis->cbAllocated = cbNewSize;
1194 }
1195
1196 /*
1197 * Copy the chars to the buffer and terminate it.
1198 */
1199 memcpy(pThis->psz, pach, cch);
1200 pThis->psz += cch;
1201 *pThis->psz = '\0';
1202 return cch;
1203}
1204
1205
1206/**
1207 * Wrapper around RTStrFormatV for use by the snapshot API.
1208 *
1209 * @returns VBox status code.
1210 * @param pThis The snapshot status structure.
1211 * @param pszFormat The format string.
1212 * @param ... Optional arguments.
1213 */
1214static int stamR3SnapshotPrintf(PSTAMR3SNAPSHOTONE pThis, const char *pszFormat, ...)
1215{
1216 va_list va;
1217 va_start(va, pszFormat);
1218 RTStrFormatV(stamR3SnapshotOutput, pThis, NULL, NULL, pszFormat, va);
1219 va_end(va);
1220 return pThis->rc;
1221}
1222
1223
1224/**
1225 * Releases a statistics snapshot returned by STAMR3Snapshot().
1226 *
1227 * @returns VBox status.
1228 * @param pUVM Pointer to the user mode VM structure.
1229 * @param pszSnapshot The snapshot data pointer returned by STAMR3Snapshot().
1230 * NULL is allowed.
1231 */
1232VMMR3DECL(int) STAMR3SnapshotFreeU(PUVM pUVM, char *pszSnapshot)
1233{
1234 if (!pszSnapshot)
1235 RTMemFree(pszSnapshot);
1236 NOREF(pUVM);
1237 return VINF_SUCCESS;
1238}
1239
1240
1241/**
1242 * Releases a statistics snapshot returned by STAMR3Snapshot().
1243 *
1244 * @returns VBox status.
1245 * @param pVM The VM handle.
1246 * @param pszSnapshot The snapshot data pointer returned by STAMR3Snapshot().
1247 * NULL is allowed.
1248 */
1249VMMR3DECL(int) STAMR3SnapshotFree(PVM pVM, char *pszSnapshot)
1250{
1251 return STAMR3SnapshotFreeU(pVM->pUVM, pszSnapshot);
1252}
1253
1254
1255/**
1256 * Dumps the selected statistics to the log.
1257 *
1258 * @returns VBox status.
1259 * @param pUVM Pointer to the user mode VM structure.
1260 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
1261 * If NULL all samples are written to the log.
1262 */
1263VMMR3DECL(int) STAMR3DumpU(PUVM pUVM, const char *pszPat)
1264{
1265 STAMR3PRINTONEARGS Args;
1266 Args.pVM = pUVM->pVM;
1267 Args.pvArg = NULL;
1268 Args.pfnPrintf = stamR3EnumLogPrintf;
1269
1270 stamR3EnumU(pUVM, pszPat, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
1271 return VINF_SUCCESS;
1272}
1273
1274
1275/**
1276 * Dumps the selected statistics to the log.
1277 *
1278 * @returns VBox status.
1279 * @param pVM The VM handle.
1280 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
1281 * If NULL all samples are written to the log.
1282 */
1283VMMR3DECL(int) STAMR3Dump(PVM pVM, const char *pszPat)
1284{
1285 return STAMR3DumpU(pVM->pUVM, pszPat);
1286}
1287
1288
1289/**
1290 * Prints to the log.
1291 *
1292 * @param pArgs Pointer to the print one argument structure.
1293 * @param pszFormat Format string.
1294 * @param ... Format arguments.
1295 */
1296static DECLCALLBACK(void) stamR3EnumLogPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
1297{
1298 va_list va;
1299 va_start(va, pszFormat);
1300 RTLogPrintfV(pszFormat, va);
1301 va_end(va);
1302 NOREF(pArgs);
1303}
1304
1305
1306/**
1307 * Dumps the selected statistics to the release log.
1308 *
1309 * @returns VBox status.
1310 * @param pUVM Pointer to the user mode VM structure.
1311 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
1312 * If NULL all samples are written to the log.
1313 */
1314VMMR3DECL(int) STAMR3DumpToReleaseLogU(PUVM pUVM, const char *pszPat)
1315{
1316 STAMR3PRINTONEARGS Args;
1317 Args.pVM = pUVM->pVM;
1318 Args.pvArg = NULL;
1319 Args.pfnPrintf = stamR3EnumRelLogPrintf;
1320
1321 stamR3EnumU(pUVM, pszPat, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
1322 return VINF_SUCCESS;
1323}
1324
1325
1326/**
1327 * Dumps the selected statistics to the release log.
1328 *
1329 * @returns VBox status.
1330 * @param pVM The VM handle.
1331 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
1332 * If NULL all samples are written to the log.
1333 */
1334VMMR3DECL(int) STAMR3DumpToReleaseLog(PVM pVM, const char *pszPat)
1335{
1336 return STAMR3DumpToReleaseLogU(pVM->pUVM, pszPat);
1337}
1338
1339
1340/**
1341 * Prints to the release log.
1342 *
1343 * @param pArgs Pointer to the print one argument structure.
1344 * @param pszFormat Format string.
1345 * @param ... Format arguments.
1346 */
1347static DECLCALLBACK(void) stamR3EnumRelLogPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
1348{
1349 va_list va;
1350 va_start(va, pszFormat);
1351 RTLogRelPrintfV(pszFormat, va);
1352 va_end(va);
1353 NOREF(pArgs);
1354}
1355
1356
1357/**
1358 * Prints the selected statistics to standard out.
1359 *
1360 * @returns VBox status.
1361 * @param pVM The VM handle.
1362 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
1363 * If NULL all samples are reset.
1364 */
1365VMMR3DECL(int) STAMR3PrintU(PUVM pUVM, const char *pszPat)
1366{
1367 STAMR3PRINTONEARGS Args;
1368 Args.pVM = pUVM->pVM;
1369 Args.pvArg = NULL;
1370 Args.pfnPrintf = stamR3EnumPrintf;
1371
1372 stamR3EnumU(pUVM, pszPat, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
1373 return VINF_SUCCESS;
1374}
1375
1376
1377/**
1378 * Prints the selected statistics to standard out.
1379 *
1380 * @returns VBox status.
1381 * @param pVM The VM handle.
1382 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
1383 * If NULL all samples are reset.
1384 */
1385VMMR3DECL(int) STAMR3Print(PVM pVM, const char *pszPat)
1386{
1387 return STAMR3PrintU(pVM->pUVM, pszPat);
1388}
1389
1390
1391/**
1392 * Prints to stdout.
1393 *
1394 * @param pArgs Pointer to the print one argument structure.
1395 * @param pszFormat Format string.
1396 * @param ... Format arguments.
1397 */
1398static DECLCALLBACK(void) stamR3EnumPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
1399{
1400 va_list va;
1401 va_start(va, pszFormat);
1402 RTPrintfV(pszFormat, va);
1403 va_end(va);
1404 NOREF(pArgs);
1405}
1406
1407
1408/**
1409 * Prints one sample.
1410 * Callback for stamR3EnumU().
1411 *
1412 * @returns VINF_SUCCESS
1413 * @param pDesc Pointer to the current descriptor.
1414 * @param pvArg User argument - STAMR3PRINTONEARGS.
1415 */
1416static int stamR3PrintOne(PSTAMDESC pDesc, void *pvArg)
1417{
1418 PSTAMR3PRINTONEARGS pArgs = (PSTAMR3PRINTONEARGS)pvArg;
1419
1420 switch (pDesc->enmType)
1421 {
1422 case STAMTYPE_COUNTER:
1423 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pCounter->c == 0)
1424 return VINF_SUCCESS;
1425
1426 pArgs->pfnPrintf(pArgs, "%-32s %8llu %s\n", pDesc->pszName, pDesc->u.pCounter->c, STAMR3GetUnit(pDesc->enmUnit));
1427 break;
1428
1429 case STAMTYPE_PROFILE:
1430 case STAMTYPE_PROFILE_ADV:
1431 {
1432 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pProfile->cPeriods == 0)
1433 return VINF_SUCCESS;
1434
1435 uint64_t u64 = pDesc->u.pProfile->cPeriods ? pDesc->u.pProfile->cPeriods : 1;
1436 pArgs->pfnPrintf(pArgs, "%-32s %8llu %s (%12llu ticks, %7llu times, max %9llu, min %7lld)\n", pDesc->pszName,
1437 pDesc->u.pProfile->cTicks / u64, STAMR3GetUnit(pDesc->enmUnit),
1438 pDesc->u.pProfile->cTicks, pDesc->u.pProfile->cPeriods, pDesc->u.pProfile->cTicksMax, pDesc->u.pProfile->cTicksMin);
1439 break;
1440 }
1441
1442 case STAMTYPE_RATIO_U32:
1443 case STAMTYPE_RATIO_U32_RESET:
1444 if (pDesc->enmVisibility == STAMVISIBILITY_USED && !pDesc->u.pRatioU32->u32A && !pDesc->u.pRatioU32->u32B)
1445 return VINF_SUCCESS;
1446 pArgs->pfnPrintf(pArgs, "%-32s %8u:%-8u %s\n", pDesc->pszName,
1447 pDesc->u.pRatioU32->u32A, pDesc->u.pRatioU32->u32B, STAMR3GetUnit(pDesc->enmUnit));
1448 break;
1449
1450 case STAMTYPE_CALLBACK:
1451 {
1452 char szBuf[512];
1453 pDesc->u.Callback.pfnPrint(pArgs->pVM, pDesc->u.Callback.pvSample, szBuf, sizeof(szBuf));
1454 pArgs->pfnPrintf(pArgs, "%-32s %s %s\n", pDesc->pszName, szBuf, STAMR3GetUnit(pDesc->enmUnit));
1455 break;
1456 }
1457
1458 case STAMTYPE_U8:
1459 case STAMTYPE_U8_RESET:
1460 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
1461 return VINF_SUCCESS;
1462 pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu8, STAMR3GetUnit(pDesc->enmUnit));
1463 break;
1464
1465 case STAMTYPE_X8:
1466 case STAMTYPE_X8_RESET:
1467 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
1468 return VINF_SUCCESS;
1469 pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu8, STAMR3GetUnit(pDesc->enmUnit));
1470 break;
1471
1472 case STAMTYPE_U16:
1473 case STAMTYPE_U16_RESET:
1474 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
1475 return VINF_SUCCESS;
1476 pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu16, STAMR3GetUnit(pDesc->enmUnit));
1477 break;
1478
1479 case STAMTYPE_X16:
1480 case STAMTYPE_X16_RESET:
1481 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
1482 return VINF_SUCCESS;
1483 pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu16, STAMR3GetUnit(pDesc->enmUnit));
1484 break;
1485
1486 case STAMTYPE_U32:
1487 case STAMTYPE_U32_RESET:
1488 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
1489 return VINF_SUCCESS;
1490 pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu32, STAMR3GetUnit(pDesc->enmUnit));
1491 break;
1492
1493 case STAMTYPE_X32:
1494 case STAMTYPE_X32_RESET:
1495 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
1496 return VINF_SUCCESS;
1497 pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu32, STAMR3GetUnit(pDesc->enmUnit));
1498 break;
1499
1500 case STAMTYPE_U64:
1501 case STAMTYPE_U64_RESET:
1502 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
1503 return VINF_SUCCESS;
1504 pArgs->pfnPrintf(pArgs, "%-32s %8llu %s\n", pDesc->pszName, *pDesc->u.pu64, STAMR3GetUnit(pDesc->enmUnit));
1505 break;
1506
1507 case STAMTYPE_X64:
1508 case STAMTYPE_X64_RESET:
1509 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
1510 return VINF_SUCCESS;
1511 pArgs->pfnPrintf(pArgs, "%-32s %8llx %s\n", pDesc->pszName, *pDesc->u.pu64, STAMR3GetUnit(pDesc->enmUnit));
1512 break;
1513
1514 default:
1515 AssertMsgFailed(("enmType=%d\n", pDesc->enmType));
1516 break;
1517 }
1518 NOREF(pvArg);
1519 return VINF_SUCCESS;
1520}
1521
1522
1523/**
1524 * Enumerate the statistics by the means of a callback function.
1525 *
1526 * @returns Whatever the callback returns.
1527 *
1528 * @param pUVM Pointer to the user mode VM structure.
1529 * @param pszPat The pattern to match samples.
1530 * @param pfnEnum The callback function.
1531 * @param pvUser The pvUser argument of the callback function.
1532 */
1533VMMR3DECL(int) STAMR3EnumU(PUVM pUVM, const char *pszPat, PFNSTAMR3ENUM pfnEnum, void *pvUser)
1534{
1535 STAMR3ENUMONEARGS Args;
1536 Args.pVM = pUVM->pVM;
1537 Args.pfnEnum = pfnEnum;
1538 Args.pvUser = pvUser;
1539
1540 return stamR3EnumU(pUVM, pszPat, true /* fUpdateRing0 */, stamR3EnumOne, &Args);
1541}
1542
1543
1544/**
1545 * Enumerate the statistics by the means of a callback function.
1546 *
1547 * @returns Whatever the callback returns.
1548 *
1549 * @param pVM The VM handle.
1550 * @param pszPat The pattern to match samples.
1551 * @param pfnEnum The callback function.
1552 * @param pvUser The pvUser argument of the callback function.
1553 */
1554VMMR3DECL(int) STAMR3Enum(PVM pVM, const char *pszPat, PFNSTAMR3ENUM pfnEnum, void *pvUser)
1555{
1556 return STAMR3EnumU(pVM->pUVM, pszPat, pfnEnum, pvUser);
1557}
1558
1559
1560/**
1561 * Callback function for STARTR3Enum().
1562 *
1563 * @returns whatever the callback returns.
1564 * @param pDesc Pointer to the current descriptor.
1565 * @param pvArg Points to a STAMR3ENUMONEARGS structure.
1566 */
1567static int stamR3EnumOne(PSTAMDESC pDesc, void *pvArg)
1568{
1569 PSTAMR3ENUMONEARGS pArgs = (PSTAMR3ENUMONEARGS)pvArg;
1570 int rc;
1571 if (pDesc->enmType == STAMTYPE_CALLBACK)
1572 {
1573 /* Give the enumerator something useful. */
1574 char szBuf[512];
1575 pDesc->u.Callback.pfnPrint(pArgs->pVM, pDesc->u.Callback.pvSample, szBuf, sizeof(szBuf));
1576 rc = pArgs->pfnEnum(pDesc->pszName, pDesc->enmType, szBuf, pDesc->enmUnit,
1577 pDesc->enmVisibility, pDesc->pszDesc, pArgs->pvUser);
1578 }
1579 else
1580 rc = pArgs->pfnEnum(pDesc->pszName, pDesc->enmType, pDesc->u.pv, pDesc->enmUnit,
1581 pDesc->enmVisibility, pDesc->pszDesc, pArgs->pvUser);
1582 return rc;
1583}
1584
1585
1586/**
1587 * Match a name against an array of patterns.
1588 *
1589 * @returns true if it matches, false if it doesn't match.
1590 * @param papszExpressions The array of pattern expressions.
1591 * @param cExpressions The number of array entries.
1592 * @param piExpression Where to read/store the current skip index. Optional.
1593 * @param pszName The name to match.
1594 */
1595static bool stamR3MultiMatch(const char * const *papszExpressions, unsigned cExpressions,
1596 unsigned *piExpression, const char *pszName)
1597{
1598 for (unsigned i = piExpression ? *piExpression : 0; i < cExpressions; i++)
1599 {
1600 const char *pszPat = papszExpressions[i];
1601 if (RTStrSimplePatternMatch(pszPat, pszName))
1602 {
1603 /* later:
1604 if (piExpression && i > *piExpression)
1605 {
1606 check if we can skip some expressions
1607 }*/
1608 return true;
1609 }
1610 }
1611 return false;
1612}
1613
1614
1615/**
1616 * Splits a multi pattern into single ones.
1617 *
1618 * @returns Pointer to an array of single patterns. Free it with RTMemTmpFree.
1619 * @param pszPat The pattern to split.
1620 * @param pcExpressions The number of array elements.
1621 * @param pszCopy The pattern copy to free using RTStrFree.
1622 */
1623static char **stamR3SplitPattern(const char *pszPat, unsigned *pcExpressions, char **ppszCopy)
1624{
1625 Assert(pszPat && *pszPat);
1626
1627 char *pszCopy = RTStrDup(pszPat);
1628 if (!pszCopy)
1629 return NULL;
1630
1631 /* count them & allocate array. */
1632 char *psz = pszCopy;
1633 unsigned cExpressions = 1;
1634 while ((psz = strchr(psz, '|')) != NULL)
1635 cExpressions++, psz++;
1636
1637 char **papszExpressions = (char **)RTMemTmpAllocZ((cExpressions + 1) * sizeof(char *));
1638 if (!papszExpressions)
1639 {
1640 RTStrFree(pszCopy);
1641 return NULL;
1642 }
1643
1644 /* split */
1645 psz = pszCopy;
1646 for (unsigned i = 0;;)
1647 {
1648 papszExpressions[i] = psz;
1649 if (++i >= cExpressions)
1650 break;
1651 psz = strchr(psz, '|');
1652 *psz++ = '\0';
1653 }
1654
1655 /* sort the array, putting '*' last. */
1656 /** @todo sort it... */
1657
1658 *pcExpressions = cExpressions;
1659 *ppszCopy = pszCopy;
1660 return papszExpressions;
1661}
1662
1663
1664/**
1665 * Enumerates the nodes selected by a pattern or all nodes if no pattern
1666 * is specified.
1667 *
1668 * The call may lock STAM for writing before calling this function, however do
1669 * not lock it for reading as this function may need to write lock STAM.
1670 *
1671 * @returns The rc from the callback.
1672 * @param pUVM Pointer to the user mode VM structure.
1673 * @param pszPat Pattern.
1674 * @param fUpdateRing0 Update the ring-0 .
1675 * @param pfnCallback Callback function which shall be called for matching nodes.
1676 * If it returns anything but VINF_SUCCESS the enumeration is
1677 * terminated and the status code returned to the caller.
1678 * @param pvArg User parameter for the callback.
1679 */
1680static int stamR3EnumU(PUVM pUVM, const char *pszPat, bool fUpdateRing0,
1681 int (*pfnCallback)(PSTAMDESC pDesc, void *pvArg), void *pvArg)
1682{
1683 int rc = VINF_SUCCESS;
1684
1685 /*
1686 * All
1687 */
1688 if (!pszPat || !*pszPat || !strcmp(pszPat, "*"))
1689 {
1690 if (fUpdateRing0)
1691 stamR3Ring0StatsUpdateU(pUVM, "*");
1692
1693 STAM_LOCK_RD(pUVM);
1694 PSTAMDESC pCur = pUVM->stam.s.pHead;
1695 while (pCur)
1696 {
1697 rc = pfnCallback(pCur, pvArg);
1698 if (rc)
1699 break;
1700
1701 /* next */
1702 pCur = pCur->pNext;
1703 }
1704 STAM_UNLOCK_RD(pUVM);
1705 }
1706
1707 /*
1708 * Single expression pattern.
1709 */
1710 else if (!strchr(pszPat, '|'))
1711 {
1712 if (fUpdateRing0)
1713 stamR3Ring0StatsUpdateU(pUVM, pszPat);
1714
1715 STAM_LOCK_RD(pUVM);
1716 /** @todo This needs to be optimized since the GUI is using this path for the VM info dialog.
1717 * Note that it's doing exact matching. Organizing the samples in a tree would speed up thing
1718 * no end (at least for debug and profile builds). */
1719 for (PSTAMDESC pCur = pUVM->stam.s.pHead; pCur; pCur = pCur->pNext)
1720 if (RTStrSimplePatternMatch(pszPat, pCur->pszName))
1721 {
1722 rc = pfnCallback(pCur, pvArg);
1723 if (rc)
1724 break;
1725 }
1726 STAM_UNLOCK_RD(pUVM);
1727 }
1728
1729 /*
1730 * Multi expression pattern.
1731 */
1732 else
1733 {
1734 /*
1735 * Split up the pattern first.
1736 */
1737 char *pszCopy;
1738 unsigned cExpressions;
1739 char **papszExpressions = stamR3SplitPattern(pszPat, &cExpressions, &pszCopy);
1740 if (!papszExpressions)
1741 return VERR_NO_MEMORY;
1742
1743 /*
1744 * Perform the enumeration.
1745 */
1746 if (fUpdateRing0)
1747 stamR3Ring0StatsUpdateMultiU(pUVM, papszExpressions, cExpressions);
1748
1749 STAM_LOCK_RD(pUVM);
1750 unsigned iExpression = 0;
1751 for (PSTAMDESC pCur = pUVM->stam.s.pHead; pCur; pCur = pCur->pNext)
1752 if (stamR3MultiMatch(papszExpressions, cExpressions, &iExpression, pCur->pszName))
1753 {
1754 rc = pfnCallback(pCur, pvArg);
1755 if (rc)
1756 break;
1757 }
1758 STAM_UNLOCK_RD(pUVM);
1759
1760 RTMemTmpFree(papszExpressions);
1761 RTStrFree(pszCopy);
1762 }
1763
1764 return rc;
1765}
1766
1767
1768/**
1769 * Registers the ring-0 statistics.
1770 *
1771 * @param pUVM Pointer to the user mode VM structure.
1772 */
1773static void stamR3Ring0StatsRegisterU(PUVM pUVM)
1774{
1775 /* GVMM */
1776 for (unsigned i = 0; i < RT_ELEMENTS(g_aGVMMStats); i++)
1777 stamR3RegisterU(pUVM, (uint8_t *)&pUVM->stam.s.GVMMStats + g_aGVMMStats[i].offVar, NULL, NULL,
1778 g_aGVMMStats[i].enmType, STAMVISIBILITY_ALWAYS, g_aGVMMStats[i].pszName,
1779 g_aGVMMStats[i].enmUnit, g_aGVMMStats[i].pszDesc);
1780 pUVM->stam.s.cRegisteredHostCpus = 0;
1781}
1782
1783
1784/**
1785 * Updates the ring-0 statistics (the copy).
1786 *
1787 * @param pUVM Pointer to the user mode VM structure.
1788 * @param pszPat The pattern.
1789 */
1790static void stamR3Ring0StatsUpdateU(PUVM pUVM, const char *pszPat)
1791{
1792 stamR3Ring0StatsUpdateMultiU(pUVM, &pszPat, 1);
1793}
1794
1795
1796/**
1797 * Updates the ring-0 statistics.
1798 *
1799 * The ring-0 statistics aren't directly addressable from ring-3 and must be
1800 * copied when needed.
1801 *
1802 * @param pUVM Pointer to the user mode VM structure.
1803 * @param pszPat The pattern (for knowing when to skip).
1804 */
1805static void stamR3Ring0StatsUpdateMultiU(PUVM pUVM, const char * const *papszExpressions, unsigned cExpressions)
1806{
1807 PVM pVM = pUVM->pVM;
1808 if (!pVM || !pVM->pSession)
1809 return;
1810
1811 /* GVMM */
1812 bool fUpdate = false;
1813 for (unsigned i = 0; i < RT_ELEMENTS(g_aGVMMStats); i++)
1814 if (stamR3MultiMatch(papszExpressions, cExpressions, NULL, g_aGVMMStats[i].pszName))
1815 {
1816 fUpdate = true;
1817 break;
1818 }
1819 if (!fUpdate)
1820 {
1821 /** @todo check the cpu leaves - rainy day. */
1822 }
1823 if (fUpdate)
1824 {
1825 GVMMQUERYSTATISTICSSREQ Req;
1826 Req.Hdr.cbReq = sizeof(Req);
1827 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
1828 Req.pSession = pVM->pSession;
1829 int rc = SUPR3CallVMMR0Ex(pVM->pVMR0, NIL_VMCPUID, VMMR0_DO_GVMM_QUERY_STATISTICS, 0, &Req.Hdr);
1830 if (RT_SUCCESS(rc))
1831 {
1832 pUVM->stam.s.GVMMStats = Req.Stats;
1833
1834 /*
1835 * Check if the number of host CPUs has changed (it will the first
1836 * time around and normally never again).
1837 */
1838 if (RT_UNLIKELY(pUVM->stam.s.GVMMStats.cHostCpus > pUVM->stam.s.cRegisteredHostCpus))
1839 {
1840 STAM_LOCK_WR(pUVM);
1841 if (RT_UNLIKELY(pUVM->stam.s.GVMMStats.cHostCpus > pUVM->stam.s.cRegisteredHostCpus))
1842 {
1843 uint32_t cCpus = pUVM->stam.s.GVMMStats.cHostCpus;
1844 for (uint32_t iCpu = pUVM->stam.s.cRegisteredHostCpus; iCpu < cCpus; iCpu++)
1845 {
1846 char szName[120];
1847 size_t cchBase = RTStrPrintf(szName, sizeof(szName), "/GVMM/HostCpus/%u", iCpu);
1848 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].idCpu, NULL, NULL,
1849 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_NONE, "Host CPU ID");
1850 strcpy(&szName[cchBase], "/idxCpuSet");
1851 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].idxCpuSet, NULL, NULL,
1852 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_NONE, "CPU Set index");
1853 strcpy(&szName[cchBase], "/DesiredHz");
1854 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].uDesiredHz, NULL, NULL,
1855 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_HZ, "The desired frequency");
1856 strcpy(&szName[cchBase], "/CurTimerHz");
1857 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].uTimerHz, NULL, NULL,
1858 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_HZ, "The current timer frequency");
1859 strcpy(&szName[cchBase], "/PPTChanges");
1860 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].cChanges, NULL, NULL,
1861 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_OCCURENCES, "RTTimerChangeInterval calls");
1862 strcpy(&szName[cchBase], "/PPTStarts");
1863 stamR3RegisterU(pUVM, &pUVM->stam.s.GVMMStats.aHostCpus[iCpu].cStarts, NULL, NULL,
1864 STAMTYPE_U32, STAMVISIBILITY_ALWAYS, szName, STAMUNIT_OCCURENCES, "RTTimerStart calls");
1865 }
1866 pUVM->stam.s.cRegisteredHostCpus = cCpus;
1867 }
1868 STAM_UNLOCK_WR(pUVM);
1869 }
1870 }
1871 }
1872}
1873
1874
1875/**
1876 * Get the unit string.
1877 *
1878 * @returns Pointer to read only unit string.
1879 * @param enmUnit The unit.
1880 */
1881VMMR3DECL(const char *) STAMR3GetUnit(STAMUNIT enmUnit)
1882{
1883 switch (enmUnit)
1884 {
1885 case STAMUNIT_NONE: return "";
1886 case STAMUNIT_CALLS: return "calls";
1887 case STAMUNIT_COUNT: return "count";
1888 case STAMUNIT_BYTES: return "bytes";
1889 case STAMUNIT_PAGES: return "pages";
1890 case STAMUNIT_ERRORS: return "errors";
1891 case STAMUNIT_OCCURENCES: return "times";
1892 case STAMUNIT_TICKS: return "ticks";
1893 case STAMUNIT_TICKS_PER_CALL: return "ticks/call";
1894 case STAMUNIT_TICKS_PER_OCCURENCE: return "ticks/time";
1895 case STAMUNIT_GOOD_BAD: return "good:bad";
1896 case STAMUNIT_MEGABYTES: return "megabytes";
1897 case STAMUNIT_KILOBYTES: return "kilobytes";
1898 case STAMUNIT_NS: return "ns";
1899 case STAMUNIT_NS_PER_CALL: return "ns/call";
1900 case STAMUNIT_NS_PER_OCCURENCE: return "ns/time";
1901 case STAMUNIT_PCT: return "%";
1902 case STAMUNIT_HZ: return "Hz";
1903
1904 default:
1905 AssertMsgFailed(("Unknown unit %d\n", enmUnit));
1906 return "(?unit?)";
1907 }
1908}
1909
1910#ifdef VBOX_WITH_DEBUGGER
1911
1912/**
1913 * The '.stats' command.
1914 *
1915 * @returns VBox status.
1916 * @param pCmd Pointer to the command descriptor (as registered).
1917 * @param pCmdHlp Pointer to command helper functions.
1918 * @param pVM Pointer to the current VM (if any).
1919 * @param paArgs Pointer to (readonly) array of arguments.
1920 * @param cArgs Number of arguments in the array.
1921 */
1922static DECLCALLBACK(int) stamR3CmdStats(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
1923{
1924 /*
1925 * Validate input.
1926 */
1927 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
1928 PUVM pUVM = pVM->pUVM;
1929 if (!pUVM->stam.s.pHead)
1930 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No statistics present");
1931
1932 /*
1933 * Do the printing.
1934 */
1935 STAMR3PRINTONEARGS Args;
1936 Args.pVM = pVM;
1937 Args.pvArg = pCmdHlp;
1938 Args.pfnPrintf = stamR3EnumDbgfPrintf;
1939
1940 return stamR3EnumU(pUVM, cArgs ? paArgs[0].u.pszString : NULL, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
1941}
1942
1943
1944/**
1945 * Display one sample in the debugger.
1946 *
1947 * @param pArgs Pointer to the print one argument structure.
1948 * @param pszFormat Format string.
1949 * @param ... Format arguments.
1950 */
1951static DECLCALLBACK(void) stamR3EnumDbgfPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
1952{
1953 PDBGCCMDHLP pCmdHlp = (PDBGCCMDHLP)pArgs->pvArg;
1954
1955 va_list va;
1956 va_start(va, pszFormat);
1957 pCmdHlp->pfnPrintfV(pCmdHlp, NULL, pszFormat, va);
1958 va_end(va);
1959 NOREF(pArgs);
1960}
1961
1962
1963/**
1964 * The '.statsreset' command.
1965 *
1966 * @returns VBox status.
1967 * @param pCmd Pointer to the command descriptor (as registered).
1968 * @param pCmdHlp Pointer to command helper functions.
1969 * @param pVM Pointer to the current VM (if any).
1970 * @param paArgs Pointer to (readonly) array of arguments.
1971 * @param cArgs Number of arguments in the array.
1972 */
1973static DECLCALLBACK(int) stamR3CmdStatsReset(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs)
1974{
1975 /*
1976 * Validate input.
1977 */
1978 DBGC_CMDHLP_REQ_VM_RET(pCmdHlp, pCmd, pVM);
1979 PUVM pUVM = pVM->pUVM;
1980 if (!pUVM->stam.s.pHead)
1981 return DBGCCmdHlpFail(pCmdHlp, pCmd, "No statistics present");
1982
1983 /*
1984 * Execute reset.
1985 */
1986 int rc = STAMR3ResetU(pUVM, cArgs ? paArgs[0].u.pszString : NULL);
1987 if (RT_SUCCESS(rc))
1988 return DBGCCmdHlpFailRc(pCmdHlp, pCmd, rc, "STAMR3ResetU");
1989 return DBGCCmdHlpPrintf(pCmdHlp, "Statistics have been reset.\n");
1990}
1991
1992#endif /* VBOX_WITH_DEBUGGER */
1993
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