VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxService/VBoxMemBalloon.cpp@ 4686

Last change on this file since 4686 was 4686, checked in by vboxsync, 17 years ago

Adjusted timeout. VBoxGuest used to wait 10 times longer than it should.

File size: 5.2 KB
Line 
1/** @file
2 *
3 * VBoxMemBalloon - Memory balloon notification
4 *
5 */
6
7/*
8 * Copyright (C) 2006-2007 innotek GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18#define _WIN32_WINNT 0x0500
19#include <windows.h>
20#include <psapi.h>
21#include "VBoxService.h"
22#include "VBoxMemBalloon.h"
23#include <VBoxDisplay.h>
24#include <VBox/VBoxDev.h>
25#include <VBoxGuestInternal.h>
26#include <iprt/assert.h>
27#include "helpers.h"
28#include <winternl.h>
29
30typedef struct _VBOXMEMBALLOONCONTEXT
31{
32 const VBOXSERVICEENV *pEnv;
33 uint32_t uMemBalloonSize;
34} VBOXMEMBALLOONCONTEXT;
35
36
37static VBOXMEMBALLOONCONTEXT gCtx = {0};
38
39
40int VBoxMemBalloonInit(const VBOXSERVICEENV *pEnv, void **ppInstance, bool *pfStartThread)
41{
42 HANDLE gVBoxDriver = pEnv->hDriver;
43 DWORD cbReturned;
44
45 dprintf(("VBoxMemBalloonInit\n"));
46
47 gCtx.pEnv = pEnv;
48 gCtx.uMemBalloonSize = 0;
49
50 /* Check balloon size */
51 DWORD dwMemBalloonSize;
52 if (DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_CTL_CHECK_BALLOON, NULL, 0, &dwMemBalloonSize, sizeof(dwMemBalloonSize), &cbReturned, NULL))
53 {
54 dprintf(("VBoxMemBalloonInit: new balloon size % MB\n", dwMemBalloonSize));
55 gCtx.uMemBalloonSize = dwMemBalloonSize;
56 }
57 else
58 dprintf(("VBoxMemBalloonInit: DeviceIoControl (balloon) failed with %d\n", GetLastError()));
59
60 *pfStartThread = true;
61 *ppInstance = &gCtx;
62 return VINF_SUCCESS;
63}
64
65
66void VBoxMemBalloonDestroy(const VBOXSERVICEENV *pEnv, void *pInstance)
67{
68 dprintf(("VBoxMemBalloonDestroy\n"));
69 return;
70}
71
72uint32_t VBoxMemBalloonQuerySize()
73{
74 return gCtx.uMemBalloonSize;
75}
76
77/**
78 * Thread function to wait for and process seamless mode change
79 * requests
80 */
81unsigned __stdcall VBoxMemBalloonThread(void *pInstance)
82{
83 VBOXMEMBALLOONCONTEXT *pCtx = (VBOXMEMBALLOONCONTEXT *)pInstance;
84 HANDLE gVBoxDriver = pCtx->pEnv->hDriver;
85 bool fTerminate = false;
86 VBoxGuestFilterMaskInfo maskInfo;
87 DWORD cbReturned;
88
89 maskInfo.u32OrMask = VMMDEV_EVENT_BALLOON_CHANGE_REQUEST;
90 maskInfo.u32NotMask = 0;
91 if (DeviceIoControl (gVBoxDriver, IOCTL_VBOXGUEST_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
92 {
93 dprintf(("VBoxMemBalloonThread: DeviceIOControl(CtlMask - or) succeeded\n"));
94 }
95 else
96 {
97 dprintf(("VBoxMemBalloonThread: DeviceIOControl(CtlMask) failed, SeamlessChangeThread exited\n"));
98 return 0;
99 }
100
101 do
102 {
103 /* wait for a seamless change event */
104 VBoxGuestWaitEventInfo waitEvent;
105 waitEvent.u32TimeoutIn = 5000;
106 waitEvent.u32EventMaskIn = VMMDEV_EVENT_BALLOON_CHANGE_REQUEST;
107 if (DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_WAITEVENT, &waitEvent, sizeof(waitEvent), &waitEvent, sizeof(waitEvent), &cbReturned, NULL))
108 {
109 dprintf(("VBoxMemBalloonThread: DeviceIOControl succeded\n"));
110
111 /* are we supposed to stop? */
112 if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 0) == WAIT_OBJECT_0)
113 break;
114
115 dprintf(("VBoxMemBalloonThread: checking event\n"));
116
117 /* did we get the right event? */
118 if (waitEvent.u32EventFlagsOut & VMMDEV_EVENT_BALLOON_CHANGE_REQUEST)
119 {
120 DWORD dwMemBalloonSize;
121 if (DeviceIoControl(gVBoxDriver, IOCTL_VBOXGUEST_CTL_CHECK_BALLOON, NULL, 0, &dwMemBalloonSize, sizeof(dwMemBalloonSize), &cbReturned, NULL))
122 {
123 dprintf(("VBoxMemBalloonThread: new balloon size % MB\n", dwMemBalloonSize));
124 pCtx->uMemBalloonSize = dwMemBalloonSize;
125 }
126 else
127 dprintf(("VBoxMemBalloonThread: DeviceIoControl (balloon) failed with %d\n", GetLastError()));
128 }
129 }
130 else
131 {
132 dprintf(("VBoxMemBalloonThread: error 0 from DeviceIoControl IOCTL_VBOXGUEST_WAITEVENT\n"));
133
134 /* sleep a bit to not eat too much CPU in case the above call always fails */
135 if (WaitForSingleObject(pCtx->pEnv->hStopEvent, 10) == WAIT_OBJECT_0)
136 {
137 fTerminate = true;
138 break;
139 }
140 }
141 }
142 while (!fTerminate);
143
144 maskInfo.u32OrMask = 0;
145 maskInfo.u32NotMask = VMMDEV_EVENT_BALLOON_CHANGE_REQUEST;
146 if (DeviceIoControl (gVBoxDriver, IOCTL_VBOXGUEST_CTL_FILTER_MASK, &maskInfo, sizeof (maskInfo), NULL, 0, &cbReturned, NULL))
147 {
148 dprintf(("VBoxMemBalloonThread: DeviceIOControl(CtlMask - not) succeeded\n"));
149 }
150 else
151 {
152 dprintf(("VBoxMemBalloonThread: DeviceIOControl(CtlMask) failed\n"));
153 }
154
155 dprintf(("VBoxMemBalloonThread: finished mem balloon change request thread\n"));
156 return 0;
157}
158
159
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