VBoxServiceTimeSync.cpp revision bc6df168670cfc0df5ae3be50ccbf098ba2cebc7
/* $Id$ */
/** @file
* VBoxService - Guest Additions TimeSync Service.
*/
/*
* Copyright (C) 2007 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.
*/
/** @page pg_vboxservice_timesync The Time Sync Service
*
* The time sync service plays along with the Time Manager (TM) in the VMM
* to keep the guest time accurate using the host machine as reference.
* TM will try its best to make sure all timer ticks gets delivered so that
* there isn't normally any need to adjust the guest time.
*
* There are three normal (= acceptable) cases:
* -# When the service starts up. This is because ticks and such might
* be lost during VM and OS startup. (Need to figure out exactly why!)
* -# When the TM is unable to deliver all the ticks and swallows a
* backlog of ticks. The threshold for this is configurable with
* a default of 60 seconds.
* -# The time is adjusted on the host. This can be caused manually by
* the user or by some time sync daemon (NTP, LAN server, etc.).
*
* There are a number of very odd case where adjusting is needed. Here
* are some of them:
* -# Timer device emulation inaccurancies (like rounding).
* -# Inaccurancies in time source VirtualBox uses.
*
* The TM is our source for the host time and will make adjustments for
* current timer delivery lag. The simplistic approach taken by TM is to
* adjust the host time by the current guest timer delivery lag, meaning that
* in the guest wall time as well.
*
* Now, there is any amount of trouble we can cause by changing the time.
* Most applications probably uses the wall time when they need to measure
* things. A walltime that is being juggled about every so often, even if just
* a little bit, could occationally upset these measurements by for instance
* yielding negative results.
*
* This bottom line here is that the time sync service isn't really supposed
* to do anything and will try avoid having to do anything when possible.
*
* The implementation uses the latency it takes to query host time as the
* absolute maximum precision to avoid messing up under timer tick catchup
* thru the inner EM loop with it's force action processing.)
*
* But this latency has to be measured from our perspective, which means it
* could just as easily come out as 0. (OS/2 and Windows guest only updates
* the current time when the timer ticks for instance.) The good thing is
* that this isn't really a problem since we won't ever do anything unless
* the drift is noticable.
*
* It now boils down to these three (configuration) factors:
* -# g_TimesyncMinAdjust - The minimum drift we will ever bother with.
* -# g_TimesyncLatencyFactor - The factor we multiply the latency by to
* calculate the dynamic minimum adjust factor.
* -# g_TimesyncMaxLatency - When to start discarding the data as utterly
* useless and take a rest (someone is too busy to give us good data).
* -# g_TimeSyncSetThreshold - The threshold at which we will just set the time
* instead of trying to adjust it (milliseconds).
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef RT_OS_WINDOWS
# include <Windows.h>
#else
# include <unistd.h>
# include <errno.h>
# include <time.h>
#endif
#include <iprt/semaphore.h>
#include <VBox/VBoxGuestLib.h>
#include "VBoxServiceInternal.h"
#include "VBoxServiceUtils.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The timesync interval (millseconds). */
/**
* @see pg_vboxservice_timesync
*
* @remark OS/2: There is either a 1 second resolution on the DosSetDateTime
* API or a but in the settimeofday implementation. Thus, don't
* bother unless there is at least a 1 second drift.
*/
#ifdef RT_OS_OS2
#else
#endif
/** @see pg_vboxservice_timesync */
/** @see pg_vboxservice_timesync */
/** @see pg_vboxservice_timesync */
/** Whether the next adjustment should just set the time instead of trying to
* adjust it. This is used to implement --timesync-set-start. */
static bool volatile g_fTimeSyncSetNext = false;
/** Current error count. Used to knowing when to bitch and when not to. */
static uint32_t g_cTimeSyncErrors = 0;
/** The semaphore we're blocking on. */
#ifdef RT_OS_WINDOWS
/** Process token. */
/** Old token privileges. */
static TOKEN_PRIVILEGES g_TkOldPrivileges;
/** Backup values for time adjustment. */
static DWORD g_dwWinTimeAdjustment;
static DWORD g_dwWinTimeIncrement;
static BOOL g_bWinTimeAdjustmentDisabled;
#endif
/** @copydoc VBOXSERVICE::pfnPreInit */
static DECLCALLBACK(int) VBoxServiceTimeSyncPreInit(void)
{
#ifdef VBOX_WITH_GUEST_PROPS
/** @todo Merge this function with VBoxServiceTimeSyncOption() to generalize
* the "command line args override guest property values" behavior.
/*
* Read the service options from the VM's guest properties.
* Note that these options can be overriden by the command line options later.
*/
if (RT_FAILURE(rc))
{
}
else
{
rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-interval",
if ( RT_SUCCESS(rc)
|| rc == VERR_NOT_FOUND)
{
rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-min-adjust",
&g_TimeSyncMinAdjust, 0, 3600000);
}
if ( RT_SUCCESS(rc)
|| rc == VERR_NOT_FOUND)
{
rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-latency-factor",
}
if ( RT_SUCCESS(rc)
|| rc == VERR_NOT_FOUND)
{
rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-max-latency",
}
if ( RT_SUCCESS(rc)
|| rc == VERR_NOT_FOUND)
{
rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold",
}
if ( RT_SUCCESS(rc)
|| rc == VERR_NOT_FOUND)
{
char *pszValue;
rc = VBoxServiceReadProp(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-start",
if (RT_SUCCESS(rc))
{
g_fTimeSyncSetNext = true;
}
}
}
rc = VINF_SUCCESS;
return rc;
#else
/* Nothing to do here yet. */
return VINF_SUCCESS;
#endif
}
/** @copydoc VBOXSERVICE::pfnOption */
static DECLCALLBACK(int) VBoxServiceTimeSyncOption(const char **ppszShort, int argc, char **argv, int *pi)
{
int rc = -1;
if (ppszShort)
/* no short options */;
&g_TimeSyncMinAdjust, 0, 3600000);
{
g_fTimeSyncSetNext = true;
rc = VINF_SUCCESS;
}
return rc;
}
/** @copydoc VBOXSERVICE::pfnInit */
static DECLCALLBACK(int) VBoxServiceTimeSyncInit(void)
{
/*
* If not specified, find the right interval default.
* Then create the event sem to block on.
*/
if (!g_TimeSyncInterval)
if (!g_TimeSyncInterval)
#ifdef RT_OS_WINDOWS
if (RT_SUCCESS(rc))
{
/*
* Adjust priviledges of this process so we can make system time adjustments.
*/
{
{
if (AdjustTokenPrivileges(g_hTokenProcess, FALSE, &tkPriv, sizeof(TOKEN_PRIVILEGES), &g_TkOldPrivileges, &cbRet))
rc = VINF_SUCCESS;
else
{
VBoxServiceError("Adjusting token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
}
}
else
{
VBoxServiceError("Looking up token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
}
if (RT_FAILURE(rc))
{
}
}
else
{
VBoxServiceError("Opening process token (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n", dwErr, rc);
}
}
if (GetSystemTimeAdjustment(&g_dwWinTimeAdjustment, &g_dwWinTimeIncrement, &g_bWinTimeAdjustmentDisabled))
VBoxServiceVerbose(3, "Windows time adjustment: Initially %ld (100ns) units per %ld (100 ns) units interval, disabled=%d\n",
else
{
}
#endif /* RT_OS_WINDOWS */
return rc;
}
/**
* Try adjust the time using adjtime or similar.
*
* @returns true on success, false on failure.
*
* @param pDrift The time adjustment.
*/
{
#ifdef RT_OS_WINDOWS
/** @todo r=bird: NT4 doesn't have GetSystemTimeAdjustment. */
return false;
if (GetSystemTimeAdjustment(&dwWinTimeAdjustment, &dwWinTimeIncrement, &fWinTimeAdjustmentDisabled))
{
if (RTTimeSpecGetMilli(pDrift) > 0)
{
{
}
}
else
{
{
}
}
VBoxServiceVerbose(3, "Windows time adjustment: OrgTA=%ld, CurTA=%ld, NewTA=%ld, DiffNew=%ld, DiffMax=%ld\n",
{
g_cTimeSyncErrors = 0;
return true;
}
if (g_cTimeSyncErrors++ < 10)
}
else if (g_cTimeSyncErrors++ < 10)
/* No API for doing gradual time adjustments. */
#else /* PORTME */
/*
* Try use adjtime(), most unix-like systems have this.
*/
{
if (g_cVerbosity >= 1)
g_cTimeSyncErrors = 0;
return true;
}
#endif
/* failed */
return false;
}
/**
* Cancels any pending time adjustment.
*
* Called when we've caught up and before calls to VBoxServiceTimeSyncSet.
*/
static void VBoxServiceTimeSyncCancelAdjust(void)
{
#ifdef RT_OS_WINDOWS
else if (g_cTimeSyncErrors++ < 10)
#endif /* !RT_OS_WINDOWS */
}
/**
* Try adjust the time using adjtime or similar.
*
* @returns true on success, false on failure.
*
* @param pDrift The time adjustment.
* @param pHostNow The host time at the time of the host query.
* REMOVE THIS ARGUMENT!
*/
{
/*
* Query the current time, add the adjustment, then try it.
*/
#ifdef RT_OS_WINDOWS
/** @todo r=bird: Get current time and add the adjustment, the host time is
* stale by now. */
{
if (!SetSystemTime(&st))
}
else
#else /* !RT_OS_WINDOWS */
errno = 0;
{
{
char sz[64];
if (g_cVerbosity >= 1)
# ifdef DEBUG
if (g_cVerbosity >= 3)
# endif
g_cTimeSyncErrors = 0;
}
else if (g_cTimeSyncErrors++ < 10)
}
else if (g_cTimeSyncErrors++ < 10)
#endif /* !RT_OS_WINDOWS */
}
/** @copydoc VBOXSERVICE::pfnWorker */
{
char sz[64];
int rc = VINF_SUCCESS;
/*
* Tell the control thread that it can continue spawning services.
*/
/*
* The Work Loop.
*/
for (;;)
{
/*
* Try get a reliable time reading.
*/
int cTries = 3;
do
{
/* query it. */
if (RT_FAILURE(rc2))
{
if (g_cTimeSyncErrors++ < 10)
break;
}
/* calc latency and check if it's ok. */
{
/*
* Calculate the adjustment threshold and the current drift.
*/
if (MinAdjust < g_TimeSyncMinAdjust)
if (RTTimeSpecGetMilli(&Drift) < 0)
if (g_cVerbosity >= 3)
{
&Drift);
}
if (AbsDriftMilli > MinAdjust)
{
/*
* Ok, the drift is above the threshold.
*
* Try a gradual adjustment first, if that fails or the drift is
* too big, fall back on just setting the time.
*/
|| !VBoxServiceTimeSyncAdjust(&Drift))
{
}
}
else
break;
}
RTThreadSleep(1000);
} while (--cTries > 0);
g_fTimeSyncSetNext = false;
/*
* 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;
}
}
return rc;
}
/** @copydoc VBOXSERVICE::pfnStop */
static DECLCALLBACK(void) VBoxServiceTimeSyncStop(void)
{
}
/** @copydoc VBOXSERVICE::pfnTerm */
static DECLCALLBACK(void) VBoxServiceTimeSyncTerm(void)
{
#ifdef RT_OS_WINDOWS
/*
* Restore the SE_SYSTEMTIME_NAME token privileges (if init succeeded).
*/
if (g_hTokenProcess)
{
if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &g_TkOldPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
{
}
}
#endif /* !RT_OS_WINDOWS */
if (g_TimeSyncEvent != NIL_RTSEMEVENTMULTI)
{
}
}
/**
* The 'timesync' service description.
*/
{
/* pszName. */
"timesync",
/* pszDescription. */
"Time synchronization",
/* pszUsage. */
"[--timesync-interval <ms>] [--timesync-min-adjust <ms>] "
"[--timesync-latency-factor <x>] [--timesync-max-latency <ms>]"
"[--timesync-set-threshold <ms>] [--timesync-set-start]"
,
/* pszOptions. */
" --timesync-interval Specifies the interval at which to synchronize the\n"
" time with the host. The default is 10000 ms.\n"
" --timesync-min-adjust\n"
" The minimum absolute drift value measured in\n"
" milliseconds to make adjustments for.\n"
" The default is 1000 ms on OS/2 and 100 ms elsewhere.\n"
" --timesync-latency-factor\n"
" The factor to multiply the time query latency with to\n"
" calculate the dynamic minimum adjust time.\n"
" The default is 8 times.\n"
" --timesync-max-latency\n"
" The max host timer query latency to accept.\n"
" The default is 250 ms.\n"
" --timesync-set-threshold\n"
" The absolute drift threshold, given as milliseconds,\n"
" where to start setting the time instead of trying to\n"
" adjust it. The default is 20 min.\n"
" --timesync-set-start\n"
" Set the time when starting the time sync service.\n"
,
/* methods */
};