| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
m> |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
local tonumber = tonumber |
| 23 |
local math = { floor = math.floor } |
| 24 |
local string = { |
| 25 |
match = string.match, |
| 26 |
gmatch = string.gmatch, |
| 27 |
find = string.find |
| 28 |
} |
| 29 |
|
| 30 |
local helpers = require("vicious.helpers") |
| 31 |
local spawn = require("vicious.spawn") |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
local mem_freebsd = {} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
function mem_freebsd.async(format, warg, callback) |
| 41 |
helpers.sysctl_async({ "hw.pagesize", |
| 42 |
"vm.stats.vm", |
| 43 |
"vm.swap_total", |
| 44 |
"vm.swap_enabled" }, |
| 45 |
function(ret) |
| 46 |
|
| 47 |
local pgsz = tonumber(ret["hw.pagesize"]) |
| 48 |
local _mem = { buf = {}, total = nil } |
| 49 |
|
| 50 |
|
| 51 |
_mem.total = tonumber(ret["vm.stats.vm.v_page_count"]) * pgsz |
| 52 |
_mem.buf.free = tonumber(ret["vm.stats.vm.v_free_count"]) * pgsz |
| 53 |
_mem.buf.laundry = tonumber(ret["vm.stats.vm.v_laundry_count"]) * pgsz |
| 54 |
_mem.buf.cache = tonumber(ret["vm.stats.vm.v_cache_count"]) * pgsz |
| 55 |
_mem.buf.wired = tonumber(ret["vm.stats.vm.v_wire_count"]) * pgsz |
| 56 |
|
| 57 |
|
| 58 |
_mem.total = math.floor(_mem.total/1048576) |
| 59 |
_mem.buf.free = math.floor(_mem.buf.free/1048576) |
| 60 |
_mem.buf.laundry = math.floor(_mem.buf.laundry/1048576) |
| 61 |
_mem.buf.cache = math.floor(_mem.buf.cache/1048576) |
| 62 |
_mem.buf.wired = math.floor(_mem.buf.wired/1048576) |
| 63 |
|
| 64 |
|
| 65 |
_mem.free = _mem.buf.free + _mem.buf.cache |
| 66 |
|
| 67 |
_mem.inuse = _mem.total - _mem.free |
| 68 |
_mem.notfreeable = _mem.inuse - _mem.buf.laundry |
| 69 |
_mem.wire = _mem.buf.wired |
| 70 |
|
| 71 |
_mem.usep = math.floor(_mem.inuse / _mem.total * 100) |
| 72 |
_mem.wirep = math.floor(_mem.wire / _mem.total * 100) |
| 73 |
_mem.notfreeablep = math.floor(_mem.notfreeable / _mem.total * 100) |
| 74 |
|
| 75 |
|
| 76 |
local vm_swap_total = tonumber(ret["vm.swap_total"]) |
| 77 |
local vm_swap_enabled = tonumber(ret["vm.swap_enabled"]) |
| 78 |
local _swp = { buf = {}, total = nil } |
| 79 |
|
| 80 |
if vm_swap_enabled == 1 and vm_swap_total > 0 then |
| 81 |
|
| 82 |
_swp.usep = 0 |
| 83 |
_swp.inuse = 0 |
| 84 |
_swp.total = 0 |
| 85 |
_swp.buf.free = 0 |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
spawn.with_line_callback("swapinfo -m", { |
| 90 |
stdout = function(line) |
| 91 |
if not string.find(line, "Device") then |
| 92 |
local ltotal, lused, lfree = string.match( |
| 93 |
line, "%s+([%d]+)%s+([%d]+)%s+([%d]+)") |
| 94 |
|
| 95 |
_swp.total = _swp.total + tonumber(ltotal) |
| 96 |
_swp.inuse = _swp.inuse + tonumber(lused) |
| 97 |
_swp.buf.free = _swp.buf.free + tonumber(lfree) |
| 98 |
end |
| 99 |
end, |
| 100 |
output_done = function() |
| 101 |
print(_swp.inuse, _swp.total) |
| 102 |
_swp.usep = math.floor(_swp.inuse / _swp.total * 100) |
| 103 |
callback({ _mem.usep, |
| 104 |
_mem.inuse, |
| 105 |
_mem.total, |
| 106 |
_mem.free, |
| 107 |
_swp.usep, |
| 108 |
_swp.inuse, |
| 109 |
_swp.total, |
| 110 |
_swp.buf.free, |
| 111 |
_mem.wirep, |
| 112 |
_mem.wire, |
| 113 |
_mem.notfreeablep, |
| 114 |
_mem.notfreeable }) |
| 115 |
end |
| 116 |
}) |
| 117 |
else |
| 118 |
_swp.usep = -1 |
| 119 |
_swp.inuse = -1 |
| 120 |
_swp.total = -1 |
| 121 |
_swp.buf.free = -1 |
| 122 |
callback({ _mem.usep, |
| 123 |
_mem.inuse, |
| 124 |
_mem.total, |
| 125 |
_mem.free, |
| 126 |
_swp.usep, |
| 127 |
_swp.inuse, |
| 128 |
_swp.total, |
| 129 |
_swp.buf.free, |
| 130 |
_mem.wirep, |
| 131 |
_mem.wire, |
| 132 |
_mem.notfreeablep, |
| 133 |
_mem.notfreeable }) |
| 134 |
end |
| 135 |
end) |
| 136 |
end |
| 137 |
|
| 138 |
|
| 139 |
return helpers.setasyncall(mem_freebsd) |