conky-config/drawing/Memory.lua

192 lines
5.2 KiB
Lua
Raw Normal View History

2021-07-18 23:45:16 -04:00
local Timeseries = require 'Timeseries'
2021-07-17 23:45:12 -04:00
local Table = require 'Table'
local Util = require 'Util'
local Common = require 'Common'
local Geometry = require 'Geometry'
2021-07-23 01:41:06 -04:00
local func = require 'func'
2017-07-19 00:36:15 -04:00
2021-07-17 23:45:12 -04:00
return function(update_freq)
local MODULE_Y = 712
local DIAL_THICKNESS = 8
2021-07-19 21:14:38 -04:00
local DIAL_RADIUS = 32
local DIAL_SPACING = 40
local CACHE_Y_OFFSET = 7
local CACHE_X_OFFSET = 50
2021-07-17 23:45:12 -04:00
local TEXT_SPACING = 20
2021-07-19 21:14:38 -04:00
local PLOT_SECTION_BREAK = 22
2021-07-17 23:45:12 -04:00
local PLOT_HEIGHT = 56
local TABLE_SECTION_BREAK = 20
local TABLE_HEIGHT = 114
local MEMINFO_REGEX = '\nMemFree:%s+(%d+).+'..
'\nBuffers:%s+(%d+).+'..
'\nCached:%s+(%d+).+'..
'\nSwapFree:%s+(%d+).+'..
2021-07-19 21:14:38 -04:00
'\nShmem:%s+(%d+).+'..
2021-07-17 23:45:12 -04:00
'\nSReclaimable:%s+(%d+)'
local __string_match = string.match
-----------------------------------------------------------------------------
-- header
local header = Common.Header(
Geometry.RIGHT_X,
MODULE_Y,
Geometry.SECTION_WIDTH,
'MEMORY'
)
2021-07-17 23:45:12 -04:00
-----------------------------------------------------------------------------
-- mem consumption dial
2021-07-19 21:14:38 -04:00
local get_meminfo_field = function(field)
return tonumber(Util.read_file('/proc/meminfo', field..':%s+(%d+)'))
end
local memtotal = get_meminfo_field('MemTotal')
local swaptotal = get_meminfo_field('SwapTotal')
2021-07-19 21:14:38 -04:00
local FORMAT_PERCENT = function(x)
return string.format('%.0f%%', x * 100)
end
local MEM_X = Geometry.RIGHT_X + DIAL_RADIUS + DIAL_THICKNESS / 2
local MEM_Y = header.bottom_y + DIAL_RADIUS + DIAL_THICKNESS / 2
local DIAL_DIAMETER = DIAL_RADIUS * 2 + DIAL_THICKNESS
local mem = Common.dial(
MEM_X,
MEM_Y,
DIAL_RADIUS,
DIAL_THICKNESS,
0.8,
FORMAT_PERCENT
2021-07-17 23:45:12 -04:00
)
-----------------------------------------------------------------------------
2021-07-19 21:14:38 -04:00
-- swap consumption dial
2021-07-17 23:45:12 -04:00
2021-07-19 21:14:38 -04:00
local SWAP_X = MEM_X + DIAL_DIAMETER + DIAL_SPACING
local swap = Common.dial(
SWAP_X,
MEM_Y,
DIAL_RADIUS,
DIAL_THICKNESS,
0.8,
FORMAT_PERCENT
2021-07-17 23:45:12 -04:00
)
2021-07-19 21:14:38 -04:00
-----------------------------------------------------------------------------
-- swap/buffers stats
local CACHE_Y = header.bottom_y + CACHE_Y_OFFSET
local CACHE_X = SWAP_X + CACHE_X_OFFSET + DIAL_DIAMETER / 2
local CACHE_WIDTH = Geometry.RIGHT_X + Geometry.SECTION_WIDTH - CACHE_X
2021-07-18 14:38:41 -04:00
local cache = Common.initTextRows_formatted(
2021-07-19 21:14:38 -04:00
CACHE_X,
CACHE_Y,
CACHE_WIDTH,
2021-07-17 23:45:12 -04:00
TEXT_SPACING,
2021-07-19 21:14:38 -04:00
{'Page Cache', 'Buffers', 'Shared', 'Kernel Slab'},
2021-07-19 01:34:03 -04:00
'%.1f%%'
2021-07-17 23:45:12 -04:00
)
-----------------------------------------------------------------------------
-- memory consumption plot
2021-07-19 21:14:38 -04:00
local PLOT_Y = header.bottom_y + PLOT_SECTION_BREAK + DIAL_DIAMETER
local plot = Common.initThemedLabelPlot(
Geometry.RIGHT_X,
2021-07-17 23:45:12 -04:00
PLOT_Y,
Geometry.SECTION_WIDTH,
2021-07-17 23:45:12 -04:00
PLOT_HEIGHT,
2021-07-24 01:02:12 -04:00
Common.percent_label_config,
update_freq
)
2021-07-17 23:45:12 -04:00
-----------------------------------------------------------------------------
-- memory top table
local NUM_ROWS = 5
2021-07-23 01:41:06 -04:00
local TABLE_CONKY = func.map(
function(i)
return {
comm = '${top_mem name '..i..'}',
pid = '${top_mem pid '..i..'}',
mem = '${top_mem mem '..i..'}',
}
end,
func.seq(NUM_ROWS))
2021-07-17 23:45:12 -04:00
local tbl = Common.initTable(
Geometry.RIGHT_X,
PLOT_Y + PLOT_HEIGHT + TABLE_SECTION_BREAK,
Geometry.SECTION_WIDTH,
TABLE_HEIGHT,
NUM_ROWS,
{'Name', 'PID', 'Mem (%)'}
)
-----------------------------------------------------------------------------
-- main functions
2021-07-26 23:45:54 -04:00
local update = function()
local conky = Util.conky
2021-07-19 21:14:38 -04:00
-- see manpage for free command for formulas
local memfree,
buffers,
cached,
swapfree,
shmem,
sreclaimable
2021-07-17 23:45:12 -04:00
= __string_match(Util.read_file('/proc/meminfo'), MEMINFO_REGEX)
2021-07-19 01:37:47 -04:00
local used_percent =
2021-07-19 21:14:38 -04:00
(memtotal -
memfree -
cached -
buffers -
sreclaimable) / memtotal
2021-07-26 23:45:54 -04:00
Common.dial_set(mem, used_percent)
Common.dial_set(swap, (swaptotal - swapfree) / swaptotal)
2021-07-26 23:45:54 -04:00
Common.text_rows_set(cache, 1, cached / memtotal * 100)
Common.text_rows_set(cache, 2, buffers / memtotal * 100)
Common.text_rows_set(cache, 3, shmem / memtotal * 100)
Common.text_rows_set(cache, 4, sreclaimable / memtotal * 100)
2021-07-18 23:45:16 -04:00
Timeseries.update(plot, used_percent)
for r = 1, NUM_ROWS do
2021-07-23 00:16:18 -04:00
Table.set(tbl, 1, r, conky(TABLE_CONKY[r].comm, '(%S+)'))
Table.set(tbl, 2, r, conky(TABLE_CONKY[r].pid))
Table.set(tbl, 3, r, conky(TABLE_CONKY[r].mem))
end
end
2017-07-19 00:36:15 -04:00
local draw_static = function(cr)
Common.drawHeader(cr, header)
2021-07-19 21:14:38 -04:00
Common.dial_draw_static(mem, cr)
Common.dial_draw_static(swap, cr)
Common.text_rows_draw_static(cache, cr)
2021-07-18 23:45:16 -04:00
Timeseries.draw_static(plot, cr)
Table.draw_static(tbl, cr)
end
local draw_dynamic = function(cr)
2021-07-19 21:14:38 -04:00
Common.dial_draw_dynamic(mem, cr)
Common.dial_draw_dynamic(swap, cr)
Common.text_rows_draw_dynamic(cache, cr)
2021-07-18 23:45:16 -04:00
Timeseries.draw_dynamic(plot, cr)
Table.draw_dynamic(tbl, cr)
end
2017-07-19 00:36:15 -04:00
return {dynamic = draw_dynamic, static = draw_static, update = update}
end