conky-config/drawing/Processor.lua

303 lines
7.9 KiB
Lua
Raw Normal View History

local M = {}
2017-07-19 00:36:15 -04:00
local CompoundDial = require 'CompoundDial'
local Line = require 'Line'
local Table = require 'Table'
local Util = require 'Util'
local Common = require 'Common'
2019-09-01 15:34:10 -04:00
2021-07-12 23:49:52 -04:00
local __string_match = string.match
2021-07-13 00:53:41 -04:00
local __string_gmatch = string.gmatch
local __string_format = string.format
local __tonumber = tonumber
2021-07-13 01:01:21 -04:00
local __math_floor = math.floor
2021-07-12 23:49:52 -04:00
2017-07-19 00:36:15 -04:00
local CORETEMP_PATH = '/sys/devices/platform/coretemp.0/hwmon/hwmon%i/%s'
local NUM_PHYSICAL_CORES = 4
local NUM_THREADS_PER_CORE = 2
2021-07-13 01:18:33 -04:00
local NCPU = NUM_THREADS_PER_CORE * NUM_PHYSICAL_CORES
2017-07-19 00:36:15 -04:00
local NUM_ROWS = 5
2018-08-04 17:54:34 -04:00
2020-01-28 20:06:56 -05:00
local HWP_PATHS = {}
2021-07-13 01:18:33 -04:00
for i = 1, NCPU do
HWP_PATHS[i] = '/sys/devices/system/cpu/cpu' .. (i - 1) ..
2020-01-28 20:06:56 -05:00
'/cpufreq/energy_performance_preference'
end
2018-08-04 17:54:34 -04:00
local TABLE_CONKY = {}
2017-07-19 00:36:15 -04:00
for r = 1, NUM_ROWS do
2018-08-04 17:54:34 -04:00
TABLE_CONKY[r] = {}
TABLE_CONKY[r].pid = '${top pid '..r..'}'
TABLE_CONKY[r].cpu = '${top cpu '..r..'}'
2017-07-19 00:36:15 -04:00
end
2017-10-23 02:40:12 -04:00
local _MODULE_Y_ = 614
2017-07-19 00:36:15 -04:00
local _DIAL_INNER_RADIUS_ = 30
local _DIAL_OUTER_RADIUS_ = 42
2021-07-11 12:58:51 -04:00
local _DIAL_THICKNESS_ = 5.5
2017-07-19 00:36:15 -04:00
local _SEPARATOR_SPACING_ = 20
2017-10-23 02:40:12 -04:00
local _TEXT_SPACING_ = 22
2017-07-19 00:36:15 -04:00
local _PLOT_SECTION_BREAK_ = 23
local _PLOT_HEIGHT_ = 56
local _TABLE_SECTION_BREAK_ = 20
local _TABLE_HEIGHT_ = 114
local _create_core_ = function(cores, id, x, y)
2018-01-05 23:36:50 -05:00
local hwmon_index = -1
while Util.read_file(string.format(CORETEMP_PATH, hwmon_index, 'name'), nil, '*l') ~= 'coretemp' do
hwmon_index = hwmon_index + 1
end
cores[id +1] = {
2021-07-11 23:10:35 -04:00
dials = Common.compound_dial(
x,
y,
_DIAL_OUTER_RADIUS_,
_DIAL_INNER_RADIUS_,
_DIAL_THICKNESS_,
0.8,
NUM_THREADS_PER_CORE
2021-07-08 22:57:35 -04:00
),
text_ring = Common.initTextRing(
x,
y,
_DIAL_INNER_RADIUS_ - 2,
'%s°C',
90
),
2018-01-05 23:36:50 -05:00
coretemp_path = string.format(CORETEMP_PATH, hwmon_index, 'temp'..(id + 2)..'_input'),
}
2017-07-19 00:36:15 -04:00
end
local header = Common.Header(
_G_INIT_DATA_.LEFT_X,
_MODULE_Y_,
_G_INIT_DATA_.SECTION_WIDTH,
'PROCESSOR'
)
2017-07-19 00:36:15 -04:00
--we assume that this cpu has 4 physical cores with 2 logical each
local cores = {}
for c = 0, NUM_PHYSICAL_CORES - 1 do
2018-01-05 23:36:50 -05:00
local dial_x = _G_INIT_DATA_.LEFT_X + _DIAL_OUTER_RADIUS_ +
2017-07-19 00:36:15 -04:00
(_G_INIT_DATA_.SECTION_WIDTH - 2 * _DIAL_OUTER_RADIUS_) * c / 3
2018-01-05 23:36:50 -05:00
local dial_y = header.bottom_y + _DIAL_OUTER_RADIUS_
_create_core_(cores, c, dial_x, dial_y)
2017-07-19 00:36:15 -04:00
end
2020-01-28 20:06:56 -05:00
local _HWP_Y_ = header.bottom_y + _DIAL_OUTER_RADIUS_ * 2 + _PLOT_SECTION_BREAK_
2017-07-19 00:36:15 -04:00
2020-01-28 20:06:56 -05:00
local _FREQ_Y_ = _HWP_Y_ + _TEXT_SPACING_
2017-10-23 02:40:12 -04:00
local cpu_status = Common.initTextRows(
_G_INIT_DATA_.LEFT_X,
_HWP_Y_,
_G_INIT_DATA_.SECTION_WIDTH,
_TEXT_SPACING_,
{'HWP Preference', 'Ave Freq'}
)
2017-10-23 02:40:12 -04:00
local _SEP_Y_ = _FREQ_Y_ + _SEPARATOR_SPACING_
2017-07-19 00:36:15 -04:00
local separator = Common.initSeparator(
_G_INIT_DATA_.LEFT_X,
_SEP_Y_,
_G_INIT_DATA_.SECTION_WIDTH
)
2017-07-19 00:36:15 -04:00
local _LOAD_Y_ = _SEP_Y_ + _SEPARATOR_SPACING_
local _PLOT_Y_ = _LOAD_Y_ + _PLOT_SECTION_BREAK_
local total_load = Common.initPercentPlot(
_G_INIT_DATA_.LEFT_X,
_LOAD_Y_,
_G_INIT_DATA_.SECTION_WIDTH,
_PLOT_HEIGHT_,
_PLOT_SECTION_BREAK_,
"Total Load"
)
local tbl = Common.initTable(
_G_INIT_DATA_.LEFT_X,
_PLOT_Y_ + _PLOT_HEIGHT_ + _TABLE_SECTION_BREAK_,
_G_INIT_DATA_.SECTION_WIDTH,
_TABLE_HEIGHT_,
NUM_ROWS,
{'Name', 'PID', 'CPU (%)'}
)
2017-07-19 00:36:15 -04:00
2021-07-12 23:49:52 -04:00
local cpu_loads = {}
for i = 1, NCPU do
cpu_loads[i] = {active_prev = 0, active_total = 0}
end
local _read_cpu = function()
local i = NCPU
local iter = io.lines('/proc/stat')
iter() -- ignore first line
for ln in iter do
if i == 0 then break end
local user, system, idle = __string_match(ln, '(%d+) %d+ (%d+) (%d+)', 5)
local c = cpu_loads[i]
c.active_prev = c.active
c.total_prev = c.total
c.active = user + system
c.total = user + system + idle
i = i - 1
end
end
_read_cpu() -- prime once
2021-07-13 00:53:41 -04:00
local _read_freq = function()
2021-07-13 00:59:20 -04:00
-- NOTE: Using the builtin conky functions for getting cpu freq seems to
-- make the entire loop jittery due to high variance latency. Querying
-- scaling_cur_freq in sysfs seems to do the same thing. It appears
-- /proc/cpuinfo is much faster and doesn't have this jittery problem.
2021-07-13 00:53:41 -04:00
local c = Util.read_file('/proc/cpuinfo')
local f = 0
for s in __string_gmatch(c, 'cpu MHz%s+: (%d+%.%d+)') do
f = f + __tonumber(s)
end
return __string_format('%.0f Mhz', f / NCPU)
end
2021-07-13 01:12:34 -04:00
local _read_hwp = function()
-- read HWP of first cpu, then test all others to see if they match
local hwp_pref = Util.read_file(HWP_PATHS[1], nil, "*l")
local mixed = false
local i = 2
while not mixed and i <= #HWP_PATHS do
if hwp_pref ~= Util.read_file(HWP_PATHS[i], nil, '*l') then
mixed = true
end
i = i + 1
end
if mixed then
return 'Mixed'
elseif hwp_pref == 'power' then
return 'Power'
elseif hwp_pref == 'balance_power' then
return 'Bal. Power'
elseif hwp_pref == 'balance_performance' then
return 'Bal. Performance'
elseif hwp_pref == 'performance' then
return 'Performance'
elseif hwp_pref == 'default' then
return 'Default'
else
return 'Unknown'
end
end
local update = function(cr, trigger)
2018-01-05 23:36:50 -05:00
local conky = Util.conky
local load_sum = 0
2019-09-01 15:34:10 -04:00
-- TODO bundle all the crap down below into this function and make it return
-- something useful rather than totally use a side effect (it will be mildly
-- slower)
-- this entire loop is about 10% total execution time
2021-07-12 23:49:52 -04:00
_read_cpu()
2018-01-05 23:36:50 -05:00
for c = 1, NUM_PHYSICAL_CORES do
local core = cores[c]
2019-09-01 15:34:10 -04:00
for t = 1, NUM_THREADS_PER_CORE do
-- TODO these might not match the actual core numbers (if I care)
2021-07-12 23:49:52 -04:00
local cl = cpu_loads[(c - 1) * NUM_THREADS_PER_CORE + t]
2021-07-13 01:12:34 -04:00
-- this is necessary to prevent 1/0 errors
2021-07-12 23:49:52 -04:00
if cl.total > cl.total_prev then
2021-07-13 01:12:34 -04:00
local p = (cl.active - cl.active_prev) / (cl.total - cl.total_prev)
CompoundDial.set(core.dials, t, p)
load_sum = load_sum + p
2021-07-12 23:49:52 -04:00
end
end
2021-07-13 01:01:21 -04:00
Common.text_ring_set(
core.text_ring,
cr,
__math_floor(0.001 * Util.read_file(core.coretemp_path, nil, '*n'))
)
2018-01-05 23:36:50 -05:00
end
-- 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 trigger == 0 then
Common.text_rows_set(cpu_status, cr, 1, _read_hwp())
end
2021-07-13 00:53:41 -04:00
Common.text_rows_set(cpu_status, cr, 2, _read_freq())
2018-01-05 23:36:50 -05:00
2021-07-13 01:12:34 -04:00
Common.percent_plot_set(total_load, cr, load_sum / NCPU * 100)
2018-01-05 23:36:50 -05:00
2018-08-04 17:54:34 -04:00
for r = 1, NUM_ROWS do
local pid = conky(TABLE_CONKY[r].pid, '(%d+)') -- may have leading spaces
2019-09-01 15:34:10 -04:00
if pid ~= '' then
local cpu = conky(TABLE_CONKY[r].cpu)
2019-10-12 11:42:44 -04:00
local comm = Util.read_file('/proc/'..pid..'/comm', '(%C+)')
2019-09-01 15:34:10 -04:00
Table.set(tbl, cr, 1, r, comm)
Table.set(tbl, cr, 2, r, pid)
Table.set(tbl, cr, 3, r, cpu)
end
2018-01-05 23:36:50 -05:00
end
2017-07-19 00:36:15 -04:00
end
_MODULE_Y_ = nil
_DIAL_INNER_RADIUS_ = nil
_DIAL_OUTER_RADIUS_ = nil
2021-07-11 12:58:51 -04:00
_DIAL_THICKNESS_ = nil
2017-07-19 00:36:15 -04:00
_TEXT_Y_OFFSET_ = nil
_SEPARATOR_SPACING_ = nil
2017-10-23 02:40:12 -04:00
_TEXT_SPACING_ = nil
2017-07-19 00:36:15 -04:00
_PLOT_SECTION_BREAK_ = nil
_PLOT_HEIGHT_ = nil
_TABLE_SECTION_BREAK_ = nil
_TABLE_HEIGHT_ = nil
_create_core_ = nil
2017-10-23 02:40:12 -04:00
_FREQ_Y_ = nil
2017-07-19 00:36:15 -04:00
_LOAD_Y_ = nil
_SEP_Y_ = nil
2020-01-28 20:06:56 -05:00
_HWP_Y_ = nil
2017-07-19 00:36:15 -04:00
_PLOT_Y_ = nil
M.draw_static = function(cr)
Common.drawHeader(cr, header)
2019-09-01 15:34:10 -04:00
for c = 1, NUM_PHYSICAL_CORES do
local this_core = cores[c]
Common.text_ring_draw_static(this_core.text_ring, cr)
CompoundDial.draw_static(this_core.dials, cr)
end
Common.text_rows_draw_static(cpu_status, cr)
Line.draw(separator, cr)
Common.percent_plot_draw_static(total_load, cr)
2018-08-05 11:56:11 -04:00
Table.draw_static(tbl, cr)
end
M.draw_dynamic = function(cr, trigger)
update(cr, trigger)
2018-01-05 23:36:50 -05:00
2018-08-05 11:08:37 -04:00
for c = 1, NUM_PHYSICAL_CORES do
local this_core = cores[c]
CompoundDial.draw_dynamic(this_core.dials, cr)
Common.text_ring_draw_dynamic(this_core.text_ring, cr)
2018-08-05 11:08:37 -04:00
end
2018-01-05 23:36:50 -05:00
Common.text_rows_draw_dynamic(cpu_status, cr)
Common.percent_plot_draw_dynamic(total_load, cr)
2019-09-01 15:34:10 -04:00
2018-08-05 11:56:11 -04:00
Table.draw_dynamic(tbl, cr)
2017-07-19 00:36:15 -04:00
end
return M