Skip to content

Commit 06f4640

Browse files
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>
1 parent 783c93a commit 06f4640

3 files changed

Lines changed: 6 additions & 10 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ PHP NEWS
2626
constant, and as an alias in class_alias() and use declarations. (Girgias)
2727
. Deprecated using "_" as a constant name and as an alias for a class or
2828
constant in use declarations. (Girgias)
29+
. Switch the POSIX high resolution timer back to CLOCK_MONOTONIC from
30+
CLOCK_MONOTONIC_RAW. (nicolas-grekas)
2931

3032
- FFI:
3133
. Fixed crashes with FFI callbacks created from __call() trampolines

‎Zend/zend_hrtime.c‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,12 @@ void zend_startup_hrtime(void)
7070

7171
struct timespec ts;
7272

73-
#ifdef CLOCK_MONOTONIC_RAW
74-
if (EXPECTED(0 == clock_gettime(CLOCK_MONOTONIC_RAW, &ts))) {
75-
zend_hrtime_posix_clock_id = CLOCK_MONOTONIC_RAW;
76-
return;
77-
}
78-
#endif
79-
80-
if (EXPECTED(0 == clock_gettime(zend_hrtime_posix_clock_id, &ts))) {
73+
if (EXPECTED(0 == clock_gettime(CLOCK_MONOTONIC, &ts))) {
8174
return;
8275
}
8376

8477
// zend_error mechanism is not initialized at that point
85-
fprintf(stderr, "No working CLOCK_MONOTONIC* found, this should never happen\n");
78+
fprintf(stderr, "No working CLOCK_MONOTONIC found, this should never happen\n");
8679
abort();
8780

8881
#endif

‎Zend/zend_hrtime.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ ZEND_API extern mach_timebase_info_data_t zend_hrtime_timerlib_info;
7272

7373
#elif ZEND_HRTIME_PLATFORM_POSIX
7474

75+
/* Not read anymore, kept for ABI compatibility */
7576
ZEND_API extern clockid_t zend_hrtime_posix_clock_id;
7677

7778
#endif
@@ -94,7 +95,7 @@ static zend_always_inline zend_hrtime_t zend_hrtime(void)
9495
return (zend_hrtime_t)mach_absolute_time() * zend_hrtime_timerlib_info.numer / zend_hrtime_timerlib_info.denom;
9596
#elif ZEND_HRTIME_PLATFORM_POSIX
9697
struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
97-
clock_gettime(zend_hrtime_posix_clock_id, &ts);
98+
clock_gettime(CLOCK_MONOTONIC, &ts);
9899
return ((zend_hrtime_t) ts.tv_sec * (zend_hrtime_t)ZEND_NANO_IN_SEC) + ts.tv_nsec;
99100
#elif ZEND_HRTIME_PLATFORM_HPUX
100101
return (zend_hrtime_t) gethrtime();

0 commit comments

Comments
 (0)