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