VBoxServiceTimeSync.cpp revision 2451dc71335c34d9049c17bb031722a93df233f8
/** $Id$ */
/** @file
* VBoxService - Guest Additions TimeSync Service.
*/
/*
* Copyright (C) 2007 innotek GmbH
*
* 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.
*/
/** @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).
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifdef RT_OS_WINDOWS
#else
# include <unistd.h>
# include <errno.h>
# include <time.h>
#endif
#include <iprt/semaphore.h>
#include <VBox/VBoxGuest.h>
#include "VBoxServiceInternal.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 */
/** The semaphore we're blocking on. */
/** @copydoc VBOXSERVICE::pfnPreInit */
static DECLCALLBACK(int) VBoxServiceTimeSyncPreInit(void)
{
return VINF_SUCCESS;
}
/** @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);
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)
return rc;
}
/** @copydoc VBOXSERVICE::pfnWorker */
{
char sz[64];
int rc;
unsigned cErrors = 0;
for (;;)
{
/*
* Try get a reliable time reading.
*/
int cTries = 3;
do
{
/* query it. */
if (RT_FAILURE(rc2))
{
if (cErrors++ < 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);
}
{
/*
* The drift is to big, we have to make adjustments. :-/
* If we've got adjtime around, try that first - most
* *NIX systems have it. Fall back on settimeofday.
*/
#ifdef RT_OS_WINDOWS
/* just make sure it compiles for now, but later:
SetSystemTimeAdjustment and fall back on SetSystemTime.
*/
#else
# if !defined(RT_OS_OS2) /* PORTME */
{
if (g_cVerbosity >= 1)
cErrors = 0;
}
else
# endif
{
errno = 0;
{
{
if (g_cVerbosity >= 1)
# ifdef DEBUG
if (g_cVerbosity >= 3)
# endif
cErrors = 0;
}
else if (cErrors++ < 10)
}
else if (cErrors++ < 10)
}
#endif /* !RT_OS_WINDOWS */
}
break;
}
RTThreadSleep(1000);
} while (--cTries > 0);
/*
* 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)
{
}
/**
* The 'timesync' service description.
*/
{
/* pszName. */
"timesync",
/* pszDescription. */
"Time synchronization",
/* pszUsage. */
"[--timesync-interval <ms>] [--timesync-min-adjust <ms>] "
"[--timesync-latency-factor <x>] [--time-sync-max-latency <ms>]"
,
/* 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 The minimum absolute drift drift value measured\n"
" in milliseconds to make adjustments for.\n"
" The default is 1000 ms on OS/2 and 100 ms elsewhere.\n"
" --timesync-latency-factor The factor to multiply the time query latency\n"
" with to calculate the dynamic minimum adjust time.\n"
" The default is 8 times.\n"
" --timesync-max-latency The max host timer query latency to accpet.\n"
" The default is 250 ms.\n"
,
/* methods */
};