0

I am working on a sensor system, which runs on an embedded Linux platform and doesn't have access to the internet. An external GPS time server device is connected to provide time synchronization using the chrony daemon.

My c++ application reads measurements at 50Hz, with an associated timestamp, however, I would like to set a flag in the data to report if the system time is correct.

By command line timedatectl can be used to report the "NTP Synchronised" status.

:~$ timedatectl
      Local time: Wed 2020-08-19 17:03:01 BST
  Universal time: Wed 2020-08-19 16:03:01 UTC
        RTC time: Wed 2020-08-19 16:03:01
       Time zone: Europe/London (BST, +0100)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

And the status of the chrony daemon will report system clock adjustments (note that the large time adjustment was caused by some phony date set for test purposes).

sudo systemctl status chrony
● chrony.service - LSB: Controls chronyd NTP time daemon
   Loaded: loaded (/etc/init.d/chrony; bad; vendor preset: enabled)
   Active: active (running) since Mon 2006-10-02 18:00:28 BST; 13 years 10 months ago
     Docs: man:systemd-sysv-generator(8)
  Process: 3201 ExecStop=/etc/init.d/chrony stop (code=exited, status=0/SUCCESS)
  Process: 3240 ExecStart=/etc/init.d/chrony start (code=exited, status=0/SUCCESS)
    Tasks: 1
   Memory: 340.0K
      CPU: 90ms
   CGroup: /system.slice/chrony.service
           └─3247 /usr/sbin/chronyd

Oct 02 18:00:26 systemd[1]: Starting LSB: Controls chronyd NTP time daemon...
Oct 02 18:00:26 chronyd[3247]: chronyd version 2.1.1 starting (+CMDMON +NTP +REFCLOCK +RTC +PR
Oct 02 18:00:26 chronyd[3247]: Frequency -5.049 +/- 2.705 ppm read from /var/lib/chrony/chrony
Oct 02 18:00:28 chrony[3240]: chronyd is running and online.
Oct 02 18:00:28 systemd[1]: Started LSB: Controls chronyd NTP time daemon.
Oct 02 18:00:30 chronyd[3247]: Selected source 10.10.11.20
Oct 02 18:00:30 chronyd[3247]: System clock wrong by 438114471.141858 seconds, adjustment star
Aug 20 12:28:21 chronyd[3247]: System clock was stepped by 438114471.141858 seconds

Naturally, these commands can be observed by eye and the reported time can be compared against what expect expected time, however I would like for my software application to determine it the reported time is correct.

I would therefore like to know how to programmatically detect if the current time has been synchronization and ideally detect the last time sync adjustment.

Edit: I simply want to be able to detect if the current time is synchronised within my c++ application. I have an external synchronisation source and so would like to know if this is operational and providing sync so that I can report in the data that the timestamps associated with the data has been synchronised. The system that my software application runs on will be used in a boat without internet access.

1 Answer 1

3

438 million second offset is 13 years. If this system has a real time clock, check it is functional.

Decide if, on system boot, you wish to wait for NTP to be synchronized before starting your application. On RHEL, your unit can have a dependency on chrony-wait.service. Its implementation makes use of chronyc waitsync

Typical NTP metrics are the number of reachable peers, and the offset of this system. See Paul Gear's ntpmon for an implementation that parses the output of chronyc or ntpq binaries.

Kernel time discipline is a frequent and gradual thing, chrony will be calling ntp_adjtime() a lot.

Perhaps you wish to detect large steps like this one chrony logged? chrony has an email callback, the mailonchange directive. It calls sendmail, so you can do something with that mail, or put in place some other script.

And there are ways to detect these from a C program. Call ntp_adjtime() yourself and detect a return of TIME_ERROR to be a problem. Or on Linux, set a long running timer with timerfd_settime and provide the TFD_TIMER_CANCEL_ON_SET flag. This has the neat trick of being canceled if the clock is stepped or otherwise has a discontinuous change.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .