]> git.madduck.net Git - etc/awesome.git/blob - widget/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:

removed whitespaced signatures; wiki updated
[etc/awesome.git] / widget / cpu.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2013,      Luke Bonham
5       * (c) 2010-2012, Peter Hofmann
6
7 --]]
8
9 local helpers  = require("lain.helpers")
10 local wibox    = require("wibox")
11 local math     = { ceil   = math.ceil }
12 local string   = { format = string.format,
13                    gmatch = string.gmatch }
14 local tostring = tostring
15
16 -- CPU usage
17 -- lain.widget.cpu
18
19 local function factory(args)
20     local cpu      = { core = {}, widget = wibox.widget.textbox() }
21     local args     = args or {}
22     local timeout  = args.timeout or 2
23     local settings = args.settings or function() end
24
25     function cpu.update()
26         -- Read the amount of time the CPUs have spent performing
27         -- different kinds of work. Read the first line of /proc/stat
28         -- which is the sum of all CPUs.
29         local times = helpers.lines_match("cpu","/proc/stat")
30
31         for index,time in pairs(times) do
32             local coreid = index - 1
33             local core   = cpu.core[coreid] or
34                            { last_active = 0 , last_total = 0, usage = 0 }
35             local at     = 1
36             local idle   = 0
37             local total  = 0
38
39             for field in string.gmatch(time, "[%s]+([^%s]+)") do
40                 -- 4 = idle, 5 = ioWait. Essentially, the CPUs have done
41                 -- nothing during these times.
42                 if at == 4 or at == 5 then
43                     idle = idle + field
44                 end
45                 total = total + field
46                 at = at + 1
47             end
48
49             local active = total - idle
50
51             if core.last_active ~= active or core.last_total ~= total then
52                 -- Read current data and calculate relative values.
53                 local dactive = active - core.last_active
54                 local dtotal  = total - core.last_total
55                 local usage   = math.ceil((dactive / dtotal) * 100)
56
57                 core.last_active = active
58                 core.last_total  = total
59                 core.usage       = usage
60
61                 -- Save current data for the next run.
62                 cpu.core[coreid] = core
63             end
64         end
65
66         cpu_now = cpu.core
67         cpu_now.usage = cpu_now[0].usage
68         widget = cpu.widget
69
70         settings()
71     end
72
73     helpers.newtimer("cpu", timeout, cpu.update)
74
75     return cpu
76 end
77
78 return factory