]> 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 #439 from gregflynn/201908_gf_customize_pulsebar_notification_char...
[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         for index,time in pairs(helpers.lines_match("cpu","/proc/stat")) do
29             local coreid = index - 1
30             local core   = cpu.core[coreid] or
31                            { last_active = 0 , last_total = 0, usage = 0 }
32             local at     = 1
33             local idle   = 0
34             local total  = 0
35
36             for field in string.gmatch(time, "[%s]+([^%s]+)") do
37                 -- 4 = idle, 5 = ioWait. Essentially, the CPUs have done
38                 -- nothing during these times.
39                 if at == 4 or at == 5 then
40                     idle = idle + field
41                 end
42                 total = total + field
43                 at = at + 1
44             end
45
46             local active = total - idle
47
48             if core.last_active ~= active or core.last_total ~= total then
49                 -- Read current data and calculate relative values.
50                 local dactive = active - core.last_active
51                 local dtotal  = total - core.last_total
52                 local usage   = math.ceil((dactive / dtotal) * 100)
53
54                 core.last_active = active
55                 core.last_total  = total
56                 core.usage       = usage
57
58                 -- Save current data for the next run.
59                 cpu.core[coreid] = core
60             end
61         end
62
63         cpu_now = cpu.core
64         cpu_now.usage = cpu_now[0].usage
65         widget = cpu.widget
66
67         settings()
68     end
69
70     helpers.newtimer("cpu", timeout, cpu.update)
71
72     return cpu
73 end
74
75 return factory