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

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