Skip to content

Commit

Permalink
pythongh-110850: Cleanup PyTime API: PyTime_t are nanoseconds (python…
Browse files Browse the repository at this point in the history
…#115753)

PyTime_t no longer uses an arbitrary unit, it's always a number of
nanoseconds (64-bit signed integer).

* Rename _PyTime_FromNanosecondsObject() to _PyTime_FromLong().
* Rename _PyTime_AsNanosecondsObject() to _PyTime_AsLong().
* Remove pytime_from_nanoseconds().
* Remove pytime_as_nanoseconds().
* Remove _PyTime_FromNanoseconds().
  • Loading branch information
vstinner authored and diegorusso committed Apr 17, 2024
1 parent 9e8a9c9 commit 718e8e9
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 100 deletions.
13 changes: 4 additions & 9 deletions Include/internal/pycore_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,13 @@ PyAPI_FUNC(PyTime_t) _PyTime_FromSecondsDouble(double seconds, _PyTime_round_t r
#define _PYTIME_FROMSECONDS(seconds) \
((PyTime_t)(seconds) * (1000 * 1000 * 1000))

// Create a timestamp from a number of nanoseconds.
// Export for '_testinternalcapi' shared extension.
PyAPI_FUNC(PyTime_t) _PyTime_FromNanoseconds(PyTime_t ns);

// Create a timestamp from a number of microseconds.
// Clamp to [PyTime_MIN; PyTime_MAX] on overflow.
extern PyTime_t _PyTime_FromMicrosecondsClamp(PyTime_t us);

// Create a timestamp from nanoseconds (Python int).
// Create a timestamp from a Python int object (number of nanoseconds).
// Export for '_lsprof' shared extension.
PyAPI_FUNC(int) _PyTime_FromNanosecondsObject(PyTime_t *t,
PyAPI_FUNC(int) _PyTime_FromLong(PyTime_t *t,
PyObject *obj);

// Convert a number of seconds (Python float or int) to a timestamp.
Expand Down Expand Up @@ -183,10 +179,9 @@ extern PyTime_t _PyTime_As100Nanoseconds(PyTime_t t,
_PyTime_round_t round);
#endif

// Convert timestamp to a number of nanoseconds (10^-9 seconds) as a Python int
// object.
// Convert a timestamp (number of nanoseconds) as a Python int object.
// Export for '_testinternalcapi' shared extension.
PyAPI_FUNC(PyObject*) _PyTime_AsNanosecondsObject(PyTime_t t);
PyAPI_FUNC(PyObject*) _PyTime_AsLong(PyTime_t t);

#ifndef MS_WINDOWS
// Create a timestamp from a timeval structure.
Expand Down
4 changes: 2 additions & 2 deletions Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_SetProfile()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_time.h" // _PyTime_FromNanosecondsObject()
#include "pycore_time.h" // _PyTime_FromLong()

#include "rotatingtree.h"

Expand Down Expand Up @@ -98,7 +98,7 @@ static PyTime_t CallExternalTimer(ProfilerObject *pObj)
if (pObj->externalTimerUnit > 0.0) {
/* interpret the result as an integer that will be scaled
in profiler_getstats() */
err = _PyTime_FromNanosecondsObject(&result, o);
err = _PyTime_FromLong(&result, o);
}
else {
/* interpret the result as a double measured in seconds.
Expand Down
22 changes: 10 additions & 12 deletions Modules/_testinternalcapi/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test_pytime_fromseconds(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t ts = _PyTime_FromSeconds(seconds);
return _PyTime_AsNanosecondsObject(ts);
return _PyTime_AsLong(ts);
}

static int
Expand Down Expand Up @@ -49,7 +49,7 @@ test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) {
return NULL;
}
return _PyTime_AsNanosecondsObject(ts);
return _PyTime_AsLong(ts);
}

static PyObject *
Expand All @@ -64,7 +64,7 @@ test_PyTime_AsTimeval(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timeval tv;
Expand All @@ -91,7 +91,7 @@ test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timeval tv;
Expand All @@ -113,7 +113,7 @@ test_PyTime_AsTimespec(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timespec ts;
Expand All @@ -131,7 +131,7 @@ test_PyTime_AsTimespec_clamp(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
struct timespec ts;
Expand All @@ -149,15 +149,14 @@ test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
PyTime_t ms = _PyTime_AsMilliseconds(t, round);
PyTime_t ns = _PyTime_FromNanoseconds(ms);
return _PyTime_AsNanosecondsObject(ns);
return _PyTime_AsLong(ms);
}

static PyObject *
Expand All @@ -169,15 +168,14 @@ test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
return NULL;
}
PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
PyTime_t us = _PyTime_AsMicroseconds(t, round);
PyTime_t ns = _PyTime_FromNanoseconds(us);
return _PyTime_AsNanosecondsObject(ns);
return _PyTime_AsLong(us);
}

static PyObject *
Expand Down
33 changes: 15 additions & 18 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ time_time_ns(PyObject *self, PyObject *unused)
if (PyTime_Time(&t) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}

PyDoc_STRVAR(time_ns_doc,
Expand Down Expand Up @@ -164,8 +164,7 @@ py_clock(time_module_state *state, PyTime_t *tp, _Py_clock_info_t *info)
"or its value cannot be represented");
return -1;
}
PyTime_t ns = _PyTimeFraction_Mul(ticks, base);
*tp = _PyTime_FromNanoseconds(ns);
*tp = _PyTimeFraction_Mul(ticks, base);
return 0;
}
#endif /* HAVE_CLOCK */
Expand Down Expand Up @@ -259,7 +258,7 @@ time_clock_gettime_ns_impl(PyObject *module, clockid_t clk_id)
if (_PyTime_FromTimespec(&t, &ts) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}
#endif /* HAVE_CLOCK_GETTIME */

Expand Down Expand Up @@ -308,7 +307,7 @@ time_clock_settime_ns(PyObject *self, PyObject *args)
return NULL;
}

if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
if (_PyTime_FromLong(&t, obj) < 0) {
return NULL;
}
if (_PyTime_AsTimespec(t, &ts) == -1) {
Expand Down Expand Up @@ -1170,7 +1169,7 @@ time_monotonic_ns(PyObject *self, PyObject *unused)
if (PyTime_Monotonic(&t) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}

PyDoc_STRVAR(monotonic_ns_doc,
Expand Down Expand Up @@ -1202,7 +1201,7 @@ time_perf_counter_ns(PyObject *self, PyObject *unused)
if (PyTime_PerfCounter(&t) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}

PyDoc_STRVAR(perf_counter_ns_doc,
Expand Down Expand Up @@ -1233,7 +1232,7 @@ process_time_times(time_module_state *state, PyTime_t *tp,
PyTime_t ns;
ns = _PyTimeFraction_Mul(process.tms_utime, base);
ns += _PyTimeFraction_Mul(process.tms_stime, base);
*tp = _PyTime_FromNanoseconds(ns);
*tp = ns;
return 1;
}
#endif
Expand All @@ -1247,7 +1246,7 @@ py_process_time(time_module_state *state, PyTime_t *tp,
HANDLE process;
FILETIME creation_time, exit_time, kernel_time, user_time;
ULARGE_INTEGER large;
PyTime_t ktime, utime, t;
PyTime_t ktime, utime;
BOOL ok;

process = GetCurrentProcess();
Expand All @@ -1274,8 +1273,7 @@ py_process_time(time_module_state *state, PyTime_t *tp,
utime = large.QuadPart;

/* ktime and utime have a resolution of 100 nanoseconds */
t = _PyTime_FromNanoseconds((ktime + utime) * 100);
*tp = t;
*tp = (ktime + utime) * 100;
return 0;
#else

Expand Down Expand Up @@ -1383,7 +1381,7 @@ time_process_time_ns(PyObject *module, PyObject *unused)
if (py_process_time(state, &t, NULL) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}

PyDoc_STRVAR(process_time_ns_doc,
Expand All @@ -1401,7 +1399,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
HANDLE thread;
FILETIME creation_time, exit_time, kernel_time, user_time;
ULARGE_INTEGER large;
PyTime_t ktime, utime, t;
PyTime_t ktime, utime;
BOOL ok;

thread = GetCurrentThread();
Expand All @@ -1428,8 +1426,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
utime = large.QuadPart;

/* ktime and utime have a resolution of 100 nanoseconds */
t = _PyTime_FromNanoseconds((ktime + utime) * 100);
*tp = t;
*tp = (ktime + utime) * 100;
return 0;
}

Expand All @@ -1453,7 +1450,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
info->adjustable = 0;
info->resolution = 1e-9;
}
*tp = _PyTime_FromNanoseconds(tc.stime + tc.utime);
*tp = (tc.stime + tc.utime);
return 0;
}

Expand All @@ -1470,7 +1467,7 @@ _PyTime_GetThreadTimeWithInfo(PyTime_t *tp, _Py_clock_info_t *info)
info->monotonic = 1;
info->adjustable = 0;
}
*tp = _PyTime_FromNanoseconds(gethrvtime());
*tp = gethrvtime();
return 0;
}

Expand Down Expand Up @@ -1550,7 +1547,7 @@ time_thread_time_ns(PyObject *self, PyObject *unused)
if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) {
return NULL;
}
return _PyTime_AsNanosecondsObject(t);
return _PyTime_AsLong(t);
}

PyDoc_STRVAR(thread_time_ns_doc,
Expand Down
Loading

0 comments on commit 718e8e9

Please sign in to comment.