5. Local Clock Design
The full accuracy and reliability made possible by NTP requires careful design of the local-clock hardware and software. In the NTP model the local clock is assumed to use a quartz crystal oscillator without temperature compensation, which is typical of ordinary computing equipment. The fundamental system time reference, or logical clock, increments at some standard rate such as 1000 Hz and can be adjusted to precise time and frequency by means of periodic offset corrections computed by NTP, another time-synchronization protocol or a timecode receiver. A typical logical-clock implementation such as the Fuzzball [MIL89a] can maintain time in room-temperature environments to within a few milliseconds and frequency to within a few milliseconds per day in the absence of corrections. Substantially better performance can be achieved using precision oven-compensated quartz oscillators.
The NTP logical-clock model shown in Figure 5 can be represented as an adaptive-parameter, first-order, phase-lock loop, which continuously adjusts the clock phase and frequency to compensate for its intrinsic jitter, wander and drift. In the figure, x_i represents the reference timestamp and c_i the local timestamp of the ith update. The difference between these timestamps x_i − c_i is the input offset, which is processed by the clock filter. The clock filter previously saved the most recent offsets and selected one of them d_i-1 as the output offset. The loop filter, represented by the equations given below, produces the oscillator correction e_i, which is used to adjust the oscillator period. During the interval u_i until the next correction the clock is slewed gradually to the given value e_i. This is done in order to smooth the time indications and insure they are monotone increasing.
Figure 5. Phase-Lock Loop Model
The behavior of the phase-lock loop can be described by a set of recurrence equations, which depend upon several variables and constants. The variables used in these equations are (in SI units, unless specified otherwise):
di clock filter output offset
ui interval until next update
ei oscillator correction
fi frequency error
gi phase error
hi compliance
These variables are set to zero on startup of the protocol. In case the local clock is to be reset, rather than adjusted gradually as described below, the phase error g_i is set to zero, but the other variables remain undisturbed. Various constants determine the stability and transient response of the loop. The constants used in the equations, along with suggested values, are:
U 2^2 adjustment interval
Kf 2^10 frequency weight
Kg 2^8 phase weight
Kh 2^8 compliance weight
S 2^4 compliance maximum
T 2^18 compliance factor
Let d_i (i = 0, 1, ...) be a sequence of updates, with each d_{i+1} occurring u_i seconds after d_i. Let q = 1 − 1/K_g and n_i be the greatest integer in u_i/U; that is, the number of adjustments that occur in the i-th interval. As each update is received, the phase error g_i, frequency error f_i and compliance h_i are recomputed. Let a_i be a quantity called the frequency gain: a_i = max(S − T|h_i|, 1). Then, upon receipt of the d_i update:
gi+1 = di
fi+1 = fi + di / (ai-1 · ui-1) (f0, f1 = 0; i > 0)
hi+1 = hi + (di - hi) / Kh (h0 = 0)
At each adjustment interval the quantity g_{i+1}/K_g + f_{i+1}/K_f is added to the local clock and the quantity g_{i+1}/K_g subtracted from g_{i+1}. Thus, at the end of the i-th interval just before the d_{i+1} update, the accumulated correction is:
di q^ni - 1 1 ni nj · dj
ei+1 = ---- · --------- + ---- · ∑ ----------------
Kg q - 1 Kf j=1 aj-1 · uj-1
This can be seen to be the characteristic equation of an adaptive-parameter, first-order, phase-lock loop. Simulation of this loop with the variables and constants specified and the clock filter described previously results in the following characteristics: For a 100-ms phase change the loop reaches zero error in 39 minutes, overshoots 7 ms in 54 minutes and settles to less than 1 ms in about six hours. For a 50-ppm frequency change the loop reaches 1 ppm in about 16 hours and 0.1 ppm in about 26 hours. When the magnitude of correction exceeds a few milliseconds or a few ppm for more than a few minutes, the compliance begins to increase, which causes the frequency gain to decrease, eventually to unity, and the loop to loosen. When the magnitude of correction falls below about 0.1 ppm for a few hours, the compliance begins to decrease, which causes the frequency gain to increase, eventually to 16, and the loop to stiffen. The effect is to provide a broad capture range exceeding four seconds per day, yet the capability to resolve oscillator drift well below a millisecond per day. These characteristics are appropriate for typical crystal-controlled oscillators with or without temperature compensation or oven control.
When the magnitude of a correction exceeds 128 ms, the possibility exists that the logical clock is so far out of synchronization with the reference source that the best action is an immediate and wholesale replacement of the Clock Register contents, rather than a graduated slewing as described above. This particular value was selected by experiment and experience with the current implementations and operating procedures. In practice, this value is exceeded with a single time-server source only under conditions of the most extreme congestion or when multiple failures of nodes or links have occurred. The most common cause is when the time-server source is changed and the difference between the old and new times is too large due to systematic errors in the primary reference source or large differential delays an the synchronization paths.
Conversion to and from the common date and time formats used by application programs is simplified with separate date and time software registers. The time register is designed to roll over at 24 hours, with its overflows (underflows) incrementing (decrementing) the date register. On the day prior to the insertion of a leap second the leap-indicator bits are set at the primary servers, presumably by manual means, and subsequently distributed via NTP throughout the synchronization subnet. This causes the modulus of the time register, which is the length of the current day, to be increased or decreased by one second as appropriate. On the day following insertion the bits are turned off at the primary servers.