Highly accurate system time
On Time for All Time
After the idea of procuring an atomic clock failed to thrill the other members of Charly's household, our intrepid columnist simply decided to tap into the timekeeping of a GPS satellite. In doing so, he ensured the kind of punctuality at home that only large data centers actually need. Precisely.
The network time protocol (NTP) is one of the easiest server-based services to configure. The ntp.conf
file requires minimal configuration; just one line with a source from which to tell the time.
pool de.pool.ntp.org iburst
You usually specify more than one source. The NTP daemon (ntpd) queries it cyclically and tries to compute running time differences caused by network latency. My local NTP service runs on a Raspberry Pi (Rasp Pi), otherwise employed full-time in driving the garden irrigation system, and has a time imprecision of 30 to 40msec.
This may be almost indecently accurate for my always slightly chaotic household, but logging with millisecond accuracy is a genuine requirement for data centers. How far can I take this newly inspired punctuality madness?
First of all, I need to reduce the stratum. A highly accurate time source that makes its time signal available to the public is a stratum-0 device. A server that requests the time from it, and distributes the results, is a stratum-1 server, and so on.
The obvious idea of buying an atomic clock strangely failed to meet approval in our family council. This prompted me to provide my Rasp Pi with a GPS receiver – any GPS satellite is a stratum-0 time source. The GPS daemon, included in the scope of most distributions, provides the time signal to the NTP server via a virtual interface. I then added two lines to the ntp.conf
file to introduce the NTP daemon to the address:
server 127.127.28.0 minpoll4 noselect fudge 127.127.28.0 time1 0.0 refid GPS
That's better; however, you can achieve even more precision, because transporting the data through the serial interface can still produce slight variations. You can compute these yourself: GPS satellites do not just transmit the time, but also a pulse per second (PPS) signal. These are short, high-precision pulses output every second.
A small tool named rpi_gpio_ntp
[1] by programmer Folkert van Heusden makes the PPS signal accessible to the time server. Again, a virtual IP is used for this, which I entered in my ntp.conf
:
server 127.127.28.1 minpoll 4 prefer fudge 127.127.28.1 refid UPPS
This was amazingly successful. My Munin graph in Figure 1, which shows the fluctuations of the time signal, flattens out to a smooth line after firing up the GPS-PPS combination – not bad for a stratum-1 time server.
Time To Go Time
Ntpd is currently dying out on clients in the wake of systemd, replace by timesyncd. Although timesyncd is leaner, it does not propagate to the clients. I configured it in my /etc/systemd/timesyncd.conf
file so that it primarily uses my irrigation Rasp Pi and only turns into Internet time servers in an emergency:
NTP=gpspi FallBackNTP=de.pool.ntp.org 0.pool.ntp.org 1.pool.ntp.org
If the day ever comes when I have accumulated enough hardware to require high-precision logging, now I'm prepared.
Infos
- rpi_gpio_ntp: https://vanheusden.com/time/rpi_gpio_ntp/
Buy this article as PDF
(incl. VAT)