VirtualBox

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

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

FE/VBoxManage: Don't hold COM objects past XPCOM shutdown; pack all handler arguments to a single structure.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.6 KB
Line 
1/* $Id: VBoxManageMetrics.cpp 16052 2009-01-19 18:24:29Z 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.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 * 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 uint32_t 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 if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &period)
258 || !period)
259 return errorArgument("Invalid value for 'period' parameter: '%s'", argv[i]);
260 }
261 else if (strcmp(argv[i], "-samples") == 0)
262 {
263 if (argc <= i + 1)
264 return errorArgument("Missing argument to '%s'", argv[i]);
265 if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &samples)
266 || !samples)
267 return errorArgument("Invalid value for 'samples' parameter: '%s'", argv[i]);
268 }
269 else if (strcmp(argv[i], "-list") == 0)
270 listMatches = true;
271 else
272 break; /* The rest of params should define the filter */
273 }
274
275 rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
276 ComSafeArrayAsOutParam(metrics),
277 ComSafeArrayAsOutParam(baseMetrics),
278 ComSafeArrayAsOutParam(objects));
279 if (FAILED(rc))
280 return 1;
281
282 com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
283 CHECK_ERROR(performanceCollector,
284 SetupMetrics(ComSafeArrayAsInParam(metrics),
285 ComSafeArrayAsInParam(objects), period, samples,
286 ComSafeArrayAsOutParam(affectedMetrics)));
287 if (listMatches)
288 listAffectedMetrics(aVirtualBox,
289 ComSafeArrayAsInParam(affectedMetrics));
290
291 return 0;
292}
293
294/**
295 * metrics query
296 */
297static int handleMetricsQuery(int argc, char *argv[],
298 ComPtr<IVirtualBox> aVirtualBox,
299 ComPtr<IPerformanceCollector> performanceCollector)
300{
301 HRESULT rc;
302 com::SafeArray<BSTR> metrics;
303 com::SafeArray<BSTR> baseMetrics;
304 com::SafeIfaceArray<IUnknown> objects;
305
306 rc = parseFilterParameters(argc - 1, &argv[1], aVirtualBox,
307 ComSafeArrayAsOutParam(metrics),
308 ComSafeArrayAsOutParam(baseMetrics),
309 ComSafeArrayAsOutParam(objects));
310 if (FAILED(rc))
311 return 1;
312
313 com::SafeArray<BSTR> retNames;
314 com::SafeIfaceArray<IUnknown> retObjects;
315 com::SafeArray<BSTR> retUnits;
316 com::SafeArray<ULONG> retScales;
317 com::SafeArray<ULONG> retSequenceNumbers;
318 com::SafeArray<ULONG> retIndices;
319 com::SafeArray<ULONG> retLengths;
320 com::SafeArray<LONG> retData;
321 CHECK_ERROR (performanceCollector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
322 ComSafeArrayAsInParam(objects),
323 ComSafeArrayAsOutParam(retNames),
324 ComSafeArrayAsOutParam(retObjects),
325 ComSafeArrayAsOutParam(retUnits),
326 ComSafeArrayAsOutParam(retScales),
327 ComSafeArrayAsOutParam(retSequenceNumbers),
328 ComSafeArrayAsOutParam(retIndices),
329 ComSafeArrayAsOutParam(retLengths),
330 ComSafeArrayAsOutParam(retData)) );
331
332 RTPrintf("Object Metric Values\n"
333 "---------- -------------------- --------------------------------------------\n");
334 for (unsigned i = 0; i < retNames.size(); i++)
335 {
336 Bstr metricUnit(retUnits[i]);
337 Bstr metricName(retNames[i]);
338 RTPrintf("%-10ls %-20ls ", getObjectName(aVirtualBox, retObjects[i]).raw(), metricName.raw());
339 const char *separator = "";
340 for (unsigned j = 0; j < retLengths[i]; j++)
341 {
342 if (retScales[i] == 1)
343 RTPrintf("%s%d %ls", separator, retData[retIndices[i] + j], metricUnit.raw());
344 else
345 RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[i] + j] / retScales[i],
346 (retData[retIndices[i] + j] * 100 / retScales[i]) % 100, metricUnit.raw());
347 separator = ", ";
348 }
349 RTPrintf("\n");
350 }
351
352 return 0;
353}
354
355static void getTimestamp(char *pts, size_t tsSize)
356{
357 *pts = 0;
358 AssertReturnVoid(tsSize >= 13); /* 3+3+3+3+1 */
359 RTTIMESPEC TimeSpec;
360 RTTIME Time;
361 RTTimeExplode(&Time, RTTimeNow(&TimeSpec));
362 pts += RTStrFormatNumber(pts, Time.u8Hour, 10, 2, 0, RTSTR_F_ZEROPAD);
363 *pts++ = ':';
364 pts += RTStrFormatNumber(pts, Time.u8Minute, 10, 2, 0, RTSTR_F_ZEROPAD);
365 *pts++ = ':';
366 pts += RTStrFormatNumber(pts, Time.u8Second, 10, 2, 0, RTSTR_F_ZEROPAD);
367 *pts++ = '.';
368 pts += RTStrFormatNumber(pts, Time.u32Nanosecond / 1000000, 10, 3, 0, RTSTR_F_ZEROPAD);
369 *pts = 0;
370}
371
372/** Used by the handleMetricsCollect loop. */
373static bool volatile g_fKeepGoing = true;
374
375#ifdef RT_OS_WINDOWS
376/**
377 * Handler routine for catching Ctrl-C, Ctrl-Break and closing of
378 * the console.
379 *
380 * @returns true if handled, false if not handled.
381 * @param dwCtrlType The type of control signal.
382 *
383 * @remarks This is called on a new thread.
384 */
385static BOOL WINAPI ctrlHandler(DWORD dwCtrlType)
386{
387 switch (dwCtrlType)
388 {
389 /* Ctrl-C or Ctrl-Break or Close */
390 case CTRL_C_EVENT:
391 case CTRL_BREAK_EVENT:
392 case CTRL_CLOSE_EVENT:
393 /* Let's shut down gracefully. */
394 ASMAtomicWriteBool(&g_fKeepGoing, false);
395 return TRUE;
396 }
397 /* Don't care about the rest -- let it die a horrible death. */
398 return FALSE;
399}
400#endif /* RT_OS_WINDOWS */
401
402/**
403 * collect
404 */
405static int handleMetricsCollect(int argc, char *argv[],
406 ComPtr<IVirtualBox> aVirtualBox,
407 ComPtr<IPerformanceCollector> performanceCollector)
408{
409 HRESULT rc;
410 com::SafeArray<BSTR> metrics;
411 com::SafeArray<BSTR> baseMetrics;
412 com::SafeIfaceArray<IUnknown> objects;
413 uint32_t period = 1, samples = 1;
414 bool isDetached = false, listMatches = false;
415 int i;
416 for (i = 1; i < argc; i++)
417 {
418 if (strcmp(argv[i], "-period") == 0)
419 {
420 if (argc <= i + 1)
421 return errorArgument("Missing argument to '%s'", argv[i]);
422 if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &period)
423 || !period)
424 return errorArgument("Invalid value for 'period' parameter: '%s'", argv[i]);
425 }
426 else if (strcmp(argv[i], "-samples") == 0)
427 {
428 if (argc <= i + 1)
429 return errorArgument("Missing argument to '%s'", argv[i]);
430 if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &samples)
431 || !samples)
432 return errorArgument("Invalid value for 'samples' parameter: '%s'", argv[i]);
433 }
434 else if (strcmp(argv[i], "-list") == 0)
435 listMatches = true;
436 else if (strcmp(argv[i], "-detach") == 0)
437 isDetached = true;
438 else
439 break; /* The rest of params should define the filter */
440 }
441
442 rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
443 ComSafeArrayAsOutParam(metrics),
444 ComSafeArrayAsOutParam(baseMetrics),
445 ComSafeArrayAsOutParam(objects));
446 if (FAILED(rc))
447 return 1;
448
449
450 com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
451 CHECK_ERROR(performanceCollector,
452 SetupMetrics(ComSafeArrayAsInParam(baseMetrics),
453 ComSafeArrayAsInParam(objects), period, samples,
454 ComSafeArrayAsOutParam(affectedMetrics)));
455 if (listMatches)
456 listAffectedMetrics(aVirtualBox,
457 ComSafeArrayAsInParam(affectedMetrics));
458 if (!affectedMetrics.size())
459 return 1;
460
461 if (isDetached)
462 {
463 RTPrintf("Warning! The background process holding collected metrics will shutdown\n"
464 "in few seconds, discarding all collected data and parameters.\n");
465 return 0;
466 }
467
468#ifdef RT_OS_WINDOWS
469 SetConsoleCtrlHandler(ctrlHandler, true);
470#endif /* RT_OS_WINDOWS */
471
472 RTPrintf("Time stamp Object Metric Value\n");
473
474 while (g_fKeepGoing)
475 {
476 RTPrintf("------------ ---------- -------------------- --------------------\n");
477 RTThreadSleep(period * 1000); // Sleep for 'period' seconds
478 char ts[15];
479
480 getTimestamp(ts, sizeof(ts));
481 com::SafeArray<BSTR> retNames;
482 com::SafeIfaceArray<IUnknown> retObjects;
483 com::SafeArray<BSTR> retUnits;
484 com::SafeArray<ULONG> retScales;
485 com::SafeArray<ULONG> retSequenceNumbers;
486 com::SafeArray<ULONG> retIndices;
487 com::SafeArray<ULONG> retLengths;
488 com::SafeArray<LONG> retData;
489 CHECK_ERROR (performanceCollector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
490 ComSafeArrayAsInParam(objects),
491 ComSafeArrayAsOutParam(retNames),
492 ComSafeArrayAsOutParam(retObjects),
493 ComSafeArrayAsOutParam(retUnits),
494 ComSafeArrayAsOutParam(retScales),
495 ComSafeArrayAsOutParam(retSequenceNumbers),
496 ComSafeArrayAsOutParam(retIndices),
497 ComSafeArrayAsOutParam(retLengths),
498 ComSafeArrayAsOutParam(retData)) );
499 for (unsigned i = 0; i < retNames.size(); i++)
500 {
501 Bstr metricUnit(retUnits[i]);
502 Bstr metricName(retNames[i]);
503 RTPrintf("%-12s %-10ls %-20ls ", ts, getObjectName(aVirtualBox, retObjects[i]).raw(), metricName.raw());
504 const char *separator = "";
505 for (unsigned j = 0; j < retLengths[i]; j++)
506 {
507 if (retScales[i] == 1)
508 RTPrintf("%s%d %ls", separator, retData[retIndices[i] + j], metricUnit.raw());
509 else
510 RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[i] + j] / retScales[i],
511 (retData[retIndices[i] + j] * 100 / retScales[i]) % 100, metricUnit.raw());
512 separator = ", ";
513 }
514 RTPrintf("\n");
515 }
516 }
517
518#ifdef RT_OS_WINDOWS
519 SetConsoleCtrlHandler(ctrlHandler, false);
520#endif /* RT_OS_WINDOWS */
521
522 return 0;
523}
524
525int handleMetrics(HandlerArg *a)
526{
527 int rc;
528
529 /* at least one option: subcommand name */
530 if (a->argc < 1)
531 return errorSyntax(USAGE_METRICS, "Subcommand missing");
532
533 ComPtr<IPerformanceCollector> performanceCollector;
534 CHECK_ERROR(a->virtualBox, COMGETTER(PerformanceCollector)(performanceCollector.asOutParam()));
535
536 if (!strcmp(a->argv[0], "list"))
537 rc = handleMetricsList(a->argc, a->argv, a->virtualBox, performanceCollector);
538 else if (!strcmp(a->argv[0], "setup"))
539 rc = handleMetricsSetup(a->argc, a->argv, a->virtualBox, performanceCollector);
540 else if (!strcmp(a->argv[0], "query"))
541 rc = handleMetricsQuery(a->argc, a->argv, a->virtualBox, performanceCollector);
542 else if (!strcmp(a->argv[0], "collect"))
543 rc = handleMetricsCollect(a->argc, a->argv, a->virtualBox, performanceCollector);
544 else
545 return errorSyntax(USAGE_METRICS, "Invalid subcommand '%s'", a->argv[0]);
546
547 return rc;
548}
549
550#endif /* VBOX_ONLY_DOCS */
551
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