Skip to content

Commit 85f69a7

Browse files
authored
zend_hrtime: Use clock_gettime_nsec_np() for macOS if available (#17089)
As per the Apple developer documentation: > Prefer to use the equivalent clock_gettime_nsec_np(CLOCK_UPTIME_RAW) in > nanoseconds. and also > This API has the potential of being misused to access device signals to try > to identify the device or user, also known as fingerprinting. Regardless of > whether a user gives your app permission to track, fingerprinting is not > allowed. When you use this API in your app or third-party SDK (an SDK not > provided by Apple), declare your usage and the reason for using the API in > your app or third-party SDK’s PrivacyInfo.xcprivacy file. see https://developer.apple.com/documentation/kernel/1462446-mach_absolute_time
1 parent 2541b9f commit 85f69a7

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ PHP NEWS
1212
. Added PHP_BUILD_DATE constant. (cmb)
1313
. Added support for Closures in constant expressions. (timwolla,
1414
Volker Dusch)
15+
. Use `clock_gettime_nsec_np()` for high resolution timer on macOS
16+
if available. (timwolla)
1517

1618
- Curl:
1719
. Added curl_multi_get_handles(). (timwolla)

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ PHP 8.5 UPGRADE NOTES
193193
13. Other Changes
194194
========================================
195195

196+
- Core:
197+
The high resolution timer (`hrtime()`) on macOS now uses the recommended
198+
`clock_gettime_nsec_np(CLOCK_UPTIME_RAW)` API instead of
199+
`mach_absolute_time()`.
200+
196201
========================================
197202
14. Performance Improvements
198203
========================================

Zend/Zend.m4

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ AC_CHECK_FUNCS(m4_normalize([
151151
pthread_stackseg_np
152152
]))
153153
154+
AC_CHECK_DECL([clock_gettime_nsec_np],
155+
[AC_DEFINE([HAVE_CLOCK_GETTIME_NSEC_NP], [1],
156+
[Define to 1 if you have the declaration of 'clock_gettime_nsec_np'.])],,
157+
[#include <time.h>])
158+
154159
dnl
155160
dnl Check for sigsetjmp. If sigsetjmp is defined as a macro, use AC_CHECK_DECL
156161
dnl as a fallback since AC_CHECK_FUNC cannot detect macros.

Zend/zend_hrtime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
ZEND_API double zend_hrtime_timer_scale = .0;
3535

36-
#elif ZEND_HRTIME_PLATFORM_APPLE
36+
#elif ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE
3737

3838
# include <mach/mach_time.h>
3939
# include <string.h>
@@ -62,7 +62,7 @@ void zend_startup_hrtime(void)
6262
zend_hrtime_timer_scale = (double)ZEND_NANO_IN_SEC / (zend_hrtime_t)tf.QuadPart;
6363
}
6464

65-
#elif ZEND_HRTIME_PLATFORM_APPLE
65+
#elif ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE
6666

6767
mach_timebase_info(&zend_hrtime_timerlib_info);
6868

Zend/zend_hrtime.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333

3434
#define ZEND_HRTIME_PLATFORM_POSIX 0
3535
#define ZEND_HRTIME_PLATFORM_WINDOWS 0
36-
#define ZEND_HRTIME_PLATFORM_APPLE 0
36+
#define ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE 0
37+
#define ZEND_HRTIME_PLATFORM_APPLE_GETTIME_NSEC 0
3738
#define ZEND_HRTIME_PLATFORM_HPUX 0
3839
#define ZEND_HRTIME_PLATFORM_AIX 0
3940

@@ -43,9 +44,12 @@
4344
#elif defined(_WIN32) || defined(_WIN64)
4445
# undef ZEND_HRTIME_PLATFORM_WINDOWS
4546
# define ZEND_HRTIME_PLATFORM_WINDOWS 1
47+
#elif HAVE_CLOCK_GETTIME_NSEC_NP
48+
# undef ZEND_HRTIME_PLATFORM_APPLE_GETTIME_NSEC
49+
# define ZEND_HRTIME_PLATFORM_APPLE_GETTIME_NSEC 1
4650
#elif defined(__APPLE__)
47-
# undef ZEND_HRTIME_PLATFORM_APPLE
48-
# define ZEND_HRTIME_PLATFORM_APPLE 1
51+
# undef ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE
52+
# define ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE 1
4953
#elif (defined(__hpux) || defined(hpux)) || ((defined(__sun__) || defined(__sun) || defined(sun)) && (defined(__SVR4) || defined(__svr4__)))
5054
# undef ZEND_HRTIME_PLATFORM_HPUX
5155
# define ZEND_HRTIME_PLATFORM_HPUX 1
@@ -54,7 +58,7 @@
5458
# define ZEND_HRTIME_PLATFORM_AIX 1
5559
#endif
5660

57-
#define ZEND_HRTIME_AVAILABLE (ZEND_HRTIME_PLATFORM_POSIX || ZEND_HRTIME_PLATFORM_WINDOWS || ZEND_HRTIME_PLATFORM_APPLE || ZEND_HRTIME_PLATFORM_HPUX || ZEND_HRTIME_PLATFORM_AIX)
61+
#define ZEND_HRTIME_AVAILABLE (ZEND_HRTIME_PLATFORM_POSIX || ZEND_HRTIME_PLATFORM_WINDOWS || ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE || ZEND_HRTIME_PLATFORM_APPLE_GETTIME_NSEC || ZEND_HRTIME_PLATFORM_HPUX || ZEND_HRTIME_PLATFORM_AIX)
5862

5963
BEGIN_EXTERN_C()
6064

@@ -82,7 +86,9 @@ static zend_always_inline zend_hrtime_t zend_hrtime(void)
8286
LARGE_INTEGER lt = {0};
8387
QueryPerformanceCounter(&lt);
8488
return (zend_hrtime_t)((zend_hrtime_t)lt.QuadPart * zend_hrtime_timer_scale);
85-
#elif ZEND_HRTIME_PLATFORM_APPLE
89+
#elif ZEND_HRTIME_PLATFORM_APPLE_GETTIME_NSEC
90+
return clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
91+
#elif ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE
8692
return (zend_hrtime_t)mach_absolute_time() * zend_hrtime_timerlib_info.numer / zend_hrtime_timerlib_info.denom;
8793
#elif ZEND_HRTIME_PLATFORM_POSIX
8894
struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };

0 commit comments

Comments
 (0)