Commit 06f4640b602 for php
commit 06f4640b602e959faefcc85a0d11181193d57225
Author: Nicolas Grekas <nicolas.grekas@gmail.com>
Date: Wed Sep 30 13:42:03 2026 +0200
zend_hrtime: use CLOCK_MONOTONIC instead of CLOCK_MONOTONIC_RAW (#23790)
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 GH-19221 wanted to avoid is bounded to 500ppm by the kernel
and is what makes CLOCK_MONOTONIC track elapsed real time.
zend_hrtime_posix_clock_id stays for ABI compatibility, but nothing reads
it anymore.
Co-authored-by: Tim Düsterhus <tim@tideways-gmbh.com>
diff --git a/NEWS b/NEWS
index cd01751fc78..6e325f3df81 100644
--- a/NEWS
+++ b/NEWS
@@ -26,6 +26,8 @@ PHP NEWS
constant, and as an alias in class_alias() and use declarations. (Girgias)
. Deprecated using "_" as a constant name and as an alias for a class or
constant in use declarations. (Girgias)
+ . Switch the POSIX high resolution timer back to CLOCK_MONOTONIC from
+ CLOCK_MONOTONIC_RAW. (nicolas-grekas)
- FFI:
. Fixed crashes with FFI callbacks created from __call() trampolines
diff --git a/Zend/zend_hrtime.c b/Zend/zend_hrtime.c
index 819f95c4766..b471ee335e1 100644
--- a/Zend/zend_hrtime.c
+++ b/Zend/zend_hrtime.c
@@ -70,19 +70,12 @@ void zend_startup_hrtime(void)
struct timespec ts;
-#ifdef CLOCK_MONOTONIC_RAW
- if (EXPECTED(0 == clock_gettime(CLOCK_MONOTONIC_RAW, &ts))) {
- zend_hrtime_posix_clock_id = CLOCK_MONOTONIC_RAW;
- return;
- }
-#endif
-
- if (EXPECTED(0 == clock_gettime(zend_hrtime_posix_clock_id, &ts))) {
+ if (EXPECTED(0 == clock_gettime(CLOCK_MONOTONIC, &ts))) {
return;
}
// zend_error mechanism is not initialized at that point
- fprintf(stderr, "No working CLOCK_MONOTONIC* found, this should never happen\n");
+ fprintf(stderr, "No working CLOCK_MONOTONIC found, this should never happen\n");
abort();
#endif
diff --git a/Zend/zend_hrtime.h b/Zend/zend_hrtime.h
index 6d802caacfe..f7d69d19f80 100644
--- a/Zend/zend_hrtime.h
+++ b/Zend/zend_hrtime.h
@@ -72,6 +72,7 @@ ZEND_API extern mach_timebase_info_data_t zend_hrtime_timerlib_info;
#elif ZEND_HRTIME_PLATFORM_POSIX
+/* Not read anymore, kept for ABI compatibility */
ZEND_API extern clockid_t zend_hrtime_posix_clock_id;
#endif
@@ -94,7 +95,7 @@ static zend_always_inline zend_hrtime_t zend_hrtime(void)
return (zend_hrtime_t)mach_absolute_time() * zend_hrtime_timerlib_info.numer / zend_hrtime_timerlib_info.denom;
#elif ZEND_HRTIME_PLATFORM_POSIX
struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
- clock_gettime(zend_hrtime_posix_clock_id, &ts);
+ clock_gettime(CLOCK_MONOTONIC, &ts);
return ((zend_hrtime_t) ts.tv_sec * (zend_hrtime_t)ZEND_NANO_IN_SEC) + ts.tv_nsec;
#elif ZEND_HRTIME_PLATFORM_HPUX
return (zend_hrtime_t) gethrtime();