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

Timebase #2011

Merged
merged 4 commits into from
Nov 10, 2021
Merged

Timebase #2011

merged 4 commits into from
Nov 10, 2021

Conversation

odormond
Copy link
Contributor

@odormond odormond commented Nov 4, 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.

Based on @rmalouf PR #1958.

@giampaolo
Copy link
Owner

the value of mach_timebase_info should be cached somewhere

Makes sense. You can do that in psutil_setup() function, which is called on module init:

psutil_setup(void) {

You should define the new global var as a C extern defined in _psutil_common.h. grep for PSUTIL_SYSTEM_INFO to see how it's done.

(float)pti.pti_total_user / 1000000000.0, // (float) cpu user time
(float)pti.pti_total_system / 1000000000.0, // (float) cpu sys time
(float)total_user / 1000000000.0, // (float) cpu user time
(float)total_system / 1000000000.0, // (float) cpu sys time
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you did the new conversion above, is the / 1000000000.0 division still necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The mach timebase conversion ratio produces nanoseconds.

@odormond
Copy link
Contributor Author

odormond commented Nov 4, 2021

the value of mach_timebase_info should be cached somewhere

Makes sense. You can do that in psutil_setup() function, which is called on module init:

psutil_setup(void) {

You should define the new global var as a C extern defined in _psutil_common.h. grep for PSUTIL_SYSTEM_INFO to see how it's done.

Sure. I just pushed another commit doing just that. If it's good for you I can squash it to replace the previous two.

About the division by a billion, the mach_timebase_info give the conversion ratio from ticks to nanoseconds so the result still needs to be converted to seconds.

rmalouf and others added 3 commits November 4, 2021 12:06
#include <mach/mach_time.h>

struct mach_timebase_info MACH_TIMEBASE_INFO;
#endif
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to be consistent with PSUTIL_SYSTEM_INFO, PSUTIL_CRITICAL_SECTION and PSUTIL_DEBUG I suggest to:

  • call this PSUTIL_MACH_TIMEBASE_INFO
  • define it in _psutil_common.h file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to be consistent with PSUTIL_SYSTEM_INFO, PSUTIL_CRITICAL_SECTION and PSUTIL_DEBUG I suggest to:

* call this `PSUTIL_MACH_TIMEBASE_INFO`

That makes more sense indeed. Sorry.

* define it in `_psutil_common.h` file

I don't understand this. The header file does have the extern definition and storage can only be in the .c file. What am I missing?

psutil/_psutil_common.c Show resolved Hide resolved
psutil/_psutil_osx.c Outdated Show resolved Hide resolved
@giampaolo giampaolo merged commit 324b297 into giampaolo:master Nov 10, 2021
giampaolo added a commit that referenced this pull request Nov 10, 2021
Signed-off-by: Giampaolo Rodola <[email protected]>
@giampaolo
Copy link
Owner

Merged. Thanks!

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