VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceTimeSync.cpp@ 58029

Last change on this file since 58029 was 58029, checked in by vboxsync, 9 years ago

VBoxService: Using prefix 'VGSvc', code style/width cleanups. No real changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.8 KB
Line 
1/* $Id: VBoxServiceTimeSync.cpp 58029 2015-10-05 20:50:18Z vboxsync $ */
2/** @file
3 * VBoxService - Guest Additions TimeSync Service.
4 */
5
6/*
7 * Copyright (C) 2007-2015 Oracle Corporation
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
18
19/** @page pg_vboxservice_timesync The Time Sync Service
20 *
21 * The time sync service plays along with the Time Manager (TM) in the VMM
22 * to keep the guest time accurate using the host machine as reference.
23 * TM will try its best to make sure all timer ticks gets delivered so that
24 * there isn't normally any need to adjust the guest time.
25 *
26 * There are three normal (= acceptable) cases:
27 * -# When the service starts up. This is because ticks and such might
28 * be lost during VM and OS startup. (Need to figure out exactly why!)
29 * -# When the TM is unable to deliver all the ticks and swallows a
30 * backlog of ticks. The threshold for this is configurable with
31 * a default of 60 seconds.
32 * -# The time is adjusted on the host. This can be caused manually by
33 * the user or by some time sync daemon (NTP, LAN server, etc.).
34 *
35 * There are a number of very odd case where adjusting is needed. Here
36 * are some of them:
37 * -# Timer device emulation inaccuracies (like rounding).
38 * -# Inaccuracies in time source VirtualBox uses.
39 * -# The Guest and/or Host OS doesn't perform proper time keeping. This
40 * come about as a result of OS and/or hardware issues.
41 *
42 * The TM is our source for the host time and will make adjustments for
43 * current timer delivery lag. The simplistic approach taken by TM is to
44 * adjust the host time by the current guest timer delivery lag, meaning that
45 * if the guest is behind 1 second with PIT/RTC/++ ticks this should be reflected
46 * in the guest wall time as well.
47 *
48 * Now, there is any amount of trouble we can cause by changing the time.
49 * Most applications probably uses the wall time when they need to measure
50 * things. A walltime that is being juggled about every so often, even if just
51 * a little bit, could occasionally upset these measurements by for instance
52 * yielding negative results.
53 *
54 * This bottom line here is that the time sync service isn't really supposed
55 * to do anything and will try avoid having to do anything when possible.
56 *
57 * The implementation uses the latency it takes to query host time as the
58 * absolute maximum precision to avoid messing up under timer tick catchup
59 * and/or heavy host/guest load. (Rational is that a *lot* of stuff may happen
60 * on our way back from ring-3 and TM/VMMDev since we're taking the route
61 * thru the inner EM loop with it's force action processing.)
62 *
63 * But this latency has to be measured from our perspective, which means it
64 * could just as easily come out as 0. (OS/2 and Windows guest only updates
65 * the current time when the timer ticks for instance.) The good thing is
66 * that this isn't really a problem since we won't ever do anything unless
67 * the drift is noticeable.
68 *
69 * It now boils down to these three (configuration) factors:
70 * -# g_TimesyncMinAdjust - The minimum drift we will ever bother with.
71 * -# g_TimesyncLatencyFactor - The factor we multiply the latency by to
72 * calculate the dynamic minimum adjust factor.
73 * -# g_TimesyncMaxLatency - When to start discarding the data as utterly
74 * useless and take a rest (someone is too busy to give us good data).
75 * -# g_TimeSyncSetThreshold - The threshold at which we will just set the time
76 * instead of trying to adjust it (milliseconds).
77 */
78
79
80/*********************************************************************************************************************************
81* Header Files *
82*********************************************************************************************************************************/
83#ifdef RT_OS_WINDOWS
84# include <Windows.h>
85#else
86# include <unistd.h>
87# include <errno.h>
88# include <time.h>
89# include <sys/time.h>
90#endif
91
92#include <iprt/thread.h>
93#include <iprt/string.h>
94#include <iprt/semaphore.h>
95#include <iprt/time.h>
96#include <iprt/assert.h>
97#include <VBox/VBoxGuestLib.h>
98#include "VBoxServiceInternal.h"
99#include "VBoxServiceUtils.h"
100
101
102/*********************************************************************************************************************************
103* Global Variables *
104*********************************************************************************************************************************/
105/** The timesync interval (milliseconds). */
106static uint32_t g_TimeSyncInterval = 0;
107/**
108 * @see pg_vboxservice_timesync
109 *
110 * @remark OS/2: There is either a 1 second resolution on the DosSetDateTime
111 * API or a bug in my settimeofday implementation. Thus, don't
112 * bother unless there is at least a 1 second drift.
113 */
114#ifdef RT_OS_OS2
115static uint32_t g_TimeSyncMinAdjust = 1000;
116#else
117static uint32_t g_TimeSyncMinAdjust = 100;
118#endif
119/** @see pg_vboxservice_timesync */
120static uint32_t g_TimeSyncLatencyFactor = 8;
121/** @see pg_vboxservice_timesync */
122static uint32_t g_TimeSyncMaxLatency = 250;
123/** @see pg_vboxservice_timesync */
124static uint32_t g_TimeSyncSetThreshold = 20*60*1000;
125/** Whether the next adjustment should just set the time instead of trying to
126 * adjust it. This is used to implement --timesync-set-start. */
127static bool volatile g_fTimeSyncSetNext = false;
128/** Whether to set the time when the VM was restored. */
129static bool g_fTimeSyncSetOnRestore = true;
130
131/** Current error count. Used to knowing when to bitch and when not to. */
132static uint32_t g_cTimeSyncErrors = 0;
133
134/** The semaphore we're blocking on. */
135static RTSEMEVENTMULTI g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
136
137/** The VM session ID. Changes whenever the VM is restored or reset. */
138static uint64_t g_idTimeSyncSession;
139
140#ifdef RT_OS_WINDOWS
141/** Process token. */
142static HANDLE g_hTokenProcess = NULL;
143/** Old token privileges. */
144static TOKEN_PRIVILEGES g_TkOldPrivileges;
145/** Backup values for time adjustment. */
146static DWORD g_dwWinTimeAdjustment;
147static DWORD g_dwWinTimeIncrement;
148static BOOL g_bWinTimeAdjustmentDisabled;
149#endif
150
151
152/**
153 * @interface_method_impl{VBOXSERVICE,pfnPreInit}
154 */
155static DECLCALLBACK(int) vgsvcTimeSyncPreInit(void)
156{
157#ifdef VBOX_WITH_GUEST_PROPS
158 /** @todo Merge this function with vgsvcTimeSyncOption() to generalize
159 * the "command line args override guest property values" behavior. */
160
161 /*
162 * Read the service options from the VM's guest properties.
163 * Note that these options can be overridden by the command line options later.
164 */
165 uint32_t uGuestPropSvcClientID;
166 int rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
167 if (RT_FAILURE(rc))
168 {
169 if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
170 {
171 VGSvcVerbose(0, "VMInfo: Guest property service is not available, skipping\n");
172 rc = VINF_SUCCESS;
173 }
174 else
175 VGSvcError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
176 }
177 else
178 {
179 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-interval",
180 &g_TimeSyncInterval, 50, UINT32_MAX - 1);
181 if ( RT_SUCCESS(rc)
182 || rc == VERR_NOT_FOUND)
183 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-min-adjust",
184 &g_TimeSyncMinAdjust, 0, 3600000);
185 if ( RT_SUCCESS(rc)
186 || rc == VERR_NOT_FOUND)
187 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-latency-factor",
188 &g_TimeSyncLatencyFactor, 1, 1024);
189 if ( RT_SUCCESS(rc)
190 || rc == VERR_NOT_FOUND)
191 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-max-latency",
192 &g_TimeSyncMaxLatency, 1, 3600000);
193 if ( RT_SUCCESS(rc)
194 || rc == VERR_NOT_FOUND)
195 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold",
196 &g_TimeSyncSetThreshold, 0, 7*24*60*60*1000 /* a week */);
197 if ( RT_SUCCESS(rc)
198 || rc == VERR_NOT_FOUND)
199 {
200 char *pszValue;
201 rc = VGSvcReadProp(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-start",
202 &pszValue, NULL /* ppszFlags */, NULL /* puTimestamp */);
203 if (RT_SUCCESS(rc))
204 {
205 g_fTimeSyncSetNext = true;
206 RTStrFree(pszValue);
207 }
208 }
209 if ( RT_SUCCESS(rc)
210 || rc == VERR_NOT_FOUND)
211 {
212 uint32_t value;
213 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-on-restore",
214 &value, 1, 1);
215 if (RT_SUCCESS(rc))
216 g_fTimeSyncSetOnRestore = !!value;
217 }
218
219 VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
220 }
221
222 if (rc == VERR_NOT_FOUND) /* If a value is not found, don't be sad! */
223 rc = VINF_SUCCESS;
224 return rc;
225#else
226 /* Nothing to do here yet. */
227 return VINF_SUCCESS;
228#endif
229}
230
231
232/**
233 * @interface_method_impl{VBOXSERVICE,pfnOption}
234 */
235static DECLCALLBACK(int) vgsvcTimeSyncOption(const char **ppszShort, int argc, char **argv, int *pi)
236{
237 int rc = -1;
238 uint32_t value;
239 if (ppszShort)
240 /* no short options */;
241 else if (!strcmp(argv[*pi], "--timesync-interval"))
242 rc = VGSvcArgUInt32(argc, argv, "", pi, &g_TimeSyncInterval, 50, UINT32_MAX - 1);
243 else if (!strcmp(argv[*pi], "--timesync-min-adjust"))
244 rc = VGSvcArgUInt32(argc, argv, "", pi, &g_TimeSyncMinAdjust, 0, 3600000);
245 else if (!strcmp(argv[*pi], "--timesync-latency-factor"))
246 rc = VGSvcArgUInt32(argc, argv, "", pi, &g_TimeSyncLatencyFactor, 1, 1024);
247 else if (!strcmp(argv[*pi], "--timesync-max-latency"))
248 rc = VGSvcArgUInt32(argc, argv, "", pi, &g_TimeSyncMaxLatency, 1, 3600000);
249 else if (!strcmp(argv[*pi], "--timesync-set-threshold"))
250 rc = VGSvcArgUInt32(argc, argv, "", pi, &g_TimeSyncSetThreshold, 0, 7*24*60*60*1000); /* a week */
251 else if (!strcmp(argv[*pi], "--timesync-set-start"))
252 {
253 g_fTimeSyncSetNext = true;
254 rc = VINF_SUCCESS;
255 }
256 else if (!strcmp(argv[*pi], "--timesync-set-on-restore"))
257 {
258 rc = VGSvcArgUInt32(argc, argv, "", pi, &value, 1, 1);
259 if (RT_SUCCESS(rc))
260 g_fTimeSyncSetOnRestore = !!value;
261 }
262
263 return rc;
264}
265
266
267/**
268 * @interface_method_impl{VBOXSERVICE,pfnInit}
269 */
270static DECLCALLBACK(int) vgsvcTimeSyncInit(void)
271{
272 /*
273 * If not specified, find the right interval default.
274 * Then create the event sem to block on.
275 */
276 if (!g_TimeSyncInterval)
277 g_TimeSyncInterval = g_DefaultInterval * 1000;
278 if (!g_TimeSyncInterval)
279 g_TimeSyncInterval = 10 * 1000;
280
281 VbglR3GetSessionId(&g_idTimeSyncSession);
282 /* The status code is ignored as this information is not available with VBox < 3.2.10. */
283
284 int rc = RTSemEventMultiCreate(&g_TimeSyncEvent);
285 AssertRC(rc);
286#ifdef RT_OS_WINDOWS
287 if (RT_SUCCESS(rc))
288 {
289 /*
290 * Adjust privileges of this process so we can make system time adjustments.
291 */
292 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &g_hTokenProcess))
293 {
294 TOKEN_PRIVILEGES tkPriv;
295 RT_ZERO(tkPriv);
296 tkPriv.PrivilegeCount = 1;
297 tkPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
298 if (LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkPriv.Privileges[0].Luid))
299 {
300 DWORD cbRet = sizeof(g_TkOldPrivileges);
301 if (AdjustTokenPrivileges(g_hTokenProcess, FALSE, &tkPriv, sizeof(TOKEN_PRIVILEGES), &g_TkOldPrivileges, &cbRet))
302 rc = VINF_SUCCESS;
303 else
304 {
305 DWORD dwErr = GetLastError();
306 rc = RTErrConvertFromWin32(dwErr);
307 VGSvcError("vgsvcTimeSyncInit: Adjusting token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n",
308 dwErr, rc);
309 }
310 }
311 else
312 {
313 DWORD dwErr = GetLastError();
314 rc = RTErrConvertFromWin32(dwErr);
315 VGSvcError("vgsvcTimeSyncInit: Looking up token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n",
316 dwErr, rc);
317 }
318 if (RT_FAILURE(rc))
319 {
320 CloseHandle(g_hTokenProcess);
321 g_hTokenProcess = NULL;
322 }
323 }
324 else
325 {
326 DWORD dwErr = GetLastError();
327 rc = RTErrConvertFromWin32(dwErr);
328 VGSvcError("vgsvcTimeSyncInit: Opening process token (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n",
329 dwErr, rc);
330 g_hTokenProcess = NULL;
331 }
332 }
333
334 if (GetSystemTimeAdjustment(&g_dwWinTimeAdjustment, &g_dwWinTimeIncrement, &g_bWinTimeAdjustmentDisabled))
335 VGSvcVerbose(3, "vgsvcTimeSyncInit: Initially %ld (100ns) units per %ld (100 ns) units interval, disabled=%d\n",
336 g_dwWinTimeAdjustment, g_dwWinTimeIncrement, g_bWinTimeAdjustmentDisabled ? 1 : 0);
337 else
338 {
339 DWORD dwErr = GetLastError();
340 rc = RTErrConvertFromWin32(dwErr);
341 VGSvcError("vgsvcTimeSyncInit: Could not get time adjustment values! Last error: %ld!\n", dwErr);
342 }
343#endif /* RT_OS_WINDOWS */
344
345 return rc;
346}
347
348
349/**
350 * Try adjust the time using adjtime or similar.
351 *
352 * @returns true on success, false on failure.
353 *
354 * @param pDrift The time adjustment.
355 */
356static bool vgsvcTimeSyncAdjust(PCRTTIMESPEC pDrift)
357{
358#ifdef RT_OS_WINDOWS
359/** @todo r=bird: g_hTokenProcess cannot be NULL here.
360 * vgsvcTimeSyncInit will fail and the service will not be started with
361 * it being NULL. vgsvcTimeSyncInit OTOH will *NOT* be called until the
362 * service thread has terminated. If anything
363 * else is the case, there is buggy code somewhere.*/
364 if (g_hTokenProcess == NULL) /* Is the token already closed when shutting down? */
365 return false;
366
367 DWORD dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwWinTimeIncrement;
368 BOOL fWinTimeAdjustmentDisabled;
369 if (GetSystemTimeAdjustment(&dwWinTimeAdjustment, &dwWinTimeIncrement, &fWinTimeAdjustmentDisabled))
370 {
371 DWORD dwDiffMax = g_dwWinTimeAdjustment * 0.50;
372 DWORD dwDiffNew = dwWinTimeAdjustment * 0.10;
373
374 if (RTTimeSpecGetMilli(pDrift) > 0)
375 {
376 dwWinNewTimeAdjustment = dwWinTimeAdjustment + dwDiffNew;
377 if (dwWinNewTimeAdjustment > (g_dwWinTimeAdjustment + dwDiffMax))
378 {
379 dwWinNewTimeAdjustment = g_dwWinTimeAdjustment + dwDiffMax;
380 dwDiffNew = dwDiffMax;
381 }
382 }
383 else
384 {
385 dwWinNewTimeAdjustment = dwWinTimeAdjustment - dwDiffNew;
386 if (dwWinNewTimeAdjustment < (g_dwWinTimeAdjustment - dwDiffMax))
387 {
388 dwWinNewTimeAdjustment = g_dwWinTimeAdjustment - dwDiffMax;
389 dwDiffNew = dwDiffMax;
390 }
391 }
392
393 VGSvcVerbose(3, "vgsvcTimeSyncAdjust: Drift=%lldms\n", RTTimeSpecGetMilli(pDrift));
394 VGSvcVerbose(3, "vgsvcTimeSyncAdjust: OrgTA=%ld, CurTA=%ld, NewTA=%ld, DiffNew=%ld, DiffMax=%ld\n",
395 g_dwWinTimeAdjustment, dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwDiffNew, dwDiffMax);
396 if (SetSystemTimeAdjustment(dwWinNewTimeAdjustment, FALSE /* Periodic adjustments enabled. */))
397 {
398 g_cTimeSyncErrors = 0;
399 return true;
400 }
401
402 if (g_cTimeSyncErrors++ < 10)
403 VGSvcError("vgsvcTimeSyncAdjust: SetSystemTimeAdjustment failed, error=%u\n", GetLastError());
404 }
405 else if (g_cTimeSyncErrors++ < 10)
406 VGSvcError("vgsvcTimeSyncAdjust: GetSystemTimeAdjustment failed, error=%ld\n", GetLastError());
407
408#elif defined(RT_OS_OS2) || defined(RT_OS_HAIKU)
409 /* No API for doing gradual time adjustments. */
410
411#else /* PORTME */
412 /*
413 * Try use adjtime(), most unix-like systems have this.
414 */
415 struct timeval tv;
416 RTTimeSpecGetTimeval(pDrift, &tv);
417 if (adjtime(&tv, NULL) == 0)
418 {
419 if (g_cVerbosity >= 1)
420 VGSvcVerbose(1, "vgsvcTimeSyncAdjust: adjtime by %RDtimespec\n", pDrift);
421 g_cTimeSyncErrors = 0;
422 return true;
423 }
424#endif
425
426 /* failed */
427 return false;
428}
429
430
431/**
432 * Cancels any pending time adjustment.
433 *
434 * Called when we've caught up and before calls to vgsvcTimeSyncSet.
435 */
436static void vgsvcTimeSyncCancelAdjust(void)
437{
438#ifdef RT_OS_WINDOWS
439/** @todo r=bird: g_hTokenProcess cannot be NULL here. See argumentation in
440 * vgsvcTimeSyncAdjust. */
441 if (g_hTokenProcess == NULL) /* No process token (anymore)? */
442 return;
443 if (SetSystemTimeAdjustment(0, TRUE /* Periodic adjustments disabled. */))
444 VGSvcVerbose(3, "vgsvcTimeSyncCancelAdjust: Windows Time Adjustment is now disabled.\n");
445 else if (g_cTimeSyncErrors++ < 10)
446 VGSvcError("vgsvcTimeSyncCancelAdjust: SetSystemTimeAdjustment(,disable) failed, error=%u\n", GetLastError());
447#endif /* !RT_OS_WINDOWS */
448}
449
450
451/**
452 * Try adjust the time using adjtime or similar.
453 *
454 * @returns true on success, false on failure.
455 *
456 * @param pDrift The time adjustment.
457 */
458static void vgsvcTimeSyncSet(PCRTTIMESPEC pDrift)
459{
460 /*
461 * Query the current time, adjust it by adding the drift and set it.
462 */
463 RTTIMESPEC NewGuestTime;
464 int rc = RTTimeSet(RTTimeSpecAdd(RTTimeNow(&NewGuestTime), pDrift));
465 if (RT_SUCCESS(rc))
466 {
467 /* Succeeded - reset the error count and log the change. */
468 g_cTimeSyncErrors = 0;
469
470 if (g_cVerbosity >= 1)
471 {
472 char sz[64];
473 RTTIME Time;
474 VGSvcVerbose(1, "time set to %s\n", RTTimeToString(RTTimeExplode(&Time, &NewGuestTime), sz, sizeof(sz)));
475#ifdef DEBUG
476 RTTIMESPEC Tmp;
477 if (g_cVerbosity >= 3)
478 VGSvcVerbose(3, " now %s\n", RTTimeToString(RTTimeExplode(&Time, RTTimeNow(&Tmp)), sz, sizeof(sz)));
479#endif
480 }
481 }
482 else if (g_cTimeSyncErrors++ < 10)
483 VGSvcError("vgsvcTimeSyncSet: RTTimeSet(%RDtimespec) failed: %Rrc\n", &NewGuestTime, rc);
484}
485
486
487/**
488 * @interface_method_impl{VBOXSERVICE,pfnWorker}
489 */
490DECLCALLBACK(int) vgsvcTimeSyncWorker(bool volatile *pfShutdown)
491{
492 RTTIME Time;
493 char sz[64];
494 int rc = VINF_SUCCESS;
495
496 /*
497 * Tell the control thread that it can continue spawning services.
498 */
499 RTThreadUserSignal(RTThreadSelf());
500
501 /*
502 * The Work Loop.
503 */
504 for (;;)
505 {
506 /*
507 * Try get a reliable time reading.
508 */
509 int cTries = 3;
510 do
511 {
512 /* query it. */
513 RTTIMESPEC GuestNow0, GuestNow, HostNow;
514 RTTimeNow(&GuestNow0);
515 int rc2 = VbglR3GetHostTime(&HostNow);
516 if (RT_FAILURE(rc2))
517 {
518 if (g_cTimeSyncErrors++ < 10)
519 VGSvcError("vgsvcTimeSyncWorker: VbglR3GetHostTime failed; rc2=%Rrc\n", rc2);
520 break;
521 }
522 RTTimeNow(&GuestNow);
523
524 /* calc latency and check if it's ok. */
525 RTTIMESPEC GuestElapsed = GuestNow;
526 RTTimeSpecSub(&GuestElapsed, &GuestNow0);
527 if ((uint32_t)RTTimeSpecGetMilli(&GuestElapsed) < g_TimeSyncMaxLatency)
528 {
529 /*
530 * Set the time once after we were restored.
531 * (Of course only if the drift is bigger than MinAdjust)
532 */
533 uint32_t TimeSyncSetThreshold = g_TimeSyncSetThreshold;
534 if (g_fTimeSyncSetOnRestore)
535 {
536 uint64_t idNewSession = g_idTimeSyncSession;
537 VbglR3GetSessionId(&idNewSession);
538 if (idNewSession != g_idTimeSyncSession)
539 {
540 VGSvcVerbose(3, "vgsvcTimeSyncWorker: The VM session ID changed, forcing resync.\n");
541 TimeSyncSetThreshold = 0;
542 g_idTimeSyncSession = idNewSession;
543 }
544 }
545
546 /*
547 * Calculate the adjustment threshold and the current drift.
548 */
549 uint32_t MinAdjust = RTTimeSpecGetMilli(&GuestElapsed) * g_TimeSyncLatencyFactor;
550 if (MinAdjust < g_TimeSyncMinAdjust)
551 MinAdjust = g_TimeSyncMinAdjust;
552
553 RTTIMESPEC Drift = HostNow;
554 RTTimeSpecSub(&Drift, &GuestNow);
555 if (RTTimeSpecGetMilli(&Drift) < 0)
556 MinAdjust += g_TimeSyncMinAdjust; /* extra buffer against moving time backwards. */
557
558 RTTIMESPEC AbsDrift = Drift;
559 RTTimeSpecAbsolute(&AbsDrift);
560 if (g_cVerbosity >= 3)
561 {
562 VGSvcVerbose(3, "vgsvcTimeSyncWorker: Host: %s (MinAdjust: %RU32 ms)\n",
563 RTTimeToString(RTTimeExplode(&Time, &HostNow), sz, sizeof(sz)), MinAdjust);
564 VGSvcVerbose(3, "vgsvcTimeSyncWorker: Guest: - %s => %RDtimespec drift\n",
565 RTTimeToString(RTTimeExplode(&Time, &GuestNow), sz, sizeof(sz)), &Drift);
566 }
567
568 uint32_t AbsDriftMilli = RTTimeSpecGetMilli(&AbsDrift);
569 if (AbsDriftMilli > MinAdjust)
570 {
571 /*
572 * Ok, the drift is above the threshold.
573 *
574 * Try a gradual adjustment first, if that fails or the drift is
575 * too big, fall back on just setting the time.
576 */
577
578 if ( AbsDriftMilli > TimeSyncSetThreshold
579 || g_fTimeSyncSetNext
580 || !vgsvcTimeSyncAdjust(&Drift))
581 {
582 vgsvcTimeSyncCancelAdjust();
583 vgsvcTimeSyncSet(&Drift);
584 }
585 }
586 else
587 vgsvcTimeSyncCancelAdjust();
588 break;
589 }
590 VGSvcVerbose(3, "vgsvcTimeSyncWorker: %RDtimespec: latency too high (%RDtimespec) sleeping 1s\n", GuestElapsed);
591 RTThreadSleep(1000);
592 } while (--cTries > 0);
593
594 /* Clear the set-next/set-start flag. */
595 g_fTimeSyncSetNext = false;
596
597 /*
598 * Block for a while.
599 *
600 * The event semaphore takes care of ignoring interruptions and it
601 * allows us to implement service wakeup later.
602 */
603 if (*pfShutdown)
604 break;
605 int rc2 = RTSemEventMultiWait(g_TimeSyncEvent, g_TimeSyncInterval);
606 if (*pfShutdown)
607 break;
608 if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
609 {
610 VGSvcError("vgsvcTimeSyncWorker: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
611 rc = rc2;
612 break;
613 }
614 }
615
616 vgsvcTimeSyncCancelAdjust();
617 RTSemEventMultiDestroy(g_TimeSyncEvent);
618 g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
619 return rc;
620}
621
622
623/**
624 * @interface_method_impl{VBOXSERVICE,pfnStop}
625 */
626static DECLCALLBACK(void) vgsvcTimeSyncStop(void)
627{
628 RTSemEventMultiSignal(g_TimeSyncEvent);
629}
630
631
632/**
633 * @interface_method_impl{VBOXSERVICE,pfnTerm}
634 */
635static DECLCALLBACK(void) vgsvcTimeSyncTerm(void)
636{
637#ifdef RT_OS_WINDOWS
638 /*
639 * Restore the SE_SYSTEMTIME_NAME token privileges (if init succeeded).
640 */
641 if (g_hTokenProcess)
642 {
643 if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &g_TkOldPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
644 {
645 DWORD dwErr = GetLastError();
646 VGSvcError("vgsvcTimeSyncTerm: Restoring token privileges (SE_SYSTEMTIME_NAME) failed with code %u!\n", dwErr);
647 }
648 CloseHandle(g_hTokenProcess);
649 g_hTokenProcess = NULL;
650 }
651#endif /* !RT_OS_WINDOWS */
652
653 if (g_TimeSyncEvent != NIL_RTSEMEVENTMULTI)
654 {
655 RTSemEventMultiDestroy(g_TimeSyncEvent);
656 g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
657 }
658}
659
660
661/**
662 * The 'timesync' service description.
663 */
664VBOXSERVICE g_TimeSync =
665{
666 /* pszName. */
667 "timesync",
668 /* pszDescription. */
669 "Time synchronization",
670 /* pszUsage. */
671 " [--timesync-interval <ms>] [--timesync-min-adjust <ms>]\n"
672 " [--timesync-latency-factor <x>] [--timesync-max-latency <ms>]\n"
673 " [--timesync-set-threshold <ms>] [--timesync-set-start]\n"
674 " [--timesync-set-on-restore 0|1]"
675 ,
676 /* pszOptions. */
677 " --timesync-interval Specifies the interval at which to synchronize the\n"
678 " time with the host. The default is 10000 ms.\n"
679 " --timesync-min-adjust The minimum absolute drift value measured in\n"
680 " milliseconds to make adjustments for.\n"
681 " The default is 1000 ms on OS/2 and 100 ms elsewhere.\n"
682 " --timesync-latency-factor\n"
683 " The factor to multiply the time query latency with\n"
684 " to calculate the dynamic minimum adjust time.\n"
685 " The default is 8 times.\n"
686 " --timesync-max-latency The max host timer query latency to accept.\n"
687 " The default is 250 ms.\n"
688 " --timesync-set-threshold\n"
689 " The absolute drift threshold, given as milliseconds,\n"
690 " where to start setting the time instead of trying to\n"
691 " adjust it. The default is 20 min.\n"
692 " --timesync-set-start Set the time when starting the time sync service.\n"
693 " --timesync-set-on-restore 0|1\n"
694 " Immediately set the time when the VM was restored.\n"
695 " 1 = set (default), 0 = don't set.\n"
696 ,
697 /* methods */
698 vgsvcTimeSyncPreInit,
699 vgsvcTimeSyncOption,
700 vgsvcTimeSyncInit,
701 vgsvcTimeSyncWorker,
702 vgsvcTimeSyncStop,
703 vgsvcTimeSyncTerm
704};
705
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