VirtualBox

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

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

VBoxService: Needed to dynamically resolve two more APIs to make it work on NT 3.1.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.7 KB
Line 
1/* $Id: VBoxServiceTimeSync.cpp 70346 2017-12-26 15:52:28Z 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 a reference.
25 * Communication is facilitated by VMMDev. TM will try its best to make sure
26 * all timer ticks get 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 * can 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
49 * reflected 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 use 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. (Rationale is that a *lot* of stuff may
63 * happen on our way back from ring-3 and TM/VMMDev since we're taking the
64 * route thru the inner EM loop with its force flag 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 guests only update
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 decide 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 (g_pfnGetSystemTimeAdjustment)
370 {
371 if (g_pfnGetSystemTimeAdjustment(&g_dwWinTimeAdjustment, &g_dwWinTimeIncrement, &g_bWinTimeAdjustmentDisabled))
372 vgsvcTimeSyncLog(0, "vgsvcTimeSyncInit: Initially %ld (100ns) units per %ld (100 ns) units interval, disabled=%d\n",
373 g_dwWinTimeAdjustment, g_dwWinTimeIncrement, g_bWinTimeAdjustmentDisabled ? 1 : 0);
374 else
375 {
376 DWORD dwErr = GetLastError();
377 rc = RTErrConvertFromWin32(dwErr);
378 VGSvcError("vgsvcTimeSyncInit: Could not get time adjustment values! Last error: %ld!\n", dwErr);
379 }
380 }
381#endif /* RT_OS_WINDOWS */
382
383 return rc;
384}
385
386
387/**
388 * Try adjusting the time using adjtime or similar.
389 *
390 * @returns true on success, false on failure.
391 *
392 * @param pDrift The time adjustment.
393 */
394static bool vgsvcTimeSyncAdjust(PCRTTIMESPEC pDrift)
395{
396#ifdef RT_OS_WINDOWS
397/** @todo r=bird: g_hTokenProcess cannot be NULL here.
398 * vgsvcTimeSyncInit will fail and the service will not be started with
399 * it being NULL. vgsvcTimeSyncInit OTOH will *NOT* be called until the
400 * service thread has terminated. If anything
401 * else is the case, there is buggy code somewhere.*/
402 if (g_hTokenProcess == NULL) /* Is the token already closed when shutting down? */
403 return false;
404
405 /* The API appeared in NT 3.50. */
406 if ( !g_pfnSetSystemTimeAdjustment
407 || !g_pfnGetSystemTimeAdjustment)
408 return false;
409
410 DWORD dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwWinTimeIncrement;
411 BOOL fWinTimeAdjustmentDisabled;
412 if (g_pfnGetSystemTimeAdjustment(&dwWinTimeAdjustment, &dwWinTimeIncrement, &fWinTimeAdjustmentDisabled))
413 {
414 DWORD dwDiffMax = g_dwWinTimeAdjustment * 0.50;
415 DWORD dwDiffNew = dwWinTimeAdjustment * 0.10;
416
417 if (RTTimeSpecGetMilli(pDrift) > 0)
418 {
419 dwWinNewTimeAdjustment = dwWinTimeAdjustment + dwDiffNew;
420 if (dwWinNewTimeAdjustment > (g_dwWinTimeAdjustment + dwDiffMax))
421 {
422 dwWinNewTimeAdjustment = g_dwWinTimeAdjustment + dwDiffMax;
423 dwDiffNew = dwDiffMax;
424 }
425 }
426 else
427 {
428 dwWinNewTimeAdjustment = dwWinTimeAdjustment - dwDiffNew;
429 if (dwWinNewTimeAdjustment < (g_dwWinTimeAdjustment - dwDiffMax))
430 {
431 dwWinNewTimeAdjustment = g_dwWinTimeAdjustment - dwDiffMax;
432 dwDiffNew = dwDiffMax;
433 }
434 }
435
436 vgsvcTimeSyncLog(3, "vgsvcTimeSyncAdjust: Drift=%lldms\n", RTTimeSpecGetMilli(pDrift));
437 vgsvcTimeSyncLog(3, "vgsvcTimeSyncAdjust: OrgTA=%ld, CurTA=%ld, NewTA=%ld, DiffNew=%ld, DiffMax=%ld\n",
438 g_dwWinTimeAdjustment, dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwDiffNew, dwDiffMax);
439 if (g_pfnSetSystemTimeAdjustment(dwWinNewTimeAdjustment, FALSE /* Periodic adjustments enabled. */))
440 {
441 g_cTimeSyncErrors = 0;
442 return true;
443 }
444
445 if (g_cTimeSyncErrors++ < 10)
446 VGSvcError("vgsvcTimeSyncAdjust: SetSystemTimeAdjustment failed, error=%u\n", GetLastError());
447 }
448 else if (g_cTimeSyncErrors++ < 10)
449 VGSvcError("vgsvcTimeSyncAdjust: GetSystemTimeAdjustment failed, error=%ld\n", GetLastError());
450
451#elif defined(RT_OS_OS2) || defined(RT_OS_HAIKU)
452 /* No API for doing gradual time adjustments. */
453
454#else /* PORTME */
455 /*
456 * Try using adjtime(), most unix-like systems have this.
457 */
458 struct timeval tv;
459 RTTimeSpecGetTimeval(pDrift, &tv);
460 if (adjtime(&tv, NULL) == 0)
461 {
462 vgsvcTimeSyncLog(1, "vgsvcTimeSyncAdjust: adjtime by %RDtimespec\n", pDrift);
463 g_cTimeSyncErrors = 0;
464 return true;
465 }
466#endif
467
468 /* failed */
469 return false;
470}
471
472
473/**
474 * Cancels any pending time adjustment.
475 *
476 * Called when we've caught up and before calls to vgsvcTimeSyncSet.
477 */
478static void vgsvcTimeSyncCancelAdjust(void)
479{
480#ifdef RT_OS_WINDOWS
481/** @todo r=bird: g_hTokenProcess cannot be NULL here. See argumentation in
482 * vgsvcTimeSyncAdjust. */
483 if (g_hTokenProcess == NULL) /* No process token (anymore)? */
484 return;
485 if (!g_pfnSetSystemTimeAdjustment)
486 return;
487 if (g_pfnSetSystemTimeAdjustment(0, TRUE /* Periodic adjustments disabled. */))
488 vgsvcTimeSyncLog(5, "vgsvcTimeSyncCancelAdjust: Windows Time Adjustment is now disabled.\n");
489 else if (g_cTimeSyncErrors++ < 10)
490 VGSvcError("vgsvcTimeSyncCancelAdjust: SetSystemTimeAdjustment(,disable) failed, error=%u\n", GetLastError());
491#endif /* !RT_OS_WINDOWS */
492}
493
494
495/**
496 * Set the wall clock to compensate for drift.
497 *
498 * @returns true on success, false on failure.
499 *
500 * @param pDrift The time adjustment.
501 */
502static void vgsvcTimeSyncSet(PCRTTIMESPEC pDrift)
503{
504 /*
505 * Query the current time, adjust it by adding the drift and set it.
506 */
507 RTTIMESPEC NewGuestTime;
508 int rc = RTTimeSet(RTTimeSpecAdd(RTTimeNow(&NewGuestTime), pDrift));
509 if (RT_SUCCESS(rc))
510 {
511 /* Succeeded - reset the error count and log the change. */
512 g_cTimeSyncErrors = 0;
513
514 if (g_cTimeSyncVerbosity >= 1)
515 {
516 char sz[64];
517 RTTIME Time;
518 vgsvcTimeSyncLog(1, "time set to %s\n", RTTimeToString(RTTimeExplode(&Time, &NewGuestTime), sz, sizeof(sz)));
519#ifdef DEBUG
520 RTTIMESPEC Tmp;
521 vgsvcTimeSyncLog(3, " now %s\n", RTTimeToString(RTTimeExplode(&Time, RTTimeNow(&Tmp)), sz, sizeof(sz)));
522#endif
523 }
524 }
525 else if (g_cTimeSyncErrors++ < 10)
526 VGSvcError("vgsvcTimeSyncSet: RTTimeSet(%RDtimespec) failed: %Rrc\n", &NewGuestTime, rc);
527}
528
529
530/**
531 * @interface_method_impl{VBOXSERVICE,pfnWorker}
532 */
533DECLCALLBACK(int) vgsvcTimeSyncWorker(bool volatile *pfShutdown)
534{
535 RTTIME Time;
536 int rc = VINF_SUCCESS;
537
538 /*
539 * Tell the control thread that it can continue spawning services.
540 */
541 RTThreadUserSignal(RTThreadSelf());
542
543 /*
544 * Initialize the last host and guest times to prevent log message.
545 * We also track whether we set the time in the previous loop.
546 */
547 RTTIMESPEC HostLast;
548 if (RT_FAILURE(VbglR3GetHostTime(&HostLast)))
549 RTTimeSpecSetNano(&HostLast, 0);
550 RTTIMESPEC GuestLast;
551 RTTimeNow(&GuestLast);
552 bool fSetTimeLastLoop = false;
553
554 /*
555 * The Work Loop.
556 */
557 for (;;)
558 {
559 /*
560 * Try to get a reliable time reading.
561 */
562 int cTries = 3;
563 do
564 {
565 /*
566 * Query the session id (first to keep lantency low) and the time.
567 */
568 uint64_t idNewSession = g_idTimeSyncSession;
569 if (g_fTimeSyncSetOnRestore)
570 VbglR3GetSessionId(&idNewSession);
571
572 RTTIMESPEC GuestNow0;
573 RTTimeNow(&GuestNow0);
574
575 RTTIMESPEC HostNow;
576 int rc2 = VbglR3GetHostTime(&HostNow);
577 if (RT_FAILURE(rc2))
578 {
579 if (g_cTimeSyncErrors++ < 10)
580 VGSvcError("vgsvcTimeSyncWorker: VbglR3GetHostTime failed; rc2=%Rrc\n", rc2);
581 break;
582 }
583
584 RTTIMESPEC GuestNow;
585 RTTimeNow(&GuestNow);
586
587 /*
588 * Calc latency and check if it's ok.
589 */
590 RTTIMESPEC GuestElapsed = GuestNow;
591 RTTimeSpecSub(&GuestElapsed, &GuestNow0);
592 if ((uint32_t)RTTimeSpecGetMilli(&GuestElapsed) < g_cMsTimeSyncMaxLatency)
593 {
594 /*
595 * If we were just restored, set the adjustment threshold to zero to force a resync.
596 */
597 uint32_t TimeSyncSetThreshold = g_TimeSyncSetThreshold;
598 if ( g_fTimeSyncSetOnRestore
599 && idNewSession != g_idTimeSyncSession)
600 {
601 vgsvcTimeSyncLog(2, "vgsvcTimeSyncWorker: The VM session ID changed, forcing resync.\n");
602 g_idTimeSyncSession = idNewSession;
603 TimeSyncSetThreshold = 0;
604 }
605
606 /*
607 * Calculate the adjustment threshold and the current drift.
608 */
609 uint32_t MinAdjust = RTTimeSpecGetMilli(&GuestElapsed) * g_TimeSyncLatencyFactor;
610 if (MinAdjust < g_cMsTimeSyncMinAdjust)
611 MinAdjust = g_cMsTimeSyncMinAdjust;
612
613 RTTIMESPEC Drift = HostNow;
614 RTTimeSpecSub(&Drift, &GuestNow);
615 if (RTTimeSpecGetMilli(&Drift) < 0)
616 MinAdjust += g_cMsTimeSyncMinAdjust; /* extra buffer against moving time backwards. */
617
618 RTTIMESPEC AbsDrift = Drift;
619 RTTimeSpecAbsolute(&AbsDrift);
620
621 if (g_cTimeSyncVerbosity >= 4)
622 {
623 char sz1[64];
624 char sz2[64];
625 vgsvcTimeSyncLog(4, "vgsvcTimeSyncWorker: Host: %s (MinAdjust: %RU32 ms), Guest: %s => %RDtimespec drift\n",
626 RTTimeToString(RTTimeExplode(&Time, &HostNow), sz1, sizeof(sz1)), MinAdjust,
627 RTTimeToString(RTTimeExplode(&Time, &GuestNow), sz2, sizeof(sz2)), &Drift);
628 }
629
630 bool fSetTimeInThisLoop = false;
631 uint64_t AbsDriftMilli = RTTimeSpecGetMilli(&AbsDrift);
632 if (AbsDriftMilli > MinAdjust)
633 {
634 /*
635 * Ok, the drift is above the threshold.
636 *
637 * Try a gradual adjustment first, if that fails or the drift is
638 * too big, fall back on just setting the time.
639 */
640 if ( AbsDriftMilli > TimeSyncSetThreshold
641 || g_fTimeSyncSetNext
642 || !vgsvcTimeSyncAdjust(&Drift))
643 {
644 vgsvcTimeSyncCancelAdjust();
645 vgsvcTimeSyncSet(&Drift);
646 fSetTimeInThisLoop = true;
647 }
648
649 /*
650 * Log radical host time changes.
651 */
652 int64_t cNsHostDelta = RTTimeSpecGetNano(&HostNow) - RTTimeSpecGetNano(&HostLast);
653 if ((uint64_t)RT_ABS(cNsHostDelta) > RT_NS_1HOUR / 2)
654 vgsvcTimeSyncLog(0, "vgsvcTimeSyncWorker: Radical host time change: %'RI64ns (HostNow=%RDtimespec HostLast=%RDtimespec)\n",
655 cNsHostDelta, &HostNow, &HostLast);
656 }
657 else
658 vgsvcTimeSyncCancelAdjust();
659 HostLast = HostNow;
660
661 /*
662 * Log radical guest time changes (we could be the cause of these, mind).
663 * Note! Right now we don't care about an extra log line after we called
664 * vgsvcTimeSyncSet. fSetTimeLastLoop helps show it though.
665 */
666 int64_t cNsGuestDelta = RTTimeSpecGetNano(&GuestNow) - RTTimeSpecGetNano(&GuestLast);
667 if ((uint64_t)RT_ABS(cNsGuestDelta) > RT_NS_1HOUR / 2)
668 vgsvcTimeSyncLog(0, "vgsvcTimeSyncWorker: Radical guest time change: %'RI64ns (GuestNow=%RDtimespec GuestLast=%RDtimespec fSetTimeLastLoop=%RTbool)\n",
669 cNsGuestDelta, &GuestNow, &GuestLast, fSetTimeLastLoop);
670 GuestLast = GuestNow;
671 fSetTimeLastLoop = fSetTimeInThisLoop;
672 break;
673 }
674 vgsvcTimeSyncLog(3, "vgsvcTimeSyncWorker: %RDtimespec: latency too high (%RDtimespec, max %ums) sleeping 1s\n",
675 &GuestNow, &GuestElapsed, g_cMsTimeSyncMaxLatency);
676 RTThreadSleep(1000);
677 } while (--cTries > 0);
678
679 /* Clear the set-next/set-start flag. */
680 g_fTimeSyncSetNext = false;
681
682 /*
683 * Block for a while.
684 *
685 * The event semaphore takes care of ignoring interruptions and it
686 * allows us to implement service wakeup later.
687 */
688 if (*pfShutdown)
689 break;
690 int rc2 = RTSemEventMultiWait(g_TimeSyncEvent, g_TimeSyncInterval);
691 if (*pfShutdown)
692 break;
693 if (rc2 != VERR_TIMEOUT && RT_FAILURE(rc2))
694 {
695 VGSvcError("vgsvcTimeSyncWorker: RTSemEventMultiWait failed; rc2=%Rrc\n", rc2);
696 rc = rc2;
697 break;
698 }
699 }
700
701 vgsvcTimeSyncCancelAdjust();
702 return rc;
703}
704
705
706/**
707 * @interface_method_impl{VBOXSERVICE,pfnStop}
708 */
709static DECLCALLBACK(void) vgsvcTimeSyncStop(void)
710{
711 if (g_TimeSyncEvent != NIL_RTSEMEVENTMULTI)
712 RTSemEventMultiSignal(g_TimeSyncEvent);
713}
714
715
716/**
717 * @interface_method_impl{VBOXSERVICE,pfnTerm}
718 */
719static DECLCALLBACK(void) vgsvcTimeSyncTerm(void)
720{
721#ifdef RT_OS_WINDOWS
722 /*
723 * Restore the SE_SYSTEMTIME_NAME token privileges (if init succeeded).
724 */
725 if (g_hTokenProcess)
726 {
727 if (!AdjustTokenPrivileges(g_hTokenProcess, FALSE, &g_TkOldPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL))
728 {
729 DWORD dwErr = GetLastError();
730 VGSvcError("vgsvcTimeSyncTerm: Restoring token privileges (SE_SYSTEMTIME_NAME) failed with code %u!\n", dwErr);
731 }
732 CloseHandle(g_hTokenProcess);
733 g_hTokenProcess = NULL;
734 }
735#endif /* !RT_OS_WINDOWS */
736
737 if (g_TimeSyncEvent != NIL_RTSEMEVENTMULTI)
738 {
739 RTSemEventMultiDestroy(g_TimeSyncEvent);
740 g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
741 }
742}
743
744
745/**
746 * The 'timesync' service description.
747 */
748VBOXSERVICE g_TimeSync =
749{
750 /* pszName. */
751 "timesync",
752 /* pszDescription. */
753 "Time synchronization",
754 /* pszUsage. */
755 " [--timesync-interval <ms>] [--timesync-min-adjust <ms>]\n"
756 " [--timesync-latency-factor <x>] [--timesync-max-latency <ms>]\n"
757 " [--timesync-set-threshold <ms>] [--timesync-set-start]\n"
758 " [--timesync-set-on-restore|--timesync-no-set-on-restore]\n"
759 " [--timesync-verbosity <level>]"
760 ,
761 /* pszOptions. */
762 " --timesync-interval Specifies the interval at which to synchronize the\n"
763 " time with the host. The default is 10000 ms.\n"
764 " --timesync-min-adjust The minimum absolute drift value measured in\n"
765 " milliseconds to make adjustments for.\n"
766 " The default is 1000 ms on OS/2 and 100 ms elsewhere.\n"
767 " --timesync-latency-factor\n"
768 " The factor to multiply the time query latency with\n"
769 " to calculate the dynamic minimum adjust time.\n"
770 " The default is 8 times.\n"
771 " --timesync-max-latency The max host timer query latency to accept.\n"
772 " The default is 250 ms.\n"
773 " --timesync-set-threshold\n"
774 " The absolute drift threshold, given as milliseconds,\n"
775 " where to start setting the time instead of trying to\n"
776 " adjust it. The default is 20 min.\n"
777 " --timesync-set-start Set the time when starting the time sync service.\n"
778 " --timesync-set-on-restore, --timesync-no-set-on-restore\n"
779 " Whether to immediately set the time when the VM is\n"
780 " restored or not. Default: --timesync-set-on-restore\n"
781 " --timesync-verbosity Sets the verbosity level. Defaults to service wide\n"
782 " verbosity level.\n"
783 ,
784 /* methods */
785 vgsvcTimeSyncPreInit,
786 vgsvcTimeSyncOption,
787 vgsvcTimeSyncInit,
788 vgsvcTimeSyncWorker,
789 vgsvcTimeSyncStop,
790 vgsvcTimeSyncTerm
791};
792
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