conky-config/drawing/graphics.lua

244 lines
6.9 KiB
Lua
Raw Normal View History

2022-07-13 01:06:15 -04:00
local pure = require 'pure'
2021-08-08 15:58:53 -04:00
local i_o = require 'i_o'
local common = require 'common'
2017-07-19 00:36:15 -04:00
return function(update_freq, config, width, point)
2021-07-17 23:28:59 -04:00
local SEPARATOR_SPACING = 20
local TEXT_SPACING = 20
local PLOT_SEC_BREAK = 20
local PLOT_HEIGHT = 56
local NA = 'N/A'
local __string_match = string.match
2021-11-10 16:55:10 -05:00
local __tonumber = tonumber
2017-07-19 00:36:15 -04:00
2022-07-13 01:06:15 -04:00
-----------------------------------------------------------------------------
-- helper functions
local _from_state = function(def, f, set)
return function(s)
if s.error == false then
set(f(s))
else
set(def)
end
end
end
local _mk_plot = function(label, getter, y)
local obj = common.make_tagged_maybe_percent_timeseries(
point.x,
2022-07-13 01:06:15 -04:00
y,
width,
2022-07-13 01:06:15 -04:00
PLOT_HEIGHT,
PLOT_SEC_BREAK,
label,
update_freq
)
local update = _from_state(
false,
getter,
pure.partial(common.tagged_maybe_percent_timeseries_set, obj)
)
local static = pure.partial(common.tagged_percent_timeseries_draw_static, obj)
local dynamic = pure.partial(common.tagged_percent_timeseries_draw_dynamic, obj)
2022-07-16 00:00:06 -04:00
return common.mk_acc(
width,
2022-07-16 00:00:06 -04:00
PLOT_HEIGHT + PLOT_SEC_BREAK,
update,
static,
dynamic
)
2022-07-13 01:06:15 -04:00
end
2021-07-13 00:14:04 -04:00
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
-- gpu status
2022-07-13 01:06:15 -04:00
local mk_status = function(y)
local obj = common.make_text_row(
point.x,
2022-07-13 01:06:15 -04:00
y,
width,
2022-07-13 01:06:15 -04:00
'Status'
)
local update = function(s)
if s.error == false then
common.text_row_set(obj, 'On')
else
common.text_row_set(obj, s.error)
end
end
local static = pure.partial(common.text_row_draw_static, obj)
local dynamic = pure.partial(common.text_row_draw_dynamic, obj)
return common.mk_acc(width, 0, update, static, dynamic)
2022-07-13 01:06:15 -04:00
end
2021-07-17 23:28:59 -04:00
2022-07-13 01:06:15 -04:00
local mk_sep = pure.partial(
common.mk_seperator,
width,
point.x
2021-07-17 23:28:59 -04:00
)
-----------------------------------------------------------------------------
-- gpu temperature
2022-07-13 01:06:15 -04:00
local mk_temp = function(y)
local obj = common.make_threshold_text_row(
point.x,
2022-07-13 01:06:15 -04:00
y,
width,
2022-07-13 01:06:15 -04:00
'Internal Temperature',
function(s)
if s == -1 then return NA else return string.format('%s°C', s) end
end,
80
)
local update = _from_state(
-1,
function(s) return __tonumber(s.temp_reading) end,
pure.partial(common.threshold_text_row_set, obj)
)
local static = pure.partial(common.threshold_text_row_draw_static, obj)
local dynamic = pure.partial(common.threshold_text_row_draw_dynamic, obj)
return common.mk_acc(width, 0, update, static, dynamic)
2022-07-13 01:06:15 -04:00
end
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
-- gpu clock speeds
2022-07-13 01:06:15 -04:00
local mk_clock = function(y)
local obj = common.make_text_rows(
point.x,
2021-07-17 23:28:59 -04:00
y,
width,
2022-07-13 01:06:15 -04:00
TEXT_SPACING,
{'GPU Clock Speed', 'memory Clock Speed'}
2021-07-17 23:28:59 -04:00
)
2022-07-13 01:06:15 -04:00
local update = function(s)
if s.error == false then
common.text_rows_set(obj, 1, s.gpu_frequency..' Mhz')
common.text_rows_set(obj, 2, s.memory_frequency..' Mhz')
else
common.text_rows_set(obj, 1, NA)
common.text_rows_set(obj, 2, NA)
end
end
local static = pure.partial(common.text_rows_draw_static, obj)
local dynamic = pure.partial(common.text_rows_draw_dynamic, obj)
return common.mk_acc(width, TEXT_SPACING, update, static, dynamic)
2021-07-17 23:28:59 -04:00
end
2022-07-13 01:06:15 -04:00
-----------------------------------------------------------------------------
-- gpu utilization plot
local mk_gpu_util = pure.partial(
_mk_plot,
'GPU utilization',
function(s) return s.gpu_utilization end
)
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
-- gpu memory consumption plot
2022-07-13 01:06:15 -04:00
local mk_mem_util = pure.partial(
_mk_plot,
'Memory utilization',
function(s) return s.used_memory / s.total_memory * 100 end
)
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
-- gpu video utilization plot
2022-07-13 01:06:15 -04:00
local mk_vid_util = pure.partial(
_mk_plot,
'Video utilization',
function(s) return s.vid_utilization end
)
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
2022-07-13 01:06:15 -04:00
-- nvidia state
2021-07-17 23:28:59 -04:00
-- 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'..
2021-07-17 23:28:59 -04:00
' -q ThermalSensorReading'..
' -q [gpu:0]/GPUCurrentClockFreqs'..
' -q [gpu:0]/GPUutilization'
2021-07-17 23:28:59 -04:00
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'
2022-07-13 01:06:15 -04:00
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()
2021-08-08 15:58:53 -04:00
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
2022-07-13 01:06:15 -04:00
state.error = 'Error'
else
2022-07-13 01:06:15 -04:00
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)
2022-07-13 01:06:15 -04:00
state.error = false
end
else
2022-07-13 01:06:15 -04:00
state.error = 'Off'
end
2022-07-13 01:06:15 -04:00
return state
end
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
-- main drawing functions
2022-07-13 01:06:15 -04:00
local rbs = common.reduce_blocks_(
2022-07-14 22:49:08 -04:00
'NVIDIA GRAPHICS',
point,
width,
{{mk_status, true, SEPARATOR_SPACING}},
common.mk_section(
SEPARATOR_SPACING,
mk_sep,
{mk_temp, config.show_temp, SEPARATOR_SPACING}
),
common.mk_section(
SEPARATOR_SPACING,
mk_sep,
{mk_clock, config.show_clock, SEPARATOR_SPACING}
),
common.mk_section(
SEPARATOR_SPACING,
mk_sep,
{mk_gpu_util, config.show_gpu_util, PLOT_SEC_BREAK},
{mk_mem_util, config.show_mem_util, PLOT_SEC_BREAK},
{mk_vid_util, config.show_vid_util, 0}
)
2022-07-13 01:06:15 -04:00
)
2022-07-16 00:00:06 -04:00
return pure.map_at("update", function(f) return function(_) f(update_state()) end end, rbs)
end