-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
[POSIX] psutil.users() loses precision for "started" attribute #2225
Comments
Please create a PR or a diff to attach to this ticket. |
I don't have a lot of CPython dev experience, but I think this is what's needed for the Linux case: However, a similar fix is almost certainly needed for (at least) _psutil_aix.c, _psutil_bsd.c, _psutil_osx.c, and _psutil_sunos.c; some of these may also need comparable changes in their psutil_boot_time() functions (which appear to use utmp rather than /proc/stat as Linux does). In a nutshell, anywhere the code says "(float)ut->ut_tv.tv_sec" or "(float)utx->ut_tv.tv_sec" requires attention. |
Yes, I added a unit test and I could verify that the time is off by 1 minute compared to |
PR: #2226. |
"boot_time = (float)ut->ut_tv.tv_sec" in _psutil_aix.c and _psutil_sunos.c probably also need similar fixes (the declaration of the boot_time variable, the cast, and the Py_BuildValue call). |
Yes, I have changed it also on those platforms. |
I see that you fixed psutil_users() for those platforms; I'm referring to psutil_boot_time(), which appears to have a nearly identical bug: Line 724 in 90b35e3
Line 1425 in 90b35e3
Technically these bugs aren't related to this psutils.users() issue, but assuming the code is used, it appears to me that psutil.boot_time() would have a similar loss of precision. (I'd file this as a separate issue, but I don't have those platforms available to confirm the bug.) |
Thank you. I have created #2228 to keep track of the boot_time() precision issue. |
In _psutil_linux.c, psutil_users() casts ut->ut_tv.tv_sec to type float (single-precision); this is insufficient to represent contemporary timestamp values with reasonable accuracy.
Currently, psutil.users()[i].started can be inaccurate by more than a full minute.
Note that while Python float values are represented as 64-bit double-precision values, the float type in C is typically only 32-bit. The code should probably use "000d0" _Py_PARSE_PID and (double)ut->ut_tv.tv_sec rather than "000f0" _Py_PARSE_PID and (float)ut->ut_tv.tv_sec in the Py_BuildValue() call.
The text was updated successfully, but these errors were encountered: