Fix inconsistent process start time#121935
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process |
There was a problem hiding this comment.
Pull request overview
This PR stabilizes Linux/Android process start time calculations by changing SystemNative_GetBootTimeTicks to use CLOCK_REALTIME (higher resolution) instead of CLOCK_REALTIME_COARSE when computing the boot-time-to-Unix-epoch offset.
Changes:
- Switch boot time offset calculation from
CLOCK_REALTIME_COARSEtoCLOCK_REALTIMEinSystemNative_GetBootTimeTicks.
| int64_t sinceBootTicks = ((int64_t)ts.tv_sec * SecondsToTicks) + (ts.tv_nsec / TicksToNanoSeconds); | ||
|
|
||
| result = clock_gettime(CLOCK_REALTIME_COARSE, &ts); | ||
| result = clock_gettime(CLOCK_REALTIME, &ts); |
There was a problem hiding this comment.
This behavior change affects cross-process determinism of Process.StartTime on Linux/Android, but there’s no regression coverage guarding it. Consider adding a managed test (e.g., using RemoteExecutor to run multiple separate processes that query the same target process’ StartTime) to ensure the computed boot-time offset is stable across process boundaries and doesn’t regress back to jittery values.
There was a problem hiding this comment.
@copilot apply changes based on this feedback
|
@copilot address feedback push to this PR |
|
I believe Copilot can't modify this PR as it's on a fork. @Neo-vortex do you want to give it a shot? |
|
@danmoseley |
Fix: Improve Clock Offset Calculation Stability by Using CLOCK_REALTIME
fixes :#108959
Summary
This pull request updates the
SystemNative_GetBootTimeTicksfunction to useCLOCK_REALTIMEinstead ofCLOCK_REALTIME_COARSEfor calculating the time elapsed since the Unix Epoch.This change is critical because the instability of the coarse clock was causing unstable process start time reads. Switching to
CLOCK_REALTIMEsignificantly reduces the observed clock synchronization jitter, leading to a much more stable and accurate boot-time offset value.Detailed Rationale
The function calculates the boot time offset using the formula:
This calculated offset is used to determine key system timestamps, including the time when a process started.
Issue with CLOCK_REALTIME_COARSE
The coarse clock, designed for speed over accuracy, introduced substantial jitter into the offset calculation. Benchmark results showed the
CLOCK_REALTIME_COARSEpath exhibited a peak-to-peak jitter of up to approximately 1 ms (978 μs).This high variability directly results in unstable process start time reads, making the resulting boot time offset unreliable for high-precision scenarios.
Stability of CLOCK_REALTIME
The corresponding high-resolution clock path, using
CLOCK_REALTIMEinstead of COARSE, showed zero jitter, providing a stable and consistent offset value.Performance Justification
Although
CLOCK_REALTIME_COARSEis intended to be faster, performance profiling confirms that the difference in call overhead between the two clock functions is negligible in our target execution environment.Proposed Change
The function is updated to use
CLOCK_REALTIMEto ensure the final calculation relies on stable, high-precision time sources, thereby eliminating the approximately 1 ms jitter currently observed.This guarantees that
SystemNative_GetBootTimeTicksreturns a highly consistent value across all calls.