VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceBalloon.cpp@ 26962

Last change on this file since 26962 was 26904, checked in by vboxsync, 15 years ago

Additions/VBoxService: don't fail if ballooning isn't available

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.5 KB
Line 
1/* $Id: VBoxServiceBalloon.cpp 26904 2010-03-01 10:38:22Z vboxsync $ */
2/** @file
3 * VBoxMemBalloon - Memory balloon notification.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21#include <iprt/assert.h>
22#include <iprt/mem.h>
23#include <iprt/thread.h>
24#include <iprt/string.h>
25#include <iprt/semaphore.h>
26#include <iprt/system.h>
27#include <iprt/time.h>
28#include <VBox/VBoxGuestLib.h>
29#include "VBoxServiceInternal.h"
30#include "VBoxServiceUtils.h"
31
32
33
34/*******************************************************************************
35* Global Variables *
36*******************************************************************************/
37static uint32_t g_MemBalloonSize = 0;
38
39/** The semaphore we're blocking on. */
40static RTSEMEVENTMULTI g_MemBalloonEvent = NIL_RTSEMEVENTMULTI;
41
42/** @copydoc VBOXSERVICE::pfnPreInit */
43static DECLCALLBACK(int) VBoxServiceBalloonPreInit(void)
44{
45 return VINF_SUCCESS;
46}
47
48
49/** @copydoc VBOXSERVICE::pfnOption */
50static DECLCALLBACK(int) VBoxServiceBalloonOption(const char **ppszShort, int argc, char **argv, int *pi)
51{
52 return VINF_SUCCESS;
53}
54
55
56/** @copydoc VBOXSERVICE::pfnInit */
57static DECLCALLBACK(int) VBoxServiceBalloonInit(void)
58{
59 VBoxServiceVerbose(3, "VBoxServiceBalloonInit\n");
60
61 int rc = RTSemEventMultiCreate(&g_MemBalloonEvent);
62 AssertRCReturn(rc, rc);
63
64 g_MemBalloonSize = 0;
65
66 /* Check balloon size */
67 rc = VbglR3MemBalloonRefresh(&g_MemBalloonSize);
68 if (RT_SUCCESS(rc))
69 VBoxServiceVerbose(3, "VBoxMemBalloonInit: new balloon size %d MB\n", g_MemBalloonSize);
70 else
71 VBoxServiceVerbose(3, "VBoxMemBalloonInit: VbglR3MemBalloonRefresh failed with %d\n", rc);
72
73 /* We shouldn't fail here if ballooning isn't available. This can have several reasons,
74 * for instance, host too old (which is not that fatal). */
75 return VINF_SUCCESS;
76}
77
78
79uint32_t VBoxServiceBalloonQuerySize()
80{
81 return g_MemBalloonSize;
82}
83
84/** @copydoc VBOXSERVICE::pfnWorker */
85DECLCALLBACK(int) VBoxServiceBalloonWorker(bool volatile *pfShutdown)
86{
87 int rc = VINF_SUCCESS;
88
89 /* Start monitoring of the stat event change event. */
90 rc = VbglR3CtlFilterMask(VMMDEV_EVENT_BALLOON_CHANGE_REQUEST, 0);
91 if (RT_FAILURE(rc))
92 {
93 VBoxServiceVerbose(3, "VBoxServiceBalloonWorker: VbglR3CtlFilterMask failed with %d\n", rc);
94 return rc;
95 }
96
97 /*
98 * Tell the control thread that it can continue
99 * spawning services.
100 */
101 RTThreadUserSignal(RTThreadSelf());
102
103 /*
104 * Now enter the loop retrieving runtime data continuously.
105 */
106 for (;;)
107 {
108 uint32_t fEvents = 0;
109
110 /* Check if an update interval change is pending. */
111 rc = VbglR3WaitEvent(VMMDEV_EVENT_BALLOON_CHANGE_REQUEST, 0 /* no wait */, &fEvents);
112 if ( RT_SUCCESS(rc)
113 && (fEvents & VMMDEV_EVENT_BALLOON_CHANGE_REQUEST))
114 {
115 rc = VbglR3MemBalloonRefresh(&g_MemBalloonSize);
116 if (RT_SUCCESS(rc))
117 VBoxServiceVerbose(3, "VBoxServiceBalloonWorker: new balloon size %d MB\n", g_MemBalloonSize);
118 else
119 VBoxServiceVerbose(3, "VBoxServiceBalloonWorker: VbglR3MemBalloonRefresh failed with %d\n", rc);
120 }
121
122 /*
123 * Block for a while.
124 *
125 * The event semaphore takes care of ignoring interruptions and it
126 * allows us to implement service wakeup later.
127 */
128 if (*pfShutdown)
129 break;
130 int rc2 = RTSemEventMultiWait(g_MemBalloonEvent, 5000);
131 if (*pfShutdown)
132 break;
133 if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
134 {
135 VBoxServiceError("RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
136 rc = rc2;
137 break;
138 }
139 }
140
141 /* Cancel monitoring of the memory balloon change event. */
142 rc = VbglR3CtlFilterMask(0, VMMDEV_EVENT_BALLOON_CHANGE_REQUEST);
143 if (RT_FAILURE(rc))
144 VBoxServiceVerbose(3, "VBoxServiceBalloonWorker: VbglR3CtlFilterMask failed with %d\n", rc);
145
146 RTSemEventMultiDestroy(g_MemBalloonEvent);
147 g_MemBalloonEvent = NIL_RTSEMEVENTMULTI;
148
149 VBoxServiceVerbose(3, "VBoxServiceBalloonWorker: finished mem balloon change request thread\n");
150 return 0;
151}
152
153/** @copydoc VBOXSERVICE::pfnTerm */
154static DECLCALLBACK(void) VBoxServiceBalloonTerm(void)
155{
156 VBoxServiceVerbose(3, "VBoxServiceBalloonTerm\n");
157 return;
158}
159
160
161/** @copydoc VBOXSERVICE::pfnStop */
162static DECLCALLBACK(void) VBoxServiceBalloonStop(void)
163{
164 RTSemEventMultiSignal(g_MemBalloonEvent);
165}
166
167
168/**
169 * The 'memballoon' service description.
170 */
171VBOXSERVICE g_MemBalloon =
172{
173 /* pszName. */
174 "memballoon",
175 /* pszDescription. */
176 "Memory Ballooning",
177 /* pszUsage. */
178 NULL,
179 /* pszOptions. */
180 NULL,
181 /* methods */
182 VBoxServiceBalloonPreInit,
183 VBoxServiceBalloonOption,
184 VBoxServiceBalloonInit,
185 VBoxServiceBalloonWorker,
186 VBoxServiceBalloonStop,
187 VBoxServiceBalloonTerm
188};
Note: See TracBrowser for help on using the repository browser.

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