Skip to content

Commit 6e185bc

Browse files
author
mutlusun
committed
calculating swap space using swapinfo (more reliable)
1 parent 18d9d3a commit 6e185bc

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

widgets/mem_freebsd.lua

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,34 @@ local function worker(format)
4747
local _swp = { buf = {}, total = nil }
4848

4949
if vm_swap_enabled == 1 and vm_swap_total > 0 then
50-
-- Get swap space in bytes
51-
_swp.total = vm_swap_total
52-
_swp.buf.free = _swp.total - tonumber(vm_stats.v_swapin)
50+
-- Initialise variables
51+
_swp.usep = 0
52+
_swp.inuse = 0
53+
_swp.total = 0
54+
_swp.buf.free = 0
55+
56+
-- Read output of swapinfo
57+
local f = io.popen("swapinfo")
58+
-- Skip first line (table header)
59+
f:read("*line")
60+
-- Read content and sum up
61+
for line in f:lines() do
62+
local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)")
63+
print(ltotal)
64+
print(lused)
65+
print(lfree)
66+
67+
-- Add swap space in kbytes
68+
_swp.total = _swp.total + tonumber(ltotal)
69+
_swp.inuse = _swp.inuse + tonumber(lused)
70+
_swp.buf.free = _swp.buf.free + tonumber(lfree)
71+
end
72+
f:close()
73+
5374
-- Rework into megabytes
54-
_swp.total = math.floor(_swp.total/1048576)
55-
_swp.buf.free = math.floor(_swp.buf.free/1048576)
75+
_swp.total = math.floor(_swp.total/1024)
76+
_swp.buf.free = math.floor(_swp.buf.free/1024)
5677
-- Calculate percentage
57-
_swp.inuse = _swp.total - _swp.buf.free
5878
_swp.usep = math.floor(_swp.inuse / _swp.total * 100)
5979
else
6080
_swp.usep = -1

0 commit comments

Comments
 (0)