From: copycat-killer Date: Thu, 3 Mar 2016 11:40:49 +0000 (+0100) Subject: some cpu usage improvements X-Git-Url: https://git.madduck.net/etc/awesome.git/commitdiff_plain/f63a7fd098e3826e397c16938968a1a840150abe?ds=inline;hp=42505f946ee6bac3f593c84e29a1bea923d07d02 some cpu usage improvements --- diff --git a/widgets/abase.lua b/widgets/abase.lua index 98f7818..fb20d76 100644 --- a/widgets/abase.lua +++ b/widgets/abase.lua @@ -28,8 +28,11 @@ local function worker(args) function abase.update() async.request(cmd, function(f) output = f - widget = abase.widget - settings() + if output ~= abase.prev then + widget = abase.widget + settings() + abase.prev = output + end end) end diff --git a/widgets/alsa.lua b/widgets/alsa.lua index 85d5311..bb54942 100644 --- a/widgets/alsa.lua +++ b/widgets/alsa.lua @@ -19,7 +19,7 @@ local setmetatable = setmetatable -- ALSA volume -- lain.widgets.alsa -local alsa = {} +local alsa = { last_level = "0", last_status = "off" } local function worker(args) local args = args or {} @@ -28,34 +28,20 @@ local function worker(args) alsa.cmd = args.cmd or "amixer" alsa.channel = args.channel or "Master" - - alsa.widget = wibox.widget.textbox('') + alsa.widget = wibox.widget.textbox('') function alsa.update() - local mixer = read_pipe(string.format("%s get %s", alsa.cmd, alsa.channel)) - - volume_now = {} + mixer = read_pipe(string.format("%s get %s", alsa.cmd, alsa.channel)) + l, s = string.match(mixer, "([%d]+)%%.*%[([%l]*)") - volume_now.level, volume_now.status = string.match(mixer, "([%d]+)%%.*%[([%l]*)") + if alsa.last_level ~= l or alsa.last_status ~= s then + volume_now = { level = l, status = s } + alsa.last_level = l + alsa.last_status = s - if volume_now.level == nil - then - volume_now.level = "0" - volume_now.status = "off" + widget = alsa.widget + settings() end - - if volume_now.status == "" - then - if volume_now.level == "0" - then - volume_now.status = "off" - else - volume_now.status = "on" - end - end - - widget = alsa.widget - settings() end timer_id = string.format("alsa-%s-%s", alsa.cmd, alsa.channel) diff --git a/widgets/alsabar.lua b/widgets/alsabar.lua index c7498d2..7e8cd5a 100644 --- a/widgets/alsabar.lua +++ b/widgets/alsabar.lua @@ -127,28 +127,26 @@ local function worker(args) -- Capture mixer control state: [5%] ... ... [on] local volu, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)") - if volu == nil then - volu = 0 - mute = "off" - end - - alsabar._current_level = tonumber(volu) - alsabar.bar:set_value(alsabar._current_level / 100) - if not mute and tonumber(volu) == 0 or mute == "off" + if tonumber(volu) ~= alsabar._current_level or string.match(mute, "on") ~= alsabar._muted then - alsabar._muted = true - alsabar.tooltip:set_text (" [Muted] ") - alsabar.bar:set_color(alsabar.colors.mute) - else - alsabar._muted = false - alsabar.tooltip:set_text(string.format(" %s:%s ", alsabar.channel, volu)) - alsabar.bar:set_color(alsabar.colors.unmute) + alsabar._current_level = tonumber(volu) + alsabar.bar:set_value(alsabar._current_level / 100) + if not mute and tonumber(volu) == 0 or mute == "off" + then + alsabar._muted = true + alsabar.tooltip:set_text (" [Muted] ") + alsabar.bar:set_color(alsabar.colors.mute) + else + alsabar._muted = false + alsabar.tooltip:set_text(string.format(" %s:%s ", alsabar.channel, volu)) + alsabar.bar:set_color(alsabar.colors.unmute) + end + + volume_now = {} + volume_now.level = tonumber(volu) + volume_now.status = mute + settings() end - - volume_now = {} - volume_now.level = tonumber(volu) - volume_now.status = mute - settings() end alsabar.bar:buttons (awful.util.table.join ( diff --git a/widgets/base.lua b/widgets/base.lua index 2f377f7..272163c 100644 --- a/widgets/base.lua +++ b/widgets/base.lua @@ -26,9 +26,12 @@ local function worker(args) base.widget = wibox.widget.textbox('') function base.update() - output = read_pipe(cmd) - widget = base.widget - settings() + if output ~= abase.prev then + output = read_pipe(cmd) + widget = base.widget + settings() + abase.prev = output + end end newtimer(cmd, timeout, base.update) diff --git a/widgets/borderbox.lua b/widgets/borderbox.lua index cce8517..fb992de 100644 --- a/widgets/borderbox.lua +++ b/widgets/borderbox.lua @@ -7,8 +7,7 @@ --]] -local wibox = require("awful.wibox") - +local wibox = require("awful.wibox") local setmetatable = setmetatable -- Creates a thin wibox at a position relative to another wibox diff --git a/widgets/cpu.lua b/widgets/cpu.lua index ec84101..6019f32 100644 --- a/widgets/cpu.lua +++ b/widgets/cpu.lua @@ -21,10 +21,7 @@ local setmetatable = setmetatable -- CPU usage -- lain.widgets.cpu -local cpu = { - last_total = 0, - last_active = 0 -} +local cpu = { last_total = 0, last_active = 0 } local function worker(args) local args = args or {} @@ -53,20 +50,22 @@ local function worker(args) at = at + 1 end local active = total - idle + + if cpu.last_active ~= active or cpu.last_total ~= total then + -- Read current data and calculate relative values. + local dactive = active - cpu.last_active + local dtotal = total - cpu.last_total - -- Read current data and calculate relative values. - local dactive = active - cpu.last_active - local dtotal = total - cpu.last_total + cpu_now = {} + cpu_now.usage = tostring(math.ceil((dactive / dtotal) * 100)) - cpu_now = {} - cpu_now.usage = tostring(math.ceil((dactive / dtotal) * 100)) + widget = cpu.widget + settings() - widget = cpu.widget - settings() - - -- Save current data for the next run. - cpu.last_active = active - cpu.last_total = total + -- Save current data for the next run. + cpu.last_active = active + cpu.last_total = total + end end newtimer("cpu", timeout, update) diff --git a/widgets/net.lua b/widgets/net.lua index af823a8..a4b1097 100644 --- a/widgets/net.lua +++ b/widgets/net.lua @@ -8,8 +8,6 @@ --]] local helpers = require("lain.helpers") - -local notify_fg = require("beautiful").fg_focus local naughty = require("naughty") local wibox = require("wibox") @@ -28,11 +26,8 @@ local function worker(args) function net.get_device() local ws = helpers.read_pipe("ip link show | cut -d' ' -f2,9") ws = ws:match("%w+: UP") or ws:match("ppp%w+: UNKNOWN") - if ws ~= nil then - return ws:match("(%w+):") - else - return "network off" - end + if ws then return ws:match("(%w+):") + else return "network off" end end local args = args or {} @@ -55,38 +50,33 @@ local function worker(args) iface = net.get_device() end - net_now.carrier = helpers.first_line('/sys/class/net/' .. iface .. - '/carrier') or "0" - net_now.state = helpers.first_line('/sys/class/net/' .. iface .. - '/operstate') or "down" - local now_t = helpers.first_line('/sys/class/net/' .. iface .. - '/statistics/tx_bytes') or 0 - local now_r = helpers.first_line('/sys/class/net/' .. iface .. - '/statistics/rx_bytes') or 0 + net_now.carrier = helpers.first_line(string.format('/sys/class/net/%s/carrier', iface)) or '0' + net_now.state = helpers.first_line(string.format('/sys/class/net/%s/operstate', iface)) or 'down' - net_now.sent = (now_t - net.last_t) / timeout / units - net_now.sent = string.gsub(string.format('%.1f', net_now.sent), ",", ".") + local now_t = helpers.first_line(string.format('/sys/class/net/%s/statistics/tx_bytes', iface)) or 0 + local now_r = helpers.first_line(string.format('/sys/class/net/%s/statistics/rx_bytes', iface)) or 0 - net_now.received = (now_r - net.last_r) / timeout / units - net_now.received = string.gsub(string.format('%.1f', net_now.received), ",", ".") + if now_t ~= net.last_t or now_r ~= net.last_r then + net_now.sent = (now_t - net.last_t) / timeout / units + net_now.sent = string.gsub(string.format('%.1f', net_now.sent), ',', '.') + net_now.received = (now_r - net.last_r) / timeout / units + net_now.received = string.gsub(string.format('%.1f', net_now.received), ',', '.') - widget = net.widget - settings() + widget = net.widget + settings() - net.last_t = now_t - net.last_r = now_r + net.last_t = now_t + net.last_r = now_r + end - if net_now.carrier ~= "1" and notify == "on" + if not string.match(net_now.carrier, "1") and notify == "on" then if helpers.get_map(iface) then naughty.notify({ title = iface, text = "no carrier", - timeout = 7, - position = "top_left", icon = helpers.icons_dir .. "no_net.png", - fg = notify_fg or "#FFFFFF", screen = screen }) helpers.set_map(iface, false) diff --git a/widgets/temp.lua b/widgets/temp.lua index 67c9456..a857e71 100644 --- a/widgets/temp.lua +++ b/widgets/temp.lua @@ -42,6 +42,7 @@ local function worker(args) end newtimer("coretemp", timeout, update) + return temp.widget end