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

后端 未结 3 2070
渐次进展
渐次进展 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条回答
  •  梦如初夏
    2021-02-08 02:50

    Is this the on board clock that is drifting? Surely it doesn't drift at this rate?
    No, they shouldn't drift

    What is the cause of this drift?
    NTP service or similar that runs your OS. They affects clock_gettime(CLOCK_REALTIME, ...);

    Is there anything I can do to keep them in sync (other than very frequently recalculating _start_tsc and _start_clock_time in step 2)? Yes you can ease the problem.

    1 You can try to use CLOCK_MONOTONIC instead of CLOCK_REALTIME.

    2 You can calculate the difference as a linear function from the time and apply it to compensate the drifting. But it will not be very reliable because time services doesn't adjust the time as linear function. But it will give you some more accuracy. Periodically you can do readjustment.


    Some drifting you can get because you calculate _ticks_per_ns not accurately. You can check it by running you program several times. If results are not reproducible, it is mean that you calculate your _ticks_per_ns incorrectly. It is better to use statistics method then just an average value.


    Also please note, _ticks_per_ns you are calculating by using CLOCK_MONOTONIC, which is related to TSC.

    Next you are using CLOCK_REALTIME. It provides the system time. If your system has NTP or similar service, the time will be adjusted.

    Your difference is around 2 micro seconds per minute. It is 0.002 * 24*60 = 2.9 milli seconds a day. It is a great accuracy for CPU clock. 3 ms a day it is a 1 second a year.

提交回复
热议问题