ntpd setup

Having issues with your DietPi installation, or, found a bug? Post it here.
Gord_W
Posts: 134
Joined: Sat Oct 24, 2015 6:30 pm

Re: ntpd setup

Post by Gord_W »

─────────────────────────────────────────────────────
DietPi CPU Info
Use dietpi-config to change CPU / performance options
─────────────────────────────────────────────────────
Architecture | armv6l
Temp | 4'c | Who put me in the freezer! :lol:
Governor | ondemand
Throttle up | 60% CPU usage

Very funny!

Gordon Williams
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: ntpd setup

Post by Fourdee »

Gord_W wrote:─────────────────────────────────────────────────────
DietPi CPU Info
Use dietpi-config to change CPU / performance options
─────────────────────────────────────────────────────
Architecture | armv6l
Temp | 4'c | Who put me in the freezer! :lol:
Governor | ondemand
Throttle up | 60% CPU usage

Very funny!

Gordon Williams
hehe, got to have a little fun now and then :D

Ok to get NTPD running from boot and constantly on your current system:
use ctrl+w to find..

Code: Select all

nano /etc/cron.daily/dietpi
#disable (comment) the following lines:

#killall ntpd &> /dev/null
#ntpd -gq &> /dev/null

Code: Select all

nano /DietPi/dietpi/boot
#Change the following line

#Update NTP (+thread)
ntpd

For v102:
I will also add an option to dietpi-config that will run NTPD from boot and keep the daemon running.
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
Gord_W
Posts: 134
Joined: Sat Oct 24, 2015 6:30 pm

Re: ntpd setup

Post by Gord_W »

Hi,

I'm not sure your boot code is going to do it.

I think there is a time stored in a file somewhere that the system time get written to occasionally. I think that is used to help set the system clock on boot. If that is far off true time or non-existent then just a straight ntpd command will fail based on my reading http://doc.ntp.org/4.1.0/ntpd.htm. It looks like ntpd will exit if sys time error exceeds 1000s from real time.
-g
Normally, ntpd exits if the offset exceeds the sanity limit, which is 1000 s by default. If the sanity limit is set to zero, no sanity checking is performed and any offset is acceptable. This option overrides the limit and allows the time to be set to any value without restriction; however, this can happen only once. After that, ntpd will exit if the limit is exceeded. This option can be used with the -q option.
I therefore think that we should have
ntpd -g
as the command to start ntp in the boot file.

Do you come to the same conclusion? I haven't tried stopping the rpi for a while and then restarting it to confirm that ntp will fail and exit due to the time discrepancy > 1000s


Gordon Williams
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: ntpd setup

Post by Fourdee »

Gord_W wrote:I therefore think that we should have
ntpd -g
as the command to start ntp in the boot file.

Do you come to the same conclusion? I haven't tried stopping the rpi for a while and then restarting it to confirm that ntp will fail and exit due to the time discrepancy > 1000s


Gordon Williams
Yep, the -g flag will do it, allows for excessive time differences. We use that at the moment with -q which quits when done.
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
Gord_W
Posts: 134
Joined: Sat Oct 24, 2015 6:30 pm

Re: ntpd setup

Post by Gord_W »

I'm not sure what the problem is with
ntpd -g
but ntp gets running but the PLL doesn't get synced and the drift frequency doesn't get corrected. It just uses the drift value in the drift file.

After about 15 minutes I was still getting this (peers were good with reach=377 (all data received) long ago):

root@DietPi:~# ntptime
ntp_gettime() returns code 5 (ERROR)
time da073f69.7c60e000 Mon, Nov 30 2015 16:34:33.485, (.485853),
maximum error 16000000 us, estimated error 16000000 us, TAI offset 0
ntp_adjtime() returns code 5 (ERROR)
modes 0x0 (),
offset 0.000 us, frequency -47.137 ppm, interval 1 s,
maximum error 16000000 us, estimated error 16000000 us,
status 0x41 (PLL,UNSYNC),
time constant 7, precision 1.000 us, tolerance 500 ppm,

If I run:
ntpd -gq
ntpd

then it magically jumps to this
root@DietPi:~# ntptime
ntp_gettime() returns code 0 (OK)
time da073fbd.1686726c Mon, Nov 30 2015 16:35:57.087, (.087989690),
maximum error 2016 us, estimated error 16 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
modes 0x0 (),
offset 0.000 us, frequency -43.938 ppm, interval 1 s,
maximum error 2016 us, estimated error 16 us,
status 0x2001 (PLL,NANO),
time constant 3, precision 0.001 us, tolerance 500 ppm,

and a few minutes later everything seems to be working OK with reasonable errors identified, pll offset is now non-zero too:

root@DietPi:~# ntptime
ntp_gettime() returns code 0 (OK)
time da07433f.914e1a08 Mon, Nov 30 2015 16:50:55.567, (.567598512),
maximum error 328335 us, estimated error 2759 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
modes 0x0 (),
offset 487.965 us, frequency -43.321 ppm, interval 1 s,
maximum error 328335 us, estimated error 2759 us,
status 0x2001 (PLL,NANO),
time constant 6, precision 0.001 us, tolerance 500 ppm,

Will change the boot file and see the effect
Gordon Williams
Gord_W
Posts: 134
Joined: Sat Oct 24, 2015 6:30 pm

Re: ntpd setup

Post by Gord_W »

This is what works and syncs up fast!

If I modify the boot file as shown:
#----------------------------------------------------------------
#Update NTP (+thread) gw modified
ntpd -gq
ntpd
#----------------------------------------------------------------

Then as soon as the wifi comes up and I ssh in (about 50 sec after boot):

First I get:

