控制linux时钟速度的几个参数

1. tick

用户态下内核时钟计数间隔,默认都是100HZ。因此单个tick代表了10^4 us。

可以设置每个tick代表的时钟长度,因此把tick增加1(即增加为10001us)的影响是每天时间慢8.64s(24小时的tick被提前消耗完)


24*3600*100*10001/10^6-24*3600=8.64s。

2. ppm

百万分之一秒,1个PPM增加


24*360*(10^6+1)/10^6-24*3600=0.0864s。

PPM 增加500,每天的时间慢500*0.0864=43.2s

假设服务器现在标准的PPM为 A,为了让系统24小时快1s。则ppm的调整为
A- 1*10^6/3600/24=A-11.574074。

处理闰秒如果想让时钟在一天内慢1s,则可以用ntptime -f把frequency改为服务器当前的值+11.574


ubuntu@ip-172-31-23-155:~/ntp-4.2.8p9$ ntptime
ntp_gettime() returns code 0 (OK)
  time dc1e24a0.5e832b98  Mon, Jan  9 2017 15:02:56.369, (.369189989),
  maximum error 73959 us, estimated error 101 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
  modes 0x0 (),
  offset 75.601 us, frequency 18.063 ppm, interval 1 s,
  maximum error 73959 us, estimated error 101 us,
  status 0x2001 (PLL,NANO),
  time constant 5, precision 0.001 us, tolerance 500 ppm,

3. freq


In struct timex, freq, ppsfreq, and stabil are ppm (parts per
       million) with a 16-bit fractional part, which means that a value of 1
       in one of those fields actually means 2^-16 ppm, and 2^16=65536 is 1
       ppm.  This is the case for both input values (in the case of freq)
       and output values.
  -f newfreq, --frequency newfreq
              Set the system clock frequency offset to newfreq.  newfreq can be negative or positive, and gives a much finer adjustment than the --tick switch.  When
              USER_HZ=100, the value is scaled such that newfreq = 65536 speeds up the system clock by about 1 ppm, or .0864 sec/day

因此freq增加65536相当于增加1个PPM(1E-6S)。freq增加65536,每天的影响-0.0864s/天。
因为单个freq对时钟的影响非常小。所以大部分时候不用调整freq,除非是要求比较高的情况下,比如这个机器本身是做时钟服务器。

4. 工具介绍

1. adjtimex

可以根据和主板上的硬件时钟作对比,调整系统的每个tick代表的us数和freq,加-c参数会给出建议值。


ubuntu@ip-172-31-23-155:~$ sudo adjtimex -c
                                      --- current ---   -- suggested --
cmos time     system-cmos  error_ppm   tick      freq    tick      freq
1484017467       0.491349             10000   1217447
1484017477       0.491537       18.8  10000   1217447
1484017487       0.491743       20.6  10000   1217447    9999   6421047
1484017497       0.491901       15.8  10000   1217447   10000    183072
1484017507       0.492101       20.0  10000   1217447    9999   6460109
1484017517       0.492283       18.2  10000   1217447    9999   6577297
1484017527       0.492490       20.7  10000   1217447    9999   6414797
2. ntptime

这个可以比较方便的直接设置frequency偏移量。


ubuntu@ip-172-31-23-155:~$ ntptime
ntp_gettime() returns code 0 (OK)
  time dc1ece09.54a99b70  Tue, Jan 10 2017  3:05:45.330, (.330713562),
  maximum error 124171 us, estimated error 64 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
  modes 0x0 (),
  offset -8.136 us, frequency 18.577 ppm, interval 1 s,
  maximum error 124171 us, estimated error 64 us,
  status 0x2001 (PLL,NANO),
  time constant 5, precision 0.001 us, tolerance 500 ppm,
ubuntu@ip-172-31-23-155:~$ ntptime --help
ntptime: unknown option --
usage: ntptime [-MNT:cde:f:hm:o:rs:t:]

-M          switch to microsecond mode
-N          switch to nanosecond mode
-T tai_offset   set TAI offset
-c          display the time taken to call ntp_gettime (us)
-e esterror     estimate of the error (us)
-f frequency    Frequency error (-500 .. 500) (ppm)
-h          display this help info
-m maxerror     max possible error (us)
-o offset       current offset (ms)
-r          print the unix and NTP time raw
-s status       Set the status bits
-t timeconstant log2 of PLL time constant (0 .. 6)

汇总

参数 增加单位数 1天影响
freq +1 -0.000001318359375s
ppm +1 -0.0864s
tick +1 -8.64s

简单的换算 1tick=100ppm=100 * 65536 freq

此条目发表在System分类目录。将固定链接加入收藏夹。

发表回复