VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxBalloonCtrl/VBoxModBallooning.cpp@ 40011

Last change on this file since 40011 was 40011, checked in by vboxsync, 13 years ago

Build fix.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette