Skip to content

Conversation

@otherscase
Copy link

Currently, monotonic clock initialization relies on the model name field from /proc/cpuinfo to retrieve the clock speed. However, this is not always present. In case it is not present, measure the clock tick and use it instead.

Before fix:

monotonic: x86 linux, unable to determine clock rate

After fix:

21695:M 25 Oct 2025 20:16:23.168 * monotonic clock: X86 TSC @ 2649 ticks/us

Fixes #2774

Signed-off-by: Ken Nam <otherscase@gmail.com>
}
/* Some CPUs may not contain clock speed in the model name */
if (mono_ticksPerMicrosecond == 0) {
/* Calibrate TSC against CLOCK_MONOTONIC */
Copy link
Member

Choose a reason for hiding this comment

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

LGTM. I wonder if we should run multiple loops and take the minimum to reduce the chance of introducing impact caused by jitter? (eg https://elixir.bootlin.com/linux/v6.17.5/source/arch/x86/kernel/tsc.c#L758)

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for taking a look. Based on my quick test, running 1000+ loops seem to see 0.04% deviation at max (1 tick deviation). We could add a loop with possibly smaller measurement window and take a max out of it but not sure if that yields to practical difference. Let me know what you think.

Copy link
Member

Choose a reason for hiding this comment

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

I have less knowledge of how important that is in different cloud platforms. maybe @JimB123 has some inputs based on his prior investigations.
The change LGTM

@codecov
Copy link

codecov bot commented Oct 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.50%. Comparing base (861d079) to head (f5be583).

Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #2776      +/-   ##
============================================
+ Coverage     72.43%   72.50%   +0.06%     
============================================
  Files           128      128              
  Lines         70145    70145              
============================================
+ Hits          50813    50860      +47     
+ Misses        19332    19285      -47     
Files with missing lines Coverage Δ
src/monotonic.c 100.00% <ø> (ø)

... and 12 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ranshid ranshid requested a review from JimB123 October 27, 2025 10:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Monotonic clock does not work on AMD CPUs

2 participants