On a cpu with constant_tsc and nonstop_tsc, why does my time drift?

后端 未结 3 2078
渐次进展
渐次进展 2021-02-08 02:09

I am running this test on a cpu with constant_tsc and nonstop_tsc

$ grep -m 1 ^flags /proc/cpuinfo | sed \'s/ /\\n/g\' | egrep \"consta         


        
3条回答
  •  猫巷女王i
    2021-02-08 02:45

    The relationship between the TSC and something like CLOCK_MONOTONIC will not be exactly unchanging. Even though you "calibrate" the TSC against CLOCK_MONOTONIC, the calibration will be out of date almost as soon as it is finished!

    The reasons they won't stay in sync long term:

    1. CLOCK_MONOTONIC is affected by NTP clock rate adjustments. NTP will constantly check network time and subtly slow down or speed up the system clock to match network time. This results in some kind of oscillating pattern in the true CLOCK_MONOTONIC frequency, and so your calibration will always be slightly off, especially the next time NTP applies a rate adjustment. You could compare against CLOCK_MONOTONIC_RAW to eliminate this effect.
    2. CLOCK_MONOTONIC and TSC are almost certainly based on totally different underlying oscillators. It is often say that modern OSes use the TSC for time-keeping, but this is only to apply a small "local" offset to some other underlying slow-running clock to provide a very precise time (e.g., the "slow time" might be updated every timer tick, and then the TSC is used to interpolate between timer ticks). It is the slow underlying clock (something like the HPET or APIC clocks) that determines the longer-term behavior of CLOCK_MONOTONIC. The TSC itself, however is an independent free running clock, deriving its frequency from a different oscillator, on a different place on the chipset/motherboard and will different natural fluctuations (in particular, different response to temperature changes).

    It is (2) that is more fundamental out of the two above: it means that even without any kind of NTP adjustments (or if you use a clock that is not subject to them), you'll see drift over time if the underlying clocks are based on different physical oscillators.

提交回复
热议问题