注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Tsecer的回音岛

Tsecer的博客

 
 
 

日志

 
 

ntp大致原理  

2013-11-15 23:22:38|  分类: 电脑基础 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
一、ntp原理
net time protocol协议,是在联网情况下通过网络上的其它主机来同步本机的系统时间,这对于一些要求大量主机协同完成一项工作的集群中是比较有用的。时间其实是系统的一个重要基础信息,如果这个基础信息不可靠,那么一些基本的功能会超出我们常规的理解范围,就像相对论,我到现在都没有能够理解。
在同步时间时,不能假设自己和目标机器有相同的基准时间,主机host的时间和网络机器net的时间可能相差了几天,但是通常假设每个机器的系统时钟都是准确的。
从网络上的资料看(没仔细看协议),这里使用到了四个时间,
同步报文从host出发时间戳 t1
报文到达net时间 t2
报文离开net的时间t3
报文回到host的时间t4
在net与host时间没有同步的情况下,net的时间可以利用的只有相对时间,这一点对于host来说也是如此。
t4 - t1表示报文在网络中游荡的总时间
t3-t2表示报文在net机器内部停留的时间
这样(t4-t1) - (t3 -t2)得到报文在网络上的往返时间,单程时间则为该值的一半。假设来去时间相同(在不同的情况下取平均值),报文离开net的时间t3加上该时间即为当前时间。在这个过程中,t4-t1与t3-t2使用的都是各自的系统时间的相对时间,保证了在同一个系统。
二、系统基准时间读取
这个信息保存在系统的bios中,即使电脑切断电源,这个时间也会由主板上的电源为该时钟供电,虽然耗电量较低,但是如果长时间没有开机(例如放暑假回来之后发现系统基准时间清零),终究有用完时间。在系统启动过程中,操作系统会从bios中读取bios中保存的基准时间,之后系统时钟在系统定时器(8253A)驱动下累加系统时间,这个定时器同样也包括了系统系统slice切换,定时器等其它功能。我们看下Linux内核中对于系统初始时间的读取linux-2.6.21\include\asm-i386\mach-default\mach_time.h:
static inline unsigned long mach_get_cmos_time(void)
{
    unsigned int year, mon, day, hour, min, sec;

    do {
        sec = CMOS_READ(RTC_SECONDS);
        min = CMOS_READ(RTC_MINUTES);
        hour = CMOS_READ(RTC_HOURS);
        day = CMOS_READ(RTC_DAY_OF_MONTH);
        mon = CMOS_READ(RTC_MONTH);
        year = CMOS_READ(RTC_YEAR);
    } while (sec != CMOS_READ(RTC_SECONDS));

    if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
        BCD_TO_BIN(sec);
        BCD_TO_BIN(min);
        BCD_TO_BIN(hour);
        BCD_TO_BIN(day);
        BCD_TO_BIN(mon);
        BCD_TO_BIN(year);
    }

    year += 1900;
    if (year < 1970)
        year += 100;

    return mktime(year, mon, day, hour, min, sec);
}
三、hwclock工具对cmos时间调整
man hwclock中对于adjusttime的说明。挑选一些信息描述下:硬件时钟通常都不是很精确,但是大部分不精确是可以预测的,每天多出或者少出的时间是相同的,这个值被叫做系统偏移。hwcloc的adjust功能让你通过系统矫正来校准系统编译。hwcloc从/etc/adjtime文件中保存一些历史信息,这个文件被叫做adjtime文件。
通过hwclock --set命令设置cmos时间时,工具会根据上次调整时间,当前系统时间,计算当前系统硬件时钟每天的秒数误差,在之后通过hwclock --adjust就可以根据这些历史信息来调整系统时间。那么这个hwclock --set的时间从哪里来呢?我想应该就是通过ntpd之类的守护任务完成吧,但是中间还是会有些基准时间之类的细节问题,不再追究。如果希望马上同步本机时间,可以通过ntpdate servername 来完成本地时间同步。

