REF make memory module kinda pretty

This commit is contained in:
Nathan Dwarshuis 2021-07-17 23:45:12 -04:00
parent 2fb3cb2aaa
commit c9d1c7f939
1 changed files with 136 additions and 136 deletions

View File

@ -7,20 +7,16 @@ local Common = require 'Common'
local Theme = require 'Patterns' local Theme = require 'Patterns'
local Geometry = require 'Geometry' local Geometry = require 'Geometry'
local __string_match = string.match return function(update_freq)
local __cairo_path_destroy = cairo_path_destroy local MODULE_Y = 712
local DIAL_THICKNESS = 8
local _MODULE_Y_ = 712 local TEXT_Y_OFFSET = 7
local _DIAL_THICKNESS_ = 8 local TEXT_LEFT_X_OFFSET = 30
local _TEXT_Y_OFFSET_ = 7 local TEXT_SPACING = 20
local _TEXT_LEFT_X_OFFSET_ = 30 local PLOT_SECTION_BREAK = 30
local _TEXT_SPACING_ = 20 local PLOT_HEIGHT = 56
local _PLOT_SECTION_BREAK_ = 30 local TABLE_SECTION_BREAK = 20
local _PLOT_HEIGHT_ = 56 local TABLE_HEIGHT = 114
local _TABLE_SECTION_BREAK_ = 20
local _TABLE_HEIGHT_ = 114
local MEM_TOTAL_KB = tonumber(Util.read_file('/proc/meminfo', '^MemTotal:%s+(%d+)'))
local MEMINFO_REGEX = '\nMemFree:%s+(%d+).+'.. local MEMINFO_REGEX = '\nMemFree:%s+(%d+).+'..
'\nBuffers:%s+(%d+).+'.. '\nBuffers:%s+(%d+).+'..
@ -29,123 +25,130 @@ local MEMINFO_REGEX = '\nMemFree:%s+(%d+).+'..
'\nSwapFree:%s+(%d+).+'.. '\nSwapFree:%s+(%d+).+'..
'\nSReclaimable:%s+(%d+)' '\nSReclaimable:%s+(%d+)'
local NUM_ROWS = 5 local __string_match = string.match
local __cairo_path_destroy = cairo_path_destroy
local TABLE_CONKY = {} -----------------------------------------------------------------------------
-- header
for r = 1, NUM_ROWS do
TABLE_CONKY[r] = {}
TABLE_CONKY[r].comm = '${top_mem name '..r..'}'
TABLE_CONKY[r].pid = '${top_mem pid '..r..'}'
TABLE_CONKY[r].mem = '${top_mem mem '..r..'}'
end
local header = Common.Header( local header = Common.Header(
Geometry.RIGHT_X, Geometry.RIGHT_X,
_MODULE_Y_, MODULE_Y,
Geometry.SECTION_WIDTH, Geometry.SECTION_WIDTH,
'MEMORY' 'MEMORY'
) )
-----------------------------------------------------------------------------
-- mem consumption dial
local mem_total_kb = tonumber(Util.read_file('/proc/meminfo', '^MemTotal:%s+(%d+)'))
local DIAL_RADIUS = 32 local DIAL_RADIUS = 32
local DIAL_THETA_0 = math.rad(90) local DIAL_THETA_0 = math.rad(90)
local DIAL_THETA_1 = math.rad(360) local DIAL_THETA_1 = math.rad(360)
local DIAL_X = Geometry.RIGHT_X + DIAL_RADIUS + _DIAL_THICKNESS_ / 2 local DIAL_X = Geometry.RIGHT_X + DIAL_RADIUS + DIAL_THICKNESS / 2
local DIAL_Y = header.bottom_y + DIAL_RADIUS + _DIAL_THICKNESS_ / 2 local DIAL_Y = header.bottom_y + DIAL_RADIUS + DIAL_THICKNESS / 2
local dial = Common.dial(DIAL_X, DIAL_Y, DIAL_RADIUS, _DIAL_THICKNESS_, 0.8) local dial = Common.dial(DIAL_X, DIAL_Y, DIAL_RADIUS, DIAL_THICKNESS, 0.8)
local cache_arc = Common.arc( local cache_arc = Common.arc(
DIAL_X, DIAL_X,
DIAL_Y, DIAL_Y,
DIAL_RADIUS, DIAL_RADIUS,
_DIAL_THICKNESS_, DIAL_THICKNESS,
Theme.INDICATOR_FG_SECONDARY Theme.INDICATOR_FG_SECONDARY
) )
local text_ring = Common.initTextRing( local text_ring = Common.initTextRing(
DIAL_X, DIAL_X,
DIAL_Y, DIAL_Y,
DIAL_RADIUS - _DIAL_THICKNESS_ / 2 - 2, DIAL_RADIUS - DIAL_THICKNESS / 2 - 2,
'%s%%', '%s%%',
80 80
) )
local _LINE_1_Y_ = header.bottom_y + _TEXT_Y_OFFSET_ -----------------------------------------------------------------------------
local _TEXT_LEFT_X_ = Geometry.RIGHT_X + DIAL_RADIUS * 2 + _TEXT_LEFT_X_OFFSET_ -- swap/buffers stats
local _RIGHT_X_ = Geometry.RIGHT_X + Geometry.SECTION_WIDTH
local LINE_1_Y = header.bottom_y + TEXT_Y_OFFSET
local TEXT_LEFT_X = Geometry.RIGHT_X + DIAL_RADIUS * 2 + TEXT_LEFT_X_OFFSET
local SWAP_BUFFERS_WIDTH = Geometry.SECTION_WIDTH - TEXT_LEFT_X_OFFSET
- DIAL_RADIUS * 2
local swap = Common.initTextRowCrit( local swap = Common.initTextRowCrit(
_TEXT_LEFT_X_, TEXT_LEFT_X,
_LINE_1_Y_, LINE_1_Y,
-- TODO this is silly SWAP_BUFFERS_WIDTH,
_RIGHT_X_ - _TEXT_LEFT_X_,
'Swap Usage', 'Swap Usage',
'%s%%', '%s%%',
80 80
) )
local cache = Common.initTextRows_color( local cache = Common.initTextRows_color(
_TEXT_LEFT_X_, TEXT_LEFT_X,
_LINE_1_Y_ + _TEXT_SPACING_, LINE_1_Y + TEXT_SPACING,
Geometry.SECTION_WIDTH - _TEXT_LEFT_X_OFFSET_ - DIAL_RADIUS * 2, SWAP_BUFFERS_WIDTH,
_TEXT_SPACING_, TEXT_SPACING,
{'Page Cache', 'Buffers', 'Kernel Slab'}, {'Page Cache', 'Buffers', 'Kernel Slab'},
Theme.SECONDARY_FG, Theme.SECONDARY_FG,
'%s%%' '%s%%'
) )
local _PLOT_Y_ = _PLOT_SECTION_BREAK_ + header.bottom_y + DIAL_RADIUS * 2 -----------------------------------------------------------------------------
-- memory consumption plot
local PLOT_Y = PLOT_SECTION_BREAK + header.bottom_y + DIAL_RADIUS * 2
local plot = Common.initThemedLabelPlot(
Geometry.RIGHT_X,
PLOT_Y,
Geometry.SECTION_WIDTH,
PLOT_HEIGHT,
Common.percent_label_style,
update_freq
)
-----------------------------------------------------------------------------
-- memory top table
local NUM_ROWS = 5
local TABLE_CONKY = {}
for r = 1, NUM_ROWS do
TABLE_CONKY[r] = {
comm = '${top_mem name '..r..'}',
pid = '${top_mem pid '..r..'}',
mem = '${top_mem mem '..r..'}',
}
end
local tbl = Common.initTable( local tbl = Common.initTable(
Geometry.RIGHT_X, Geometry.RIGHT_X,
_PLOT_Y_ + _PLOT_HEIGHT_ + _TABLE_SECTION_BREAK_, PLOT_Y + PLOT_HEIGHT + TABLE_SECTION_BREAK,
Geometry.SECTION_WIDTH, Geometry.SECTION_WIDTH,
_TABLE_HEIGHT_, TABLE_HEIGHT,
NUM_ROWS, NUM_ROWS,
{'Name', 'PID', 'Mem (%)'} {'Name', 'PID', 'Mem (%)'}
) )
-- _MODULE_Y_ = nil -----------------------------------------------------------------------------
-- _DIAL_THICKNESS_ = nil -- main functions
-- _DIAL_SPACING_ = nil
-- _TEXT_Y_OFFSET_ = nil
-- _TEXT_LEFT_X_OFFSET_ = nil
-- _TEXT_SPACING_ = nil
-- _PLOT_SECTION_BREAK_ = nil
-- _PLOT_HEIGHT_ = nil
-- _TABLE_SECTION_BREAK_ = nil
-- _TABLE_HEIGHT_ = nil
-- _LINE_1_Y_ = nil
-- _TEXT_LEFT_X_ = nil
-- _RIGHT_X_ = nil
-- _PLOT_Y_ = nil
return function(update_freq)
local plot = Common.initThemedLabelPlot(
Geometry.RIGHT_X,
_PLOT_Y_,
Geometry.SECTION_WIDTH,
_PLOT_HEIGHT_,
Common.percent_label_style,
update_freq
)
local update = function(cr) local update = function(cr)
local conky = Util.conky local conky = Util.conky
-- see source for the 'free' command (sysinfo.c) for formulas -- see source for the 'free' command (sysinfo.c) for formulas
local memfree_kb, buffers_kb, cached_kb, swap_total_kb, swap_free_kb, local memfree_kb,
slab_reclaimable_kb = __string_match(Util.read_file('/proc/meminfo'), MEMINFO_REGEX) buffers_kb,
cached_kb,
swap_total_kb,
swap_free_kb,
slab_reclaimable_kb
= __string_match(Util.read_file('/proc/meminfo'), MEMINFO_REGEX)
local used_percent = (MEM_TOTAL_KB - memfree_kb - cached_kb - buffers_kb - slab_reclaimable_kb) / MEM_TOTAL_KB local used_percent = (mem_total_kb - memfree_kb - cached_kb - buffers_kb - slab_reclaimable_kb) / mem_total_kb
Dial.set(dial, used_percent) Dial.set(dial, used_percent)
Common.text_ring_set(text_ring, cr, Util.round_to_string(used_percent * 100)) Common.text_ring_set(text_ring, cr, Util.round_to_string(used_percent * 100))
local cache_theta = (DIAL_THETA_0 - DIAL_THETA_1) / MEM_TOTAL_KB * memfree_kb + DIAL_THETA_1 local cache_theta = (DIAL_THETA_0 - DIAL_THETA_1) / mem_total_kb * memfree_kb + DIAL_THETA_1
__cairo_path_destroy(cache_arc.path) __cairo_path_destroy(cache_arc.path)
cache_arc.path = Arc.create_path(cr, DIAL_X, DIAL_Y, DIAL_RADIUS, dial.dial_angle, cache_theta) cache_arc.path = Arc.create_path(cr, DIAL_X, DIAL_Y, DIAL_RADIUS, dial.dial_angle, cache_theta)
@ -155,23 +158,20 @@ return function(update_freq)
/ swap_total_kb * 100)) / swap_total_kb * 100))
Common.text_rows_set(cache, cr, 1, Util.precision_round_to_string( Common.text_rows_set(cache, cr, 1, Util.precision_round_to_string(
cached_kb / MEM_TOTAL_KB * 100)) cached_kb / mem_total_kb * 100))
Common.text_rows_set(cache, cr, 2, Util.precision_round_to_string( Common.text_rows_set(cache, cr, 2, Util.precision_round_to_string(
buffers_kb / MEM_TOTAL_KB * 100)) buffers_kb / mem_total_kb * 100))
Common.text_rows_set(cache, cr, 3, Util.precision_round_to_string( Common.text_rows_set(cache, cr, 3, Util.precision_round_to_string(
slab_reclaimable_kb / MEM_TOTAL_KB * 100)) slab_reclaimable_kb / mem_total_kb * 100))
LabelPlot.update(plot, used_percent) LabelPlot.update(plot, used_percent)
for r = 1, NUM_ROWS do for r = 1, NUM_ROWS do
local comm = conky(TABLE_CONKY[r].comm, '(%S+)') -- may have trailing space Table.set(tbl, cr, 1, r, conky(TABLE_CONKY[r].comm, '(%S+)'))
local pid = conky(TABLE_CONKY[r].pid) Table.set(tbl, cr, 2, r, conky(TABLE_CONKY[r].pid))
local mem = conky(TABLE_CONKY[r].mem) Table.set(tbl, cr, 3, r, conky(TABLE_CONKY[r].mem))
Table.set(tbl, cr, 1, r, comm)
Table.set(tbl, cr, 2, r, pid)
Table.set(tbl, cr, 3, r, mem)
end end
end end