Data shown in this section can be downloaded here.
More testing results could be accessed here.
A naive solution is to do an one time calibration of TSC rate. If this calibration is good enought, we can fully rely on a TSC-based software clock. Using the following calculation:
Time = TSC_rate * tsc_ticks + Offset (1)
Where TSC_rate and Offset are obtained during one measurement. The advantage this solution provides is that we do not need to rely on system to get TSC rate, and could choose custom-optimized calibration method. The pseudo-code to obtain TSC_rate is:
TSC_rate = (sys_end - sys_begin)/(ticks_end - ticks_begin);
According to (1), Offset should be SysTime - TSC_rate * tsc_ticks. In pratical, it is a bit more complex than that, because we need to consider the calling time of
gettimeofday(). There was a model for offset estimation established by Shalunov, and used in TSC-I2. In short, the model measures local time before and after measuring reference time, calculate the two differences delta1 and delta2, and then choose the ( min(delta1) - min(delta2) /2 ) as offset between local time and reference time.
iterate n times
deltai1 = sys_time - ticks_begin * TSC_rate;
deltai2 = ticks_end * TSC_rate - sys_time;
min_delta1 = minimum(deltai1);
min_delta2 = minimum(deltai2);
Offset = (min_delta1 - min_delta2)/2;
Below is the result of a free-running clock implementation. 'offset' and 'rate error' denotes the difference of time measurement and rate measurement between the TSC clock and system clock, and will be used afterwards.
|Fig. 1 Free-running clock offset measurement during 12 hours.||Fig. 2 Free-running clock rate error measurement during 12 hours.|
We can easily observe from the plots that although free-running clock can provide perfect linear timing, i.e. very stable rate, it suffers from the error of initial calibration of TSC_rate. This error is fixed and causes the offset between TSC clock and system clock to increase linearly. Such characteristics indicate that a free-running clock alone can not satisfy our needs. Stanislav Shalunov, NTP Implementation and Assumptions about the Network.