VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxManage/VBoxManageMetrics.cpp@ 14736

Last change on this file since 14736 was 14736, checked in by vboxsync, 16 years ago

VBoxManageMetrics.cpp: file header

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.7 KB
Line 
1/* $Id: VBoxManageMetrics.cpp 14736 2008-11-27 18:47:42Z vboxsync $ */
2/** @file
3 * VBoxManage - The 'metrics' command.
4 */
5
6/*
7 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#ifndef VBOX_ONLY_DOCS
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27#include <VBox/com/com.h>
28#include <VBox/com/array.h>
29#include <VBox/com/ErrorInfo.h>
30#include <VBox/com/VirtualBox.h>
31
32#include <iprt/asm.h>
33#include <iprt/stream.h>
34#include <iprt/string.h>
35#include <iprt/time.h>
36#include <iprt/thread.h>
37#include <VBox/log.h>
38
39#include "VBoxManage.h"
40using namespace com;
41
42
43// funcs
44///////////////////////////////////////////////////////////////////////////////
45
46
47static char *toBaseMetricNames(const char *metricList)
48{
49 char *newList = (char*)RTMemAlloc(strlen(metricList) + 1);
50 int cSlashes = 0;
51 bool fSkip = false;
52 const char *src = metricList;
53 char c, *dst = newList;
54 while ((c = *src++))
55 if (c == ':')
56 fSkip = true;
57 else if (c == '/' && ++cSlashes == 2)
58 fSkip = true;
59 else if (c == ',')
60 {
61 fSkip = false;
62 cSlashes = 0;
63 *dst++ = c;
64 }
65 else
66 if (!fSkip)
67 *dst++ = c;
68 *dst = 0;
69 return newList;
70}
71
72static int parseFilterParameters(int argc, char *argv[],
73 ComPtr<IVirtualBox> aVirtualBox,
74 ComSafeArrayOut(BSTR, outMetrics),
75 ComSafeArrayOut(BSTR, outBaseMetrics),
76 ComSafeArrayOut(IUnknown *, outObjects))
77{
78 HRESULT rc = S_OK;
79 com::SafeArray<BSTR> retMetrics(1);
80 com::SafeArray<BSTR> retBaseMetrics(1);
81 com::SafeIfaceArray <IUnknown> retObjects;
82
83 Bstr metricNames, baseNames;
84
85 /* Metric list */
86 if (argc > 1)
87 {
88 metricNames = argv[1];
89 char *tmp = toBaseMetricNames(argv[1]);
90 if (!tmp)
91 return VERR_NO_MEMORY;
92 baseNames = tmp;
93 RTMemFree(tmp);
94 }
95 else
96 {
97 metricNames = L"*";
98 baseNames = L"*";
99 }
100 metricNames.cloneTo(&retMetrics[0]);
101 baseNames.cloneTo(&retBaseMetrics[0]);
102
103 /* Object name */
104 if (argc > 0 && strcmp(argv[0], "*"))
105 {
106 if (!strcmp(argv[0], "host"))
107 {
108 ComPtr<IHost> host;
109 CHECK_ERROR(aVirtualBox, COMGETTER(Host)(host.asOutParam()));
110 retObjects.reset(1);
111 host.queryInterfaceTo(&retObjects[0]);
112 }
113 else
114 {
115 ComPtr <IMachine> machine;
116 rc = aVirtualBox->FindMachine(Bstr(argv[0]), machine.asOutParam());
117 if (SUCCEEDED (rc))
118 {
119 retObjects.reset(1);
120 machine.queryInterfaceTo(&retObjects[0]);
121 }
122 else
123 {
124 errorArgument("Invalid machine name: '%s'", argv[0]);
125 return rc;
126 }
127 }
128
129 }
130
131 retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
132 retBaseMetrics.detachTo(ComSafeArrayOutArg(outBaseMetrics));
133 retObjects.detachTo(ComSafeArrayOutArg(outObjects));
134
135 return rc;
136}
137
138static Bstr getObjectName(ComPtr<IVirtualBox> aVirtualBox,
139 ComPtr<IUnknown> aObject)
140{
141 HRESULT rc;
142
143 ComPtr<IHost> host = aObject;
144 if (!host.isNull())
145 return Bstr("host");
146
147 ComPtr<IMachine> machine = aObject;
148 if (!machine.isNull())
149 {
150 Bstr name;
151 CHECK_ERROR(machine, COMGETTER(Name)(name.asOutParam()));
152 if (SUCCEEDED(rc))
153 return name;
154 }
155 return Bstr("unknown");
156}
157
158static void listAffectedMetrics(ComPtr<IVirtualBox> aVirtualBox,
159 ComSafeArrayIn(IPerformanceMetric*, aMetrics))
160{
161 HRESULT rc;
162 com::SafeIfaceArray<IPerformanceMetric> metrics(ComSafeArrayInArg(aMetrics));
163 if (metrics.size())
164 {
165 ComPtr<IUnknown> object;
166 Bstr metricName;
167 RTPrintf("The following metrics were modified:\n\n"
168 "Object Metric\n"
169 "---------- --------------------\n");
170 for (size_t i = 0; i < metrics.size(); i++)
171 {
172 CHECK_ERROR(metrics[i], COMGETTER(Object)(object.asOutParam()));
173 CHECK_ERROR(metrics[i], COMGETTER(MetricName)(metricName.asOutParam()));
174 RTPrintf("%-10ls %-20ls\n",
175 getObjectName(aVirtualBox, object).raw(), metricName.raw());
176 }
177 RTPrintf("\n");
178 }
179 else
180 {
181 RTPrintf("No metrics match the specified filter!\n");
182 }
183}
184
185/**
186 * list *
187 */
188static int handleMetricsList(int argc, char *argv[],
189 ComPtr<IVirtualBox> aVirtualBox,
190 ComPtr<IPerformanceCollector> performanceCollector)
191{
192 HRESULT rc;
193 com::SafeArray<BSTR> metrics;
194 com::SafeArray<BSTR> baseMetrics;
195 com::SafeIfaceArray<IUnknown> objects;
196
197 rc = parseFilterParameters(argc - 1, &argv[1], aVirtualBox,
198 ComSafeArrayAsOutParam(metrics),
199 ComSafeArrayAsOutParam(baseMetrics),
200 ComSafeArrayAsOutParam(objects));
201 if (FAILED(rc))
202 return 1;
203
204 com::SafeIfaceArray<IPerformanceMetric> metricInfo;
205
206 CHECK_ERROR(performanceCollector,
207 GetMetrics(ComSafeArrayAsInParam(metrics),
208 ComSafeArrayAsInParam(objects),
209 ComSafeArrayAsOutParam(metricInfo)));
210
211 ComPtr<IUnknown> object;
212 Bstr metricName, unit, description;
213 ULONG period, count;
214 LONG minimum, maximum;
215 RTPrintf(
216"Object Metric Unit Minimum Maximum Period Count Description\n"
217"---------- -------------------- ---- ---------- ---------- ---------- ---------- -----------\n");
218 for (size_t i = 0; i < metricInfo.size(); i++)
219 {
220 CHECK_ERROR(metricInfo[i], COMGETTER(Object)(object.asOutParam()));
221 CHECK_ERROR(metricInfo[i], COMGETTER(MetricName)(metricName.asOutParam()));
222 CHECK_ERROR(metricInfo[i], COMGETTER(Period)(&period));
223 CHECK_ERROR(metricInfo[i], COMGETTER(Count)(&count));
224 CHECK_ERROR(metricInfo[i], COMGETTER(MinimumValue)(&minimum));
225 CHECK_ERROR(metricInfo[i], COMGETTER(MaximumValue)(&maximum));
226 CHECK_ERROR(metricInfo[i], COMGETTER(Unit)(unit.asOutParam()));
227 CHECK_ERROR(metricInfo[i], COMGETTER(Description)(description.asOutParam()));
228 RTPrintf("%-10ls %-20ls %-4ls %10d %10d %10u %10u %ls\n",
229 getObjectName(aVirtualBox, object).raw(), metricName.raw(), unit.raw(),
230 minimum, maximum, period, count, description.raw());
231 }
232
233 return 0;
234}
235
236/**
237 * Metics setup
238 */
239static int handleMetricsSetup(int argc, char *argv[],
240 ComPtr<IVirtualBox> aVirtualBox,
241 ComPtr<IPerformanceCollector> performanceCollector)
242{
243 HRESULT rc;
244 com::SafeArray<BSTR> metrics;
245 com::SafeArray<BSTR> baseMetrics;
246 com::SafeIfaceArray<IUnknown> objects;
247 ULONG period = 1, samples = 1;
248 bool listMatches = false;
249 int i;
250
251 for (i = 1; i < argc; i++)
252 {
253 if (strcmp(argv[i], "-period") == 0)
254 {
255 if (argc <= i + 1)
256 return errorArgument("Missing argument to '%s'", argv[i]);
257 char *endptr = NULL;
258 period = strtoul (argv[++i], &endptr, 10);
259 if (!endptr || *endptr || !period)
260 return errorArgument("Invalid value for 'period' parameter: '%s'", argv[i]);
261 }
262 else if (strcmp(argv[i], "-samples") == 0)
263 {
264 if (argc <= i + 1)
265 return errorArgument("Missing argument to '%s'", argv[i]);
266 char *endptr = NULL;
267 samples = strtoul (argv[++i], &endptr, 10);
268 if (!endptr || *endptr)
269 return errorArgument("Invalid value for 'samples' parameter: '%s'", argv[i]);
270 }
271 else if (strcmp(argv[i], "-list") == 0)
272 listMatches = true;
273 else
274 break; /* The rest of params should define the filter */
275 }
276
277 rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
278 ComSafeArrayAsOutParam(metrics),
279 ComSafeArrayAsOutParam(baseMetrics),
280 ComSafeArrayAsOutParam(objects));
281 if (FAILED(rc))
282 return 1;
283
284 com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
285 CHECK_ERROR(performanceCollector,
286 SetupMetrics(ComSafeArrayAsInParam(metrics),
287 ComSafeArrayAsInParam(objects), period, samples,
288 ComSafeArrayAsOutParam(affectedMetrics)));
289 if (listMatches)
290 listAffectedMetrics(aVirtualBox,
291 ComSafeArrayAsInParam(affectedMetrics));
292
293 return 0;
294}
295
296/**
297 * metrics query
298 */
299static int handleMetricsQuery(int argc, char *argv[],
300 ComPtr<IVirtualBox> aVirtualBox,
301 ComPtr<IPerformanceCollector> performanceCollector)
302{
303 HRESULT rc;
304 com::SafeArray<BSTR> metrics;
305 com::SafeArray<BSTR> baseMetrics;
306 com::SafeIfaceArray<IUnknown> objects;
307
308 rc = parseFilterParameters(argc - 1, &argv[1], aVirtualBox,
309 ComSafeArrayAsOutParam(metrics),
310 ComSafeArrayAsOutParam(baseMetrics),
311 ComSafeArrayAsOutParam(objects));
312 if (FAILED(rc))
313 return 1;
314
315 com::SafeArray<BSTR> retNames;
316 com::SafeIfaceArray<IUnknown> retObjects;
317 com::SafeArray<BSTR> retUnits;
318 com::SafeArray<ULONG> retScales;
319 com::SafeArray<ULONG> retSequenceNumbers;
320 com::SafeArray<ULONG> retIndices;
321 com::SafeArray<ULONG> retLengths;
322 com::SafeArray<LONG> retData;
323 CHECK_ERROR (performanceCollector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
324 ComSafeArrayAsInParam(objects),
325 ComSafeArrayAsOutParam(retNames),
326 ComSafeArrayAsOutParam(retObjects),
327 ComSafeArrayAsOutParam(retUnits),
328 ComSafeArrayAsOutParam(retScales),
329 ComSafeArrayAsOutParam(retSequenceNumbers),
330 ComSafeArrayAsOutParam(retIndices),
331 ComSafeArrayAsOutParam(retLengths),
332 ComSafeArrayAsOutParam(retData)) );
333
334 RTPrintf("Object Metric Values\n"
335 "---------- -------------------- --------------------------------------------\n");
336 for (unsigned i = 0; i < retNames.size(); i++)
337 {
338 Bstr metricUnit(retUnits[i]);
339 Bstr metricName(retNames[i]);
340 RTPrintf("%-10ls %-20ls ", getObjectName(aVirtualBox, retObjects[i]).raw(), metricName.raw());
341 const char *separator = "";
342 for (unsigned j = 0; j < retLengths[i]; j++)
343 {
344 if (retScales[i] == 1)
345 RTPrintf("%s%d %ls", separator, retData[retIndices[i] + j], metricUnit.raw());
346 else
347 RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[i] + j] / retScales[i],
348 (retData[retIndices[i] + j] * 100 / retScales[i]) % 100, metricUnit.raw());
349 separator = ", ";
350 }
351 RTPrintf("\n");
352 }
353
354 return 0;
355}
356
357static void getTimestamp(char *pts, size_t tsSize)
358{
359 *pts = 0;
360 AssertReturnVoid(tsSize >= 13); /* 3+3+3+3+1 */
361 RTTIMESPEC TimeSpec;
362 RTTIME Time;
363 RTTimeExplode(&Time, RTTimeNow(&TimeSpec));
364 pts += RTStrFormatNumber(pts, Time.u8Hour, 10, 2, 0, RTSTR_F_ZEROPAD);
365 *pts++ = ':';
366 pts += RTStrFormatNumber(pts, Time.u8Minute, 10, 2, 0, RTSTR_F_ZEROPAD);
367 *pts++ = ':';
368 pts += RTStrFormatNumber(pts, Time.u8Second, 10, 2, 0, RTSTR_F_ZEROPAD);
369 *pts++ = '.';
370 pts += RTStrFormatNumber(pts, Time.u32Nanosecond / 1000000, 10, 3, 0, RTSTR_F_ZEROPAD);
371 *pts = 0;
372}
373
374/** Used by the handleMetricsCollect loop. */
375static bool volatile g_fKeepGoing = true;
376
377#ifdef RT_OS_WINDOWS
378/**
379 * Handler routine for catching Ctrl-C, Ctrl-Break and closing of
380 * the console.
381 *
382 * @returns true if handled, false if not handled.
383 * @param dwCtrlType The type of control signal.
384 *
385 * @remarks This is called on a new thread.
386 */
387static BOOL WINAPI ctrlHandler(DWORD dwCtrlType)
388{
389 switch (dwCtrlType)
390 {
391 /* Ctrl-C or Ctrl-Break or Close */
392 case CTRL_C_EVENT:
393 case CTRL_BREAK_EVENT:
394 case CTRL_CLOSE_EVENT:
395 /* Let's shut down gracefully. */
396 ASMAtomicWriteBool(&g_fKeepGoing, false);
397 return TRUE;
398 }
399 /* Don't care about the rest -- let it die a horrible death. */
400 return FALSE;
401}
402#endif /* RT_OS_WINDOWS */
403
404/**
405 * collect
406 */
407static int handleMetricsCollect(int argc, char *argv[],
408 ComPtr<IVirtualBox> aVirtualBox,
409 ComPtr<IPerformanceCollector> performanceCollector)
410{
411 HRESULT rc;
412 com::SafeArray<BSTR> metrics;
413 com::SafeArray<BSTR> baseMetrics;
414 com::SafeIfaceArray<IUnknown> objects;
415 ULONG period = 1, samples = 1;
416 bool isDetached = false, listMatches = false;
417 int i;
418 for (i = 1; i < argc; i++)
419 {
420 if (strcmp(argv[i], "-period") == 0)
421 {
422 if (argc <= i + 1)
423 return errorArgument("Missing argument to '%s'", argv[i]);
424 char *endptr = NULL;
425 period = strtoul (argv[++i], &endptr, 10);
426 if (!endptr || *endptr || !period)
427 return errorArgument("Invalid value for 'period' parameter: '%s'", argv[i]);
428 }
429 else if (strcmp(argv[i], "-samples") == 0)
430 {
431 if (argc <= i + 1)
432 return errorArgument("Missing argument to '%s'", argv[i]);
433 char *endptr = NULL;
434 samples = strtoul (argv[++i], &endptr, 10);
435 if (!endptr || *endptr || !samples)
436 return errorArgument("Invalid value for 'samples' parameter: '%s'", argv[i]);
437 }
438 else if (strcmp(argv[i], "-list") == 0)
439 listMatches = true;
440 else if (strcmp(argv[i], "-detach") == 0)
441 isDetached = true;
442 else
443 break; /* The rest of params should define the filter */
444 }
445
446 rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
447 ComSafeArrayAsOutParam(metrics),
448 ComSafeArrayAsOutParam(baseMetrics),
449 ComSafeArrayAsOutParam(objects));
450 if (FAILED(rc))
451 return 1;
452
453
454 com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
455 CHECK_ERROR(performanceCollector,
456 SetupMetrics(ComSafeArrayAsInParam(baseMetrics),
457 ComSafeArrayAsInParam(objects), period, samples,
458 ComSafeArrayAsOutParam(affectedMetrics)));
459 if (listMatches)
460 listAffectedMetrics(aVirtualBox,
461 ComSafeArrayAsInParam(affectedMetrics));
462 if (!affectedMetrics.size())
463 return 1;
464
465 if (isDetached)
466 {
467 RTPrintf("Warning! The background process holding collected metrics will shutdown\n"
468 "in few seconds, discarding all collected data and parameters.\n");
469 return 0;
470 }
471
472#ifdef RT_OS_WINDOWS
473 SetConsoleCtrlHandler(ctrlHandler, true);
474#endif /* RT_OS_WINDOWS */
475
476 RTPrintf("Time stamp Object Metric Value\n");
477
478 while (g_fKeepGoing)
479 {
480 RTPrintf("------------ ---------- -------------------- --------------------\n");
481 RTThreadSleep(period * 1000); // Sleep for 'period' seconds
482 char ts[15];
483
484 getTimestamp(ts, sizeof(ts));
485 com::SafeArray<BSTR> retNames;
486 com::SafeIfaceArray<IUnknown> retObjects;
487 com::SafeArray<BSTR> retUnits;
488 com::SafeArray<ULONG> retScales;
489 com::SafeArray<ULONG> retSequenceNumbers;
490 com::SafeArray<ULONG> retIndices;
491 com::SafeArray<ULONG> retLengths;
492 com::SafeArray<LONG> retData;
493 CHECK_ERROR (performanceCollector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
494 ComSafeArrayAsInParam(objects),
495 ComSafeArrayAsOutParam(retNames),
496 ComSafeArrayAsOutParam(retObjects),
497 ComSafeArrayAsOutParam(retUnits),
498 ComSafeArrayAsOutParam(retScales),
499 ComSafeArrayAsOutParam(retSequenceNumbers),
500 ComSafeArrayAsOutParam(retIndices),
501 ComSafeArrayAsOutParam(retLengths),
502 ComSafeArrayAsOutParam(retData)) );
503 for (unsigned i = 0; i < retNames.size(); i++)
504 {
505 Bstr metricUnit(retUnits[i]);
506 Bstr metricName(retNames[i]);
507 RTPrintf("%-12s %-10ls %-20ls ", ts, getObjectName(aVirtualBox, retObjects[i]).raw(), metricName.raw());
508 const char *separator = "";
509 for (unsigned j = 0; j < retLengths[i]; j++)
510 {
511 if (retScales[i] == 1)
512 RTPrintf("%s%d %ls", separator, retData[retIndices[i] + j], metricUnit.raw());
513 else
514 RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[i] + j] / retScales[i],
515 (retData[retIndices[i] + j] * 100 / retScales[i]) % 100, metricUnit.raw());
516 separator = ", ";
517 }
518 RTPrintf("\n");
519 }
520 }
521
522#ifdef RT_OS_WINDOWS
523 SetConsoleCtrlHandler(ctrlHandler, false);
524#endif /* RT_OS_WINDOWS */
525
526 return 0;
527}
528
529int handleMetrics(int argc, char *argv[],
530 ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
531{
532 int rc;
533
534 /* at least one option: subcommand name */
535 if (argc < 1)
536 return errorSyntax(USAGE_METRICS, "Subcommand missing");
537
538 ComPtr<IPerformanceCollector> performanceCollector;
539 CHECK_ERROR(aVirtualBox, COMGETTER(PerformanceCollector)(performanceCollector.asOutParam()));
540
541 if (!strcmp(argv[0], "list"))
542 rc = handleMetricsList(argc, argv, aVirtualBox, performanceCollector);
543 else if (!strcmp(argv[0], "setup"))
544 rc = handleMetricsSetup(argc, argv, aVirtualBox, performanceCollector);
545 else if (!strcmp(argv[0], "query"))
546 rc = handleMetricsQuery(argc, argv, aVirtualBox, performanceCollector);
547 else if (!strcmp(argv[0], "collect"))
548 rc = handleMetricsCollect(argc, argv, aVirtualBox, performanceCollector);
549 else
550 return errorSyntax(USAGE_METRICS, "Invalid subcommand '%s'", argv[0]);
551
552 return rc;
553}
554
555#endif /* VBOX_ONLY_DOCS */
556
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette