Non-monotonic clock when rebooting with no internet access

Topics about the Software of Revolution Pi
Post Reply
aharrah
Posts: 11
Joined: 23 Aug 2019, 18:40

Non-monotonic clock when rebooting with no internet access

Post by aharrah »

We are using a RevPi on a product and we do not always have access to the internet. We are finding that when the RevPi is restarted it sets the clock to the last time it had access to the internet. This results in time “going backwards”

An example will help:
2/1/2021 - RevPi is on and has internet connectivity. Time is set correctly.
2/1 -3/1: RevPi is unpowered and shipped to customer
3/1: RevPi is turned on but has no internet access. Date is set to 2/1/2021. While the clock is not correct it continues to move forward and files written on this date appear after files written prior to this date.
3/1-3/2: The RevPi is continuously powered for 48 hours. Clock advances. Time is off by a month but continues to move forward.

3/3: RevPi is rebooted.
-- Prior to rebooting the clock thinks the date is 2/3/2021.
-- After rebooting the clock is set to 2/1/2021. Files written just before rebooting will appear to be 2 days in the future.

Clearly the RevPi records the time to a file while it is synced with NTP. This allows the Pi to boot a month later and still have the date/time advance forward from the last time the system was operating.

Is there a way to tell the Pi to periodically update the time in this file, even when not synced with NTP, such that time never appears to go backwards, even when there is no internet?

