conky-config/drawing/graphics.lua

219 lines
6.4 KiB
Lua
Raw Normal View History

2021-07-29 22:37:30 -04:00
local text = require 'text'
local line = require 'line'
local util = require 'util'
local common = require 'common'
local geometry = require 'geometry'
2017-07-19 00:36:15 -04:00
2021-07-17 23:28:59 -04:00
return function(update_freq)
local MODULE_Y = 145
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
local __string_format = string.format
2017-07-19 00:36:15 -04:00
2021-07-13 00:14:04 -04:00
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
-- header
2021-07-13 00:14:04 -04:00
local header = common.Header(
geometry.LEFT_X,
2021-07-17 23:28:59 -04:00
MODULE_Y,
geometry.SECTION_WIDTH,
2021-07-17 23:28:59 -04:00
'NVIDIA GRAPHICS'
)
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
-- gpu status
2021-07-29 22:37:30 -04:00
local status = common.inittextRow(
geometry.LEFT_X,
2021-07-17 23:28:59 -04:00
header.bottom_y,
geometry.SECTION_WIDTH,
2021-07-17 23:28:59 -04:00
'Status'
)
2021-07-17 23:28:59 -04:00
local SEP_Y1 = header.bottom_y + SEPARATOR_SPACING
local separator1 = common.initSeparator(
geometry.LEFT_X,
2021-07-17 23:28:59 -04:00
SEP_Y1,
geometry.SECTION_WIDTH
2021-07-17 23:28:59 -04:00
)
-----------------------------------------------------------------------------
-- gpu temperature
local INTERNAL_TEMP_Y = SEP_Y1 + SEPARATOR_SPACING
2021-07-29 22:37:30 -04:00
local internal_temp = common.inittextRowCrit(
geometry.LEFT_X,
2021-07-17 23:28:59 -04:00
INTERNAL_TEMP_Y,
geometry.SECTION_WIDTH,
2021-07-17 23:28:59 -04:00
'Internal Temperature',
function(s)
if s == -1 then return NA else return string.format('%s°C', s) end
end,
80
)
2021-07-17 23:28:59 -04:00
local SEP_Y2 = INTERNAL_TEMP_Y + SEPARATOR_SPACING
local separator2 = common.initSeparator(
geometry.LEFT_X,
2021-07-17 23:28:59 -04:00
SEP_Y2,
geometry.SECTION_WIDTH
2021-07-17 23:28:59 -04:00
)
-----------------------------------------------------------------------------
-- gpu clock speeds
local CLOCK_SPEED_Y = SEP_Y2 + SEPARATOR_SPACING
2021-07-29 22:37:30 -04:00
local clock_speed = common.inittextRows(
geometry.LEFT_X,
2021-07-17 23:28:59 -04:00
CLOCK_SPEED_Y,
geometry.SECTION_WIDTH,
2021-07-17 23:28:59 -04:00
TEXT_SPACING,
{'GPU Clock Speed', 'memory Clock Speed'}
)
2021-07-17 23:28:59 -04:00
local SEP_Y3 = CLOCK_SPEED_Y + TEXT_SPACING * 2
local separator3 = common.initSeparator(
geometry.LEFT_X,
2021-07-17 23:28:59 -04:00
SEP_Y3,
geometry.SECTION_WIDTH
2021-07-17 23:28:59 -04:00
)
-----------------------------------------------------------------------------
-- gpu utilization plot
local na_percent_format = function(x)
if x == -1 then return NA else return __string_format('%s%%', x) end
end
local build_plot = function(y, label)
return common.initPercentPlot_formatted(
geometry.LEFT_X,
2021-07-17 23:28:59 -04:00
y,
geometry.SECTION_WIDTH,
2021-07-17 23:28:59 -04:00
PLOT_HEIGHT,
PLOT_SEC_BREAK,
label,
update_freq,
na_percent_format
)
end
local GPU_UTIL_Y = SEP_Y3 + SEPARATOR_SPACING
local gpu_util = build_plot(GPU_UTIL_Y, 'GPU utilization')
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
-- gpu memory consumption plot
local MEM_UTIL_Y = GPU_UTIL_Y + PLOT_HEIGHT + PLOT_SEC_BREAK * 2
local mem_util = build_plot(MEM_UTIL_Y, 'memory utilization')
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
-- gpu video utilization plot
local VID_UTIL_Y = MEM_UTIL_Y + PLOT_HEIGHT + PLOT_SEC_BREAK * 2
local vid_util = build_plot(VID_UTIL_Y, 'Video utilization')
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
-- update function
-- 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'
2021-07-26 23:45:54 -04:00
local nvidia_off = function()
common.text_row_crit_set(internal_temp, -1)
common.text_rows_set(clock_speed, 1, NA)
common.text_rows_set(clock_speed, 2, NA)
common.percent_plot_set(gpu_util, nil)
common.percent_plot_set(vid_util, nil)
common.percent_plot_set(mem_util, nil)
end
2021-07-26 23:45:54 -04:00
local update = function()
if util.read_file(GPU_BUS_CTRL, nil, '*l') == 'on' then
local nvidia_settings_glob = util.execute_cmd(NV_QUERY)
if nvidia_settings_glob == '' then
2021-07-29 22:37:30 -04:00
text.set(status.value, 'Error')
2021-07-26 23:45:54 -04:00
nvidia_off()
else
common.text_row_set(status, 'On')
local used_memory, total_memory, temp_reading, gpu_frequency,
memory_frequency, gpu_utilization, vid_utilization
= __string_match(nvidia_settings_glob, NV_REGEX)
common.text_row_crit_set(internal_temp, temp_reading)
common.text_rows_set(clock_speed, 1, gpu_frequency..' Mhz')
common.text_rows_set(clock_speed, 2, memory_frequency..' Mhz')
common.percent_plot_set(gpu_util, gpu_utilization)
common.percent_plot_set(mem_util, used_memory / total_memory * 100)
common.percent_plot_set(vid_util, vid_utilization)
end
else
2021-07-29 22:37:30 -04:00
text.set(status.value, 'Off')
2021-07-26 23:45:54 -04:00
nvidia_off()
end
end
2021-07-17 23:28:59 -04:00
-----------------------------------------------------------------------------
-- main drawing functions
local draw_static = function(cr)
common.drawHeader(cr, header)
common.text_row_draw_static(status, cr)
2021-07-29 22:37:30 -04:00
line.draw(separator1, cr)
common.text_row_crit_draw_static(internal_temp, cr)
2021-07-29 22:37:30 -04:00
line.draw(separator2, cr)
2017-07-19 00:36:15 -04:00
common.text_rows_draw_static(clock_speed, cr)
2021-07-29 22:37:30 -04:00
line.draw(separator3, cr)
common.percent_plot_draw_static(gpu_util, cr)
common.percent_plot_draw_static(mem_util, cr)
common.percent_plot_draw_static(vid_util, cr)
end
local draw_dynamic = function(cr)
common.text_row_draw_dynamic(status, cr)
common.text_row_crit_draw_dynamic(internal_temp, cr)
common.text_rows_draw_dynamic(clock_speed, cr)
common.percent_plot_draw_dynamic(gpu_util, cr)
common.percent_plot_draw_dynamic(mem_util, cr)
common.percent_plot_draw_dynamic(vid_util, cr)
end
2017-07-19 00:36:15 -04:00
return {static = draw_static, dynamic = draw_dynamic, update = update}
end