The Adjust Function

The Hardware Clock is usually not very accurate. However, much of its inaccuracy is completely predictable - it gains or loses the same amount of time every day. This is called systematic drift. hwclock's "adjust" function lets you make systematic corrections to correct the systematic drift.

It works like this: hwclock keeps a file, /etc/adjtime, that keeps some historical information. This is called the adjtime file.

Suppose you start with no adjtime file. You issue a hwclock --set command to set the Hardware Clock to the true current time. Hwclock creates the adjtime file and records in it the current time as the last time the clock was calibrated. 5 days later, the clock has gained 10 seconds, so you issue another hwclock --set command to set it back 10 seconds. Hwclock updates the adjtime file to show the current time as the last time the clock was calibrated, and records 2 seconds per day as the systematic drift rate. 24 hours go by, and then you issue a hwclock --adjust command. Hwclock consults the adjtime file and sees that the clock gains 2 seconds per day when left alone and that it has been left alone for exactly one day. So it subtracts 2 seconds from the Hardware Clock. It then records the current time as the last time the clock was adjusted. Another 24 hours goes by and you issue another hwclock --adjust. Hwclock does the same thing: subtracts 2 seconds and updates the adjtime file with the current time as the last time the clock was adjusted.

Every time you calibrate (set) the clock (using --set or --systohc), hwclock recalculates the systematic drift rate based on how long it has been since the last calibration, how long it has been since the last adjustment, what drift rate was assumed in any intervening adjustments, and the amount by which the clock is presently off.

A small amount of error creeps in any time hwclock sets the clock, so it refrains from making an adjustment that would be less than 1 second. Later on, when you request an adjustment again, the accumulated drift will be more than a second and hwclock will do the adjustment then.

It is good to do a hwclock --adjust just before the hwclock --hctosys at system startup time, and maybe periodically while the system is running via cron.

The adjtime file, while named for its historical purpose of controlling adjustments only, actually contains other information for use by hwclock in remembering information from one invocation to the next.

The format of the adjtime file is, in ASCII:

Line 1: 3 numbers, separated by blanks: 1) systematic drift rate in seconds per day, floating point decimal; 2) Resulting number of seconds since 1969 UTC of most recent adjustment or calibration, decimal integer; 3) zero (for compatibility with clock(8)) as a decimal integer.

Line 2: 1 number: Resulting number of seconds since 1969 UTC of most recent calibration. Zero if there has been no calibration yet or it is known that any previous calibration is moot (for example, because the Hardware Clock has been found, since that calibration, not to contain a valid time). This is a decimal integer.

Line 3: "UTC" or "LOCAL". Tells whether the Hardware Clock is set to Coordinated Universal Time or local time. You can always override this value with options on the hwclock command line.

You can use an adjtime file that was previously used with the clock(8) program with hwclock.

Automatic Hardware Clock Synchronization By the Kernel

You should be aware of another way that the Hardware Clock is kept synchronized in some systems. The Linux kernel has a mode wherein it copies the System Time to the Hardware Clock every 11 minutes. This is a good mode to use when you are using something sophisticated like ntp to keep your System Time synchronized. (ntp is a way to keep your System Time synchronized either to a time server somewhere on the network or to a radio clock hooked up to your system. See RFC 1305).

This mode (we'll call it "11 minute mode") is off until something turns it on. The ntp daemon xntpd is one thing that turns it on. You can turn it off by running anything, including hwclock --hctosys, that sets the System Time the old fashioned way.

To see if it is on or off, use the command adjtimex --print and look at the value of "status". If the "64" bit of this number (expressed in binary) equal to 0, 11 minute mode is on. Otherwise, it is off.

If your system runs with 11 minute mode on, don't use hwclock --adjust or hwclock --hctosys. You'll just make a mess. It is acceptable to use a hwclock --hctosys at startup time to get a reasonable System Time until your system is able to set the System Time from the external source and start 11 minute mode.
  评论这张
 
阅读(716)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017