[Python-checkins] bpo-40192: Use thread_cputime for time.thread_time to improve resolution (GH-19381)

Batuhan Taskaya webhook-mailer at python.org
Sat May 16 05:39:13 EDT 2020


https://github.com/python/cpython/commit/45410862321ae509e8753f239b0ea28fdcef5bad
commit: 45410862321ae509e8753f239b0ea28fdcef5bad
branch: master
author: Batuhan Taskaya <isidentical at gmail.com>
committer: GitHub <noreply at github.com>
date: 2020-05-16T11:39:09+02:00
summary:

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.

files:
A Misc/NEWS.d/next/Library/2020-04-05-04-16-14.bpo-40192.nk8uRJ.rst
M Doc/whatsnew/3.9.rst
M Modules/timemodule.c

diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst
index c721a167440c3..cbddbb4f3f962 100644
--- a/Doc/whatsnew/3.9.rst
+++ b/Doc/whatsnew/3.9.rst
@@ -480,6 +480,14 @@ The :mod:`socket` module now exports the :data:`~socket.CAN_RAW_JOIN_FILTERS`
 constant on Linux 4.1 and greater.
 (Contributed by Stefan Tatschner and Zackery Spytz in :issue:`25780`.)
 
+time
+----
+
+On AIX, :func:`~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.
+(Contributed by Batuhan Taskaya in :issue:`40192`)
+
 sys
 ---
 
diff --git a/Misc/NEWS.d/next/Library/2020-04-05-04-16-14.bpo-40192.nk8uRJ.rst b/Misc/NEWS.d/next/Library/2020-04-05-04-16-14.bpo-40192.nk8uRJ.rst
new file mode 100644
index 0000000000000..e1e7fcefe3f94
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-04-05-04-16-14.bpo-40192.nk8uRJ.rst
@@ -0,0 +1,4 @@
+On AIX, :func:`~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.
+Patch by Batuhan Taskaya.
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index a0e66ac170b21..8a4d149befb52 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -24,6 +24,10 @@
 #  include <pthread.h>
 #endif
 
+#if defined(_AIX)
+#   include <sys/thread.h>
+#endif
+
 #if defined(__WATCOMC__) && !defined(__QNX__)
 #  include <i86.h>
 #else
@@ -1343,6 +1347,30 @@ _PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
     return 0;
 }
 
+#elif defined(_AIX)
+#define HAVE_THREAD_TIME
+static int
+_PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
+{
+    /* bpo-40192: On AIX, thread_cputime() is preferred: it has nanosecond
+       resolution, whereas clock_gettime(CLOCK_THREAD_CPUTIME_ID)
+       has a resolution of 10 ms. */
+    thread_cputime_t tc;
+    if (thread_cputime(-1, &tc) != 0) {
+        PyErr_SetFromErrno(PyExc_OSError);
+        return -1;
+    }
+
+    if (info) {
+        info->implementation = "thread_cputime()";
+        info->monotonic = 1;
+        info->adjustable = 0;
+        info->resolution = 1e-9;
+    }
+    *tp = _PyTime_FromNanoseconds(tc.stime + tc.utime);
+    return 0;
+}
+
 #elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID)
 #define HAVE_THREAD_TIME
 static int



More information about the Python-checkins mailing list