More generally, is there a way to ensure that upon booting the clock never moves backwards. (To be reasonable let's say never moves backwards more than 5 minutes.)

If anyone can recommend an RTC module that plugs into the Pi we would welcome that solution too.

Thank you,
Andy
User avatar
p.rosenberger
Posts: 91
Joined: 03 Jul 2020, 11:07

Re: Non-monotonic clock when rebooting with no internet access

Post by p.rosenberger »

Hi Andy,

timekeeping is a tough one on embedded devices. We include a RTC in all our products. It is configured and is available as /dev/rtc (/dev/rtc0). But there is a drawback with our RTC it is not battary backed. We use currently a supercap to keep it powered for up to ~24h.

We have some documentation here: https://revolution.kunbus.com/tutorials ... llen-flat/

This is for the RevPi Flat but should work pretty much the same on the other devices as well. For future products we plan to include a battery to power the RTC when the RevPi is not powered.

Best Regards
Philipp
aharrah
Posts: 11
Joined: 23 Aug 2019, 18:40

Re: Non-monotonic clock when rebooting with no internet access

Post by aharrah »

The RTC on the Pi does not seem to be used in this case. Rebooting the system results in the RevPi's time being set back in time upon reboot, possibly by several weeks or months. I'm rebooting with "shutdown -r" so the RevPi is being continuously powered while it is restarted.
User avatar
lukas
Expert
Posts: 186
Joined: 13 Feb 2017, 10:29

Re: Non-monotonic clock when rebooting with no internet access

Post by lukas »

The timestamp is saved in /var/lib/systemd/timesync/clock. See here and here.
->Johannes<-

Re: Non-monotonic clock when rebooting with no internet access

Post by ->Johannes<- »

Hi Andy,

With that workaround your time/date should buffered on the RTC again for 24 h.
The RTC on the Pi does not seem to be used in this case. Rebooting the system results in the RevPi's time being set back in time upon reboot, possibly by several weeks or months. I'm rebooting with "shutdown -r" so the RevPi is being continuously powered while it is restarted.
please check with

Code: Select all

sudo timedatectl status
and post your result.

You have to disable the NTP,

Code: Select all

sudo timedatectl set-ntp 0
set RTC to UTC

Code: Select all

sudo timedatectl set-local-rtc 0
set your time/date manually

Code: Select all

sudo timedatectl set-time "YYYY-MM-DD HH:MM:SS"


(e.g. "2021-04-06 07:30:00)

check the status

Code: Select all

sudo timedatectl status
Check your time after reboot.

Code: Select all

date

Code: Select all

sudo timedatectl status


and post it.

Please share your experience with that way.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Clearly the RevPi records the time to a file while it is synced with NTP. This allows the Pi to boot a month later and still have the date/time advance forward from the last time the system was operating.

Is there a way to tell the Pi to periodically update the time in this file, even when not synced with NTP, such that time never appears to go backwards, even when there is no internet?
To keep the file
/var/lib/systemd/clock

up to date you can use the "touch" command.

Code: Select all

sudo touch /var/lib/systemd/clock
However, it is not recommended to write constantly, but perhaps once before restarting or shutting down.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
I can't personally give you a recommendation for an external RTC, but maybe someone here in the forum has already had experience with an external RTC.

Best regards
Johannes
aharrah
Posts: 11
Joined: 23 Aug 2019, 18:40

Re: Non-monotonic clock when rebooting with no internet access

Post by aharrah »

Hi Johannes,

Thank you very much for your reply. There is a lot of good information.

touching /var/lib/systemd/clock at some regular interval works well and the system does not "go back in time" more than whatever interval is chosen.

You mention that the file should not be written to constantly. What interval would you recommend? What is the reason for not writing constantly?

--

I would like to enable NTP if it is available, and disable NTP (as you describe) if it is unavailable. It is easy to envision a script that would do this, but I had two questions that will save me time and testing.

To enable ntp are there any steps other than?

Code: Select all

sudo timedatectl set-ntp 1

When NTP is disabled it is appropriate to replace

Code: Select all

sudo timedatectl set-time "YYYY-MM-DD HH:MM:SS"
with

Code: Select all

hwclock -w

I realize the RTC would not be set to anything close to the actual time, but at least the RTC would be initialized. My issue with the set-time command is our device is headless and the customer that plugs it in is not equipped to enter the date.

Thank you,
Andy
->Johannes<-

Re: Non-monotonic clock when rebooting with no internet access

Post by ->Johannes<- »

Hi Andy,
You mention that the file should not be written to constantly. What interval would you recommend? What is the reason for not writing constantly?
I can not recommend any interval, sorry.
The reason is that the read/write cyclus are limited and therefore my hint.
Maybe I am too cautious :)
I would like to enable NTP if it is available, and disable NTP (as you describe) if it is unavailable. It is easy to envision a script that would do this, but I had two questions that will save me time and testing.

To enable ntp are there any steps other than?

Code: Select all

sudo timedatectl set-ntp 1
I checked it for you. In my opinion it is enough, because you can check the NTP service with

Code: Select all

pi@RevPi47116:~ $ sudo systemctl status systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: ena
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: active (running) since Fri 2021-04-09 11:12:35 CEST; 1min 15s ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 2549 (systemd-timesyn)
   Status: "Synchronized to time server 193.30.121.7:123 (0.debian.pool.ntp.org)."
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/systemd-timesyncd.service
           └─2549 /lib/systemd/systemd-timesyncd

Apr 09 11:12:34 RevPi47116 systemd[1]: Starting Network Time Synchronization...
Apr 09 11:12:35 RevPi47116 systemd[1]: Started Network Time Synchronization.
Apr 09 11:12:35 RevPi47116 systemd-timesyncd[2549]: Synchronized to time server 193.30.121.7:

and if you set

Code: Select all

sudo timedatectl set-ntp 0
you get

Code: Select all

Active: inactive (dead)
.

When NTP is disabled it is appropriate to replace

Code: Select all

sudo timedatectl set-time "YYYY-MM-DD HH:MM:SS"
with

Code: Select all

hwclock -w

I realize the RTC would not be set to anything close to the actual time, but at least the RTC would be initialized. My issue with the set-time command is our device is headless and the customer that plugs it in is not equipped to enter the date.
It is up to you :)
We recommend timedatectl -- > Setting the Date and Time
With “timedatectl” you can query and change the system clock and its settings as well as enable or disable services for time synchronization. We recommend “timedatectl” over “hwclock” because it ties to “systemd-timesyncd”, which is enabled by default in Raspbian and thus in the KUNBUS image for the RevPi Flat.
But if it works for you, why not?

Best regards
Johannes
Post Reply