]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lscpu: read MHZ from /sys/.../cpufreq/scaling_cur_freq
authorKarel Zak <kzak@redhat.com>
Tue, 25 May 2021 11:23:39 +0000 (13:23 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 25 May 2021 11:30:13 +0000 (13:30 +0200)
This is more portable and provides more stable results than
/proc/cpuinfo.

Fixes: https://github.com/karelzak/util-linux/pull/1317
Co-Author: Thomas Weißschuh
Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/lscpu-topology.c

index 328ee7cbc2323a8b1fc872d77ec1aa534b59cb51..987ebff42a399f287e05b78f6ac29ed15608ebd3 100644 (file)
@@ -546,6 +546,16 @@ static int read_mhz(struct lscpu_cxt *cxt, struct lscpu_cpu *cpu)
        if (ul_path_readf_s32(sys, &mhz, "cpu%d/cpufreq/cpuinfo_min_freq", num) == 0)
                cpu->mhz_min_freq = (float) mhz / 1000;
 
+       /* The default current-frequency value comes is from /proc/cpuinfo (if
+        * available).  This /proc value is usually based on MSR registers
+        * (APERF/APERF) and it changes pretty often. It seems better to read
+        * frequency from cpufreq subsystem that provides the current frequency
+        * for the current policy. There is also cpuinfo_cur_freq in sysfs, but
+        * it's not always available.
+        */
+       if (ul_path_readf_s32(sys, &mhz, "cpu%d/cpufreq/scaling_cur_freq", num) == 0)
+               cpu->mhz_cur_freq = (float) mhz / 1000;
+
        if (cpu->type && (cpu->mhz_min_freq || cpu->mhz_max_freq))
                cpu->type->has_freq = 1;