Skip to content

zend_hrtime: use CLOCK_MONOTONIC instead of CLOCK_MONOTONIC_RAW - #23790

Open
nicolas-grekas wants to merge 1 commit into
php:masterfrom
nicolas-grekas:hrtime-clock-monotonic
Open

nicolas-grekas wants to merge 1 commit into
php:masterfrom
nicolas-grekas:hrtime-clock-monotonic

Conversation

@nicolas-grekas

Copy link
Copy Markdown
Contributor

This should fix the hrtime.phpt failure reported in GH-22508.

CLOCK_MONOTONIC_RAW is not disciplined by NTP, so it ticks with the raw frequency error of the underlying oscillator. Under WSL2 here, that is 4% fast, measured over a 1s sleep:

             microtime delta   hrtime delta
8.5          1.000083 s        1.000086 s
8.6          1.000072 s        1.038898 s
8.6 + this   1.000158 s        1.000163 s

The relative uncertainty that ext/standard/tests/hrtime/hrtime.phpt computes goes from 0.038 back to 0.0002 with this, against the 0.05 the test allows. @mbeccati measured 0.0501 on Ubuntu 26.04 while packaging 8.6.0alpha1 and suggested raising that limit - I think the limit is fine and it is the clock that moved, but a confirmation on that box would be welcome.

The adjtime/NTP slew that GH-19221 wanted to avoid is bounded to 500ppm by the kernel, and it is precisely what makes CLOCK_MONOTONIC track elapsed real time, which is the contract that test asserts. CLOCK_MONOTONIC_RAW is also served by the vDSO only since Linux 5.3, so on eg. RHEL 8 it costs a syscall per call.

I kept the check-the-clock-once part of GH-19221, only the _RAW preference goes.

CLOCK_MONOTONIC_RAW is not disciplined by NTP, so it ticks with the raw
frequency error of the underlying oscillator, which is 4% under WSL2 and
makes hrtime() disagree with microtime() by that much.

The slew that phpGH-19221 wanted to avoid is bounded to 500ppm by the kernel
and is what makes CLOCK_MONOTONIC track elapsed real time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant