From 14062c2cbd534b4c7aefd8bee82058eba5e2ae8d Mon Sep 17 00:00:00 2001 From: petrucci4prez Date: Mon, 17 Jul 2017 01:34:03 -0400 Subject: [PATCH] optimize Network.lua --- module/Network.lua | 70 +++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 42 deletions(-) diff --git a/module/Network.lua b/module/Network.lua index 65748b4..bb13e98 100644 --- a/module/Network.lua +++ b/module/Network.lua @@ -10,10 +10,6 @@ local __string_gmatch = string.gmatch local _PLOT_SEC_BREAK_ = 20 local _PLOT_HEIGHT_ = 56 -local SYSFS_NET = '/sys/class/net/' -local STATS_RX = '/statistics/rx_bytes' -local STATS_TX = '/statistics/tx_bytes' - local network_label_function = function(bytes) local new_unit = util.get_unit(bytes) @@ -77,55 +73,45 @@ local upload = { } } -local interfaces = {} - -local add_interface = function(iface) - local rx_path = SYSFS_NET..iface..STATS_RX - local tx_path = SYSFS_NET..iface..STATS_TX - - interfaces[iface] = { - rx_path = rx_path, - tx_path = tx_path, - rx_cumulative_bytes = 0, - tx_cumulative_bytes = 0, - prev_rx_cumulative_bytes = util.read_file(rx_path, nil, '*n'), - prev_tx_cumulative_bytes = util.read_file(tx_path, nil, '*n'), - } -end - -for iface in io.popen('ls -1 '..SYSFS_NET):lines() do - add_interface(iface) -end +local interface_counters_tbl = {} local update = function(cr, update_frequency) local dspeed, uspeed = 0, 0 - local glob = util.execute_cmd('ip route show') - local rx_bps, tx_bps + local rx_delta, tx_delta - for iface in __string_gmatch(glob, 'default via %d+%.%d+%.%d+%.%d+ dev (%w+) ') do - local current_iface = interfaces[iface] + -- iterate through the route file and filter on interfaces that are gateways (flag = 0003) + local iterator = __string_gmatch(util.read_file('/proc/net/route'), + '(%w+)%s+%w+%s+%w+%s+0003%s+') - if not current_iface then - add_interface(iface) - current_iface = interfaces[iface] + for interface in iterator do + local interface_counters = interface_counters_tbl[interface] + + if not interface_counters then + local rx_path = '/sys/class/net/'..interface..'/statistics/rx_bytes' + local tx_path = '/sys/class/net/'..interface..'/statistics/tx_bytes' + + interface_counters = { + rx_path = rx_path, + tx_path = tx_path, + prev_rx_byte_cnt = util.read_file(rx_path, nil, '*n'), + prev_tx_byte_cnt = util.read_file(tx_path, nil, '*n'), + } + interface_counters_tbl[interface] = interface_counters end - local new_rx_cumulative_bytes = util.read_file(current_iface.rx_path, nil, '*n') - local new_tx_cumulative_bytes = util.read_file(current_iface.tx_path, nil, '*n') + local rx_byte_cnt = util.read_file(interface_counters.rx_path, nil, '*n') + local tx_byte_cnt = util.read_file(interface_counters.tx_path, nil, '*n') - rx_bps = (new_rx_cumulative_bytes - current_iface.prev_rx_cumulative_bytes) * update_frequency - tx_bps = (new_tx_cumulative_bytes - current_iface.prev_tx_cumulative_bytes) * update_frequency + rx_delta = rx_byte_cnt - interface_counters.prev_rx_byte_cnt + tx_delta = tx_byte_cnt - interface_counters.prev_tx_byte_cnt - current_iface.prev_rx_cumulative_bytes = new_rx_cumulative_bytes - current_iface.prev_tx_cumulative_bytes = new_tx_cumulative_bytes + interface_counters.prev_rx_byte_cnt = rx_byte_cnt + interface_counters.prev_tx_byte_cnt = tx_byte_cnt - --mask overflow - if rx_bps < 0 then rx_bps = 0 end - if tx_bps < 0 then tx_bps = 0 end - - dspeed = dspeed + rx_bps - uspeed = uspeed + tx_bps + -- mask overflow + if rx_delta > 0 then dspeed = dspeed + rx_delta * update_frequency end + if tx_delta > 0 then uspeed = uspeed + tx_delta * update_frequency end end local dspeed_unit = util.get_unit(dspeed)