Repository navigation
fix: count the SDK clock on CLOCK_MONOTONIC_RAW on Linux - #415
Merged
Merged
Conversation
Stopwatch reads CLOCK_MONOTONIC on Linux, which NTP slews. aiosendspin timestamps on CLOCK_MONOTONIC_RAW (aiosendspin#211), so while the host's time daemon is correcting, the offset the clock synchroniser measures moves at the slew rate: tens of ppm that keep changing, and several milliseconds within seconds when the daemon does an initial sync. The Kalman filter models drift as near-constant, so it lags that, the error grows past the sync threshold, and playback hard-corrects audibly. On one Home Assistant OS host, systemd-timesyncd's initial sync moved the two clocks 11.5 ms apart in a single 10 s time-sync burst. HighPrecisionTimer now counts elapsed time on clock_gettime( CLOCK_MONOTONIC_RAW) on Linux, through a blittable libc P/Invoke that the runtime resolves on glibc and musl under JIT and NativeAOT. It falls back to Stopwatch if the raw clock cannot be read. Windows (QPC) and macOS (CLOCK_UPTIME_RAW) already count on an unslewed clock and are unchanged, as are the UTC anchor and every consumer of the timer. The AOT smoke test now reads the shared clock, so a published binary binds the P/Invoke the way a consumer's does.
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.
What
HighPrecisionTimernow counts elapsed time onclock_gettime(CLOCK_MONOTONIC_RAW)on Linux. Windows and macOS keepStopwatch, which already reads an unslewed clock there (QueryPerformanceCounter andCLOCK_UPTIME_RAW).Why
On Linux,
StopwatchreadsCLOCK_MONOTONIC, which NTP slews. aiosendspin timestamps onCLOCK_MONOTONIC_RAW(aiosendspin#211, "to avoid NTP slewing"), and so do sendspin-rs and SendspinKit. So whenever the player host's time daemon is correcting, the offset the clock synchroniser measures moves at the slew rate. That rate is tens of ppm, it keeps changing, and an initial sync can move the clock several milliseconds within seconds. The Kalman filter models drift as near-constant, so it lags behind. The mapping error grows past the sync threshold, and playback hard-corrects audibly.Measured on a Home Assistant OS box, with the server and a player on the same host (sendspin-cpp, which reads the same slewed clock):
CLOCK_MONOTONICran +24.5 → +11.3 ppm againstCLOCK_MONOTONIC_RAWwhile systemd-timesyncd was correcting. The player's measured server offset tracked that exactly, burst by burst.The .NET runtime reads
CLOCK_MONOTONICforStopwatchon Linux (minipal_hires_ticks), so the SDK is exposed in the same way.How
HighPrecisionTimerpicks its counter once per process: on Linux, the raw clock if it can be read, otherwiseStopwatch. The UTC anchor,MonotonicTimerand every consumer are unchanged. Only the elapsed part changes clock.[DllImport("libc", EntryPoint = "clock_gettime")]. Both runtime loaders map"libc"to the platform C library (CoreCLR's PALFixLibCName, and System.Native'spal_dynamicload.cfor NativeAOT), so it resolves on glibc and musl.IAudioPlayer.GetAudioClockMicroseconds()are unaffected: that value replaces the timer for local time.Testing
HighPrecisionTimerTests: the raw clock is used on Linux and only there, elapsed time tracks real time (pins the tick-to-µs conversion), and the timer reports Unix-epoch microseconds and never decreases.dotnet/sdk:10.0: the full net10.0 suite matchesmain. Both have the same 4 failures, which are file-permission tests that cannot fail as root in a container and are unrelated to this change.dotnet/sdk:10.0-alpine: the synchronization tests pass (39/39), and the raw clock is in use.linux-x64publish: no IL or AOT warnings. The smoke test passes, including a new check that reads the shared clock.