[php-src] PHP-8.6: zend_hrtime: use CLOCK_MONOTONIC instead of CLOCK_MONOTONIC_RAW (#23790)
| From: | Nicolas Grekas via GitHub | Date: | Wed, 30 Sep 2026 11:42:06 +0000 |
| Subject: | [php-src] PHP-8.6: zend_hrtime: use CLOCK_MONOTONIC instead of CLOCK_MONOTONIC_RAW (#23790) | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141765@lists.php.net to get a copy of this message | ||
Author: Nicolas Grekas (nicolas-grekas)
Committer: GitHub (web-flow)
Pusher: TimWolla
Date: 2026-09-30T13:42:03+02:00
Commit: https://github.com/php/php-src/commit/06f4640b602e959faefcc85a0d11181193d57225
Raw diff: https://github.com/php/php-src/commit/06f4640b602e959faefcc85a0d11181193d57225.diff
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>
Changed paths:
M NEWS
M Zend/zend_hrtime.c
M Zend/zend_hrtime.h
Diff:
diff --git a/NEWS b/NEWS
index cd01751fc78b..6e325f3df810 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 819f95c47662..b471ee335e1d 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 6d802caacfe0..f7d69d19f80b 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();