Skip to content

Commit

Permalink
update widget.memory, used = neofetch, htop
Browse files Browse the repository at this point in the history
Cached includes tmpfs & shmem.

ref:
1. neofetch get_memory()
2. kernel Documentation/filesystems/proc.rst
3. lcpz/lain#271
  • Loading branch information
shmilee committed May 22, 2024
1 parent 3ddc4d4 commit ea6fba2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
3 changes: 3 additions & 0 deletions readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ Memory
mem = away.widget.memory({
theme = theme,
timeout = 2,
-- calculate mem.now.perc, mem.now.used by mem.now.info {MemTotal ...}
--calculate = function(mem) end,
-- setting mem.now.text, mem.now.notification_text
--setting = function(mem) end,
})
Expand Down
87 changes: 57 additions & 30 deletions widget/memory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,84 @@ local spawn = require("awful.spawn")

local math = { floor = math.floor }
local string = { format = string.format, match = string.match, gmatch = string.gmatch }
local ipairs, tonumber = ipairs, tonumber

-- Memory infos, aligned to htop, neofetch, free -h
-- Memory infos, aligned to htop, neofetch, or top
-- Ref:
-- 1. neofetch
-- 2. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/proc.rst?id=HEAD#n1052
-- Cached includes tmpfs & shmem.
-- Others: https://www.baeldung.com/linux/proc-meminfo
-- https://github.com/lcpz/lain/issues/271
local function worker(args)
local args = args or {}
local theme = args.theme or {}
local theme = args.theme or {}
args.timeout = args.timeout or 2
args.font = args.font or nil
local calculate = args.calculate or function(mem)
local I = mem.now.info
if I.MemTotal then
mem.now.used = I.MemTotal - I.MemFree - I.Buffers - I.Cached+I.Shmem - I.SReclaimable
mem.now.perc = mem.now.used / I.MemTotal * 100
else
mem.now.perc, mem.now.used = 0, 0
end
end
local noti_keys = args.noti_keys or {
'Used', -- for mem.now.used
'MemTotal', 'MemFree', 'Buffers', 'Cached', 'Shmem', 'SReclaimable',
'MemAvailable', -- 'PageTables',
--'SwapTotal', 'SwapFree', 'SwapCached',
}
local setting = args.setting or function(mem)
-- setting, mem.now.text
if mem.now.used / 1024 > 1 then
mem.now.text = util.markup_span(string.format("%.2fG(%.0f%%)", mem.now.used/1024, mem.now.perc), theme.mem_high_color or "#e0da37")
if mem.now.perc > 40 then
mem.now.text = util.markup_span(string.format("%.0f%%", mem.now.perc), theme.mem_high_color or "#e0da37")
else
mem.now.text = string.format("%sM(%.0f%%)", mem.now.used, mem.now.perc)
mem.now.text = string.format("%.0f%%", mem.now.perc)
end
local noti, line = {}, nil
for i, k in ipairs(noti_keys) do
local v = mem.now.info[k] -- kB
if k == 'Used' then
k = string.format('%.1f%% %s', mem.now.perc, k)
v = mem.now.used
end
if v ~=nil then
if v < 1024 then
line = string.format('%12s: %.0fK', k, v)
elseif v < 1024*1024 then
line = string.format('%12s: %.1fM', k, v/1024)
else
line = string.format('%12s: %.3fG', k, v/1024/1024)
end
table.insert(noti, line)
end
end
mem.wtext:set_markup(mem.now.text)
mem.now.notification_text = table.concat(noti, '\n')
end

-- get now: {total, free, buf, cache, swap, swapf, share, srec,
-- used, swapused, perc} in MB
-- get now.info: {MemTotal, MemFree, Buffers, Cached, Shmem, SReclaimable, etc.} kB
args.update = args.update or function (mem)
spawn.easy_async('cat /proc/meminfo', function(stdout, stderr, reason, exit_code)
if exit_code ~= 0 then
mem.now.used = -1
mem.now.perc = -1
setting(mem)
return
end
for line in string.gmatch(stdout, "[^\r\n]+") do
-- ref: https://github.com/lcpz/lain/issues/271
local k, v = string.match(line, "([%a]+):[%s]+([%d]+).+")
if k == "MemTotal" then mem.now.total = math.floor(v / 1024 + 0.5)
elseif k == "MemFree" then mem.now.free = math.floor(v / 1024 + 0.5)
elseif k == "Buffers" then mem.now.buf = math.floor(v / 1024 + 0.5)
elseif k == "Cached" then mem.now.cache = math.floor(v / 1024 + 0.5)
elseif k == "SwapTotal" then mem.now.swap = math.floor(v / 1024 + 0.5)
elseif k == "SwapFree" then mem.now.swapf = math.floor(v / 1024 + 0.5)
--elseif k == "Shmem" then mem.now.share = math.floor(v / 1024 + 0.5)
elseif k == "SReclaimable" then mem.now.srec = math.floor(v / 1024 + 0.5)
mem.now.info = {}
if exit_code == 0 then
for line in string.gmatch(stdout, "[^\r\n]+") do
local k, v = string.match(line, "([%a%d()_]+):[%s]+([%d]+).*")
mem.now.info[k] = tonumber(v)
end
end
mem.now.used = mem.now.total --+ mem.now.share
- mem.now.free - mem.now.buf - mem.now.cache - mem.now.srec
mem.now.swapused = mem.now.swap - mem.now.swapf
mem.now.perc = math.floor(mem.now.used / mem.now.total * 100)
calculate(mem)
setting(mem)
mem.wtext:set_markup(mem.now.text)
end)
end

local mem = core.worker(args)
local mem = core.popup_worker(args)
if theme.mem then
mem.wicon:set_image(theme.mem)
end
mem.now.notification_icon = args.notification_icon or theme.mem
mem.timer:emit_signal('timeout')

return mem
Expand Down

0 comments on commit ea6fba2

Please sign in to comment.