Skip to content

gh-115714: Don't use CLOCK_PROCESS_CPUTIME_ID and times() on WASI #115757

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

Merged
merged 2 commits into from
Feb 22, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
On WASI, the :mod:`time` module no longer get process time using ``times()``
or ``CLOCK_PROCESS_CPUTIME_ID``, system API is that is unreliable and is
likely to be removed from WASI. The affected clock functions fall back to
calling ``clock()``.
16 changes: 11 additions & 5 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ static int pysleep(PyTime_t timeout);

typedef struct {
PyTypeObject *struct_time_type;
#ifdef HAVE_TIMES
// gh-115714: Don't use times() on WASI.
#if defined(HAVE_TIMES) && !defined(__wasi__)
// times() clock frequency in hertz
_PyTimeFraction times_base;
#endif
Expand Down Expand Up @@ -1211,7 +1212,8 @@ PyDoc_STRVAR(perf_counter_ns_doc,
Performance counter for benchmarking as nanoseconds.");


#ifdef HAVE_TIMES
// gh-115714: Don't use times() on WASI.
#if defined(HAVE_TIMES) && !defined(__wasi__)
static int
process_time_times(time_module_state *state, PyTime_t *tp,
_Py_clock_info_t *info)
Expand Down Expand Up @@ -1280,8 +1282,10 @@ py_process_time(time_module_state *state, PyTime_t *tp,
#else

/* clock_gettime */
// gh-115714: Don't use CLOCK_PROCESS_CPUTIME_ID on WASI.
#if defined(HAVE_CLOCK_GETTIME) \
&& (defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_PROF))
&& (defined(CLOCK_PROCESS_CPUTIME_ID) || defined(CLOCK_PROF)) \
&& !defined(__wasi__)
struct timespec ts;

if (HAVE_CLOCK_GETTIME_RUNTIME) {
Expand Down Expand Up @@ -1343,7 +1347,8 @@ py_process_time(time_module_state *state, PyTime_t *tp,
#endif

/* times() */
#ifdef HAVE_TIMES
// gh-115714: Don't use times() on WASI.
#if defined(HAVE_TIMES) && !defined(__wasi__)
int res = process_time_times(state, tp, info);
if (res < 0) {
return -1;
Expand Down Expand Up @@ -2071,7 +2076,8 @@ time_exec(PyObject *module)
}
#endif

#ifdef HAVE_TIMES
// gh-115714: Don't use times() on WASI.
#if defined(HAVE_TIMES) && !defined(__wasi__)
long ticks_per_second;
if (_Py_GetTicksPerSecond(&ticks_per_second) < 0) {
PyErr_SetString(PyExc_RuntimeError,
Expand Down