Skip to content
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

convert mach tick units to nsecs #1958

Closed
wants to merge 1 commit into from
Closed

Conversation

rmalouf
Copy link

@rmalouf rmalouf commented Jun 6, 2021

Summary

Description

{{{
Mach sys calls report cpu time in "tick units" -- on intel cpus, these happen to be nsecs but on M1 machines they're about 40 nsecs. This PR corrects for this, but I think it still needs two improvements: the value of mach_timebase_info should be cached somewhere, and there are probably other places where the code depends on 1 tick=1 nsec that also need to be updated. Sounds reasonable? I can work on both of those, but I wanted to get some feedback first before I spend any more time on this.
}}}

@giampaolo
Copy link
Owner

Shouldn't this be executed on M1 only (if M1 {do_this} else {do_old_stuff})?

@rmalouf
Copy link
Author

rmalouf commented Jun 7, 2021

No, that's not necessary: mach_timebase_info will always give you a valid conversion factor, even if it happens to be 1/1.

@giampaolo
Copy link
Owner

(closing & reopening to trigger a CI test run)

@giampaolo giampaolo closed this Oct 2, 2021
@giampaolo giampaolo reopened this Oct 2, 2021
@giampaolo
Copy link
Owner

(closing & reopening to trigger a CI test run)

@giampaolo giampaolo closed this Oct 2, 2021
@giampaolo giampaolo reopened this Oct 2, 2021
@giampaolo
Copy link
Owner

@rmalouf looks like there are compilation issues:

2021-10-02T09:27:47.8665360Z   psutil/_psutil_osx.c:224:25: error: use of undeclared identifier 'info'; did you mean 'sinf'?
2021-10-02T09:27:47.8666080Z       mach_timebase_info(&info);
2021-10-02T09:27:47.8666510Z                           ^~~~
2021-10-02T09:27:47.8666870Z                           sinf
2021-10-02T09:27:47.8668040Z   /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/math.h:328:14: note: 'sinf' declared here
2021-10-02T09:27:47.8668860Z   extern float sinf(float);
2021-10-02T09:27:47.8669250Z                ^
2021-10-02T09:27:47.8670190Z   psutil/_psutil_osx.c:225:39: error: use of undeclared identifier 'info'
2021-10-02T09:27:47.8670860Z       total_user = pti.pti_total_user * info.numer;
2021-10-02T09:27:47.8671370Z                                         ^
2021-10-02T09:27:47.8672280Z   psutil/_psutil_osx.c:226:19: error: use of undeclared identifier 'info'
2021-10-02T09:27:47.8672930Z       total_user /= info.denom;
2021-10-02T09:27:47.8673330Z                     ^
2021-10-02T09:27:47.8674210Z   psutil/_psutil_osx.c:227:43: error: use of undeclared identifier 'info'
2021-10-02T09:27:47.8675150Z       total_system = pti.pti_total_system * info.numer;
2021-10-02T09:27:47.8675680Z                                             ^
2021-10-02T09:27:47.8676670Z   psutil/_psutil_osx.c:228:21: error: use of undeclared identifier 'info'
2021-10-02T09:27:47.8677690Z       total_system /= info.denom;
2021-10-02T09:27:47.8678100Z                       ^
2021-10-02T09:27:47.8678490Z   5 errors generated.
2021-10-02T09:27:47.8679390Z   error: command 'gcc' failed with exit status 1

@odormond
Copy link
Contributor

Hi @giampaolo!

The compilation error is fixed for me by applying the following patch:

diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c
index 67a52aa9..2effa0a0 100644
--- a/psutil/_psutil_osx.c
+++ b/psutil/_psutil_osx.c
@@ -214,6 +214,7 @@ static PyObject *
 psutil_proc_pidtaskinfo_oneshot(PyObject *self, PyObject *args) {
     pid_t pid;
     struct proc_taskinfo pti;
+    struct mach_timebase_info info;
     uint64_t total_user, total_system;
 
     if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid))

I have only tested it on Intel mac yet but will give it a shot tomorrow on M1.

@odormond
Copy link
Contributor

odormond commented Nov 3, 2021

I have only tested it on Intel mac yet but will give it a shot tomorrow on M1.

I finally managed to get my hands on an M1 and I can now confirm that this works.

I've tested it on intel Monterey with python 3.9.6 from brew and M1 BigSur with python 3.8.2 from Apple.

@odormond odormond mentioned this pull request Nov 4, 2021
@giampaolo
Copy link
Owner

Closing this out since it's been outdated / fixed by #2011.

@giampaolo giampaolo closed this Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[macOS / M1] cpu_percent() gives incorrect information
3 participants