VirtualBox

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

最後變更 在這個檔案從92682是 86876,由 vboxsync 提交於 4 年 前

Guest Additions/VbglR3: Condensed / removed duplicate guest property code by adding VbglR3GuestPropExist() + VbglR3GuestPropReadEx().

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 31.0 KB
 
1/* $Id: VBoxServiceTimeSync.cpp 86876 2020-11-12 16:38:00Z vboxsync $ */
2/** @file
3 * VBoxService - Guest Additions TimeSync Service.
4 */
5
6/*
7 * Copyright (C) 2007-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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/assert.h>
96#include <iprt/string.h>
97#include <iprt/semaphore.h>
98#include <iprt/time.h>
99#include <iprt/thread.h>
100#include <VBox/err.h>
101#include <VBox/VBoxGuestLib.h>
102#include "VBoxServiceInternal.h"
103#include "VBoxServiceUtils.h"
104
105
106/*********************************************************************************************************************************
107* Global Variables *
108*********************************************************************************************************************************/
109/** The timesync interval (milliseconds). */
110static uint32_t g_TimeSyncInterval = 0;
111/**
112 * @see pg_vgsvc_timesync
113 *
114 * @remark OS/2: There is either a 1 second resolution on the DosSetDateTime
115 * API or a bug in my settimeofday implementation. Thus, don't
116 * bother unless there is at least a 1 second drift.
117 */
118#ifdef RT_OS_OS2
119static uint32_t g_cMsTimeSyncMinAdjust = 1000;
120#else
121static uint32_t g_cMsTimeSyncMinAdjust = 100;
122#endif
123/** @see pg_vgsvc_timesync */
124static uint32_t g_TimeSyncLatencyFactor = 8;
125/** @see pg_vgsvc_timesync */
126static uint32_t g_cMsTimeSyncMaxLatency = 250;
127/** @see pg_vgsvc_timesync */
128static uint32_t g_TimeSyncSetThreshold = 20*60*1000;
129/** Whether the next adjustment should just set the time instead of trying to
130 * adjust it. This is used to implement --timesync-set-start.
131 * For purposes of setting the kernel timezone, OS/2 always starts with this. */
132#ifdef RT_OS_OS2
133static bool volatile g_fTimeSyncSetOnStart = true;
134#else
135static bool volatile g_fTimeSyncSetOnStart = false;
136#endif
137/** Whether to set the time when the VM was restored. */
138static bool g_fTimeSyncSetOnRestore = true;
139/** The logging verbosity level.
140 * This uses the global verbosity level by default. */
141static uint32_t g_cTimeSyncVerbosity = 0;
142
143/** Current error count. Used to decide when to bitch and when not to. */
144static uint32_t g_cTimeSyncErrors = 0;
145
146/** The semaphore we're blocking on. */
147static RTSEMEVENTMULTI g_TimeSyncEvent = NIL_RTSEMEVENTMULTI;
148
149/** The VM session ID. Changes whenever the VM is restored or reset. */
150static uint64_t g_idTimeSyncSession;
151
152#ifdef RT_OS_WINDOWS
153/** Process token. */
154static HANDLE g_hTokenProcess = NULL;
155/** Old token privileges. */
156static TOKEN_PRIVILEGES g_TkOldPrivileges;
157/** Backup values for time adjustment. */
158static DWORD g_dwWinTimeAdjustment;
159static DWORD g_dwWinTimeIncrement;
160static BOOL g_bWinTimeAdjustmentDisabled;
161#endif
162
163
164/**
165 * @interface_method_impl{VBOXSERVICE,pfnPreInit}
166 */
167static DECLCALLBACK(int) vgsvcTimeSyncPreInit(void)
168{
169 /* Use global verbosity as default. */
170 g_cTimeSyncVerbosity = g_cVerbosity;
171
172#ifdef VBOX_WITH_GUEST_PROPS
173 /** @todo Merge this function with vgsvcTimeSyncOption() to generalize
174 * the "command line args override guest property values" behavior. */
175
176 /*
177 * Read the service options from the VM's guest properties.
178 * Note that these options can be overridden by the command line options later.
179 */
180 uint32_t uGuestPropSvcClientID;
181 int rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
182 if (RT_FAILURE(rc))
183 {
184 if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
185 {
186 VGSvcVerbose(0, "VMInfo: Guest property service is not available, skipping\n");
187 rc = VINF_SUCCESS;
188 }
189 else
190 VGSvcError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
191 }
192 else
193 {
194 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-interval",
195 &g_TimeSyncInterval, 50, UINT32_MAX - 1);
196 if ( RT_SUCCESS(rc)
197 || rc == VERR_NOT_FOUND)
198 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-min-adjust",
199 &g_cMsTimeSyncMinAdjust, 0, 3600000);
200 if ( RT_SUCCESS(rc)
201 || rc == VERR_NOT_FOUND)
202 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-latency-factor",
203 &g_TimeSyncLatencyFactor, 1, 1024);
204 if ( RT_SUCCESS(rc)
205 || rc == VERR_NOT_FOUND)
206 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-max-latency",
207 &g_cMsTimeSyncMaxLatency, 1, 3600000);
208 if ( RT_SUCCESS(rc)
209 || rc == VERR_NOT_FOUND)
210 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold",
211 &g_TimeSyncSetThreshold, 0, 7*24*60*60*1000 /* a week */);
212
213 if (VbglR3GuestPropExist(uGuestPropSvcClientID,
214 "/VirtualBox/GuestAdd/VBoxService/--timesync-set-start"))
215 g_fTimeSyncSetOnStart = true;
216
217 if (VbglR3GuestPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-no-set-start"))
218 g_fTimeSyncSetOnStart = false;
219
220
221 if (VbglR3GuestPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-on-restore"))
222 g_fTimeSyncSetOnRestore = true;
223
224 if (VbglR3GuestPropExist(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-no-set-on-restore"))
225 g_fTimeSyncSetOnRestore = false;
226
227 uint32_t uValue;
228 rc = VGSvcReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--timesync-verbosity",
229 &uValue, 0 /*uMin*/, 255 /*uMax*/);
230 if (RT_SUCCESS(rc))
231 g_cTimeSyncVerbosity = uValue;
232
233 VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
234 }
235
236 if (rc == VERR_NOT_FOUND) /* If a value is not found, don't be sad! */
237 rc = VINF_SUCCESS;
238 return rc;
239#else
240 /* Nothing to do here yet. */
241 return VINF_SUCCESS;
242#endif
243}
244
245
246/**
247 * Displays a verbose message based on the currently
248 * set timesync verbosity level.
249 *
250 * @param iLevel Minimum log level required to display this message.
251 * @param pszFormat The message text.
252 * @param ... Format arguments.
253 */
254static void vgsvcTimeSyncLog(unsigned iLevel, const char *pszFormat, ...)
255{
256 if (iLevel <= g_cTimeSyncVerbosity)
257 {
258 va_list va;
259 va_start(va, pszFormat);
260 VGSvcLogV(pszFormat, va);
261 va_end(va);
262 }
263}
264
265
266/**
267 * @interface_method_impl{VBOXSERVICE,pfnOption}
268 */
269static DECLCALLBACK(int) vgsvcTimeSyncOption(const char **ppszShort, int argc, char **argv, int *pi)
270{
271 int rc = VINF_SUCCESS;
272 if (ppszShort)
273 rc = -1 ;/* no short options */
274 else if (!strcmp(argv[*pi], "--timesync-interval"))
275 rc = VGSvcArgUInt32(argc, argv, "", pi, &g_TimeSyncInterval, 50, UINT32_MAX - 1);
276 else if (!strcmp(argv[*pi], "--timesync-min-adjust"))
277 rc = VGSvcArgUInt32(argc, argv, "", pi, &g_cMsTimeSyncMinAdjust, 0, 3600000);
278 else if (!strcmp(argv[*pi], "--timesync-latency-factor"))
279 rc = VGSvcArgUInt32(argc, argv, "", pi, &g_TimeSyncLatencyFactor, 1, 1024);
280 else if (!strcmp(argv[*pi], "--timesync-max-latency"))
281 rc = VGSvcArgUInt32(argc, argv, "", pi, &g_cMsTimeSyncMaxLatency, 1, 3600000);
282 else if (!strcmp(argv[*pi], "--timesync-set-threshold"))
283 rc = VGSvcArgUInt32(argc, argv, "", pi, &g_TimeSyncSetThreshold, 0, 7*24*60*60*1000); /* a week */
284 else if (!strcmp(argv[*pi], "--timesync-set-start"))
285 g_fTimeSyncSetOnStart = true;
286 else if (!strcmp(argv[*pi], "--timesync-no-set-start"))
287 g_fTimeSyncSetOnStart = false;
288 else if (!strcmp(argv[*pi], "--timesync-set-on-restore"))
289 g_fTimeSyncSetOnRestore = true;
290 else if (!strcmp(argv[*pi], "--timesync-no-set-on-restore"))
291 g_fTimeSyncSetOnRestore = false;
292 else if (!strcmp(argv[*pi], "--timesync-verbosity"))
293 rc = VGSvcArgUInt32(argc, argv, "", pi, &g_cTimeSyncVerbosity, 0 /*uMin*/, 255 /*uMax*/);
294 else
295 rc = -1;
296
297 return rc;
298}
299
300
301/**
302 * @interface_method_impl{VBOXSERVICE,pfnInit}
303 */
304static DECLCALLBACK(int) vgsvcTimeSyncInit(void)
305{
306 /*
307 * If not specified, find the right interval default.
308 * Then create the event sem to block on.
309 */
310 if (!g_TimeSyncInterval)
311 g_TimeSyncInterval = g_DefaultInterval * 1000;
312 if (!g_TimeSyncInterval)
313 g_TimeSyncInterval = 10 * 1000;
314
315 VbglR3GetSessionId(&g_idTimeSyncSession);
316 /* The status code is ignored as this information is not available with VBox < 3.2.10. */
317
318 int rc = RTSemEventMultiCreate(&g_TimeSyncEvent);
319 AssertRC(rc);
320#ifdef RT_OS_WINDOWS
321 if (RT_SUCCESS(rc))
322 {
323 /*
324 * Adjust privileges of this process so we can make system time adjustments.
325 */
326 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &g_hTokenProcess))
327 {
328 TOKEN_PRIVILEGES tkPriv;
329 RT_ZERO(tkPriv);
330 tkPriv.PrivilegeCount = 1;
331 tkPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
332 if (LookupPrivilegeValue(NULL, SE_SYSTEMTIME_NAME, &tkPriv.Privileges[0].Luid))
333 {
334 DWORD cbRet = sizeof(g_TkOldPrivileges);
335 if (AdjustTokenPrivileges(g_hTokenProcess, FALSE, &tkPriv, sizeof(TOKEN_PRIVILEGES), &g_TkOldPrivileges, &cbRet))
336 rc = VINF_SUCCESS;
337 else
338 {
339 DWORD dwErr = GetLastError();
340 rc = RTErrConvertFromWin32(dwErr);
341 VGSvcError("vgsvcTimeSyncInit: Adjusting token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n",
342 dwErr, rc);
343 }
344 }
345 else
346 {
347 DWORD dwErr = GetLastError();
348 rc = RTErrConvertFromWin32(dwErr);
349 VGSvcError("vgsvcTimeSyncInit: Looking up token privileges (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n",
350 dwErr, rc);
351 }
352 if (RT_FAILURE(rc))
353 {
354 CloseHandle(g_hTokenProcess);
355 g_hTokenProcess = NULL;
356 }
357 }
358 else
359 {
360 DWORD dwErr = GetLastError();
361 rc = RTErrConvertFromWin32(dwErr);
362 VGSvcError("vgsvcTimeSyncInit: Opening process token (SE_SYSTEMTIME_NAME) failed with status code %u/%Rrc!\n",
363 dwErr, rc);
364 g_hTokenProcess = NULL;
365 }
366 }
367
368 if (g_pfnGetSystemTimeAdjustment)
369 {
370 if (g_pfnGetSystemTimeAdjustment(&g_dwWinTimeAdjustment, &g_dwWinTimeIncrement, &g_bWinTimeAdjustmentDisabled))
371 vgsvcTimeSyncLog(0, "vgsvcTimeSyncInit: Initially %ld (100ns) units per %ld (100 ns) units interval, disabled=%d\n",
372 g_dwWinTimeAdjustment, g_dwWinTimeIncrement, g_bWinTimeAdjustmentDisabled ? 1 : 0);
373 else
374 {
375 DWORD dwErr = GetLastError();
376 rc = RTErrConvertFromWin32(dwErr);
377 VGSvcError("vgsvcTimeSyncInit: Could not get time adjustment values! Last error: %ld!\n", dwErr);
378 }
379 }
380#endif /* RT_OS_WINDOWS */
381
382 return rc;
383}
384
385
386/**
387 * Try adjusting the time using adjtime or similar.
388 *
389 * @returns true on success, false on failure.
390 *
391 * @param pDrift The time adjustment.
392 */
393static bool vgsvcTimeSyncAdjust(PCRTTIMESPEC pDrift)
394{
395#ifdef RT_OS_WINDOWS
396/** @todo r=bird: g_hTokenProcess cannot be NULL here.
397 * vgsvcTimeSyncInit will fail and the service will not be started with
398 * it being NULL. vgsvcTimeSyncInit OTOH will *NOT* be called until the
399 * service thread has terminated. If anything
400 * else is the case, there is buggy code somewhere.*/
401 if (g_hTokenProcess == NULL) /* Is the token already closed when shutting down? */
402 return false;
403
404 /* The API appeared in NT 3.50. */
405 if ( !g_pfnSetSystemTimeAdjustment
406 || !g_pfnGetSystemTimeAdjustment)
407 return false;
408
409 DWORD dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwWinTimeIncrement;
410 BOOL fWinTimeAdjustmentDisabled;
411 if (g_pfnGetSystemTimeAdjustment(&dwWinTimeAdjustment, &dwWinTimeIncrement, &fWinTimeAdjustmentDisabled))
412 {
413 DWORD dwDiffMax = g_dwWinTimeAdjustment * 0.50;
414 DWORD dwDiffNew = dwWinTimeAdjustment * 0.10;
415
416 if (RTTimeSpecGetMilli(pDrift) > 0)
417 {
418 dwWinNewTimeAdjustment = dwWinTimeAdjustment + dwDiffNew;
419 if (dwWinNewTimeAdjustment > (g_dwWinTimeAdjustment + dwDiffMax))
420 {
421 dwWinNewTimeAdjustment = g_dwWinTimeAdjustment + dwDiffMax;
422 dwDiffNew = dwDiffMax;
423 }
424 }
425 else
426 {
427 dwWinNewTimeAdjustment = dwWinTimeAdjustment - dwDiffNew;
428 if (dwWinNewTimeAdjustment < (g_dwWinTimeAdjustment - dwDiffMax))
429 {
430 dwWinNewTimeAdjustment = g_dwWinTimeAdjustment - dwDiffMax;
431 dwDiffNew = dwDiffMax;
432 }
433 }
434
435 vgsvcTimeSyncLog(3, "vgsvcTimeSyncAdjust: Drift=%lldms\n", RTTimeSpecGetMilli(pDrift));
436 vgsvcTimeSyncLog(3, "vgsvcTimeSyncAdjust: OrgTA=%ld, CurTA=%ld, NewTA=%ld, DiffNew=%ld, DiffMax=%ld\n",
437 g_dwWinTimeAdjustment, dwWinTimeAdjustment, dwWinNewTimeAdjustment, dwDiffNew, dwDiffMax);
438 if (g_pfnSetSystemTimeAdjustment(dwWinNewTimeAdjustment, FALSE /* Periodic adjustments enabled. */))
439 {
440 g_cTimeSyncErrors = 0;
441 return true;
442 }
443
444 if (g_cTimeSyncErrors++ < 10)
445 VGSvcError("vgsvcTimeSyncAdjust: SetSystemTimeAdjustment failed, error=%u\n", GetLastError());
446 }
447 else if (g_cTimeSyncErrors++ < 10)
448 VGSvcError("vgsvcTimeSyncAdjust: GetSystemTimeAdjustment failed, error=%ld\n", GetLastError());
449
450#elif defined(RT_OS_OS2) || defined(RT_OS_HAIKU)
451 /* No API for doing gradual time adjustments. */
452
453#else /* PORTME */
454 /*
455 * Try using adjtime(), most unix-like systems have this.
456 */
457 struct timeval tv;
458 RTTimeSpecGetTimeval(pDrift, &tv);
459 if (adjtime(&tv, NULL) == 0)
460 {
461 vgsvcTimeSyncLog(1, "vgsvcTimeSyncAdjust: adjtime by %RDtimespec\n", pDrift);
462 g_cTimeSyncErrors = 0;
463 return true;
464 }
465#endif
466
467 /* failed */
468 return false;
469}
470
471
472/**
473 * Cancels any pending time adjustment.
474 *
475 * Called when we've caught up and before calls to vgsvcTimeSyncSet.
476 */
477static void vgsvcTimeSyncCancelAdjust(void)
478{
479#ifdef RT_OS_WINDOWS
480/** @todo r=bird: g_hTokenProcess cannot be NULL here. See argumentation in
481 * vgsvcTimeSyncAdjust. */
482 if (g_hTokenProcess == NULL) /* No process token (anymore)? */
483 return;
484 if (!g_pfnSetSystemTimeAdjustment)
485 return;
486 if (g_pfnSetSystemTimeAdjustment(0, TRUE /* Periodic adjustments disabled. */))
487 vgsvcTimeSyncLog(5, "vgsvcTimeSyncCancelAdjust: Windows Time Adjustment is now disabled.\n");
488 else if (g_cTimeSyncErrors++ < 10)
489 VGSvcError("vgsvcTimeSyncCancelAdjust: SetSystemTimeAdjustment(,disable) failed, error=%u\n", GetLastError());
490#endif /* !RT_OS_WINDOWS */
491}
492
493
494/**
495 * Set the wall clock to compensate for drift.
496 *
497 * @returns true on success, false on failure.
498 *
499 * @param pDrift The time adjustment.
500 */
501static void vgsvcTimeSyncSet(PCRTTIMESPEC pDrift)
502{
503 /*
504 * Query the current time, adjust it by adding the drift and set it.
505 */
506 RTTIMESPEC NewGuestTime;
507 int rc = RTTimeSet(RTTimeSpecAdd(RTTimeNow(&NewGuestTime), pDrift));
508 if (RT_SUCCESS(rc))
509 {
510 /* Succeeded - reset the error count and log the change. */
511 g_cTimeSyncErrors = 0;
512
513 if (g_cTimeSyncVerbosity >= 1)
514 {
515 char sz[64];
516 RTTIME Time;
517 vgsvcTimeSyncLog(1, "time set to %s\n", RTTimeToString(RTTimeExplode(&Time, &NewGuestTime), sz, sizeof(sz)));
518#ifdef DEBUG
519 RTTIMESPEC Tmp;
520 vgsvcTimeSyncLog(3, " now %s\n", RTTimeToString(RTTimeExplode(&Time, RTTimeNow(&Tmp)), sz, sizeof(sz)));
521#endif
522 }
523 }
524 else if (g_cTimeSyncErrors++ < 10)
525 VGSvcError("vgsvcTimeSyncSet: RTTimeSet(%RDtimespec) failed: %Rrc\n", &NewGuestTime, rc);
526}
527
528
529/**
530 * @interface_method_impl{VBOXSERVICE,pfnWorker}
531 */
532DECLCALLBACK(int) vgsvcTimeSyncWorker(bool volatile *pfShutdown)
533{
534 RTTIME Time;
535 int rc = VINF_SUCCESS;
536
537 /*
538 * Tell the control thread that it can continue spawning services.
539 */
540 RTThreadUserSignal(RTThreadSelf());
541
542 /*
543 * Initialize the last host and guest times to prevent log message.
544 * We also track whether we set the time in the previous loop.
545 */
546 RTTIMESPEC HostLast;
547 if (RT_FAILURE(VbglR3GetHostTime(&HostLast)))
548 RTTimeSpecSetNano(&HostLast, 0);
549 RTTIMESPEC GuestLast;
550 RTTimeNow(&GuestLast);
551 bool fSetTimeLastLoop = false;
552
553 /*
554 * The Work Loop.
555 */
556 for (;;)
557 {
558 /*
559 * Try to get a reliable time reading.
560 */
561 int cTries = 3;
562 do
563 {
564 /*
565 * Query the session id (first to keep lantency low) and the time.
566 */
567 uint64_t idNewSession = g_idTimeSyncSession;
568 if (g_fTimeSyncSetOnRestore)
569 VbglR3GetSessionId(&idNewSession);
570
571 RTTIMESPEC GuestNow0;
572 RTTimeNow(&GuestNow0);
573
574 RTTIMESPEC HostNow;
575 int rc2 = VbglR3GetHostTime(&HostNow);
576 if (RT_FAILURE(rc2))
577 {
578 if (g_cTimeSyncErrors++ < 10)
579 VGSvcError("vgsvcTimeSyncWorker: VbglR3GetHostTime failed; rc2=%Rrc\n", rc2);
580 break;
581 }
582
583 RTTIMESPEC GuestNow;
584 RTTimeNow(&GuestNow);
585
586 /*
587 * Calc latency and check if it's ok.
588 */
589 RTTIMESPEC GuestElapsed = GuestNow;
590 RTTimeSpecSub(&GuestElapsed, &GuestNow0);
591 if ((uint32_t)RTTimeSpecGetMilli(&GuestElapsed) < g_cMsTimeSyncMaxLatency)
592 {
593 /*
594 * If we were just restored, set the adjustment threshold to zero to force a resync.
595 */
596 uint32_t TimeSyncSetThreshold = g_TimeSyncSetThreshold;
597 if ( g_fTimeSyncSetOnRestore
598 && idNewSession != g_idTimeSyncSession)
599 {
600 vgsvcTimeSyncLog(2, "vgsvcTimeSyncWorker: The VM session ID changed, forcing resync.\n");
601 g_idTimeSyncSession = idNewSession;
602 TimeSyncSetThreshold = 0;
603 }
604
605 /*
606 * Calculate the adjustment threshold and the current drift.
607 */
608 uint32_t MinAdjust = RTTimeSpecGetMilli(&GuestElapsed) * g_TimeSyncLatencyFactor;
609 if (MinAdjust < g_cMsTimeSyncMinAdjust)
610 MinAdjust = g_cMsTimeSyncMinAdjust;
611
612 RTTIMESPEC Drift = HostNow;
613 RTTimeSpecSub(&Drift, &GuestNow);
614 if (RTTimeSpecGetMilli(&Drift) < 0)
615 MinAdjust += g_cMsTimeSyncMinAdjust; /* extra buffer against moving time backwards. */
616
617 RTTIMESPEC AbsDrift = Drift;
618 RTTimeSpecAbsolute(&AbsDrift);
619
620 if (g_cTimeSyncVerbosity >= 4)
621 {
622 char sz1[64];
623 char sz2[64];
624 vgsvcTimeSyncLog(4, "vgsvcTimeSyncWorker: Host: %s (MinAdjust: %RU32 ms), Guest: %s => %RDtimespec drift\n",
625 RTTimeToString(RTTimeExplode(&Time, &HostNow), sz1, sizeof(sz1)), MinAdjust,
626 RTTimeToString(RTTimeExplode(&Time, &GuestNow), sz2, sizeof(sz2)), &Drift);
627 }
628
629 bool fSetTimeInThisLoop = false;
630 uint64_t AbsDriftMilli = RTTimeSpecGetMilli(&AbsDrift);
631 if ( AbsDriftMilli > MinAdjust
632 || g_fTimeSyncSetOnStart)
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_fTimeSyncSetOnStart
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_fTimeSyncSetOnStart = 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>]\n"
758 " [--timesync-set-start|--timesync-no-set-start]\n"
759 " [--timesync-set-on-restore|--timesync-no-set-on-restore]\n"
760 " [--timesync-verbosity <level>]"
761 ,
762 /* pszOptions. */
763 " --timesync-interval Specifies the interval at which to synchronize the\n"
764 " time with the host. The default is 10000 ms.\n"
765 " --timesync-min-adjust The minimum absolute drift value measured in\n"
766 " milliseconds to make adjustments for.\n"
767 " The default is 1000 ms on OS/2 and 100 ms elsewhere.\n"
768 " --timesync-latency-factor\n"
769 " The factor to multiply the time query latency with\n"
770 " to calculate the dynamic minimum adjust time.\n"
771 " The default is 8 times.\n"
772 " --timesync-max-latency The max host timer query latency to accept.\n"
773 " The default is 250 ms.\n"
774 " --timesync-set-threshold\n"
775 " The absolute drift threshold, given as milliseconds,\n"
776 " where to start setting the time instead of trying to\n"
777 " adjust it. The default is 20 min.\n"
778 " --timesync-set-start, --timesync-no-set-start \n"
779 " Set the time when starting the time sync service.\n"
780#ifdef RT_OS_OS2
781 " Default: --timesync-set-start\n"
782#else
783 " Default: --timesync-no-set-start\n"
784#endif
785 " --timesync-set-on-restore, --timesync-no-set-on-restore\n"
786 " Whether to immediately set the time when the VM is\n"
787 " restored or not. Default: --timesync-set-on-restore\n"
788 " --timesync-verbosity Sets the verbosity level. Defaults to service wide\n"
789 " verbosity level.\n"
790 ,
791 /* methods */
792 vgsvcTimeSyncPreInit,
793 vgsvcTimeSyncOption,
794 vgsvcTimeSyncInit,
795 vgsvcTimeSyncWorker,
796 vgsvcTimeSyncStop,
797 vgsvcTimeSyncTerm
798};
799
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette