VirtualBox

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

Last change on this file since 5605 was 5275, checked in by vboxsync, 17 years ago

GVMMSTATS offsets intead of VM.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 56.9 KB
Line 
1/* $Id: STAM.cpp 5275 2007-10-12 18:26:35Z vboxsync $ */
2/** @file
3 * STAM - The Statistics Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_STAM
23#include <VBox/stam.h>
24#include "STAMInternal.h"
25#include <VBox/vm.h>
26#include <VBox/err.h>
27#include <VBox/dbg.h>
28#include <VBox/log.h>
29
30#include <iprt/assert.h>
31#include <iprt/asm.h>
32#include <iprt/alloc.h>
33#include <iprt/stream.h>
34#include <iprt/string.h>
35
36
37/*******************************************************************************
38* Structures and Typedefs *
39*******************************************************************************/
40/**
41 * Argument structure for stamR3PrintOne().
42 */
43typedef struct STAMR3PRINTONEARGS
44{
45 PVM pVM;
46 void *pvArg;
47 DECLCALLBACKMEMBER(void, pfnPrintf)(struct STAMR3PRINTONEARGS *pvArg, const char *pszFormat, ...);
48} STAMR3PRINTONEARGS, *PSTAMR3PRINTONEARGS;
49
50
51/**
52 * Argument structure to stamR3EnumOne().
53 */
54typedef struct STAMR3ENUMONEARGS
55{
56 PVM pVM;
57 PFNSTAMR3ENUM pfnEnum;
58 void *pvUser;
59} STAMR3ENUMONEARGS, *PSTAMR3ENUMONEARGS;
60
61
62/**
63 * The snapshot status structure.
64 * Argument package passed to stamR3SnapshotOne, stamR3SnapshotPrintf and stamR3SnapshotOutput.
65 */
66typedef struct STAMR3SNAPSHOTONE
67{
68 /** Pointer to the buffer start. */
69 char *pszStart;
70 /** Pointer to the buffer end. */
71 char *pszEnd;
72 /** Pointer to the current buffer position. */
73 char *psz;
74 /** The VM handle (just in case). */
75 PVM pVM;
76 /** The number of bytes allocated. */
77 size_t cbAllocated;
78 /** The status code. */
79 int rc;
80 /** Whether to include the description strings. */
81 bool fWithDesc;
82} STAMR3SNAPSHOTONE, *PSTAMR3SNAPSHOTONE;
83
84
85/**
86 * Init record for a ring-0 statistic sample.
87 */
88typedef struct STAMR0SAMPLE
89{
90 /** The GVMMSTATS structure offset of the variable. */
91 unsigned offVar;
92 /** The type. */
93 STAMTYPE enmType;
94 /** The unit. */
95 STAMUNIT enmUnit;
96 /** The name. */
97 const char *pszName;
98 /** The description. */
99 const char *pszDesc;
100} STAMR0SAMPLE;
101
102
103/*******************************************************************************
104* Internal Functions *
105*******************************************************************************/
106static int stamR3Register(PVM pVM, void *pvSample, PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
107 STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc);
108static int stamR3ResetOne(PSTAMDESC pDesc, void *pvArg);
109static DECLCALLBACK(void) stamR3EnumLogPrintf(PSTAMR3PRINTONEARGS pvArg, const char *pszFormat, ...);
110static DECLCALLBACK(void) stamR3EnumRelLogPrintf(PSTAMR3PRINTONEARGS pvArg, const char *pszFormat, ...);
111static DECLCALLBACK(void) stamR3EnumPrintf(PSTAMR3PRINTONEARGS pvArg, const char *pszFormat, ...);
112static int stamR3SnapshotOne(PSTAMDESC pDesc, void *pvArg);
113static int stamR3SnapshotPrintf(PSTAMR3SNAPSHOTONE pThis, const char *pszFormat, ...);
114static int stamR3PrintOne(PSTAMDESC pDesc, void *pvArg);
115static int stamR3EnumOne(PSTAMDESC pDesc, void *pvArg);
116static bool stamR3MultiMatch(const char * const *papszExpressions, unsigned cExpressions, unsigned *piExpression, const char *pszName);
117static char **stamR3SplitPattern(const char *pszPat, unsigned *pcExpressions, char **ppszCopy);
118static int stamR3Enum(PVM pVM, const char *pszPat, bool fUpdateRing0, int (pfnCallback)(PSTAMDESC pDesc, void *pvArg), void *pvArg);
119static void stamR3Ring0StatsRegister(PVM pVM);
120static void stamR3Ring0StatsUpdate(PVM pVM, const char *pszPat);
121static void stamR3Ring0StatsUpdateMulti(PVM pVM, const char * const *papszExpressions, unsigned cExpressions);
122
123#ifdef VBOX_WITH_DEBUGGER
124static DECLCALLBACK(int) stamR3CmdStats(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
125static DECLCALLBACK(void) stamR3EnumDbgfPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...);
126static DECLCALLBACK(int) stamR3CmdStatsReset(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult);
127#endif
128
129
130/*******************************************************************************
131* Global Variables *
132*******************************************************************************/
133#ifdef VBOX_WITH_DEBUGGER
134/** Pattern argument. */
135static const DBGCVARDESC g_aArgPat[] =
136{
137 /* cTimesMin, cTimesMax, enmCategory, fFlags, pszName, pszDescription */
138 { 0, 1, DBGCVAR_CAT_STRING, 0, "pattern", "Which samples the command shall be applied to. Use '*' as wildcard. Use ';' to separate expression." }
139};
140
141/** Command descriptors. */
142static const DBGCCMD g_aCmds[] =
143{
144 /* pszCmd, cArgsMin, cArgsMax, paArgDesc, cArgDescs, pResultDesc, fFlags, pfnHandler pszSyntax, ....pszDescription */
145 { "stats", 0, 1, &g_aArgPat[0], ELEMENTS(g_aArgPat), NULL, 0, stamR3CmdStats, "[pattern]", "Display statistics." },
146 { "statsreset", 0, 1, &g_aArgPat[0], ELEMENTS(g_aArgPat), NULL, 0, stamR3CmdStatsReset,"[pattern]", "Resets statistics." }
147};
148#endif
149
150
151/**
152 * The GVMM mapping records.
153 */
154static const STAMR0SAMPLE g_aGVMMStats[] =
155{
156 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltCalls", "The number of calls to GVMMR0SchedHalt." },
157 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltBlocking), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltBlocking", "The number of times we did go to sleep in GVMMR0SchedHalt." },
158 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltTimeouts), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltTimeouts", "The number of times we timed out in GVMMR0SchedHalt." },
159 { 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." },
160 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cHaltWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/HaltWakeUps", "The number of wake ups done during GVMMR0SchedHalt." },
161 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cWakeUpCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/WakeUpCalls", "The number of calls to GVMMR0WakeUp." },
162 { 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." },
163 { 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)." },
164 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cPollCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/PollCalls", "The number of calls to GVMMR0SchedPoll." },
165 { 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." },
166 { RT_UOFFSETOF(GVMMSTATS, SchedVM.cPollWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/VM/PollWakeUps", "The number of wake ups done during GVMMR0SchedPoll." },
167
168 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltCalls", "The number of calls to GVMMR0SchedHalt." },
169 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltBlocking), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltBlocking", "The number of times we did go to sleep in GVMMR0SchedHalt." },
170 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltTimeouts), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltTimeouts", "The number of times we timed out in GVMMR0SchedHalt." },
171 { 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." },
172 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cHaltWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/HaltWakeUps", "The number of wake ups done during GVMMR0SchedHalt." },
173 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cWakeUpCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/WakeUpCalls", "The number of calls to GVMMR0WakeUp." },
174 { 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." },
175 { 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)." },
176 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cPollCalls), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/PollCalls", "The number of calls to GVMMR0SchedPoll." },
177 { 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." },
178 { RT_UOFFSETOF(GVMMSTATS, SchedSum.cPollWakeUps), STAMTYPE_U64_RESET, STAMUNIT_CALLS, "/GVMM/Sum/PollWakeUps", "The number of wake ups done during GVMMR0SchedPoll." },
179
180 { RT_UOFFSETOF(GVMMSTATS, cVMs), STAMTYPE_U32, STAMUNIT_CALLS, "/GVMM/VMs", "The number of VMs accessible to the caller." },
181};
182
183
184/**
185 * Initializes the STAM.
186 *
187 * @returns VBox status code.
188 * @param pVM The VM to operate on.
189 */
190STAMR3DECL(int) STAMR3Init(PVM pVM)
191{
192 LogFlow(("STAMR3Init\n"));
193
194 /*
195 * Assert alignment and sizes.
196 */
197 AssertRelease(!(RT_OFFSETOF(VM, stam.s) & 31));
198 AssertRelease(sizeof(pVM->stam.s) <= sizeof(pVM->stam.padding));
199
200 /*
201 * Setup any fixed pointers and offsets.
202 */
203 pVM->stam.s.offVM = RT_OFFSETOF(VM, stam);
204 int rc = RTSemRWCreate(&pVM->stam.s.RWSem);
205 AssertRC(rc);
206 if (VBOX_FAILURE(rc))
207 return rc;
208
209 /*
210 * Register the ring-0 statistics (GVMM/GMM).
211 */
212 stamR3Ring0StatsRegister(pVM);
213
214#ifdef VBOX_WITH_DEBUGGER
215 /*
216 * Register debugger commands.
217 */
218 static bool fRegisteredCmds = false;
219 if (!fRegisteredCmds)
220 {
221 int rc = DBGCRegisterCommands(&g_aCmds[0], ELEMENTS(g_aCmds));
222 if (VBOX_SUCCESS(rc))
223 fRegisteredCmds = true;
224 }
225#endif
226
227 return VINF_SUCCESS;
228}
229
230
231/**
232 * Applies relocations to data and code managed by this
233 * component. This function will be called at init and
234 * whenever the VMM need to relocate it self inside the GC.
235 *
236 * @param pVM The VM.
237 */
238STAMR3DECL(void) STAMR3Relocate(PVM pVM)
239{
240 LogFlow(("STAMR3Relocate\n"));
241 NOREF(pVM);
242}
243
244
245/**
246 * Terminates the STAM.
247 *
248 * Termination means cleaning up and freeing all resources,
249 * the VM it self is at this point powered off or suspended.
250 *
251 * @returns VBox status code.
252 * @param pVM The VM to operate on.
253 */
254STAMR3DECL(int) STAMR3Term(PVM pVM)
255{
256 /*
257 * Free used memory and the RWLock.
258 */
259 PSTAMDESC pCur = pVM->stam.s.pHead;
260 while (pCur)
261 {
262 void *pvFree = pCur;
263 pCur = pCur->pNext;
264 RTMemFree(pvFree);
265 }
266 pVM->stam.s.pHead = NULL;
267
268 /* careful here as we might be called twice in on some failure paths (?) */
269 if (pVM->stam.s.RWSem != NIL_RTSEMRW)
270 RTSemRWDestroy(pVM->stam.s.RWSem);
271 pVM->stam.s.RWSem = NIL_RTSEMRW;
272 return VINF_SUCCESS;
273}
274
275
276
277
278/**
279 * Registers a sample with the statistics mamanger.
280 *
281 * Statistics are maintained on a per VM basis and should therefore
282 * be registered during the VM init stage. However, there is not problem
283 * registering temporary samples or samples for hotpluggable devices. Samples
284 * can be deregisterd using the STAMR3Deregister() function, but note that
285 * this is only necessary for temporary samples or hotpluggable devices.
286 *
287 * It is not possible to register the same sample twice.
288 *
289 * @returns VBox status.
290 * @param pVM The VM handle.
291 * @param pvSample Pointer to the sample.
292 * @param enmType Sample type. This indicates what pvSample is pointing at.
293 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
294 * @param pszName Sample name. The name is on this form "/<component>/<sample>".
295 * Further nesting is possible.
296 * @param enmUnit Sample unit.
297 * @param pszDesc Sample description.
298 */
299STAMR3DECL(int) STAMR3Register(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
300{
301 AssertReturn(enmType != STAMTYPE_CALLBACK, VERR_INVALID_PARAMETER);
302 return stamR3Register(pVM, pvSample, NULL, NULL, enmType, enmVisibility, pszName, enmUnit, pszDesc);
303}
304
305
306/**
307 * Same as STAMR3Register except that the name is specified in a
308 * RTStrPrintf like fashion.
309 *
310 * @returns VBox status.
311 * @param pVM The VM handle.
312 * @param pvSample Pointer to the sample.
313 * @param enmType Sample type. This indicates what pvSample is pointing at.
314 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
315 * @param enmUnit Sample unit.
316 * @param pszDesc Sample description.
317 * @param pszName The sample name format string.
318 * @param ... Arguments to the format string.
319 */
320STAMR3DECL(int) STAMR3RegisterF(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
321 const char *pszDesc, const char *pszName, ...)
322{
323 va_list args;
324 va_start(args, pszName);
325 int rc = STAMR3RegisterV(pVM, pvSample, enmType, enmVisibility, enmUnit, pszDesc, pszName, args);
326 va_end(args);
327 return rc;
328}
329
330
331/**
332 * Same as STAMR3Register except that the name is specified in a
333 * RTStrPrintfV like fashion.
334 *
335 * @returns VBox status.
336 * @param pVM The VM handle.
337 * @param pvSample Pointer to the sample.
338 * @param enmType Sample type. This indicates what pvSample is pointing at.
339 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
340 * @param enmUnit Sample unit.
341 * @param pszDesc Sample description.
342 * @param pszName The sample name format string.
343 * @param args Arguments to the format string.
344 */
345STAMR3DECL(int) STAMR3RegisterV(PVM pVM, void *pvSample, STAMTYPE enmType, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
346 const char *pszDesc, const char *pszName, va_list args)
347{
348 AssertReturn(enmType != STAMTYPE_CALLBACK, VERR_INVALID_PARAMETER);
349
350 char *pszFormattedName;
351 RTStrAPrintfV(&pszFormattedName, pszName, args);
352 if (!pszFormattedName)
353 return VERR_NO_MEMORY;
354
355 int rc = STAMR3Register(pVM, pvSample, enmType, enmVisibility, pszFormattedName, enmUnit, pszDesc);
356 RTStrFree(pszFormattedName);
357 return rc;
358}
359
360
361/**
362 * Similar to STAMR3Register except for the two callbacks, the implied type (STAMTYPE_CALLBACK),
363 * and name given in an RTStrPrintf like fashion.
364 *
365 * @returns VBox status.
366 * @param pVM The VM handle.
367 * @param pvSample Pointer to the sample.
368 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
369 * @param enmUnit Sample unit.
370 * @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
371 * @param pfnPrint Print the sample.
372 * @param pszDesc Sample description.
373 * @param pszName The sample name format string.
374 * @param ... Arguments to the format string.
375 * @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
376 */
377STAMR3DECL(int) STAMR3RegisterCallback(PVM pVM, void *pvSample, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
378 PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
379 const char *pszDesc, const char *pszName, ...)
380{
381 va_list args;
382 va_start(args, pszName);
383 int rc = STAMR3RegisterCallbackV(pVM, pvSample, enmVisibility, enmUnit, pfnReset, pfnPrint, pszDesc, pszName, args);
384 va_end(args);
385 return rc;
386}
387
388
389/**
390 * Same as STAMR3RegisterCallback() except for the ellipsis which is a va_list here.
391 *
392 * @returns VBox status.
393 * @param pVM The VM handle.
394 * @param pvSample Pointer to the sample.
395 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
396 * @param enmUnit Sample unit.
397 * @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
398 * @param pfnPrint Print the sample.
399 * @param pszDesc Sample description.
400 * @param pszName The sample name format string.
401 * @param args Arguments to the format string.
402 * @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
403 */
404STAMR3DECL(int) STAMR3RegisterCallbackV(PVM pVM, void *pvSample, STAMVISIBILITY enmVisibility, STAMUNIT enmUnit,
405 PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
406 const char *pszDesc, const char *pszName, va_list args)
407{
408 char *pszFormattedName;
409 RTStrAPrintfV(&pszFormattedName, pszName, args);
410 if (!pszFormattedName)
411 return VERR_NO_MEMORY;
412
413 int rc = stamR3Register(pVM, pvSample, pfnReset, pfnPrint, STAMTYPE_CALLBACK, enmVisibility, pszFormattedName, enmUnit, pszDesc);
414 RTStrFree(pszFormattedName);
415 return rc;
416}
417
418
419/**
420 * Internal worker for the different register calls.
421 *
422 * @returns VBox status.
423 * @param pVM The VM handle.
424 * @param pvSample Pointer to the sample.
425 * @param pfnReset Callback for resetting the sample. NULL should be used if the sample can't be reset.
426 * @param pfnPrint Print the sample.
427 * @param enmType Sample type. This indicates what pvSample is pointing at.
428 * @param enmVisibility Visibility type specifying whether unused statistics should be visible or not.
429 * @param enmUnit Sample unit.
430 * @param pszDesc Sample description.
431 * @param pszName The sample name format string.
432 * @param args Arguments to the format string.
433 * @remark There is currently no device or driver variant of this API. Add one if it should become necessary!
434 */
435static int stamR3Register(PVM pVM, void *pvSample, PFNSTAMR3CALLBACKRESET pfnReset, PFNSTAMR3CALLBACKPRINT pfnPrint,
436 STAMTYPE enmType, STAMVISIBILITY enmVisibility, const char *pszName, STAMUNIT enmUnit, const char *pszDesc)
437{
438 STAM_LOCK_WR(pVM);
439
440 /*
441 * Check if exists.
442 */
443 PSTAMDESC pPrev = NULL;
444 PSTAMDESC pCur = pVM->stam.s.pHead;
445 while (pCur)
446 {
447 int iDiff = strcmp(pCur->pszName, pszName);
448 /* passed it */
449 if (iDiff > 0)
450 break;
451 /* found it. */
452 if (!iDiff)
453 {
454 STAM_UNLOCK_WR(pVM);
455 AssertMsgFailed(("Duplicate sample name: %s\n", pszName));
456 return VERR_ALREADY_EXISTS;
457 }
458
459 /* next */
460 pPrev = pCur;
461 pCur = pCur->pNext;
462 }
463
464 /*
465 * Create a new node and insert it at the current location.
466 */
467 int rc;
468 size_t cchName = strlen(pszName) + 1;
469 size_t cchDesc = pszDesc ? strlen(pszDesc) + 1 : 0;
470 PSTAMDESC pNew = (PSTAMDESC)RTMemAlloc(sizeof(*pNew) + cchName + cchDesc);
471 if (pNew)
472 {
473 pNew->pszName = (char *)memcpy((char *)(pNew + 1), pszName, cchName);
474 pNew->enmType = enmType;
475 pNew->enmVisibility = enmVisibility;
476 if (enmType != STAMTYPE_CALLBACK)
477 pNew->u.pv = pvSample;
478 else
479 {
480 pNew->u.Callback.pvSample = pvSample;
481 pNew->u.Callback.pfnReset = pfnReset;
482 pNew->u.Callback.pfnPrint = pfnPrint;
483 }
484 pNew->enmUnit = enmUnit;
485 pNew->pszDesc = NULL;
486 if (pszDesc)
487 pNew->pszDesc = (char *)memcpy((char *)(pNew + 1) + cchName, pszDesc, cchDesc);
488
489 pNew->pNext = pCur;
490 if (pPrev)
491 pPrev->pNext = pNew;
492 else
493 pVM->stam.s.pHead = pNew;
494
495 stamR3ResetOne(pNew, pVM);
496 rc = VINF_SUCCESS;
497 }
498 else
499 rc = VERR_NO_MEMORY;
500
501 STAM_UNLOCK_WR(pVM);
502 return rc;
503}
504
505
506/**
507 * Deregisters a sample previously registered by STAR3Register().
508 *
509 * This is intended used for devices which can be unplugged and for
510 * temporary samples.
511 *
512 * @returns VBox status.
513 * @param pVM The VM handle.
514 * @param pvSample Pointer to the sample registered with STAMR3Register().
515 */
516STAMR3DECL(int) STAMR3Deregister(PVM pVM, void *pvSample)
517{
518 STAM_LOCK_WR(pVM);
519
520 /*
521 * Search for it.
522 */
523 int rc = VERR_INVALID_HANDLE;
524 PSTAMDESC pPrev = NULL;
525 PSTAMDESC pCur = pVM->stam.s.pHead;
526 while (pCur)
527 {
528 if (pCur->u.pv == pvSample)
529 {
530 void *pvFree = pCur;
531 pCur = pCur->pNext;
532 if (pPrev)
533 pPrev->pNext = pCur;
534 else
535 pVM->stam.s.pHead = pCur;
536
537 RTMemFree(pvFree);
538 rc = VINF_SUCCESS;
539 continue;
540 }
541
542 /* next */
543 pPrev = pCur;
544 pCur = pCur->pNext;
545 }
546
547 STAM_UNLOCK_WR(pVM);
548 return rc;
549}
550
551
552/**
553 * Resets statistics for the specified VM.
554 * It's possible to select a subset of the samples.
555 *
556 * @returns VBox status. (Basically, it cannot fail.)
557 * @param pVM The VM handle.
558 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
559 * If NULL all samples are reset.
560 * @remarks Don't confuse this with the other 'XYZR3Reset' methods, it's not called at VM reset.
561 */
562STAMR3DECL(int) STAMR3Reset(PVM pVM, const char *pszPat)
563{
564 int rc = VINF_SUCCESS;
565
566 /* ring-0 */
567 GVMMRESETSTATISTICSSREQ GVMMReq;
568 //GMMRESETSTATISTICSSREQ GMMReq;
569 bool fGVMMMatched = !pszPat || !*pszPat;
570 //bool fGMMMatched = fGVMMMatched;
571 if (fGVMMMatched)
572 memset(&GVMMReq.Stats, 0xff, sizeof(GVMMReq.Stats));
573 else
574 {
575 char *pszCopy;
576 unsigned cExpressions;
577 char **papszExpressions = stamR3SplitPattern(pszPat, &cExpressions, &pszCopy);
578 if (!papszExpressions)
579 return VERR_NO_MEMORY;
580
581 /* GVMM */
582 memset(&GVMMReq.Stats, 0, sizeof(GVMMReq.Stats));
583 for (unsigned i = 0; i < RT_ELEMENTS(g_aGVMMStats); i++)
584 if (stamR3MultiMatch(papszExpressions, cExpressions, NULL, g_aGVMMStats[i].pszName))
585 {
586 *((uint8_t *)&GVMMReq.Stats + g_aGVMMStats[i].offVar) = 0xff;
587 fGVMMMatched = true;
588 }
589
590 /* GMM */
591// memset(&GMMReq.Stats, 0, sizeof(GMMReq.Stats));
592// for (unsigned i = 0; i < RT_ELEMENTS(g_aGMMStats); i++)
593// if (stamR3MultiMatch(papszExpressions, cExpressions, NULL, g_aGMMStats[i].pszName))
594// {
595// *((uint8_t *)&GMMReq.Stats + g_aGMMStats[i].offVar) = 0xff;
596// fGMMMatched = true;
597// }
598
599 RTMemTmpFree(papszExpressions);
600 RTStrFree(pszCopy);
601 }
602
603 STAM_LOCK_WR(pVM);
604 if (fGVMMMatched)
605 {
606 GVMMReq.Hdr.cbReq = sizeof(GVMMReq);
607 GVMMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
608 GVMMReq.pSession = pVM->pSession;
609 rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GVMM_RESET_STATISTICS, 0, &GVMMReq.Hdr);
610 }
611
612// if (fGMMMatched)
613// {
614// GMMReq.Hdr.cbReq = sizeof(Req);
615// GMMReq.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
616// GMMReq.pSession = pVM->pSession;
617// rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GMM_RESET_STATISTICS, 0, &Req.Hdr);
618// }
619
620 /* and the reset */
621 stamR3Enum(pVM, pszPat, false /* fUpdateRing0 */, stamR3ResetOne, pVM);
622
623 STAM_UNLOCK_WR(pVM);
624 return rc;
625}
626
627
628/**
629 * Resets one statistics sample.
630 * Callback for stamR3Enum().
631 *
632 * @returns VINF_SUCCESS
633 * @param pDesc Pointer to the current descriptor.
634 * @param pvArg User argument - The VM handle.
635 */
636static int stamR3ResetOne(PSTAMDESC pDesc, void *pvArg)
637{
638 switch (pDesc->enmType)
639 {
640 case STAMTYPE_COUNTER:
641 ASMAtomicXchgU64(&pDesc->u.pCounter->c, 0);
642 break;
643
644 case STAMTYPE_PROFILE:
645 case STAMTYPE_PROFILE_ADV:
646 ASMAtomicXchgU64(&pDesc->u.pProfile->cPeriods, 0);
647 ASMAtomicXchgU64(&pDesc->u.pProfile->cTicks, 0);
648 ASMAtomicXchgU64(&pDesc->u.pProfile->cTicksMax, 0);
649 ASMAtomicXchgU64(&pDesc->u.pProfile->cTicksMin, ~0);
650 break;
651
652 case STAMTYPE_RATIO_U32_RESET:
653 ASMAtomicXchgU32(&pDesc->u.pRatioU32->u32A, 0);
654 ASMAtomicXchgU32(&pDesc->u.pRatioU32->u32B, 0);
655 break;
656
657 case STAMTYPE_CALLBACK:
658 if (pDesc->u.Callback.pfnReset)
659 pDesc->u.Callback.pfnReset((PVM)pvArg, pDesc->u.Callback.pvSample);
660 break;
661
662 case STAMTYPE_U8_RESET:
663 case STAMTYPE_X8_RESET:
664 ASMAtomicXchgU8(pDesc->u.pu8, 0);
665 break;
666
667 case STAMTYPE_U16_RESET:
668 case STAMTYPE_X16_RESET:
669 ASMAtomicXchgU16(pDesc->u.pu16, 0);
670 break;
671
672 case STAMTYPE_U32_RESET:
673 case STAMTYPE_X32_RESET:
674 ASMAtomicXchgU32(pDesc->u.pu32, 0);
675 break;
676
677 case STAMTYPE_U64_RESET:
678 case STAMTYPE_X64_RESET:
679 ASMAtomicXchgU64(pDesc->u.pu64, 0);
680 break;
681
682 /* These are custom and will not be touched. */
683 case STAMTYPE_U8:
684 case STAMTYPE_X8:
685 case STAMTYPE_U16:
686 case STAMTYPE_X16:
687 case STAMTYPE_U32:
688 case STAMTYPE_X32:
689 case STAMTYPE_U64:
690 case STAMTYPE_X64:
691 case STAMTYPE_RATIO_U32:
692 break;
693
694 default:
695 AssertMsgFailed(("enmType=%d\n", pDesc->enmType));
696 break;
697 }
698 NOREF(pvArg);
699 return VINF_SUCCESS;
700}
701
702
703/**
704 * Get a snapshot of the statistics.
705 * It's possible to select a subset of the samples.
706 *
707 * @returns VBox status. (Basically, it cannot fail.)
708 * @param pVM The VM handle.
709 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
710 * If NULL all samples are reset.
711 * @param fWithDesc Whether to include the descriptions.
712 * @param ppszSnapshot Where to store the pointer to the snapshot data.
713 * The format of the snapshot should be XML, but that will have to be discussed
714 * when this function is implemented.
715 * The returned pointer must be freed by calling STAMR3SnapshotFree().
716 * @param pcchSnapshot Where to store the size of the snapshot data. (Excluding the trailing '\0')
717 */
718STAMR3DECL(int) STAMR3Snapshot(PVM pVM, const char *pszPat, char **ppszSnapshot, size_t *pcchSnapshot, bool fWithDesc)
719{
720 STAMR3SNAPSHOTONE State = { NULL, NULL, NULL, pVM, 0, VINF_SUCCESS, fWithDesc };
721
722 /*
723 * Write the XML header.
724 */
725 /** @todo Make this proper & valid XML. */
726 stamR3SnapshotPrintf(&State, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n");
727
728 /*
729 * Write the content.
730 */
731 stamR3SnapshotPrintf(&State, "<Statistics>\n");
732 STAM_LOCK_RD(pVM);
733 int rc = stamR3Enum(pVM, pszPat, true /* fUpdateRing0 */, stamR3SnapshotOne, &State);
734 STAM_UNLOCK_RD(pVM);
735 stamR3SnapshotPrintf(&State, "</Statistics>\n");
736
737 if (VBOX_SUCCESS(rc))
738 rc = State.rc;
739 else
740 {
741 RTMemFree(State.pszStart);
742 State.pszStart = State.pszEnd = State.psz = NULL;
743 State.cbAllocated = 0;
744 }
745
746 /*
747 * Done.
748 */
749 *ppszSnapshot = State.pszStart;
750 if (pcchSnapshot)
751 *pcchSnapshot = State.psz - State.pszStart;
752 return rc;
753}
754
755
756/**
757 * stamR3Enum callback employed by STAMR3Snapshot.
758 *
759 * @returns VBox status code, but it's interpreted as 0 == success / !0 == failure by enmR3Enum.
760 * @param pDesc The sample.
761 * @param pvArg The snapshot status structure.
762 */
763static int stamR3SnapshotOne(PSTAMDESC pDesc, void *pvArg)
764{
765 PSTAMR3SNAPSHOTONE pThis = (PSTAMR3SNAPSHOTONE)pvArg;
766
767 switch (pDesc->enmType)
768 {
769 case STAMTYPE_COUNTER:
770 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pCounter->c == 0)
771 return VINF_SUCCESS;
772 stamR3SnapshotPrintf(pThis, "<Counter c=\"%lld\"", pDesc->u.pCounter->c);
773 break;
774
775 case STAMTYPE_PROFILE:
776 case STAMTYPE_PROFILE_ADV:
777 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pProfile->cPeriods == 0)
778 return VINF_SUCCESS;
779 stamR3SnapshotPrintf(pThis, "<Profile cPeriods=\"%lld\" cTicks=\"%lld\" cTicksMin=\"%lld\" cTicksMax=\"%lld\"",
780 pDesc->u.pProfile->cPeriods, pDesc->u.pProfile->cTicks, pDesc->u.pProfile->cTicksMin,
781 pDesc->u.pProfile->cTicksMax);
782 break;
783
784 case STAMTYPE_RATIO_U32:
785 case STAMTYPE_RATIO_U32_RESET:
786 if (pDesc->enmVisibility == STAMVISIBILITY_USED && !pDesc->u.pRatioU32->u32A && !pDesc->u.pRatioU32->u32B)
787 return VINF_SUCCESS;
788 stamR3SnapshotPrintf(pThis, "<Ratio32 u32A=\"%lld\" u32B=\"%lld\"",
789 pDesc->u.pRatioU32->u32A, pDesc->u.pRatioU32->u32B);
790 break;
791
792 case STAMTYPE_CALLBACK:
793 {
794 char szBuf[512];
795 pDesc->u.Callback.pfnPrint(pThis->pVM, pDesc->u.Callback.pvSample, szBuf, sizeof(szBuf));
796 stamR3SnapshotPrintf(pThis, "<Callback val=\"%s\"", szBuf);
797 break;
798 }
799
800 case STAMTYPE_U8:
801 case STAMTYPE_U8_RESET:
802 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
803 return VINF_SUCCESS;
804 stamR3SnapshotPrintf(pThis, "<U8 val=\"%u\"", *pDesc->u.pu8);
805 break;
806
807 case STAMTYPE_X8:
808 case STAMTYPE_X8_RESET:
809 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
810 return VINF_SUCCESS;
811 stamR3SnapshotPrintf(pThis, "<X8 val=\"%#x\"", *pDesc->u.pu8);
812 break;
813
814 case STAMTYPE_U16:
815 case STAMTYPE_U16_RESET:
816 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
817 return VINF_SUCCESS;
818 stamR3SnapshotPrintf(pThis, "<U16 val=\"%u\"", *pDesc->u.pu16);
819 break;
820
821 case STAMTYPE_X16:
822 case STAMTYPE_X16_RESET:
823 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
824 return VINF_SUCCESS;
825 stamR3SnapshotPrintf(pThis, "<X16 val=\"%#x\"", *pDesc->u.pu16);
826 break;
827
828 case STAMTYPE_U32:
829 case STAMTYPE_U32_RESET:
830 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
831 return VINF_SUCCESS;
832 stamR3SnapshotPrintf(pThis, "<U32 val=\"%u\"", *pDesc->u.pu32);
833 break;
834
835 case STAMTYPE_X32:
836 case STAMTYPE_X32_RESET:
837 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
838 return VINF_SUCCESS;
839 stamR3SnapshotPrintf(pThis, "<X32 val=\"%#x\"", *pDesc->u.pu32);
840 break;
841
842 case STAMTYPE_U64:
843 case STAMTYPE_U64_RESET:
844 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
845 return VINF_SUCCESS;
846 stamR3SnapshotPrintf(pThis, "<U64 val=\"%llu\"", *pDesc->u.pu64);
847 break;
848
849 case STAMTYPE_X64:
850 case STAMTYPE_X64_RESET:
851 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
852 return VINF_SUCCESS;
853 stamR3SnapshotPrintf(pThis, "<X64 val=\"%#llx\"", *pDesc->u.pu64);
854 break;
855
856 default:
857 AssertMsgFailed(("%d\n", pDesc->enmType));
858 return 0;
859 }
860
861 stamR3SnapshotPrintf(pThis, " unit=\"%s\"", STAMR3GetUnit(pDesc->enmUnit));
862
863 switch (pDesc->enmVisibility)
864 {
865 default:
866 case STAMVISIBILITY_ALWAYS:
867 break;
868 case STAMVISIBILITY_USED:
869 stamR3SnapshotPrintf(pThis, " vis=\"used\"");
870 break;
871 case STAMVISIBILITY_NOT_GUI:
872 stamR3SnapshotPrintf(pThis, " vis=\"not-gui\"");
873 break;
874 }
875
876 stamR3SnapshotPrintf(pThis, " name=\"%s\"", pDesc->pszName);
877
878 if (pThis->fWithDesc && pDesc->pszDesc)
879 return stamR3SnapshotPrintf(pThis, " desc=\"%s\"/>\n", pDesc->pszDesc);
880 return stamR3SnapshotPrintf(pThis, "/>\n");
881}
882
883
884/**
885 * Output callback for stamR3SnapshotPrintf.
886 *
887 * @returns number of bytes written.
888 * @param pvArg The snapshot status structure.
889 * @param pach Pointer to an array of characters (bytes).
890 * @param cch The number or chars (bytes) to write from the array.
891 */
892static DECLCALLBACK(size_t) stamR3SnapshotOutput(void *pvArg, const char *pach, size_t cch)
893{
894 PSTAMR3SNAPSHOTONE pThis = (PSTAMR3SNAPSHOTONE)pvArg;
895
896 /*
897 * Make sure we've got space for it.
898 */
899 if (RT_UNLIKELY((uintptr_t)pThis->pszEnd - (uintptr_t)pThis->psz < cch + 1))
900 {
901 if (RT_FAILURE(pThis->rc))
902 return 0;
903
904 size_t cbNewSize = pThis->cbAllocated;
905 if (cbNewSize > cch)
906 cbNewSize *= 2;
907 else
908 cbNewSize += RT_ALIGN(cch + 1, 0x1000);
909 char *pszNew = (char *)RTMemRealloc(pThis->pszStart, cbNewSize);
910 if (!pszNew)
911 {
912 /*
913 * Free up immediately, out-of-memory is bad news and this
914 * isn't an important allocations / API.
915 */
916 pThis->rc = VERR_NO_MEMORY;
917 RTMemFree(pThis->pszStart);
918 pThis->pszStart = pThis->pszEnd = pThis->psz = NULL;
919 pThis->cbAllocated = 0;
920 return 0;
921 }
922
923 pThis->psz = pszNew + (pThis->psz - pThis->pszStart);
924 pThis->pszStart = pszNew;
925 pThis->pszEnd = pszNew + cbNewSize;
926 pThis->cbAllocated = cbNewSize;
927 }
928
929 /*
930 * Copy the chars to the buffer and terminate it.
931 */
932 memcpy(pThis->psz, pach, cch);
933 pThis->psz += cch;
934 *pThis->psz = '\0';
935 return cch;
936}
937
938
939/**
940 * Wrapper around RTStrFormatV for use by the snapshot API.
941 *
942 * @returns VBox status code.
943 * @param pThis The snapshot status structure.
944 * @param pszFormat The format string.
945 * @param ... Optional arguments.
946 */
947static int stamR3SnapshotPrintf(PSTAMR3SNAPSHOTONE pThis, const char *pszFormat, ...)
948{
949 va_list va;
950 va_start(va, pszFormat);
951 RTStrFormatV(stamR3SnapshotOutput, pThis, NULL, NULL, pszFormat, va);
952 va_end(va);
953 return pThis->rc;
954}
955
956
957/**
958 * Releases a statistics snapshot returned by STAMR3Snapshot().
959 *
960 * @returns VBox status.
961 * @param pVM The VM handle.
962 * @param pszSnapshot The snapshot data pointer returned by STAMR3Snapshot().
963 * NULL is allowed.
964 */
965STAMR3DECL(int) STAMR3SnapshotFree(PVM pVM, char *pszSnapshot)
966{
967 if (!pszSnapshot)
968 RTMemFree(pszSnapshot);
969 return VINF_SUCCESS;
970}
971
972
973/**
974 * Dumps the selected statistics to the log.
975 *
976 * @returns VBox status.
977 * @param pVM The VM handle.
978 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
979 * If NULL all samples are written to the log.
980 */
981STAMR3DECL(int) STAMR3Dump(PVM pVM, const char *pszPat)
982{
983 STAMR3PRINTONEARGS Args;
984 Args.pVM = pVM;
985 Args.pvArg = NULL;
986 Args.pfnPrintf = stamR3EnumLogPrintf;
987
988 STAM_LOCK_RD(pVM);
989 stamR3Enum(pVM, pszPat, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
990 STAM_UNLOCK_RD(pVM);
991 return VINF_SUCCESS;
992}
993
994
995/**
996 * Prints to the log.
997 *
998 * @param pArgs Pointer to the print one argument structure.
999 * @param pszFormat Format string.
1000 * @param ... Format arguments.
1001 */
1002static DECLCALLBACK(void) stamR3EnumLogPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
1003{
1004 va_list va;
1005 va_start(va, pszFormat);
1006 RTLogPrintfV(pszFormat, va);
1007 va_end(va);
1008 NOREF(pArgs);
1009}
1010
1011
1012/**
1013 * Dumps the selected statistics to the release log.
1014 *
1015 * @returns VBox status.
1016 * @param pVM The VM handle.
1017 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
1018 * If NULL all samples are written to the log.
1019 */
1020STAMR3DECL(int) STAMR3DumpToReleaseLog(PVM pVM, const char *pszPat)
1021{
1022 STAMR3PRINTONEARGS Args;
1023 Args.pVM = pVM;
1024 Args.pvArg = NULL;
1025 Args.pfnPrintf = stamR3EnumRelLogPrintf;
1026
1027 STAM_LOCK_RD(pVM);
1028 stamR3Enum(pVM, pszPat, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
1029 STAM_UNLOCK_RD(pVM);
1030 return VINF_SUCCESS;
1031}
1032
1033
1034/**
1035 * Prints to the release log.
1036 *
1037 * @param pArgs Pointer to the print one argument structure.
1038 * @param pszFormat Format string.
1039 * @param ... Format arguments.
1040 */
1041static DECLCALLBACK(void) stamR3EnumRelLogPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
1042{
1043 va_list va;
1044 va_start(va, pszFormat);
1045 RTLogRelPrintfV(pszFormat, va);
1046 va_end(va);
1047 NOREF(pArgs);
1048}
1049
1050
1051/**
1052 * Prints the selected statistics to standard out.
1053 *
1054 * @returns VBox status.
1055 * @param pVM The VM handle.
1056 * @param pszPat The name matching pattern. See somewhere_where_this_is_described_in_detail.
1057 * If NULL all samples are reset.
1058 */
1059STAMR3DECL(int) STAMR3Print(PVM pVM, const char *pszPat)
1060{
1061 STAMR3PRINTONEARGS Args;
1062 Args.pVM = pVM;
1063 Args.pvArg = NULL;
1064 Args.pfnPrintf = stamR3EnumPrintf;
1065
1066 STAM_LOCK_RD(pVM);
1067 stamR3Enum(pVM, pszPat, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
1068 STAM_UNLOCK_RD(pVM);
1069 return VINF_SUCCESS;
1070}
1071
1072
1073/**
1074 * Prints to stdout.
1075 *
1076 * @param pArgs Pointer to the print one argument structure.
1077 * @param pszFormat Format string.
1078 * @param ... Format arguments.
1079 */
1080static DECLCALLBACK(void) stamR3EnumPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
1081{
1082 va_list va;
1083 va_start(va, pszFormat);
1084 RTPrintfV(pszFormat, va);
1085 va_end(va);
1086 NOREF(pArgs);
1087}
1088
1089
1090/**
1091 * Prints one sample.
1092 * Callback for stamR3Enum().
1093 *
1094 * @returns VINF_SUCCESS
1095 * @param pDesc Pointer to the current descriptor.
1096 * @param pvArg User argument - STAMR3PRINTONEARGS.
1097 */
1098static int stamR3PrintOne(PSTAMDESC pDesc, void *pvArg)
1099{
1100 PSTAMR3PRINTONEARGS pArgs = (PSTAMR3PRINTONEARGS)pvArg;
1101
1102 switch (pDesc->enmType)
1103 {
1104 case STAMTYPE_COUNTER:
1105 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pCounter->c == 0)
1106 return VINF_SUCCESS;
1107
1108 pArgs->pfnPrintf(pArgs, "%-32s %8llu %s\n", pDesc->pszName, pDesc->u.pCounter->c, STAMR3GetUnit(pDesc->enmUnit));
1109 break;
1110
1111 case STAMTYPE_PROFILE:
1112 case STAMTYPE_PROFILE_ADV:
1113 {
1114 if (pDesc->enmVisibility == STAMVISIBILITY_USED && pDesc->u.pProfile->cPeriods == 0)
1115 return VINF_SUCCESS;
1116
1117 uint64_t u64 = pDesc->u.pProfile->cPeriods ? pDesc->u.pProfile->cPeriods : 1;
1118 pArgs->pfnPrintf(pArgs, "%-32s %8llu %s (%12llu ticks, %7llu times, max %9llu, min %7lld)\n", pDesc->pszName,
1119 pDesc->u.pProfile->cTicks / u64, STAMR3GetUnit(pDesc->enmUnit),
1120 pDesc->u.pProfile->cTicks, pDesc->u.pProfile->cPeriods, pDesc->u.pProfile->cTicksMax, pDesc->u.pProfile->cTicksMin);
1121 break;
1122 }
1123
1124 case STAMTYPE_RATIO_U32:
1125 case STAMTYPE_RATIO_U32_RESET:
1126 if (pDesc->enmVisibility == STAMVISIBILITY_USED && !pDesc->u.pRatioU32->u32A && !pDesc->u.pRatioU32->u32B)
1127 return VINF_SUCCESS;
1128 pArgs->pfnPrintf(pArgs, "%-32s %8u:%-8u %s\n", pDesc->pszName,
1129 pDesc->u.pRatioU32->u32A, pDesc->u.pRatioU32->u32B, STAMR3GetUnit(pDesc->enmUnit));
1130 break;
1131
1132 case STAMTYPE_CALLBACK:
1133 {
1134 char szBuf[512];
1135 pDesc->u.Callback.pfnPrint(pArgs->pVM, pDesc->u.Callback.pvSample, szBuf, sizeof(szBuf));
1136 pArgs->pfnPrintf(pArgs, "%-32s %s %s\n", pDesc->pszName, szBuf, STAMR3GetUnit(pDesc->enmUnit));
1137 break;
1138 }
1139
1140 case STAMTYPE_U8:
1141 case STAMTYPE_U8_RESET:
1142 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
1143 return VINF_SUCCESS;
1144 pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu8, STAMR3GetUnit(pDesc->enmUnit));
1145 break;
1146
1147 case STAMTYPE_X8:
1148 case STAMTYPE_X8_RESET:
1149 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu8 == 0)
1150 return VINF_SUCCESS;
1151 pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu8, STAMR3GetUnit(pDesc->enmUnit));
1152 break;
1153
1154 case STAMTYPE_U16:
1155 case STAMTYPE_U16_RESET:
1156 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
1157 return VINF_SUCCESS;
1158 pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu16, STAMR3GetUnit(pDesc->enmUnit));
1159 break;
1160
1161 case STAMTYPE_X16:
1162 case STAMTYPE_X16_RESET:
1163 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu16 == 0)
1164 return VINF_SUCCESS;
1165 pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu16, STAMR3GetUnit(pDesc->enmUnit));
1166 break;
1167
1168 case STAMTYPE_U32:
1169 case STAMTYPE_U32_RESET:
1170 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
1171 return VINF_SUCCESS;
1172 pArgs->pfnPrintf(pArgs, "%-32s %8u %s\n", pDesc->pszName, *pDesc->u.pu32, STAMR3GetUnit(pDesc->enmUnit));
1173 break;
1174
1175 case STAMTYPE_X32:
1176 case STAMTYPE_X32_RESET:
1177 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu32 == 0)
1178 return VINF_SUCCESS;
1179 pArgs->pfnPrintf(pArgs, "%-32s %8x %s\n", pDesc->pszName, *pDesc->u.pu32, STAMR3GetUnit(pDesc->enmUnit));
1180 break;
1181
1182 case STAMTYPE_U64:
1183 case STAMTYPE_U64_RESET:
1184 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
1185 return VINF_SUCCESS;
1186 pArgs->pfnPrintf(pArgs, "%-32s %8llu %s\n", pDesc->pszName, *pDesc->u.pu64, STAMR3GetUnit(pDesc->enmUnit));
1187 break;
1188
1189 case STAMTYPE_X64:
1190 case STAMTYPE_X64_RESET:
1191 if (pDesc->enmVisibility == STAMVISIBILITY_USED && *pDesc->u.pu64 == 0)
1192 return VINF_SUCCESS;
1193 pArgs->pfnPrintf(pArgs, "%-32s %8llx %s\n", pDesc->pszName, *pDesc->u.pu64, STAMR3GetUnit(pDesc->enmUnit));
1194 break;
1195
1196 default:
1197 AssertMsgFailed(("enmType=%d\n", pDesc->enmType));
1198 break;
1199 }
1200 NOREF(pvArg);
1201 return VINF_SUCCESS;
1202}
1203
1204
1205/**
1206 * Enumerate the statistics by the means of a callback function.
1207 *
1208 * @returns Whatever the callback returns.
1209 *
1210 * @param pVM The VM handle.
1211 * @param pszPat The pattern to match samples.
1212 * @param pfnEnum The callback function.
1213 * @param pvUser The pvUser argument of the callback function.
1214 */
1215STAMR3DECL(int) STAMR3Enum(PVM pVM, const char *pszPat, PFNSTAMR3ENUM pfnEnum, void *pvUser)
1216{
1217 STAMR3ENUMONEARGS Args;
1218 Args.pVM = pVM;
1219 Args.pfnEnum = pfnEnum;
1220 Args.pvUser = pvUser;
1221
1222 STAM_LOCK_RD(pVM);
1223 int rc = stamR3Enum(pVM, pszPat, true /* fUpdateRing0 */, stamR3EnumOne, &Args);
1224 STAM_UNLOCK_RD(pVM);
1225 return rc;
1226}
1227
1228
1229/**
1230 * Callback function for STARTR3Enum().
1231 *
1232 * @returns whatever the callback returns.
1233 * @param pDesc Pointer to the current descriptor.
1234 * @param pvArg Points to a STAMR3ENUMONEARGS structure.
1235 */
1236static int stamR3EnumOne(PSTAMDESC pDesc, void *pvArg)
1237{
1238 PSTAMR3ENUMONEARGS pArgs = (PSTAMR3ENUMONEARGS)pvArg;
1239 int rc;
1240 if (pDesc->enmType == STAMTYPE_CALLBACK)
1241 {
1242 /* Give the enumerator something useful. */
1243 char szBuf[512];
1244 pDesc->u.Callback.pfnPrint(pArgs->pVM, pDesc->u.Callback.pvSample, szBuf, sizeof(szBuf));
1245 rc = pArgs->pfnEnum(pDesc->pszName, pDesc->enmType, szBuf, pDesc->enmUnit,
1246 pDesc->enmVisibility, pDesc->pszDesc, pArgs->pvUser);
1247 }
1248 else
1249 rc = pArgs->pfnEnum(pDesc->pszName, pDesc->enmType, pDesc->u.pv, pDesc->enmUnit,
1250 pDesc->enmVisibility, pDesc->pszDesc, pArgs->pvUser);
1251 return rc;
1252}
1253
1254
1255/**
1256 * Matches a sample name against a pattern.
1257 *
1258 * @returns True if matches, false if not.
1259 * @param pszPat Pattern.
1260 * @param pszName Name to match against the pattern.
1261 */
1262static bool stamR3Match(const char *pszPat, const char *pszName)
1263{
1264 /* ASSUMES ASCII */
1265 for (;;)
1266 {
1267 char chPat = *pszPat;
1268 switch (chPat)
1269 {
1270 default:
1271 if (*pszName != chPat)
1272 return false;
1273 break;
1274
1275 case '*':
1276 {
1277 while ((chPat = *++pszPat) == '*' || chPat == '?')
1278 /* nothing */;
1279
1280 for (;;)
1281 {
1282 char ch = *pszName++;
1283 if ( ch == chPat
1284 && ( !chPat
1285 || stamR3Match(pszPat + 1, pszName)))
1286 return true;
1287 if (!ch)
1288 return false;
1289 }
1290 /* won't ever get here */
1291 break;
1292 }
1293
1294 case '?':
1295 if (!*pszName)
1296 return false;
1297 break;
1298
1299 case '\0':
1300 return !*pszName;
1301 }
1302 pszName++;
1303 pszPat++;
1304 }
1305 return true;
1306}
1307
1308
1309/**
1310 * Match a name against an array of patterns.
1311 *
1312 * @returns true if it matches, false if it doesn't match.
1313 * @param papszExpressions The array of pattern expressions.
1314 * @param cExpressions The number of array entries.
1315 * @param piExpression Where to read/store the current skip index. Optional.
1316 * @param pszName The name to match.
1317 */
1318static bool stamR3MultiMatch(const char * const *papszExpressions, unsigned cExpressions,
1319 unsigned *piExpression, const char *pszName)
1320{
1321 for (unsigned i = piExpression ? *piExpression : 0; i < cExpressions; i++)
1322 {
1323 const char *pszPat = papszExpressions[i];
1324 if (stamR3Match(pszPat, pszName))
1325 {
1326 /* later:
1327 if (piExpression && i > *piExpression)
1328 {
1329 check if we can skip some expressions
1330 }*/
1331 return true;
1332 }
1333 }
1334 return false;
1335}
1336
1337
1338/**
1339 * Splits a multi pattern into single ones.
1340 *
1341 * @returns Pointer to an array of single patterns. Free it with RTMemTmpFree.
1342 * @param pszPat The pattern to split.
1343 * @param pcExpressions The number of array elements.
1344 * @param pszCopy The pattern copy to free using RTStrFree.
1345 */
1346static char **stamR3SplitPattern(const char *pszPat, unsigned *pcExpressions, char **ppszCopy)
1347{
1348 Assert(pszPat && *pszPat);
1349
1350 char *pszCopy = RTStrDup(pszPat);
1351 if (!pszCopy)
1352 return NULL;
1353
1354 /* count them & allocate array. */
1355 char *psz = pszCopy;
1356 unsigned cExpressions = 1;
1357 while ((psz = strchr(psz, '|')) != NULL)
1358 cExpressions++, psz++;
1359
1360 char **papszExpressions = (char **)RTMemTmpAllocZ((cExpressions + 1) * sizeof(char *));
1361 if (!papszExpressions)
1362 {
1363 RTStrFree(pszCopy);
1364 return NULL;
1365 }
1366
1367 /* split */
1368 psz = pszCopy;
1369 for (unsigned i = 0;;)
1370 {
1371 papszExpressions[i] = psz;
1372 if (++i >= cExpressions)
1373 break;
1374 psz = strchr(psz, '|');
1375 *psz++ = '\0';
1376 }
1377
1378 /* sort the array, putting '*' last. */
1379 /** @todo sort it... */
1380
1381 *pcExpressions = cExpressions;
1382 *ppszCopy = pszCopy;
1383 return papszExpressions;
1384}
1385
1386
1387/**
1388 * Enumerates the nodes selected by a pattern or all nodes if no pattern
1389 * is specified.
1390 *
1391 * The call must own at least a read lock to the STAM data.
1392 *
1393 * @returns The rc from the callback.
1394 * @param pVM VM handle
1395 * @param pszPat Pattern.
1396 * @param fUpdateRing0 Update the ring-0 .
1397 * @param pfnCallback Callback function which shall be called for matching nodes.
1398 * If it returns anything but VINF_SUCCESS the enumeration is
1399 * terminated and the status code returned to the caller.
1400 * @param pvArg User parameter for the callback.
1401 */
1402static int stamR3Enum(PVM pVM, const char *pszPat, bool fUpdateRing0, int (*pfnCallback)(PSTAMDESC pDesc, void *pvArg), void *pvArg)
1403{
1404 int rc = VINF_SUCCESS;
1405
1406 /*
1407 * All
1408 */
1409 if (!pszPat || !*pszPat || !strcmp(pszPat, "*"))
1410 {
1411 if (fUpdateRing0)
1412 stamR3Ring0StatsUpdate(pVM, "*");
1413
1414 PSTAMDESC pCur = pVM->stam.s.pHead;
1415 while (pCur)
1416 {
1417 rc = pfnCallback(pCur, pvArg);
1418 if (rc)
1419 break;
1420
1421 /* next */
1422 pCur = pCur->pNext;
1423 }
1424 }
1425
1426 /*
1427 * Single expression pattern.
1428 */
1429 else if (!strchr(pszPat, '|'))
1430 {
1431 if (fUpdateRing0)
1432 stamR3Ring0StatsUpdate(pVM, pszPat);
1433
1434 for (PSTAMDESC pCur = pVM->stam.s.pHead; pCur; pCur = pCur->pNext)
1435 if (stamR3Match(pszPat, pCur->pszName))
1436 {
1437 rc = pfnCallback(pCur, pvArg);
1438 if (rc)
1439 break;
1440 }
1441 }
1442
1443 /*
1444 * Multi expression pattern.
1445 */
1446 else
1447 {
1448 /*
1449 * Split up the pattern first.
1450 */
1451 char *pszCopy;
1452 unsigned cExpressions;
1453 char **papszExpressions = stamR3SplitPattern(pszPat, &cExpressions, &pszCopy);
1454 if (!papszExpressions)
1455 return VERR_NO_MEMORY;
1456
1457 /*
1458 * Perform the enumeration.
1459 */
1460 if (fUpdateRing0)
1461 stamR3Ring0StatsUpdateMulti(pVM, papszExpressions, cExpressions);
1462
1463 unsigned iExpression = 0;
1464 for (PSTAMDESC pCur = pVM->stam.s.pHead; pCur; pCur = pCur->pNext)
1465 if (stamR3MultiMatch(papszExpressions, cExpressions, &iExpression, pCur->pszName))
1466 {
1467 rc = pfnCallback(pCur, pvArg);
1468 if (rc)
1469 break;
1470 }
1471
1472 RTMemTmpFree(papszExpressions);
1473 RTStrFree(pszCopy);
1474 }
1475
1476 return rc;
1477}
1478
1479
1480/**
1481 * Registers the ring-0 statistics.
1482 *
1483 * @param pVM Pointer to the shared VM structure.
1484 */
1485static void stamR3Ring0StatsRegister(PVM pVM)
1486{
1487 /* GVMM */
1488 for (unsigned i = 0; i < RT_ELEMENTS(g_aGVMMStats); i++)
1489 stamR3Register(pVM, (uint8_t *)&pVM->stam.s.GVMMStats + g_aGVMMStats[i].offVar, NULL, NULL,
1490 g_aGVMMStats[i].enmType, STAMVISIBILITY_ALWAYS, g_aGVMMStats[i].pszName,
1491 g_aGVMMStats[i].enmUnit, g_aGVMMStats[i].pszDesc);
1492}
1493
1494
1495/**
1496 * Updates the ring-0 statistics (the copy).
1497 *
1498 * @param pVM Pointer to the shared VM structure.
1499 * @param pszPat The pattern.
1500 */
1501static void stamR3Ring0StatsUpdate(PVM pVM, const char *pszPat)
1502{
1503 stamR3Ring0StatsUpdateMulti(pVM, &pszPat, 1);
1504}
1505
1506
1507/**
1508 * Updates the ring-0 statistics.
1509 *
1510 * The ring-0 statistics aren't directly addressable from ring-3 and
1511 * must be copied when needed.
1512 *
1513 * @param pVM Pointer to the shared VM structure.
1514 * @param pszPat The pattern (for knowing when to skip).
1515 */
1516static void stamR3Ring0StatsUpdateMulti(PVM pVM, const char * const *papszExpressions, unsigned cExpressions)
1517{
1518 if (!pVM->pSession)
1519 return;
1520
1521 /* GVMM */
1522 bool fUpdate = false;
1523 for (unsigned i = 0; i < RT_ELEMENTS(g_aGVMMStats); i++)
1524 if (stamR3MultiMatch(papszExpressions, cExpressions, NULL, g_aGVMMStats[i].pszName))
1525 {
1526 fUpdate = true;
1527 break;
1528 }
1529 if (fUpdate)
1530 {
1531 GVMMQUERYSTATISTICSSREQ Req;
1532 Req.Hdr.cbReq = sizeof(Req);
1533 Req.Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
1534 Req.pSession = pVM->pSession;
1535 int rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_GVMM_QUERY_STATISTICS, 0, &Req.Hdr);
1536 if (RT_SUCCESS(rc))
1537 pVM->stam.s.GVMMStats = Req.Stats;
1538 }
1539}
1540
1541
1542/**
1543 * Get the unit string.
1544 *
1545 * @returns Pointer to read only unit string.
1546 * @param enmUnit The unit.
1547 */
1548STAMR3DECL(const char *) STAMR3GetUnit(STAMUNIT enmUnit)
1549{
1550 switch (enmUnit)
1551 {
1552 case STAMUNIT_NONE: return "";
1553 case STAMUNIT_CALLS: return "calls";
1554 case STAMUNIT_COUNT: return "count";
1555 case STAMUNIT_BYTES: return "bytes";
1556 case STAMUNIT_PAGES: return "pages";
1557 case STAMUNIT_ERRORS: return "errors";
1558 case STAMUNIT_OCCURENCES: return "times";
1559 case STAMUNIT_TICKS_PER_CALL: return "ticks/call";
1560 case STAMUNIT_TICKS_PER_OCCURENCE: return "ticks/time";
1561 case STAMUNIT_GOOD_BAD: return "good:bad";
1562 case STAMUNIT_MEGABYTES: return "megabytes";
1563 case STAMUNIT_KILOBYTES: return "kilobytes";
1564 case STAMUNIT_NS: return "ns";
1565 case STAMUNIT_NS_PER_CALL: return "ns/call";
1566 case STAMUNIT_NS_PER_OCCURENCE: return "ns/time";
1567 case STAMUNIT_PCT: return "%";
1568
1569 default:
1570 AssertMsgFailed(("Unknown unit %d\n", enmUnit));
1571 return "(?unit?)";
1572 }
1573}
1574
1575
1576#ifdef VBOX_WITH_DEBUGGER
1577/**
1578 * The '.stats' command.
1579 *
1580 * @returns VBox status.
1581 * @param pCmd Pointer to the command descriptor (as registered).
1582 * @param pCmdHlp Pointer to command helper functions.
1583 * @param pVM Pointer to the current VM (if any).
1584 * @param paArgs Pointer to (readonly) array of arguments.
1585 * @param cArgs Number of arguments in the array.
1586 */
1587static DECLCALLBACK(int) stamR3CmdStats(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1588{
1589 /*
1590 * Validate input.
1591 */
1592 if (!pVM)
1593 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The command requires VM to be selected.\n");
1594 if (!pVM->stam.s.pHead)
1595 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Sorry, no statistics present.\n");
1596
1597 /*
1598 * Do the printing.
1599 */
1600 STAMR3PRINTONEARGS Args;
1601 Args.pVM = pVM;
1602 Args.pvArg = pCmdHlp;
1603 Args.pfnPrintf = stamR3EnumDbgfPrintf;
1604
1605 STAM_LOCK_RD(pVM);
1606 int rc = stamR3Enum(pVM, cArgs ? paArgs[0].u.pszString : NULL, true /* fUpdateRing0 */, stamR3PrintOne, &Args);
1607 STAM_UNLOCK_RD(pVM);
1608
1609 return rc;
1610}
1611
1612
1613/**
1614 * Display one sample in the debugger.
1615 *
1616 * @param pArgs Pointer to the print one argument structure.
1617 * @param pszFormat Format string.
1618 * @param ... Format arguments.
1619 */
1620static DECLCALLBACK(void) stamR3EnumDbgfPrintf(PSTAMR3PRINTONEARGS pArgs, const char *pszFormat, ...)
1621{
1622 PDBGCCMDHLP pCmdHlp = (PDBGCCMDHLP)pArgs->pvArg;
1623
1624 va_list va;
1625 va_start(va, pszFormat);
1626 pCmdHlp->pfnPrintfV(pCmdHlp, NULL, pszFormat, va);
1627 va_end(va);
1628 NOREF(pArgs);
1629}
1630
1631
1632/**
1633 * The '.statsreset' command.
1634 *
1635 * @returns VBox status.
1636 * @param pCmd Pointer to the command descriptor (as registered).
1637 * @param pCmdHlp Pointer to command helper functions.
1638 * @param pVM Pointer to the current VM (if any).
1639 * @param paArgs Pointer to (readonly) array of arguments.
1640 * @param cArgs Number of arguments in the array.
1641 */
1642static DECLCALLBACK(int) stamR3CmdStatsReset(PCDBGCCMD pCmd, PDBGCCMDHLP pCmdHlp, PVM pVM, PCDBGCVAR paArgs, unsigned cArgs, PDBGCVAR pResult)
1643{
1644 /*
1645 * Validate input.
1646 */
1647 if (!pVM)
1648 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "error: The command requires VM to be selected.\n");
1649 if (!pVM->stam.s.pHead)
1650 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "Sorry, no statistics present.\n");
1651
1652 /*
1653 * Execute reset.
1654 */
1655 int rc = STAMR3Reset(pVM, cArgs ? paArgs[0].u.pszString : NULL);
1656 if (VBOX_SUCCESS(rc))
1657 return pCmdHlp->pfnPrintf(pCmdHlp, NULL, "info: Statistics reset.\n");
1658
1659 return pCmdHlp->pfnVBoxError(pCmdHlp, rc, "Restting statistics.\n");
1660}
1661#endif
1662
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