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

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