zoukankan      html  css  js  c++  java
  • adjtimex和时钟的几个概念tick,freq,ppm,jiffies

    adjtimex使用

    今天遇到一个ntp的同步问题。服务器上配置好了ntpd,在启动前也手动进行过同步,但是过段时间ntpq查询发现服务器即便能选出同步服务器,但是系统的时间偏差越来越大。
    服务器上实际有2个时钟,一个是主板电池驱动的硬件时间(RTC或者CMOS时间),另外就是系统时间。服务器启动时会从RTC里读取一次时间,之后便靠中断来计时。可以设置ntpd同步后讲时间写回RTC.

    语法:adjtimex [OPTION]… 主要参数说明:
    -p, –print   输出内核时间变量的值
    -t, –tick val    设置内核时钟计数间隔(微秒)
    -f, –frequency newfreq   设置系统时钟偏移量
    -c, –compare[=count]     比较系统时钟和CMOS时钟
    -i, –interval tim    设置时钟比较间隔时间 (sec)
    -l, –log[=file]  将当前时间记录到文件中
    –host timeserver     查询时间服务器
    -u, –utc     将CMOS时钟设置成UTC
    在服务器上先比较一下系统时间和CMOS时间
    
    # adjtimex --compare
                                          --- current ---   -- suggested --
    cmos time     system-cmos  error_ppm   tick      freq    tick      freq
    1381503971     1.751318
    1381503981     1.721794    -2952.4  10000  -1573016
    1381503991     1.692179    -2961.5  10000  -1573016   10029   2456959
    1381504001     1.662619    -2956.0  10000  -1573016   10029   2097584
    1381504011     1.633055    -2956.4  10000  -1573016   10029   2122584
    1381504021     1.603491    -2956.4  10000  -1573016   10029   2122584
    1381504031     1.573928    -2956.3  10000  -1573016   10029   2117896
    1381504041     1.544361    -2956.7  10000  -1573016   10029   2142896
    
    

    然后使用

    # adjtimex -t 10029
    

    修改一下tick,重新手动同步后,再启动ntpd发现时间终于正常了。

    毫秒

    毫秒是一种较为微小的时间单位,是一秒的千分之一(0.001秒),简称ms。典型照相机的最短曝光时间为一毫秒。一只家蝇每三毫秒扇一次翅膀;蚊子二十毫秒振翅一次;蜜蜂则每五毫秒扇一次。由于月亮绕地球的轨道逐渐变宽,它绕一圈所需的时间每年长两毫秒。在计算机科学中,10毫秒的间隔称为一个jiffy。

    微秒

    微秒(microsecond)即百万分之一秒(10的负6次秒),简称μs。光在这个时间里可以传播300米,大约是3个足球场的长度,但是海平面上的声波只能传播1/3毫米。高速的商业频闪仪闪烁一次大约持续1微秒。一个BOMB在它的引信烧完之后大约24微秒开始爆炸。

    纳秒

    纳秒(nanosecond)即一秒的10亿分之一(10的负9次秒),简称ns。常用作内存读写速度的单位。光在真空中一纳秒仅传播30厘米(不足一个步长)。个人电脑的微处理器执行一道指令(如将两数相加)约需2至4纳秒。另一种罕见的亚原子粒子K介子的存在时间为12纳秒。

    2. ppm

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

    24*3600*(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。
    

    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. jiffies

    全局变量jiffies用来记录自系统启动以来产生的节拍的总数。启动时,内核将该变量初始化为0,此后,每次时钟中断处理程序都会增加该变量的值。一秒内时钟中断的次数等于Hz,所以jiffies一秒内增加的值也就是Hz。

    系统运行时间以秒为单位,等于jiffies/Hz。

    注意,jiffies类型为无符号长整型(unsigned long),其他任何类型存放它都不正确。

    将以秒为单位的时间转化为jiffies:

    seconds * Hz

    将jiffies转化为以秒为单位的时间:

    jiffies / Hz

    相比之下,内核中将秒转换为jiffies用的多些

    5. 工具介绍

    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
    

    ntptime

    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天影响
    tick +1 +8.64s
    ppm +1 +0.0864s
    freq +1 +0.000001318359375s

    简单的换算 1tick=100ppm=2^16*100 freq

  • 相关阅读:
    Python即时网络爬虫项目启动说明
    Spring Cloud Consul入门
    Selenium用法示例
    云中漫步,做个公众号方便生活、取悦自己
    PhantomJS用法示例
    Python3环境安装PySpider爬虫框架过程
    Python3环境安装Scrapy爬虫框架过程及常见错误
    爬虫扒下 bilibili 视频信息
    Sql优化(一) Merge Join vs. Hash Join vs. Nested Loop
    email.py
  • 原文地址:https://www.cnblogs.com/muahao/p/6274930.html
Copyright © 2011-2022 走看看