From 6ed8b4e290489b154a660f1ab0a890da10c67ee3 Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Mon, 19 Oct 2026 13:16:19 +0200 Subject: [PATCH 1/3] Use `clock_gettime` on all MacOS after 10.11 `clock_gettime` is available on macOS since 10.12 (September 2016), and noticeably faster than mach's `thread_info`, so use it whenever available. Assisted-By: Codex --- src/timers.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/timers.cc b/src/timers.cc index 53cf4875e1..01d1d421f4 100644 --- a/src/timers.cc +++ b/src/timers.cc @@ -35,6 +35,7 @@ #include #endif #if defined(BENCHMARK_OS_MACOSX) +#include #include #include #include @@ -150,9 +151,7 @@ double ProcessCPUUsage() { (static_cast(ts.tv_nsec) * 1e-9); } DiagnoseAndExit("clock_gettime(CLOCK_MONOTONIC, ...) failed"); -#elif defined(CLOCK_PROCESS_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX) - // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. - // See https://github.com/google/benchmark/pull/292 +#elif defined(CLOCK_PROCESS_CPUTIME_ID) struct timespec spec {}; if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &spec) == 0) { return MakeTime(spec); @@ -183,9 +182,11 @@ double ThreadCPUUsage() { return static_cast( qurt_timer_timetick_to_us(qurt_sysclock_get_hw_ticks())) * 1.0e-6; -#elif defined(BENCHMARK_OS_MACOSX) - // FIXME We want to use clock_gettime, but its not available in MacOS 10.11. - // See https://github.com/google/benchmark/pull/292 +#elif defined(BENCHMARK_OS_MACOSX) && \ + (!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || \ + __MAC_OS_X_VERSION_MIN_REQUIRED < 101200) + // clock_gettime is available since macOS 10.12, older deployment targets + // and other Apple platforms read mach's thread_info, which is slower. mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; thread_basic_info_data_t info; mach_port_t thread = pthread_mach_thread_np(pthread_self()); From e7332a3acd6b976d317e32a010544fdc10bf1693 Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Mon, 19 Oct 2026 15:02:30 +0200 Subject: [PATCH 2/3] Drop ThreadCPUUsage() branch for macOS<10.12 --- src/timers.cc | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/timers.cc b/src/timers.cc index 01d1d421f4..13ea270783 100644 --- a/src/timers.cc +++ b/src/timers.cc @@ -182,20 +182,6 @@ double ThreadCPUUsage() { return static_cast( qurt_timer_timetick_to_us(qurt_sysclock_get_hw_ticks())) * 1.0e-6; -#elif defined(BENCHMARK_OS_MACOSX) && \ - (!defined(__MAC_OS_X_VERSION_MIN_REQUIRED) || \ - __MAC_OS_X_VERSION_MIN_REQUIRED < 101200) - // clock_gettime is available since macOS 10.12, older deployment targets - // and other Apple platforms read mach's thread_info, which is slower. - mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; - thread_basic_info_data_t info; - mach_port_t thread = pthread_mach_thread_np(pthread_self()); - if (thread_info(thread, THREAD_BASIC_INFO, - reinterpret_cast(&info), - &count) == KERN_SUCCESS) { - return MakeTime(info); - } - DiagnoseAndExit("ThreadCPUUsage() failed when evaluating thread_info"); #elif defined(BENCHMARK_OS_EMSCRIPTEN) // Emscripten doesn't support traditional threads return ProcessCPUUsage(); From e566a104008942e94f7793d28989739daa019cfd Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Mon, 19 Oct 2026 19:52:07 +0200 Subject: [PATCH 3/3] Remove macOS-specific includes, mach-specific MakeTime() overload Since we assume that we can access `clock_gettime()` on all supported macOS versions, we can remove the mach thread-info route entirely. --- src/timers.cc | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/timers.cc b/src/timers.cc index 13ea270783..d029dfbb25 100644 --- a/src/timers.cc +++ b/src/timers.cc @@ -34,12 +34,6 @@ defined BENCHMARK_OS_MACOSX #include #endif -#if defined(BENCHMARK_OS_MACOSX) -#include -#include -#include -#include -#endif #if defined(BENCHMARK_OS_QURT) #include #endif @@ -95,14 +89,6 @@ double MakeTime(struct rusage const& ru) { static_cast(ru.ru_stime.tv_usec) * 1e-6); } #endif -#if defined(BENCHMARK_OS_MACOSX) -double MakeTime(thread_basic_info_data_t const& info) { - return (static_cast(info.user_time.seconds) + - static_cast(info.user_time.microseconds) * 1e-6 + - static_cast(info.system_time.seconds) + - static_cast(info.system_time.microseconds) * 1e-6); -} -#endif #if defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_THREAD_CPUTIME_ID) double MakeTime(struct timespec const& ts) { return static_cast(ts.tv_sec) +