Repository navigation
Take the server clock from CLOCK_MONOTONIC_RAW on Linux - #99
Merged
Merged
Conversation
NTP slews CLOCK_MONOTONIC's rate, which the clients' time filters then have to follow. The raw clock runs at the hardware's own rate. The docs of now_us() no longer claim it counts through sleep on Linux, which neither clock does.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The Linux clock test needs an updated reference, and early timer expiry can leave drain completion pending indefinitely.
2 open issues
What changed in this PR
This PR switches the server’s Linux timestamp clock to CLOCK_MONOTONIC_RAW so NTP adjustments no longer change its rate.
Changes:
- Switch Linux clock reads while preserving macOS and ESP-IDF behavior.
- Correct clock documentation and clarify steady-clock timer drift.
| File | Description |
|---|---|
| src/transport.h | Clarifies timer clock drift. |
| src/clock.h | Documents platform clocks and NTP independence. |
| src/clock.cpp | Selects CLOCK_MONOTONIC_RAW on Linux. |
| include/sendspin/server/server.h | Updates the public clock description. |
Review effort: Balanced
On Linux the steady clock Asio waits on is NTP-slewed CLOCK_MONOTONIC, which can run ahead of now_us()'s raw clock, so a wait could end short of at_us. A caller that rechecks and returns, as the drain check does, then never ran again. The clock test reads CLOCK_MONOTONIC_RAW, as now_us() does on both hosts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
On Linux,
now_us()readCLOCK_MONOTONIC, whose rate NTP slews (byup to 500 ppm). Every timestamp the server sends follows that rate, so
the clients' time filters had to track each change the NTP daemon made.
The server clock now runs at the hardware's own rate.
Change
now_us()readsCLOCK_MONOTONIC_RAWon Linux. ESP-IDF keeps itsesp_timer
CLOCK_MONOTONIC, and macOS keepsmach_continuous_time,neither of which NTP slews.
clock.handSendspinServer::now_us()drop the claim thatthe clock counts through system sleep on Linux, which neither clock
did.
Timerre-arms a wait that ends beforenow_us()reachesits deadline. Asio waits on the slewed
CLOCK_MONOTONIC, which can runahead of the raw clock, and a caller that rechecks and returns would
never run again: the drain check would leave
on_drainedpending, andSessionDriverskips re-arming an unchanged deadline. ESP-IDF's loopalready fires only once
now_us()reaches the deadline.Tests
Clock.ReadsTheMonotonicClockInMicrosecondsreadsCLOCK_MONOTONIC_RAWas its reference on both hosts.ahead of the raw one. The Linux path was compiled with
-Werrorand run in acontainer, where
now_us()matchedCLOCK_MONOTONIC_RAW.CLOCK_MONOTONIC_RAWis asyscall rather than a vDSO read.