2017-01-25 17:53:29 +01:00
|
|
|
-- {{{ Grab environment
|
|
|
|
local tonumber = tonumber
|
|
|
|
local math = { floor = math.floor }
|
|
|
|
local helpers = require("vicious.helpers")
|
2019-05-27 09:42:22 +02:00
|
|
|
local spawn = require("vicious.spawn")
|
|
|
|
local string = {
|
|
|
|
match = string.match,
|
|
|
|
gmatch = string.gmatch
|
|
|
|
}
|
2017-01-25 17:53:29 +01:00
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- Mem: provides RAM and Swap usage statistics
|
|
|
|
-- vicious.widgets.mem_freebsd
|
|
|
|
local mem_freebsd = {}
|
|
|
|
|
|
|
|
|
|
|
|
-- {{{ Memory widget type
|
2019-05-27 09:42:22 +02:00
|
|
|
local function parse(stdout, stderr, exitreason, exitcode)
|
2017-01-25 17:53:29 +01:00
|
|
|
local pagesize = tonumber(helpers.sysctl("hw.pagesize"))
|
|
|
|
local vm_stats = helpers.sysctl_table("vm.stats.vm")
|
|
|
|
local _mem = { buf = {}, total = nil }
|
|
|
|
|
2018-03-10 12:54:05 +01:00
|
|
|
-- Get memory space in bytes
|
2017-01-25 17:53:29 +01:00
|
|
|
_mem.total = tonumber(vm_stats.v_page_count) * pagesize
|
2018-03-12 21:01:07 +01:00
|
|
|
_mem.buf.free = tonumber(vm_stats.v_free_count) * pagesize
|
|
|
|
_mem.buf.laundry = tonumber(vm_stats.v_laundry_count) * pagesize
|
|
|
|
_mem.buf.cache = tonumber(vm_stats.v_cache_count) * pagesize
|
|
|
|
_mem.buf.wired = tonumber(vm_stats.v_wire_count) * pagesize
|
2017-01-25 17:53:29 +01:00
|
|
|
|
2018-03-10 12:54:05 +01:00
|
|
|
-- Rework into megabytes
|
|
|
|
_mem.total = math.floor(_mem.total/1048576)
|
2018-03-12 21:01:07 +01:00
|
|
|
_mem.buf.free = math.floor(_mem.buf.free/1048576)
|
|
|
|
_mem.buf.laundry = math.floor(_mem.buf.laundry/1048576)
|
|
|
|
_mem.buf.cache = math.floor(_mem.buf.cache/1048576)
|
|
|
|
_mem.buf.wired = math.floor(_mem.buf.wired/1048576)
|
2017-01-25 17:53:29 +01:00
|
|
|
|
|
|
|
-- Calculate memory percentage
|
2018-03-14 19:15:09 +01:00
|
|
|
_mem.free = _mem.buf.free + _mem.buf.cache
|
2018-03-12 21:01:07 +01:00
|
|
|
-- used memory basically consists of active+inactive+wired
|
2018-03-10 12:54:05 +01:00
|
|
|
_mem.inuse = _mem.total - _mem.free
|
2018-03-14 19:15:09 +01:00
|
|
|
_mem.notfreeable = _mem.inuse - _mem.buf.laundry
|
2018-03-12 21:01:07 +01:00
|
|
|
_mem.wire = _mem.buf.wired
|
2018-03-14 19:15:09 +01:00
|
|
|
|
2017-01-25 17:53:29 +01:00
|
|
|
_mem.usep = math.floor(_mem.inuse / _mem.total * 100)
|
2018-03-12 21:01:07 +01:00
|
|
|
_mem.wirep = math.floor(_mem.wire / _mem.total * 100)
|
2018-03-14 19:15:09 +01:00
|
|
|
_mem.notfreeablep = math.floor(_mem.notfreeable / _mem.total * 100)
|
2017-01-25 17:53:29 +01:00
|
|
|
|
|
|
|
-- Get swap states
|
2018-03-10 12:54:05 +01:00
|
|
|
local vm_swap_total = tonumber(helpers.sysctl("vm.swap_total"))
|
|
|
|
local vm_swap_enabled = tonumber(helpers.sysctl("vm.swap_enabled"))
|
2017-01-25 17:53:29 +01:00
|
|
|
local _swp = { buf = {}, total = nil }
|
2018-03-12 21:01:07 +01:00
|
|
|
|
2018-03-10 12:54:05 +01:00
|
|
|
if vm_swap_enabled == 1 and vm_swap_total > 0 then
|
2018-06-06 21:55:36 +02:00
|
|
|
-- Initialise variables
|
|
|
|
_swp.usep = 0
|
|
|
|
_swp.inuse = 0
|
|
|
|
_swp.total = 0
|
|
|
|
_swp.buf.free = 0
|
|
|
|
|
2019-05-27 09:42:22 +02:00
|
|
|
-- Read output of swapinfo in Mbytes (from async function call)
|
2018-06-06 21:55:36 +02:00
|
|
|
-- Read content and sum up
|
2019-05-27 09:42:22 +02:00
|
|
|
local counter = 1
|
|
|
|
for line in string.gmatch(stdout, "[^\n]+") do
|
|
|
|
-- Skip first line (table header)
|
|
|
|
if counter ~= 1 then
|
|
|
|
local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)")
|
|
|
|
-- 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
|
|
|
|
counter = counter + 1
|
2018-06-06 21:55:36 +02:00
|
|
|
end
|
|
|
|
|
2017-01-25 17:53:29 +01:00
|
|
|
-- Calculate percentage
|
|
|
|
_swp.usep = math.floor(_swp.inuse / _swp.total * 100)
|
|
|
|
else
|
|
|
|
_swp.usep = -1
|
|
|
|
_swp.inuse = -1
|
|
|
|
_swp.total = -1
|
2018-03-14 19:15:09 +01:00
|
|
|
_swp.buf.free = -1
|
2017-01-25 17:53:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return { _mem.usep, _mem.inuse, _mem.total, _mem.free,
|
2018-03-14 19:15:09 +01:00
|
|
|
_swp.usep, _swp.inuse, _swp.total, _swp.buf.free,
|
|
|
|
_mem.wirep, _mem.wire, _mem.notfreeablep, _mem.notfreeable }
|
2017-01-25 17:53:29 +01:00
|
|
|
end
|
|
|
|
|
2019-05-27 09:42:22 +02:00
|
|
|
function mem_freebsd.async(format, warg, callback)
|
|
|
|
spawn.easy_async("swapinfo -m",
|
|
|
|
function (...) callback(parse(...)) end)
|
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
return helpers.setasyncall(mem_freebsd)
|