From c4606372b3ddc4ac2bf16b67eadfc61598cd27aa Mon Sep 17 00:00:00 2001 From: Nuno Silva Date: Sun, 6 Jun 2021 02:24:32 +0100 Subject: net-speed-widget: fix speed calculation need to divide by time --- net-speed-widget/net-speed.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/net-speed-widget/net-speed.lua b/net-speed-widget/net-speed.lua index 2fb1aa1..8ba708d 100644 --- a/net-speed-widget/net-speed.lua +++ b/net-speed-widget/net-speed.lua @@ -104,8 +104,8 @@ local function worker(user_args) if i%2 == 0 then cur_tx = cur_tx + cur_vals[i] end end - local speed_rx = cur_rx - prev_rx - local speed_tx = cur_tx - prev_tx + local speed_rx = (cur_rx - prev_rx) / timeout + local speed_tx = (cur_tx - prev_tx) / timeout widget:set_rx_text(convert_to_h(speed_rx)) widget:set_tx_text(convert_to_h(speed_tx)) -- cgit v1.2.3 From e97d0df3c9d80a5fe77d0e61e4116d157d97a45c Mon Sep 17 00:00:00 2001 From: Nuno Silva Date: Sun, 6 Jun 2021 02:26:13 +0100 Subject: net-speed-widget: fix race condition when using multiple monitors/widgets --- net-speed-widget/net-speed.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/net-speed-widget/net-speed.lua b/net-speed-widget/net-speed.lua index 8ba708d..6dd3b05 100644 --- a/net-speed-widget/net-speed.lua +++ b/net-speed-widget/net-speed.lua @@ -17,9 +17,6 @@ local ICONS_DIR = WIDGET_DIR .. 'icons/' local net_speed_widget = {} -local prev_rx = 0 -local prev_tx = 0 - local function convert_to_h(bytes) local speed local dim @@ -92,6 +89,11 @@ local function worker(user_args) end } + -- make sure these are not shared across different worker/widgets (e.g. two monitors) + -- otherwise the speed will be randomly split among the worker in each monitor + local prev_rx = 0 + local prev_tx = 0 + local update_widget = function(widget, stdout) local cur_vals = split(stdout, '\r\n') @@ -99,9 +101,9 @@ local function worker(user_args) local cur_rx = 0 local cur_tx = 0 - for i, _ in ipairs(cur_vals) do - if i%2 == 1 then cur_rx = cur_rx + cur_vals[i] end - if i%2 == 0 then cur_tx = cur_tx + cur_vals[i] end + for i, v in ipairs(cur_vals) do + if i%2 == 1 then cur_rx = cur_rx + v end + if i%2 == 0 then cur_tx = cur_tx + v end end local speed_rx = (cur_rx - prev_rx) / timeout -- cgit v1.2.3