1 | /* $Id: VBoxModBallooning.cpp 39988 2012-02-03 16:23:24Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxModBallooning - Module for handling the automatic ballooning of VMs.
|
---|
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 | #ifndef VBOX_ONLY_DOCS
|
---|
23 | # include <VBox/com/com.h>
|
---|
24 | # include <VBox/com/string.h>
|
---|
25 | # include <VBox/com/Guid.h>
|
---|
26 | # include <VBox/com/array.h>
|
---|
27 | # include <VBox/com/ErrorInfo.h>
|
---|
28 | # include <VBox/com/errorprint.h>
|
---|
29 |
|
---|
30 | # include <VBox/com/EventQueue.h>
|
---|
31 | # include <VBox/com/listeners.h>
|
---|
32 | # include <VBox/com/VirtualBox.h>
|
---|
33 | #endif /* !VBOX_ONLY_DOCS */
|
---|
34 |
|
---|
35 | #include <VBox/err.h>
|
---|
36 | #include <VBox/log.h>
|
---|
37 | #include <VBox/version.h>
|
---|
38 |
|
---|
39 | #include <package-generated.h>
|
---|
40 |
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include <iprt/buildconfig.h>
|
---|
43 | #include <iprt/critsect.h>
|
---|
44 | #include <iprt/getopt.h>
|
---|
45 | #include <iprt/initterm.h>
|
---|
46 | #include <iprt/path.h>
|
---|
47 | #include <iprt/process.h>
|
---|
48 | #include <iprt/semaphore.h>
|
---|
49 | #include <iprt/stream.h>
|
---|
50 | #include <iprt/string.h>
|
---|
51 | #include <iprt/system.h>
|
---|
52 |
|
---|
53 | #include <map>
|
---|
54 | #include <string>
|
---|
55 | #include <signal.h>
|
---|
56 |
|
---|
57 | #include "VBoxWatchdogInternal.h"
|
---|
58 |
|
---|
59 | using namespace com;
|
---|
60 |
|
---|
61 | #define VBOX_MOD_BALLOONING_NAME "balloonctrl"
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * The module's RTGetOpt-IDs for the command line.
|
---|
65 | */
|
---|
66 | enum GETOPTDEF_BALLOONCTRL
|
---|
67 | {
|
---|
68 | GETOPTDEF_BALLOONCTRL_BALLOOINC = 1000,
|
---|
69 | GETOPTDEF_BALLOONCTRL_BALLOONDEC,
|
---|
70 | GETOPTDEF_BALLOONCTRL_BALLOONLOWERLIMIT,
|
---|
71 | GETOPTDEF_BALLOONCTRL_BALLOONMAX,
|
---|
72 | GETOPTDEF_BALLOONCTRL_TIMEOUTMS,
|
---|
73 | GETOPTDEF_BALLOONCTRL_GROUPS
|
---|
74 | };
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * The module's command line arguments.
|
---|
78 | */
|
---|
79 | static const RTGETOPTDEF g_aBalloonOpts[] = {
|
---|
80 | { "--balloon-dec", GETOPTDEF_BALLOONCTRL_BALLOONDEC, RTGETOPT_REQ_UINT32 },
|
---|
81 | { "--balloon-groups", GETOPTDEF_BALLOONCTRL_GROUPS, RTGETOPT_REQ_STRING },
|
---|
82 | { "--balloon-inc", GETOPTDEF_BALLOONCTRL_BALLOOINC, RTGETOPT_REQ_UINT32 },
|
---|
83 | { "--balloon-interval", GETOPTDEF_BALLOONCTRL_TIMEOUTMS, RTGETOPT_REQ_UINT32 },
|
---|
84 | { "--balloon-lower-limit", GETOPTDEF_BALLOONCTRL_BALLOONLOWERLIMIT, RTGETOPT_REQ_UINT32 },
|
---|
85 | { "--balloon-max", GETOPTDEF_BALLOONCTRL_BALLOONMAX, RTGETOPT_REQ_UINT32 }
|
---|
86 | };
|
---|
87 |
|
---|
88 | static unsigned long g_ulMemoryBalloonTimeoutMS = 30 * 1000; /* Default is 30 seconds timeout. */
|
---|
89 | static unsigned long g_ulMemoryBalloonIncrementMB = 256;
|
---|
90 | static unsigned long g_ulMemoryBalloonDecrementMB = 128;
|
---|
91 | /** Global balloon limit is 0, so disabled. Can be overridden by a per-VM
|
---|
92 | * "VBoxInternal/Guest/BalloonSizeMax" value. */
|
---|
93 | static unsigned long g_ulMemoryBalloonMaxMB = 0;
|
---|
94 | static unsigned long g_ulMemoryBalloonLowerLimitMB = 64;
|
---|
95 |
|
---|
96 | /** The ballooning module's payload. */
|
---|
97 | typedef struct VBOXWATCHDOG_BALLOONCTRL_PAYLOAD
|
---|
98 | {
|
---|
99 | /** The maximum ballooning size for the VM.
|
---|
100 | * Specify 0 for ballooning disabled. */
|
---|
101 | unsigned long ulBalloonSizeMax;
|
---|
102 | } VBOXWATCHDOG_BALLOONCTRL_PAYLOAD, *PVBOXWATCHDOG_BALLOONCTRL_PAYLOAD;
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Retrieves the current delta value
|
---|
107 | *
|
---|
108 | * @return long Delta (MB) of the balloon to be deflated (<0) or inflated (>0).
|
---|
109 | * @param ulCurrentDesktopBalloonSize The balloon's current size.
|
---|
110 | * @param ulDesktopFreeMemory The VM's current free memory.
|
---|
111 | * @param ulMaxBalloonSize The maximum balloon size (MB) it can inflate to.
|
---|
112 | */
|
---|
113 | static long balloonGetDelta(unsigned long ulCurrentDesktopBalloonSize,
|
---|
114 | unsigned long ulDesktopFreeMemory, unsigned long ulMaxBalloonSize)
|
---|
115 | {
|
---|
116 | if (ulCurrentDesktopBalloonSize > ulMaxBalloonSize)
|
---|
117 | return (ulMaxBalloonSize - ulCurrentDesktopBalloonSize);
|
---|
118 |
|
---|
119 | long lBalloonDelta = 0;
|
---|
120 | if (ulDesktopFreeMemory < g_ulMemoryBalloonLowerLimitMB)
|
---|
121 | {
|
---|
122 | /* Guest is running low on memory, we need to
|
---|
123 | * deflate the balloon. */
|
---|
124 | lBalloonDelta = (g_ulMemoryBalloonDecrementMB * -1);
|
---|
125 |
|
---|
126 | /* Ensure that the delta will not return a negative
|
---|
127 | * balloon size. */
|
---|
128 | if ((long)ulCurrentDesktopBalloonSize + lBalloonDelta < 0)
|
---|
129 | lBalloonDelta = 0;
|
---|
130 | }
|
---|
131 | else if (ulMaxBalloonSize > ulCurrentDesktopBalloonSize)
|
---|
132 | {
|
---|
133 | /* We want to inflate the balloon if we have room. */
|
---|
134 | long lIncrement = g_ulMemoryBalloonIncrementMB;
|
---|
135 | while (lIncrement >= 16 && (ulDesktopFreeMemory - lIncrement) < g_ulMemoryBalloonLowerLimitMB)
|
---|
136 | {
|
---|
137 | lIncrement = (lIncrement / 2);
|
---|
138 | }
|
---|
139 |
|
---|
140 | if ((ulDesktopFreeMemory - lIncrement) > g_ulMemoryBalloonLowerLimitMB)
|
---|
141 | lBalloonDelta = lIncrement;
|
---|
142 | }
|
---|
143 | if (ulCurrentDesktopBalloonSize + lBalloonDelta > ulMaxBalloonSize)
|
---|
144 | lBalloonDelta = (ulMaxBalloonSize - ulCurrentDesktopBalloonSize);
|
---|
145 | return lBalloonDelta;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Determines the maximum balloon size to set for the specified machine.
|
---|
150 | *
|
---|
151 | * @return unsigned long Balloon size (in MB) to set, 0 if no ballooning required.
|
---|
152 | * @param rptrMachine Pointer to interface of specified machine.
|
---|
153 | */
|
---|
154 | static unsigned long balloonGetMaxSize(const ComPtr<IMachine> &rptrMachine)
|
---|
155 | {
|
---|
156 | /*
|
---|
157 | * Try to retrieve the balloon maximum size via the following order:
|
---|
158 | * - command line parameter ("--balloon-max")
|
---|
159 | * Legacy (VBoxBalloonCtrl):
|
---|
160 | * - per-VM parameter ("VBoxInternal/Guest/BalloonSizeMax")
|
---|
161 | * Global:
|
---|
162 | * - global parameter ("VBoxInternal/Guest/BalloonSizeMax")
|
---|
163 | * New:
|
---|
164 | * - per-VM parameter ("VBoxInternal2/Watchdog/BalloonCtrl/BalloonSizeMax")
|
---|
165 | *
|
---|
166 | * By default (e.g. if none of above is set), ballooning is disabled.
|
---|
167 | */
|
---|
168 | unsigned long ulBalloonMax = g_ulMemoryBalloonMaxMB; /* Use global limit as default. */
|
---|
169 | if (!ulBalloonMax) /* Not set by command line? */
|
---|
170 | {
|
---|
171 | /* Try per-VM approach. */
|
---|
172 | Bstr strValue;
|
---|
173 | HRESULT rc = rptrMachine->GetExtraData(Bstr("VBoxInternal/Guest/BalloonSizeMax").raw(),
|
---|
174 | strValue.asOutParam());
|
---|
175 | if ( SUCCEEDED(rc)
|
---|
176 | && !strValue.isEmpty())
|
---|
177 | {
|
---|
178 | ulBalloonMax = Utf8Str(strValue).toUInt32();
|
---|
179 | }
|
---|
180 | }
|
---|
181 | if (!ulBalloonMax) /* Still not set by per-VM value? */
|
---|
182 | {
|
---|
183 | /* Try global approach. */
|
---|
184 | Bstr strValue;
|
---|
185 | HRESULT rc = g_pVirtualBox->GetExtraData(Bstr("VBoxInternal/Guest/BalloonSizeMax").raw(),
|
---|
186 | strValue.asOutParam());
|
---|
187 | if ( SUCCEEDED(rc)
|
---|
188 | && !strValue.isEmpty())
|
---|
189 | {
|
---|
190 | ulBalloonMax = Utf8Str(strValue).toUInt32();
|
---|
191 | }
|
---|
192 | }
|
---|
193 | if (!ulBalloonMax)
|
---|
194 | {
|
---|
195 | /** @todo ("VBoxInternal2/Watchdog/BalloonCtrl/BalloonSizeMax") */
|
---|
196 | }
|
---|
197 | return ulBalloonMax;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Indicates whether ballooning on the specified machine state is
|
---|
202 | * possible -- this only is true if the machine is up and running.
|
---|
203 | *
|
---|
204 | * @return bool Flag indicating whether the VM is running or not.
|
---|
205 | * @param enmState The VM's machine state to judge whether it's running or not.
|
---|
206 | */
|
---|
207 | static bool balloonIsPossible(MachineState_T enmState)
|
---|
208 | {
|
---|
209 | switch (enmState)
|
---|
210 | {
|
---|
211 | case MachineState_Running:
|
---|
212 | #if 0
|
---|
213 | /* Not required for ballooning. */
|
---|
214 | case MachineState_Teleporting:
|
---|
215 | case MachineState_LiveSnapshotting:
|
---|
216 | case MachineState_Paused:
|
---|
217 | case MachineState_TeleportingPausedVM:
|
---|
218 | #endif
|
---|
219 | return true;
|
---|
220 | default:
|
---|
221 | break;
|
---|
222 | }
|
---|
223 | return false;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Determines whether ballooning is required to the specified machine.
|
---|
228 | *
|
---|
229 | * @return bool True if ballooning is required, false if not.
|
---|
230 | * @param pMachine Machine to determine ballooning for.
|
---|
231 | */
|
---|
232 | static bool balloonIsRequired(PVBOXWATCHDOG_MACHINE pMachine)
|
---|
233 | {
|
---|
234 | AssertPtrReturn(pMachine, false);
|
---|
235 |
|
---|
236 | /* Only do ballooning if we have a maximum balloon size set. */
|
---|
237 | PVBOXWATCHDOG_BALLOONCTRL_PAYLOAD pData = (PVBOXWATCHDOG_BALLOONCTRL_PAYLOAD)
|
---|
238 | getPayload(pMachine, VBOX_MOD_BALLOONING_NAME);
|
---|
239 | AssertPtr(pData);
|
---|
240 | pData->ulBalloonSizeMax = pMachine->machine.isNull()
|
---|
241 | ? 0 : balloonGetMaxSize(pMachine->machine);
|
---|
242 |
|
---|
243 | /** @todo Add grouping as a criteria! */
|
---|
244 |
|
---|
245 | return pData->ulBalloonSizeMax ? true : false;
|
---|
246 | }
|
---|
247 |
|
---|
248 | int balloonMachineSetup(const Bstr& strUuid)
|
---|
249 | {
|
---|
250 | int vrc = VINF_SUCCESS;
|
---|
251 |
|
---|
252 | do
|
---|
253 | {
|
---|
254 | PVBOXWATCHDOG_MACHINE pMachine = getMachine(strUuid);
|
---|
255 | AssertPtrBreakStmt(pMachine, vrc=VERR_INVALID_PARAMETER);
|
---|
256 |
|
---|
257 | ComPtr<IMachine> m = pMachine->machine;
|
---|
258 |
|
---|
259 | /*
|
---|
260 | * Setup metrics required for ballooning.
|
---|
261 | */
|
---|
262 | com::SafeArray<BSTR> metricNames(1);
|
---|
263 | com::SafeIfaceArray<IUnknown> metricObjects(1);
|
---|
264 | com::SafeIfaceArray<IPerformanceMetric> metricAffected;
|
---|
265 |
|
---|
266 | Bstr strMetricNames(L"Guest/RAM/Usage");
|
---|
267 | strMetricNames.cloneTo(&metricNames[0]);
|
---|
268 |
|
---|
269 | HRESULT rc = m.queryInterfaceTo(&metricObjects[0]);
|
---|
270 |
|
---|
271 | #ifdef VBOX_WATCHDOG_GLOBAL_PERFCOL
|
---|
272 | CHECK_ERROR_BREAK(g_pPerfCollector, SetupMetrics(ComSafeArrayAsInParam(metricNames),
|
---|
273 | ComSafeArrayAsInParam(metricObjects),
|
---|
274 | 5 /* 5 seconds */,
|
---|
275 | 1 /* One sample is enough */,
|
---|
276 | ComSafeArrayAsOutParam(metricAffected)));
|
---|
277 | #else
|
---|
278 | ComPtr<IPerformanceCollector> coll = pMachine->collector;
|
---|
279 |
|
---|
280 | CHECK_ERROR_BREAK(g_pVirtualBox, COMGETTER(PerformanceCollector)(coll.asOutParam()));
|
---|
281 | CHECK_ERROR_BREAK(coll, SetupMetrics(ComSafeArrayAsInParam(metricNames),
|
---|
282 | ComSafeArrayAsInParam(metricObjects),
|
---|
283 | 5 /* 5 seconds */,
|
---|
284 | 1 /* One sample is enough */,
|
---|
285 | ComSafeArrayAsOutParam(metricAffected)));
|
---|
286 | #endif
|
---|
287 | if (FAILED(rc))
|
---|
288 | vrc = VERR_COM_IPRT_ERROR; /* @todo Find better rc! */
|
---|
289 |
|
---|
290 | } while (0);
|
---|
291 |
|
---|
292 | return vrc;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /**
|
---|
296 | * Does the actual ballooning and assumes the machine is
|
---|
297 | * capable and ready for ballooning.
|
---|
298 | *
|
---|
299 | * @return IPRT status code.
|
---|
300 | * @param strUuid UUID of the specified machine.
|
---|
301 | * @param pMachine Pointer to the machine's internal structure.
|
---|
302 | */
|
---|
303 | static int balloonMachineUpdate(const Bstr &strUuid, PVBOXWATCHDOG_MACHINE pMachine)
|
---|
304 | {
|
---|
305 | AssertPtrReturn(pMachine, VERR_INVALID_POINTER);
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * Get metrics collected at this point.
|
---|
309 | */
|
---|
310 | LONG lMemFree, lBalloonCur;
|
---|
311 | int vrc = getMetric(pMachine, L"Guest/RAM/Usage/Free", &lMemFree);
|
---|
312 | if (RT_SUCCESS(vrc))
|
---|
313 | vrc = getMetric(pMachine, L"Guest/RAM/Usage/Balloon", &lBalloonCur);
|
---|
314 |
|
---|
315 | if (RT_SUCCESS(vrc))
|
---|
316 | {
|
---|
317 | /* If guest statistics are not up and running yet, skip this iteration
|
---|
318 | * and try next time. */
|
---|
319 | if (lMemFree <= 0)
|
---|
320 | {
|
---|
321 | #ifdef DEBUG
|
---|
322 | serviceLogVerbose(("%ls: No metrics available yet!\n", strUuid.raw()));
|
---|
323 | #endif
|
---|
324 | return VINF_SUCCESS;
|
---|
325 | }
|
---|
326 |
|
---|
327 | lMemFree /= 1024;
|
---|
328 | lBalloonCur /= 1024;
|
---|
329 |
|
---|
330 | PVBOXWATCHDOG_BALLOONCTRL_PAYLOAD pData = (PVBOXWATCHDOG_BALLOONCTRL_PAYLOAD)
|
---|
331 | getPayload(pMachine, VBOX_MOD_BALLOONING_NAME);
|
---|
332 | AssertPtr(pData);
|
---|
333 |
|
---|
334 | serviceLogVerbose(("%ls: Balloon: %ld, Free mem: %ld, Max ballon: %ld\n",
|
---|
335 | strUuid.raw(),
|
---|
336 | lBalloonCur, lMemFree, pData->ulBalloonSizeMax));
|
---|
337 |
|
---|
338 | /* Calculate current balloon delta. */
|
---|
339 | long lDelta = balloonGetDelta(lBalloonCur, lMemFree, pData->ulBalloonSizeMax);
|
---|
340 | if (lDelta) /* Only do ballooning if there's really smth. to change ... */
|
---|
341 | {
|
---|
342 | lBalloonCur = lBalloonCur + lDelta;
|
---|
343 | Assert(lBalloonCur > 0);
|
---|
344 |
|
---|
345 | serviceLog("%ls: %s balloon by %ld to %ld ...\n",
|
---|
346 | strUuid.raw(),
|
---|
347 | lDelta > 0 ? "Inflating" : "Deflating", lDelta, lBalloonCur);
|
---|
348 |
|
---|
349 | HRESULT rc;
|
---|
350 |
|
---|
351 | /* Open a session for the VM. */
|
---|
352 | CHECK_ERROR(pMachine->machine, LockMachine(g_pSession, LockType_Shared));
|
---|
353 |
|
---|
354 | do
|
---|
355 | {
|
---|
356 | /* Get the associated console. */
|
---|
357 | ComPtr<IConsole> console;
|
---|
358 | CHECK_ERROR_BREAK(g_pSession, COMGETTER(Console)(console.asOutParam()));
|
---|
359 |
|
---|
360 | ComPtr <IGuest> guest;
|
---|
361 | rc = console->COMGETTER(Guest)(guest.asOutParam());
|
---|
362 | if (SUCCEEDED(rc))
|
---|
363 | CHECK_ERROR_BREAK(guest, COMSETTER(MemoryBalloonSize)(lBalloonCur));
|
---|
364 | else
|
---|
365 | serviceLog("Error: Unable to set new balloon size %ld for machine \"%ls\", rc=%Rhrc",
|
---|
366 | lBalloonCur, strUuid.raw(), rc);
|
---|
367 | } while (0);
|
---|
368 |
|
---|
369 | /* Unlock the machine again. */
|
---|
370 | g_pSession->UnlockMachine();
|
---|
371 | }
|
---|
372 | }
|
---|
373 | else
|
---|
374 | serviceLog("Error: Unable to retrieve metrics for machine \"%ls\", rc=%Rrc",
|
---|
375 | strUuid.raw(), vrc);
|
---|
376 | return vrc;
|
---|
377 | }
|
---|
378 |
|
---|
379 | /* Callbacks. */
|
---|
380 | static DECLCALLBACK(int) VBoxModBallooningPreInit(void)
|
---|
381 | {
|
---|
382 | return VINF_SUCCESS;
|
---|
383 | }
|
---|
384 |
|
---|
385 | static DECLCALLBACK(int) VBoxModBallooningOption(int argc, char **argv)
|
---|
386 | {
|
---|
387 | if (!argc) /* Take a shortcut. */
|
---|
388 | return -1;
|
---|
389 |
|
---|
390 | AssertPtrReturn(argv, VERR_INVALID_PARAMETER);
|
---|
391 |
|
---|
392 | RTGETOPTSTATE GetState;
|
---|
393 | int rc = RTGetOptInit(&GetState, argc, argv,
|
---|
394 | g_aBalloonOpts, RT_ELEMENTS(g_aBalloonOpts),
|
---|
395 | 0 /* First */, 0 /*fFlags*/);
|
---|
396 | if (RT_FAILURE(rc))
|
---|
397 | return rc;
|
---|
398 |
|
---|
399 | rc = 0; /* Set default parsing result to valid. */
|
---|
400 |
|
---|
401 | int c;
|
---|
402 | RTGETOPTUNION ValueUnion;
|
---|
403 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
404 | {
|
---|
405 | switch (c)
|
---|
406 | {
|
---|
407 | case GETOPTDEF_BALLOONCTRL_BALLOONDEC:
|
---|
408 | g_ulMemoryBalloonDecrementMB = ValueUnion.u32;
|
---|
409 | break;
|
---|
410 |
|
---|
411 | case GETOPTDEF_BALLOONCTRL_BALLOOINC:
|
---|
412 | g_ulMemoryBalloonIncrementMB = ValueUnion.u32;
|
---|
413 | break;
|
---|
414 |
|
---|
415 | case GETOPTDEF_BALLOONCTRL_GROUPS:
|
---|
416 | /** @todo Add ballooning groups cmd line arg. */
|
---|
417 | break;
|
---|
418 |
|
---|
419 | case GETOPTDEF_BALLOONCTRL_BALLOONLOWERLIMIT:
|
---|
420 | g_ulMemoryBalloonLowerLimitMB = ValueUnion.u32;
|
---|
421 | break;
|
---|
422 |
|
---|
423 | case GETOPTDEF_BALLOONCTRL_BALLOONMAX:
|
---|
424 | g_ulMemoryBalloonMaxMB = ValueUnion.u32;
|
---|
425 | break;
|
---|
426 |
|
---|
427 | /** @todo Add (legacy) "--inverval" setting! */
|
---|
428 | /** @todo This option is a common moudle option! Put
|
---|
429 | * this into a utility function! */
|
---|
430 | case GETOPTDEF_BALLOONCTRL_TIMEOUTMS:
|
---|
431 | g_ulMemoryBalloonTimeoutMS = ValueUnion.u32;
|
---|
432 | if (g_ulMemoryBalloonTimeoutMS < 500)
|
---|
433 | g_ulMemoryBalloonTimeoutMS = 500;
|
---|
434 | break;
|
---|
435 |
|
---|
436 | default:
|
---|
437 | rc = -1; /* We don't handle this option, skip. */
|
---|
438 | break;
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | return rc;
|
---|
443 | }
|
---|
444 |
|
---|
445 | static DECLCALLBACK(int) VBoxModBallooningInit(void)
|
---|
446 | {
|
---|
447 | return VINF_SUCCESS; /* Nothing to do here right now. */
|
---|
448 | }
|
---|
449 |
|
---|
450 | static DECLCALLBACK(int) VBoxModBallooningMain(void)
|
---|
451 | {
|
---|
452 | static uint64_t uLast = UINT64_MAX;
|
---|
453 | uint64_t uNow = RTTimeProgramMilliTS() / g_ulMemoryBalloonTimeoutMS;
|
---|
454 | if (uLast == uNow)
|
---|
455 | return VINF_SUCCESS;
|
---|
456 | uLast = uNow;
|
---|
457 |
|
---|
458 | int rc = VINF_SUCCESS;
|
---|
459 |
|
---|
460 | /** @todo Provide API for enumerating/working w/ machines inside a module! */
|
---|
461 | mapVMIter it = g_mapVM.begin();
|
---|
462 | while (it != g_mapVM.end())
|
---|
463 | {
|
---|
464 | MachineState_T state = getMachineState(&it->second);
|
---|
465 |
|
---|
466 | /* Our actual ballooning criteria. */
|
---|
467 | if ( balloonIsPossible(state)
|
---|
468 | && balloonIsRequired(&it->second))
|
---|
469 | {
|
---|
470 | rc = balloonMachineUpdate(it->first /* UUID */,
|
---|
471 | &it->second /* Machine */);
|
---|
472 | AssertRC(rc);
|
---|
473 | }
|
---|
474 | if (RT_FAILURE(rc))
|
---|
475 | break;
|
---|
476 |
|
---|
477 | it++;
|
---|
478 | }
|
---|
479 |
|
---|
480 | return rc;
|
---|
481 | }
|
---|
482 |
|
---|
483 | static DECLCALLBACK(int) VBoxModBallooningStop(void)
|
---|
484 | {
|
---|
485 | return VINF_SUCCESS;
|
---|
486 | }
|
---|
487 |
|
---|
488 | static DECLCALLBACK(void) VBoxModBallooningTerm(void)
|
---|
489 | {
|
---|
490 | }
|
---|
491 |
|
---|
492 | static DECLCALLBACK(int) VBoxModBallooningOnMachineRegistered(const Bstr &strUuid)
|
---|
493 | {
|
---|
494 | PVBOXWATCHDOG_MACHINE pMachine = getMachine(strUuid);
|
---|
495 | AssertPtrReturn(pMachine, VERR_INVALID_PARAMETER);
|
---|
496 |
|
---|
497 | PVBOXWATCHDOG_BALLOONCTRL_PAYLOAD pData;
|
---|
498 | int rc = payloadAlloc(pMachine, VBOX_MOD_BALLOONING_NAME,
|
---|
499 | sizeof(VBOXWATCHDOG_BALLOONCTRL_PAYLOAD), (void**)&pData);
|
---|
500 | if (RT_SUCCESS(rc))
|
---|
501 | rc = balloonMachineUpdate(strUuid, pMachine);
|
---|
502 |
|
---|
503 | return rc;
|
---|
504 | }
|
---|
505 |
|
---|
506 | static DECLCALLBACK(int) VBoxModBallooningOnMachineUnregistered(const Bstr &strUuid)
|
---|
507 | {
|
---|
508 | PVBOXWATCHDOG_MACHINE pMachine = getMachine(strUuid);
|
---|
509 | AssertPtrReturn(pMachine, VERR_INVALID_PARAMETER);
|
---|
510 |
|
---|
511 | payloadFree(pMachine, VBOX_MOD_BALLOONING_NAME);
|
---|
512 |
|
---|
513 | return VINF_SUCCESS;
|
---|
514 | }
|
---|
515 |
|
---|
516 | static DECLCALLBACK(int) VBoxModBallooningOnMachineStateChanged(const Bstr &strUuid,
|
---|
517 | MachineState_T enmState)
|
---|
518 | {
|
---|
519 | PVBOXWATCHDOG_MACHINE pMachine = getMachine(strUuid);
|
---|
520 | AssertPtrReturn(pMachine, VERR_INVALID_PARAMETER);
|
---|
521 |
|
---|
522 | return balloonMachineUpdate(strUuid, pMachine);
|
---|
523 | }
|
---|
524 |
|
---|
525 | static DECLCALLBACK(int) VBoxModBallooningOnServiceStateChanged(bool fAvailable)
|
---|
526 | {
|
---|
527 | return VINF_SUCCESS;
|
---|
528 | }
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * The 'balloonctrl' module description.
|
---|
532 | */
|
---|
533 | VBOXMODULE g_ModBallooning =
|
---|
534 | {
|
---|
535 | /* pszName. */
|
---|
536 | VBOX_MOD_BALLOONING_NAME,
|
---|
537 | /* pszDescription. */
|
---|
538 | "Memory Ballooning Control",
|
---|
539 | /* pszDepends. */
|
---|
540 | NULL,
|
---|
541 | /* uPriority. */
|
---|
542 | 0 /* Not used */,
|
---|
543 | /* pszUsage. */
|
---|
544 | " [--balloon-dec=<MB>] [--balloon-groups=<string>] [--balloon-inc=<MB>]\n"
|
---|
545 | " [--balloon-interval=<ms>] [--balloon-lower-limit=<MB>]\n"
|
---|
546 | " [--balloon-max=<MB>]\n",
|
---|
547 | /* pszOptions. */
|
---|
548 | "--balloon-dec Sets the ballooning decrement in MB (128 MB).\n"
|
---|
549 | "--balloon-groups Sets the VM groups for ballooning (all).\n"
|
---|
550 | "--balloon-inc Sets the ballooning increment in MB (256 MB).\n"
|
---|
551 | "--balloon-interval Sets the check interval in ms (30 seconds).\n"
|
---|
552 | "--balloon-lower-limit Sets the ballooning lower limit in MB (64 MB).\n"
|
---|
553 | "--balloon-max Sets the balloon maximum limit in MB (0 MB).\n"
|
---|
554 | " Specifying \"0\" means disabled ballooning.\n"
|
---|
555 | #if 1
|
---|
556 | /* (Legacy) note. */
|
---|
557 | "Set \"VBoxInternal/Guest/BalloonSizeMax\" for a per-VM maximum ballooning size.\n"
|
---|
558 | #endif
|
---|
559 | ,
|
---|
560 | /* methods. */
|
---|
561 | VBoxModBallooningPreInit,
|
---|
562 | VBoxModBallooningOption,
|
---|
563 | VBoxModBallooningInit,
|
---|
564 | VBoxModBallooningMain,
|
---|
565 | VBoxModBallooningStop,
|
---|
566 | VBoxModBallooningTerm,
|
---|
567 | /* callbacks. */
|
---|
568 | VBoxModBallooningOnMachineRegistered,
|
---|
569 | VBoxModBallooningOnMachineUnregistered,
|
---|
570 | VBoxModBallooningOnMachineStateChanged,
|
---|
571 | VBoxModBallooningOnServiceStateChanged
|
---|
572 | };
|
---|
573 |
|
---|