diff options
| author | Danilo M. <danix@danix.xyz> | 2026-09-17 12:08:52 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-09-17 12:08:52 +0200 |
| commit | 3690d0ee9ceb5c0287cfe72eb4dcfd8f43324f0e (patch) | |
| tree | 7f7575c456fb3d1b4d75158f45ed0e339ec7cf22 /lib/data.lua | |
| parent | 64e8f20ce3a27dfc6cc2f37ae449dcb5eb91e7a9 (diff) | |
| download | conky-theme-udt-3690d0ee9ceb5c0287cfe72eb4dcfd8f43324f0e.tar.gz conky-theme-udt-3690d0ee9ceb5c0287cfe72eb4dcfd8f43324f0e.zip | |
feat: add per-core CPU times and named sensor reads
Diffstat (limited to 'lib/data.lua')
| -rw-r--r-- | lib/data.lua | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/data.lua b/lib/data.lua index 28dce1e..9bd5433 100644 --- a/lib/data.lua +++ b/lib/data.lua @@ -99,4 +99,57 @@ function M.new_cpu_counter() } end +-- Per-core jiffies from /proc/stat, in file order, so entry N is core N. +-- +-- Separate from cpu_times() rather than a flag on it: the aggregate is a +-- single pair and this is a list, and a function returning one or the other +-- depending on an argument is worse than two functions. +-- +-- The leading 'cpu ' aggregate is excluded by requiring a digit after 'cpu', +-- since counting it would draw an extra bar showing the average alongside the +-- real cores. +function M.per_cpu_times(stat) + local out = {} + if type(stat) ~= 'string' then return out end + for line in stat:gmatch('[^\n]+') do + local nums = line:match('^cpu%d+%s+(.+)$') + if nums then + local v = {} + for n in nums:gmatch('%d+') do v[#v + 1] = tonumber(n) end + -- Needs at least user..iowait to compute a busy fraction; a shorter line + -- is truncated or from a kernel that reports differently, and a partial + -- sum would look plausible while being wrong. + if #v >= 5 then + local total = 0 + for _, n in ipairs(v) do total = total + n end + out[#out + 1] = { total = total, idle = v[4] + v[5] } + end + end + end + return out +end + +-- A named hwmon sensor's value in whole degrees, or nil. +-- +-- Wraps hwmon_dir + slurp + millidegrees so a widget names the chip and the +-- file rather than building paths. nil, never 0: zero degrees is a plausible +-- reading and must not be indistinguishable from a missing sensor. +function M.sensor(chip, file) + if type(chip) ~= 'string' or type(file) ~= 'string' then return nil end + local dir = M.hwmon_dir(chip) + if not dir then return nil end + return M.millidegrees(M.slurp(dir .. '/' .. file)) +end + +-- A raw hwmon integer (fan RPM, power microwatts), or nil. Same lookup as +-- sensor() without the millidegree conversion. +function M.sensor_raw(chip, file) + if type(chip) ~= 'string' or type(file) ~= 'string' then return nil end + local dir = M.hwmon_dir(chip) + if not dir then return nil end + local s = M.slurp(dir .. '/' .. file) + if not s then return nil end + return tonumber(s:match('^%s*(-?%d+)')) +end + return M |
