[hwmontemp] Bring back sysfs path cache

Cache sysfs path resolved from given name index in local table. This avoids
spawning a `grep` process over and over again for each invocation.

Co-authored-by: Nguyễn Gia Phong <mcsinyx@disroot.org>
This commit is contained in:
Alexander Koch 2020-08-26 13:53:15 +02:00 committed by Nguyễn Gia Phong
parent a2f83bf9d6
commit 5e0cb0c84a
1 changed files with 25 additions and 12 deletions

View File

@ -1,5 +1,5 @@
-- widget type providing name-indexed temperatures from /sys/class/hwmon -- widget type providing name-indexed temperatures from /sys/class/hwmon
-- Copyright (C) 2019 Alexander Koch <lynix47@gmail.com> -- Copyright (C) 2019, 2020 Alexander Koch <lynix47@gmail.com>
-- Copyright (C) 2019 Nguyễn Gia Phong <vn.mcsinyx@gmail.com> -- Copyright (C) 2019 Nguyễn Gia Phong <vn.mcsinyx@gmail.com>
-- --
-- This file is part of Vicious. -- This file is part of Vicious.
@ -25,6 +25,14 @@ local io = { open = io.open }
local helpers = require"vicious.helpers" local helpers = require"vicious.helpers"
local spawn = require"vicious.spawn" local spawn = require"vicious.spawn"
local pathcache = {}
local function read_sensor(path, callback)
local f = io.open(path, "r")
callback{ tonumber(f:read"*line") / 1000 }
f:close()
end
-- hwmontemp: provides name-indexed temps from /sys/class/hwmon -- hwmontemp: provides name-indexed temps from /sys/class/hwmon
-- vicious.widgets.hwmontemp -- vicious.widgets.hwmontemp
return helpers.setasyncall{ return helpers.setasyncall{
@ -32,6 +40,7 @@ return helpers.setasyncall{
if type(warg) ~= "table" or type(warg[1]) ~= "string" then if type(warg) ~= "table" or type(warg[1]) ~= "string" then
return callback{} return callback{}
end end
local input = warg[2] local input = warg[2]
if type(input) == "number" then if type(input) == "number" then
input = ("temp%d_input"):format(input) input = ("temp%d_input"):format(input)
@ -39,16 +48,20 @@ return helpers.setasyncall{
input = "temp1_input" input = "temp1_input"
end end
spawn.easy_async_with_shell( local sensor = warg[1]
"grep " .. warg[1] .. " -wl /sys/class/hwmon/*/name", if pathcache[sensor] then
function (stdout, stderr, exitreason, exitcode) read_sensor(pathcache[sensor] .. input, callback)
if exitreason == "exit" and exitcode == 0 then else
local f = io.open(stdout:gsub("name%s+", input), "r") spawn.easy_async_with_shell(
callback{ tonumber(f:read"*line") / 1000 } "grep " .. sensor .. " -wl /sys/class/hwmon/*/name",
f:close() function (stdout, stderr, exitreason, exitcode)
else if exitreason == "exit" and exitcode == 0 then
callback{} pathcache[sensor] = stdout:gsub("name%s+", "")
end read_sensor(pathcache[sensor] .. input, callback)
end) else
callback{}
end
end)
end
end } end }
-- vim: ts=4:sw=4:expandtab -- vim: ts=4:sw=4:expandtab