REF modify state in place

This commit is contained in:
Nathan Dwarshuis 2022-07-17 19:30:48 -04:00
parent 8b3244c727
commit 73fd89a6f5
3 changed files with 121 additions and 123 deletions

View File

@ -10,13 +10,71 @@ return function(update_freq, config, common, width, point)
local __string_match = string.match local __string_match = string.match
local __tonumber = tonumber local __tonumber = tonumber
-----------------------------------------------------------------------------
-- nvidia state
-- vars to process the nv settings glob
--
-- glob will be of the form:
-- <used_mem>
-- <total_mem>
-- <temp>
-- <gpu_freq>,<mem_freq>
-- graphics=<gpu_util>, memory=<mem_util>, video=<vid_util>, PCIe=<pci_util>
local NV_QUERY = 'nvidia-settings -t'..
' -q UsedDedicatedGPUmemory'..
' -q TotalDedicatedGPUmemory'..
' -q ThermalSensorReading'..
' -q [gpu:0]/GPUCurrentClockFreqs'..
' -q [gpu:0]/GPUutilization'
local NV_REGEX = '(%d+)\n'..
'(%d+)\n'..
'(%d+)\n'..
'(%d+),(%d+)\n'..
'graphics=(%d+), memory=%d+, video=(%d+), PCIe=%d+\n'
local GPU_BUS_CTRL = '/sys/bus/pci/devices/0000:01:00.0/power/control'
local mod_state = {
error = false,
used_memory = 0,
total_memory = 0,
temp_reading = 0,
gpu_frequency = 0,
memory_frequency = 0,
gpu_utilization = 0,
vid_utilization = 0
}
local update_state = function()
if i_o.read_file(GPU_BUS_CTRL, nil, '*l') == 'on' then
local nvidia_settings_glob = i_o.execute_cmd(NV_QUERY)
if nvidia_settings_glob == '' then
mod_state.error = 'Error'
else
mod_state.used_memory,
mod_state.total_memory,
mod_state.temp_reading,
mod_state.gpu_frequency,
mod_state.memory_frequency,
mod_state.gpu_utilization,
mod_state.vid_utilization
= __string_match(nvidia_settings_glob, NV_REGEX)
mod_state.error = false
end
else
mod_state.error = 'Off'
end
end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- helper functions -- helper functions
local _from_state = function(def, f, set) local _from_state = function(def, get, set)
return function(s) return function()
if s.error == false then if mod_state.error == false then
set(f(s)) set(get(mod_state))
else else
set(def) set(def)
end end
@ -59,11 +117,11 @@ return function(update_freq, config, common, width, point)
width, width,
'Status' 'Status'
) )
local update = function(s) local update = function()
if s.error == false then if mod_state.error == false then
common.text_row_set(obj, 'On') common.text_row_set(obj, 'On')
else else
common.text_row_set(obj, s.error) common.text_row_set(obj, mod_state.error)
end end
end end
local static = pure.partial(common.text_row_draw_static, obj) local static = pure.partial(common.text_row_draw_static, obj)
@ -106,10 +164,10 @@ return function(update_freq, config, common, width, point)
TEXT_SPACING, TEXT_SPACING,
{'GPU Clock Speed', 'memory Clock Speed'} {'GPU Clock Speed', 'memory Clock Speed'}
) )
local update = function(s) local update = function()
if s.error == false then if mod_state.error == false then
common.text_rows_set(obj, 1, s.gpu_frequency..' Mhz') common.text_rows_set(obj, 1, mod_state.gpu_frequency..' Mhz')
common.text_rows_set(obj, 2, s.memory_frequency..' Mhz') common.text_rows_set(obj, 2, mod_state.memory_frequency..' Mhz')
else else
common.text_rows_set(obj, 1, NA) common.text_rows_set(obj, 1, NA)
common.text_rows_set(obj, 2, NA) common.text_rows_set(obj, 2, NA)
@ -147,65 +205,6 @@ return function(update_freq, config, common, width, point)
function(s) return s.vid_utilization end function(s) return s.vid_utilization end
) )
-----------------------------------------------------------------------------
-- nvidia state
-- vars to process the nv settings glob
--
-- glob will be of the form:
-- <used_mem>
-- <total_mem>
-- <temp>
-- <gpu_freq>,<mem_freq>
-- graphics=<gpu_util>, memory=<mem_util>, video=<vid_util>, PCIe=<pci_util>
local NV_QUERY = 'nvidia-settings -t'..
' -q UsedDedicatedGPUmemory'..
' -q TotalDedicatedGPUmemory'..
' -q ThermalSensorReading'..
' -q [gpu:0]/GPUCurrentClockFreqs'..
' -q [gpu:0]/GPUutilization'
local NV_REGEX = '(%d+)\n'..
'(%d+)\n'..
'(%d+)\n'..
'(%d+),(%d+)\n'..
'graphics=(%d+), memory=%d+, video=(%d+), PCIe=%d+\n'
local GPU_BUS_CTRL = '/sys/bus/pci/devices/0000:01:00.0/power/control'
local state = {
error = false,
used_memory = 0,
total_memory = 0,
temp_reading = 0,
gpu_frequency = 0,
memory_frequency = 0,
gpu_utilization = 0,
vid_utilization = 0
}
local update_state = function()
if i_o.read_file(GPU_BUS_CTRL, nil, '*l') == 'on' then
local nvidia_settings_glob = i_o.execute_cmd(NV_QUERY)
if nvidia_settings_glob == '' then
state.error = 'Error'
else
state.used_memory,
state.total_memory,
state.temp_reading,
state.gpu_frequency,
state.memory_frequency,
state.gpu_utilization,
state.vid_utilization
= __string_match(nvidia_settings_glob, NV_REGEX)
state.error = false
end
else
state.error = 'Off'
end
return state
end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- main drawing functions -- main drawing functions
@ -213,7 +212,7 @@ return function(update_freq, config, common, width, point)
header = 'NVIDIA GRAPHICS', header = 'NVIDIA GRAPHICS',
point = point, point = point,
width = width, width = width,
update_wrapper = function(f) return function(_) f(update_state()) end end, update_wrapper = function(f) return function(_) update_state() f() end end,
top = {{mk_status, true, SEPARATOR_SPACING}}, top = {{mk_status, true, SEPARATOR_SPACING}},
common.mk_section( common.mk_section(
SEPARATOR_SPACING, SEPARATOR_SPACING,

View File

@ -18,6 +18,43 @@ return function(update_freq, config, common, width, point)
local __string_match = string.match local __string_match = string.match
local __math_floor = math.floor local __math_floor = math.floor
-----------------------------------------------------------------------------
-- state
local MEMINFO_REGEX = '\nMemFree:%s+(%d+).+'..
'\nBuffers:%s+(%d+).+'..
'\nCached:%s+(%d+).+'..
'\nSwapFree:%s+(%d+).+'..
'\nShmem:%s+(%d+).+'..
'\nSReclaimable:%s+(%d+)'
local get_meminfo_field = function(field)
return tonumber(i_o.read_file('/proc/meminfo', field..':%s+(%d+)'))
end
local mod_state = {
mem = {total = get_meminfo_field('MemTotal')},
swap = {total = get_meminfo_field('SwapTotal')}
}
local read_state = function()
local m = mod_state.mem
-- see manpage for free command for formulas
m.memfree,
m.buffers,
m.cached,
mod_state.swap.free,
m.shmem,
m.sreclaimable
= __string_match(i_o.read_file('/proc/meminfo'), MEMINFO_REGEX)
m.used_percent =
(m.total -
m.memfree -
m.cached -
m.buffers -
m.sreclaimable) / m.total
end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- mem stats (dial + text) -- mem stats (dial + text)
@ -56,9 +93,9 @@ return function(update_freq, config, common, width, point)
{'Page Cache', 'Buffers', 'Shared', 'Kernel Slab'}, {'Page Cache', 'Buffers', 'Shared', 'Kernel Slab'},
'%.1f%%' '%.1f%%'
) )
local update = function(s) local update = function()
local m = s.mem local m = mod_state.mem
local w = s.swap local w = mod_state.swap
common.dial_set(mem, m.used_percent * 100) common.dial_set(mem, m.used_percent * 100)
common.dial_set(swap, (w.total - w.free) / w.total * 100) common.dial_set(swap, (w.total - w.free) / w.total * 100)
@ -94,7 +131,7 @@ return function(update_freq, config, common, width, point)
return common.mk_acc( return common.mk_acc(
width, width,
PLOT_HEIGHT, PLOT_HEIGHT,
function(s) timeseries.update(obj, s.mem.used_percent) end, function() timeseries.update(obj, mod_state.mem.used_percent) end,
pure.partial(timeseries.draw_static, obj), pure.partial(timeseries.draw_static, obj),
pure.partial(timeseries.draw_dynamic, obj) pure.partial(timeseries.draw_dynamic, obj)
) )
@ -122,7 +159,7 @@ return function(update_freq, config, common, width, point)
NUM_ROWS, NUM_ROWS,
'Mem (%)' 'Mem (%)'
) )
local update = function(_) local update = function()
for r = 1, NUM_ROWS do for r = 1, NUM_ROWS do
text_table.set(obj, 1, r, i_o.conky(TABLE_CONKY[r].comm, '(%S+)')) text_table.set(obj, 1, r, i_o.conky(TABLE_CONKY[r].comm, '(%S+)'))
text_table.set(obj, 2, r, i_o.conky(TABLE_CONKY[r].pid)) text_table.set(obj, 2, r, i_o.conky(TABLE_CONKY[r].pid))
@ -138,43 +175,6 @@ return function(update_freq, config, common, width, point)
) )
end end
-----------------------------------------------------------------------------
-- state
local MEMINFO_REGEX = '\nMemFree:%s+(%d+).+'..
'\nBuffers:%s+(%d+).+'..
'\nCached:%s+(%d+).+'..
'\nSwapFree:%s+(%d+).+'..
'\nShmem:%s+(%d+).+'..
'\nSReclaimable:%s+(%d+)'
local get_meminfo_field = function(field)
return tonumber(i_o.read_file('/proc/meminfo', field..':%s+(%d+)'))
end
local state = {
mem = {total = get_meminfo_field('MemTotal')},
swap = {total = get_meminfo_field('SwapTotal')}
}
local read_state = function()
local m = state.mem
-- see manpage for free command for formulas
m.memfree,
m.buffers,
m.cached,
state.swap.free,
m.shmem,
m.sreclaimable
= __string_match(i_o.read_file('/proc/meminfo'), MEMINFO_REGEX)
m.used_percent =
(m.total -
m.memfree -
m.cached -
m.buffers -
m.sreclaimable) / m.total
return state
end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- main functions -- main functions
@ -182,7 +182,7 @@ return function(update_freq, config, common, width, point)
header = 'MEMORY', header = 'MEMORY',
point = point, point = point,
width = width, width = width,
update_wrapper = function(f) return function(_) f(read_state()) end end, update_wrapper = function(f) return function(_) read_state() f() end end,
top = { top = {
{mk_stats, config.show_stats, PLOT_SECTION_BREAK}, {mk_stats, config.show_stats, PLOT_SECTION_BREAK},
{mk_plot, config.show_plot, TABLE_SECTION_BREAK}, {mk_plot, config.show_plot, TABLE_SECTION_BREAK},

View File

@ -12,17 +12,16 @@ return function(update_freq, common, width, point)
return i_o.read_file(path, nil, '*n') * 8 return i_o.read_file(path, nil, '*n') * 8
end end
local state = {rx_bits = 0, tx_bits = 0} local mod_state = {rx_bits = 0, tx_bits = 0}
local read_interfaces = function() local read_interfaces = function()
state.rx_bits = 0 mod_state.rx_bits = 0
state.tx_bits = 0 mod_state.tx_bits = 0
for i = 1, #INTERFACE_PATHS do for i = 1, #INTERFACE_PATHS do
local p = INTERFACE_PATHS[i] local p = INTERFACE_PATHS[i]
state.rx_bits = state.rx_bits + get_bits(p.rx) mod_state.rx_bits = mod_state.rx_bits + get_bits(p.rx)
state.tx_bits = state.tx_bits + get_bits(p.tx) mod_state.tx_bits = mod_state.tx_bits + get_bits(p.tx)
end end
return state
end end
-- prime initial state -- prime initial state
@ -48,12 +47,12 @@ return function(update_freq, common, width, point)
label, label,
2, 2,
update_freq, update_freq,
state[key] mod_state[key]
) )
return common.mk_acc( return common.mk_acc(
width, width,
PLOT_HEIGHT + PLOT_SEC_BREAK, PLOT_HEIGHT + PLOT_SEC_BREAK,
function(s) common.update_rate_timeseries(obj, s[key]) end, function() common.update_rate_timeseries(obj, mod_state[key]) end,
pure.partial(common.tagged_scaled_timeseries_draw_static, obj), pure.partial(common.tagged_scaled_timeseries_draw_static, obj),
pure.partial(common.tagged_scaled_timeseries_draw_dynamic, obj) pure.partial(common.tagged_scaled_timeseries_draw_dynamic, obj)
) )
@ -69,7 +68,7 @@ return function(update_freq, common, width, point)
header = 'NETWORK', header = 'NETWORK',
point = point, point = point,
width = width, width = width,
update_wrapper = function(f) return function(_) f(read_interfaces()) end end, update_wrapper = function(f) return function(_) read_interfaces() f() end end,
top = { top = {
{mk_rx, true, PLOT_SEC_BREAK}, {mk_rx, true, PLOT_SEC_BREAK},
{mk_tx, true, 0}, {mk_tx, true, 0},