Skip to content

Use clock_gettime on all MacOS after 10.11 - #2341

Open
nicholasjng wants to merge 3 commits into
google:mainfrom
nicholasjng:push-pzxxlxstmtmy
Open

nicholasjng wants to merge 3 commits into
google:mainfrom
nicholasjng:push-pzxxlxstmtmy

Conversation

@nicholasjng

@nicholasjng nicholasjng commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

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


From experimentation with scoped pauses in mew. This cuts around 500ns off the total cost of a pause in Python code.

EDIT: clock_gettime is available here because of <ctime>, which includes <time.h> on Apple's libc++.

Comment thread src/timers.cc Outdated
(static_cast<double>(ts.tv_nsec) * 1e-9);
}
DiagnoseAndExit("clock_gettime(CLOCK_MONOTONIC, ...) failed");
#elif defined(CLOCK_PROCESS_CPUTIME_ID) && !defined(BENCHMARK_OS_MACOSX)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should just remove this section then i think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if C++17 is the floor now, which requires macOS 10.14+, it's reasonable to assume clock_gettime to always be available, but I didn't want to break backwards compatibility without confirmation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, but your new check below will use thread_id if the version is older, and if not then it'll fall through to the bottom CLOCK_PROCESS_CPUTIME_ID check (line 216) and use clock_gettime. so i think this section is redundant?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this block is for ProcessCPUUsage(), line 216 is for ThreadCPUUsage(). could you recheck?

I also think this could be simplified, but we need to untangle the two functions, and I'd like your opinion on whether to drop support for macOS without clock_gettime.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 years is plenty of time. i think we can drop support for the older versions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, i pushed two more commits with the necessary cleanups.

@nicholasjng

Copy link
Copy Markdown
Contributor Author

re: the clang-format failure: the struct declarations were autofixed on clang-format==23.1.3 , which I have installed locally, I think CI has an older version (18?). Your call.

Comment thread src/timers.cc Outdated
`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
Since we assume that we can access `clock_gettime()` on all supported macOS
versions, we can remove the mach thread-info route entirely.

@LebedevRI LebedevRI left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine, i guess?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants