From ebc3a08c9c1567fc0ace96e435b77286390f00db Mon Sep 17 00:00:00 2001 From: mutlusun Date: Sat, 10 Mar 2018 12:54:05 +0100 Subject: [PATCH 1/4] freebsd memory clean ups and correct calculation --- README.md | 5 ++--- widgets/bat_freebsd.lua | 2 ++ widgets/mem_freebsd.lua | 37 ++++++++++++++++++------------------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 93b1f18..a6818d8 100644 --- a/README.md +++ b/README.md @@ -323,9 +323,8 @@ Supported platforms: Linux, FreeBSD. total system memory, 4th as free memory, 5th as swap usage in percent, 6th as swap usage, 7th as total system swap, 8th as free swap and 9th as memory usage with buffers and cache - * FreeBSD: see above, but 10th value as memory usage with buffers and cache - as percent and 11th value as wired memory (memory used by kernel) is - reported + * FreeBSD: see above, but 9th value is not returned (always `-1`) and there + is a 10th value giving wired memory **vicious.widgets.mpd** diff --git a/widgets/bat_freebsd.lua b/widgets/bat_freebsd.lua index 919ed6b..22e4762 100644 --- a/widgets/bat_freebsd.lua +++ b/widgets/bat_freebsd.lua @@ -30,6 +30,8 @@ local function worker(format, warg) state = "↯" elseif bat_info["State"] == "charging" then state = "+" + elseif bat_info["State"] == "critical charging" then + state = "+" elseif bat_info["State"] == "discharging" then state = "-" else diff --git a/widgets/mem_freebsd.lua b/widgets/mem_freebsd.lua index 618c418..59dc968 100644 --- a/widgets/mem_freebsd.lua +++ b/widgets/mem_freebsd.lua @@ -16,6 +16,7 @@ local function worker(format) local vm_stats = helpers.sysctl_table("vm.stats.vm") local _mem = { buf = {}, total = nil } + -- Get memory space in bytes _mem.total = tonumber(vm_stats.v_page_count) * pagesize _mem.buf.f = tonumber(vm_stats.v_free_count) * pagesize _mem.buf.a = tonumber(vm_stats.v_active_count) * pagesize @@ -23,35 +24,33 @@ local function worker(format) _mem.buf.c = tonumber(vm_stats.v_cache_count) * pagesize _mem.buf.w = tonumber(vm_stats.v_wire_count) * pagesize - -- rework into Megabytes - _mem.total = math.floor(_mem.total/(1024*1024)) - _mem.buf.f = math.floor(_mem.buf.f/(1024*1024)) - _mem.buf.a = math.floor(_mem.buf.a/(1024*1024)) - _mem.buf.i = math.floor(_mem.buf.i/(1024*1024)) - _mem.buf.c = math.floor(_mem.buf.c/(1024*1024)) - _mem.buf.w = math.floor(_mem.buf.w/(1024*1024)) + -- Rework into megabytes + _mem.total = math.floor(_mem.total/1048576) + _mem.buf.f = math.floor(_mem.buf.f/1048576) + _mem.buf.a = math.floor(_mem.buf.a/1048576) + _mem.buf.i = math.floor(_mem.buf.i/1048576) + _mem.buf.c = math.floor(_mem.buf.c/1048576) + _mem.buf.w = math.floor(_mem.buf.w/1048576) -- Calculate memory percentage - _mem.free = _mem.buf.f + _mem.buf.c - _mem.inuse = _mem.buf.a + _mem.buf.i + _mem.free = _mem.buf.f + _mem.buf.c + _mem.buf.i + _mem.inuse = _mem.total - _mem.free _mem.wire = _mem.buf.w - _mem.bcuse = _mem.total - _mem.buf.f _mem.usep = math.floor(_mem.inuse / _mem.total * 100) _mem.inusep= math.floor(_mem.inuse / _mem.total * 100) - _mem.buffp = math.floor(_mem.bcuse / _mem.total * 100) - _mem.wirep = math.floor(_mem.wire / _mem.total * 100) -- Get swap states - local vm = helpers.sysctl_table("vm") + local vm_swap_total = tonumber(helpers.sysctl("vm.swap_total")) + local vm_swap_enabled = tonumber(helpers.sysctl("vm.swap_enabled")) local _swp = { buf = {}, total = nil } - if tonumber(vm.swap_enabled) == 1 and tonumber(vm.swap_total) > 0 then - -- Get swap space - _swp.total = tonumber(vm.swap_total) + if vm_swap_enabled == 1 and vm_swap_total > 0 then + -- Get swap space in bytes + _swp.total = vm_swap_total _swp.buf.f = _swp.total - tonumber(vm_stats.v_swapin) -- Rework into megabytes - _swp.total = math.floor(_swp.total/(1024*1024)) - _swp.buf.f = math.floor(_swp.buf.f/(1024*1024)) + _swp.total = math.floor(_swp.total/1048576) + _swp.buf.f = math.floor(_swp.buf.f/1048576) -- Calculate percentage _swp.inuse = _swp.total - _swp.buf.f _swp.usep = math.floor(_swp.inuse / _swp.total * 100) @@ -64,7 +63,7 @@ local function worker(format) return { _mem.usep, _mem.inuse, _mem.total, _mem.free, _swp.usep, _swp.inuse, _swp.total, _swp.buf.f, - _mem.bcuse, _mem.buffp, _mem.wirep } + -1, _mem.wire } end return setmetatable(mem_freebsd, { __call = function(_, ...) return worker(...) end }) From 6e185bc89ea61869f5d6c2f22ffbdc356cb3cbb3 Mon Sep 17 00:00:00 2001 From: mutlusun Date: Wed, 6 Jun 2018 21:55:36 +0200 Subject: [PATCH 2/4] calculating swap space using swapinfo (more reliable) --- widgets/mem_freebsd.lua | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/widgets/mem_freebsd.lua b/widgets/mem_freebsd.lua index b3c1fae..1044d41 100644 --- a/widgets/mem_freebsd.lua +++ b/widgets/mem_freebsd.lua @@ -47,14 +47,34 @@ local function worker(format) local _swp = { buf = {}, total = nil } if vm_swap_enabled == 1 and vm_swap_total > 0 then - -- Get swap space in bytes - _swp.total = vm_swap_total - _swp.buf.free = _swp.total - tonumber(vm_stats.v_swapin) + -- Initialise variables + _swp.usep = 0 + _swp.inuse = 0 + _swp.total = 0 + _swp.buf.free = 0 + + -- Read output of swapinfo + local f = io.popen("swapinfo") + -- Skip first line (table header) + f:read("*line") + -- Read content and sum up + for line in f:lines() do + local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)") + print(ltotal) + print(lused) + print(lfree) + + -- Add swap space in kbytes + _swp.total = _swp.total + tonumber(ltotal) + _swp.inuse = _swp.inuse + tonumber(lused) + _swp.buf.free = _swp.buf.free + tonumber(lfree) + end + f:close() + -- Rework into megabytes - _swp.total = math.floor(_swp.total/1048576) - _swp.buf.free = math.floor(_swp.buf.free/1048576) + _swp.total = math.floor(_swp.total/1024) + _swp.buf.free = math.floor(_swp.buf.free/1024) -- Calculate percentage - _swp.inuse = _swp.total - _swp.buf.free _swp.usep = math.floor(_swp.inuse / _swp.total * 100) else _swp.usep = -1 From 2e832bc82e53a73607e08d600295f4e20057c2e7 Mon Sep 17 00:00:00 2001 From: mutlusun Date: Wed, 6 Jun 2018 22:46:03 +0200 Subject: [PATCH 3/4] remove print statements (for testing purposes) --- widgets/mem_freebsd.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/widgets/mem_freebsd.lua b/widgets/mem_freebsd.lua index 1044d41..05b0a39 100644 --- a/widgets/mem_freebsd.lua +++ b/widgets/mem_freebsd.lua @@ -60,10 +60,6 @@ local function worker(format) -- Read content and sum up for line in f:lines() do local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)") - print(ltotal) - print(lused) - print(lfree) - -- Add swap space in kbytes _swp.total = _swp.total + tonumber(ltotal) _swp.inuse = _swp.inuse + tonumber(lused) From af979a04f7af265ed8ba08ed86f2c3614548841b Mon Sep 17 00:00:00 2001 From: mutlusun Date: Wed, 20 Jun 2018 22:53:16 +0200 Subject: [PATCH 4/4] read swapinfo in megabytes --- widgets/mem_freebsd.lua | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/widgets/mem_freebsd.lua b/widgets/mem_freebsd.lua index 05b0a39..66dbbaf 100644 --- a/widgets/mem_freebsd.lua +++ b/widgets/mem_freebsd.lua @@ -2,6 +2,7 @@ local tonumber = tonumber local setmetatable = setmetatable local math = { floor = math.floor } +local io = { popen = io.popen } local helpers = require("vicious.helpers") -- }}} @@ -53,23 +54,20 @@ local function worker(format) _swp.total = 0 _swp.buf.free = 0 - -- Read output of swapinfo - local f = io.popen("swapinfo") + -- Read output of swapinfo in Mbytes + local f = io.popen("swapinfo -m") -- Skip first line (table header) f:read("*line") -- Read content and sum up for line in f:lines() do local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)") - -- Add swap space in kbytes + -- Add swap space in Mbytes _swp.total = _swp.total + tonumber(ltotal) _swp.inuse = _swp.inuse + tonumber(lused) _swp.buf.free = _swp.buf.free + tonumber(lfree) end f:close() - -- Rework into megabytes - _swp.total = math.floor(_swp.total/1024) - _swp.buf.free = math.floor(_swp.buf.free/1024) -- Calculate percentage _swp.usep = math.floor(_swp.inuse / _swp.total * 100) else