-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-32025: Add time.thread_time() #4410
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2017-11-15-20-03-45.bpo-32025.lnIKYT.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add time.thread_time() and time.thread_time_ns() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1258,6 +1258,112 @@ Process time for profiling as nanoseconds:\n\ | |
sum of the kernel and user-space CPU time."); | ||
|
||
|
||
#if defined(MS_WINDOWS) | ||
#define HAVE_THREAD_TIME | ||
static int | ||
_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; | ||
BOOL ok; | ||
|
||
thread = GetCurrentThread(); | ||
ok = GetThreadTimes(thread, &creation_time, &exit_time, | ||
&kernel_time, &user_time); | ||
if (!ok) { | ||
PyErr_SetFromWindowsErr(0); | ||
return -1; | ||
} | ||
|
||
if (info) { | ||
info->implementation = "GetThreadTimes()"; | ||
info->resolution = 1e-7; | ||
info->monotonic = 1; | ||
info->adjustable = 0; | ||
} | ||
|
||
large.u.LowPart = kernel_time.dwLowDateTime; | ||
large.u.HighPart = kernel_time.dwHighDateTime; | ||
ktime = large.QuadPart; | ||
|
||
large.u.LowPart = user_time.dwLowDateTime; | ||
large.u.HighPart = user_time.dwHighDateTime; | ||
utime = large.QuadPart; | ||
|
||
/* ktime and utime have a resolution of 100 nanoseconds */ | ||
t = _PyTime_FromNanoseconds((ktime + utime) * 100); | ||
*tp = t; | ||
return 0; | ||
} | ||
|
||
#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_PROCESS_CPUTIME_ID) | ||
#define HAVE_THREAD_TIME | ||
static int | ||
_PyTime_GetThreadTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info) | ||
{ | ||
struct timespec ts; | ||
const clockid_t clk_id = CLOCK_THREAD_CPUTIME_ID; | ||
const char *function = "clock_gettime(CLOCK_THREAD_CPUTIME_ID)"; | ||
|
||
if (clock_gettime(clk_id, &ts)) { | ||
PyErr_SetFromErrno(PyExc_OSError); | ||
return -1; | ||
} | ||
if (info) { | ||
struct timespec res; | ||
info->implementation = function; | ||
info->monotonic = 1; | ||
info->adjustable = 0; | ||
if (clock_getres(clk_id, &res)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is really a copy-paste job. |
||
PyErr_SetFromErrno(PyExc_OSError); | ||
return -1; | ||
} | ||
info->resolution = res.tv_sec + res.tv_nsec * 1e-9; | ||
} | ||
|
||
if (_PyTime_FromTimespec(tp, &ts) < 0) { | ||
return -1; | ||
} | ||
return 0; | ||
} | ||
#endif | ||
|
||
#ifdef HAVE_THREAD_TIME | ||
static PyObject * | ||
time_thread_time(PyObject *self, PyObject *unused) | ||
{ | ||
_PyTime_t t; | ||
if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) { | ||
return NULL; | ||
} | ||
return _PyFloat_FromPyTime(t); | ||
} | ||
|
||
PyDoc_STRVAR(thread_time_doc, | ||
"thread_time() -> float\n\ | ||
\n\ | ||
Thread time for profiling: sum of the kernel and user-space CPU time."); | ||
|
||
static PyObject * | ||
time_thread_time_ns(PyObject *self, PyObject *unused) | ||
{ | ||
_PyTime_t t; | ||
if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) { | ||
return NULL; | ||
} | ||
return _PyTime_AsNanosecondsObject(t); | ||
} | ||
|
||
PyDoc_STRVAR(thread_time_ns_doc, | ||
"thread_time() -> int\n\ | ||
\n\ | ||
Thread time for profiling as nanoseconds:\n\ | ||
sum of the kernel and user-space CPU time."); | ||
#endif | ||
|
||
|
||
static PyObject * | ||
time_get_clock_info(PyObject *self, PyObject *args) | ||
{ | ||
|
@@ -1311,6 +1417,13 @@ time_get_clock_info(PyObject *self, PyObject *args) | |
return NULL; | ||
} | ||
} | ||
#ifdef HAVE_THREAD_TIME | ||
else if (strcmp(name, "thread_time") == 0) { | ||
if (_PyTime_GetThreadTimeWithInfo(&t, &info) < 0) { | ||
return NULL; | ||
} | ||
} | ||
#endif | ||
else { | ||
PyErr_SetString(PyExc_ValueError, "unknown clock"); | ||
return NULL; | ||
|
@@ -1519,6 +1632,10 @@ static PyMethodDef time_methods[] = { | |
{"monotonic_ns", time_monotonic_ns, METH_NOARGS, monotonic_ns_doc}, | ||
{"process_time", time_process_time, METH_NOARGS, process_time_doc}, | ||
{"process_time_ns", time_process_time_ns, METH_NOARGS, process_time_ns_doc}, | ||
#ifdef HAVE_THREAD_TIME | ||
{"thread_time", time_thread_time, METH_NOARGS, thread_time_doc}, | ||
{"thread_time_ns", time_thread_time_ns, METH_NOARGS, thread_time_ns_doc}, | ||
#endif | ||
{"perf_counter", time_perf_counter, METH_NOARGS, perf_counter_doc}, | ||
{"perf_counter_ns", time_perf_counter_ns, METH_NOARGS, perf_counter_ns_doc}, | ||
{"get_clock_info", time_get_clock_info, METH_VARARGS, get_clock_info_doc}, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be
0.100 - 0.020
or maybe just0.100
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't be too strict, or the test will fail randomly on grumpy buildbots.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole CPU time in those 0.1 seconds was not necessarily allocated to the current process / thread.