conky-config/drawing/processor.lua

252 lines
7.3 KiB
Lua
Raw Normal View History

local compound_dial = require 'compound_dial'
2021-08-08 18:19:37 -04:00
local text_table = require 'text_table'
2021-08-08 15:58:53 -04:00
local i_o = require 'i_o'
local common = require 'common'
local geometry = require 'geometry'
2021-08-08 15:10:09 -04:00
local cpu = require 'sys'
local pure = require 'pure'
2019-09-01 15:34:10 -04:00
2021-07-13 01:01:21 -04:00
local __math_floor = math.floor
2021-07-12 23:49:52 -04:00
return function(update_freq, main_state, point)
-- local SHOW_DIALS = true
-- local SHOW_TIMESERIES = true
-- local SHOW_TABLE = true
2021-07-17 23:06:30 -04:00
local DIAL_INNER_RADIUS = 30
local DIAL_OUTER_RADIUS = 42
local DIAL_THICKNESS = 5.5
local SEPARATOR_SPACING = 20
local TEXT_SPACING = 22
local PLOT_SECTION_BREAK = 23
local PLOT_HEIGHT = 56
local TABLE_SECTION_BREAK = 20
local TABLE_HEIGHT = 114
-----------------------------------------------------------------------------
-- cores (loads and temps)
-- this totally is not supposed to be a state monad (ssssh...)
local update_state = function(cpu_loads)
return {
cpu_loads = cpu.read_cpu_loads(cpu_loads),
load_sum = 0,
trigger = main_state.trigger10
}
end
local state = update_state(cpu.init_cpu_loads())
local ncpus = cpu.get_cpu_number()
local ncores = cpu.get_core_number()
2021-07-17 21:22:17 -04:00
local nthreads = ncpus / ncores
2021-07-17 23:06:30 -04:00
local create_core = function(x, y)
return {
2021-07-30 22:02:46 -04:00
loads = common.make_compound_dial(
2021-07-17 23:06:30 -04:00
x,
y,
DIAL_OUTER_RADIUS,
DIAL_INNER_RADIUS,
DIAL_THICKNESS,
80,
2021-07-17 23:06:30 -04:00
nthreads
),
coretemp = common.make_text_circle(
2021-07-17 23:06:30 -04:00
x,
y,
DIAL_INNER_RADIUS - 2,
'%s°C',
2021-11-10 16:26:48 -05:00
80,
__math_floor
2021-07-17 23:06:30 -04:00
)
}
end
local mk_cores = function(y)
local coretemp_paths = cpu.get_coretemp_paths()
local cores = {}
2022-07-12 01:23:59 -04:00
-- TODO what happens when the number of cores changes?
for c = 1, ncores do
local dial_x = point.x + DIAL_OUTER_RADIUS +
(geometry.SECTION_WIDTH - 2 * DIAL_OUTER_RADIUS) * (c - 1) / 3
local dial_y = y + DIAL_OUTER_RADIUS
cores[c] = create_core(dial_x, dial_y)
end
local update = function(state_)
local s = state_.load_sum
for _, load_data in pairs(state_.cpu_loads) do
local cur = load_data.percent_active
s = s + cur
compound_dial.set(
cores[load_data.conky_core_id].loads,
load_data.conky_thread_id,
cur * 100
)
end
for conky_core_id, path in pairs(coretemp_paths) do
local temp = __math_floor(0.001 * i_o.read_file(path, nil, '*n'))
common.text_circle_set(cores[conky_core_id].coretemp, temp)
end
state_.load_sum = s
return state_
end
local static = function(cr)
for i = 1, #cores do
common.text_circle_draw_static(cores[i].coretemp, cr)
compound_dial.draw_static(cores[i].loads, cr)
end
end
local dynamic = function(cr)
for i = 1, #cores do
common.text_circle_draw_dynamic(cores[i].coretemp, cr)
2022-07-12 01:23:59 -04:00
compound_dial.draw_dynamic(cores[i].loads, cr)
end
end
2022-07-16 00:00:06 -04:00
return common.mk_acc(
geometry.SECTION_WIDTH,
DIAL_OUTER_RADIUS * 2,
update,
static,
dynamic
)
2021-07-17 21:22:17 -04:00
end
2021-07-17 23:06:30 -04:00
-----------------------------------------------------------------------------
-- HWP status
local mk_hwp_freq = function(y)
local hwp_paths = cpu.get_hwp_paths()
local cpu_status = common.make_text_rows(
point.x,
y,
geometry.SECTION_WIDTH,
TEXT_SPACING,
{'HWP Preference', 'Ave Freq'}
)
local update = function(state_)
-- For some reason this call is slow (querying anything with pstate in
-- general seems slow), but I also don't need to see an update every
-- cycle, hence the trigger
if state_.trigger == 0 then
common.text_rows_set(cpu_status, 1, cpu.read_hwp(hwp_paths))
end
common.text_rows_set(cpu_status, 2, cpu.read_freq())
return state_
end
local static = pure.partial(common.text_rows_draw_static, cpu_status)
local dynamic = pure.partial(common.text_rows_draw_dynamic, cpu_status)
2022-07-16 00:00:06 -04:00
return common.mk_acc(
geometry.SECTION_WIDTH,
TEXT_SPACING,
update,
static,
dynamic
)
end
2021-07-17 23:06:30 -04:00
-----------------------------------------------------------------------------
-- frequency
2022-07-13 01:06:15 -04:00
local mk_sep = pure.partial(
common.mk_seperator,
geometry.SECTION_WIDTH,
point.x
2022-07-13 01:06:15 -04:00
)
2021-07-17 23:06:30 -04:00
-----------------------------------------------------------------------------
-- total load plot
local mk_load_plot = function(y)
local total_load = common.make_tagged_percent_timeseries(
point.x,
y,
geometry.SECTION_WIDTH,
PLOT_HEIGHT,
PLOT_SECTION_BREAK,
"Total Load",
update_freq
)
local update = function(state_)
common.tagged_percent_timeseries_set(
total_load,
state_.load_sum / ncpus * 100
)
return state_
end
local static = pure.partial(common.tagged_percent_timeseries_draw_static, total_load)
local dynamic = pure.partial(common.tagged_percent_timeseries_draw_dynamic, total_load)
2022-07-16 00:00:06 -04:00
return common.mk_acc(
geometry.SECTION_WIDTH,
PLOT_HEIGHT + PLOT_SECTION_BREAK,
update,
static,
dynamic
)
end
2021-07-17 23:06:30 -04:00
-----------------------------------------------------------------------------
-- cpu top table
local mk_tbl = function(y)
local NUM_ROWS = 5
local TABLE_CONKY = pure.map_n(
function(i) return {pid = '${top pid '..i..'}', cpu = '${top cpu '..i..'}'} end,
NUM_ROWS
)
local tbl = common.make_text_table(
point.x,
y,
geometry.SECTION_WIDTH,
TABLE_HEIGHT,
NUM_ROWS,
'CPU (%)'
)
local update = function(state_)
for r = 1, NUM_ROWS do
local pid = i_o.conky(TABLE_CONKY[r].pid, '(%d+)') -- may have leading spaces
if pid ~= '' then
text_table.set(tbl, 1, r, i_o.read_file('/proc/'..pid..'/comm', '(%C+)'))
text_table.set(tbl, 2, r, pid)
text_table.set(tbl, 3, r, i_o.conky(TABLE_CONKY[r].cpu))
end
end
return state_
2019-09-01 15:34:10 -04:00
end
local static = pure.partial(text_table.draw_static, tbl)
local dynamic = pure.partial(text_table.draw_dynamic, tbl)
2022-07-16 00:00:06 -04:00
return common.mk_acc(
geometry.SECTION_WIDTH,
TABLE_HEIGHT,
update,
static,
dynamic
)
end
-----------------------------------------------------------------------------
-- main functions
2022-07-12 01:23:59 -04:00
local rbs = common.reduce_blocks(
2022-07-14 22:49:08 -04:00
'PROCESSOR',
point,
geometry.SECTION_WIDTH,
{
2022-07-16 14:30:26 -04:00
common.mk_block(mk_cores, true, TEXT_SPACING),
common.mk_block(mk_hwp_freq, true, SEPARATOR_SPACING),
2022-07-12 01:23:59 -04:00
common.mk_block(mk_sep, true, SEPARATOR_SPACING),
2022-07-16 14:30:26 -04:00
common.mk_block(mk_load_plot, true, TABLE_SECTION_BREAK),
common.mk_block(mk_tbl, true, 0)
}
)
return pure.map_at(
"update",
function(f)
return function()
f(update_state(state.cpu_loads))
end
end,
rbs
)
end