From 32e10b1d6962afd04a209f300ebb8ab9ee29f070 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Thu, 17 Sep 2026 12:40:10 +0200 Subject: fix: stop the widgets from stalling or raising the draw Memoize hwmon_dir by chip name so sensor reads no longer popen grep on every frame, cache misses included. Floor fractional byte counts in card.human so it cannot raise. Drop the GPU package-temperature row, which duplicated the headline. Set umask before mkdir in both samplers so a fresh cache directory is not world-readable. Make render.lua exit non-zero on a draw error and tolerate an unset HOME. --- lib/card.lua | 2 +- lib/data.lua | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/card.lua b/lib/card.lua index 48a7f2e..185703c 100644 --- a/lib/card.lua +++ b/lib/card.lua @@ -213,7 +213,7 @@ function M.human(bytes) local units = { 'B', 'K', 'M', 'G', 'T', 'P' } local i = 1 while n >= 1024 and i < #units do n = n / 1024; i = i + 1 end - if i == 1 then return string.format('%d%s', n, units[i]) end + if i == 1 then return string.format('%d%s', math.floor(n), units[i]) end if n >= 100 then return string.format('%.0f%s', n, units[i]) end return string.format('%.1f%s', n, units[i]) end diff --git a/lib/data.lua b/lib/data.lua index acf7a0f..2047ef8 100644 --- a/lib/data.lua +++ b/lib/data.lua @@ -62,14 +62,28 @@ end -- Globbing by name, never by a fixed hwmon index: indices drift across kernel -- and hardware reorders, and a stale index silently reports a different chip. -- Carried over from the previous conky config, where this was the hard-won bit. +-- +-- The result is memoized per chip name, hits and misses alike. A chip's hwmon +-- directory is stable for the process's life, and the draw hook calls this +-- several times per frame: without the cache every sensor read popens grep, +-- which is exactly the subprocess-per-draw the samplers exist to avoid. A miss +-- is cached too, so an absent chip is not re-globbed on every frame. +local hwmon_cache = {} + function M.hwmon_dir(name) - local p = io.popen('grep -lx ' .. ("%q"):format(name) - .. ' /sys/class/hwmon/hwmon*/name 2>/dev/null') - if not p then return nil end - local hit = p:read('*l') - p:close() - if not hit then return nil end - return hit:match('^(.*)/name$') + if type(name) ~= 'string' then return nil end + if hwmon_cache[name] == nil then + local p = io.popen('grep -lx ' .. ("%q"):format(name) + .. ' /sys/class/hwmon/hwmon*/name 2>/dev/null') + local dir = false + if p then + local hit = p:read('*l') + p:close() + if hit then dir = hit:match('^(.*)/name$') end + end + hwmon_cache[name] = dir + end + return hwmon_cache[name] or nil end -- A stateful CPU-load counter. -- cgit v1.2.3