2017-12-01 00:21:33 -06:00
|
|
|
---------------------------------------------------
|
|
|
|
-- Licensed under the GNU General Public License v2
|
|
|
|
-- * (c) 2011, Adrian C. <anrxc@sysphere.org>
|
|
|
|
-- * (c) 2009, Lucas de Vries <lucas@glacicle.com>
|
|
|
|
-- * (c) 2011, Jörg Thalheim <jthalheim@gmail.com>
|
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
-- {{{ Grab environment
|
|
|
|
local ipairs = ipairs
|
|
|
|
local io = { open = io.open }
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local math = { floor = math.floor }
|
|
|
|
local table = { insert = table.insert }
|
|
|
|
local string = {
|
2017-12-17 12:06:39 -06:00
|
|
|
sub = string.sub,
|
|
|
|
gmatch = string.gmatch
|
2017-12-01 00:21:33 -06:00
|
|
|
}
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
|
|
|
-- Cpu: provides CPU usage for all available CPUs/cores
|
|
|
|
-- vicious.widgets.cpu
|
|
|
|
local cpu = {}
|
|
|
|
|
|
|
|
|
|
|
|
-- Initialize function tables
|
|
|
|
local cpu_usage = {}
|
|
|
|
local cpu_total = {}
|
|
|
|
local cpu_active = {}
|
|
|
|
|
|
|
|
-- {{{ CPU widget type
|
|
|
|
local function worker(format)
|
2017-12-17 12:06:39 -06:00
|
|
|
local cpu_lines = {}
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
-- Get CPU stats
|
|
|
|
local f = io.open("/proc/stat")
|
|
|
|
for line in f:lines() do
|
|
|
|
if string.sub(line, 1, 3) ~= "cpu" then break end
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
cpu_lines[#cpu_lines+1] = {}
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
for i in string.gmatch(line, "[%s]+([^%s]+)") do
|
|
|
|
table.insert(cpu_lines[#cpu_lines], i)
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
2017-12-17 12:06:39 -06:00
|
|
|
end
|
|
|
|
f:close()
|
|
|
|
|
|
|
|
-- Ensure tables are initialized correctly
|
|
|
|
for i = #cpu_total + 1, #cpu_lines do
|
|
|
|
cpu_total[i] = 0
|
|
|
|
cpu_usage[i] = 0
|
|
|
|
cpu_active[i] = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
for i, v in ipairs(cpu_lines) do
|
|
|
|
-- Calculate totals
|
|
|
|
local total_new = 0
|
|
|
|
for j = 1, #v do
|
|
|
|
total_new = total_new + v[j]
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
2017-12-17 12:06:39 -06:00
|
|
|
local active_new = total_new - (v[4] + v[5])
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
-- Calculate percentage
|
|
|
|
local diff_total = total_new - cpu_total[i]
|
|
|
|
local diff_active = active_new - cpu_active[i]
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
if diff_total == 0 then diff_total = 1E-6 end
|
|
|
|
cpu_usage[i] = math.floor((diff_active / diff_total) * 100)
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
-- Store totals
|
|
|
|
cpu_total[i] = total_new
|
|
|
|
cpu_active[i] = active_new
|
|
|
|
end
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
return cpu_usage
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
return setmetatable(cpu, { __call = function(_, ...) return worker(...) end })
|