mem: rewrite and simplify

This commit is contained in:
Adrian C. (anrxc) 2010-03-11 02:57:07 +01:00
parent 77d1a0ba2f
commit 3178068b96
1 changed files with 14 additions and 19 deletions

33
mem.lua
View File

@ -8,7 +8,7 @@
local io = { open = io.open }
local setmetatable = setmetatable
local math = { floor = math.floor }
local string = { match = string.match }
local string = { gmatch = string.gmatch }
-- }}}
@ -23,33 +23,28 @@ local function worker(format)
local mem = { buf = {}, swp = {} }
for line in f:lines() do
if string.match(line, "^MemTotal.*") then
mem.total = math.floor(string.match(line, "([%d]+)")/1024)
elseif string.match(line, "^MemFree.*") then
mem.buf.f = math.floor(string.match(line, "([%d]+)")/1024)
elseif string.match(line, "^Buffers.*") then
mem.buf.b = math.floor(string.match(line, "([%d]+)")/1024)
elseif string.match(line, "^Cached.*") then
mem.buf.c = math.floor(string.match(line, "([%d]+)")/1024)
-- Get swap stats while we are at it
elseif string.match(line, "^SwapTotal.*") then
mem.swp.total = math.floor(string.match(line, "([%d]+)")/1024)
elseif string.match(line, "^SwapFree.*") then
mem.swp.free = math.floor(string.match(line, "([%d]+)")/1024)
for k, v in string.gmatch(line, "([%a]+):[%s]+([%d]+).+") do
if k == "MemTotal" then mem.total = math.floor(v/1024)
elseif k == "MemFree" then mem.buf.f = math.floor(v/1024)
elseif k == "Buffers" then mem.buf.b = math.floor(v/1024)
elseif k == "Cached" then mem.buf.c = math.floor(v/1024)
elseif k == "SwapTotal" then mem.swp.t = math.floor(v/1024)
elseif k == "SwapFree" then mem.swp.f = math.floor(v/1024)
end
end
end
f:close()
-- Calculate percentage
-- Calculate memory percentage
mem.free = mem.buf.f + mem.buf.b + mem.buf.c
mem.inuse = mem.total - mem.free
mem.usep = math.floor(mem.inuse / mem.total * 100)
-- Calculate swap percentage
mem.swp.inuse = mem.swp.total - mem.swp.free
mem.swp.usep = math.floor(mem.swp.inuse / mem.swp.total * 100)
mem.swp.inuse = mem.swp.t - mem.swp.f
mem.swp.usep = math.floor(mem.swp.inuse / mem.swp.t * 100)
return {mem.usep, mem.inuse, mem.total, mem.free,
mem.swp.usep, mem.swp.inuse, mem.swp.total, mem.swp.free}
return {mem.usep, mem.inuse, mem.total, mem.free,
mem.swp.usep, mem.swp.inuse, mem.swp.t, mem.swp.f}
end
-- }}}