[ntp:hackers] Non-monotonic time on NT

David L. Mills mills at udel.edu
Wed May 9 10:17:45 PDT 2007


Martin,

Thanks for the explanation. I knew about the TSC interpolation, but not 
about the callback function. I assumed the code used the machine 
instruction to read the TSC in the wrapper that calls gettimeofday().

I probably should keep my mouth zipped, but that is not in my nature.

Once each second or so call gettimeofday() and save the ToD and TSC in 
global variables of the same names. Before saving, compute deltaToD and 
deltaTSC as the difference between current and past values. Compute the 
ratio deltaTod / deltaTSC = d and save that.

In the get_systime() routine compute the difference between the current 
TSC and the saved TSC multiplied by d. Add that to the saved ToD to 
determine the interpolated time.

Note that it soesn't matter if the computations captured once each 
second are delayed be scheduling; the only thing that matters is that 
the gettimeofday() call itself is not preempted.

This is how the nanotime() routine in the precision kernel distribution 
works, but I can tell you the devil is in the details. Hint: The timer 
interrupt and TSC frequencies are not necessarily syntonic and evil tiny 
whiskers can occur near the vicinity of a timer interrupt.

Dave

Martin Burnicki wrote:
> Dave,
> 
> this whole thing is about the implementation of the gettimeofday() call under 
> Windows.
> 
> The adequate function under Windows returns the system time with a granularity 
> of e.g. 15.625 milliseconds, which is the normal Windows timer tick interval. 
> Subsequent calls return exacly the same system time during a tick interval, 
> and after the next timer tick the returned time increases by those 15.625 ms.
> 
> The distributon contains some Windows-specific code already for years now, 
> which tries to interpolate the system time between the timer tick using some 
> other high resolution timer, e.g. the CPU's TSC, and thus provide the system 
> time to the common NTP algorithms with higher resolution. 
> 
> This has been proven to work quite good, but obviously the accuracy you can 
> yield depends on how accurate time stamps of the hires timer can be 
> associated to the timer tick events. 
> 
> Currently this is done using a callback function which shall be called after a 
> timer tick has occurred, and grap a time stamp of the hires timer. However, 
> under heavy system load, that callback function could be scheduled delayed, 
> so the picked up timer values are also biased by the delay.
> 
> This obviously decreases the accuracy of the interpolated sytem time stamps 
> returned by ntpd's gettimeofday() emulation for Windows.
> 
> The discussion is just about a way to determine those delayed callbacks, and 
> discard them in order to avoid decreasing the accuracy of the interpolation. 
> This touches only Windows-specific code, and will the result will be able to  
> provide a "better", i.e. more stable, system time to the NTP algorithms.
> 
> Martin


More information about the hackers mailing list