Skip to content

linux: fix cpu_freq() when one cpufreq policy affects multiple CPUs #2562

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,12 +698,27 @@ def cpu_freq():
"/sys/devices/system/cpu/cpufreq/policy[0-9]*"
) or glob.glob("/sys/devices/system/cpu/cpu[0-9]*/cpufreq")
paths.sort(key=lambda x: int(re.search(r"[0-9]+", x).group()))

cpu_to_policy = {}
for path in paths:
try:
with open(os.path.join(path, "affected_cpus")) as f:
cpus = [int(x) for x in f.read().strip().split()]
for cpu in cpus:
cpu_to_policy[cpu] = path
except (OSError, ValueError):
continue

ret = []
pjoin = os.path.join
for i, path in enumerate(paths):
if len(paths) == len(cpuinfo_freqs):
# take cached value from cpuinfo if available, see:
# https://github.com/giampaolo/psutil/issues/1851
num_cpus = cpu_count_logical()
for i in range(num_cpus):
path = cpu_to_policy.get(i)
if not path:
ret.append(_common.scpufreq(0.0, 0.0, 0.0))
continue

if cpuinfo_freqs and i < len(cpuinfo_freqs):
curr = cpuinfo_freqs[i] * 1000
else:
curr = bcat(pjoin(path, "scaling_cur_freq"), fallback=None)
Expand Down
Loading