Skip to content

Commit

Permalink
pythongh-112567: Add _Py_GetTicksPerSecond() function (python#112587)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
vstinner authored Dec 1, 2023
1 parent a907356 commit 05a370a
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 125 deletions.
4 changes: 4 additions & 0 deletions Include/internal/pycore_fileutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ PyAPI_FUNC(char*) _Py_UniversalNewlineFgetsWithSize(char *, int, FILE*, PyObject

extern int _PyFile_Flush(PyObject *);

#ifndef MS_WINDOWS
extern int _Py_GetTicksPerSecond(long *ticks_per_second);
#endif

#ifdef __cplusplus
}
#endif
Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ extern void _PySys_FiniTypes(PyInterpreterState *interp);
extern int _PyBuiltins_AddExceptions(PyObject * bltinmod);
extern PyStatus _Py_HashRandomization_Init(const PyConfig *);

extern PyStatus _PyTime_Init(void);
extern PyStatus _PyGC_Init(PyInterpreterState *interp);
extern PyStatus _PyAtExit_Init(PyInterpreterState *interp);
extern int _Py_Deepfreeze_Init(void);
Expand Down
2 changes: 0 additions & 2 deletions Include/internal/pycore_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ extern "C" {
#include "pycore_pymem.h" // struct _pymem_allocators
#include "pycore_pythread.h" // struct _pythread_runtime_state
#include "pycore_signal.h" // struct _signals_runtime_state
#include "pycore_time.h" // struct _time_runtime_state
#include "pycore_tracemalloc.h" // struct _tracemalloc_runtime_state
#include "pycore_typeobject.h" // struct _types_runtime_state
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_state
Expand Down Expand Up @@ -205,7 +204,6 @@ typedef struct pyruntimestate {
struct _pymem_allocators allocators;
struct _obmalloc_global_state obmalloc;
struct pyhash_runtime_state pyhash_state;
struct _time_runtime_state time;
struct _pythread_runtime_state threads;
struct _signals_runtime_state signals;

Expand Down
10 changes: 0 additions & 10 deletions Include/internal/pycore_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ extern "C" {
#endif


struct _time_runtime_state {
#ifdef HAVE_TIMES
int ticks_per_second_initialized;
long ticks_per_second;
#else
int _not_used;
#endif
};


#ifdef __clang__
struct timeval;
#endif
Expand Down
10 changes: 1 addition & 9 deletions Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 28 additions & 25 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,10 @@ typedef struct {
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;


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




static PyStructSequence_Field times_result_fields[] = {
{"user", "user time"},
{"system", "system time"},
Expand All @@ -10013,12 +10015,6 @@ static PyStructSequence_Desc times_result_desc = {
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,
Expand Down Expand Up @@ -10064,8 +10060,8 @@ All fields are floating point numbers.
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();
Expand All @@ -10083,28 +10079,26 @@ os_times_impl(PyObject *module)
(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)
Expand Down Expand Up @@ -17279,6 +17273,15 @@ posixmodule_exec(PyObject *m)
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);
}

Expand Down
Loading

0 comments on commit 05a370a

Please sign in to comment.