Skip to content

gh-86273: On macOS >= 10.12, use clock_gettime(CLOCK_MONOTONIC_RAW) for time.monotonic #134521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/library/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ Functions

* On Windows, call ``QueryPerformanceCounter()`` and
``QueryPerformanceFrequency()``.
* On macOS, call ``mach_absolute_time()`` and ``mach_timebase_info()``.
* On macOS, call ``clock_gettime(CLOCK_MONOTONIC_RAW)`` if available, otherwise call ``mach_absolute_time()`` and ``mach_timebase_info()``.
* On HP-UX, call ``gethrtime()``.
* Call ``clock_gettime(CLOCK_HIGHRES)`` if available.
* Otherwise, call ``clock_gettime(CLOCK_MONOTONIC)``.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
On macOS >= 10.12, use ``clock_gettime(CLOCK_MONOTONIC_RAW)`` for
:func:`time.monotonic`. Patch by John Keith Hohm.
13 changes: 8 additions & 5 deletions Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# include <winsock2.h> // struct timeval
#endif

#if defined(__APPLE__)
#if defined(__APPLE__) && !defined(CLOCK_MONOTONIC_RAW)
# include <mach/mach_time.h> // mach_absolute_time(), mach_timebase_info()

#if defined(__APPLE__) && defined(__has_builtin)
Expand Down Expand Up @@ -933,7 +933,7 @@ py_get_system_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
struct timespec ts;
#endif

#if !defined(HAVE_CLOCK_GETTIME) || defined(__APPLE__)
#if !defined(HAVE_CLOCK_GETTIME) || (defined(__APPLE__) && !defined(CLOCK_MONOTONIC_RAW))
struct timeval tv;
#endif

Expand Down Expand Up @@ -1090,7 +1090,7 @@ py_get_win_perf_counter(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
#endif // MS_WINDOWS


#ifdef __APPLE__
#if defined(__APPLE__) && !defined(CLOCK_MONOTONIC_RAW)
static PyStatus
py_mach_timebase_info(_PyTimeFraction *base)
{
Expand Down Expand Up @@ -1125,7 +1125,7 @@ _PyTime_Init(struct _Py_time_runtime_state *state)
{
#ifdef MS_WINDOWS
return py_win_perf_counter_frequency(&state->base);
#elif defined(__APPLE__)
#elif defined(__APPLE__) && !defined(CLOCK_MONOTONIC_RAW)
return py_mach_timebase_info(&state->base);
#else
return PyStatus_Ok();
Expand All @@ -1146,7 +1146,7 @@ py_get_monotonic_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
if (py_get_win_perf_counter(tp, info, raise_exc) < 0) {
return -1;
}
#elif defined(__APPLE__)
#elif defined(__APPLE__) && !defined(CLOCK_MONOTONIC_RAW)
if (info) {
info->implementation = "mach_absolute_time()";
info->resolution = _PyTimeFraction_Resolution(&_PyRuntime.time.base);
Expand Down Expand Up @@ -1185,6 +1185,9 @@ py_get_monotonic_clock(PyTime_t *tp, _Py_clock_info_t *info, int raise_exc)
#ifdef CLOCK_HIGHRES
const clockid_t clk_id = CLOCK_HIGHRES;
const char *implementation = "clock_gettime(CLOCK_HIGHRES)";
#elif defined(__APPLE__) && defined(CLOCK_MONOTONIC_RAW)
const clockid_t clk_id = CLOCK_MONOTONIC_RAW;
const char *implementation = "clock_gettime(CLOCK_MONOTONIC_RAW)";
#else
const clockid_t clk_id = CLOCK_MONOTONIC;
const char *implementation = "clock_gettime(CLOCK_MONOTONIC)";
Expand Down
Loading