8000 gh-81057: Move time Globals to _PyRuntimeState by ericsnowcurrently · Pull Request #100122 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-81057: Move time Globals to _PyRuntimeState #100122

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
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move _PyTime_GetProcessTimeWithInfo():ticks_per_second to _PyRuntimeS…
…tate.
  • Loading branch information
ericsnowcurrently committed Dec 8, 2022
commit f22e017be2df4a923b402a708d4f139134310bcf
14 changes: 0 additions & 14 deletions Include/internal/pycore_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,12 @@ extern "C" {
#endif


#ifndef MS_WINDOWS
#define _OS_NEED_TICKS_PER_SECOND
# define need_ticks_per_second_STATE \
long ticks_per_second;
# define need_ticks_per_second_INIT \
.ticks_per_second = -1,
#else
# define need_ticks_per_second_STATE
# define need_ticks_per_second_INIT
#endif /* MS_WINDOWS */


struct _os_runtime_state {
int _not_used;
need_ticks_per_second_STATE
};
# define _OS_RUNTIME_INIT \
{ \
._not_used = 0, \
need_ticks_per_second_INIT \
}


Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ extern void _PySys_Fini(PyInterpreterState *interp);
extern int _PyBuiltins_AddExceptions(PyObject * bltinmod);
extern PyStatus _Py_HashRandomization_Init(const PyConfig *);

extern PyStatus _PyTime_Init(void);
extern PyStatus _PyImportZip_Init(PyThreadState *tstate);
extern PyStatus _PyGC_Init(PyInterpreterState *interp);
extern PyStatus _PyAtExit_Init(PyInterpreterState *interp);
Expand Down
7 changes: 6 additions & 1 deletion Include/internal/pycore_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ extern "C" {


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


Expand Down
30 changes: 5 additions & 25 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -9065,24 +9065,6 @@ build_times_result(PyObject *module, double user, double system,
}


#ifdef _OS_NEED_TICKS_PER_SECOND
#define ticks_per_second _PyRuntime.os.ticks_per_second
static void
ticks_per_second_init(void)
{
if (ticks_per_second != -1) {
return;
}
# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
ticks_per_second = sysconf(_SC_CLK_TCK);
# elif defined(HZ)
ticks_per_second = HZ;
# else
ticks_per_second = 60; /* magic fallback value; may be bogus */
# endif
}
#endif

/*[clinic input]
os.times

Expand Down Expand Up @@ -9116,22 +9098,24 @@ os_times_impl(PyObject *module)
(double)0,
(double)0);
}
#elif !defined(_OS_NEED_TICKS_PER_SECOND)
# error "missing ticks_per_second"
#else /* MS_WINDOWS */
{
struct tms t;
clock_t c;
errno = 0;
c = times(&t);
if (c == (clock_t) -1)
if (c == (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
}
#endif /* MS_WINDOWS */
#endif /* HAVE_TIMES */
Expand Down Expand Up @@ -15950,10 +15934,6 @@ posixmodule_exec(PyObject *m)
PyModule_AddObject(m, "statvfs_result", Py_NewRef(StatVFSResultType));
state->StatVFSResultType = StatVFSResultType;

#ifdef _OS_NEED_TICKS_PER_SECOND
ticks_per_second_init();
#endif

#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
sched_param_desc.name = MODNAME ".sched_param";
PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
Expand Down
92 changes: 53 additions & 39 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,54 @@
#define SEC_TO_NS (1000 * 1000 * 1000)


#ifdef HAVE_TIMES

static int
check_ticks_per_second(long tps, const char *context)
{
/* Effectively, check that _PyTime_MulDiv(t, SEC_TO_NS, ticks_per_second)
cannot overflow. */
if (tps >= 0 && (_PyTime_t)tps > _PyTime_MAX / SEC_TO_NS) {
PyErr_Format(PyExc_OverflowError, "%s is too large", context);
return -1;
}
return 0;
}

# 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);

Expand Down Expand Up @@ -140,16 +188,8 @@ Return the current time in nanoseconds since the Epoch.");
static int
_PyTime_GetClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
{
if (!_PyRuntime.time.clocks_per_sec_checked) {
_PyRuntime.time.clocks_per_sec_checked = 1;

/* Make sure that _PyTime_MulDiv(ticks, SEC_TO_NS, CLOCKS_PER_SEC)
above cannot overflow */
if ((_PyTime_t)CLOCKS_PER_SEC > _PyTime_MAX / SEC_TO_NS) {
PyErr_SetString(PyExc_OverflowError,
"CLOCKS_PER_SEC is too large");
return -1;
}
if (check_ticks_per_second(CLOCKS_PER_SEC, "CLOCKS_PER_SEC") < 0) {
return -1;
}

if (info) {
Expand Down Expand Up @@ -1306,36 +1346,10 @@ _PyTime_GetProcessTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
struct tms t;

if (times(&t) != (clock_t)-1) {
static long ticks_per_second = -1;

if (ticks_per_second == -1) {
long freq;
#if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
freq = sysconf(_SC_CLK_TCK);
if (freq < 1) {
freq = -1;
}
#elif defined(HZ)
freq = HZ;
#else
freq = 60; /* magic fallback value; may be bogus */
#endif

if (freq != -1) {
/* check that _PyTime_MulDiv(t, SEC_TO_NS, ticks_per_second)
cannot overflow below */
#if LONG_MAX > _PyTime_MAX / SEC_TO_NS
if ((_PyTime_t)freq > _PyTime_MAX / SEC_TO_NS) {
PyErr_SetString(PyExc_OverflowError,
"_SC_CLK_TCK is too large");
return -1;
}
#endif

ticks_per_second = freq;
}
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()";
Expand Down
5 changes: 5 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,11 @@ pycore_init_runtime(_PyRuntimeState *runtime,
return status;
}

status = _PyTime_Init();
if (_PyStatus_EXCEPTION(status)) {
return status;
}

status = _PyImport_Init();
if (_PyStatus_EXCEPTION(status)) {
return status;
Expand Down
5 changes: 0 additions & 5 deletions Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,6 @@ Modules/faulthandler.c - old_stack -
##################################
## global non-objects to fix in builtin modules

##-----------------------
## initialized once

Modules/timemodule.c _PyTime_GetProcessTimeWithInfo ticks_per_second -

##-----------------------
## state

Expand Down
0