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

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