| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 28 |
|
| 29 |
local cpu_openbsd = {} |
| 30 |
|
| 31 |
|
| 32 |
|
| 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) |