Clock drift on Windows

后端 未结 13 592
无人共我
无人共我 2020-12-28 18:42

I\'ve developed a Windows service which tracks business events. It uses the Windows clock to timestamp events. However, the underlying clock can drift quite dramatically (e.

相关标签:
13条回答
  • 2020-12-28 19:00

    I believe Windows Time Service only implements SNTP, which is a simplified version of NTP. A full NTP implementation takes into account the stability of your clock in deciding how often to sync.

    You can get the full NTP server for Windows here.

    0 讨论(0)
  • 2020-12-28 19:01

    What servers are you running? In desktops the times I've come across this are with Spread Spectrum FSB enabled, causes some issues with the interrupt timing which is what makes that clock tick. May want to see if this is an option in BIOS on one of those servers and turn it off if enabled.

    Another option you have is to edit the time polling interval and make it much shorter using the following registry key, most likely you'll have to add it (note this is a DWORD value and the value is in seconds, e.g. 600 for 10min):

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient\SpecialPollInterval
    

    Here's a full workup on it: KB816042

    0 讨论(0)
  • 2020-12-28 19:03

    Clock ticks should be predictable, but on most PC hardware - because they're not designed for real-time systems - other I/O device interrupts have priority over the clock tick interrupt, and some drivers do extensive processing in the interrupt service routine rather than defer it to a deferred procedure call (DPC), which means the system may not be able to serve the clock tick interrupt until (sometimes) long after it was signalled.

    Other factors include bus-mastering I/O controllers which steal many memory bus cycles from the CPU, causing it to be starved of memory bus bandwidth for significant periods.

    As others have said, the clock-generation hardware may also vary its frequency as component values change with temperature.

    Windows does allow the amount of ticks added to the real-time clock on every interrupt to be adjusted: see SetSystemTimeAdjustment. This would only work if you had a predictable clock skew, however. If the clock is only slightly off, the SNTP client ("Windows Time" service) will adjust this skew to make the clock tick slightly faster or slower to trend towards the correct time.

    0 讨论(0)
  • 2020-12-28 19:06

    You could run "w32tm /resync" in a scheduled task .bat file. This works on Windows Server 2003.

    0 讨论(0)
  • 2020-12-28 19:12

    I don't know if this applies, but ...

    There's an issue with Windows that if you change the timer resolution with timeBeginPeriod() a lot, the clock will drift.

    Actually, there is a bug in Java's Thread wait() (and the os::sleep()) function's Windows implementation that causes this behaviour. It always sets the timer resolution to 1 ms before wait in order to be accurate (regardless of sleep length), and restores it immediately upon completion, unless any other threads are still sleeping. This set/reset will then confuse the Windows clock, which expects the windows time quantum to be fairly constant.

    Sun has actually known about this since 2006, and hasn't fixed it, AFAICT!

    We actually had the clock going twice as fast because of this! A simple Java program that sleeps 1 millisec in a loop shows this behaviour.

    The solution is to set the time resolution yourself, to something low, and keep it there as long as possible. Use timeBeginPeriod() to control that. (We set it to 1 ms without any adverse effects.)

    For those coding in Java, the easier way to fix this is by creating a thread that sleeps as long as the app lives.

    Note that this will fix this issue on the machine globally, regardless of which application is the actual culprit.

    0 讨论(0)
  • 2020-12-28 19:13

    Clock drift may be a consequence of the temperature; maybe you could try to get temperature more constant - using better cooling perhaps? You're never going to loose drift totally, though.

    Using an external clock (GPS receiver etc...), and a statistical method to relate CPU time to Absolute Time is what we use here to synch events in distributed systems.

    0 讨论(0)
提交回复
热议问题