3. 技术总结
为了更全面地理解该模型的工作原理, 前面提到的 kernel.tar.Z 发行版中包含一个独立的模拟器 kern.c 和头文件 timex.h. 此外, 还提供了一个完整的 C 程序 kern_ntptime.c, 它实现了 ntp_gettime() 和 ntp_adjtime() 函数, 但删除了特定于厂商的参数传递代码. 由于该发行版因大量注释和修饰而略显庞大, 在本备忘录中列出这些程序的清单并不实际. 无论如何, 实现者可以选择截取模拟器的一部分用于新的内核设计, 但如果将其包含在本备忘录中, 由于格式约定, 这样做会很困难.
kern.c 程序是一个自适应参数, 一阶, II 型锁相环的实现. 系统时钟通过模拟器中定义的一组变量和算法实现, 并由程序中同样包含的一个驱动程序生成的显式偏移驱动. 这些算法包含的代码片段与特定机器内核实现中的代码片段几乎相同, 并以相同方式工作, 但可以在不与这些片段可能集成进去的任何授权源代码关联的情况下理解其操作. 这些代码片段本身并非源自任何授权代码. 以下讨论假定模拟器代码可供查阅.
3.1. PLL 模拟
该模拟器的运行符合 [3] 中描述的分析模型. main() 程序作为 hardupdate(), hardclock(), second_overflow(), hardpps() 和 microtime() 这些片段的驱动程序, 尽管这些片段中实现的所有功能并非都被模拟. 该程序在每次定时器中断时模拟 PLL, 并在每次时间更新时打印关键程序变量的摘要.
内核配置文件中针对每种实现定义了三个选项. PPS_SYNC 选项提供对每秒脉冲 (PPS) 信号的支持, 该信号用于驯服 CPU 时钟振荡器的频率. EXT_CLOCK 选项提供对外部内核可读时钟的支持, 例如用于 SBus 的 KSI/Odetics TPRO IRIG 接口或 HIGHBALL 精密振荡器. TPRO 选项支持前者, HIGHBALL 选项支持后者. 外部时钟作为 microtime() 时钟驱动程序实现, 具体源代码由内核配置文件选择.
3.1.1. hardupdate() 片段
hardupdate() 片段由 ntp_adjtime() 在计算出每次更新时调用, 以调整系统时钟的相位和频率. 注意, 时间常数以 2 的幂为单位, 因此乘法可以通过简单的移位完成. 相位变量计算为偏移除以时间常数. 然后, 计算自上次更新以来的时间, 并将其限制到一个最大值 (为稳健起见), 在初始化时则限制为零. 偏移乘以 (抱歉这个难看的乘法) 该结果, 再除以时间常数的平方, 然后加到频率变量上. 注意, 假定所有移位均为正数, 而对有符号量进行右移需要一点小技巧.
在给定的定义下, 最大时间偏移由 long 类型的位数 (32 或 64) 减去 SHIFT_UPDATE 比例因子 (12) 决定, 即至少 20 位 (有符号). 选择该比例因子是为了在后续步骤中不损失有效位, 这些步骤可能涉及最多 SHIFT_UPDATE 位的右移. 这样得到的时间调整范围为 +-512 ms. 由于 time_constant 必须大于或等于零, 最大频率偏移由 SHIFT_USEC 比例因子 (16) 决定, 即至少 16 位 (有符号). 这样得到的频率调整范围为 +-31,500 ppm.
在加法步骤中, offset * mtemp 的值不大于 MAXPHASE * MAXSEC = 31 位 (有符号), 因此在 32 位机器上不会使 long 加法溢出. 由于 time_constant 上限为 6, 最多右移 12 位可能造成精度损失. 这导致最坏情况下的净频率分辨率约为 .063 ppm, 对大多数石英振荡器而言并不显著. 只有当 NTP 对等体按照协议规范行为异常时, 才可能出现最坏情况.
time_offset 值在入口处被钳位. time_phase 变量是一个累加器, 因此每次调用时都被钳位到容差范围内. 这有助于在振荡器频率尚未确定之前抑制瞬态, 并在时间同步协议或实现行为异常时满足正确性断言.
3.1.2. hardclock() 片段
hardclock() 片段插入到硬件定时器中断例程中系统时钟即将递增的位置. 在该片段之前, time_update 变量已被初始化为原有 Unix 内核中 adjtime() 系统调用计算出的值, 通常为正负 tickadj 值, 后者一般在 5 us 左右. 表示系统时钟瞬时相位的 time_phase 变量由 time_adj 推进, time_adj 在下述 second_overflow() 片段中计算. 如果 time_phase 的值按缩放单位超过 1 us, 则 time_update 增加该有符号超出量, 而 time_phase 保留余数.
除使用外部振荡器 (例如 HIGHBALL 接口) 的情形外, hardclock() 片段通过 tick 加 time_update 的值推进系统时钟. 然而, 在使用外部振荡器的情况下, 系统时钟直接从接口获取, 而 time_update 则用于
驯服该接口. 不过, 系统时钟仍必须如前所述被驯服, 因此改用 second_overflow() 片段计算出的 clock_cpu 值.
3.1.3. second_overflow() 片段
second_overflow() 片段插入在系统时间变量的微秒字段被检查溢出的位置. 溢出时, 最大误差 time_maxerror 增加 time_tolerance, 以反映由振荡器频率误差引起的最大时间偏移. 然后, 根据上次调用 hardclock() 片段时更新的 (缩放后的) time_offset 和 time_freq 变量, 计算出用于推进内核时间变量的增量 time_adj.
相位调整计算为剩余 time_offset 的一个 (有符号) 分数, 该分数加到 time_adj 上, 然后从 time_offset 中减去. 该技术在偏移较大时提供快速收敛, 在偏移较小时提供良好的分辨率. 频率调整是 (缩放后的) time_freq 变量, 滴答间隔无法整除一秒时所需的调整 fixtick, 以及 PPS 频率调整 pps_ybar (如果已配置) 之和.
用移位近似精确乘除运算的方案效果良好, 除非需要精确计算, 例如下述使用 PPS 信号驯服 CPU 时钟振荡器频率时. 只要实际振荡器频率在以秒为单位的表示中是 2 的幂, 就无需校正. 然而, 在 SunOS 内核中, 时钟频率为 100 Hz, 这导致误差因子为 0.78. 在这种情况下, 代码将 time_adj 增加 1.25 倍, 从而使总体误差小于百分之三.
在一天翻转时, 下述闰秒状态机决定是否在时标中插入或删除一秒. microtime() 例程确保所报告的时间始终单调递增.
3.1.4. hardpps() 片段
只有在内核配置文件中指定了 PPS_SYNC 选项时, hardpps() 片段才有效. 在 PPS 信号的准时跳变时刻, 串口驱动程序或等效接口调用它. 该片段作为一个
一阶, I 型锁频环 (FLL) 工作, 由 pps_ybar 变量表示的频率与硬件时钟振荡器频率之间的差控制.
为避免每次 PPS 跳变多次调用 microtime() 例程, 该接口要求调用程序在 PPS 信号的准时跳变时刻捕获系统时间和硬件计数器内容, 并将指向时间戳 (Unix timeval) 和计数器内容的指针作为参数提供给 hardpps() 调用. 硬件计数器内容可以通过保存系统时间的微秒字段, 调用 microtime() 例程, 然后减去保存值来确定. 如果在此过程中发生了计数器溢出, 得到的微秒值将为负数, 此时调用者加上 1000000 以规范化微秒字段.
硬件振荡器的频率可由校准间隔开始和结束时的硬件计数器读数之差除以该间隔的持续时间确定. 然而, 振荡器频率容差 (高达 100 ppm) 可能使该差值超过滴答值, 从而产生歧义. 为避免这种歧义, 间隔开始时的硬件计数器值每秒增加一次当前 pps_ybar 值, 但按滴答值取模计算. 在间隔结束时, 该值与由硬件计数器计算出的值之间的差值被用作 FLL 的控制信号样本.
超过频率容差的控制信号样本, 以及由间隔持续时间抖动过大产生的样本都会被丢弃. 保留下来的样本随后经过三级中值滤波器处理. 驱动 FLL 的信号来自中值样本, 而另外两个样本之间差异的平均值用作离散度的度量. 如果离散度低于阈值 pps_dispmax, 则使用以移位 PPS_AVG (2) 表示的权重用中值校正 pps_ybar 值. 除平均功能外, pps_disp 每秒增加 pps_dispinc 的量. 结果是, 如果离散度异常高, 或 PPS 信号因某种原因失效, pps_disp 最终将超过 pps_dispmax 并发出告警.
最初, pps_ybar 的近似值未知, 因此校准间隔的持续时间必须保持较短, 以避免滴答溢出. 测量校准间隔结束时的
时间差. 如果大于 tick/4 的某个 fraction, 则间隔减半. 如果连续四个校准间隔都小于该 fraction, 则间隔加倍. 该设计自动适应 PPS 信号中的标称抖动以及 tick 的值. 校准间隔的持续时间由 pps_shift 变量以 2 的幂移位的形式设置. 选择最小值 PPS_SHIFT (2) 是为了在最高 CPU 振荡器频率 1024 Hz 和频率容差 100 ppm 下滴答不会溢出. 选择最大值 PPS_SHIFTMAX (8) 是为了使最大平均时间约为 1000 s, 这是由 Allan 方差测量 [5] 确定的.
如果 PPS 信号失效, 当前频率估计 pps_ybar 将继续使用, 因此标称频率仍然正确, 仅受未驯服振荡器不稳定性的影响. 保存和恢复频率估计的过程如下. 从文件设置频率时, time_freq 值设为文件值减去 pps_ybar 值; 检索频率时, 两个值相加后再保存到文件中. 如果 PPS 信号失效或内核配置发生变化, 该方案可提供无缝接口. 注意, 无论同步守护进程是否活动, 频率驯服都是活动的. 由于所有 Unix 系统在重启后都需要一些时间才能构建起运行中的系统, 通常到那时驯服过程已经稳定下来, 由频率驯服引起的初始瞬态已经衰减.
3.1.4. 外部时钟接口
外部时钟驱动程序接口通过两个例程实现: microtime() 返回当前时钟时间, clock_set() 提供从内核时间变量导出的表观系统时间. 后一个例程仅在使用 settimeofday() 系统调用设置时钟时调用, 但也可以从驱动程序内部调用, 例如在年份翻转时.
在原有 SunOS 内核以及修改后的 Ultrix 和 OSF/1 内核中, microtime() 例程返回内核时间变量加上基于硬件计数器内容的定时器中断间插值. 对于如上所述的外部时钟, 系统时钟直接从硬件时钟寄存器读取. 外部时钟驱动程序的示例见 kernel.tar.Z 发行版中包含的 tprotime.c 和 hightime.c 例程.
外部时钟例程返回状态码, 指示时钟是否正常工作, 如果不正常则指示问题的性质. 返回码由 ntp_gettime() 系统调用解释, 如果返回错误码, 则将状态状态机转换到 TIME_ERR 状态. 在当前版本的代码中, 这是针对外部时钟实现的唯一错误检查.
该模拟器已被用于检查 PLL 在设计范围内的运行, 该范围为时间误差 +-512 ms 和频率误差 +-100 ppm. 这证实了不会发生溢出, 并且对于 50 Hz 至 1024 Hz 的定时器中断速率, 环路最初大约在 15 分钟内收敛. 该环路通常有过冲百分之几, 最终收敛时间为数小时, 取决于初始时间和频率误差.
3.2. 闰秒
在用户应用接口中提供内核和同步协议私有的额外细节 (例如层级, 参考标识符, 参考时间戳等) 似乎通常并无用处. 原则上, 应用程序可以独立评估时间质量, 并预测该时间在未来可能保持 "有效" 多久. 然而, 要正确做到这一点, 将重复同步协议的功能, 并需要了解平台架构的许多平凡细节, 例如子网配置, 可达性状态和相关变量. 对于好奇者, 可以使用 ntp_adjtime() 系统调用来揭示其中一些奥秘.
然而, 用户应用可能需要知道是否计划了闰秒, 因为这可能影响跨越该事件的间隔计算. 闰警告条件由同步协议 (如果是远程同步), 时间码接收机 (如果可用), 或操作员 (如果清醒) 确定. 该条件由同步守护进程在闰秒将要发生的当天 (如所宣布的 6 月 30 日或 12 月 31 日) 设置, 方法是在 ntp_adjtime() 系统调用中指定时钟状态: 如果要删除一秒则为 TIME_DEL, 如果要插入一秒则为 TIME_INS. 注意, 自闰秒方案实施以来, 从未发生过删除的情况, 未来也不太可能发生. 如果值为 TIME_DEL, 内核在 23:59:58 秒之后立即向系统时间增加一秒, 并将时钟状态重置为 TIME_OK. 如果值为 TIME_INS, 内核在 23:59:59 秒之后立即从系统时间减去一秒, 并将时钟状态重置为 TIME_OOP, 实际上导致系统
时间重复第 59 秒. 紧接重复秒之后, 内核将时钟状态重置为 TIME_OK.
根据系统调用实现的不同, 闰秒期间报告的时间可能重复 (并设置 TIME_OOP 返回码以宣告这一事实), 也可能被单调调整直到系统时间 "追上" 报告时间. 采用后一种方案时, 在闰秒之前和之后短时间内报告的时间是正确的 (取决于闰秒期间调用 microtime() 的次数), 但在闰秒本身期间会冻结或缓慢前进. 不过, 大多数程序可能会使用 ctime() 库例程将 timeval (秒, 微秒) 格式转换为 tm 格式 (秒, 分钟, ...). 如果修改该例程以使用 ntp_gettime() 系统调用并检查返回码, 它就可以简单地将闰秒报告为第 60 秒.
3.3. 时钟状态状态机
本备忘录描述的系统时钟模型的各种可选特性, 要求在关键信号或接口失效时仔细考察状态转换, 状态指示和恢复过程. 本节给出一个原型状态机, 旨在支持闰秒的插入和删除, 以及揭示同步过程中的各种错误. 该状态机的状态解码如下:
TIME_OK If an external clock is present, it is working properly
and the system clock is derived from it. If no external
clock is present, the synchronization daemon is working
properly and the system clock is synchronized to a radio
clock or one or more peers.
TIME_INS An insertion of one second in the system clock has been
declared following the last second of the current day,
but has not yet been executed.
TIME_DEL A deletion of the last second of the current day has
been declared, but not yet executed.
TIME_OOP An insertion of one second in the system clock has been
declared following the last second of the current day.
The second is in progress, but not yet completed.
Library conversion routines should interpret this second
as 23:59:60.
TIME_BAD Either (a) the synchronization daemon has declared the
protocol is not working properly, (b) all sources of
outside synchronization have been lost or (c) an
external clock is present and it has just become
operational following a non-operational condition.
TIME_ERR An external clock is present, but is in a non-
operational condition.
In all except the TIME_ERR state the system clock is derived from
either an external clock, if present, or the kernel time variable,
if not. In the TIME_ERR state the external clock is present, but
not working properly, so the system clock may be derived from the
kernel time variable. The following diagram indicates the normal
transitions of the state machine. Not all valid transitions are
shown.
+--------+ +--------+ +--------+ +--------+
| | | | | | | |
|TIME_BAD|---->|TIME_OK |<----|TIME_OOP|<----|TIME_INS|
| | | | | | | |
+--------+ +--------+ +--------+ +--------+
A A
| |
| |
+--------+ +--------+
| | | |
|TIME_ERR| |TIME_DEL|
| | | |
+--------+ +--------+
The state machine makes a transition once each second at an
instant where the microseconds field of the kernel time variable
overflows and one second is added to the seconds field. However,
this condition is checked at each timer interrupt, which may not
exactly coincide with the actual instant of overflow. This may
lead to some interesting anomalies, such as a status indication of
a leap second in progress (TIME_OOP) when actually the leap second
had already expired.
The following state transitions are executed automatically by the
kernel:
any state -> TIME_ERR This transition occurs when an external
clock is present and an attempt is made to
read it when in a non-operational
condition.
TIME_INS -> TIME_OOP This transition occurs immediately
following second 86,400 of the current day
when an insert-second event has been
declared.
TIME_OOP -> TIME_OK This transition occurs immediately
following second 86,401 of the current
day; that is, one second after entry to
the TIME_OOP state.
TIME_DEL -> TIME_OK This transition occurs immediately
following second 86,399 of the current day
when a delete-second event has been
declared.
The following state transitions are executed by specific
ntp_adjtime() system calls:
TIME_OK -> TIME_INS This transition occurs as the result of a
ntp_adjtime() system call to declare an
insert-second event.
TIME_OK -> TIME_DEL This transition occurs as the result of a
ntp_adjtime() system call to declare a
delete-second event.
any state -> TIME_BAD This transition occurs as the result of a
ntp_adjtime() system call to declare loss
of all sources of synchronization or in
other cases of error.
The following table summarizes the actions just before, during and
just after a leap-second event. Each line in the table shows the
UTC and NTP times at the beginning of the second. The left column
shows the behavior when no leap event is to occur. In the middle
column the state machine is in TIME_INS at the end of UTC second
23:59:59 and the NTP time has just reached 400. The NTP time is
set back one second to 399 and the machine enters TIME_OOP. At the
end of the repeated second the machine enters TIME_OK and the UTC
and NTP times are again in correspondence. In the right column the
state machine is in TIME_DEL at the end of UTC second 23:59:58 and
the NTP time has just reached 399. The NTP time is incremented,
the machine enters TIME_OK and both UTC and NTP times are again in
correspondence.
No Leap Leap Insert Leap Delete
UTC NTP UTC NTP UTC NTP
---------------------------------------------
23:59:58|398 23:59:58|398 23:59:58|398
| | |
23:59:59|399 23:59:59|399 00:00:00|400
| | |
00:00:00|400 23:59:60|399 00:00:01|401
| | |
00:00:01|401 00:00:00|400 00:00:02|402
| | |
00:00:02|402 00:00:01|401 00:00:03|403
| | |
To determine local midnight without fuss, the kernel code simply
finds the residue of the time.tv_sec (or time.tv_sec + 1) value
mod 86,400, but this requires a messy divide. Probably a better
way to do this is to initialize an auxiliary counter in the
settimeofday() routine using an ugly divide and increment the
counter at the same time the time.tv_sec is incremented in the
timer interrupt routine. For future embellishment.