psutil icon indicating copy to clipboard operation
psutil copied to clipboard

[macOS] CPU utilization contains zeroes

Open ArneTR opened this issue 1 year ago • 9 comments

Summary

  • OS: macOS 13.6.3 (22G436)
  • Architecture: ARM
  • Psutil version: 5.9.8
  • Python version: 3.10.13
  • Type: core

Description

I searched for utilization and could not find anything that matches this, so I hope it is not a dup.

When using psutil.cpu_percent(0.1) on my macOS while running a constant stress in parallel (stress-ng -c 1) I get a lot of zeros in between.

Screenshot 2024-02-05 at 10 38 33 AM

The same behaviour is also when I increase the stress to more cores. Say 8

>>> while True: psutil.cpu_percent(0.1)
...
100.0
100.0
100.0
100.0
0.0
0.0
0.0
0.0
0.0

The same behaviour does not happen on linux machines.

ArneTR avatar Feb 05 '24 09:02 ArneTR

What does htop or similar tools show? 0.1 is a small interval. Task manager tools usually use 1 sec instead. What happens if you use 1 sec?

giampaolo avatar Mar 15 '24 19:03 giampaolo

htop works fine. You can just let it run with 100ms resolution and the many consecutive "0" values do not occur.

For clarification: These values do occuer always no matter what kind of load we are running. So even if all cores are fully stressed zeros creep in, which is probably due to the used syscall.

We have actually created working code for one our tools. Please find it here: https://github.com/green-coding-solutions/green-metrics-tool/blob/main/metric_providers/cpu/utilization/mach/system/source.c

We are using host_processor_info and not host_statistics.

See also discussion here: https://github.com/green-coding-solutions/green-metrics-tool/issues/330

ArneTR avatar Mar 15 '24 21:03 ArneTR

@ribalba FYI

ArneTR avatar Mar 15 '24 21:03 ArneTR

I wrote a blog article about the problem here:

https://www.green-coding.io/blog/cpu-utilization-mac/

ribalba avatar Mar 15 '24 21:03 ribalba

Nice finding! It's kind of shocking that CPU metrics on macOS have been wrong for so long and nobody noticed.

giampaolo avatar Mar 15 '24 21:03 giampaolo

To be fair, it is unknown exactly in which version of macOS this isse happens and if it also is on all chip.s

@ribalba and I both have M1 machines. Untested on Intel and M2.

We are happy to provide a PR and some tests for macOS that test this feature (Stressing a core and seeing if zeros still exist in output), but I see that this repo currently has no macOS Github Actions runner defined.

If we provide a PR, does this repo have the possiblity to have a macOS Runner running that can test the CPU Utilization?

ArneTR avatar Mar 16 '24 07:03 ArneTR

We currently have CI runners for macOS x86-64 and arm (this one was added yesterday).

giampaolo avatar Mar 16 '24 07:03 giampaolo