Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4541086

Browse files
authored
bpo-40192: Use thread_cputime for time.thread_time to improve resolution (GH-19381)
On AIX, time.thread_time() is now implemented with thread_cputime() which has nanosecond resolution, rather than clock_gettime(CLOCK_THREAD_CPUTIME_ID) which has a resolution of 10 ms.
1 parent 62972d9 commit 4541086

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

Doc/whatsnew/3.9.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,14 @@ The :mod:`socket` module now exports the :data:`~socket.CAN_RAW_JOIN_FILTERS`
480480
constant on Linux 4.1 and greater.
481481
(Contributed by Stefan Tatschner and Zackery Spytz in :issue:`25780`.)
482482

483+
time
484+
----
485+
486+
On AIX, :func:`~time.thread_time` is now implemented with ``thread_cputime()``
487+
which has nanosecond resolution, rather than
488+
``clock_gettime(CLOCK_THREAD_CPUTIME_ID)`` which has a resolution of 10 ms.
489+
(Contributed by Batuhan Taskaya in :issue:`40192`)
490+
483491
sys
484492
---
485493

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
On AIX, :func:`~time.thread_time` is now implemented with ``thread_cputime()``
2+
which has nanosecond resolution, rather than
3+
``clock_gettime(CLOCK_THREAD_CPUTIME_ID)`` which has a resolution of 10 ms.
4+
Patch by Batuhan Taskaya.

Modules/timemodule.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
# include <pthread.h>
2525
#endif
2626

27+
#if defined(_AIX)
28+
# include <sys/thread.h>
29+
#endif
30+
2731
#if defined(__WATCOMC__) && !defined(__QNX__)
2832
# include <i86.h>
2933
#else
@@ -1343,6 +1347,30 @@ _PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
13431347
return 0;
13441348
}
13451349

1350+
#elif defined(_AIX)
1351+
#define HAVE_THREAD_TIME
1352+
static int
1353+
_PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
1354+
{
1355+
/* bpo-40192: On AIX, thread_cputime() is preferred: it has nanosecond
1356+
resolution, whereas clock_gettime(CLOCK_THREAD_CPUTIME_ID)
1357+
has a resolution of 10 ms. */
1358+
thread_cputime_t tc;
1359+
if (thread_cputime(-1, &tc) != 0) {
1360+
PyErr_SetFromErrno(PyExc_OSError);
1361+
return -1;
1362+
}
1363+
1364+
if (info) {
1365+
info->implementation = "thread_cputime()";
1366+
info->monotonic = 1;
1367+
info->adjustable = 0;
1368+
info->resolution = 1e-9;
1369+
}
1370+
*tp = _PyTime_FromNanoseconds(tc.stime + tc.utime);
1371+
return 0;
1372+
}
1373+
13461374
#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
13471375
#define HAVE_THREAD_TIME
13481376
static int

0 commit comments

Comments
 (0)