]> git.madduck.net Git - etc/awesome.git/blob - widgets/cpu.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

3890e5ea2b22974334ccba6bd1b823b03dbfdb27
[etc/awesome.git] / widgets / cpu.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013,      Luke Bonham                
6       * (c) 2010-2012, Peter Hofmann              
7                                                   
8 --]]
9
10 local markup       = require("lain.util.markup")
11 local first_line   = require("lain.helpers").first_line
12
13 local beautiful    = require("beautiful")
14 local wibox        = require("wibox")
15
16 local math         = { ceil   = math.ceil }
17 local string       = { format = string.format,
18                        gmatch = string.gmatch }
19
20 local setmetatable = setmetatable
21
22 -- CPU usage
23 -- lain.widgets.cpu
24 local cpu = {
25     last_total = 0,
26     last_active = 0
27 }
28
29 function worker(args)
30     local args = args or {}
31     local refresh_timeout = args.refresh_timeout or 5
32     local header = args.header or " Cpu "
33     local header_color = args.header or beautiful.fg_normal or "#FFFFFF"
34     local color = args.color or beautiful.fg_focus or "#FFFFFF"
35     local footer = args.footer or "% "
36
37     local w = wibox.widget.textbox()
38
39     local cpuusageupdate = function()
40         -- Read the amount of time the CPUs have spent performing
41         -- different kinds of work. Read the first line of /proc/stat
42         -- which is the sum of all CPUs.
43         local times = first_line("/proc/stat")
44         local at = 1
45         local idle = 0
46         local total = 0
47         for field in string.gmatch(times, "[%s]+([^%s]+)")
48         do
49             -- 3 = idle, 4 = ioWait. Essentially, the CPUs have done
50             -- nothing during these times.
51             if at == 3 or at == 4
52             then
53                 idle = idle + field
54             end
55             total = total + field
56             at = at + 1
57         end
58         local active = total - idle
59
60         -- Read current data and calculate relative values.
61         local dactive = active - cpu.last_active
62         local dtotal = total - cpu.last_total
63         local dta = math.ceil((dactive / dtotal) * 100)
64
65         w:set_markup(markup(header_color, header) .. markup(color, dta .. footer))
66
67         -- Save current data for the next run.
68         cpu.last_active = active
69         cpu.last_total = total
70     end
71
72     local cpuusagetimer = timer({ timeout = refresh_timeout })
73     cpuusagetimer:connect_signal("timeout", cpuusageupdate)
74     cpuusagetimer:start()
75     cpuusagetimer:emit_signal("timeout")
76
77     return w
78 end
79
80 return setmetatable(cpu, { __call = function(_, ...) return worker(...) end })