conky-config/module/Processor.lua

249 lines
6.0 KiB
Lua
Raw Normal View History

2016-08-11 23:25:56 -04:00
local Widget = require 'Widget'
local Arc = require 'Arc'
local CompoundDial = require 'CompoundDial'
local CriticalText = require 'CriticalText'
local Text = require 'Text'
local Line = require 'Line'
local LabelPlot = require 'LabelPlot'
local Table = require 'Table'
local util = require 'util'
local schema = require 'default_patterns'
2017-07-15 20:08:39 -04:00
local CORETEMP_PATH = '/sys/devices/platform/coretemp.0/hwmon/hwmon%i/%s'
2016-08-11 23:25:56 -04:00
2017-07-15 20:08:39 -04:00
local NUM_PHYSICAL_CORES = 4
2017-07-16 14:54:03 -04:00
local NUM_THREADS_PER_CORE = 2
2016-08-11 23:25:56 -04:00
2017-07-15 20:08:39 -04:00
local NUM_ROWS = 5
2016-08-11 23:25:56 -04:00
local TABLE_CONKY = {{}, {}, {}}
2017-07-15 20:08:39 -04:00
for r = 1, NUM_ROWS do
2016-08-11 23:25:56 -04:00
TABLE_CONKY[1][r] = '${top name '..r..'}'
TABLE_CONKY[2][r] = '${top pid '..r..'}'
TABLE_CONKY[3][r] = '${top cpu '..r..'}'
end
2017-07-16 14:50:33 -04:00
local _MODULE_Y_ = 636
local _DIAL_INNER_RADIUS_ = 30
local _DIAL_OUTER_RADIUS_ = 42
local _DIAL_SPACING_ = 1
local _TEXT_Y_OFFSET_ = 15
local _SEPARATOR_SPACING_ = 20
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)
2017-07-15 20:08:39 -04:00
local conky_threads = {}
2017-07-16 14:54:03 -04:00
for c = 0, NUM_PHYSICAL_CORES * NUM_THREADS_PER_CORE - 1 do
2017-07-15 20:08:39 -04:00
if util.read_file('/sys/devices/system/cpu/cpu'..c..'/topology/core_id', nil, '*n') == id then
table.insert(conky_threads, '${cpu cpu'..c..'}')
end
end
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] = {
dials = Widget.CompoundDial{
x = x,
y = y,
2017-07-16 14:50:33 -04:00
inner_radius = _DIAL_INNER_RADIUS_,
outer_radius = _DIAL_OUTER_RADIUS_,
spacing = _DIAL_SPACING_,
2017-07-16 14:54:03 -04:00
num_dials = NUM_THREADS_PER_CORE,
2017-07-15 20:08:39 -04:00
critical_limit = '>0.8'
},
inner_ring = Widget.Arc{
x = x,
y = y,
2017-07-16 14:50:33 -04:00
radius = _DIAL_INNER_RADIUS_ - 2,
2017-07-15 20:08:39 -04:00
theta0 = 0,
theta1 = 360
},
coretemp_text = Widget.CriticalText{
x = x,
y = y,
x_align = 'center',
y_align = 'center',
append_end = '°C',
critical_limit = '>90'
},
coretemp_path = string.format(CORETEMP_PATH, hwmon_index, 'temp'..(id + 2)..'_input'),
conky_threads = conky_threads
}
end
2016-08-11 23:25:56 -04:00
local header = Widget.Header{
2017-07-16 14:50:33 -04:00
x = _G_INIT_DATA_.LEFT_X,
y = _MODULE_Y_,
width = _G_INIT_DATA_.SECTION_WIDTH,
2016-08-11 23:25:56 -04:00
header = "PROCESSOR"
}
2017-07-15 20:08:39 -04:00
--we assume that this cpu has 4 physical cores with 2 logical each
local cores = {}
2016-08-11 23:25:56 -04:00
2017-07-15 20:08:39 -04:00
for c = 0, NUM_PHYSICAL_CORES - 1 do
2017-07-16 14:50:33 -04:00
local dial_x = _G_INIT_DATA_.LEFT_X + _DIAL_OUTER_RADIUS_ +
(_G_INIT_DATA_.SECTION_WIDTH - 2 * _DIAL_OUTER_RADIUS_) * c / 3
local dial_y = header.bottom_y + _DIAL_OUTER_RADIUS_
_create_core_(cores, c, dial_x, dial_y)
2017-07-15 20:08:39 -04:00
end
2016-08-11 23:25:56 -04:00
2017-07-16 14:50:33 -04:00
local _RIGHT_X_ = _G_INIT_DATA_.LEFT_X + _G_INIT_DATA_.SECTION_WIDTH
2017-07-15 20:08:39 -04:00
2017-07-16 14:50:33 -04:00
local _PROCESS_Y_ = header.bottom_y + _DIAL_OUTER_RADIUS_ * 2 + _PLOT_SECTION_BREAK_
2017-07-15 20:08:39 -04:00
local process = {
labels = Widget.Text{
2017-07-16 14:50:33 -04:00
x = _G_INIT_DATA_.LEFT_X,
y = _PROCESS_Y_,
2017-07-15 20:08:39 -04:00
text = 'R | S | D | T | Z'
2016-08-11 23:25:56 -04:00
},
2017-07-15 20:08:39 -04:00
values = Widget.Text{
2017-07-16 14:50:33 -04:00
x = _RIGHT_X_,
y = _PROCESS_Y_,
2016-08-11 23:25:56 -04:00
x_align = 'right',
2017-07-15 20:08:39 -04:00
text_color = schema.blue,
text = '<R.S.D.T.Z>'
2016-08-11 23:25:56 -04:00
}
}
2017-07-16 14:50:33 -04:00
local _SEP_Y_ = _PROCESS_Y_ + _SEPARATOR_SPACING_
2016-08-11 23:25:56 -04:00
local separator = Widget.Line{
2017-07-16 14:50:33 -04:00
p1 = {x = _G_INIT_DATA_.LEFT_X, y = _SEP_Y_},
p2 = {x = _RIGHT_X_, y = _SEP_Y_}
2016-08-11 23:25:56 -04:00
}
2017-07-16 14:50:33 -04:00
local _LOAD_Y_ = _SEP_Y_ + _SEPARATOR_SPACING_
2016-08-11 23:25:56 -04:00
2017-07-15 20:08:39 -04:00
local total_load = {
label = Widget.Text{
2017-07-16 14:50:33 -04:00
x = _G_INIT_DATA_.LEFT_X,
y = _LOAD_Y_,
2017-07-15 20:08:39 -04:00
text = 'Total Load'
2016-08-11 23:25:56 -04:00
},
2017-07-15 20:08:39 -04:00
value = Widget.CriticalText{
2017-07-16 14:50:33 -04:00
x = _RIGHT_X_,
y = _LOAD_Y_,
2017-07-15 20:08:39 -04:00
x_align = 'right',
append_end = '%',
critical_limit = '>80'
}
2016-08-11 23:25:56 -04:00
}
2017-07-16 14:50:33 -04:00
local _PLOT_Y_ = _LOAD_Y_ + _PLOT_SECTION_BREAK_
2016-08-11 23:25:56 -04:00
local plot = Widget.LabelPlot{
2017-07-16 14:50:33 -04:00
x = _G_INIT_DATA_.LEFT_X,
y = _PLOT_Y_,
width = _G_INIT_DATA_.SECTION_WIDTH,
height = _PLOT_HEIGHT_
2016-08-11 23:25:56 -04:00
}
local tbl = Widget.Table{
2017-07-16 14:50:33 -04:00
x = _G_INIT_DATA_.LEFT_X,
y = _PLOT_Y_ + _PLOT_HEIGHT_ + _TABLE_SECTION_BREAK_,
width = _G_INIT_DATA_.SECTION_WIDTH,
height = _TABLE_HEIGHT_,
2017-07-15 20:08:39 -04:00
num_rows = NUM_ROWS,
2016-08-11 23:25:56 -04:00
'Name',
'PID',
'CPU (%)'
}
2017-07-16 14:50:33 -04:00
local update = function(cr)
2016-08-11 23:25:56 -04:00
local conky = util.conky
local char_count = util.char_count
local sum = 0
2017-07-15 20:08:39 -04:00
for c = 1, NUM_PHYSICAL_CORES do
local core = cores[c]
local conky_threads = core.conky_threads
2017-07-16 14:54:03 -04:00
for t = 1, NUM_THREADS_PER_CORE do
2017-07-15 20:08:39 -04:00
local percent = util.conky_numeric(conky_threads[t]) * 0.01
CompoundDial.set(core.dials, t, percent)
sum = sum + percent
end
CriticalText.set(core.coretemp_text, cr, util.round(0.001 * util.read_file(core.coretemp_path, nil, '*n')))
2016-08-11 23:25:56 -04:00
end
local process_glob = util.execute_cmd('ps -A -o s')
2017-07-15 20:08:39 -04:00
--subtract one from running b/c ps will always be "running"
Text.set(process.values, cr, (char_count(process_glob, 'R') - 1)..' | '..
char_count(process_glob, 'S')..' | '..
char_count(process_glob, 'D')..' | '..
char_count(process_glob, 'T')..' | '..
char_count(process_glob, 'Z'))
2016-08-11 23:25:56 -04:00
2017-07-16 14:54:03 -04:00
local load_percent = util.round(sum / NUM_PHYSICAL_CORES / NUM_THREADS_PER_CORE, 2)
2017-07-15 20:08:39 -04:00
CriticalText.set(total_load.value, cr, load_percent * 100)
2016-08-11 23:25:56 -04:00
LabelPlot.update(plot, load_percent)
for c = 1, 3 do
local column = TABLE_CONKY[c]
2017-07-15 20:08:39 -04:00
for r = 1, NUM_ROWS do
2016-08-11 23:25:56 -04:00
Table.set(tbl, cr, c, r, conky(column[r], '(%S+)'))
end
end
end
Widget = nil
schema = nil
2017-07-16 14:50:33 -04:00
_MODULE_Y_ = nil
_DIAL_INNER_RADIUS_ = nil
_DIAL_OUTER_RADIUS_ = nil
_DIAL_SPACING_ = nil
_TEXT_Y_OFFSET_ = nil
_SEPARATOR_SPACING_ = nil
_PLOT_SECTION_BREAK_ = nil
_PLOT_HEIGHT_ = nil
_TABLE_SECTION_BREAK_ = nil
_TABLE_HEIGHT_ = nil
_create_core_ = nil
_LOAD_Y_ = nil
_RIGHT_X_ = nil
_SEP_Y_ = nil
_PROCESS_Y_ = nil
_PLOT_Y_ = nil
2016-08-11 23:25:56 -04:00
local draw = function(cr, current_interface)
2017-07-16 14:50:33 -04:00
update(cr)
2016-08-11 23:25:56 -04:00
if current_interface == 0 then
Text.draw(header.text, cr)
Line.draw(header.underline, cr)
2017-07-15 20:08:39 -04:00
for c = 1, NUM_PHYSICAL_CORES do
local core = cores[c]
CompoundDial.draw(core.dials, cr)
Arc.draw(core.inner_ring, cr)
CriticalText.draw(core.coretemp_text, cr)
end
2016-08-11 23:25:56 -04:00
2017-07-15 20:08:39 -04:00
Text.draw(process.labels, cr)
Text.draw(process.values, cr)
2016-08-11 23:25:56 -04:00
Line.draw(separator, cr)
2017-07-15 20:08:39 -04:00
Text.draw(total_load.label, cr)
CriticalText.draw(total_load.value, cr)
2016-08-11 23:25:56 -04:00
LabelPlot.draw(plot, cr)
Table.draw(tbl, cr)
end
end
return draw