1 | /* $Id: VBoxManageMetrics.cpp 32718 2010-09-23 12:57:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - The 'metrics' command.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 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]).raw(),
|
---|
114 | machine.asOutParam());
|
---|
115 | if (SUCCEEDED (rc))
|
---|
116 | {
|
---|
117 | retObjects.reset(1);
|
---|
118 | machine.queryInterfaceTo(&retObjects[0]);
|
---|
119 | }
|
---|
120 | else
|
---|
121 | {
|
---|
122 | errorArgument("Invalid machine name: '%s'", argv[0]);
|
---|
123 | return rc;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | }
|
---|
128 |
|
---|
129 | retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
|
---|
130 | retBaseMetrics.detachTo(ComSafeArrayOutArg(outBaseMetrics));
|
---|
131 | retObjects.detachTo(ComSafeArrayOutArg(outObjects));
|
---|
132 |
|
---|
133 | return rc;
|
---|
134 | }
|
---|
135 |
|
---|
136 | static Bstr getObjectName(ComPtr<IVirtualBox> aVirtualBox,
|
---|
137 | ComPtr<IUnknown> aObject)
|
---|
138 | {
|
---|
139 | HRESULT rc;
|
---|
140 |
|
---|
141 | ComPtr<IHost> host = aObject;
|
---|
142 | if (!host.isNull())
|
---|
143 | return Bstr("host");
|
---|
144 |
|
---|
145 | ComPtr<IMachine> machine = aObject;
|
---|
146 | if (!machine.isNull())
|
---|
147 | {
|
---|
148 | Bstr name;
|
---|
149 | CHECK_ERROR(machine, COMGETTER(Name)(name.asOutParam()));
|
---|
150 | if (SUCCEEDED(rc))
|
---|
151 | return name;
|
---|
152 | }
|
---|
153 | return Bstr("unknown");
|
---|
154 | }
|
---|
155 |
|
---|
156 | static void listAffectedMetrics(ComPtr<IVirtualBox> aVirtualBox,
|
---|
157 | ComSafeArrayIn(IPerformanceMetric*, aMetrics))
|
---|
158 | {
|
---|
159 | HRESULT rc;
|
---|
160 | com::SafeIfaceArray<IPerformanceMetric> metrics(ComSafeArrayInArg(aMetrics));
|
---|
161 | if (metrics.size())
|
---|
162 | {
|
---|
163 | ComPtr<IUnknown> object;
|
---|
164 | Bstr metricName;
|
---|
165 | RTPrintf("The following metrics were modified:\n\n"
|
---|
166 | "Object Metric\n"
|
---|
167 | "---------- --------------------\n");
|
---|
168 | for (size_t i = 0; i < metrics.size(); i++)
|
---|
169 | {
|
---|
170 | CHECK_ERROR(metrics[i], COMGETTER(Object)(object.asOutParam()));
|
---|
171 | CHECK_ERROR(metrics[i], COMGETTER(MetricName)(metricName.asOutParam()));
|
---|
172 | RTPrintf("%-10ls %-20ls\n",
|
---|
173 | getObjectName(aVirtualBox, object).raw(), metricName.raw());
|
---|
174 | }
|
---|
175 | RTPrintf("\n");
|
---|
176 | }
|
---|
177 | else
|
---|
178 | {
|
---|
179 | RTMsgError("No metrics match the specified filter!");
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * list
|
---|
185 | */
|
---|
186 | static int handleMetricsList(int argc, char *argv[],
|
---|
187 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
188 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
189 | {
|
---|
190 | HRESULT rc;
|
---|
191 | com::SafeArray<BSTR> metrics;
|
---|
192 | com::SafeArray<BSTR> baseMetrics;
|
---|
193 | com::SafeIfaceArray<IUnknown> objects;
|
---|
194 |
|
---|
195 | rc = parseFilterParameters(argc - 1, &argv[1], aVirtualBox,
|
---|
196 | ComSafeArrayAsOutParam(metrics),
|
---|
197 | ComSafeArrayAsOutParam(baseMetrics),
|
---|
198 | ComSafeArrayAsOutParam(objects));
|
---|
199 | if (FAILED(rc))
|
---|
200 | return 1;
|
---|
201 |
|
---|
202 | com::SafeIfaceArray<IPerformanceMetric> metricInfo;
|
---|
203 |
|
---|
204 | CHECK_ERROR(performanceCollector,
|
---|
205 | GetMetrics(ComSafeArrayAsInParam(metrics),
|
---|
206 | ComSafeArrayAsInParam(objects),
|
---|
207 | ComSafeArrayAsOutParam(metricInfo)));
|
---|
208 |
|
---|
209 | ComPtr<IUnknown> object;
|
---|
210 | Bstr metricName, unit, description;
|
---|
211 | ULONG period, count;
|
---|
212 | LONG minimum, maximum;
|
---|
213 | RTPrintf(
|
---|
214 | "Object Metric Unit Minimum Maximum Period Count Description\n"
|
---|
215 | "---------- -------------------- ---- ---------- ---------- ---------- ---------- -----------\n");
|
---|
216 | for (size_t i = 0; i < metricInfo.size(); i++)
|
---|
217 | {
|
---|
218 | CHECK_ERROR(metricInfo[i], COMGETTER(Object)(object.asOutParam()));
|
---|
219 | CHECK_ERROR(metricInfo[i], COMGETTER(MetricName)(metricName.asOutParam()));
|
---|
220 | CHECK_ERROR(metricInfo[i], COMGETTER(Period)(&period));
|
---|
221 | CHECK_ERROR(metricInfo[i], COMGETTER(Count)(&count));
|
---|
222 | CHECK_ERROR(metricInfo[i], COMGETTER(MinimumValue)(&minimum));
|
---|
223 | CHECK_ERROR(metricInfo[i], COMGETTER(MaximumValue)(&maximum));
|
---|
224 | CHECK_ERROR(metricInfo[i], COMGETTER(Unit)(unit.asOutParam()));
|
---|
225 | CHECK_ERROR(metricInfo[i], COMGETTER(Description)(description.asOutParam()));
|
---|
226 | RTPrintf("%-10ls %-20ls %-4ls %10d %10d %10u %10u %ls\n",
|
---|
227 | getObjectName(aVirtualBox, object).raw(), metricName.raw(), unit.raw(),
|
---|
228 | minimum, maximum, period, count, description.raw());
|
---|
229 | }
|
---|
230 |
|
---|
231 | return 0;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Metics setup
|
---|
236 | */
|
---|
237 | static int handleMetricsSetup(int argc, char *argv[],
|
---|
238 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
239 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
240 | {
|
---|
241 | HRESULT rc;
|
---|
242 | com::SafeArray<BSTR> metrics;
|
---|
243 | com::SafeArray<BSTR> baseMetrics;
|
---|
244 | com::SafeIfaceArray<IUnknown> objects;
|
---|
245 | uint32_t period = 1, samples = 1;
|
---|
246 | bool listMatches = false;
|
---|
247 | int i;
|
---|
248 |
|
---|
249 | for (i = 1; i < argc; i++)
|
---|
250 | {
|
---|
251 | if ( !strcmp(argv[i], "--period")
|
---|
252 | || !strcmp(argv[i], "-period"))
|
---|
253 | {
|
---|
254 | if (argc <= i + 1)
|
---|
255 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
256 | if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &period)
|
---|
257 | || !period)
|
---|
258 | return errorArgument("Invalid value for 'period' parameter: '%s'", argv[i]);
|
---|
259 | }
|
---|
260 | else if ( !strcmp(argv[i], "--samples")
|
---|
261 | || !strcmp(argv[i], "-samples"))
|
---|
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")
|
---|
270 | || !strcmp(argv[i], "-list"))
|
---|
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 | */
|
---|
298 | static 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 |
|
---|
356 | static 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. */
|
---|
374 | static 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 | */
|
---|
386 | static 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 | */
|
---|
406 | static 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")
|
---|
420 | || !strcmp(argv[i], "-period"))
|
---|
421 | {
|
---|
422 | if (argc <= i + 1)
|
---|
423 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
424 | if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &period)
|
---|
425 | || !period)
|
---|
426 | return errorArgument("Invalid value for 'period' parameter: '%s'", argv[i]);
|
---|
427 | }
|
---|
428 | else if ( !strcmp(argv[i], "--samples")
|
---|
429 | || !strcmp(argv[i], "-samples"))
|
---|
430 | {
|
---|
431 | if (argc <= i + 1)
|
---|
432 | return errorArgument("Missing argument to '%s'", argv[i]);
|
---|
433 | if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &samples)
|
---|
434 | || !samples)
|
---|
435 | return errorArgument("Invalid value for 'samples' parameter: '%s'", argv[i]);
|
---|
436 | }
|
---|
437 | else if ( !strcmp(argv[i], "--list")
|
---|
438 | || !strcmp(argv[i], "-list"))
|
---|
439 | listMatches = true;
|
---|
440 | else if ( !strcmp(argv[i], "--detach")
|
---|
441 | || !strcmp(argv[i], "-detach"))
|
---|
442 | isDetached = true;
|
---|
443 | else
|
---|
444 | break; /* The rest of params should define the filter */
|
---|
445 | }
|
---|
446 |
|
---|
447 | rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
|
---|
448 | ComSafeArrayAsOutParam(metrics),
|
---|
449 | ComSafeArrayAsOutParam(baseMetrics),
|
---|
450 | ComSafeArrayAsOutParam(objects));
|
---|
451 | if (FAILED(rc))
|
---|
452 | return 1;
|
---|
453 |
|
---|
454 |
|
---|
455 | com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
|
---|
456 | CHECK_ERROR(performanceCollector,
|
---|
457 | SetupMetrics(ComSafeArrayAsInParam(baseMetrics),
|
---|
458 | ComSafeArrayAsInParam(objects), period, samples,
|
---|
459 | ComSafeArrayAsOutParam(affectedMetrics)));
|
---|
460 | if (listMatches)
|
---|
461 | listAffectedMetrics(aVirtualBox,
|
---|
462 | ComSafeArrayAsInParam(affectedMetrics));
|
---|
463 | if (!affectedMetrics.size())
|
---|
464 | return 1;
|
---|
465 |
|
---|
466 | if (isDetached)
|
---|
467 | {
|
---|
468 | RTMsgWarning("The background process holding collected metrics will shutdown\n"
|
---|
469 | "in few seconds, discarding all collected data and parameters.");
|
---|
470 | return 0;
|
---|
471 | }
|
---|
472 |
|
---|
473 | #ifdef RT_OS_WINDOWS
|
---|
474 | SetConsoleCtrlHandler(ctrlHandler, true);
|
---|
475 | #endif /* RT_OS_WINDOWS */
|
---|
476 |
|
---|
477 | RTPrintf("Time stamp Object Metric Value\n");
|
---|
478 |
|
---|
479 | while (g_fKeepGoing)
|
---|
480 | {
|
---|
481 | RTPrintf("------------ ---------- -------------------- --------------------\n");
|
---|
482 | RTThreadSleep(period * 1000); // Sleep for 'period' seconds
|
---|
483 | char ts[15];
|
---|
484 |
|
---|
485 | getTimestamp(ts, sizeof(ts));
|
---|
486 | com::SafeArray<BSTR> retNames;
|
---|
487 | com::SafeIfaceArray<IUnknown> retObjects;
|
---|
488 | com::SafeArray<BSTR> retUnits;
|
---|
489 | com::SafeArray<ULONG> retScales;
|
---|
490 | com::SafeArray<ULONG> retSequenceNumbers;
|
---|
491 | com::SafeArray<ULONG> retIndices;
|
---|
492 | com::SafeArray<ULONG> retLengths;
|
---|
493 | com::SafeArray<LONG> retData;
|
---|
494 | CHECK_ERROR (performanceCollector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
|
---|
495 | ComSafeArrayAsInParam(objects),
|
---|
496 | ComSafeArrayAsOutParam(retNames),
|
---|
497 | ComSafeArrayAsOutParam(retObjects),
|
---|
498 | ComSafeArrayAsOutParam(retUnits),
|
---|
499 | ComSafeArrayAsOutParam(retScales),
|
---|
500 | ComSafeArrayAsOutParam(retSequenceNumbers),
|
---|
501 | ComSafeArrayAsOutParam(retIndices),
|
---|
502 | ComSafeArrayAsOutParam(retLengths),
|
---|
503 | ComSafeArrayAsOutParam(retData)) );
|
---|
504 | for (unsigned j = 0; j < retNames.size(); j++)
|
---|
505 | {
|
---|
506 | Bstr metricUnit(retUnits[j]);
|
---|
507 | Bstr metricName(retNames[j]);
|
---|
508 | RTPrintf("%-12s %-10ls %-20ls ", ts, getObjectName(aVirtualBox, retObjects[j]).raw(), metricName.raw());
|
---|
509 | const char *separator = "";
|
---|
510 | for (unsigned k = 0; k < retLengths[j]; k++)
|
---|
511 | {
|
---|
512 | if (retScales[j] == 1)
|
---|
513 | RTPrintf("%s%d %ls", separator, retData[retIndices[j] + k], metricUnit.raw());
|
---|
514 | else
|
---|
515 | RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[j] + k] / retScales[j],
|
---|
516 | (retData[retIndices[j] + k] * 100 / retScales[j]) % 100, metricUnit.raw());
|
---|
517 | separator = ", ";
|
---|
518 | }
|
---|
519 | RTPrintf("\n");
|
---|
520 | }
|
---|
521 | RTStrmFlush(g_pStdOut);
|
---|
522 | }
|
---|
523 |
|
---|
524 | #ifdef RT_OS_WINDOWS
|
---|
525 | SetConsoleCtrlHandler(ctrlHandler, false);
|
---|
526 | #endif /* RT_OS_WINDOWS */
|
---|
527 |
|
---|
528 | return 0;
|
---|
529 | }
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Enable metrics
|
---|
533 | */
|
---|
534 | static int handleMetricsEnable(int argc, char *argv[],
|
---|
535 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
536 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
537 | {
|
---|
538 | HRESULT rc;
|
---|
539 | com::SafeArray<BSTR> metrics;
|
---|
540 | com::SafeArray<BSTR> baseMetrics;
|
---|
541 | com::SafeIfaceArray<IUnknown> objects;
|
---|
542 | bool listMatches = false;
|
---|
543 | int i;
|
---|
544 |
|
---|
545 | for (i = 1; i < argc; i++)
|
---|
546 | {
|
---|
547 | if ( !strcmp(argv[i], "--list")
|
---|
548 | || !strcmp(argv[i], "-list"))
|
---|
549 | listMatches = true;
|
---|
550 | else
|
---|
551 | break; /* The rest of params should define the filter */
|
---|
552 | }
|
---|
553 |
|
---|
554 | rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
|
---|
555 | ComSafeArrayAsOutParam(metrics),
|
---|
556 | ComSafeArrayAsOutParam(baseMetrics),
|
---|
557 | ComSafeArrayAsOutParam(objects));
|
---|
558 | if (FAILED(rc))
|
---|
559 | return 1;
|
---|
560 |
|
---|
561 | com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
|
---|
562 | CHECK_ERROR(performanceCollector,
|
---|
563 | EnableMetrics(ComSafeArrayAsInParam(metrics),
|
---|
564 | ComSafeArrayAsInParam(objects),
|
---|
565 | ComSafeArrayAsOutParam(affectedMetrics)));
|
---|
566 | if (listMatches)
|
---|
567 | listAffectedMetrics(aVirtualBox,
|
---|
568 | ComSafeArrayAsInParam(affectedMetrics));
|
---|
569 |
|
---|
570 | return 0;
|
---|
571 | }
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * Disable metrics
|
---|
575 | */
|
---|
576 | static int handleMetricsDisable(int argc, char *argv[],
|
---|
577 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
578 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
579 | {
|
---|
580 | HRESULT rc;
|
---|
581 | com::SafeArray<BSTR> metrics;
|
---|
582 | com::SafeArray<BSTR> baseMetrics;
|
---|
583 | com::SafeIfaceArray<IUnknown> objects;
|
---|
584 | bool listMatches = false;
|
---|
585 | int i;
|
---|
586 |
|
---|
587 | for (i = 1; i < argc; i++)
|
---|
588 | {
|
---|
589 | if ( !strcmp(argv[i], "--list")
|
---|
590 | || !strcmp(argv[i], "-list"))
|
---|
591 | listMatches = true;
|
---|
592 | else
|
---|
593 | break; /* The rest of params should define the filter */
|
---|
594 | }
|
---|
595 |
|
---|
596 | rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
|
---|
597 | ComSafeArrayAsOutParam(metrics),
|
---|
598 | ComSafeArrayAsOutParam(baseMetrics),
|
---|
599 | ComSafeArrayAsOutParam(objects));
|
---|
600 | if (FAILED(rc))
|
---|
601 | return 1;
|
---|
602 |
|
---|
603 | com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
|
---|
604 | CHECK_ERROR(performanceCollector,
|
---|
605 | DisableMetrics(ComSafeArrayAsInParam(metrics),
|
---|
606 | ComSafeArrayAsInParam(objects),
|
---|
607 | ComSafeArrayAsOutParam(affectedMetrics)));
|
---|
608 | if (listMatches)
|
---|
609 | listAffectedMetrics(aVirtualBox,
|
---|
610 | ComSafeArrayAsInParam(affectedMetrics));
|
---|
611 |
|
---|
612 | return 0;
|
---|
613 | }
|
---|
614 |
|
---|
615 |
|
---|
616 | int handleMetrics(HandlerArg *a)
|
---|
617 | {
|
---|
618 | int rc;
|
---|
619 |
|
---|
620 | /* at least one option: subcommand name */
|
---|
621 | if (a->argc < 1)
|
---|
622 | return errorSyntax(USAGE_METRICS, "Subcommand missing");
|
---|
623 |
|
---|
624 | ComPtr<IPerformanceCollector> performanceCollector;
|
---|
625 | CHECK_ERROR(a->virtualBox, COMGETTER(PerformanceCollector)(performanceCollector.asOutParam()));
|
---|
626 |
|
---|
627 | if (!strcmp(a->argv[0], "list"))
|
---|
628 | rc = handleMetricsList(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
629 | else if (!strcmp(a->argv[0], "setup"))
|
---|
630 | rc = handleMetricsSetup(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
631 | else if (!strcmp(a->argv[0], "query"))
|
---|
632 | rc = handleMetricsQuery(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
633 | else if (!strcmp(a->argv[0], "collect"))
|
---|
634 | rc = handleMetricsCollect(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
635 | else if (!strcmp(a->argv[0], "enable"))
|
---|
636 | rc = handleMetricsEnable(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
637 | else if (!strcmp(a->argv[0], "disable"))
|
---|
638 | rc = handleMetricsDisable(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
639 | else
|
---|
640 | return errorSyntax(USAGE_METRICS, "Invalid subcommand '%s'", a->argv[0]);
|
---|
641 |
|
---|
642 | return rc;
|
---|
643 | }
|
---|
644 |
|
---|
645 | #endif /* VBOX_ONLY_DOCS */
|
---|
646 |
|
---|