All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
4 Licensed under GNU General Public License v2
5 * (c) 2013, Luke Bonham
6 * (c) 2010-2012, Peter Hofmann
10 local helpers = require("lain.helpers")
11 local wibox = require("wibox")
12 local math = { ceil = math.ceil }
13 local string = { format = string.format,
14 gmatch = string.gmatch }
15 local tostring = tostring
16 local setmetatable = setmetatable
20 local cpu = { core = {} }
22 local function worker(args)
23 local args = args or {}
24 local timeout = args.timeout or 2
25 local settings = args.settings or function() end
27 cpu.widget = wibox.widget.textbox()
30 -- Read the amount of time the CPUs have spent performing
31 -- different kinds of work. Read the first line of /proc/stat
32 -- which is the sum of all CPUs.
33 local times = helpers.lines_match("cpu","/proc/stat")
35 for index,time in pairs(times) do
36 local coreid = index - 1
37 local core = cpu.core[coreid] or
38 { last_active = 0 , last_total = 0, usage = 0 }
43 for field in string.gmatch(time, "[%s]+([^%s]+)") do
44 -- 4 = idle, 5 = ioWait. Essentially, the CPUs have done
45 -- nothing during these times.
46 if at == 4 or at == 5 then
53 local active = total - idle
55 if core.last_active ~= active or core.last_total ~= total then
56 -- Read current data and calculate relative values.
57 local dactive = active - core.last_active
58 local dtotal = total - core.last_total
59 local usage = math.ceil((dactive / dtotal) * 100)
61 core.last_active = active
62 core.last_total = total
65 -- Save current data for the next run.
66 cpu.core[coreid] = core
71 cpu_now.usage = cpu_now[0].usage
77 helpers.newtimer("cpu", timeout, cpu.update)
82 return setmetatable(cpu, { __call = function(_, ...) return worker(...) end })