-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
OSError: Invalid argument in test_get_clock_info when testing thread_time clock on NetBSD #123974
Comments
According to the NetBSD documentation, CLOCK_THREAD_CPUTIME_ID is supported by clock_getres. However, when using clock_getres with the CLOCK_THREAD_CPUTIME_ID clock ID, the function returns -1 and sets errno to EINVAL. Example: #include <stdio.h>
#include <stdlib.h>
#include <time.h>
void print_clock_resolution(clockid_t clock_id, const char *name) {
struct timespec res;
if (clock_getres(clock_id, &res) == -1) {
perror(name);
return;
}
printf("Resolution of %s: %ld.%09ld seconds\n", name, res.tv_sec, res.tv_nsec);
}
int main() {
print_clock_resolution(CLOCK_REALTIME, "CLOCK_REALTIME");
print_clock_resolution(CLOCK_MONOTONIC, "CLOCK_MONOTONIC");
print_clock_resolution(CLOCK_VIRTUAL, "CLOCK_VIRTUAL");
print_clock_resolution(CLOCK_PROF, "CLOCK_PROF");
print_clock_resolution(CLOCK_PROCESS_CPUTIME_ID, "CLOCK_PROCESS_CPUTIME_ID");
print_clock_resolution(CLOCK_THREAD_CPUTIME_ID, "CLOCK_THREAD_CPUTIME_ID");
return 0;
} Output: $ gcc -o test_clock test_clock.c
$ ./test_clock
Resolution of CLOCK_REALTIME: 0.000000010 seconds
Resolution of CLOCK_MONOTONIC: 0.000000010 seconds
CLOCK_VIRTUAL: Invalid argument
CLOCK_PROF: Invalid argument
CLOCK_PROCESS_CPUTIME_ID: Invalid argument
CLOCK_THREAD_CPUTIME_ID: Invalid argument |
If I understand correctly, only CLOCK_REALTIME and CLOCK_MONOTONIC are supported: The result depends on the frequency. The source of |
Yes, CLOCK_REALTIME and CLOCK_MONOTONIC are supported. >>> import time
>>> time.clock_getres(time.CLOCK_MONOTONIC)
1e-09
>>> I updated the code with resolution 1e-09. |
Does the test for @vstinner, should |
Fix OSError for thread_time clock on NetBSD by setting default resolution.
Fix OSError for thread_time clock on NetBSD by setting default resolution. (cherry picked from commit b1d6f8a) Co-authored-by: Furkan Onder <[email protected]>
Fix OSError for thread_time clock on NetBSD by setting default resolution. (cherry picked from commit b1d6f8a) Co-authored-by: Furkan Onder <[email protected]>
I merged the PR with a hardcoded resolution of 1 ns. It's not great, but better than nothing. It allows to get other information about the clock at least. |
@furkanonder: You may report the issue to NetBSD. clock_getres() should return "some value" rather than fail with EINVAL as if the clock doesn't exist. |
Sorry, I forgot to mention. I reported the issue. |
This is the only failed test(#123979) at the moment. |
…24073) gh-123974: Fix time.get_clock_info() on NetBSD (GH-123975) Fix OSError for thread_time clock on NetBSD by setting default resolution. (cherry picked from commit b1d6f8a) Co-authored-by: Furkan Onder <[email protected]>
I close again the issue, I'm not sure why you reopened it. The remaining test failure is tracked by #123978. |
I couldn't make comment without reopen. Also I asked question to @serhiy-storchaka . I was waiting her response. |
Oh ok. Thanks for your bug report and fix, by the way ;-) |
Your are welcome :) |
Are test_process_time and test_get_clock_info passed? What does test_thread_time is also affected by this issue, but it fails earlier for other reason. |
Python 3.13.0rc2+ (heads/3.13:112b1704fa6, Sep 14 2024, 02:50:28) [GCC 10.5.0] on netbsd10
Type "help", "copyright", "credits" or "license" for more information.
warning: can't use pyrepl: No module named 'msvcrt'
>>> import time
>>> time.get_clock_info('process_time')
namespace(implementation='getrusage(RUSAGE_SELF)', monotonic=True, adjustable=False, resolution=1e-06)
>>> time.get_clock_info('thread_time')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
time.get_clock_info('thread_time')
~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument
>>> time.thread_time()
5.73
>>>
After the merge(#123975); -bash-5.2$ ./python
Python 3.14.0a0 (heads/main:b1d6f8a2ee0, Sep 14 2024, 02:36:32) [GCC 10.5.0] on netbsd10
Type "help", "copyright", "credits" or "license" for more information.
warning: can't use pyrepl: No module named 'msvcrt'
>>> import time
>>> time.get_clock_info('process_time')
namespace(implementation='getrusage(RUSAGE_SELF)', monotonic=True, adjustable=False, resolution=1e-06)
>>> time.get_clock_info('thread_time')
namespace(implementation='clock_gettime(CLOCK_THREAD_CPUTIME_ID)', monotonic=True, adjustable=False, resolution=1e-09)
>>> time.thread_time()
5.04
>>>
There was no issue with static PyObject *
time_thread_time(PyObject *self, PyObject *unused)
{
PyTime_t t;
if (_PyTime_GetThreadTimeWithInfo(&t, NULL) < 0) {
return NULL;
}
return _PyFloat_FromPyTime(t);
} 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)) {
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;
} |
See the discussion in #123979. It seems that After this, I am not sure that we should support |
I would be fine with removing time.thread_time() on NetBSD. |
Should it be removed immediately, or should a |
It can be removed immediatly if it's broken |
…H-123975)" (pythonGH-124115) This reverts commit b1d6f8a. (cherry picked from commit 79a7410) Co-authored-by: Serhiy Storchaka <[email protected]>
)" (GH-124115) (GH-124200) This reverts commit b1d6f8a. (cherry picked from commit 79a7410) Co-authored-by: Serhiy Storchaka <[email protected]>
…123975)" (pythonGH-124115) This reverts commit b1d6f8a.
Bug report
Bug description:
OS:
NetBSD 10.0 amd64
CPython versions tested on:
CPython main branch
Operating systems tested on:
Other
Linked PRs
The text was updated successfully, but these errors were encountered: