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 first_line = require("lain.helpers").first_line
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
29 local function worker(args)
30 local args = args or {}
31 local timeout = args.timeout or 2
32 local settings = args.settings or function() end
34 cpu.widget = wibox.widget.textbox('')
37 -- Read the amount of time the CPUs have spent performing
38 -- different kinds of work. Read the first line of /proc/stat
39 -- which is the sum of all CPUs.
40 local times = first_line("/proc/stat")
44 for field in string.gmatch(times, "[%s]+([^%s]+)")
46 -- 4 = idle, 5 = ioWait. Essentially, the CPUs have done
47 -- nothing during these times.
55 local active = total - idle
57 -- Read current data and calculate relative values.
58 local dactive = active - cpu.last_active
59 local dtotal = total - cpu.last_total
62 cpu_now.usage = tostring(math.ceil((dactive / dtotal) * 100))
67 -- Save current data for the next run.
68 cpu.last_active = active
69 cpu.last_total = total
72 newtimer("cpu", timeout, update)
77 return setmetatable(cpu, { __call = function(_, ...) return worker(...) end })