Skip to content

Commit ebc3a08

Browse files
author
mutlusun
committed
freebsd memory clean ups and correct calculation
1 parent a81435e commit ebc3a08

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,8 @@ Supported platforms: Linux, FreeBSD.
323323
total system memory, 4th as free memory, 5th as swap usage in percent, 6th
324324
as swap usage, 7th as total system swap, 8th as free swap and 9th as
325325
memory usage with buffers and cache
326-
* FreeBSD: see above, but 10th value as memory usage with buffers and cache
327-
as percent and 11th value as wired memory (memory used by kernel) is
328-
reported
326+
* FreeBSD: see above, but 9th value is not returned (always `-1`) and there
327+
is a 10th value giving wired memory
329328

330329
**vicious.widgets.mpd**
331330

widgets/bat_freebsd.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ local function worker(format, warg)
3030
state = ""
3131
elseif bat_info["State"] == "charging" then
3232
state = "+"
33+
elseif bat_info["State"] == "critical charging" then
34+
state = "+"
3335
elseif bat_info["State"] == "discharging" then
3436
state = "-"
3537
else

widgets/mem_freebsd.lua

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,41 @@ local function worker(format)
1616
local vm_stats = helpers.sysctl_table("vm.stats.vm")
1717
local _mem = { buf = {}, total = nil }
1818

19+
-- Get memory space in bytes
1920
_mem.total = tonumber(vm_stats.v_page_count) * pagesize
2021
_mem.buf.f = tonumber(vm_stats.v_free_count) * pagesize
2122
_mem.buf.a = tonumber(vm_stats.v_active_count) * pagesize
2223
_mem.buf.i = tonumber(vm_stats.v_inactive_count) * pagesize
2324
_mem.buf.c = tonumber(vm_stats.v_cache_count) * pagesize
2425
_mem.buf.w = tonumber(vm_stats.v_wire_count) * pagesize
2526

26-
-- rework into Megabytes
27-
_mem.total = math.floor(_mem.total/(1024*1024))
28-
_mem.buf.f = math.floor(_mem.buf.f/(1024*1024))
29-
_mem.buf.a = math.floor(_mem.buf.a/(1024*1024))
30-
_mem.buf.i = math.floor(_mem.buf.i/(1024*1024))
31-
_mem.buf.c = math.floor(_mem.buf.c/(1024*1024))
32-
_mem.buf.w = math.floor(_mem.buf.w/(1024*1024))
27+
-- Rework into megabytes
28+
_mem.total = math.floor(_mem.total/1048576)
29+
_mem.buf.f = math.floor(_mem.buf.f/1048576)
30+
_mem.buf.a = math.floor(_mem.buf.a/1048576)
31+
_mem.buf.i = math.floor(_mem.buf.i/1048576)
32+
_mem.buf.c = math.floor(_mem.buf.c/1048576)
33+
_mem.buf.w = math.floor(_mem.buf.w/1048576)
3334

3435
-- Calculate memory percentage
35-
_mem.free = _mem.buf.f + _mem.buf.c
36-
_mem.inuse = _mem.buf.a + _mem.buf.i
36+
_mem.free = _mem.buf.f + _mem.buf.c + _mem.buf.i
37+
_mem.inuse = _mem.total - _mem.free
3738
_mem.wire = _mem.buf.w
38-
_mem.bcuse = _mem.total - _mem.buf.f
3939
_mem.usep = math.floor(_mem.inuse / _mem.total * 100)
4040
_mem.inusep= math.floor(_mem.inuse / _mem.total * 100)
41-
_mem.buffp = math.floor(_mem.bcuse / _mem.total * 100)
42-
_mem.wirep = math.floor(_mem.wire / _mem.total * 100)
4341

4442
-- Get swap states
45-
local vm = helpers.sysctl_table("vm")
43+
local vm_swap_total = tonumber(helpers.sysctl("vm.swap_total"))
44+
local vm_swap_enabled = tonumber(helpers.sysctl("vm.swap_enabled"))
4645
local _swp = { buf = {}, total = nil }
4746

48-
if tonumber(vm.swap_enabled) == 1 and tonumber(vm.swap_total) > 0 then
49-
-- Get swap space
50-
_swp.total = tonumber(vm.swap_total)
47+
if vm_swap_enabled == 1 and vm_swap_total > 0 then
48+
-- Get swap space in bytes
49+
_swp.total = vm_swap_total
5150
_swp.buf.f = _swp.total - tonumber(vm_stats.v_swapin)
5251
-- Rework into megabytes
53-
_swp.total = math.floor(_swp.total/(1024*1024))
54-
_swp.buf.f = math.floor(_swp.buf.f/(1024*1024))
52+
_swp.total = math.floor(_swp.total/1048576)
53+
_swp.buf.f = math.floor(_swp.buf.f/1048576)
5554
-- Calculate percentage
5655
_swp.inuse = _swp.total - _swp.buf.f
5756
_swp.usep = math.floor(_swp.inuse / _swp.total * 100)
@@ -64,7 +63,7 @@ local function worker(format)
6463

6564
return { _mem.usep, _mem.inuse, _mem.total, _mem.free,
6665
_swp.usep, _swp.inuse, _swp.total, _swp.buf.f,
67-
_mem.bcuse, _mem.buffp, _mem.wirep }
66+
-1, _mem.wire }
6867
end
6968

7069
return setmetatable(mem_freebsd, { __call = function(_, ...) return worker(...) end })

0 commit comments

Comments
 (0)