Skip to content

sys/net/sntp: migrate from xtimer to ztimer#21633

Merged
maribu merged 2 commits intoRIOT-OS:masterfrom
crasbe:pr/sntp
Aug 21, 2025
Merged

sys/net/sntp: migrate from xtimer to ztimer#21633
maribu merged 2 commits intoRIOT-OS:masterfrom
crasbe:pr/sntp

Conversation

@crasbe
Copy link
Copy Markdown
Contributor

@crasbe crasbe commented Jul 30, 2025

Contribution description

The sntp module still uses the deprecated xtimer. This PR migrates it to ztimer64.

Testing procedure

Testing is a bit non-trivial because RIOT can't access internet-based NTP servers (I think?) and systemd has an integrated NTP client, which does not act as an NTP server itself.

Therefore you have to install the good old ntpd and run it as sudo. Also a tap-network has to be setup of course.

The output should look something like this. In the build log, you should see ztimer64 instead of xtimer and the output of ntpdate should be pretty much the same.

cbuec@W11nMate:~/RIOTstuff/riot-crc8/RIOT$ PORT=tap0 make -C tests/net/sntp all term -j
make: Entering directory '/home/cbuec/RIOTstuff/riot-crc8/RIOT/tests/net/sntp'
using BOARD="native64" as "native" on a 64-bit system
Building application "tests_sntp" for "native64" with CPU "native".

"make" -C /home/cbuec/RIOTstuff/riot-crc8/RIOT/boards
...
"make" -C /home/cbuec/RIOTstuff/riot-crc8/RIOT/sys/net/gnrc/transport_layer/udp
"make" -C /home/cbuec/RIOTstuff/riot-crc8/RIOT/sys/ztimer
"make" -C /home/cbuec/RIOTstuff/riot-crc8/RIOT/sys/ztimer64
"make" -C /home/cbuec/RIOTstuff/riot-crc8/RIOT/sys/net/gnrc/netif/ethernet
"make" -C /home/cbuec/RIOTstuff/riot-crc8/RIOT/sys/net/gnrc/netif/hdr
"make" -C /home/cbuec/RIOTstuff/riot-crc8/RIOT/sys/net/gnrc/netif/init_devs
   text    data     bss     dec     hex filename
 145294    2224  110768  258286   3f0ee /home/cbuec/RIOTstuff/riot-crc8/RIOT/tests/net/sntp/bin/native64/tests_sntp.elf
/home/cbuec/RIOTstuff/riot-crc8/RIOT/dist/tools/pyterm/pyterm -ps /home/cbuec/RIOTstuff/riot-crc8/RIOT/tests/net/sntp/bin/native64/tests_sntp.elf --process-args tap0
2025-07-30 23:54:40,148 # RIOT native interrupts/signals initialized.
Welcome to pyterm!
Type '/exit' to exit.
2025-07-30 23:54:40,149 # TZ not set, setting UTC
2025-07-30 23:54:40,151 # RIOT native64 board initialized.
2025-07-30 23:54:40,151 # RIOT native hardware initialization complete.
2025-07-30 23:54:40,151 #
2025-07-30 23:54:40,152 # main(): This is RIOT! (Version: 2025.07-devel-437-gded43-pr/sntp)
> ntpdate fe80::6cb9:43ff:fe48:a430
2025-07-30 23:54:42,616 # ntpdate fe80::6cb9:43ff:fe48:a430
2025-07-30 23:54:42,617 # 1753912482.1753912482 (-620366685 us)

Issues/PRs references

Progress for tracking issue #13667.

@crasbe crasbe requested a review from miri64 July 30, 2025 21:58
@crasbe crasbe requested a review from PeterKietzmann as a code owner July 30, 2025 21:58
@crasbe crasbe added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Impact: minor The PR is small in size and might only require a quick look of a knowledgeable reviewer CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jul 30, 2025
@github-actions github-actions bot added Area: network Area: Networking Area: sys Area: System labels Jul 30, 2025
@riot-ci
Copy link
Copy Markdown

riot-ci commented Jul 30, 2025

Murdock results

✔️ PASSED

acf10d9 sys/net/ntp_packet: add ref to SNTP, fix time

Success Failures Total Runtime
10538 0 10538 23m:40s

Artifacts

@miri64
Copy link
Copy Markdown
Member

miri64 commented Jul 31, 2025

Do we really need microsecond precision for that? Wouldn't ztimer_msec (and 32-bit accuracy) be enough for the type of time-synchronization that SNTP offers?

@crasbe
Copy link
Copy Markdown
Contributor Author

crasbe commented Jul 31, 2025

To be honest: I don't know 🤷

@mguetschow
Copy link
Copy Markdown
Contributor

Indeed RFC 5905 only mentions millisecond accuracy, so it indeed seems overkill to allow for microsecond precision here. However, the current implementation indeed gives microsecond accuracy so it is out of scope for this PR to change it IMO.

A follow-up could introduce a millisecond precision (pseudo)module and deprecate the old API to be removed following the usual deprecation process.

@miri64
Copy link
Copy Markdown
Member

miri64 commented Aug 1, 2025

Indeed RFC 5905 only mentions millisecond accuracy, so it indeed seems overkill to allow for microsecond precision here. However, the current implementation indeed gives microsecond accuracy so it is out of scope for this PR to change it IMO.

Well this PR changes from xtimer to ztimer. While with xtimer the timing-precision does not have any influence on power consumption, with ztimer it does. The millisecond timer is much more power-saving, while the 64-bit accuracy microsecond timer is very power-consuming. If one wants to migrate, why not make it properly. So yes, I think this is in scope of this PR.

@kfessel
Copy link
Copy Markdown
Contributor

kfessel commented Aug 5, 2025

Indeed RFC 5905 only mentions millisecond accuracy, so it indeed seems overkill to allow for microsecond precision here. However, the current implementation indeed gives microsecond accuracy so it is out of scope for this PR to change it IMO.

A follow-up could introduce a millisecond precision (pseudo)module and deprecate the old API to be removed following the usual deprecation process.

The abstract

NTPv4 includes fundamental improvements in the mitigation and discipline algorithms that extend
the potential accuracy to the tens of microseconds with modern
workstations and fast LANs.

> grep -ni millisec rfc5905.txt
211:   servers and clients are precise within a few tens of milliseconds
> grep -ni microsec rfc5905.txt
28:   the potential accuracy to the tens of microseconds with modern
208:   few tens of microseconds.  Typical secondary servers and clients on
209:   fast LANs are within a few hundred microseconds with poll intervals
1170:   corresponds to a precision of about one microsecond.  The precision
4103: * 32-bit words in seconds and microseconds (timeval) or nanoseconds

@kfessel
Copy link
Copy Markdown
Contributor

kfessel commented Aug 5, 2025

* @brief Offset in seconds of NTP timestamp (seconds from 1990-01-01 00:00:00 UTC)

should be

... 1900-01-01 00:00:00 UTC 

maybe you can fix that

@mguetschow
Copy link
Copy Markdown
Contributor

The millisecond timer is much more power-saving, while the 64-bit accuracy microsecond timer is very power-consuming. If one wants to migrate, why not make it properly. So yes, I think this is in scope of this PR.

Sure, but the xtimer was also power-consuming wasn't it? I would only say this is strictly in scope of this PR if it would introduce a (power consumption) regression. That said, I wouldn't mind having the change to millisecond precision in here already :)

@crasbe
Copy link
Copy Markdown
Contributor Author

crasbe commented Aug 21, 2025

I had to put this on the backburner for some time.
As a compromise I can add a note to the documentation that sntp currently is not power efficient. We want to use it in future projects, so additional improvements are planned and one of them could be to improve the power efficiency.

@crasbe
Copy link
Copy Markdown
Contributor Author

crasbe commented Aug 21, 2025

Changing from Microsecond to Millisecond precision would also require an API extension (because get microseconds would be pointless if we don't actually get microseconds) and a deprecation of the other function etc.

@maribu
Copy link
Copy Markdown
Member

maribu commented Aug 21, 2025

I agree that moving to milliseonds is out of scope.

There actually is another benefit in moving to milliseonds: Better frequency stability of RTT compared to periph_timer in many cases, as RTT is often backed by a 32.678 kHz watch crystal, while the high frequency clock domain is typically generated from some DCO / PLL that has a lot of drift.

If we were to e.g. sync only a few times a day, using ZTIMER_MSEC will provide a major improvement in accuracy.

@maribu
Copy link
Copy Markdown
Member

maribu commented Aug 21, 2025

I'm personally fine with either adding a note that SNTP is not power efficient / accurate, or merging as is. Hopefully someone can find time soonish for the us --> ms shift.

Copy link
Copy Markdown
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please squash

@maribu maribu enabled auto-merge August 21, 2025 12:29
@maribu maribu added this pull request to the merge queue Aug 21, 2025
Merged via the queue into RIOT-OS:master with commit c0cc617 Aug 21, 2025
25 checks passed
@crasbe crasbe deleted the pr/sntp branch August 22, 2025 11:05
@crasbe crasbe mentioned this pull request Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: network Area: Networking Area: sys Area: System CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Impact: minor The PR is small in size and might only require a quick look of a knowledgeable reviewer Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants