1 | /* $Id: VBoxManageMetrics.cpp 94234 2022-03-15 09:19:29Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxManage - The 'metrics' command.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 | /*********************************************************************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *********************************************************************************************************************************/
|
---|
21 | #include <VBox/com/com.h>
|
---|
22 | #include <VBox/com/array.h>
|
---|
23 | #include <VBox/com/ErrorInfo.h>
|
---|
24 | #include <VBox/com/errorprint.h>
|
---|
25 | #include <VBox/com/VirtualBox.h>
|
---|
26 |
|
---|
27 | #include <iprt/asm.h>
|
---|
28 | #include <iprt/stream.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 | #include <iprt/time.h>
|
---|
31 | #include <iprt/thread.h>
|
---|
32 | #include <VBox/log.h>
|
---|
33 |
|
---|
34 | #include <set>
|
---|
35 | #include <utility>
|
---|
36 |
|
---|
37 | #include "VBoxManage.h"
|
---|
38 | using namespace com;
|
---|
39 |
|
---|
40 | DECLARE_TRANSLATION_CONTEXT(Metrics);
|
---|
41 |
|
---|
42 | // funcs
|
---|
43 | ///////////////////////////////////////////////////////////////////////////////
|
---|
44 |
|
---|
45 |
|
---|
46 | static HRESULT parseFilterParameters(int argc, char *argv[],
|
---|
47 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
48 | ComSafeArrayOut(BSTR, outMetrics),
|
---|
49 | ComSafeArrayOut(IUnknown *, outObjects))
|
---|
50 | {
|
---|
51 | HRESULT rc = S_OK;
|
---|
52 | com::SafeArray<BSTR> retMetrics(1);
|
---|
53 | com::SafeIfaceArray <IUnknown> retObjects;
|
---|
54 |
|
---|
55 | Bstr metricNames, baseNames;
|
---|
56 |
|
---|
57 | /* Metric list */
|
---|
58 | if (argc > 1)
|
---|
59 | metricNames = argv[1];
|
---|
60 | else
|
---|
61 | {
|
---|
62 | metricNames = L"*";
|
---|
63 | baseNames = L"*";
|
---|
64 | }
|
---|
65 | metricNames.cloneTo(&retMetrics[0]);
|
---|
66 |
|
---|
67 | /* Object name */
|
---|
68 | if (argc > 0 && strcmp(argv[0], "*"))
|
---|
69 | {
|
---|
70 | if (!strcmp(argv[0], "host"))
|
---|
71 | {
|
---|
72 | ComPtr<IHost> host;
|
---|
73 | CHECK_ERROR(aVirtualBox, COMGETTER(Host)(host.asOutParam()));
|
---|
74 | retObjects.reset(1);
|
---|
75 | host.queryInterfaceTo(&retObjects[0]);
|
---|
76 | }
|
---|
77 | else
|
---|
78 | {
|
---|
79 | ComPtr<IMachine> machine;
|
---|
80 | rc = aVirtualBox->FindMachine(Bstr(argv[0]).raw(),
|
---|
81 | machine.asOutParam());
|
---|
82 | if (SUCCEEDED(rc))
|
---|
83 | {
|
---|
84 | retObjects.reset(1);
|
---|
85 | machine.queryInterfaceTo(&retObjects[0]);
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 | errorArgument(Metrics::tr("Invalid machine name: '%s'"), argv[0]);
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | }
|
---|
95 |
|
---|
96 | retMetrics.detachTo(ComSafeArrayOutArg(outMetrics));
|
---|
97 | retObjects.detachTo(ComSafeArrayOutArg(outObjects));
|
---|
98 |
|
---|
99 | return rc;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static Bstr toBaseName(Utf8Str& aFullName)
|
---|
103 | {
|
---|
104 | char *pszRaw = aFullName.mutableRaw();
|
---|
105 | /*
|
---|
106 | * Currently there are two metrics which base name is the same as the
|
---|
107 | * sub-metric name: CPU/MHz and Net/<iface>/LinkSpeed.
|
---|
108 | */
|
---|
109 | if (pszRaw && strcmp(pszRaw, "CPU/MHz") && !RTStrSimplePatternMatch("Net/*/LinkSpeed", pszRaw))
|
---|
110 | {
|
---|
111 | char *pszSlash = strrchr(pszRaw, '/');
|
---|
112 | if (pszSlash)
|
---|
113 | {
|
---|
114 | *pszSlash = 0;
|
---|
115 | aFullName.jolt();
|
---|
116 | }
|
---|
117 | }
|
---|
118 | return Bstr(aFullName);
|
---|
119 | }
|
---|
120 |
|
---|
121 | static Bstr getObjectName(ComPtr<IUnknown> aObject)
|
---|
122 | {
|
---|
123 | HRESULT rc;
|
---|
124 |
|
---|
125 | ComPtr<IHost> host = aObject;
|
---|
126 | if (!host.isNull())
|
---|
127 | return Bstr(Metrics::tr("host"));
|
---|
128 |
|
---|
129 | ComPtr<IMachine> machine = aObject;
|
---|
130 | if (!machine.isNull())
|
---|
131 | {
|
---|
132 | Bstr name;
|
---|
133 | CHECK_ERROR(machine, COMGETTER(Name)(name.asOutParam()));
|
---|
134 | if (SUCCEEDED(rc))
|
---|
135 | return name;
|
---|
136 | }
|
---|
137 | return Bstr(Metrics::tr("unknown"));
|
---|
138 | }
|
---|
139 |
|
---|
140 | static void listAffectedMetrics(ComSafeArrayIn(IPerformanceMetric*, aMetrics))
|
---|
141 | {
|
---|
142 | HRESULT rc;
|
---|
143 | com::SafeIfaceArray<IPerformanceMetric> metrics(ComSafeArrayInArg(aMetrics));
|
---|
144 | if (metrics.size())
|
---|
145 | {
|
---|
146 | ComPtr<IUnknown> object;
|
---|
147 | Bstr metricName;
|
---|
148 | RTPrintf(Metrics::tr("The following metrics were modified:\n\n"
|
---|
149 | "Object Metric\n"
|
---|
150 | "---------- --------------------\n"));
|
---|
151 | for (size_t i = 0; i < metrics.size(); i++)
|
---|
152 | {
|
---|
153 | CHECK_ERROR(metrics[i], COMGETTER(Object)(object.asOutParam()));
|
---|
154 | CHECK_ERROR(metrics[i], COMGETTER(MetricName)(metricName.asOutParam()));
|
---|
155 | RTPrintf("%-10ls %-20ls\n",
|
---|
156 | getObjectName(object).raw(), metricName.raw());
|
---|
157 | }
|
---|
158 | RTPrintf("\n");
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | RTMsgError(Metrics::tr("No metrics match the specified filter!"));
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * list
|
---|
168 | */
|
---|
169 | static RTEXITCODE handleMetricsList(int argc, char *argv[],
|
---|
170 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
171 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
172 | {
|
---|
173 | HRESULT rc;
|
---|
174 | com::SafeArray<BSTR> metrics;
|
---|
175 | com::SafeIfaceArray<IUnknown> objects;
|
---|
176 |
|
---|
177 | setCurrentSubcommand(HELP_SCOPE_METRICS_LIST);
|
---|
178 |
|
---|
179 | rc = parseFilterParameters(argc - 1, &argv[1], aVirtualBox,
|
---|
180 | ComSafeArrayAsOutParam(metrics),
|
---|
181 | ComSafeArrayAsOutParam(objects));
|
---|
182 | if (FAILED(rc))
|
---|
183 | return RTEXITCODE_FAILURE;
|
---|
184 |
|
---|
185 | com::SafeIfaceArray<IPerformanceMetric> metricInfo;
|
---|
186 |
|
---|
187 | CHECK_ERROR(performanceCollector,
|
---|
188 | GetMetrics(ComSafeArrayAsInParam(metrics),
|
---|
189 | ComSafeArrayAsInParam(objects),
|
---|
190 | ComSafeArrayAsOutParam(metricInfo)));
|
---|
191 |
|
---|
192 | ComPtr<IUnknown> object;
|
---|
193 | Bstr metricName, unit, description;
|
---|
194 | ULONG period, count;
|
---|
195 | LONG minimum, maximum;
|
---|
196 | RTPrintf(Metrics::tr(
|
---|
197 | "Object Metric Unit Minimum Maximum Period Count Description\n"
|
---|
198 | "--------------- ---------------------------------------- ---- ---------- ---------- ---------- ---------- -----------\n"));
|
---|
199 | for (size_t i = 0; i < metricInfo.size(); i++)
|
---|
200 | {
|
---|
201 | CHECK_ERROR(metricInfo[i], COMGETTER(Object)(object.asOutParam()));
|
---|
202 | CHECK_ERROR(metricInfo[i], COMGETTER(MetricName)(metricName.asOutParam()));
|
---|
203 | CHECK_ERROR(metricInfo[i], COMGETTER(Period)(&period));
|
---|
204 | CHECK_ERROR(metricInfo[i], COMGETTER(Count)(&count));
|
---|
205 | CHECK_ERROR(metricInfo[i], COMGETTER(MinimumValue)(&minimum));
|
---|
206 | CHECK_ERROR(metricInfo[i], COMGETTER(MaximumValue)(&maximum));
|
---|
207 | CHECK_ERROR(metricInfo[i], COMGETTER(Unit)(unit.asOutParam()));
|
---|
208 | CHECK_ERROR(metricInfo[i], COMGETTER(Description)(description.asOutParam()));
|
---|
209 | RTPrintf("%-15ls %-40ls %-4ls %10d %10d %10u %10u %ls\n",
|
---|
210 | getObjectName(object).raw(), metricName.raw(), unit.raw(),
|
---|
211 | minimum, maximum, period, count, description.raw());
|
---|
212 | }
|
---|
213 |
|
---|
214 | return RTEXITCODE_SUCCESS;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Metrics setup
|
---|
219 | */
|
---|
220 | static RTEXITCODE handleMetricsSetup(int argc, char *argv[],
|
---|
221 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
222 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
223 | {
|
---|
224 | HRESULT rc;
|
---|
225 | com::SafeArray<BSTR> metrics;
|
---|
226 | com::SafeIfaceArray<IUnknown> objects;
|
---|
227 | uint32_t period = 1, samples = 1;
|
---|
228 | bool listMatches = false;
|
---|
229 | int i;
|
---|
230 |
|
---|
231 | setCurrentSubcommand(HELP_SCOPE_METRICS_SETUP);
|
---|
232 |
|
---|
233 | for (i = 1; i < argc; i++)
|
---|
234 | {
|
---|
235 | if ( !strcmp(argv[i], "--period")
|
---|
236 | || !strcmp(argv[i], "-period"))
|
---|
237 | {
|
---|
238 | if (argc <= i + 1)
|
---|
239 | return errorArgument(Metrics::tr("Missing argument to '%s'"), argv[i]);
|
---|
240 | if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &period)
|
---|
241 | || !period)
|
---|
242 | return errorArgument(Metrics::tr("Invalid value for 'period' parameter: '%s'"), argv[i]);
|
---|
243 | }
|
---|
244 | else if ( !strcmp(argv[i], "--samples")
|
---|
245 | || !strcmp(argv[i], "-samples"))
|
---|
246 | {
|
---|
247 | if (argc <= i + 1)
|
---|
248 | return errorArgument(Metrics::tr("Missing argument to '%s'"), argv[i]);
|
---|
249 | if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &samples)
|
---|
250 | || !samples)
|
---|
251 | return errorArgument(Metrics::tr("Invalid value for 'samples' parameter: '%s'"), argv[i]);
|
---|
252 | }
|
---|
253 | else if ( !strcmp(argv[i], "--list")
|
---|
254 | || !strcmp(argv[i], "-list"))
|
---|
255 | listMatches = true;
|
---|
256 | else
|
---|
257 | break; /* The rest of params should define the filter */
|
---|
258 | }
|
---|
259 |
|
---|
260 | rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
|
---|
261 | ComSafeArrayAsOutParam(metrics),
|
---|
262 | ComSafeArrayAsOutParam(objects));
|
---|
263 | if (FAILED(rc))
|
---|
264 | return RTEXITCODE_FAILURE;
|
---|
265 |
|
---|
266 | com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
|
---|
267 | CHECK_ERROR(performanceCollector,
|
---|
268 | SetupMetrics(ComSafeArrayAsInParam(metrics),
|
---|
269 | ComSafeArrayAsInParam(objects), period, samples,
|
---|
270 | ComSafeArrayAsOutParam(affectedMetrics)));
|
---|
271 | if (FAILED(rc))
|
---|
272 | return RTEXITCODE_SYNTAX; /** @todo figure out why we must return 2 here. */
|
---|
273 |
|
---|
274 | if (listMatches)
|
---|
275 | listAffectedMetrics(ComSafeArrayAsInParam(affectedMetrics));
|
---|
276 |
|
---|
277 | return RTEXITCODE_SUCCESS;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * metrics query
|
---|
282 | */
|
---|
283 | static RTEXITCODE handleMetricsQuery(int argc, char *argv[],
|
---|
284 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
285 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
286 | {
|
---|
287 | HRESULT rc;
|
---|
288 | com::SafeArray<BSTR> metrics;
|
---|
289 | com::SafeIfaceArray<IUnknown> objects;
|
---|
290 |
|
---|
291 | setCurrentSubcommand(HELP_SCOPE_METRICS_QUERY);
|
---|
292 |
|
---|
293 | rc = parseFilterParameters(argc - 1, &argv[1], aVirtualBox,
|
---|
294 | ComSafeArrayAsOutParam(metrics),
|
---|
295 | ComSafeArrayAsOutParam(objects));
|
---|
296 | if (FAILED(rc))
|
---|
297 | return RTEXITCODE_FAILURE;
|
---|
298 |
|
---|
299 | com::SafeArray<BSTR> retNames;
|
---|
300 | com::SafeIfaceArray<IUnknown> retObjects;
|
---|
301 | com::SafeArray<BSTR> retUnits;
|
---|
302 | com::SafeArray<ULONG> retScales;
|
---|
303 | com::SafeArray<ULONG> retSequenceNumbers;
|
---|
304 | com::SafeArray<ULONG> retIndices;
|
---|
305 | com::SafeArray<ULONG> retLengths;
|
---|
306 | com::SafeArray<LONG> retData;
|
---|
307 | CHECK_ERROR(performanceCollector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
|
---|
308 | ComSafeArrayAsInParam(objects),
|
---|
309 | ComSafeArrayAsOutParam(retNames),
|
---|
310 | ComSafeArrayAsOutParam(retObjects),
|
---|
311 | ComSafeArrayAsOutParam(retUnits),
|
---|
312 | ComSafeArrayAsOutParam(retScales),
|
---|
313 | ComSafeArrayAsOutParam(retSequenceNumbers),
|
---|
314 | ComSafeArrayAsOutParam(retIndices),
|
---|
315 | ComSafeArrayAsOutParam(retLengths),
|
---|
316 | ComSafeArrayAsOutParam(retData)) );
|
---|
317 |
|
---|
318 | RTPrintf(Metrics::tr(
|
---|
319 | "Object Metric Values\n"
|
---|
320 | "--------------- ---------------------------------------- --------------------------------------------\n"));
|
---|
321 | for (unsigned i = 0; i < retNames.size(); i++)
|
---|
322 | {
|
---|
323 | Bstr metricUnit(retUnits[i]);
|
---|
324 | Bstr metricName(retNames[i]);
|
---|
325 | RTPrintf("%-15ls %-40ls ", getObjectName(retObjects[i]).raw(), metricName.raw());
|
---|
326 | const char *separator = "";
|
---|
327 | for (unsigned j = 0; j < retLengths[i]; j++)
|
---|
328 | {
|
---|
329 | if (retScales[i] == 1)
|
---|
330 | RTPrintf("%s%d %ls", separator, retData[retIndices[i] + j], metricUnit.raw());
|
---|
331 | else
|
---|
332 | RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[i] + j] / retScales[i],
|
---|
333 | (retData[retIndices[i] + j] * 100 / retScales[i]) % 100, metricUnit.raw());
|
---|
334 | separator = ", ";
|
---|
335 | }
|
---|
336 | RTPrintf("\n");
|
---|
337 | }
|
---|
338 |
|
---|
339 | return RTEXITCODE_SUCCESS;
|
---|
340 | }
|
---|
341 |
|
---|
342 | static void getTimestamp(char *pts, size_t tsSize)
|
---|
343 | {
|
---|
344 | *pts = 0;
|
---|
345 | AssertReturnVoid(tsSize >= 13); /* 3+3+3+3+1 */
|
---|
346 | RTTIMESPEC TimeSpec;
|
---|
347 | RTTIME Time;
|
---|
348 | RTTimeExplode(&Time, RTTimeNow(&TimeSpec));
|
---|
349 | pts += RTStrFormatNumber(pts, Time.u8Hour, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
350 | *pts++ = ':';
|
---|
351 | pts += RTStrFormatNumber(pts, Time.u8Minute, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
352 | *pts++ = ':';
|
---|
353 | pts += RTStrFormatNumber(pts, Time.u8Second, 10, 2, 0, RTSTR_F_ZEROPAD);
|
---|
354 | *pts++ = '.';
|
---|
355 | pts += RTStrFormatNumber(pts, Time.u32Nanosecond / 1000000, 10, 3, 0, RTSTR_F_ZEROPAD);
|
---|
356 | *pts = 0;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /** Used by the handleMetricsCollect loop. */
|
---|
360 | static bool volatile g_fKeepGoing = true;
|
---|
361 |
|
---|
362 | #ifdef RT_OS_WINDOWS
|
---|
363 | /**
|
---|
364 | * Handler routine for catching Ctrl-C, Ctrl-Break and closing of
|
---|
365 | * the console.
|
---|
366 | *
|
---|
367 | * @returns true if handled, false if not handled.
|
---|
368 | * @param dwCtrlType The type of control signal.
|
---|
369 | *
|
---|
370 | * @remarks This is called on a new thread.
|
---|
371 | */
|
---|
372 | static BOOL WINAPI ctrlHandler(DWORD dwCtrlType) RT_NOTHROW_DEF
|
---|
373 | {
|
---|
374 | switch (dwCtrlType)
|
---|
375 | {
|
---|
376 | /* Ctrl-C or Ctrl-Break or Close */
|
---|
377 | case CTRL_C_EVENT:
|
---|
378 | case CTRL_BREAK_EVENT:
|
---|
379 | case CTRL_CLOSE_EVENT:
|
---|
380 | /* Let's shut down gracefully. */
|
---|
381 | ASMAtomicWriteBool(&g_fKeepGoing, false);
|
---|
382 | return TRUE;
|
---|
383 | }
|
---|
384 | /* Don't care about the rest -- let it die a horrible death. */
|
---|
385 | return FALSE;
|
---|
386 | }
|
---|
387 | #endif /* RT_OS_WINDOWS */
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * collect
|
---|
391 | */
|
---|
392 | static RTEXITCODE handleMetricsCollect(int argc, char *argv[],
|
---|
393 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
394 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
395 | {
|
---|
396 | HRESULT rc;
|
---|
397 | com::SafeArray<BSTR> metrics;
|
---|
398 | com::SafeIfaceArray<IUnknown> objects;
|
---|
399 | uint32_t period = 1, samples = 1;
|
---|
400 | bool isDetached = false, listMatches = false;
|
---|
401 | int i;
|
---|
402 |
|
---|
403 | setCurrentSubcommand(HELP_SCOPE_METRICS_COLLECT);
|
---|
404 |
|
---|
405 | for (i = 1; i < argc; i++)
|
---|
406 | {
|
---|
407 | if ( !strcmp(argv[i], "--period")
|
---|
408 | || !strcmp(argv[i], "-period"))
|
---|
409 | {
|
---|
410 | if (argc <= i + 1)
|
---|
411 | return errorArgument(Metrics::tr("Missing argument to '%s'"), argv[i]);
|
---|
412 | if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &period)
|
---|
413 | || !period)
|
---|
414 | return errorArgument(Metrics::tr("Invalid value for 'period' parameter: '%s'"), argv[i]);
|
---|
415 | }
|
---|
416 | else if ( !strcmp(argv[i], "--samples")
|
---|
417 | || !strcmp(argv[i], "-samples"))
|
---|
418 | {
|
---|
419 | if (argc <= i + 1)
|
---|
420 | return errorArgument(Metrics::tr("Missing argument to '%s'"), argv[i]);
|
---|
421 | if ( VINF_SUCCESS != RTStrToUInt32Full(argv[++i], 10, &samples)
|
---|
422 | || !samples)
|
---|
423 | return errorArgument(Metrics::tr("Invalid value for 'samples' parameter: '%s'"), argv[i]);
|
---|
424 | }
|
---|
425 | else if ( !strcmp(argv[i], "--list")
|
---|
426 | || !strcmp(argv[i], "-list"))
|
---|
427 | listMatches = true;
|
---|
428 | else if ( !strcmp(argv[i], "--detach")
|
---|
429 | || !strcmp(argv[i], "-detach"))
|
---|
430 | isDetached = true;
|
---|
431 | else
|
---|
432 | break; /* The rest of params should define the filter */
|
---|
433 | }
|
---|
434 |
|
---|
435 | rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
|
---|
436 | ComSafeArrayAsOutParam(metrics),
|
---|
437 | ComSafeArrayAsOutParam(objects));
|
---|
438 | if (FAILED(rc))
|
---|
439 | return RTEXITCODE_FAILURE;
|
---|
440 |
|
---|
441 | com::SafeIfaceArray<IPerformanceMetric> metricInfo;
|
---|
442 |
|
---|
443 | CHECK_ERROR(performanceCollector,
|
---|
444 | GetMetrics(ComSafeArrayAsInParam(metrics),
|
---|
445 | ComSafeArrayAsInParam(objects),
|
---|
446 | ComSafeArrayAsOutParam(metricInfo)));
|
---|
447 |
|
---|
448 | std::set<std::pair<ComPtr<IUnknown>,Bstr> > baseMetrics;
|
---|
449 | ComPtr<IUnknown> objectFiltered;
|
---|
450 | Bstr metricNameFiltered;
|
---|
451 | for (i = 0; i < (int)metricInfo.size(); i++)
|
---|
452 | {
|
---|
453 | CHECK_ERROR(metricInfo[i], COMGETTER(Object)(objectFiltered.asOutParam()));
|
---|
454 | CHECK_ERROR(metricInfo[i], COMGETTER(MetricName)(metricNameFiltered.asOutParam()));
|
---|
455 | Utf8Str baseMetricName(metricNameFiltered);
|
---|
456 | baseMetrics.insert(std::make_pair(objectFiltered, toBaseName(baseMetricName)));
|
---|
457 | }
|
---|
458 | com::SafeArray<BSTR> baseMetricsFiltered(baseMetrics.size());
|
---|
459 | com::SafeIfaceArray<IUnknown> objectsFiltered(baseMetrics.size());
|
---|
460 | std::set<std::pair<ComPtr<IUnknown>,Bstr> >::iterator it;
|
---|
461 | i = 0;
|
---|
462 | for (it = baseMetrics.begin(); it != baseMetrics.end(); ++it)
|
---|
463 | {
|
---|
464 | it->first.queryInterfaceTo(&objectsFiltered[i]);
|
---|
465 | Bstr(it->second).detachTo(&baseMetricsFiltered[i++]);
|
---|
466 | }
|
---|
467 | com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
|
---|
468 | CHECK_ERROR(performanceCollector,
|
---|
469 | SetupMetrics(ComSafeArrayAsInParam(baseMetricsFiltered),
|
---|
470 | ComSafeArrayAsInParam(objectsFiltered), period, samples,
|
---|
471 | ComSafeArrayAsOutParam(affectedMetrics)));
|
---|
472 | if (FAILED(rc))
|
---|
473 | return RTEXITCODE_SYNTAX; /** @todo figure out why we must return 2 here. */
|
---|
474 |
|
---|
475 | if (listMatches)
|
---|
476 | listAffectedMetrics(ComSafeArrayAsInParam(affectedMetrics));
|
---|
477 | if (!affectedMetrics.size())
|
---|
478 | return RTEXITCODE_FAILURE;
|
---|
479 |
|
---|
480 | if (isDetached)
|
---|
481 | {
|
---|
482 | RTMsgWarning(Metrics::tr("The background process holding collected metrics will shutdown\n"
|
---|
483 | "in few seconds, discarding all collected data and parameters."));
|
---|
484 | return RTEXITCODE_SUCCESS;
|
---|
485 | }
|
---|
486 |
|
---|
487 | #ifdef RT_OS_WINDOWS
|
---|
488 | SetConsoleCtrlHandler(ctrlHandler, true);
|
---|
489 | #endif /* RT_OS_WINDOWS */
|
---|
490 |
|
---|
491 | RTPrintf(Metrics::tr("Time stamp Object Metric Value\n"));
|
---|
492 |
|
---|
493 | while (g_fKeepGoing)
|
---|
494 | {
|
---|
495 | RTPrintf("------------ ---------- -------------------- --------------------\n");
|
---|
496 | RTThreadSleep(period * 1000); // Sleep for 'period' seconds
|
---|
497 | char ts[15];
|
---|
498 |
|
---|
499 | getTimestamp(ts, sizeof(ts));
|
---|
500 | com::SafeArray<BSTR> retNames;
|
---|
501 | com::SafeIfaceArray<IUnknown> retObjects;
|
---|
502 | com::SafeArray<BSTR> retUnits;
|
---|
503 | com::SafeArray<ULONG> retScales;
|
---|
504 | com::SafeArray<ULONG> retSequenceNumbers;
|
---|
505 | com::SafeArray<ULONG> retIndices;
|
---|
506 | com::SafeArray<ULONG> retLengths;
|
---|
507 | com::SafeArray<LONG> retData;
|
---|
508 | CHECK_ERROR(performanceCollector, QueryMetricsData(ComSafeArrayAsInParam(metrics),
|
---|
509 | ComSafeArrayAsInParam(objects),
|
---|
510 | ComSafeArrayAsOutParam(retNames),
|
---|
511 | ComSafeArrayAsOutParam(retObjects),
|
---|
512 | ComSafeArrayAsOutParam(retUnits),
|
---|
513 | ComSafeArrayAsOutParam(retScales),
|
---|
514 | ComSafeArrayAsOutParam(retSequenceNumbers),
|
---|
515 | ComSafeArrayAsOutParam(retIndices),
|
---|
516 | ComSafeArrayAsOutParam(retLengths),
|
---|
517 | ComSafeArrayAsOutParam(retData)) );
|
---|
518 | for (unsigned j = 0; j < retNames.size(); j++)
|
---|
519 | {
|
---|
520 | Bstr metricUnit(retUnits[j]);
|
---|
521 | Bstr metricName(retNames[j]);
|
---|
522 | RTPrintf("%-12s %-10ls %-20ls ", ts, getObjectName(retObjects[j]).raw(), metricName.raw());
|
---|
523 | const char *separator = "";
|
---|
524 | for (unsigned k = 0; k < retLengths[j]; k++)
|
---|
525 | {
|
---|
526 | if (retScales[j] == 1)
|
---|
527 | RTPrintf("%s%d %ls", separator, retData[retIndices[j] + k], metricUnit.raw());
|
---|
528 | else
|
---|
529 | RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[j] + k] / retScales[j],
|
---|
530 | (retData[retIndices[j] + k] * 100 / retScales[j]) % 100, metricUnit.raw());
|
---|
531 | separator = ", ";
|
---|
532 | }
|
---|
533 | RTPrintf("\n");
|
---|
534 | }
|
---|
535 | RTStrmFlush(g_pStdOut);
|
---|
536 | }
|
---|
537 |
|
---|
538 | #ifdef RT_OS_WINDOWS
|
---|
539 | SetConsoleCtrlHandler(ctrlHandler, false);
|
---|
540 | #endif /* RT_OS_WINDOWS */
|
---|
541 |
|
---|
542 | return RTEXITCODE_SUCCESS;
|
---|
543 | }
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * Enable metrics
|
---|
547 | */
|
---|
548 | static RTEXITCODE handleMetricsEnable(int argc, char *argv[],
|
---|
549 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
550 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
551 | {
|
---|
552 | HRESULT rc;
|
---|
553 | com::SafeArray<BSTR> metrics;
|
---|
554 | com::SafeIfaceArray<IUnknown> objects;
|
---|
555 | bool listMatches = false;
|
---|
556 | int i;
|
---|
557 |
|
---|
558 | setCurrentSubcommand(HELP_SCOPE_METRICS_ENABLE);
|
---|
559 |
|
---|
560 | for (i = 1; i < argc; i++)
|
---|
561 | {
|
---|
562 | if ( !strcmp(argv[i], "--list")
|
---|
563 | || !strcmp(argv[i], "-list"))
|
---|
564 | listMatches = true;
|
---|
565 | else
|
---|
566 | break; /* The rest of params should define the filter */
|
---|
567 | }
|
---|
568 |
|
---|
569 | rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
|
---|
570 | ComSafeArrayAsOutParam(metrics),
|
---|
571 | ComSafeArrayAsOutParam(objects));
|
---|
572 | if (FAILED(rc))
|
---|
573 | return RTEXITCODE_FAILURE;
|
---|
574 |
|
---|
575 | com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
|
---|
576 | CHECK_ERROR(performanceCollector,
|
---|
577 | EnableMetrics(ComSafeArrayAsInParam(metrics),
|
---|
578 | ComSafeArrayAsInParam(objects),
|
---|
579 | ComSafeArrayAsOutParam(affectedMetrics)));
|
---|
580 | if (FAILED(rc))
|
---|
581 | return RTEXITCODE_SYNTAX; /** @todo figure out why we must return 2 here. */
|
---|
582 |
|
---|
583 | if (listMatches)
|
---|
584 | listAffectedMetrics(ComSafeArrayAsInParam(affectedMetrics));
|
---|
585 |
|
---|
586 | return RTEXITCODE_SUCCESS;
|
---|
587 | }
|
---|
588 |
|
---|
589 | /**
|
---|
590 | * Disable metrics
|
---|
591 | */
|
---|
592 | static RTEXITCODE handleMetricsDisable(int argc, char *argv[],
|
---|
593 | ComPtr<IVirtualBox> aVirtualBox,
|
---|
594 | ComPtr<IPerformanceCollector> performanceCollector)
|
---|
595 | {
|
---|
596 | HRESULT rc;
|
---|
597 | com::SafeArray<BSTR> metrics;
|
---|
598 | com::SafeIfaceArray<IUnknown> objects;
|
---|
599 | bool listMatches = false;
|
---|
600 | int i;
|
---|
601 |
|
---|
602 | setCurrentSubcommand(HELP_SCOPE_METRICS_DISABLE);
|
---|
603 |
|
---|
604 | for (i = 1; i < argc; i++)
|
---|
605 | {
|
---|
606 | if ( !strcmp(argv[i], "--list")
|
---|
607 | || !strcmp(argv[i], "-list"))
|
---|
608 | listMatches = true;
|
---|
609 | else
|
---|
610 | break; /* The rest of params should define the filter */
|
---|
611 | }
|
---|
612 |
|
---|
613 | rc = parseFilterParameters(argc - i, &argv[i], aVirtualBox,
|
---|
614 | ComSafeArrayAsOutParam(metrics),
|
---|
615 | ComSafeArrayAsOutParam(objects));
|
---|
616 | if (FAILED(rc))
|
---|
617 | return RTEXITCODE_FAILURE;
|
---|
618 |
|
---|
619 | com::SafeIfaceArray<IPerformanceMetric> affectedMetrics;
|
---|
620 | CHECK_ERROR(performanceCollector,
|
---|
621 | DisableMetrics(ComSafeArrayAsInParam(metrics),
|
---|
622 | ComSafeArrayAsInParam(objects),
|
---|
623 | ComSafeArrayAsOutParam(affectedMetrics)));
|
---|
624 | if (FAILED(rc))
|
---|
625 | return RTEXITCODE_SYNTAX; /** @todo figure out why we must return 2 here. */
|
---|
626 |
|
---|
627 | if (listMatches)
|
---|
628 | listAffectedMetrics(ComSafeArrayAsInParam(affectedMetrics));
|
---|
629 |
|
---|
630 | return RTEXITCODE_SUCCESS;
|
---|
631 | }
|
---|
632 |
|
---|
633 |
|
---|
634 | RTEXITCODE handleMetrics(HandlerArg *a)
|
---|
635 | {
|
---|
636 | /* at least one option: subcommand name */
|
---|
637 | if (a->argc < 1)
|
---|
638 | return errorSyntax(Metrics::tr("Subcommand missing"));
|
---|
639 |
|
---|
640 | ComPtr<IPerformanceCollector> performanceCollector;
|
---|
641 | CHECK_ERROR2I_RET(a->virtualBox, COMGETTER(PerformanceCollector)(performanceCollector.asOutParam()), RTEXITCODE_FAILURE);
|
---|
642 |
|
---|
643 | RTEXITCODE rcExit;
|
---|
644 | if (!strcmp(a->argv[0], "list"))
|
---|
645 | rcExit = handleMetricsList(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
646 | else if (!strcmp(a->argv[0], "setup"))
|
---|
647 | rcExit = handleMetricsSetup(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
648 | else if (!strcmp(a->argv[0], "query"))
|
---|
649 | rcExit = handleMetricsQuery(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
650 | else if (!strcmp(a->argv[0], "collect"))
|
---|
651 | rcExit = handleMetricsCollect(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
652 | else if (!strcmp(a->argv[0], "enable"))
|
---|
653 | rcExit = handleMetricsEnable(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
654 | else if (!strcmp(a->argv[0], "disable"))
|
---|
655 | rcExit = handleMetricsDisable(a->argc, a->argv, a->virtualBox, performanceCollector);
|
---|
656 | else
|
---|
657 | return errorSyntax(Metrics::tr("Invalid subcommand '%s'"), a->argv[0]);
|
---|
658 |
|
---|
659 | return rcExit;
|
---|
660 | }
|
---|