]> 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:

merge and resolve conflicts from #134; #114
[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 helpers      = require("lain.helpers")
11 local wibox        = require("wibox")
12
13 local math         = { ceil   = math.ceil }
14 local string       = { format = string.format,
15                        gmatch = string.gmatch }
16 local tostring     = tostring
17 local setmetatable = setmetatable
18
19 -- CPU usage
20 -- lain.widgets.cpu
21 local cpu = {
22     last_total = 0,
23     last_active = 0
24 }
25
26 local function worker(args)
27     local args     = args or {}
28     local timeout  = args.timeout or 1
29     local settings = args.settings or function() end
30
31     cpu.widget = wibox.widget.textbox('')
32     helpers.set_map("cpuactive", 0)
33     helpers.set_map("cputotal", 0)
34
35     function update()
36         -- Read the amount of time the CPUs have spent performing
37         -- different kinds of work. Read the first line of /proc/stat
38         -- which is the sum of all CPUs.
39         local times = helpers.first_line("/proc/stat")
40         local at = 1
41         local idle = 0
42         local total = 0
43         for field in string.gmatch(times, "[%s]+([^%s]+)")
44         do
45             -- 4 = idle, 5 = ioWait. Essentially, the CPUs have done
46             -- nothing during these times.
47             if at == 4 or at == 5
48             then
49                 idle = idle + field
50             end
51             total = total + field
52             at = at + 1
53         end
54         local active = total - idle
55
56         if helpers.get_map("cpuactive") ~= active
57            or helpers.get_map("cputotal") ~= total
58         then
59             -- Read current data and calculate relative values.
60             local dactive = active - cpu.last_active
61             local dtotal = total - cpu.last_total
62
63             cpu_now = {}
64             cpu_now.usage = tostring(math.ceil((dactive / dtotal) * 100))
65
66             widget = cpu.widget
67             settings()
68
69             -- Save current data for the next run.
70             helpers.set_map("cpuactive", active)
71             helpers.set_map("cputotal", total)
72         end
73     end
74
75     helpers.newtimer("cpu", timeout, update)
76
77     return cpu.widget
78 end
79
80 return setmetatable(cpu, { __call = function(_, ...) return worker(...) end })