时钟同步软件ptp4l简介、安装、使用

一、ptp4l简介

ptp4l 是 Linux 下的一个开源软件,是 IEEE 1588 Precision Time Protocol (PTP) 实现之一。它的功能是通过网络同步多个系统的时钟,并提供高精度的时间戳。下面分别解释其概念、实现功能、优缺点、应用场景。

概念:

PTP 是一种网络协议,用于在分布式系统中实现时钟同步。ptp4l (PTPv2 Linux implementation)是一个 PTP 协议的实现,支持 PTPv2 协议、时钟精度等级(clock precision)选择、延迟补偿等功能。其可以通过网络同步多个 PTP 节点的时钟,并提供高精度的时间戳。

实现功能:

ptp4l 常用于实现时钟同步和高精度时间戳,可以用于音视频同步、数据采集与处理、金融交易、电力系统同步、工业控制等领域。它提供以下主要功能:

  1. 实现时钟同步:ptp4l 可以通过协议同步多个节点的时钟,以达到高精度的时间同步。

  2. 提供高精度时间戳:ptp4l 可以为接收到的数据进行时间戳,并且提供子纳秒级别的时间精度。

  3. 支持不同精度等级:ptp4l 可以支持不同的时钟精度等级,以达到更高精度的时钟同步。

  4. 支持延迟补偿:ptp4l 可以诊断网络延迟,并根据延迟对时钟进行补偿。

优点:

  1. 高精度:ptp4l 提供子纳秒级别的时间精度,可满足对时间精度要求较高的场景。

  2. 可扩展性:ptp4l 可以通过协议同步多个节点的时钟,可以扩展到分布式系统中。

  3. 灵活性:ptp4l 可以通过配置不同的时钟精度等级、延迟补偿等参数,以适应不同的应用场景。

缺点:

  1. 受限于硬件支持:ptp4l 的时间同步精度受限于硬件支持,如果硬件不支持高精度时钟同步,则精度会受到限制。

  2. 依赖于网络:ptp4l 通过网络实现时钟同步,因此受网络延迟、带宽等因素的影响。

二、应用场景:

ptp4l 主要应用于需要高精度时钟同步的场景,如音视频同步、数据采集与处理、金融交易、电力系统同步、工业控制等领域。在这些场景中,需要对不同系统中的时钟进行同步,以达到高精度和一致性,从而保证系统的稳定性和正常运行。

以下是在 CentOS 7 上安装和使用 LinuxPTP 的简要步骤:

  1. 打开终端,使用以下命令安装 LinuxPTP:

    sudo yum install linuxptp
    

  2. 验证 LinuxPTP 是否已成功安装。在终端中输入以下命令:

    ptp4l -v
    

    如果显示 LinuxPTP 版本号,则表示安装成功。

  3. 配置 LinuxPTP。在终端中输入以下命令:

    sudo nano /etc/ptp4l.conf
    

    打开配置文件并根据需要进行更改。例如,您可以更改时钟设置、物理网络接口、PTP 网络接口等。

  4. 启动 LinuxPTP。在终端中输入以下命令:

    [root@localhost ~]# ptp4l -i enp1s0 -f /etc/ptp4l.conf -m
    ptp4l[519569.670]: selected /dev/ptp0 as PTP clock
    ptp4l[519569.672]: port 1: INITIALIZING to LISTENING on INIT_COMPLETE
    ptp4l[519569.672]: port 0: INITIALIZING to LISTENING on INIT_COMPLETE
    ptp4l[519577.354]: port 1: LISTENING to MASTER on ANNOUNCE_RECEIPT_TIMEOUT_EXPIRES
    ptp4l[519577.354]: selected local clock 001b21.fffe.c4522a as best master
    ptp4l[519577.354]: assuming the grand master role
    
    

    这将启动 PTP 守护程序并将其配置为使用 eth0 接口,以 /etc/linuxptp/ptp4l.conf 中指定的设置运行。

  5. 检查时钟同步情况。在终端中输入以下命令:

    [root@localhost ~]# phc2sys -s enp1s0 -w CLOCK_REALTIME -m
    phc2sys[519870.160]: CLOCK_REALTIME phc offset      -172 s0 freq  +44777 delay    749
    phc2sys[519871.161]: CLOCK_REALTIME phc offset      -177 s2 freq  +44772 delay    758
    phc2sys[519872.161]: CLOCK_REALTIME phc offset      -173 s2 freq  +44599 delay    758
    phc2sys[519873.161]: CLOCK_REALTIME phc offset       -25 s2 freq  +44695 delay    743
    phc2sys[519874.161]: CLOCK_REALTIME phc offset        44 s2 freq  +44757 delay    745
    phc2sys[519875.161]: CLOCK_REALTIME phc offset        34 s2 freq  +44760 delay    751
    phc2sys[519876.162]: CLOCK_REALTIME phc offset        61 s2 freq  +44797 delay    745
    phc2sys[519877.162]: CLOCK_REALTIME phc offset     42355 s2 freq  +87109 delay    749
    phc2sys[519878.162]: CLOCK_REALTIME phc offset       122 s2 freq  +57583 delay    757
    phc2sys[519879.162]: CLOCK_REALTIME phc offset    -12775 s2 freq  +44722 delay    745
    phc2sys[519880.162]: CLOCK_REALTIME phc offset    -12753 s2 freq  +40912 delay    754
    phc2sys[519881.163]: CLOCK_REALTIME phc offset     -8903 s2 freq  +40936 delay    744
    
    

    这将将 PHC(Ptp Hardware Clock)与真实时钟同步,从而确保时钟同步。

以上是在 CentOS 7 上安装和使用 LinuxPTP 的基本步骤,实际上,您还需要了解更多关于 LinuxPTP 的详细配置和使用方法,以确保系统正常运行。

常用参数

ptp4l 常用参数含义如下:

usage: ptp4l [OPTION]... [INTERFACE]...

ptp4l is an implementation of the Precision Time Protocol (PTP) according
to IEEE standard 1588 for Linux.  PTP is used to synchronize the clocks of
devices over a network. ptp4l, in conjunction with phc2sys, can be used to
synchronize the system clock to an external PTP clock.

OPTION list:
  -C, --config-file     load configuration from file
  -g, --global-override enable global override of settings in configuration files
  -i, --interface       specify an interface to use
  -m, --management      enable the PTP management messages
  -p, --priority        select the priority1 and priority2 for PTP port
  -s, --step-seconds    step the clock if offset is greater than given seconds
  -A, --announce-interval
                        interval in seconds between sending announce messages
  -D, --drift-threshold drift threshold for the clock in seconds per second (default 1e-6)
  -G, --domain          specify PTP domain number (default 0)
  -H, --pdelay-req-hops number of hops for pdelay-req messages (default 1)
  -I, --init-delay      initial delay before sending first announce message (seconds)
  -M, --max-adjustable-offset
                        the maximum offset the clock can be adjusted in seconds
  -O, --outlier-threshold
                        specify outlier threshold for the clock discipline
  -P, --phc2sys         enable phc2sys compatibility mode
  -Q, --ptpengine-debug output debug information for ptpengine (use multiple times for more detail)
  -R, --hw-timestamps   use hardware timestamps instead of software
  -S, --src-addr        specify source IPv4 or IPv6 address (receiver mode only)
  -T, --unicast-address specify unicast destination address instead of multicast
  -U, --user-description
                        specify user description for PTP port
  -V, --version         print version number and exit
  -W, --override-offset-allowed
                        override the offset if the difference is outside acceptable limits (default false)
  -X, --set-timeout     specify timeout in milliseconds for message receptions
  -Y, --sync-rate-limit
                        limit the number of sync messages propagated per second
  -Z, --pid-file        write the PID of ptp4l to a file
  -d, --debug-level     output debugging information (use multiple times for more detail)
  -f, --foreground      stay in foreground and output messages to stdout
  -h, --help            display this help and exit
  -l, --lock-file       lock file used for PID file (default /var/run/ptp4l.lock)
  -o, --one-step        enable the one-step clock mode
  -r, --priority1       specify PTP priority1 value (default 128)
  -t, --priority2       specify PTP priority2 value (default 0)
  -u, --unicast-dest    specify unicast destination for PTP messages
  -v, --verbose         output verbose messages

See the ptp4l(8) man page for more information.

其中一些常用的参数的使用方法如下:

  • -C, --config-file: 指定配置文件的位置,例如 -C /etc/ptp4l.conf
  • -i, --interface: 指定要使用的接口名称,例如 -i eth0
  • -m, --management: 启用 PTP 管理消息,例如 -m
  • -s, --step-seconds: 如果时钟偏移大于给定的秒数,则步进时钟,例如 -s 1
  • -A, --announce-interval: 指定发送 announce 消息的间隔时间,例如 -A 30
  • -M, --max-adjustable-offset: 指定时钟可以调整的最大偏移量,例如 -M 0.1
  • -Q, --ptpengine-debug: 输出 ptpengine 的调试信息。您可以使用多次 -Q 选项来获取更多的调试信息,例如 -Q -Q -Q
  • -d, --debug-level: 输出调试信息,使用多个 -d 选项可以获取更详细的调试信息,例如 -d -d -d
  • -f, --foreground: 以前台进程方式运行,将消息输出到标准输出中,例如 -f
  • -h, --help: 显示帮助信息,例如 -h
  • -v, --verbose: 打印详细的消息,例如 -v

这些参数的使用方法可以结合具体的应用场景来确定。

[root@localhost ~]# ptp4l -h

usage: ptp4l [options]

 Delay Mechanism

 -A        Auto, starting with E2E
 -E        E2E, delay request-response (default)
 -P        P2P, peer delay mechanism

 Network Transport

 -2        IEEE 802.3
 -4        UDP IPV4 (default)
 -6        UDP IPV6

 Time Stamping

 -H        HARDWARE (default)
 -S        SOFTWARE
 -L        LEGACY HW

 Other Options

 -f [file] read configuration from 'file'
 -i [dev]  interface device to use, for example 'eth0'
           (may be specified multiple times)
 -p [dev]  PTP hardware clock device to use, default auto
           (ignored for SOFTWARE/LEGACY HW time stamping)
 -s        slave only mode (overrides configuration file)
 -t        transparent clock
 -l [num]  set the logging level to 'num'
 -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

附:报错:failed to create a clock

ptp4l[519360.082]: interface 'enp4s0' does not support requested timestamping mode
failed to create a clock

这个错误通常出现在 Linux 系统上,表明 ptp4l 无法在指定的网络接口上启用 PTP 时间戳模式。可能的原因是:

  1. 网络接口不支持 PTP 时间戳模式。某些网络接口可能不支持硬件时间戳或软件时间戳,因此无法用于 PTP 同步。

  2. 没有足够的权限来启用 PTP 时间戳模式。在某些系统中,需要以管理员或特权用户身份运行 ptp4l,才能访问必要的系统资源。

为了解决这个问题,可以尝试以下步骤:

  1. 检查网络接口是否支持 PTP 时间戳模式。可以使用 ethtool 命令来查看网络接口是否支持硬件时间戳或软件时间戳。例如,使用以下命令查看 enp4s0 网络接口的时间戳能力:

    sudo ethtool -T enp4s0
    

    如果输出中包含 PTPv2(IEEE 1588),则表示该网络接口支持 PTP 时间戳模式。如果没有该选项,则表示网络接口不支持 PTP 时间戳模式。

  2. 确认当前用户是否具有足够的权限启用 PTP 时间戳模式。可以尝试使用 sudo 命令以管理员或特权用户身份运行 ptp4l。

  3. 如果确认网络接口支持 PTP 时间戳模式且当前用户具有足够的权限,但仍然遇到该错误,请尝试升级系统内核或驱动程序,以确保网络接口驱动程序支持 PTP 时间戳模式。

希望以上步骤可以帮助您解决这个问题。

接口不支持PTP时间戳模式:

 接口支持PTP时间戳模式:

猜你喜欢

转载自blog.csdn.net/songpeiying/article/details/131826141
今日推荐