aboutsummaryrefslogtreecommitdiffstats
path: root/lib/data.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/data.lua')
-rw-r--r--lib/data.lua28
1 files changed, 21 insertions, 7 deletions
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.