From 8da174c61e92bc63c06978f1c9ac2e4f631305a9 Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Sat, 30 Sep 2023 12:05:45 -0600 Subject: [PATCH] fixed bug 1630: wrong CPU values when no ${top *} var was used https://github.com/brndnmtthws/conky/issues/1630 My per-core CPU values were wrong because Conky was ignoring half the data in /proc/stat. In Linux, Conky supports short and long /proc/stat formats. The type is auto-detected by determine_longstat_file(). However, this function is only called if the user's config uses a ${top *} var. If the user doesn't use this widget, the CPU values are calculated wrong because 4 of the 8 stat values are ignored. In my case, I discovered it because conky showed one of my CPU cores pegged at 100% all the time no matter what. But htop + btop showed under 5% for that core. So I did some digging, and found it was happening because that core was stuck in iowait due to a hung NFS mount. Conky normally handles iowait correctly, but only if it detected the long stat format. I didn't have any ${top *} vars, so conky wasn't trying to detect the stat format, defaulting to short stat which ignores half the data, and this made it think the CPU was always at 100%. This fix detects the stat format the first time it's accessed, regardless of which widgets the user config contains. --- src/linux.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/linux.cc b/src/linux.cc index 4e6609d05c..1096d728d3 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -902,8 +902,12 @@ void determine_longstat_file(void) { #define MAX_PROCSTAT_LINELEN 255 FILE *stat_fp; static int reported = 0; + static bool first = true; char buf[MAX_PROCSTAT_LINELEN + 1]; + if (! first) return; + first = false; + if (!(stat_fp = open_file("/proc/stat", &reported))) return; while (!feof(stat_fp) && fgets(buf, MAX_PROCSTAT_LINELEN, stat_fp) != nullptr) { @@ -1000,6 +1004,7 @@ int update_stat(void) { } if (!stat_template) { + determine_longstat_file(); stat_template = KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGSTAT : TMPL_SHORTSTAT; }