From: Karel Zak Date: Tue, 25 May 2021 11:23:39 +0000 (+0200) Subject: lscpu: read MHZ from /sys/.../cpufreq/scaling_cur_freq X-Git-Tag: v2.38-rc1~486^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f2d08d4ddc9bc601107df28dd8e4598cf2e0b420;p=thirdparty%2Futil-linux.git lscpu: read MHZ from /sys/.../cpufreq/scaling_cur_freq 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 --- diff --git a/sys-utils/lscpu-topology.c b/sys-utils/lscpu-topology.c index 328ee7cbc2..987ebff42a 100644 --- a/sys-utils/lscpu-topology.c +++ b/sys-utils/lscpu-topology.c @@ -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;