当前位置:   article > 正文

Linux时间同步(PPS、PTP、chrony)分析笔记

Linux时间同步(PPS、PTP、chrony)分析笔记

1 PPS(pulse per second)

1.1 简介

LinuxPPS provides a programming interface (API) to define in the system several PPS sources.


PPS means "pulse per second" and a PPS source is just a device which provides a high precision signal each second so that an application can use it to adjust system clock time.

A PPS source can be connected to a serial port (usually to the Data Carrier Detect pin) or to a parallel port (ACK-pin) or to a special CPU's GPIOs (this is the common case in embedded systems) but in each case when a new pulse arrives the system must apply to it a timestamp
and record it for userland.

                                                                                        《<kernel_src>/Documentation/pps/pps.txt》

1.2 PPS时间同步信号的输入源

1.2.1 串口信号中的DCD(Data Carrier Detect)信号

1.2.1.1 普通串口

以drivers/tty/serial/amba-pl010.c驱动为例

  1. pl010_int();
  2. -> pl010_modem_status();
  3. -> uart_handle_dcd_change();
  4. -> ld->ops->dcd_change();
  5. -> pps_tty_dcd_change();
  6. -> pps_event()

1.2.1.2 USB串口

以drivers/usb/serial/pl2303.c驱动为例

  1. pl2303_read_int_callback();
  2. -> pl2303_update_line_status();
  3. -> usb_serial_handle_dcd_change();
  4. -> ld->ops->dcd_change();
  5. -> pps_tty_dcd_change();
  6. -> pps_event()

1.2.3 GPIO

1.2.3.1 在设备树中指定使用哪个GPIO

例如

  1. pps {
  2. compatible = "pps-gpio";
  3. pinctrl-names = "default";
  4. pinctrl-0 = <&pinctrl_pps>;
  5. gpios = <&gpio1 26 GPIO_ACTIVE_HIGH>;
  6. status = "okay";
  7. };
1.2.3.2 驱动

drivers/pps/clients/pps-gpio.c

  1. pps_gpio_irq_handler();
  2. -> pps_event()

1.2.4 并口

drivers/pps/clients/pps_parport.c

  1. parport_irq();
  2. -> pps_event();

1.2.5 PTP

1.2.5.1 判断网卡是否打开PPS功能

/sys/class/ptp/ptp0/pps_available

        This file indicates whether the PTP hardware clock supports a Pulse Per Second to the host CPU.        

/sys/class/ptp/ptp0/pps_enable

        This write-only file enables or disables delivery of PPS events to the Linux PPS subsystem.

1.2.5.2 驱动

以Intel的网卡驱动为例

drivers/net/ethernet/intel/igb/igb_main.c

  1. igb_tsync_interrupt();
  2. -> ptp_clock_event();
  3. -> pps_event();

1.3 文件操作接口/dev/ppsX

  1. /*
  2. * Char device stuff
  3. */
  4. static const struct file_operations pps_cdev_fops = {
  5. .owner = THIS_MODULE,
  6. .llseek = no_llseek,
  7. .poll = pps_cdev_poll,
  8. .fasync = pps_cdev_fasync,
  9. .compat_ioctl = pps_cdev_compat_ioctl,
  10. .unlocked_ioctl = pps_cdev_ioctl,
  11. .open = pps_cdev_open,
  12. .release = pps_cdev_release,
  13. };

1.4 在chrony服务中使用PPS作为时间源

在配置文件/etc/chrony/chrony.conf中添加以下内容

  1. refclock SHM 0 poll -2 refid GPS precision 1e-1 offset 0.9999 delay 0.2
  2. refclock PPS /dev/pps0 lock NMEA refid PPS

执行"chronyc sources" 命令,出现以下结果表示配置成功

  1. # chronyc sources | grep PPS
  2. #- PPS 0 4 77 16 -309ms[ -309ms] +/- 29ms

2 PTP(precise time protocol; IEEE 1588协议)

2.1 简介

IEEE 1588 addresses the clock synchronization requirements of measurement and control systems. The protocol supports system-wide synchronization accuracy in the sub-microsecond range with minimal network and local clock computing resources.

                                                                        《Intel ® Ethernet Controller I350 Datasheet》P457

2.2 判断网卡是否支持PTP

PTP功能需要网卡硬件支持,可通过ethtool -T  xxx来查看

出现以下信息,表示网卡硬件支持PTP

  1. # ethtool -T enp0s31f6
  2. Time stamping parameters for enp0s31f6:
  3. Capabilities:
  4. hardware-transmit (SOF_TIMESTAMPING_TX_HARDWARE)
  5. software-transmit (SOF_TIMESTAMPING_TX_SOFTWARE)
  6. hardware-receive (SOF_TIMESTAMPING_RX_HARDWARE)
  7. software-receive (SOF_TIMESTAMPING_RX_SOFTWARE)
  8. software-system-clock (SOF_TIMESTAMPING_SOFTWARE)
  9. hardware-raw-clock (SOF_TIMESTAMPING_RAW_HARDWARE)
  10. PTP Hardware Clock: 0
  11. Hardware Transmit Timestamp Modes:
  12. off (HWTSTAMP_TX_OFF)
  13. on (HWTSTAMP_TX_ON)

2.3 在chrony服务中使用PTP作为时间源

在配置文件/etc/chrony/chrony.conf中添加以下内容

refclock PHC /dev/ptp0 poll 2

执行"chronyc sources" 命令,出现以下结果表示配置成功 

  1. # chronyc sources | grep PHC
  2. #x PHC2 0 2 377 5 +39.6s[ +39.6s] +/- 1139us

缩写:

        PHC: PTP hardware clock 

2.4 /sys/class/ptp/ptpN/

2.5 扩展知识:虚拟机和物理机之间的时间同步(ptp_kvm)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/191099
推荐阅读
相关标签
  

闽ICP备14008679号