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

Merge pull request #420 from sim590/unpack-lua51-compat
[etc/awesome.git] / widget / cpu.lua
1 --[[
2
3      Licensed under GNU General Public License v2
4       * (c) 2013,      Luca CPZ
5       * (c) 2010-2012, Peter Hofmann
6
7 --]]
8
9 local helpers  = require("lain.helpers")
10 local wibox    = require("wibox")
11 local math     = math
12 local string   = string
13 local tostring = tostring
14
15 -- CPU usage
16 -- lain.widget.cpu
17
18 local function factory(args)
19     local cpu      = { core = {}, widget = wibox.widget.textbox() }
20     local args     = args or {}
21     local timeout  = args.timeout or 2
22     local settings = args.settings or function() end
23
24     function cpu.update()
25         -- Read the amount of time the CPUs have spent performing
26         -- different kinds of work. Read the first line of /proc/stat
27         -- which is the sum of all CPUs.
28         local times = helpers.lines_match("cpu","/proc/stat")
29
30         for index,time in pairs(times) do
31             local coreid = index - 1
32             local core   = cpu.core[coreid] or
33                            { last_active = 0 , last_total = 0, usage = 0 }
34             local at     = 1
35             local idle   = 0
36             local total  = 0
37
38             for field in string.gmatch(time, "[%s]+([^%s]+)") do
39                 -- 4 = idle, 5 = ioWait. Essentially, the CPUs have done
40                 -- nothing during these times.
41                 if at == 4 or at == 5 then
42                     idle = idle + field
43                 end
44                 total = total + field
45                 at = at + 1
46             end
47
48             local active = total - idle
49
50             if core.last_active ~= active or core.last_total ~= total then
51                 -- Read current data and calculate relative values.
52                 local dactive = active - core.last_active
53                 local dtotal  = total - core.last_total
54                 local usage   = math.ceil((dactive / dtotal) * 100)
55
56                 core.last_active = active
57                 core.last_total  = total
58                 core.usage       = usage
59
60                 -- Save current data for the next run.
61                 cpu.core[coreid] = core
62             end
63         end
64
65         cpu_now = cpu.core
66         cpu_now.usage = cpu_now[0].usage
67         widget = cpu.widget
68
69         settings()
70     end
71
72     helpers.newtimer("cpu", timeout, cpu.update)
73
74     return cpu
75 end
76
77 return factory