diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-12-30 00:37:39 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-12-30 00:37:39 +0100 |
commit | f18438d135c12f7eb186f49622e0f6683c37f7f5 (patch) | |
tree | 48af0881c4015305717645d870242ba8ea161174 | |
parent | 1188d8e72bd8dd520ad0ea44397bde2a9f1bcd4e (diff) | |
download | psutil-f18438d135c12f7eb186f49622e0f6683c37f7f5.tar.gz |
Linux, cpu_freq(): diminish os.stat() calls a bit
Micro optimization in reference to #1852 and #1851.
Use glob.glob(), which internally relies on os.scandir()
in order to list /sys/devices/system/cpu/cpufreq files.
In doing so, we avoid os.path.exists() for each CPU, which
internally uses os.stat().
Signed-off-by: Giampaolo Rodola <g.rodola@gmail.com>
-rw-r--r-- | psutil/_pslinux.py | 17 |
1 files changed, 5 insertions, 12 deletions
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 39155745..ff8d9247 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -726,19 +726,12 @@ if os.path.exists("/sys/devices/system/cpu/cpufreq/policy0") or \ Contrarily to other OSes, Linux updates these values in real-time. """ - 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 - + paths = sorted( + glob.glob("/sys/devices/system/cpu/cpufreq/policy[0-9]*") or + glob.glob("/sys/devices/system/cpu/cpu[0-9]*/cpufreq")) ret = [] - for n in range(cpu_count_logical()): - path = get_path(n) - if not path: - continue - - pjoin = os.path.join + pjoin = os.path.join + for path in paths: curr = cat(pjoin(path, "scaling_cur_freq"), fallback=None) if curr is None: # Likely an old RedHat, see: |