Section 9.36

Wall clock and clock synchronization

Definition

A wall clock is a clock that reports the current date and time on a shared civil time scale, usually UTC (Coordinated Universal Time), the world’s reference time. A computer’s wall clock can be set, corrected and stepped backward. Clock synchronization is the process of estimating how two clocks relate, through their offset (the difference between their readings at the same instant) and their rate difference, and then either correcting one clock or converting readings from one clock to the other, with a stated bound on the error.

The idea

A host computer logs an event at 12:00:00.020 and a connected device logs another event at 12:00:00.010. The device’s event looks 10 milliseconds (ms) earlier. But if the host clock runs 20 ms ahead of the device clock, the host event happened at 12:00:00.000 in device time, 10 ms before the device event: the true order is the reverse of the printed one. Two timestamps written in the same format by two different clocks are readings of two instruments, not positions on one shared timeline.

Two people comparing wristwatches are the picture. If one watch is two minutes fast and also gains a second every hour, a note that says “meet at 3:00” needs both corrections, and the second one grows over time. Synchronization is the act of finding those corrections. The picture stops being accurate in one respect. People can look at both watches at the same instant, but two computers learn each other’s time only through messages that take time to travel, and that travel time leaves an uncertainty.

How it works

Over a stated interval, model a clock against a reference time tt, the time of a clock chosen as the reference: the clock reads approximately at+ba t + b. The offset bb is a constant shift. The rate aa says how fast the clock runs relative to the reference; a=1.00002a = 1.00002 means 20 parts per million (ppm) fast, so its error grows by 20 microseconds every second, or 0.00002×3600=0.0720.00002 \times 3600 = 0.072 s, which is 72 ms, every hour. This departure of the rate from 1 is called clock drift §6.18, and it changes slowly itself, for example with temperature. Whatever the model leaves over, the residual, collects reading noise and timestamping delays.

Offset from a message exchange. The Network Time Protocol (NTP) synchronizes computer clocks over networks this way, and the same exchange works between any host and device:

  1. The device notes its own clock reading t1t_1 and sends a request.

  2. The host notes its clock reading T2T_2 when the request arrives.

  3. The host notes T3T_3 when it sends the reply, and puts T2T_2 and T3T_3 into the reply.

  4. The device notes t4t_4 when the reply arrives.

The round-trip delay, the total time the two messages spent traveling, is δ=(t4−t1)−(T3−T2)\delta = (t_4 - t_1) - (T_3 - T_2). The estimated offset of the host clock relative to the device clock is θ=((T2−t1)+(T3−t4))/2\theta = ((T_2 - t_1) + (T_3 - t_4)) / 2. The estimate is exact when the two directions take equal time. When they do not, the estimate is wrong by up to δ/2\delta / 2.

Rate from repeated exchanges. Repeat the exchange after a known interval. The change in the offset divided by the elapsed time is the rate difference. With offset and rate known, a reading taken at any time between or shortly after the exchanges can be converted from one clock to the other.

Other ways to relate clocks.

How a wall clock is corrected. Stepping sets it to a new value at once, possibly backward. Slewing makes it run slightly faster or slower until the error is gone. UTC also inserts leap seconds, occasional extra seconds that keep it close to the Earth’s rotation. Most systems count seconds since 1970 without leap seconds, a count called Unix time, so around a leap second their clock repeats one second, or is smeared, meaning it runs slightly slow for some hours to absorb the extra second. Each of these corrections makes differences of wall-clock readings unreliable as durations; local durations are measured with a monotonic clock §9.35, a clock whose readings never decrease.

Worked example

An exchange with real values, all in milliseconds. The device sends at t1=5000t_1 = 5000 on its own clock. The host receives at T2=5025T_2 = 5025 and replies at T3=5026T_3 = 5026 on its clock. The device receives at t4=5011t_4 = 5011. The round-trip delay is δ=(5011−5000)−(5026−5025)=11−1=10\delta = (5011 - 5000) - (5026 - 5025) = 11 - 1 = 10 ms. The offset is θ=((5025−5000)+(5026−5011))/2=(25+15)/2=20\theta = ((5025 - 5000) + (5026 - 5011)) / 2 = (25 + 15) / 2 = 20 ms: the host clock is 20 ms ahead. The error of this estimate is at most δ/2=10/2=5\delta / 2 = 10 / 2 = 5 ms, so the true offset lies between 20−5=1520 - 5 = 15 and 20+5=2520 + 5 = 25 ms.

Ten minutes later, after 600,000 ms of host time, a second exchange gives an offset of 32 ms. The offset grew by 32−20=1232 - 20 = 12 ms in 600,000 ms, a rate difference of 12/600,000=0.0000212 / 600{,}000 = 0.00002, which is 20 ppm: the device clock loses 0.00002×1000=0.020.00002 \times 1000 = 0.02 ms every second relative to the host. For a device reading taken 300,000 ms of host time after the first exchange, the offset to apply is 20+0.00002×300,000=20+6=2620 + 0.00002 \times 300{,}000 = 20 + 6 = 26 ms.

Now order two events. A device event has the device reading 7010 ms, taken while the offset is 20 ms with an error bound of 5 ms. In host time it happened at 7010+20=70307010 + 20 = 7030 ms. A host event has the host reading 7022 ms. Each side’s own timestamp is good to 1 ms. The difference is 7030−7022=87030 - 7022 = 8 ms, and its error is at most 5+1+1=75 + 1 + 1 = 7 ms, so the true difference lies between 8−7=18 - 7 = 1 and 8+7=158 + 7 = 15 ms. The whole range is positive: the device event came after the host event. Had the difference been 4 ms, the range would run from 4−7=−34 - 7 = -3 to 4+7=114 + 7 = 11 ms. That range contains 0, so the data cannot say which event came first.

Stated precisely

Let tt be reference time. Model the reading of clock XX as CX(t)=aXt+bX+eX(t)C_X(t) = a_X t + b_X + e_X(t), where aXa_X is the rate (a pure number, 1 for a perfect clock), bXb_X is the offset in time units and eX(t)e_X(t) is the residual in time units. The model is an approximation valid over a stated interval; a restart, a step correction or a change of temperature can end that interval.

For one exchange, with t1t_1 and t4t_4 read on the device clock and T2T_2 and T3T_3 read on the host clock, δ=(t4−t1)−(T3−T2),θ=(T2−t1)+(T3−t4)2.\delta = (t_4 - t_1) - (T_3 - T_2), \qquad \theta = \frac{(T_2 - t_1) + (T_3 - t_4)}{2}. If both one-way travel times are nonnegative and the two clocks run at the same rate during the exchange, the true offset of the host clock relative to the device clock lies between θ−δ/2\theta - \delta / 2 and θ+δ/2\theta + \delta / 2.

Ordering rule. Let xx be the difference between two event times after both are mapped to one time base, and let uu be a bound on the total error of that difference: the sum of the error bound of the mapping and the error bounds of the two timestamps. The order of the two events is established when |x|>u|x| > u. When |x|≤u|x| \le u, the order is unknown.

Limits and common mistakes

Synchronized clocks do not make a timestamp name the right event. Software reads the clock some time after the event, for example after an interrupt handler §6.12 starts running; that delay belongs to the timestamp’s error, however good the synchronization.

A mapping expires. A restart resets a device’s counter, a step correction moves a wall clock, and temperature changes the rate. Measure again, or bound how far the error can have grown since the last measurement.

Wall-clock differences are not durations. A step or a leap-second correction during an interval changes the difference. Measure local durations with a monotonic clock.

Printed precision is not accuracy. A timestamp written to the microsecond can be milliseconds wrong.

The exchange cannot see asymmetric delay. If one direction is always slower than the other, the offset estimate is off by up to half the round-trip delay, and repeating the exchange does not reveal it; only an independent reference does.

Local time is not UTC. Time zones and daylight saving time shift displayed times by hours, or in some zones by parts of an hour. Store and compare UTC.

Questions answered

How do you compare timestamps from host and device clocks?

Convert both timestamps to one time base using an estimated offset and rate between the two clocks, and compare them only together with the error bound of that conversion. The offset is the difference between the two clocks’ readings at the same instant; the rate difference says how fast that offset grows. Estimate them with timed message exchanges, as the Network Time Protocol (NTP) does, or by timestamping one shared physical signal on both sides, and repeat the estimate often enough that the offset cannot drift beyond the error bound between estimates.

Then add up the error bounds: the conversion error plus the timestamping error on each side. Two events are ordered only when their mapped difference is larger than that total; otherwise their order is unknown. Never subtract raw readings of two clocks with unrelated zero points. Record with the result how and when the mapping was made, the measured round-trip delay, and the assumption that both directions take equal time.

Related entries: Monotonic clock (§9.35), Counter quantization (§11.35), Measurement uncertainty (§11.33), Clock source and drift (§6.18), Clock domain (§13.29), Resolution versus precision versus accuracy (§11.34). Further reading: [33, 301].