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
13 local wibox = require("wibox")
15 local math = { ceil = math.ceil }
16 local string = { format = string.format,
17 gmatch = string.gmatch }
18 local tostring = tostring
20 local setmetatable = setmetatable
24 local cpu = { core = {} }
26 local function worker(args)
27 local args = args or {}
28 local timeout = args.timeout or 2
29 local settings = args.settings or function() end
31 cpu.widget = wibox.widget.textbox('')
34 -- Read the amount of time the CPUs have spent performing
35 -- different kinds of work. Read the first line of /proc/stat
36 -- which is the sum of all CPUs.
37 local times = lines_match("cpu","/proc/stat")
39 for index,time in pairs(times)
41 local coreid = index - 1
42 local core = cpu.core[coreid] or
43 { last_active = 0 , last_total = 0, usage = 0 }
48 for field in string.gmatch(time, "[%s]+([^%s]+)")
50 -- 4 = idle, 5 = ioWait. Essentially, the CPUs have done
51 -- nothing during these times.
52 if at == 4 or at == 5 then
59 local active = total - idle
61 if core.last_active ~= active or core.last_total ~= total then
62 -- Read current data and calculate relative values.
63 local dactive = active - core.last_active
64 local dtotal = total - core.last_total
66 local usage = math.ceil((dactive / dtotal) * 100)
68 core.last_active = active
69 core.last_total = total
72 -- Save current data for the next run.
73 cpu.core[coreid] = core;
79 cpu_now.usage = cpu_now[0].usage
84 newtimer("cpu", timeout, update)
88 return setmetatable(cpu, { __call = function(_, ...) return worker(...) end })