Precision Time Protocol (PTP) Algorithm
Published: Sat Sep 20 2025
Every computer system has a Real Time Clock (RTC) on the motherboard. If the initial time is set, it keeps ticking even when the system is powered off. The component that keeps track of time is called a quartz crystal oscillator. It works by the inverse piezoelectric effect. This crystal material will be placed between two metal plates, and when a voltage, typically biasing voltage (Vcc), is introduced to an oscillator circuit, the crystal will start vibrating at its natural frequency. This vibration will be sensed by the amplifier in the oscillator circuit and fed back to the crystal again. Here’s where the time calculation kicks in.
A crystal material oscillates or vibrates 32,768 times per second. So the frequency of the crystal material is 32,768 Hz.
frequency = number of cycles / second In the oscillator circuit, we have 15 flip-flops connected in series to divide 32,768. Each flip-flop will divide the input value by 2. So the 15th flip flop should get a quotient of 1 to conclude that the crystal material exhibited a frequency of 32,768 Hz. The flip-flops act as a counter here.
The drawback of using RTC is that we don’t have any global reference. In a network that has two systems which is geographically separated and in different timezones should need to have the time reference to timestamp packets.
For instance, consider a trading network. If an exchange receives two orders (two packets) for the same asset instrument, it needs to pick the one that arrived first. Often the difference will be nanoseconds. This is one application of having the same time reference between the systems (servers, switches, and more) to timestamp the packets.
To solve the time reference problem, NTP and PTP were introduced.
In NTP, every device in the network will sync the time from one device. That one device syncs time from accurate clocks like GPS receivers, atomic clocks, etc.
PTP
In a network, as packets are bridged and routed by the intermediate devices, there is a time associated with each bridging and routing operation. NTP won’t take that switching time into account. This is solved by PTP.
In short, PTP-aware devices use a mechanism to calculate the delay time for the PTP packets that are received from their peers. This delay time will be adjusted to maintain the system clock.
Say there are two devices A and B. Let’s assume B is syncing time from A using PTP. The objective is that B should calculate both delay and the offset.
Delay: Time taken in the path Offset: Time difference between two devices when a message is received.
The following is the simplified version of the PTP offset and delay calculation algorithm:
- A sends two messages to B, Sync and Follow-up.
- Sync Message contains the current time of A. As the PTP packets are generated by the software, A doesn’t know when the sync packet is sent on the wire.
- The hardware captures the sent timestamp and communicates it to the software. A can now be sent this time in the follow-up packet.
- B records the time when it receives Sync and Follow-up messages from A.
The time that A sends in a follow-up message is t1, and the time that B records it is taken as t2.
Offset from A = (t2 - t1) - delay The final block to calculate delay:
- B will send out a delay request packet. The time when B sends will be marked as t3. This is like B is asking A to send the time upon receiving its packet.
- A will send a delay response upon receiving a delay request packet from B. This is taken as t4.
Message Intervals: The sync message interval is 2^-3, which is 0.125 s (8 messages per second). The announce message interval is 2^0, which is 1s (1 message per second). The delay request message interval is 2^-3, which is 0.125 s (8 messages per second).
Derivation to find Mean Path Delay
offset + Df = (t2 - t1) --- equation 1
Dr = forward delay
t4 = t3 + Dr - Offset --- equation 2
Dr = reverse delay
Solving equations 1 and 2:
t2 - t1 = Df + offset
t4 - t3 = Dr - offset
(t2 - t1) + (t4 - t3) = Df + Dr
Mean Path Delay (MPD) = (Df + Dr) / 2
Mean Path Delay (MPD) = ((t2 - t1) + (t4 - t3)) / 2