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

[macOS / M1] cpu_percent() gives incorrect information #1956

Closed
rmalouf opened this issue Jun 3, 2021 · 3 comments · Fixed by #2011
Closed

[macOS / M1] cpu_percent() gives incorrect information #1956

rmalouf opened this issue Jun 3, 2021 · 3 comments · Fixed by #2011

Comments

@rmalouf
Copy link

rmalouf commented Jun 3, 2021

Summary

  • OS: macOS 11.4
  • Architecture: ARM
  • Psutil version: 5.8.0
  • Python version: 3.9.4
  • Type: core

Description

Process.cpu_percent() always reports very low cpu usage, in contradiction to top and Activity monitor. This test program:

from concurrent.futures import ThreadPoolExecutor
import psutil
import time
from math import log

def work(n):
    m = 0
    for i in range(1, n**2):
        m += log(i)
    return True
    
executor = ThreadPoolExecutor(max_workers=2)
task = executor.submit(work, 10000)

proc = psutil.Process()
while not task.done():
    print(proc.cpu_percent(interval=1))

consistently reports 2.4% usage on the Mac (vs. ~100% usage, as expected, on linux with the same version of psutil).

@rmalouf rmalouf added the bug label Jun 3, 2021
@github-actions github-actions bot added the macos label Jun 3, 2021
@rmalouf
Copy link
Author

rmalouf commented Jun 5, 2021

Some updates:

  1. I've confirmed that this gives the correct result on an intel Mac, with the same versions of everything
  2. The problem is really in cpu_times() -- cpu_percent() is just using the value it gets from there
  3. The value returned by cpu_times() is consistently off by a factor of about 0.024. This makes me strongly suspect that the times reported by the kernel via proc_taskinfo aren't in the same units on an M1 vs. intel.

@giampaolo
Copy link
Owner

PR in #1958

@odormond odormond mentioned this issue Nov 4, 2021
@odormond
Copy link
Contributor

odormond commented Nov 4, 2021

Updated PR in #2011

giampaolo pushed a commit that referenced this issue Nov 10, 2021
giampaolo added a commit that referenced this issue Nov 10, 2021
Signed-off-by: Giampaolo Rodola <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants