[core] Fix FormatTimeSys rendering the same timestamp inconsistently (#3225)#3342
Open
steps-re wants to merge 1 commit into
Open
[core] Fix FormatTimeSys rendering the same timestamp inconsistently (#3225)#3342steps-re wants to merge 1 commit into
steps-re wants to merge 1 commit into
Conversation
FormatTimeSys mapped a steady-clock timestamp onto wall-clock time by combining whole-second wall time from ::time() with the sub-second part of steady_clock::now(). The two clocks have unrelated sub-second phases (steady counts from an arbitrary epoch such as boot), so near a second boundary the very same timestamp could render one second apart between calls (issue Haivision#3225). Map the timestamp using a single steady<->wall reference pair captured once at load time, in a consistent microsecond domain. This removes the +/-1 s flicker and also the ~1 us jitter that per-call re-sampling would cause, so a given timestamp always formats to exactly the same string. The mapping arithmetic is factored into a pure overload FormatTimeSys(target_us, steady_now_us, wall_now_us) so it can be unit tested deterministically. Adds a regression test (Sync.FormatTimeSysStable) that sweeps the "now" reference across steady/wall second boundaries with misaligned phases and asserts the output is stable. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: Mike German <mike@stepsventures.com>
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.
Fixes #3225.
Bug
FormatTimeSys()renders asteady_clocktimestamp as wall-clock time, but it combined whole-second wall time from::time()with the sub-second part ofsteady_clock::now(). Those clocks have unrelated sub-second phases (steady counts from an arbitrary epoch), so near a second boundary the same timestamp rendered one second apart between calls — the ±1s discrepancy in the issue. Per-call re-sampling of two clocks non-atomically also adds ~1µs jitter.Fix
Map the timestamp through a single steady↔wall reference pair, captured once at load time, in one consistent microsecond domain. A given timestamp now always formats to exactly the same string. The arithmetic is factored into a pure, testable overload
FormatTimeSys(target_us, steady_now_us, wall_now_us). This touches only the[SYST]logging/trace formatter — not the transport path.Tradeoff (for maintainer review)
This caches the steady↔wall offset for stability. If the wall clock is NTP-stepped mid-run,
[SYST]times drift by the step amount but stay internally consistent. The alternative — live re-tracking with only the flicker removed — is also reasonable; happy to switch to that if you prefer. Flagging since it's your issue and touches a utility.Verification
Sync.FormatTimeSysStablesweeps the reference across misaligned steady/wall second boundaries and asserts a fixed timestamp renders identically — fails on the old arithmetic, passes with the fix.--gtest_repeat=200onSync.FormatTime*→ 200/200 (also removes a latent flake in the existing back-to-back-equality test).-DENABLE_UNITTESTS=ON, no new warnings. DCO sign-off included.