Skip to content

Commit f0b23b0

Browse files
vstinnerGlyphack
authored andcommitted
pythongh-112567: Add _Py_GetTicksPerSecond() function (python#112587)
* Move _PyRuntimeState.time to _posixstate.ticks_per_second and time_module_state.ticks_per_second. * Add time_module_state.clocks_per_second. * Rename _PyTime_GetClockWithInfo() to py_clock(). * Rename _PyTime_GetProcessTimeWithInfo() to py_process_time(). * Add process_time_times() helper function, called by py_process_time(). * os.times() is now always built: no longer rely on HAVE_TIMES.
1 parent 33fba34 commit f0b23b0

File tree

9 files changed

+142
-125
lines changed

9 files changed

+142
-125
lines changed

Include/internal/pycore_fileutils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ PyAPI_FUNC(char*) _Py_UniversalNewlineFgetsWithSize(char *, int, FILE*, PyObject
320320

321321
extern int _PyFile_Flush(PyObject *);
322322

323+
#ifndef MS_WINDOWS
324+
extern int _Py_GetTicksPerSecond(long *ticks_per_second);
325+
#endif
326+
323327
#ifdef __cplusplus
324328
}
325329
#endif

Include/internal/pycore_pylifecycle.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ extern void _PySys_FiniTypes(PyInterpreterState *interp);
4040
extern int _PyBuiltins_AddExceptions(PyObject * bltinmod);
4141
extern PyStatus _Py_HashRandomization_Init(const PyConfig *);
4242

43-
extern PyStatus _PyTime_Init(void);
4443
extern PyStatus _PyGC_Init(PyInterpreterState *interp);
4544
extern PyStatus _PyAtExit_Init(PyInterpreterState *interp);
4645
extern int _Py_Deepfreeze_Init(void);

Include/internal/pycore_runtime.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ extern "C" {
2121
#include "pycore_pymem.h" // struct _pymem_allocators
2222
#include "pycore_pythread.h" // struct _pythread_runtime_state
2323
#include "pycore_signal.h" // struct _signals_runtime_state
24-
#include "pycore_time.h" // struct _time_runtime_state
2524
#include "pycore_tracemalloc.h" // struct _tracemalloc_runtime_state
2625
#include "pycore_typeobject.h" // struct _types_runtime_state
2726
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_state
@@ -205,7 +204,6 @@ typedef struct pyruntimestate {
205204
struct _pymem_allocators allocators;
206205
struct _obmalloc_global_state obmalloc;
207206
struct pyhash_runtime_state pyhash_state;
208-
struct _time_runtime_state time;
209207
struct _pythread_runtime_state threads;
210208
struct _signals_runtime_state signals;
211209

Include/internal/pycore_time.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@ extern "C" {
5252
#endif
5353

5454

55-
struct _time_runtime_state {
56-
#ifdef HAVE_TIMES
57-
int ticks_per_second_initialized;
58-
long ticks_per_second;
59-
#else
60-
int _not_used;
61-
#endif
62-
};
63-
64-
6555
#ifdef __clang__
6656
struct timeval;
6757
#endif

Modules/clinic/posixmodule.c.h

Lines changed: 1 addition & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/posixmodule.c

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,10 @@ typedef struct {
10301030
PyObject *struct_rusage;
10311031
#endif
10321032
PyObject *st_mode;
1033+
#ifndef MS_WINDOWS
1034+
// times() clock frequency in hertz; used by os.times()
1035+
long ticks_per_second;
1036+
#endif
10331037
} _posixstate;
10341038

10351039

@@ -9986,8 +9990,6 @@ os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
99869990
#endif /* HAVE_SYMLINK */
99879991

99889992

9989-
9990-
99919993
static PyStructSequence_Field times_result_fields[] = {
99929994
{"user", "user time"},
99939995
{"system", "system time"},
@@ -10013,12 +10015,6 @@ static PyStructSequence_Desc times_result_desc = {
1001310015
5
1001410016
};
1001510017

10016-
#ifdef MS_WINDOWS
10017-
#define HAVE_TIMES /* mandatory, for the method table */
10018-
#endif
10019-
10020-
#ifdef HAVE_TIMES
10021-
1002210018
static PyObject *
1002310019
build_times_result(PyObject *module, double user, double system,
1002410020
double children_user, double children_system,
@@ -10064,8 +10060,8 @@ All fields are floating point numbers.
1006410060
static PyObject *
1006510061
os_times_impl(PyObject *module)
1006610062
/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
10067-
#ifdef MS_WINDOWS
1006810063
{
10064+
#ifdef MS_WINDOWS
1006910065
FILETIME create, exit, kernel, user;
1007010066
HANDLE hProc;
1007110067
hProc = GetCurrentProcess();
@@ -10083,28 +10079,26 @@ os_times_impl(PyObject *module)
1008310079
(double)0,
1008410080
(double)0,
1008510081
(double)0);
10086-
}
1008710082
#else /* MS_WINDOWS */
10088-
{
10089-
struct tms t;
10090-
clock_t c;
10083+
_posixstate *state = get_posix_state(module);
10084+
long ticks_per_second = state->ticks_per_second;
10085+
10086+
struct tms process;
10087+
clock_t elapsed;
1009110088
errno = 0;
10092-
c = times(&t);
10093-
if (c == (clock_t) -1) {
10089+
elapsed = times(&process);
10090+
if (elapsed == (clock_t) -1) {
1009410091
return posix_error();
1009510092
}
10096-
assert(_PyRuntime.time.ticks_per_second_initialized);
10097-
#define ticks_per_second _PyRuntime.time.ticks_per_second
10093+
1009810094
return build_times_result(module,
10099-
(double)t.tms_utime / ticks_per_second,
10100-
(double)t.tms_stime / ticks_per_second,
10101-
(double)t.tms_cutime / ticks_per_second,
10102-
(double)t.tms_cstime / ticks_per_second,
10103-
(double)c / ticks_per_second);
10104-
#undef ticks_per_second
10105-
}
10095+
(double)process.tms_utime / ticks_per_second,
10096+
(double)process.tms_stime / ticks_per_second,
10097+
(double)process.tms_cutime / ticks_per_second,
10098+
(double)process.tms_cstime / ticks_per_second,
10099+
(double)elapsed / ticks_per_second);
1010610100
#endif /* MS_WINDOWS */
10107-
#endif /* HAVE_TIMES */
10101+
}
1010810102

1010910103

1011010104
#if defined(HAVE_TIMERFD_CREATE)
@@ -17279,6 +17273,15 @@ posixmodule_exec(PyObject *m)
1727917273
Py_DECREF(unicode);
1728017274
}
1728117275

17276+
#ifndef MS_WINDOWS
17277+
if (_Py_GetTicksPerSecond(&state->ticks_per_second) < 0) {
17278+
PyErr_SetString(PyExc_RuntimeError,
17279+
"cannot read ticks_per_second");
17280+
return -1;
17281+
}
17282+
assert(state->ticks_per_second >= 1);
17283+
#endif
17284+
1728217285
return PyModule_Add(m, "_have_functions", list);
1728317286
}
1728417287

0 commit comments

Comments
 (0)