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

[Linux] Differences between psutil.cpu_freq(True) and psutil.cpu_count() #1472

Closed
amanusk opened this issue Apr 2, 2019 · 2 comments
Closed
Assignees

Comments

@amanusk
Copy link
Collaborator

amanusk commented Apr 2, 2019

Rasbperry pi 3

  • Raspbian
  • latest master branch b23fb29

Bug description
Wonder about behaviour:

psutil.cpu_freq(percpu=True), on a Raspberry pi 3 now returns:
[scpufreq(current=600.0, min=600.0, max=1200.0)] i.e. only 1 CPU

When both psutil.cpu_count(logical=False) and psutil.cpu_count(logical=True) return 4: The expected behaviour.

Looking at the code, it seems this is because on the Pi,
there is only /sys/devices/system/cpu/cpufreq/policy0 while there are

/sys/devices/system/cpu/cpu0
/sys/devices/system/cpu/cpu1
/sys/devices/system/cpu/cpu2
/sys/devices/system/cpu/cpu3

And the former is checked first.
Should this be compared to psutil.cpu_count(logical=False), and the more accurate result taken?

Should I open a PR for this?

@giampaolo
Copy link
Owner

giampaolo commented Apr 2, 2019

Mh... annoying. Looking back at how this code is implemented, it looks like we may want to try something different. Instead of glob we could use the CPU numbers directly. Something like this:

for x in range(psutil.cpu_count(logical=True)):
    if os.path.exists("/sys/devices/system/cpu/cpufreq/policy%s" % x):
        ...
    elif os.path.exists("/sys/devices/system/cpu/cpu%s/cpufreq" % x):
        ...
    else:
        break

It seems to me this will be more robust.

@giampaolo giampaolo added the linux label Apr 2, 2019
@giampaolo
Copy link
Owner

giampaolo commented Apr 2, 2019

...or possibly this:

def cpu_freq():
    def get_path(num):
        for p in ("/sys/devices/system/cpu/cpufreq/policy%s" % num,
                  "/sys/devices/system/cpu/cpu%s/cpufreq" % num):
            if os.path.exists(p):
                return p

    for n in range(psutil.cpu_count(logical=True)):
        path = get_path(n)
        if not path:
            break
        ...

@amanusk amanusk mentioned this issue Apr 5, 2019
giampaolo added a commit that referenced this issue Apr 11, 2019
nlevitt added a commit to nlevitt/psutil that referenced this issue May 6, 2019
* origin/master:
  Fix giampaolo#1276: [AIX] use getargs to get process cmdline (giampaolo#1500) (patch by @wiggin15)
  Fix Process.ionice example using wrong keyword arg (giampaolo#1504)
  fix history syntax
  remove catching IOError; let the test fail and adjust it later
  Fix cpu freq (giampaolo#1496)
  pre release
  fix giampaolo#1493: [Linux] cpu_freq(): handle the case where /sys/devices/system/cpu/cpufreq/ exists but is empty.
  Revert "Fix cpu_freq (giampaolo#1493)" (giampaolo#1495)
  Fix cpu_freq (giampaolo#1493)
  Update cpu_freq to return 0 for max/min if not available (giampaolo#1487)
  give CREDITS to @agnewee for giampaolo#1491
  SunOS / net_if_addrs(): free() ifap struct on error (giampaolo#1491)
  fix giampaolo#1486: add wraps() decorator around wrap_exceptions
  refactor/move some utilities into _common.py
  update doc
  update HISTORY
  Implement getloadavg on Windows. Fixes giampaolo#604 and giampaolo#1484 (giampaolo#1485) (patch by Ammar Askar)
  give credits to @amanusk for giampaolo#1472
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants