Time Synchronization

Precision Time Protocol (PTP)

Precision Time Protocol (PTP) is a protocol used to synchronize clocks in a networked environment with high accuracy.

PTP vs. NTP?

PTP provides higher accuracy (down to nanoseconds), while NTP is more suited for scenarios where millisecond accuracy is sufficient.

Many times, your computer doesn’t have built-in support for PTP timestamping.

Example of me trying it on a computer. That’s ok because your computer doesn’t rely on it.

steven@watothink1-ThinkPad-E590:~/Downloads$ ethtool -T enp4s0
Time stamping parameters for enp4s0:
Capabilities:
	software-transmit
	software-receive
	software-system-clock
PTP Hardware Clock: none
Hardware Transmit Timestamp Modes: none
Hardware Receive Filter Modes: none

How does PTP correct for errors?

PTP devices exchange timestamped messages to measure and compensate for network delays. The key steps include:

  1. Sync Message: The master device sends a sync message to the slave. This message includes the timestamp of when it left the master device (let’s call this T1).
  2. Follow-Up Message: The master sends a follow-up message to the slave with the exact time (T1) at which the sync message was transmitted. This accounts for the time taken by the master to prepare and send the sync message.
  3. Delay Request and Delay Response: The slave then sends a delay request message back to the master, which is timestamped as T2 when it leaves the slave. When the master receives this message, it timestamps the arrival time (T3) and sends back a delay response message with T3.
  4. Path Delay Calculation: With these four timestamps (T1, T2, T3, T4), the slave can calculate the path delay and the clock offset between itself and the master.
    • Path delay is estimated as:
    • Clock Offset is calculated as the difference between the slave’s local time and the corrected master time after accounting for the path delay.
  5. Clock Correction: Using the calculated offset and delay, the slave adjusts its clock to align with the master’s clock. Over time, regular exchanges of sync messages ensure the clocks remain synchronized, and the slave continually compensates for drift or network-induced delays.