VirtualBox

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

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

Main: rework error macros everywhere; make error messages much more readable (esp. with VBoxManage); use shared function to actually print message; reduces size of VBoxManage debug build from 3.4 to 2.3 MB

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