VirtualBox

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

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

VBoxManageMetrics.cpp: Trimmed down the include list.

  • 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 14731 2008-11-27 18:35:31Z vboxsync $ */
2/** @file
3 * VBoxManage - VirtualBox's command-line interface.
4 * Metrics handling code.
5 */
6
7/*
8 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
19 * Clara, CA 95054 USA or visit http://www.sun.com if you need
20 * additional information or have any questions.
21 */
22
23#ifndef VBOX_ONLY_DOCS
24
25/*******************************************************************************
26* Header Files *
27*******************************************************************************/
28#include <VBox/com/array.h>
29#include <VBox/com/ErrorInfo.h>
30#include <VBox/com/VirtualBox.h>
31
32#include <iprt/stream.h>
33#include <iprt/string.h>
34#include <iprt/time.h>
35#include <iprt/thread.h>
36#include <VBox/log.h>
37
38#include "VBoxManage.h"
39using namespace com;
40
41
42// funcs
43///////////////////////////////////////////////////////////////////////////////
44
45
46static char *toBaseMetricNames(const char *metricList)
47{
48 char *newList = (char*)RTMemAlloc(strlen(metricList) + 1);
49 int cSlashes = 0;
50 bool fSkip = false;
51 const char *src = metricList;
52 char c, *dst = newList;
53 while ((c = *src++))
54 if (c == ':')
55 fSkip = true;
56 else if (c == '/' && ++cSlashes == 2)
57 fSkip = true;
58 else if (c == ',')
59 {
60 fSkip = false;
61 cSlashes = 0;
62 *dst++ = c;
63 }
64 else
65 if (!fSkip)
66 *dst++ = c;
67 *dst = 0;
68 return newList;
69}
70
71static int parseFilterParameters(int argc, char *argv[],
72 ComPtr<IVirtualBox> aVirtualBox,
73 ComSafeArrayOut(BSTR, outMetrics),
74 ComSafeArrayOut(BSTR, outBaseMetrics),
75 ComSafeArrayOut(IUnknown *, outObjects))
76{
77 HRESULT rc = S_OK;
78 com::SafeArray<BSTR> retMetrics(1);
79 com::SafeArray<BSTR> retBaseMetrics(1);
80 com::SafeIfaceArray <IUnknown> retObjects;
81
82 Bstr metricNames, baseNames;
83
84 /* Metric list */
85 if (argc > 1)
86 {
87 metricNames = argv[1];
88 char *tmp = toBaseMetricNames(argv[1]);
89 if (!tmp)
90 return VERR_NO_MEMORY;
91 baseNames = tmp;
92 RTMemFree(tmp);
93 }
94 else
95 {
96 metricNames = L"*";
97 baseNames = L"*";
98 }
99 metricNames.cloneTo(&retMetrics[0]);
100 baseNames.cloneTo(&retBaseMetrics[0]);
101
102 /* Object name */
103 if (argc > 0 && strcmp(argv[0], "*"))
104 {
105 if (!strcmp(argv[0], "host"))
106 {
107 ComPtr<IHost> host;
108 CHECK_ERROR(aVirtualBox, COMGETTER(Host)(host.asOutParam()));
109 retObjects.reset(1);
110 host.queryInterfaceTo(&retObjects[0]);
111 }
112 else
113 {
114 ComPtr <IMachine> machine;
115 rc = aVirtualBox->FindMachine(Bstr(argv[0]), machine.asOutParam());
116 if (SUCCEEDED (rc))
117 {
118 retObjects.reset(1);
119 machine.queryInterfaceTo(&retObjects[0]);
120 }
121 else
122 {
123 errorArgument("Invalid machine name: '%s'", argv[0]);
124 return rc;
125 }
126 }
127
128 }
129
130 retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
131 retBaseMetrics.detachTo(ComSafeArrayOutArg(outBaseMetrics));
132 retObjects.detachTo(ComSafeArrayOutArg(outObjects));
133
134 return rc;
135}
136
137static Bstr getObjectName(ComPtr<IVirtualBox> aVirtualBox,
138 ComPtr<IUnknown> aObject)
139{
140 HRESULT rc;
141
142 ComPtr<IHost> host = aObject;
143 if (!host.isNull())
144 return Bstr("host");
145
146 ComPtr<IMachine> machine = aObject;
147 if (!machine.isNull())
148 {
149 Bstr name;
150 CHECK_ERROR(machine, COMGETTER(Name)(name.asOutParam()));
151 if (SUCCEEDED(rc))
152 return name;
153 }
154 return Bstr("unknown");
155}
156
157static void listAffectedMetrics(ComPtr<IVirtualBox> aVirtualBox,
158 ComSafeArrayIn(IPerformanceMetric*, aMetrics))
159{
160 HRESULT rc;
161 com::SafeIfaceArray<IPerformanceMetric> metrics(ComSafeArrayInArg(aMetrics));
162 if (metrics.size())
163 {
164 ComPtr<IUnknown> object;
165 Bstr metricName;
166 RTPrintf("The following metrics were modified:\n\n"
167 "Object Metric\n"
168 "---------- --------------------\n");
169 for (size_t i = 0; i < metrics.size(); i++)
170 {
171 CHECK_ERROR(metrics[i], COMGETTER(Object)(object.asOutParam()));
172 CHECK_ERROR(metrics[i], COMGETTER(MetricName)(metricName.asOutParam()));
173 RTPrintf("%-10ls %-20ls\n",
174 getObjectName(aVirtualBox, object).raw(), metricName.raw());
175 }
176 RTPrintf("\n");
177 }
178 else
179 {
180 RTPrintf("No metrics match the specified filter!\n");
181 }
182}
183
184/**
185 * list *
186 */
187static int handleMetricsList(int argc, char *argv[],
188 ComPtr<IVirtualBox> aVirtualBox,
189 ComPtr<IPerformanceCollector> performanceCollector)
190{
191 HRESULT rc;
192 com::SafeArray<BSTR> metrics;
193 com::SafeArray<BSTR> baseMetrics;
194 com::SafeIfaceArray<IUnknown> objects;
195
196 rc = parseFilterParameters(argc - 1, &argv[1], aVirtualBox,
197 ComSafeArrayAsOutParam(metrics),
198 ComSafeArrayAsOutParam(baseMetrics),
199 ComSafeArrayAsOutParam(objects));
200 if (FAILED(rc))
201 return 1;
202
203 com::SafeIfaceArray<IPerformanceMetric> metricInfo;
204
205 CHECK_ERROR(performanceCollector,
206 GetMetrics(ComSafeArrayAsInParam(metrics),
207 ComSafeArrayAsInParam(objects),
208 ComSafeArrayAsOutParam(metricInfo)));
209
210 ComPtr<IUnknown> object;
211 Bstr metricName, unit, description;
212 ULONG period, count;
213 LONG minimum, maximum;
214 RTPrintf(
215"Object Metric Unit Minimum Maximum Period Count Description\n"
216"---------- -------------------- ---- ---------- ---------- ---------- ---------- -----------\n");
217 for (size_t i = 0; i < metricInfo.size(); i++)
218 {
219 CHECK_ERROR(metricInfo[i], COMGETTER(Object)(object.asOutParam()));
220 CHECK_ERROR(metricInfo[i], COMGETTER(MetricName)(metricName.asOutParam()));
221 CHECK_ERROR(metricInfo[i], COMGETTER(Period)(&period));
222 CHECK_ERROR(metricInfo[i], COMGETTER(Count)(&count));
223 CHECK_ERROR(metricInfo[i], COMGETTER(MinimumValue)(&minimum));
224 CHECK_ERROR(metricInfo[i], COMGETTER(MaximumValue)(&maximum));
225 CHECK_ERROR(metricInfo[i], COMGETTER(Unit)(unit.asOutParam()));
226 CHECK_ERROR(metricInfo[i], COMGETTER(Description)(description.asOutParam()));
227 RTPrintf("%-10ls %-20ls %-4ls %10d %10d %10u %10u %ls\n",
228 getObjectName(aVirtualBox, object).raw(), metricName.raw(), unit.raw(),
229 minimum, maximum, period, count, description.raw());
230 }
231
232 return 0;
233}
234
235/**
236 * Metics setup
237 */
238static int handleMetricsSetup(int argc, char *argv[],
239 ComPtr<IVirtualBox> aVirtualBox,
240 ComPtr<IPerformanceCollector> performanceCollector)
241{
242 HRESULT rc;
243 com::SafeArray<BSTR> metrics;
244 com::SafeArray<BSTR> baseMetrics;
245 com::SafeIfaceArray<IUnknown> objects;
246 ULONG period = 1, samples = 1;
247 bool listMatches = false;
248 int i;
249
250 for (i = 1; i < argc; i++)
251 {
252 if (strcmp(argv[i], "-period") == 0)
253 {
254 if (argc <= i + 1)
255 return errorArgument("Missing argument to '%s'", argv[i]);
256 char *endptr = NULL;
257 period = strtoul (argv[++i], &endptr, 10);
258 if (!endptr || *endptr || !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 char *endptr = NULL;
266 samples = strtoul (argv[++i], &endptr, 10);
267 if (!endptr || *endptr)
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 ULONG 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 char *endptr = NULL;
424 period = strtoul (argv[++i], &endptr, 10);
425 if (!endptr || *endptr || !period)
426 return errorArgument("Invalid value for 'period' parameter: '%s'", argv[i]);
427 }
428 else if (strcmp(argv[i], "-samples") == 0)
429 {
430 if (argc <= i + 1)
431 return errorArgument("Missing argument to '%s'", argv[i]);
432 char *endptr = NULL;
433 samples = strtoul (argv[++i], &endptr, 10);
434 if (!endptr || *endptr || !samples)
435 return errorArgument("Invalid value for 'samples' parameter: '%s'", argv[i]);
436 }
437 else if (strcmp(argv[i], "-list") == 0)
438 listMatches = true;
439 else if (strcmp(argv[i], "-detach") == 0)
440 isDetached = true;
441 else
442 break; /* The rest of params should define the filter */
443 }
444
445 rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
446 ComSafeArrayAsOutParam(metrics),
447 ComSafeArrayAsOutParam(baseMetrics),
448 ComSafeArrayAsOutParam(objects));
449 if (FAILED(rc))
450 return 1;
451
452
453 com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
454 CHECK_ERROR(performanceCollector,
455 SetupMetrics(ComSafeArrayAsInParam(baseMetrics),
456 ComSafeArrayAsInParam(objects), period, samples,
457 ComSafeArrayAsOutParam(affectedMetrics)));
458 if (listMatches)
459 listAffectedMetrics(aVirtualBox,
460 ComSafeArrayAsInParam(affectedMetrics));
461 if (!affectedMetrics.size())
462 return 1;
463
464 if (isDetached)
465 {
466 RTPrintf("Warning! The background process holding collected metrics will shutdown\n"
467 "in few seconds, discarding all collected data and parameters.\n");
468 return 0;
469 }
470
471#ifdef RT_OS_WINDOWS
472 SetConsoleCtrlHandler(ctrlHandler, true);
473#endif /* RT_OS_WINDOWS */
474
475 RTPrintf("Time stamp Object Metric Value\n");
476
477 while (g_fKeepGoing)
478 {
479 RTPrintf("------------ ---------- -------------------- --------------------\n");
480 RTThreadSleep(period * 1000); // Sleep for 'period' seconds
481 char ts[15];
482
483 getTimestamp(ts, sizeof(ts));
484 com::SafeArray<BSTR> retNames;
485 com::SafeIfaceArray<IUnknown> retObjects;
486 com::SafeArray<BSTR> retUnits;
487 com::SafeArray<ULONG> retScales;
488 com::SafeArray<ULONG> retSequenceNumbers;
489 com::SafeArray<ULONG> retIndices;
490 com::SafeArray<ULONG> retLengths;
491 com::SafeArray<LONG> retData;
492 CHECK_ERROR (performanceCollector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
493 ComSafeArrayAsInParam(objects),
494 ComSafeArrayAsOutParam(retNames),
495 ComSafeArrayAsOutParam(retObjects),
496 ComSafeArrayAsOutParam(retUnits),
497 ComSafeArrayAsOutParam(retScales),
498 ComSafeArrayAsOutParam(retSequenceNumbers),
499 ComSafeArrayAsOutParam(retIndices),
500 ComSafeArrayAsOutParam(retLengths),
501 ComSafeArrayAsOutParam(retData)) );
502 for (unsigned i = 0; i < retNames.size(); i++)
503 {
504 Bstr metricUnit(retUnits[i]);
505 Bstr metricName(retNames[i]);
506 RTPrintf("%-12s %-10ls %-20ls ", ts, getObjectName(aVirtualBox, retObjects[i]).raw(), metricName.raw());
507 const char *separator = "";
508 for (unsigned j = 0; j < retLengths[i]; j++)
509 {
510 if (retScales[i] == 1)
511 RTPrintf("%s%d %ls", separator, retData[retIndices[i] + j], metricUnit.raw());
512 else
513 RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[i] + j] / retScales[i],
514 (retData[retIndices[i] + j] * 100 / retScales[i]) % 100, metricUnit.raw());
515 separator = ", ";
516 }
517 RTPrintf("\n");
518 }
519 }
520
521#ifdef RT_OS_WINDOWS
522 SetConsoleCtrlHandler(ctrlHandler, false);
523#endif /* RT_OS_WINDOWS */
524
525 return 0;
526}
527
528int handleMetrics(int argc, char *argv[],
529 ComPtr<IVirtualBox> aVirtualBox, ComPtr<ISession> aSession)
530{
531 int rc;
532
533 /* at least one option: subcommand name */
534 if (argc < 1)
535 return errorSyntax(USAGE_METRICS, "Subcommand missing");
536
537 ComPtr<IPerformanceCollector> performanceCollector;
538 CHECK_ERROR(aVirtualBox, COMGETTER(PerformanceCollector)(performanceCollector.asOutParam()));
539
540 if (!strcmp(argv[0], "list"))
541 rc = handleMetricsList(argc, argv, aVirtualBox, performanceCollector);
542 else if (!strcmp(argv[0], "setup"))
543 rc = handleMetricsSetup(argc, argv, aVirtualBox, performanceCollector);
544 else if (!strcmp(argv[0], "query"))
545 rc = handleMetricsQuery(argc, argv, aVirtualBox, performanceCollector);
546 else if (!strcmp(argv[0], "collect"))
547 rc = handleMetricsCollect(argc, argv, aVirtualBox, performanceCollector);
548 else
549 return errorSyntax(USAGE_METRICS, "Invalid subcommand '%s'", argv[0]);
550
551 return rc;
552}
553
554#endif /* VBOX_ONLY_DOCS */
555
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