1 | /* $Id: */
|
---|
2 | /** @file
|
---|
3 | * VBoxWatchdogUtils - Misc. utility functions for modules.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2012 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 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <VBox/com/array.h>
|
---|
23 | #include "VBoxWatchdogInternal.h"
|
---|
24 |
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Retrieves a metric from a specified machine.
|
---|
28 | *
|
---|
29 | * @return IPRT status code.
|
---|
30 | * @param pMachine Pointer to the machine's internal structure.
|
---|
31 | * @param strName Name of metric to retrieve.
|
---|
32 | * @param pulData Pointer to value to retrieve the actual metric value.
|
---|
33 | */
|
---|
34 | int getMetric(PVBOXWATCHDOG_MACHINE pMachine, const Bstr& strName, LONG *pulData)
|
---|
35 | {
|
---|
36 | AssertPtrReturn(pMachine, VERR_INVALID_POINTER);
|
---|
37 | AssertPtrReturn(pulData, VERR_INVALID_POINTER);
|
---|
38 |
|
---|
39 | /* Input. */
|
---|
40 | com::SafeArray<BSTR> metricNames(1);
|
---|
41 | com::SafeIfaceArray<IUnknown> metricObjects(1);
|
---|
42 | pMachine->machine.queryInterfaceTo(&metricObjects[0]);
|
---|
43 |
|
---|
44 | /* Output. */
|
---|
45 | com::SafeArray<BSTR> retNames;
|
---|
46 | com::SafeIfaceArray<IUnknown> retObjects;
|
---|
47 | com::SafeArray<BSTR> retUnits;
|
---|
48 | com::SafeArray<ULONG> retScales;
|
---|
49 | com::SafeArray<ULONG> retSequenceNumbers;
|
---|
50 | com::SafeArray<ULONG> retIndices;
|
---|
51 | com::SafeArray<ULONG> retLengths;
|
---|
52 | com::SafeArray<LONG> retData;
|
---|
53 |
|
---|
54 | /* Query current memory free. */
|
---|
55 | strName.cloneTo(&metricNames[0]);
|
---|
56 | #ifdef VBOX_WATCHDOG_GLOBAL_PERFCOL
|
---|
57 | Assert(!g_pPerfCollector.isNull());
|
---|
58 | HRESULT hrc = g_pPerfCollector->QueryMetricsData(
|
---|
59 | #else
|
---|
60 | Assert(!pMachine->collector.isNull());
|
---|
61 | HRESULT hrc = pMachine->collector->QueryMetricsData(
|
---|
62 | #endif
|
---|
63 | ComSafeArrayAsInParam(metricNames),
|
---|
64 | ComSafeArrayAsInParam(metricObjects),
|
---|
65 | ComSafeArrayAsOutParam(retNames),
|
---|
66 | ComSafeArrayAsOutParam(retObjects),
|
---|
67 | ComSafeArrayAsOutParam(retUnits),
|
---|
68 | ComSafeArrayAsOutParam(retScales),
|
---|
69 | ComSafeArrayAsOutParam(retSequenceNumbers),
|
---|
70 | ComSafeArrayAsOutParam(retIndices),
|
---|
71 | ComSafeArrayAsOutParam(retLengths),
|
---|
72 | ComSafeArrayAsOutParam(retData));
|
---|
73 | #if 0
|
---|
74 | /* Useful for metrics debugging. */
|
---|
75 | for (unsigned j = 0; j < retNames.size(); j++)
|
---|
76 | {
|
---|
77 | Bstr metricUnit(retUnits[j]);
|
---|
78 | Bstr metricName(retNames[j]);
|
---|
79 | RTPrintf("%-20ls ", metricName.raw());
|
---|
80 | const char *separator = "";
|
---|
81 | for (unsigned k = 0; k < retLengths[j]; k++)
|
---|
82 | {
|
---|
83 | if (retScales[j] == 1)
|
---|
84 | RTPrintf("%s%d %ls", separator, retData[retIndices[j] + k], metricUnit.raw());
|
---|
85 | else
|
---|
86 | RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[j] + k] / retScales[j],
|
---|
87 | (retData[retIndices[j] + k] * 100 / retScales[j]) % 100, metricUnit.raw());
|
---|
88 | separator = ", ";
|
---|
89 | }
|
---|
90 | RTPrintf("\n");
|
---|
91 | }
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | if (SUCCEEDED(hrc))
|
---|
95 | *pulData = retData.size() ? retData[retIndices[0]] : 0;
|
---|
96 |
|
---|
97 | return SUCCEEDED(hrc) ? VINF_SUCCESS : VINF_NOT_SUPPORTED;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Returns the payload of a machine.
|
---|
102 | *
|
---|
103 | * @return void* Pointer to payload data. Mutable!
|
---|
104 | * @param pMachine Machine to get payload for.
|
---|
105 | * @param pszModule Module name to get payload from.
|
---|
106 | */
|
---|
107 | void* getPayload(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule)
|
---|
108 | {
|
---|
109 | AssertPtrReturn(pMachine, NULL);
|
---|
110 | AssertPtrReturn(pszModule, NULL);
|
---|
111 | mapPayloadIter it = pMachine->payload.find(pszModule);
|
---|
112 | if (it == pMachine->payload.end())
|
---|
113 | return NULL;
|
---|
114 | Assert(it->second.cbData);
|
---|
115 | return it->second.pvData;
|
---|
116 | }
|
---|
117 |
|
---|
118 | int payloadAlloc(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule,
|
---|
119 | size_t cbSize, void **ppszPayload)
|
---|
120 | {
|
---|
121 | AssertPtrReturn(pMachine, VERR_INVALID_POINTER);
|
---|
122 | AssertPtrReturn(pszModule, VERR_INVALID_POINTER);
|
---|
123 | AssertReturn(cbSize, VERR_INVALID_PARAMETER);
|
---|
124 |
|
---|
125 | void *pvData = RTMemAlloc(cbSize);
|
---|
126 | AssertPtrReturn(pvData, VERR_NO_MEMORY);
|
---|
127 |
|
---|
128 | mapPayloadIter it = pMachine->payload.find(pszModule);
|
---|
129 | AssertReturn(it == pMachine->payload.end(), VERR_INVALID_PARAMETER);
|
---|
130 |
|
---|
131 | VBOXWATCHDOG_MODULE_PAYLOAD p;
|
---|
132 | p.pvData = pvData;
|
---|
133 | p.cbData = cbSize;
|
---|
134 |
|
---|
135 | if (ppszPayload)
|
---|
136 | *ppszPayload = p.pvData;
|
---|
137 |
|
---|
138 | pMachine->payload.insert(std::make_pair(pszModule, p));
|
---|
139 |
|
---|
140 | return VINF_SUCCESS;
|
---|
141 | }
|
---|
142 |
|
---|
143 | void payloadFree(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule)
|
---|
144 | {
|
---|
145 | AssertPtrReturnVoid(pMachine);
|
---|
146 | AssertPtrReturnVoid(pszModule);
|
---|
147 |
|
---|
148 | mapPayloadIter it = pMachine->payload.find(pszModule);
|
---|
149 | if (it != pMachine->payload.end())
|
---|
150 | {
|
---|
151 | RTMemFree(it->second.pvData);
|
---|
152 | pMachine->payload.erase(it);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | PVBOXWATCHDOG_MACHINE getMachine(const Bstr& strUuid)
|
---|
157 | {
|
---|
158 | mapVMIter it = g_mapVM.find(strUuid);
|
---|
159 | if (it != g_mapVM.end())
|
---|
160 | return &it->second;
|
---|
161 | return NULL;
|
---|
162 | }
|
---|
163 |
|
---|
164 | MachineState_T getMachineState(const PVBOXWATCHDOG_MACHINE pMachine)
|
---|
165 | {
|
---|
166 | AssertPtrReturn(pMachine, MachineState_Null);
|
---|
167 | MachineState_T machineState;
|
---|
168 | Assert(!pMachine->machine.isNull());
|
---|
169 | HRESULT rc = pMachine->machine->COMGETTER(State)(&machineState);
|
---|
170 | if (SUCCEEDED(rc))
|
---|
171 | return machineState;
|
---|
172 | return MachineState_Null;
|
---|
173 | }
|
---|
174 |
|
---|