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

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