Skip to content

Commit dcd5a89

Browse files
committed
zend_hrtime: check posix clock once and prefer CLOCK_MONOTONIC_RAW
1 parent 719419a commit dcd5a89

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

Zend/zend_hrtime.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
# include <time.h>
2828
# include <string.h>
2929

30+
ZEND_API clockid_t zend_hrtime_posix_clock_id = CLOCK_MONOTONIC;
31+
3032
#elif ZEND_HRTIME_PLATFORM_WINDOWS
3133

3234
# define WIN32_LEAN_AND_MEAN
@@ -66,5 +68,22 @@ void zend_startup_hrtime(void)
6668

6769
mach_timebase_info(&zend_hrtime_timerlib_info);
6870

71+
#elif ZEND_HRTIME_PLATFORM_POSIX
72+
73+
struct timespec ts;
74+
75+
#ifdef CLOCK_MONOTONIC_RAW
76+
if (EXPECTED(0 == clock_gettime(CLOCK_MONOTONIC_RAW, &ts))) {
77+
zend_hrtime_posix_clock_id = CLOCK_MONOTONIC_RAW;
78+
return;
79+
}
80+
#endif
81+
82+
if (EXPECTED(0 == clock_gettime(zend_hrtime_posix_clock_id, &ts))) {
83+
return;
84+
}
85+
86+
ZEND_ASSERT(0 && "No working CLOCK_MONOTONIC* found, this should never happen");
87+
6988
#endif
7089
}

Zend/zend_hrtime.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ ZEND_API extern double zend_hrtime_timer_scale;
7272
# include <string.h>
7373
ZEND_API extern mach_timebase_info_data_t zend_hrtime_timerlib_info;
7474

75+
#elif ZEND_HRTIME_PLATFORM_POSIX
76+
77+
ZEND_API extern clockid_t zend_hrtime_posix_clock_id;
78+
7579
#endif
7680

7781
#define ZEND_NANO_IN_SEC UINT64_C(1000000000)
@@ -92,10 +96,8 @@ static zend_always_inline zend_hrtime_t zend_hrtime(void)
9296
return (zend_hrtime_t)mach_absolute_time() * zend_hrtime_timerlib_info.numer / zend_hrtime_timerlib_info.denom;
9397
#elif ZEND_HRTIME_PLATFORM_POSIX
9498
struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
95-
if (EXPECTED(0 == clock_gettime(CLOCK_MONOTONIC, &ts))) {
96-
return ((zend_hrtime_t) ts.tv_sec * (zend_hrtime_t)ZEND_NANO_IN_SEC) + ts.tv_nsec;
97-
}
98-
return 0;
99+
clock_gettime(zend_hrtime_posix_clock_id, &ts);
100+
return ((zend_hrtime_t) ts.tv_sec * (zend_hrtime_t)ZEND_NANO_IN_SEC) + ts.tv_nsec;
99101
#elif ZEND_HRTIME_PLATFORM_HPUX
100102
return (zend_hrtime_t) gethrtime();
101103
#elif ZEND_HRTIME_PLATFORM_AIX

0 commit comments

Comments
 (0)