Skip to content

Commit a081bf7

Browse files
authored
Merge pull request #58 from mutlusun/master
Calculating swap space using swapinfo on FreeBSD
2 parents d9287de + af979a0 commit a081bf7

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

widgets/mem_freebsd.lua

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
local tonumber = tonumber
33
local setmetatable = setmetatable
44
local math = { floor = math.floor }
5+
local io = { popen = io.popen }
56
local helpers = require("vicious.helpers")
67
-- }}}
78

@@ -47,14 +48,27 @@ local function worker(format)
4748
local _swp = { buf = {}, total = nil }
4849

4950
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)
53-
-- Rework into megabytes
54-
_swp.total = math.floor(_swp.total/1048576)
55-
_swp.buf.free = math.floor(_swp.buf.free/1048576)
51+
-- Initialise variables
52+
_swp.usep = 0
53+
_swp.inuse = 0
54+
_swp.total = 0
55+
_swp.buf.free = 0
56+
57+
-- Read output of swapinfo in Mbytes
58+
local f = io.popen("swapinfo -m")
59+
-- Skip first line (table header)
60+
f:read("*line")
61+
-- Read content and sum up
62+
for line in f:lines() do
63+
local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)")
64+
-- Add swap space in Mbytes
65+
_swp.total = _swp.total + tonumber(ltotal)
66+
_swp.inuse = _swp.inuse + tonumber(lused)
67+
_swp.buf.free = _swp.buf.free + tonumber(lfree)
68+
end
69+
f:close()
70+
5671
-- Calculate percentage
57-
_swp.inuse = _swp.total - _swp.buf.free
5872
_swp.usep = math.floor(_swp.inuse / _swp.total * 100)
5973
else
6074
_swp.usep = -1

0 commit comments

Comments
 (0)