[mem_freebsd] Correct async behaviour
This commit is contained in:
parent
8092915e86
commit
bc134d8792
|
@ -5,7 +5,8 @@ local helpers = require("vicious.helpers")
|
||||||
local spawn = require("vicious.spawn")
|
local spawn = require("vicious.spawn")
|
||||||
local string = {
|
local string = {
|
||||||
match = string.match,
|
match = string.match,
|
||||||
gmatch = string.gmatch
|
gmatch = string.gmatch,
|
||||||
|
find = string.find
|
||||||
}
|
}
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
@ -15,17 +16,22 @@ local mem_freebsd = {}
|
||||||
|
|
||||||
|
|
||||||
-- {{{ Memory widget type
|
-- {{{ Memory widget type
|
||||||
local function parse(stdout, stderr, exitreason, exitcode)
|
function mem_freebsd.async(format, warg, callback)
|
||||||
local pagesize = tonumber(helpers.sysctl("hw.pagesize"))
|
helpers.sysctl_async({ "hw.pagesize",
|
||||||
local vm_stats = helpers.sysctl_table("vm.stats.vm")
|
"vm.stats.vm",
|
||||||
|
"vm.swap_total",
|
||||||
|
"vm.swap_enabled" },
|
||||||
|
function(ret)
|
||||||
|
|
||||||
|
local pagesize = tonumber(ret["hw.pagesize"])
|
||||||
local _mem = { buf = {}, total = nil }
|
local _mem = { buf = {}, total = nil }
|
||||||
|
|
||||||
-- Get memory space in bytes
|
-- Get memory space in bytes
|
||||||
_mem.total = tonumber(vm_stats.v_page_count) * pagesize
|
_mem.total = tonumber(ret["vm.stats.vm.v_page_count"]) * pagesize
|
||||||
_mem.buf.free = tonumber(vm_stats.v_free_count) * pagesize
|
_mem.buf.free = tonumber(ret["vm.stats.vm.v_free_count"]) * pagesize
|
||||||
_mem.buf.laundry = tonumber(vm_stats.v_laundry_count) * pagesize
|
_mem.buf.laundry = tonumber(ret["vm.stats.vm.v_laundry_count"]) * pagesize
|
||||||
_mem.buf.cache = tonumber(vm_stats.v_cache_count) * pagesize
|
_mem.buf.cache = tonumber(ret["vm.stats.vm.v_cache_count"]) * pagesize
|
||||||
_mem.buf.wired = tonumber(vm_stats.v_wire_count) * pagesize
|
_mem.buf.wired = tonumber(ret["vm.stats.vm.v_wire_count"]) * pagesize
|
||||||
|
|
||||||
-- Rework into megabytes
|
-- Rework into megabytes
|
||||||
_mem.total = math.floor(_mem.total/1048576)
|
_mem.total = math.floor(_mem.total/1048576)
|
||||||
|
@ -46,8 +52,8 @@ local function parse(stdout, stderr, exitreason, exitcode)
|
||||||
_mem.notfreeablep = math.floor(_mem.notfreeable / _mem.total * 100)
|
_mem.notfreeablep = math.floor(_mem.notfreeable / _mem.total * 100)
|
||||||
|
|
||||||
-- Get swap states
|
-- Get swap states
|
||||||
local vm_swap_total = tonumber(helpers.sysctl("vm.swap_total"))
|
local vm_swap_total = tonumber(ret["vm.swap_total"])
|
||||||
local vm_swap_enabled = tonumber(helpers.sysctl("vm.swap_enabled"))
|
local vm_swap_enabled = tonumber(ret["vm.swap_enabled"])
|
||||||
local _swp = { buf = {}, total = nil }
|
local _swp = { buf = {}, total = nil }
|
||||||
|
|
||||||
if vm_swap_enabled == 1 and vm_swap_total > 0 then
|
if vm_swap_enabled == 1 and vm_swap_total > 0 then
|
||||||
|
@ -59,36 +65,52 @@ local function parse(stdout, stderr, exitreason, exitcode)
|
||||||
|
|
||||||
-- Read output of swapinfo in Mbytes (from async function call)
|
-- Read output of swapinfo in Mbytes (from async function call)
|
||||||
-- Read content and sum up
|
-- Read content and sum up
|
||||||
local counter = 1
|
spawn.with_line_callback("swapinfo -m", {
|
||||||
for line in string.gmatch(stdout, "[^\n]+") do
|
stdout = function(line)
|
||||||
-- Skip first line (table header)
|
if not string.find(line, "Device") then
|
||||||
if counter ~= 1 then
|
|
||||||
local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)")
|
local ltotal, lused, lfree = string.match(line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)")
|
||||||
-- Add swap space in Mbytes
|
-- Add swap space in Mbytes
|
||||||
_swp.total = _swp.total + tonumber(ltotal)
|
_swp.total = _swp.total + tonumber(ltotal)
|
||||||
_swp.inuse = _swp.inuse + tonumber(lused)
|
_swp.inuse = _swp.inuse + tonumber(lused)
|
||||||
_swp.buf.free = _swp.buf.free + tonumber(lfree)
|
_swp.buf.free = _swp.buf.free + tonumber(lfree)
|
||||||
end
|
end
|
||||||
counter = counter + 1
|
end,
|
||||||
end
|
output_done = function()
|
||||||
|
print(_swp.inuse, _swp.total)
|
||||||
-- Calculate percentage
|
|
||||||
_swp.usep = math.floor(_swp.inuse / _swp.total * 100)
|
_swp.usep = math.floor(_swp.inuse / _swp.total * 100)
|
||||||
|
callback({ _mem.usep,
|
||||||
|
_mem.inuse,
|
||||||
|
_mem.total,
|
||||||
|
_mem.free,
|
||||||
|
_swp.usep,
|
||||||
|
_swp.inuse,
|
||||||
|
_swp.total,
|
||||||
|
_swp.buf.free,
|
||||||
|
_mem.wirep,
|
||||||
|
_mem.wire,
|
||||||
|
_mem.notfreeablep,
|
||||||
|
_mem.notfreeable })
|
||||||
|
end
|
||||||
|
})
|
||||||
else
|
else
|
||||||
_swp.usep = -1
|
_swp.usep = -1
|
||||||
_swp.inuse = -1
|
_swp.inuse = -1
|
||||||
_swp.total = -1
|
_swp.total = -1
|
||||||
_swp.buf.free = -1
|
_swp.buf.free = -1
|
||||||
|
callback({ _mem.usep,
|
||||||
|
_mem.inuse,
|
||||||
|
_mem.total,
|
||||||
|
_mem.free,
|
||||||
|
_swp.usep,
|
||||||
|
_swp.inuse,
|
||||||
|
_swp.total,
|
||||||
|
_swp.buf.free,
|
||||||
|
_mem.wirep,
|
||||||
|
_mem.wire,
|
||||||
|
_mem.notfreeablep,
|
||||||
|
_mem.notfreeable })
|
||||||
end
|
end
|
||||||
|
end)
|
||||||
return { _mem.usep, _mem.inuse, _mem.total, _mem.free,
|
|
||||||
_swp.usep, _swp.inuse, _swp.total, _swp.buf.free,
|
|
||||||
_mem.wirep, _mem.wire, _mem.notfreeablep, _mem.notfreeable }
|
|
||||||
end
|
|
||||||
|
|
||||||
function mem_freebsd.async(format, warg, callback)
|
|
||||||
spawn.easy_async("swapinfo -m",
|
|
||||||
function (...) callback(parse(...)) end)
|
|
||||||
end
|
end
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue