From 5e0cb0c84a204c1b63b77a079d81bda2ad74ee25 Mon Sep 17 00:00:00 2001 From: Alexander Koch Date: Wed, 26 Aug 2020 13:53:15 +0200 Subject: [PATCH] [hwmontemp] Bring back sysfs path cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- widgets/hwmontemp_linux.lua | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/widgets/hwmontemp_linux.lua b/widgets/hwmontemp_linux.lua index 7631dc9..fe3e50d 100644 --- a/widgets/hwmontemp_linux.lua +++ b/widgets/hwmontemp_linux.lua @@ -1,5 +1,5 @@ -- widget type providing name-indexed temperatures from /sys/class/hwmon --- Copyright (C) 2019 Alexander Koch +-- Copyright (C) 2019, 2020 Alexander Koch -- Copyright (C) 2019 Nguyễn Gia Phong -- -- This file is part of Vicious. @@ -25,6 +25,14 @@ local io = { open = io.open } local helpers = require"vicious.helpers" 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 -- vicious.widgets.hwmontemp return helpers.setasyncall{ @@ -32,6 +40,7 @@ return helpers.setasyncall{ if type(warg) ~= "table" or type(warg[1]) ~= "string" then return callback{} end + local input = warg[2] if type(input) == "number" then input = ("temp%d_input"):format(input) @@ -39,16 +48,20 @@ return helpers.setasyncall{ input = "temp1_input" end - spawn.easy_async_with_shell( - "grep " .. warg[1] .. " -wl /sys/class/hwmon/*/name", - function (stdout, stderr, exitreason, exitcode) - if exitreason == "exit" and exitcode == 0 then - local f = io.open(stdout:gsub("name%s+", input), "r") - callback{ tonumber(f:read"*line") / 1000 } - f:close() - else - callback{} - end - end) + local sensor = warg[1] + if pathcache[sensor] then + read_sensor(pathcache[sensor] .. input, callback) + else + spawn.easy_async_with_shell( + "grep " .. sensor .. " -wl /sys/class/hwmon/*/name", + function (stdout, stderr, exitreason, exitcode) + if exitreason == "exit" and exitcode == 0 then + pathcache[sensor] = stdout:gsub("name%s+", "") + read_sensor(pathcache[sensor] .. input, callback) + else + callback{} + end + end) + end end } -- vim: ts=4:sw=4:expandtab