2013-09-07 12:06:42 +02:00
|
|
|
--[[
|
2017-09-04 12:43:00 +02:00
|
|
|
|
|
|
|
Licensed under GNU General Public License v2
|
2017-09-11 06:51:52 +02:00
|
|
|
* (c) 2013, Luca CPZ
|
2017-09-04 12:43:00 +02:00
|
|
|
* (c) 2010-2012, Peter Hofmann
|
|
|
|
|
2013-09-07 12:06:42 +02:00
|
|
|
--]]
|
|
|
|
|
2017-04-02 19:35:03 +02:00
|
|
|
local helpers = require("lain.helpers")
|
|
|
|
local wibox = require("wibox")
|
|
|
|
local gmatch, lines, floor = string.gmatch, io.lines, math.floor
|
2015-10-20 15:17:44 +02:00
|
|
|
|
|
|
|
-- Memory usage (ignoring caches)
|
2017-02-08 14:15:48 +01:00
|
|
|
-- lain.widget.mem
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2017-02-08 20:45:11 +01:00
|
|
|
local function factory(args)
|
2020-11-29 22:48:32 +01:00
|
|
|
args = args or {}
|
|
|
|
|
2020-10-31 18:11:29 +01:00
|
|
|
local mem = { widget = args.widget or wibox.widget.textbox() }
|
2015-10-20 15:17:44 +02:00
|
|
|
local timeout = args.timeout or 2
|
2013-09-10 23:02:11 +02:00
|
|
|
local settings = args.settings or function() end
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2017-01-27 14:35:27 +01:00
|
|
|
function mem.update()
|
2013-09-13 00:07:44 +02:00
|
|
|
mem_now = {}
|
2017-01-23 13:35:30 +01:00
|
|
|
for line in lines("/proc/meminfo") do
|
|
|
|
for k, v in gmatch(line, "([%a]+):[%s]+([%d]+).+") do
|
2017-01-24 16:38:28 +01:00
|
|
|
if k == "MemTotal" then mem_now.total = floor(v / 1024 + 0.5)
|
|
|
|
elseif k == "MemFree" then mem_now.free = floor(v / 1024 + 0.5)
|
|
|
|
elseif k == "Buffers" then mem_now.buf = floor(v / 1024 + 0.5)
|
|
|
|
elseif k == "Cached" then mem_now.cache = floor(v / 1024 + 0.5)
|
|
|
|
elseif k == "SwapTotal" then mem_now.swap = floor(v / 1024 + 0.5)
|
|
|
|
elseif k == "SwapFree" then mem_now.swapf = floor(v / 1024 + 0.5)
|
|
|
|
elseif k == "SReclaimable" then mem_now.srec = floor(v / 1024 + 0.5)
|
2013-09-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-23 13:35:30 +01:00
|
|
|
mem_now.used = mem_now.total - mem_now.free - mem_now.buf - mem_now.cache - mem_now.srec
|
2015-10-20 15:17:44 +02:00
|
|
|
mem_now.swapused = mem_now.swap - mem_now.swapf
|
2016-07-27 11:40:41 +02:00
|
|
|
mem_now.perc = math.floor(mem_now.used / mem_now.total * 100)
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2015-10-20 15:17:44 +02:00
|
|
|
widget = mem.widget
|
|
|
|
settings()
|
2013-09-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
2017-01-27 14:35:27 +01:00
|
|
|
helpers.newtimer("mem", timeout, mem.update)
|
2013-09-07 12:06:42 +02:00
|
|
|
|
2017-01-25 17:13:14 +01:00
|
|
|
return mem
|
2013-09-07 12:06:42 +02:00
|
|
|
end
|
|
|
|
|
2017-04-02 19:35:03 +02:00
|
|
|
return factory
|