VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxBalloonCtrl/VBoxWatchdogUtils.cpp@ 74942

Last change on this file since 74942 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1/* $Id: */
2/** @file
3 * VBoxWatchdogUtils - Misc. utility functions for modules.
4 */
5
6/*
7 * Copyright (C) 2011-2017 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 * Adds a group / a set of groups to the specified map.
28 * If a group in the group map exists there will be no action.
29 *
30 * @return IPRT status code.
31 * @param groups Map to add group(s) to.
32 * @param pszGroupsToAdd Comma-separated string of one or more groups to add.
33 * @param fFlags Flags to set to the groups added.
34 */
35int groupAdd(mapGroups &groups, const char *pszGroupsToAdd, uint32_t fFlags)
36{
37 AssertPtrReturn(pszGroupsToAdd, VERR_INVALID_POINTER);
38
39 try
40 {
41 std::istringstream strGroups(pszGroupsToAdd);
42 for(std::string strToken; getline(strGroups, strToken, ','); )
43 {
44 strToken.erase(remove_if(strToken.begin(), strToken.end(), isspace), strToken.end());
45
46 Utf8Str strTokenUtf8(strToken.c_str());
47 mapGroupsIterConst it = groups.find(strTokenUtf8);
48
49 if (it == groups.end())
50 groups.insert(std::make_pair(strTokenUtf8, fFlags));
51 }
52 }
53 catch (...)
54 {
55 AssertFailed();
56 }
57
58 return VINF_SUCCESS;
59}
60
61/**
62 * Retrieves a metric from a specified machine.
63 *
64 * @return IPRT status code.
65 * @param pMachine Pointer to the machine's internal structure.
66 * @param strName Name of metric to retrieve.
67 * @param pulData Pointer to value to retrieve the actual metric value.
68 */
69int getMetric(PVBOXWATCHDOG_MACHINE pMachine, const Bstr& strName, LONG *pulData)
70{
71 AssertPtrReturn(pMachine, VERR_INVALID_POINTER);
72 AssertPtrReturn(pulData, VERR_INVALID_POINTER);
73
74 /* Input. */
75 com::SafeArray<BSTR> metricNames(1);
76 com::SafeIfaceArray<IUnknown> metricObjects(1);
77 pMachine->machine.queryInterfaceTo(&metricObjects[0]);
78
79 /* Output. */
80 com::SafeArray<BSTR> retNames;
81 com::SafeIfaceArray<IUnknown> retObjects;
82 com::SafeArray<BSTR> retUnits;
83 com::SafeArray<ULONG> retScales;
84 com::SafeArray<ULONG> retSequenceNumbers;
85 com::SafeArray<ULONG> retIndices;
86 com::SafeArray<ULONG> retLengths;
87 com::SafeArray<LONG> retData;
88
89 /* Query current memory free. */
90 strName.cloneTo(&metricNames[0]);
91#ifdef VBOX_WATCHDOG_GLOBAL_PERFCOL
92 Assert(!g_pPerfCollector.isNull());
93 HRESULT hrc = g_pPerfCollector->QueryMetricsData(
94#else
95 Assert(!pMachine->collector.isNull());
96 HRESULT hrc = pMachine->collector->QueryMetricsData(
97#endif
98 ComSafeArrayAsInParam(metricNames),
99 ComSafeArrayAsInParam(metricObjects),
100 ComSafeArrayAsOutParam(retNames),
101 ComSafeArrayAsOutParam(retObjects),
102 ComSafeArrayAsOutParam(retUnits),
103 ComSafeArrayAsOutParam(retScales),
104 ComSafeArrayAsOutParam(retSequenceNumbers),
105 ComSafeArrayAsOutParam(retIndices),
106 ComSafeArrayAsOutParam(retLengths),
107 ComSafeArrayAsOutParam(retData));
108#if 0
109 /* Useful for metrics debugging. */
110 for (unsigned j = 0; j < retNames.size(); j++)
111 {
112 Bstr metricUnit(retUnits[j]);
113 Bstr metricName(retNames[j]);
114 RTPrintf("%-20ls ", metricName.raw());
115 const char *separator = "";
116 for (unsigned k = 0; k < retLengths[j]; k++)
117 {
118 if (retScales[j] == 1)
119 RTPrintf("%s%d %ls", separator, retData[retIndices[j] + k], metricUnit.raw());
120 else
121 RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[j] + k] / retScales[j],
122 (retData[retIndices[j] + k] * 100 / retScales[j]) % 100, metricUnit.raw());
123 separator = ", ";
124 }
125 RTPrintf("\n");
126 }
127#endif
128
129 if (SUCCEEDED(hrc))
130 *pulData = retData.size() ? retData[retIndices[0]] : 0;
131
132 return SUCCEEDED(hrc) ? VINF_SUCCESS : VINF_NOT_SUPPORTED;
133}
134
135/**
136 * Returns the payload of a machine.
137 *
138 * @return void* Pointer to payload data. Mutable!
139 * @param pMachine Machine to get payload for.
140 * @param pszModule Module name to get payload from.
141 */
142void* payloadFrom(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule)
143{
144 AssertPtrReturn(pMachine, NULL);
145 AssertPtrReturn(pszModule, NULL);
146 mapPayloadIter it = pMachine->payload.find(pszModule);
147 if (it == pMachine->payload.end())
148 return NULL;
149 Assert(it->second.cbData);
150 return it->second.pvData;
151}
152
153int payloadAlloc(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule,
154 size_t cbSize, void **ppszPayload)
155{
156 AssertPtrReturn(pMachine, VERR_INVALID_POINTER);
157 AssertPtrReturn(pszModule, VERR_INVALID_POINTER);
158 AssertReturn(cbSize, VERR_INVALID_PARAMETER);
159
160 void *pvData = RTMemAllocZ(cbSize);
161 AssertPtrReturn(pvData, VERR_NO_MEMORY);
162
163 mapPayloadIter it = pMachine->payload.find(pszModule);
164 AssertReturn(it == pMachine->payload.end(), VERR_INVALID_PARAMETER);
165
166 VBOXWATCHDOG_MODULE_PAYLOAD p;
167 p.pvData = pvData;
168 p.cbData = cbSize;
169
170 if (ppszPayload)
171 *ppszPayload = p.pvData;
172
173 pMachine->payload.insert(std::make_pair(pszModule, p));
174
175 return VINF_SUCCESS;
176}
177
178void payloadFree(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule)
179{
180 AssertPtrReturnVoid(pMachine);
181 AssertPtrReturnVoid(pszModule);
182
183 mapPayloadIter it = pMachine->payload.find(pszModule);
184 if (it != pMachine->payload.end())
185 {
186 RTMemFree(it->second.pvData);
187 pMachine->payload.erase(it);
188 }
189}
190
191PVBOXWATCHDOG_MACHINE getMachine(const Bstr& strUuid)
192{
193 mapVMIter it = g_mapVM.find(strUuid);
194 if (it != g_mapVM.end())
195 return &it->second;
196 return NULL;
197}
198
199MachineState_T getMachineState(const PVBOXWATCHDOG_MACHINE pMachine)
200{
201 AssertPtrReturn(pMachine, MachineState_Null);
202 MachineState_T machineState;
203 Assert(!pMachine->machine.isNull());
204 HRESULT rc = pMachine->machine->COMGETTER(State)(&machineState);
205 if (SUCCEEDED(rc))
206 return machineState;
207 return MachineState_Null;
208}
209
210int cfgGetValueStr(const ComPtr<IVirtualBox> &rptrVBox, const ComPtr<IMachine> &rptrMachine,
211 const char *pszGlobal, const char *pszVM, Utf8Str &strValue, Utf8Str strDefault)
212{
213 AssertReturn(!rptrVBox.isNull(), VERR_INVALID_POINTER);
214
215
216 /* Try per-VM approach. */
217 Bstr strTemp;
218 HRESULT hr;
219 if (!rptrMachine.isNull())
220 {
221 AssertPtr(pszVM);
222 hr = rptrMachine->GetExtraData(Bstr(pszVM).raw(),
223 strTemp.asOutParam());
224 if ( SUCCEEDED(hr)
225 && !strTemp.isEmpty())
226 {
227 strValue = Utf8Str(strTemp);
228 }
229 }
230
231 if (strValue.isEmpty()) /* Not set by per-VM value? */
232 {
233 AssertPtr(pszGlobal);
234
235 /* Try global approach. */
236 hr = rptrVBox->GetExtraData(Bstr(pszGlobal).raw(),
237 strTemp.asOutParam());
238 if ( SUCCEEDED(hr)
239 && !strTemp.isEmpty())
240 {
241 strValue = Utf8Str(strTemp);
242 }
243 }
244
245 if (strValue.isEmpty())
246 {
247 strValue = strDefault;
248 return VERR_NOT_FOUND;
249 }
250
251 return VINF_SUCCESS;
252}
253
254int cfgGetValueU32(const ComPtr<IVirtualBox> &rptrVBox, const ComPtr<IMachine> &rptrMachine,
255 const char *pszGlobal, const char *pszVM, uint32_t *puValue, uint32_t uDefault)
256{
257 Utf8Str strValue;
258 int rc = cfgGetValueStr(rptrVBox, rptrMachine, pszGlobal, pszVM, strValue, "" /* Default */);
259 if (RT_SUCCESS(rc))
260 *puValue = strValue.toUInt32();
261 else
262 *puValue = uDefault;
263 return rc;
264}
265
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use