VirtualBox

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

Last change on this file since 70032 was 70032, checked in by vboxsync, 7 years ago

Timesync: Oops, need two buffers.

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