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 lines_match = require("lain.helpers").lines_match
11 local newtimer = require("lain.helpers").newtimer
12 local wibox = require("wibox")
13 local math = { ceil = math.ceil }
14 local string = { format = string.format,
15 gmatch = string.gmatch }
16 local tostring = tostring
17 local setmetatable = setmetatable
21 local cpu = { core = {} }
23 local function worker(args)
24 local args = args or {}
25 local timeout = args.timeout or 2
26 local settings = args.settings or function() end
28 cpu.widget = wibox.widget.textbox()
31 -- Read the amount of time the CPUs have spent performing
32 -- different kinds of work. Read the first line of /proc/stat
33 -- which is the sum of all CPUs.
34 local times = lines_match("cpu","/proc/stat")
36 for index,time in pairs(times) do
37 local coreid = index - 1
38 local core = cpu.core[coreid] or
39 { last_active = 0 , last_total = 0, usage = 0 }
44 for field in string.gmatch(time, "[%s]+([^%s]+)") do
45 -- 4 = idle, 5 = ioWait. Essentially, the CPUs have done
46 -- nothing during these times.
47 if at == 4 or at == 5 then
54 local active = total - idle
56 if core.last_active ~= active or core.last_total ~= total then
57 -- Read current data and calculate relative values.
58 local dactive = active - core.last_active
59 local dtotal = total - core.last_total
60 local usage = math.ceil((dactive / dtotal) * 100)
62 core.last_active = active
63 core.last_total = total
66 -- Save current data for the next run.
67 cpu.core[coreid] = core
73 cpu_now.usage = cpu_now[0].usage
78 newtimer("cpu", timeout, update)
80 return setmetatable(cpu, { __index = cpu.widget })
83 return setmetatable(cpu, { __call = function(_, ...) return worker(...) end })