PyObject *struct_rusage;
#endif
PyObject *st_mode;
+#ifndef MS_WINDOWS
+ // times() clock frequency in hertz; used by os.times()
+ long ticks_per_second;
+#endif
} _posixstate;
#endif /* HAVE_SYMLINK */
-
-
static PyStructSequence_Field times_result_fields[] = {
{"user", "user time"},
{"system", "system time"},
5
};
-#ifdef MS_WINDOWS
-#define HAVE_TIMES /* mandatory, for the method table */
-#endif
-
-#ifdef HAVE_TIMES
-
static PyObject *
build_times_result(PyObject *module, double user, double system,
double children_user, double children_system,
static PyObject *
os_times_impl(PyObject *module)
/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
-#ifdef MS_WINDOWS
{
+#ifdef MS_WINDOWS
FILETIME create, exit, kernel, user;
HANDLE hProc;
hProc = GetCurrentProcess();
(double)0,
(double)0,
(double)0);
-}
#else /* MS_WINDOWS */
-{
- struct tms t;
- clock_t c;
+ _posixstate *state = get_posix_state(module);
+ long ticks_per_second = state->ticks_per_second;
+
+ struct tms process;
+ clock_t elapsed;
errno = 0;
- c = times(&t);
- if (c == (clock_t) -1) {
+ elapsed = times(&process);
+ if (elapsed == (clock_t) -1) {
return posix_error();
}
- assert(_PyRuntime.time.ticks_per_second_initialized);
-#define ticks_per_second _PyRuntime.time.ticks_per_second
+
return build_times_result(module,
- (double)t.tms_utime / ticks_per_second,
- (double)t.tms_stime / ticks_per_second,
- (double)t.tms_cutime / ticks_per_second,
- (double)t.tms_cstime / ticks_per_second,
- (double)c / ticks_per_second);
-#undef ticks_per_second
-}
+ (double)process.tms_utime / ticks_per_second,
+ (double)process.tms_stime / ticks_per_second,
+ (double)process.tms_cutime / ticks_per_second,
+ (double)process.tms_cstime / ticks_per_second,
+ (double)elapsed / ticks_per_second);
#endif /* MS_WINDOWS */
-#endif /* HAVE_TIMES */
+}
#if defined(HAVE_TIMERFD_CREATE)
Py_DECREF(unicode);
}
+#ifndef MS_WINDOWS
+ if (_Py_GetTicksPerSecond(&state->ticks_per_second) < 0) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "cannot read ticks_per_second");
+ return -1;
+ }
+ assert(state->ticks_per_second >= 1);
+#endif
+
return PyModule_Add(m, "_have_functions", list);
}
static int
check_ticks_per_second(long tps, const char *context)
{
- /* Effectively, check that _PyTime_MulDiv(t, SEC_TO_NS, ticks_per_second)
+ /* Effectively, check that _PyTime_MulDiv(t, SEC_TO_NS, tps)
cannot overflow. */
if (tps >= 0 && (_PyTime_t)tps > _PyTime_MAX / SEC_TO_NS) {
PyErr_Format(PyExc_OverflowError, "%s is too large", context);
return -1;
}
+ if (tps < 1) {
+ PyErr_Format(PyExc_RuntimeError, "invalid %s", context);
+ return -1;
+ }
return 0;
}
#endif /* HAVE_TIMES || HAVE_CLOCK */
-#ifdef HAVE_TIMES
-
-# define ticks_per_second _PyRuntime.time.ticks_per_second
-
-static void
-ensure_ticks_per_second(void)
-{
- if (_PyRuntime.time.ticks_per_second_initialized) {
- return;
- }
- _PyRuntime.time.ticks_per_second_initialized = 1;
-# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
- ticks_per_second = sysconf(_SC_CLK_TCK);
- if (ticks_per_second < 1) {
- ticks_per_second = -1;
- }
-# elif defined(HZ)
- ticks_per_second = HZ;
-# else
- ticks_per_second = 60; /* magic fallback value; may be bogus */
-# endif
-}
-
-#endif /* HAVE_TIMES */
-
-
-PyStatus
-_PyTime_Init(void)
-{
-#ifdef HAVE_TIMES
- ensure_ticks_per_second();
-#endif
- return PyStatus_Ok();
-}
-
/* Forward declarations */
static int pysleep(_PyTime_t timeout);
typedef struct {
PyTypeObject *struct_time_type;
+#ifdef HAVE_TIMES
+ // times() clock frequency in hertz
+ long ticks_per_second;
+#endif
+#ifdef HAVE_CLOCK
+ // clock() frequency in hertz
+ long clocks_per_second;
+#endif
} time_module_state;
static inline time_module_state*
\n\
Return the current time in nanoseconds since the Epoch.");
-#if defined(HAVE_CLOCK)
+#ifdef HAVE_CLOCK
#ifndef CLOCKS_PER_SEC
# ifdef CLK_TCK
#endif
static int
-_PyTime_GetClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
+py_clock(time_module_state *state, _PyTime_t *tp, _Py_clock_info_t *info)
{
- if (check_ticks_per_second(CLOCKS_PER_SEC, "CLOCKS_PER_SEC") < 0) {
- return -1;
- }
-
+ long clocks_per_second = state->clocks_per_second;
if (info) {
info->implementation = "clock()";
- info->resolution = 1.0 / (double)CLOCKS_PER_SEC;
+ info->resolution = 1.0 / (double)clocks_per_second;
info->monotonic = 1;
info->adjustable = 0;
}
"or its value cannot be represented");
return -1;
}
- _PyTime_t ns = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)CLOCKS_PER_SEC);
+ _PyTime_t ns = _PyTime_MulDiv(ticks, SEC_TO_NS, clocks_per_second);
*tp = _PyTime_FromNanoseconds(ns);
return 0;
}
\n\
Performance counter for benchmarking as nanoseconds.");
+
+#ifdef HAVE_TIMES
static int
-_PyTime_GetProcessTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
+process_time_times(time_module_state *state, _PyTime_t *tp,
+ _Py_clock_info_t *info)
+{
+ long ticks_per_second = state->ticks_per_second;
+
+ struct tms process;
+ if (times(&process) == (clock_t)-1) {
+ return 0;
+ }
+
+ if (info) {
+ info->implementation = "times()";
+ info->monotonic = 1;
+ info->adjustable = 0;
+ info->resolution = 1.0 / (double)ticks_per_second;
+ }
+
+ _PyTime_t ns;
+ ns = _PyTime_MulDiv(process.tms_utime, SEC_TO_NS, ticks_per_second);
+ ns += _PyTime_MulDiv(process.tms_stime, SEC_TO_NS, ticks_per_second);
+ *tp = _PyTime_FromNanoseconds(ns);
+ return 1;
+}
+#endif
+
+
+static int
+py_process_time(time_module_state *state, _PyTime_t *tp,
+ _Py_clock_info_t *info)
{
#if defined(MS_WINDOWS)
HANDLE process;
/* times() */
#ifdef HAVE_TIMES
- struct tms t;
-
- if (times(&t) != (clock_t)-1) {
- assert(_PyRuntime.time.ticks_per_second_initialized);
- if (check_ticks_per_second(ticks_per_second, "_SC_CLK_TCK") < 0) {
- return -1;
- }
- if (ticks_per_second != -1) {
- if (info) {
- info->implementation = "times()";
- info->monotonic = 1;
- info->adjustable = 0;
- info->resolution = 1.0 / (double)ticks_per_second;
- }
-
- _PyTime_t ns;
- ns = _PyTime_MulDiv(t.tms_utime, SEC_TO_NS, ticks_per_second);
- ns += _PyTime_MulDiv(t.tms_stime, SEC_TO_NS, ticks_per_second);
- *tp = _PyTime_FromNanoseconds(ns);
- return 0;
- }
+ int res = process_time_times(state, tp, info);
+ if (res < 0) {
+ return -1;
}
+ if (res == 1) {
+ return 0;
+ }
+ // times() failed, ignore failure
#endif
/* clock */
/* Currently, Python 3 requires clock() to build: see issue #22624 */
- return _PyTime_GetClockWithInfo(tp, info);
+ return py_clock(state, tp, info);
#endif
}
static PyObject *
-time_process_time(PyObject *self, PyObject *unused)
+time_process_time(PyObject *module, PyObject *unused)
{
+ time_module_state *state = get_time_state(module);
_PyTime_t t;
- if (_PyTime_GetProcessTimeWithInfo(&t, NULL) < 0) {
+ if (py_process_time(state, &t, NULL) < 0) {
return NULL;
}
return _PyFloat_FromPyTime(t);
Process time for profiling: sum of the kernel and user-space CPU time.");
static PyObject *
-time_process_time_ns(PyObject *self, PyObject *unused)
+time_process_time_ns(PyObject *module, PyObject *unused)
{
+ time_module_state *state = get_time_state(module);
_PyTime_t t;
- if (_PyTime_GetProcessTimeWithInfo(&t, NULL) < 0) {
+ if (py_process_time(state, &t, NULL) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
static PyObject *
-time_get_clock_info(PyObject *self, PyObject *args)
+time_get_clock_info(PyObject *module, PyObject *args)
{
char *name;
_Py_clock_info_t info;
}
}
else if (strcmp(name, "process_time") == 0) {
- if (_PyTime_GetProcessTimeWithInfo(&t, &info) < 0) {
+ time_module_state *state = get_time_state(module);
+ if (py_process_time(state, &t, &info) < 0) {
return NULL;
}
}
}
#endif
+#ifdef HAVE_TIMES
+ if (_Py_GetTicksPerSecond(&state->ticks_per_second) < 0) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "cannot read ticks_per_second");
+ return -1;
+ }
+
+ if (check_ticks_per_second(state->ticks_per_second, "_SC_CLK_TCK") < 0) {
+ return -1;
+ }
+#endif
+
+#ifdef HAVE_CLOCK
+ state->clocks_per_second = CLOCKS_PER_SEC;
+ if (check_ticks_per_second(state->clocks_per_second, "CLOCKS_PER_SEC") < 0) {
+ return -1;
+ }
+#endif
+
return 0;
}