Debian Codegraph @

vicious/widgets/cpu_openbsd.lua

raw symbols SCIP
1 -- CPU usage widget type for OpenBSD
2 -- Copyright (C) 2019 Enric Morales
3 --
4 -- This file is part of Vicious.
5 --
6 -- Vicious is free software: you can redistribute it and/or modify
7 -- it under the terms of the GNU General Public License as
8 -- published by the Free Software Foundation, either version 2 of the
9 -- License, or (at your option) any later version.
10 --
11 -- Vicious is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU General Public License for more details.
15 --
16 -- You should have received a copy of the GNU General Public License
17 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
18
19 local math = { ceil = math.ceil }
20 local string = { gmatch = string.gmatch }
21 local table = { insert = table.insert }
22 local tonumber = tonumber
23
24 local helpers = require("vicious.helpers")
25
26
27 -- cpu_openbsd: provides both a helper function that allows reading
28 -- the CPU usage on OpenBSD systems.
29 local cpu_openbsd = {}
30
31 -- Initialize the table that will contain the ticks spent in each subsystem
32 -- values: user, nice, system, spin, interrupts, idle
33 local ticks = { 0, 0, 0, 0, 0, 0 }
34
35 function cpu_openbsd.async(format, warg, callback)
36 helpers.sysctl_async({ "kern.cp_time" },
37 function (ret)
38 local current_ticks = {}
39 for match in string.gmatch(ret["kern.cp_time"], "(%d+)") do
40 table.insert(current_ticks, tonumber(match))
41 end
42
43 local period_ticks = {}
44 for i = 1, 6 do
45 table.insert(period_ticks,
46 current_ticks[i] - ticks[i])
47 end
48
49 local cpu_total, cpu_busy = 0, 0
50 for i = 1, 6 do cpu_total = cpu_total + period_ticks[i] end
51 for i = 1, 5 do cpu_busy = cpu_busy + period_ticks[i] end
52
53 local cpu_usage = math.ceil((cpu_busy / cpu_total) * 100)
54
55 ticks = current_ticks
56 return callback({ cpu_usage })
57 end)
58 end
59
60 return helpers.setasyncall(cpu_openbsd)