1 | /* $Id: VBoxManageMetrics.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - The 'metrics' command.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2009 Oracle Corporation
|
---|
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 |
|
---|
18 | #ifndef VBOX_ONLY_DOCS
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #include <VBox/com/com.h>
|
---|
24 | #include <VBox/com/array.h>
|
---|
25 | #include <VBox/com/ErrorInfo.h>
|
---|
26 | #include <VBox/com/errorprint.h>
|
---|
27 | #include <VBox/com/VirtualBox.h>
|
---|
28 |
|
---|
29 | #include <iprt/asm.h>
|
---|
30 | #include <iprt/stream.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <iprt/time.h>
|
---|
33 | #include <iprt/thread.h>
|
---|
34 | #include <VBox/log.h>
|
---|
35 |
|
---|
36 | #include "VBoxManage.h"
|
---|
37 | using namespace com;
|
---|
38 |
|
---|
39 |
|
---|
40 | // funcs
|
---|
41 | ///////////////////////////////////////////////////////////////////////////////
|
---|
42 |
|
---|
43 |
|
---|
44 | static char *toBaseMetricNames(const char *metricList)
|
---|
45 | {
|
---|
46 | char *newList = (char*)RTMemAlloc(strlen(metricList) + 1);
|
---|
47 | int cSlashes = 0;
|
---|
48 | bool fSkip = false;
|
---|
49 | const char *src = metricList;
|
---|
50 | char c, *dst = newList;
|
---|
51 | while ((c = *src++))
|
---|
52 | if (c == ':')
|
---|
53 | fSkip = true;
|
---|
54 | else if (c == '/' && ++cSlashes == 2)
|
---|
55 | fSkip = true;
|
---|
56 | else if (c == ',')
|
---|
57 | {
|
---|
58 | fSkip = false;
|
---|
59 | cSlashes = 0;
|
---|
60 | *dst++ = c;
|
---|
61 | }
|
---|
62 | else
|
---|
63 | if (!fSkip)
|
---|
64 | *dst++ = c;
|
---|
65 | *dst = 0;
|
---|
66 | return newList;
|
---|
67 | }
|
---|
68 |
|
---|
69 | static int parseFilterParameters(int argc, char *argv[],
|
---|
70 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
71 | ComSafeArrayOut(BSTR, outMetrics),
|
---|
72 | ComSafeArrayOut(BSTR, outBaseMetrics),
|
---|
73 | ComSafeArrayOut(IUnknown *, outObjects))
|
---|
74 | {
|
---|
75 | HRESULT rc = S_OK;
|
---|
76 | com::SafeArray<BSTR> retMetrics(1);
|
---|
77 | com::SafeArray<BSTR> retBaseMetrics(1);
|
---|
78 | com::SafeIfaceArray <IUnknown> retObjects;
|
---|
79 |
|
---|
80 | Bstr metricNames, baseNames;
|
---|
81 |
|
---|
82 | /* Metric list */
|
---|
83 | if (argc > 1)
|
---|
84 | {
|
---|
85 | metricNames = argv[1];
|
---|
86 | char *tmp = toBaseMetricNames(argv[1]);
|
---|
87 | if (!tmp)
|
---|
88 | return VERR_NO_MEMORY;
|
---|
89 | baseNames = tmp;
|
---|
90 | RTMemFree(tmp);
|
---|
91 | }
|
---|
92 | else
|
---|
93 | {
|
---|
94 | metricNames = L"*";
|
---|
95 | baseNames = L"*";
|
---|
96 | }
|
---|
97 | metricNames.cloneTo(&retMetrics[0]);
|
---|
98 | baseNames.cloneTo(&retBaseMetrics[0]);
|
---|
99 |
|
---|
100 | /* Object name */
|
---|
101 | if (argc > 0 && strcmp(argv[0], "*"))
|
---|
102 | {
|
---|
103 | if (!strcmp(argv[0], "host"))
|
---|
104 | {
|
---|
105 | ComPtr<IHost> host;
|
---|
106 | CHECK_ERROR(aVirtualBox, COMGETTER(Host)(host.asOutParam()));
|
---|
107 | retObjects.reset(1);
|
---|
108 | host.queryInterfaceTo(&retObjects[0]);
|
---|
109 | }
|
---|
110 | else
|
---|
111 | {
|
---|
112 | ComPtr <IMachine> machine;
|
---|
113 | rc = aVirtualBox->FindMachine(Bstr(argv[0]), machine.asOutParam());
|
---|
114 | if (SUCCEEDED (rc))
|
---|
115 | {
|
---|
116 | retObjects.reset(1);
|
---|
117 | machine.queryInterfaceTo(&retObjects[0]);
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | errorArgument("Invalid machine name: '%s'", argv[0]);
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|
127 |
|
---|
128 | retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
|
---|
129 | retBaseMetrics.detachTo(ComSafeArrayOutArg(outBaseMetrics));
|
---|
130 | retObjects.detachTo(ComSafeArrayOutArg(outObjects));
|
---|
131 |
|
---|
132 | return rc;
|
---|
133 | }
|
---|
134 |
|
---|
135 | static Bstr getObjectName(ComPtr<IVirtualBox> aVirtualBox,
|
---|
136 | ComPtr<IUnknown> aObject)
|
---|
137 | {
|
---|
138 | HRESULT rc;
|
---|
139 |
|
---|
140 | ComPtr<IHost> host = aObject;
|
---|
141 | if (!host.isNull())
|
---|
142 | return Bstr("host");
|
---|
143 |
|
---|
144 | ComPtr<IMachine> machine = aObject;
|
---|
145 | if (!machine.isNull())
|
---|
146 | {
|
---|
147 | Bstr name;
|
---|
148 | CHECK_ERROR(machine, COMGETTER(Name)(name.asOutParam()));
|
---|
149 | if (SUCCEEDED(rc))
|
---|
150 | return name;
|
---|
151 | }
|
---|
152 | return Bstr("unknown");
|
---|
153 | }
|
---|
154 |
|
---|
155 | static void listAffectedMetrics(ComPtr<IVirtualBox> aVirtualBox,
|
---|
156 | ComSafeArrayIn(IPerformanceMetric*, aMetrics))
|
---|
157 | {
|
---|
158 | HRESULT rc;
|
---|
159 | com::SafeIfaceArray<IPerformanceMetric> metrics(ComSafeArrayInArg(aMetrics));
|
---|
160 | if (metrics.size())
|
---|
161 | {
|
---|
162 | ComPtr<IUnknown> object;
|
---|
163 | Bstr metricName;
|
---|
164 | RTPrintf("The following metrics were modified:\n\n"
|
---|
165 | "Object Metric\n"
|
---|
166 | "---------- --------------------\n");
|
---|
167 | for (size_t i = 0; i < metrics.size(); i++)
|
---|
168 | {
|
---|
169 | CHECK_ERROR(metrics[i], COMGETTER(Object)(object.asOutParam()));
|
---|
170 | CHECK_ERROR(metrics[i], COMGETTER(MetricName)(metricName.asOutParam()));
|
---|
171 | RTPrintf("%-10ls %-20ls\n",
|
---|
172 | getObjectName(aVirtualBox, object).raw(), metricName.raw());
|
---|
173 | }
|
---|
174 | RTPrintf("\n");
|
---|
175 | }
|
---|
176 | else
|
---|
177 | {
|
---|
178 | RTPrintf("No metrics match the specified filter!\n");
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * list *
|
---|
184 | */
|
---|
185 | static int handleMetricsList(int argc, char *argv[],
|
---|
186 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
187 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
188 | {
|
---|
189 | HRESULT rc;
|
---|
190 | com::SafeArray<BSTR> metrics;
|
---|
191 | com::SafeArray<BSTR> baseMetrics;
|
---|
192 | com::SafeIfaceArray<IUnknown> objects;
|
---|
193 |
|
---|
194 | rc = parseFilterParameters(argc - 1, &argv[1], aVirtualBox,
|
---|
195 | ComSafeArrayAsOutParam(metrics),
|
---|
196 | ComSafeArrayAsOutParam(baseMetrics),
|
---|
197 | ComSafeArrayAsOutParam(objects));
|
---|
198 | if (FAILED(rc))
|
---|
199 | return 1;
|
---|
200 |
|
---|
201 | com::SafeIfaceArray<IPerformanceMetric> metricInfo;
|
---|
202 |
|
---|
203 | CHECK_ERROR(performanceCollector,
|
---|
204 | GetMetrics(ComSafeArrayAsInParam(metrics),
|
---|
205 | ComSafeArrayAsInParam(objects),
|
---|
206 | ComSafeArrayAsOutParam(metricInfo)));
|
---|
207 |
|
---|
208 | ComPtr<IUnknown> object;
|
---|
209 | Bstr metricName, unit, description;
|
---|
210 | ULONG period, count;
|
---|
211 | LONG minimum, maximum;
|
---|
212 | RTPrintf(
|
---|
213 | "Object Metric Unit Minimum Maximum Period Count Description\n"
|
---|
214 | "---------- -------------------- ---- ---------- ---------- ---------- ---------- -----------\n");
|
---|
215 | for (size_t i = 0; i < metricInfo.size(); i++)
|
---|
216 | {
|
---|
217 | CHECK_ERROR(metricInfo[i], COMGETTER(Object)(object.asOutParam()));
|
---|
218 | CHECK_ERROR(metricInfo[i], COMGETTER(MetricName)(metricName.asOutParam()));
|
---|
219 | CHECK_ERROR(metricInfo[i], COMGETTER(Period)(&period));
|
---|
220 | CHECK_ERROR(metricInfo[i], COMGETTER(Count)(&count));
|
---|
221 | CHECK_ERROR(metricInfo[i], COMGETTER(MinimumValue)(&minimum));
|
---|
222 | CHECK_ERROR(metricInfo[i], COMGETTER(MaximumValue)(&maximum));
|
---|
223 | CHECK_ERROR(metricInfo[i], COMGETTER(Unit)(unit.asOutParam()));
|
---|
224 | CHECK_ERROR(metricInfo[i], COMGETTER(Description)(description.asOutParam()));
|
---|
225 | RTPrintf("%-10ls %-20ls %-4ls %10d %10d %10u %10u %ls\n",
|
---|
226 | getObjectName(aVirtualBox, object).raw(), metricName.raw(), unit.raw(),
|
---|
227 | minimum, maximum, period, count, description.raw());
|
---|
228 | }
|
---|
229 |
|
---|
230 | return 0;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Metics setup
|
---|
235 | */
|
---|
236 | static int handleMetricsSetup(int argc, char *argv[],
|
---|
237 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
238 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
239 | {
|
---|
240 | HRESULT rc;
|
---|
241 | com::SafeArray<BSTR> metrics;
|
---|
242 | com::SafeArray<BSTR> baseMetrics;
|
---|
243 | com::SafeIfaceArray<IUnknown> objects;
|
---|
244 | uint32_t period = 1, samples = 1;
|
---|
245 | bool listMatches = false;
|
---|
246 | int i;
|
---|
247 |
|
---|
248 | for (i = 1; i < argc; i++)
|
---|
249 | {
|
---|
250 | if ( !strcmp(argv[i], "--period")
|
---|
251 | || !strcmp(argv[i], "-period"))
|
---|
252 | {
|
---|
253 | if (argc <= i + 1)
|
---|
254 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
255 | if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &period)
|
---|
256 | || !period)
|
---|
257 | return errorArgument("Invalid value for 'period' parameter: '%s'", argv[i]);
|
---|
258 | }
|
---|
259 | else if ( !strcmp(argv[i], "--samples")
|
---|
260 | || !strcmp(argv[i], "-samples"))
|
---|
261 | {
|
---|
262 | if (argc <= i + 1)
|
---|
263 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
264 | if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &samples)
|
---|
265 | || !samples)
|
---|
266 | return errorArgument("Invalid value for 'samples' parameter: '%s'", argv[i]);
|
---|
267 | }
|
---|
268 | else if ( !strcmp(argv[i], "--list")
|
---|
269 | || !strcmp(argv[i], "-list"))
|
---|
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 | */
|
---|
297 | static 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 |
|
---|
355 | static 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. */
|
---|
373 | static 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 | */
|
---|
385 | static 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 | */
|
---|
405 | static 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")
|
---|
419 | || !strcmp(argv[i], "-period"))
|
---|
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")
|
---|
428 | || !strcmp(argv[i], "-samples"))
|
---|
429 | {
|
---|
430 | if (argc <= i + 1)
|
---|
431 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
432 | if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &samples)
|
---|
433 | || !samples)
|
---|
434 | return errorArgument("Invalid value for 'samples' parameter: '%s'", argv[i]);
|
---|
435 | }
|
---|
436 | else if ( !strcmp(argv[i], "--list")
|
---|
437 | || !strcmp(argv[i], "-list"))
|
---|
438 | listMatches = true;
|
---|
439 | else if ( !strcmp(argv[i], "--detach")
|
---|
440 | || !strcmp(argv[i], "-detach"))
|
---|
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 j = 0; j < retNames.size(); j++)
|
---|
504 | {
|
---|
505 | Bstr metricUnit(retUnits[j]);
|
---|
506 | Bstr metricName(retNames[j]);
|
---|
507 | RTPrintf("%-12s %-10ls %-20ls ", ts, getObjectName(aVirtualBox, retObjects[j]).raw(), metricName.raw());
|
---|
508 | const char *separator = "";
|
---|
509 | for (unsigned k = 0; k < retLengths[j]; k++)
|
---|
510 | {
|
---|
511 | if (retScales[j] == 1)
|
---|
512 | RTPrintf("%s%d %ls", separator, retData[retIndices[j] + k], metricUnit.raw());
|
---|
513 | else
|
---|
514 | RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[j] + k] / retScales[j],
|
---|
515 | (retData[retIndices[j] + k] * 100 / retScales[j]) % 100, metricUnit.raw());
|
---|
516 | separator = ", ";
|
---|
517 | }
|
---|
518 | RTPrintf("\n");
|
---|
519 | }
|
---|
520 | RTStrmFlush(g_pStdOut);
|
---|
521 | }
|
---|
522 |
|
---|
523 | #ifdef RT_OS_WINDOWS
|
---|
524 | SetConsoleCtrlHandler(ctrlHandler, false);
|
---|
525 | #endif /* RT_OS_WINDOWS */
|
---|
526 |
|
---|
527 | return 0;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Enable metrics
|
---|
532 | */
|
---|
533 | static int handleMetricsEnable(int argc, char *argv[],
|
---|
534 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
535 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
536 | {
|
---|
537 | HRESULT rc;
|
---|
538 | com::SafeArray<BSTR> metrics;
|
---|
539 | com::SafeArray<BSTR> baseMetrics;
|
---|
540 | com::SafeIfaceArray<IUnknown> objects;
|
---|
541 | bool listMatches = false;
|
---|
542 | int i;
|
---|
543 |
|
---|
544 | for (i = 1; i < argc; i++)
|
---|
545 | {
|
---|
546 | if ( !strcmp(argv[i], "--list")
|
---|
547 | || !strcmp(argv[i], "-list"))
|
---|
548 | listMatches = true;
|
---|
549 | else
|
---|
550 | break; /* The rest of params should define the filter */
|
---|
551 | }
|
---|
552 |
|
---|
553 | rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
|
---|
554 | ComSafeArrayAsOutParam(metrics),
|
---|
555 | ComSafeArrayAsOutParam(baseMetrics),
|
---|
556 | ComSafeArrayAsOutParam(objects));
|
---|
557 | if (FAILED(rc))
|
---|
558 | return 1;
|
---|
559 |
|
---|
560 | com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
|
---|
561 | CHECK_ERROR(performanceCollector,
|
---|
562 | EnableMetrics(ComSafeArrayAsInParam(metrics),
|
---|
563 | ComSafeArrayAsInParam(objects),
|
---|
564 | ComSafeArrayAsOutParam(affectedMetrics)));
|
---|
565 | if (listMatches)
|
---|
566 | listAffectedMetrics(aVirtualBox,
|
---|
567 | ComSafeArrayAsInParam(affectedMetrics));
|
---|
568 |
|
---|
569 | return 0;
|
---|
570 | }
|
---|
571 |
|
---|
572 | /**
|
---|
573 | * Disable metrics
|
---|
574 | */
|
---|
575 | static int handleMetricsDisable(int argc, char *argv[],
|
---|
576 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
577 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
578 | {
|
---|
579 | HRESULT rc;
|
---|
580 | com::SafeArray<BSTR> metrics;
|
---|
581 | com::SafeArray<BSTR> baseMetrics;
|
---|
582 | com::SafeIfaceArray<IUnknown> objects;
|
---|
583 | bool listMatches = false;
|
---|
584 | int i;
|
---|
585 |
|
---|
586 | for (i = 1; i < argc; i++)
|
---|
587 | {
|
---|
588 | if ( !strcmp(argv[i], "--list")
|
---|
589 | || !strcmp(argv[i], "-list"))
|
---|
590 | listMatches = true;
|
---|
591 | else
|
---|
592 | break; /* The rest of params should define the filter */
|
---|
593 | }
|
---|
594 |
|
---|
595 | rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
|
---|
596 | ComSafeArrayAsOutParam(metrics),
|
---|
597 | ComSafeArrayAsOutParam(baseMetrics),
|
---|
598 | ComSafeArrayAsOutParam(objects));
|
---|
599 | if (FAILED(rc))
|
---|
600 | return 1;
|
---|
601 |
|
---|
602 | com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
|
---|
603 | CHECK_ERROR(performanceCollector,
|
---|
604 | DisableMetrics(ComSafeArrayAsInParam(metrics),
|
---|
605 | ComSafeArrayAsInParam(objects),
|
---|
606 | ComSafeArrayAsOutParam(affectedMetrics)));
|
---|
607 | if (listMatches)
|
---|
608 | listAffectedMetrics(aVirtualBox,
|
---|
609 | ComSafeArrayAsInParam(affectedMetrics));
|
---|
610 |
|
---|
611 | return 0;
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | int handleMetrics(HandlerArg *a)
|
---|
616 | {
|
---|
617 | int rc;
|
---|
618 |
|
---|
619 | /* at least one option: subcommand name */
|
---|
620 | if (a->argc < 1)
|
---|
621 | return errorSyntax(USAGE_METRICS, "Subcommand missing");
|
---|
622 |
|
---|
623 | ComPtr<IPerformanceCollector> performanceCollector;
|
---|
624 | CHECK_ERROR(a->virtualBox, COMGETTER(PerformanceCollector)(performanceCollector.asOutParam()));
|
---|
625 |
|
---|
626 | if (!strcmp(a->argv[0], "list"))
|
---|
627 | rc = handleMetricsList(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
628 | else if (!strcmp(a->argv[0], "setup"))
|
---|
629 | rc = handleMetricsSetup(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
630 | else if (!strcmp(a->argv[0], "query"))
|
---|
631 | rc = handleMetricsQuery(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
632 | else if (!strcmp(a->argv[0], "collect"))
|
---|
633 | rc = handleMetricsCollect(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
634 | else if (!strcmp(a->argv[0], "enable"))
|
---|
635 | rc = handleMetricsEnable(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
636 | else if (!strcmp(a->argv[0], "disable"))
|
---|
637 | rc = handleMetricsDisable(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
638 | else
|
---|
639 | return errorSyntax(USAGE_METRICS, "Invalid subcommand '%s'", a->argv[0]);
|
---|
640 |
|
---|
641 | return rc;
|
---|
642 | }
|
---|
643 |
|
---|
644 | #endif /* VBOX_ONLY_DOCS */
|
---|
645 |
|
---|