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

c33c7eb68736692b9eede3e456b0cf616b474e8d
[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 lines_match  = require("lain.helpers").lines_match
11 local newtimer     = require("lain.helpers").newtimer
12
13 local wibox        = require("wibox")
14
15 local math         = { ceil   = math.ceil }
16 local string       = { format = string.format,
17                        gmatch = string.gmatch }
18 local tostring     = tostring
19
20 local setmetatable = setmetatable
21
22 -- CPU usage
23 -- lain.widgets.cpu
24 local cpu = { core = {} }
25
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
30
31     cpu.widget = wibox.widget.textbox('')
32
33     function update()
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")
38
39         for index,time in pairs(times) do
40             local coreid = index - 1
41             local core   = cpu.core[coreid] or
42                            { last_active = 0 , last_total = 0, usage = 0 }
43             local at     = 1
44             local idle   = 0
45             local total  = 0
46
47             for field in string.gmatch(time, "[%s]+([^%s]+)") do
48                 -- 4 = idle, 5 = ioWait. Essentially, the CPUs have done
49                 -- nothing during these times.
50                 if at == 4 or at == 5 then
51                     idle = idle + field
52                 end
53                 total = total + field
54                 at = at + 1
55             end
56
57             local active = total - idle
58
59             if core.last_active ~= active or core.last_total ~= total then
60                 -- Read current data and calculate relative values.
61                 local dactive = active - core.last_active
62                 local dtotal  = total - core.last_total
63                 local usage   = math.ceil((dactive / dtotal) * 100)
64
65                 core.last_active = active
66                 core.last_total  = total
67                 core.usage       = usage
68
69                 -- Save current data for the next run.
70                 cpu.core[coreid] = core
71             end
72         end
73
74         widget = cpu.widget
75         cpu_now = cpu.core
76         cpu_now.usage = cpu_now[0].usage
77
78         settings()
79     end
80
81     newtimer("cpu", timeout, update)
82
83     return cpu.widget
84 end
85
86 return setmetatable(cpu, { __call = function(_, ...) return worker(...) end })