VBoxServiceBalloon.cpp revision ac50ca1fcadfbac385a203cb6d32d2cd1295ebc1
/* $Id$ */
/** @file
* VBoxService - Memory Ballooning.
*/
/*
* Copyright (C) 2006-2010 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/semaphore.h>
#include <VBox/VBoxGuestLib.h>
#include "VBoxServiceInternal.h"
#include "VBoxServiceUtils.h"
#ifdef RT_OS_LINUX
# ifndef MADV_DONTFORK
# define MADV_DONTFORK 10
# endif
#endif
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The balloon size. */
static uint32_t g_cMemBalloonChunks = 0;
/** The semaphore we're blocking on. */
/** The array holding the R3 pointers of the balloon. */
static void **g_pavBalloon = NULL;
/** True = madvise(MADV_DONTFORK) works, false otherwise. */
static bool g_fSysMadviseWorks;
/**
* Check weather madvise() works.
*/
static void VBoxServiceBalloonInitMadvise(void)
{
#ifdef RT_OS_LINUX
#endif
}
/**
* Allocate a chunk of the balloon. Fulfil the prerequisite that we can lock this memory
* and protect it against fork() in R0. See also suplibOsPageAlloc().
*/
static void* VBoxServiceBalloonAllocChunk(void)
{
char *pu8;
#ifdef RT_OS_LINUX
if (!g_fSysMadviseWorks)
if (pu8 == MAP_FAILED)
return NULL;
if (g_fSysMadviseWorks)
{
/*
* It is not fatal if we fail here but a forked child (e.g. the ALSA sound server)
* could crash. Linux < 2.6.16 does not implement madvise(MADV_DONTFORK) but the
* kernel seems to split bigger VMAs and that is all that we want -- later we set the
* VM_DONTCOPY attribute in supdrvOSLockMemOne().
*/
}
else
{
/*
* madvise(MADV_DONTFORK) is not available (most probably Linux 2.4). Enclose any
* mmapped region by two unmapped pages to guarantee that there is exactly one VM
* area struct of the very same size as the mmap area.
*/
}
#else
if (!pu8)
return pu8;
#endif
return pu8;
}
/**
* Free an allocated chunk undoing VBoxServiceBalloonAllocChunk().
*/
static void VBoxServiceBalloonFreeChunk(void *pv)
{
#ifdef RT_OS_LINUX
if (!g_fSysMadviseWorks)
{
/* This is not really necessary */
}
#else
#endif
}
/**
* Adapt the R0 memory balloon by granting/reclaiming 1MB chunks to/from R0.
*
* returns IPRT status code.
* @param cNewChunks The new number of 1MB chunks in the balloon.
*/
{
if (cNewChunks == g_cMemBalloonChunks)
return VINF_SUCCESS;
VBoxServiceVerbose(3, "VBoxServiceBalloonSetUser: cNewChunks=%u g_cMemBalloonChunks=%u\n", cNewChunks, g_cMemBalloonChunks);
int rc = VINF_SUCCESS;
if (cNewChunks > g_cMemBalloonChunks)
{
/* inflate */
uint32_t i;
for (i = g_cMemBalloonChunks; i < cNewChunks; i++)
{
void *pv = VBoxServiceBalloonAllocChunk();
if (!pv)
break;
if (RT_SUCCESS(rc))
{
g_pavBalloon[i] = pv;
#ifndef RT_OS_SOLARIS
/*
* Protect against access by dangling pointers (ignore errors as it may fail).
* On Solaris it corrupts the address space leaving the process unkillable. This
* could perhaps be related to what the underlying segment driver does; currently
* just disable it.
*/
#endif
}
else
{
break;
}
}
}
else
{
/* deflate */
uint32_t i;
for (i = g_cMemBalloonChunks; i-- > cNewChunks;)
{
void *pv = g_pavBalloon[i];
if (RT_SUCCESS(rc))
{
#ifndef RT_OS_SOLARIS
/* unprotect */
#endif
g_pavBalloon[i] = NULL;
}
else
break;
}
}
return VINF_SUCCESS;
}
/** @copydoc VBOXSERVICE::pfnPreInit */
static DECLCALLBACK(int) VBoxServiceBalloonPreInit(void)
{
return VINF_SUCCESS;
}
/** @copydoc VBOXSERVICE::pfnOption */
static DECLCALLBACK(int) VBoxServiceBalloonOption(const char **ppszShort, int argc, char **argv, int *pi)
{
return VINF_SUCCESS;
}
/** @copydoc VBOXSERVICE::pfnInit */
static DECLCALLBACK(int) VBoxServiceBalloonInit(void)
{
g_cMemBalloonChunks = 0;
uint32_t cNewChunks = 0;
bool fHandleInR3;
/* Check balloon size */
if (RT_SUCCESS(rc))
{
if (fHandleInR3)
else
}
else
/* We shouldn't fail here if ballooning isn't available. This can have several reasons,
* for instance, host too old (which is not that fatal). */
return VINF_SUCCESS;
}
/**
* Query the size of the memory balloon, given as a page count.
*
* @returns Number of pages.
* @param cbPage The page size.
*/
{
}
/** @copydoc VBOXSERVICE::pfnWorker */
{
/* Start monitoring of the stat event change event. */
if (RT_FAILURE(rc))
{
return rc;
}
/*
* Tell the control thread that it can continue
* spawning services.
*/
/*
* Now enter the loop retrieving runtime data continuously.
*/
for (;;)
{
/* Check if an update interval change is pending. */
if ( RT_SUCCESS(rc)
{
bool fHandleInR3;
if (RT_SUCCESS(rc))
{
if (fHandleInR3)
{
if (RT_FAILURE(rc))
{
}
else
VBoxServiceVerbose(3, "VBoxServiceBalloonWorker: successfully set requested balloon size %d.\n", cNewChunks);
}
else
}
else
}
/*
* Block for a while.
*
* The event semaphore takes care of ignoring interruptions and it
* allows us to implement service wakeup later.
*/
if (*pfShutdown)
break;
if (*pfShutdown)
break;
{
break;
}
}
/* Cancel monitoring of the memory balloon change event. */
if (RT_FAILURE(rc))
return 0;
}
/** @copydoc VBOXSERVICE::pfnTerm */
static DECLCALLBACK(void) VBoxServiceBalloonTerm(void)
{
return;
}
/** @copydoc VBOXSERVICE::pfnStop */
static DECLCALLBACK(void) VBoxServiceBalloonStop(void)
{
}
/**
* The 'memballoon' service description.
*/
{
/* pszName. */
"memballoon",
/* pszDescription. */
"Memory Ballooning",
/* pszUsage. */
NULL,
/* pszOptions. */
NULL,
/* methods */
};