root@DietPi:~# ntptime
ntp_gettime() returns code 0 (OK)
time da07695b.9090c000 Mon, Nov 30 2015 19:33:31.564, (.564709),
maximum error 1016 us, estimated error 16 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
modes 0x0 (),
offset 0.000 us, frequency -41.414 ppm, interval 1 s,
maximum error 1016 us, estimated error 16 us,
status 0x1 (PLL),
time constant 7, precision 1.000 us, tolerance 500 ppm,

The a couple of seconds later:

root@DietPi:~# ntptime
ntp_gettime() returns code 0 (OK)
time da076961.b7ee8038 Mon, Nov 30 2015 19:33:37.718, (.718483614),
maximum error 1042781 us, estimated error 738 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
modes 0x0 (),
offset 2081.837 us, frequency -41.400 ppm, interval 1 s,
maximum error 1042781 us, estimated error 738 us,
status 0x2001 (PLL,NANO),
time constant 6, precision 0.001 us, tolerance 500 ppm,

So no error messages! Both show returns code 0 (OK) rather than error 5.

Regards,
Gordon Williams
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: ntpd setup

Post by Fourdee »

Hi Gordon,

Thanks for the update and info. I've updated the v102 code.

When DietPi v102 is released, you can do the following:
dietpi-config > advanced options > Time Sync (NTPD)
Then select option 4 and reboot

ps. If you run the following, it will execute ntpd -gq then the daemon in that order, whilst creating a separate thread for it. This should prevent any delay during the boot script execution as the system won't wait for it to complete, before it carries on with other things.

Code: Select all

ntpd -gq && ntpd -g -l /var/log/ntpd.log &
pps. although, if you want the time to be correct before other software runs, the above isn't recommended.
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
Gord_W
Posts: 134
Joined: Sat Oct 24, 2015 6:30 pm

Re: ntpd setup

Post by Gord_W »

Hi,

You have -l /var/log/ntpd.log

The log file dir is already identified in the ntp.config file so you may not want to override that here. As well, the stats are commented out in the ntp.conf file by default so normally you wouldn't get any stats anyway.

The hourly ram log copies the ntp log to my root home directory, but it leaves a lot of empty log files behind in the /var/log/ntpstats/ dir. An example

root@DietPi:/var/log/ntpstats# ls -l
total 24
-rwxrwxr-x 2 root root 119 Dec 2 12:29 loopstats
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1893C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1907C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1912C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1918C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1919C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1920C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1923C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1924C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1925C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1928C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1932C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.20151128
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.20151129
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.20151130
-rwxrwxr-x 1 root root 1 Dec 1 17:17 loopstats.20151201
-rwxrwxr-x 2 root root 119 Dec 2 12:29 loopstats.20151202
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.2270C0
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.2704C1
-rwxrwxr-x 2 root root 418 Dec 2 12:29 peerstats
-rwxrwxr-x 1 root root 0 Dec 1 14:17 peerstats.1893C0
-rwxrwxr-x 1 root root 0 Dec 1 14:17 peerstats.1907C0
-rwxrwxr-x 1 root root 0 Dec 1 14:17 peerstats.1912C0
-rwxrwxr-x 1 root root 0 Dec 1 14:17 peerstats.1918C0
... and so on ...

Gordon Williams
User avatar
Fourdee
Site Admin
Posts: 2788
Joined: Tue Feb 06, 2007 1:36 pm

Re: ntpd setup

Post by Fourdee »

Gord_W wrote:You have -l /var/log/ntpd.log
The log file dir is already identified in the ntp.config file so you may not want to override that here.
This is intentional.
I wanted to override the filepath. This will ensure the file gets included (/var/log/*) when dietpi-bugreport is sent.
Gord_W wrote:The hourly ram log copies the ntp log to my root home directory, but it leaves a lot of empty log files behind in the /var/log/ntpstats/ dir. An example

root@DietPi:/var/log/ntpstats# ls -l
total 24
-rwxrwxr-x 2 root root 119 Dec 2 12:29 loopstats
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1893C1
-rwxrwxr-x 1 root root 0 Dec 1 14:17 loopstats.1907C1
Thanks for the report.

DietPi-Ramlog clears the contents of the logfile but keeps the original file in place. I originally designed the program to delete the file itself, however, this caused certain programs (eg: dnsmasq) to stop writing to the log files after that point.
I also tested restarting the services after deleting the logfile, although it fixed the issue above, it wasn't practical to create a downtime.

Although the size is reported at 0, each of those empty files will use 4096bytes of RAM (tmpfs allocation size).
You can run the following to delete all log files:

Code: Select all

dietpi-logclear 2
Then i'd suggest you reboot.
If you find our project or support useful, then we’d really appreciate it if you’d consider contributing to the project however you can.
Donating is the easiest – you can use PayPal or become a DietPi patron.
Gord_W
Posts: 134
Joined: Sat Oct 24, 2015 6:30 pm

Re: ntpd setup

Post by Gord_W »

Hi,

As I've now had a chance to play with ntp a bit and monitor it's clock performance, I'm quite impressed.

A suggestion that might be useful to people who want to use their rpi not connected to the internet:
If their rpi during setting up is connected to the internet, they can let ntp run for a few hours to calibrate the drift in their system clock and that drift will be written to the drift file. When ntpd is started again it will apply that drift to the oscillator and the correction will be continued to be applied even if ntpd is stopped again.

Therefore on reboot when the rpi is not connected to the internet and ntpd -gq is run, the time will have to be set manually, but with the drift offset the rpi will keep time pretty well.

My rpi has a drift of about 45 ppm at room temp and using the calibration it brings it down to 2 ppm or so.

To my surprise, putting the rpi in the deep freeze didn't cause it to drift very much. From my initial test it seems less than 5ppm, which is amazing as I was expecting 100 ppm. I need to confirm that is true, but it looks that way.

Gordon Williams
Post Reply