conky-config/drawing/memory.lua

191 lines
5.2 KiB
Lua
Raw Normal View History

2021-07-29 22:37:30 -04:00
local timeseries = require 'timeseries'
local texttable = require 'texttable'
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.make_header(
geometry.RIGHT_X,
2021-07-17 23:45:12 -04:00
MODULE_Y,
geometry.SECTION_WIDTH,
2021-07-17 23:45:12 -04:00
'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+)'))
2021-07-19 21:14:38 -04:00
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
2021-07-19 21:14:38 -04:00
local MEM_Y = header.bottom_y + DIAL_RADIUS + DIAL_THICKNESS / 2
local DIAL_DIAMETER = DIAL_RADIUS * 2 + DIAL_THICKNESS
2021-07-30 22:02:46 -04:00
local mem = common.make_dial(
2021-07-19 21:14:38 -04:00
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
2021-07-30 22:02:46 -04:00
local swap = common.make_dial(
2021-07-19 21:14:38 -04:00
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-19 21:14:38 -04:00
local cache = common.make_text_rows_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
2021-07-30 22:46:20 -04:00
local plot = common.make_percent_timeseries(
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,
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.make_text_table(
geometry.RIGHT_X,
2021-07-17 23:45:12 -04:00
PLOT_Y + PLOT_HEIGHT + TABLE_SECTION_BREAK,
geometry.SECTION_WIDTH,
2021-07-17 23:45:12 -04:00
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
= __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
common.dial_set(mem, used_percent)
common.dial_set(swap, (swaptotal - swapfree) / swaptotal)
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-29 22:37:30 -04:00
timeseries.update(plot, used_percent)
for r = 1, NUM_ROWS do
2021-07-29 22:37:30 -04:00
texttable.set(tbl, 1, r, conky(TABLE_CONKY[r].comm, '(%S+)'))
texttable.set(tbl, 2, r, conky(TABLE_CONKY[r].pid))
texttable.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.draw_header(cr, header)
common.dial_draw_static(mem, cr)
common.dial_draw_static(swap, cr)
common.text_rows_draw_static(cache, cr)
2021-07-29 22:37:30 -04:00
timeseries.draw_static(plot, cr)
texttable.draw_static(tbl, cr)
end
local draw_dynamic = function(cr)
common.dial_draw_dynamic(mem, cr)
common.dial_draw_dynamic(swap, cr)
common.text_rows_draw_dynamic(cache, cr)
2021-07-29 22:37:30 -04:00
timeseries.draw_dynamic(plot, cr)
texttable.draw_dynamic(tbl, cr)
end
2017-07-19 00:36:15 -04:00
return {dynamic = draw_dynamic, static = draw_static, update = update}
end