利用 PTP 同步时间

最近在学习如何利用 PTP 来同步两台计算机的时间,之前仅仅在研究 ptp4l。这算一个简单的篇章,仅记录一下,备忘。

首先,需要用到的工具有两个 ptp4l 和 phc2sys。第一个工具很熟悉了,是最基础的工具,不多作介绍,第二个从名字可以看出来其功能,phc to sys 即 PTP hardware clock to system。phc2sys -h如下,具体应用结合下面的命令来看。

$ sudo phc2sys -h

usage: phc2sys [options]


 automatic configuration:
 -a             turn on autoconfiguration
 -r             synchronize system (realtime) clock
                repeat -r to consider it also as a time source
 manual configuration:
 -c [dev|name]  slave clock (CLOCK_REALTIME)
 -d [dev]       master PPS device
 -s [dev|name]  master clock
 -O [offset]    slave-master time offset (0)
 -w             wait for ptp4l
 common options:
 -E [pi|linreg] clock servo (pi)
 -P [kp]        proportional constant (0.7)
 -I [ki]        integration constant (0.3)
 -S [step]      step threshold (disabled)
 -F [step]      step threshold only on start (0.00002)
 -R [rate]      slave clock update rate in HZ (1.0)
 -N [num]       number of master clock readings per update (5)
 -L [limit]     sanity frequency limit in ppb (200000000)
 -M [num]       NTP SHM segment number (0)
 -u [num]       number of clock updates in summary stats (0)
 -n [num]       domain number (0)
 -x             apply leap seconds by servo instead of kernel
 -z [path]      server address for UDS (/var/run/ptp4l)
 -l [num]       set the logging level to 'num' (6)
 -m             print messages to stdout
 -q             do not print messages to the syslog
 -v             prints the software version and exits
 -h             prints this message and exits

Master 端

master 端主要执行两个命令,第一个是关于 ptp4l 的:

$ sudo ptp4l -i enp3s0 -m -H

第二个是关于 phc2sys:

$ sudo phc2sys -s CLOCK_REALTIME -c enp3s0 -w -m

-s 是指主时钟,这里用的是 CLOCK_REALTIME,是系统时间。-c 是从时钟,enp3s0 是网卡的name。-w 是在等待 ptp4l,配合使用。-m 就是把消息打印到标准输出(stdout)。这个命令的目的是让网卡和master的系统时间同步,我们是通过网卡来实现时间同步的。

执行这两个命令,phc2sys 的执行结果如下,可以看到在等到 ptp4l 之后,网卡成功调整了时间。

$ sudo phc2sys -s CLOCK_REALTIME -c enp3s0 -w -m


phc2sys[125.887]: Waiting for ptp4l...
phc2sys[126.888]: Waiting for ptp4l...
phc2sys[127.888]: Waiting for ptp4l...
phc2sys[128.889]: Waiting for ptp4l...
phc2sys[129.891]: Waiting for ptp4l...
phc2sys[130.892]: Waiting for ptp4l...
phc2sys[131.892]: Waiting for ptp4l...
phc2sys[132.893]: Waiting for ptp4l...
phc2sys[133.894]: enp3s0 sys offset -37000100494 s0 freq      +0 delay  27055
phc2sys[134.895]: enp3s0 sys offset -37000108905 s1 freq   -8408 delay  28045
phc2sys[135.895]: enp3s0 sys offset       151 s2 freq   -8257 delay  28375
phc2sys[136.895]: enp3s0 sys offset       488 s2 freq   -7875 delay  27380
phc2sys[137.896]: enp3s0 sys offset     -1455 s2 freq   -9671 delay  34696
phc2sys[138.897]: enp3s0 sys offset        29 s2 freq   -8624 delay  34665
phc2sys[139.897]: enp3s0 sys offset       477 s2 freq   -8167 delay  34660
phc2sys[140.898]: enp3s0 sys offset       425 s2 freq   -8076 delay  34670
phc2sys[141.898]: enp3s0 sys offset       294 s2 freq   -8079 delay  34645
phc2sys[142.899]: enp3s0 sys offset       213 s2 freq   -8072 delay  34675
phc2sys[143.900]: enp3s0 sys offset        14 s2 freq   -8207 delay  34680
phc2sys[144.900]: enp3s0 sys offset        20 s2 freq   -8197 delay  34665
phc2sys[145.901]: enp3s0 sys offset         0 s2 freq   -8211 delay  34676
phc2sys[146.901]: enp3s0 sys offset       -18 s2 freq   -8229 delay  34665
phc2sys[147.902]: enp3s0 sys offset        22 s2 freq   -8194 delay  34656
phc2sys[148.903]: enp3s0 sys offset       -51 s2 freq   -8261 delay  34651
phc2sys[149.903]: enp3s0 sys offset       -62 s2 freq   -8287 delay  34690
phc2sys[150.904]: enp3s0 sys offset         1 s2 freq   -8243 delay  34655
phc2sys[151.993]: enp3s0 sys offset      1429 s2 freq   -6814 delay  27552
phc2sys[152.994]: enp3s0 sys offset     -1621 s2 freq   -9436 delay  34960
phc2sys[153.994]: enp3s0 sys offset      -535 s2 freq   -8836 delay  35136

Slave 端

slave 端先看看时间:

$ date
2023年 01月 01日 星期日 01:01:14 CST

这个时间是故意设置的错误时间。

然后看看要执行的命令,ptp4l 如下,不多作介绍:

sudo ptp4l -i enp2s0 -m -H -s

第二个是关于 phc2sys:

$ sudo phc2sys -a -r -m

-a 开启自动配置,-r 同步系统(实时)时钟,-m 就是输出消息。

这里的命令也可以换成下面这个,参考master端的命令很容易理解。即以网卡为主时钟,slave 的系统时间为从时钟,那么系统时间就会跟着网卡同步,而网卡的时间是通过 ptp4l 和 master 端同步的。

$sudo phc2sys -s enp2s0 -c CLOCK_REALTIME -w -m

ptp4l的执行结果:

ptp4l[105.028]: port 1: LISTENING to UNCALIBRATED on RS_SLAVE
ptp4l[106.028]: master offset -23374308124703652 s0 freq      +0 path delay         0
ptp4l[107.028]: master offset -23374308124707712 s1 freq   -4059 path delay      2742
ptp4l[108.028]: master offset       2762 s2 freq   -1297 path delay      2742
ptp4l[108.028]: port 1: UNCALIBRATED to SLAVE on MASTER_CLOCK_SELECTED
ptp4l[109.028]: master offset       2820 s2 freq    -411 path delay      2688
ptp4l[110.028]: master offset       1879 s2 freq    -506 path delay      2742
ptp4l[111.028]: master offset       1083 s2 freq    -738 path delay      2742
ptp4l[112.029]: master offset        480 s2 freq   -1016 path delay      2786
ptp4l[113.029]: master offset       -158 s2 freq   -1433 path delay      2632
ptp4l[114.029]: master offset        -89 s2 freq   -1441 path delay      2813
ptp4l[115.029]: master offset         86 s2 freq   -1293 path delay      2797
ptp4l[116.029]: master offset        128 s2 freq   -1225 path delay      2769
ptp4l[117.029]: master offset         78 s2 freq   -1237 path delay      2769
ptp4l[118.029]: master offset         74 s2 freq   -1217 path delay      2733
ptp4l[119.029]: master offset         17 s2 freq   -1252 path delay      2733
ptp4l[120.030]: master offset         52 s2 freq   -1212 path delay      2665
ptp4l[121.030]: master offset        -12 s2 freq   -1260 path delay      2665
ptp4l[122.030]: master offset        -26 s2 freq   -1278 path delay      2655
ptp4l[123.030]: master offset        -19 s2 freq   -1279 path delay      2643
ptp4l[124.030]: master offset        -11 s2 freq   -1277 path delay      2638
ptp4l[125.030]: master offset        -20 s2 freq   -1289 path delay      2635
ptp4l[126.030]: master offset        -19 s2 freq   -1294 path delay      2635
ptp4l[127.031]: master offset          4 s2 freq   -1277 path delay      2635
ptp4l[128.031]: master offset          7 s2 freq   -1272 path delay      2635
ptp4l[129.031]: master offset          3 s2 freq   -1274 path delay      2635
ptp4l[130.031]: master offset         -1 s2 freq   -1277 path delay      2635

phc2sys 的执行结果:

howard@howard-System-Product-Name:~$ sudo phc2sys -a -r -m
[sudo] password for howard: 
phc2sys[273.968]: reconfiguring after port state change
phc2sys[273.968]: selecting CLOCK_REALTIME for synchronization
phc2sys[273.968]: selecting enp2s0 as the master clock
phc2sys[273.968]: phc offset -23377916464248180 s0 freq      +0 delay  30651
phc2sys[274.969]: phc offset -23377916464250928 s1 freq   -2747 delay  30625
phc2sys[275.969]: phc offset       -44 s2 freq   -2791 delay  30617
phc2sys[276.969]: phc offset        23 s2 freq   -2737 delay  30652
phc2sys[277.970]: phc offset        31 s2 freq   -2722 delay  30648
phc2sys[278.970]: phc offset        -8 s2 freq   -2752 delay  30593
phc2sys[279.971]: phc offset        17 s2 freq   -2729 delay  30618
phc2sys[280.971]: phc offset      -113 s2 freq   -2854 delay  30617
phc2sys[281.971]: phc offset         9 s2 freq   -2766 delay  30640
phc2sys[282.972]: phc offset        47 s2 freq   -2725 delay  30661
phc2sys[283.972]: phc offset        -5 s2 freq   -2763 delay  30617
phc2sys[284.972]: phc offset         9 s2 freq   -2751 delay  30633
phc2sys[285.973]: phc offset       103 s2 freq   -2654 delay  30514
phc2sys[286.973]: phc offset      -111 s2 freq   -2837 delay  30615
phc2sys[287.974]: phc offset       -16 s2 freq   -2776 delay  30626
phc2sys[288.974]: phc offset         4 s2 freq   -2760 delay  30629
phc2sys[289.974]: phc offset        21 s2 freq   -2742 delay  30612
phc2sys[290.975]: phc offset       -14 s2 freq   -2771 delay  30615
phc2sys[291.975]: phc offset        -3 s2 freq   -2764 delay  30652
phc2sys[292.976]: phc offset        24 s2 freq   -2738 delay  30644

这时可以看到 slave 端的时间已经变了,和 master 保持一致了。

$ date 
2023年 09月 29日 星期四 14:56:12 CST

至此就完成了时间同步。

如果觉得这篇文章有用的话,可以点赞、评论或者收藏,万分感谢,goodbye~

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值