VirtualBox

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

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

*: Added svn:keywords where missing.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 5.3 KB
Line 
1/* $Id: VBoxServiceBalloon.cpp 26562 2010-02-16 01:05:49Z 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 return rc;
74}
75
76
77uint32_t VBoxServiceBalloonQuerySize()
78{
79 return g_MemBalloonSize;
80}
81
82/** @copydoc VBOXSERVICE::pfnWorker */
83DECLCALLBACK(int) VBoxServiceBalloonWorker(bool volatile *pfShutdown)
84{
85 int rc = VINF_SUCCESS;
86
87 /* Start monitoring of the stat event change event. */
88 rc = VbglR3CtlFilterMask(VMMDEV_EVENT_BALLOON_CHANGE_REQUEST, 0);
89 if (RT_FAILURE(rc))
90 {
91 VBoxServiceVerbose(3, "VBoxServiceBalloonWorker: VbglR3CtlFilterMask failed with %d\n", rc);
92 return rc;
93 }
94
95 /*
96 * Tell the control thread that it can continue
97 * spawning services.
98 */
99 RTThreadUserSignal(RTThreadSelf());
100
101 /*
102 * Now enter the loop retrieving runtime data continuously.
103 */
104 for (;;)
105 {
106 uint32_t fEvents = 0;
107
108 /* Check if an update interval change is pending. */
109 rc = VbglR3WaitEvent(VMMDEV_EVENT_BALLOON_CHANGE_REQUEST, 0 /* no wait */, &fEvents);
110 if ( RT_SUCCESS(rc)
111 && (fEvents & VMMDEV_EVENT_BALLOON_CHANGE_REQUEST))
112 {
113 rc = VbglR3MemBalloonRefresh(&g_MemBalloonSize);
114 if (RT_SUCCESS(rc))
115 VBoxServiceVerbose(3, "VBoxServiceBalloonWorker: new balloon size %d MB\n", g_MemBalloonSize);
116 else
117 VBoxServiceVerbose(3, "VBoxServiceBalloonWorker: VbglR3MemBalloonRefresh failed with %d\n", rc);
118 }
119
120 /*
121 * Block for a while.
122 *
123 * The event semaphore takes care of ignoring interruptions and it
124 * allows us to implement service wakeup later.
125 */
126 if (*pfShutdown)
127 break;
128 int rc2 = RTSemEventMultiWait(g_MemBalloonEvent, 5000);
129 if (*pfShutdown)
130 break;
131 if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
132 {
133 VBoxServiceError("RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
134 rc = rc2;
135 break;
136 }
137 }
138
139 /* Cancel monitoring of the memory balloon change event. */
140 rc = VbglR3CtlFilterMask(0, VMMDEV_EVENT_BALLOON_CHANGE_REQUEST);
141 if (RT_FAILURE(rc))
142 VBoxServiceVerbose(3, "VBoxServiceBalloonWorker: VbglR3CtlFilterMask failed with %d\n", rc);
143
144 RTSemEventMultiDestroy(g_MemBalloonEvent);
145 g_MemBalloonEvent = NIL_RTSEMEVENTMULTI;
146
147 VBoxServiceVerbose(3, "VBoxServiceBalloonWorker: finished mem balloon change request thread\n");
148 return 0;
149}
150
151/** @copydoc VBOXSERVICE::pfnTerm */
152static DECLCALLBACK(void) VBoxServiceBalloonTerm(void)
153{
154 VBoxServiceVerbose(3, "VBoxServiceBalloonTerm\n");
155 return;
156}
157
158
159/** @copydoc VBOXSERVICE::pfnStop */
160static DECLCALLBACK(void) VBoxServiceBalloonStop(void)
161{
162 RTSemEventMultiSignal(g_MemBalloonEvent);
163}
164
165
166/**
167 * The 'memballoon' service description.
168 */
169VBOXSERVICE g_MemBalloon =
170{
171 /* pszName. */
172 "memballoon",
173 /* pszDescription. */
174 "Memory Ballooning",
175 /* pszUsage. */
176 NULL,
177 /* pszOptions. */
178 NULL,
179 /* methods */
180 VBoxServiceBalloonPreInit,
181 VBoxServiceBalloonOption,
182 VBoxServiceBalloonInit,
183 VBoxServiceBalloonWorker,
184 VBoxServiceBalloonStop,
185 VBoxServiceBalloonTerm
186};
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