fixed wrong calculations in cpu_freebsd

This commit is contained in:
mutlusun 2017-01-04 13:05:12 +01:00
parent 6ef9a39702
commit 23c4e8de81
1 changed files with 41 additions and 26 deletions

View File

@ -18,43 +18,58 @@ local string = { gmatch = string.gmatch }
-- vicious.widgets.cpu_freebsd -- vicious.widgets.cpu_freebsd
local cpu_freebsd = {} local cpu_freebsd = {}
-- Initialize function tables
local cpu_total = {}
local cpu_idle = {}
-- {{{ CPU widget type -- {{{ CPU widget type
local function worker(format) local function worker(format)
local kern = helpers.sysctl_table("kern") local cp_times = helpers.sysctl("kern.cp_times")
local cpu_total = {} local matches = {}
local cpu_idle = {} local tmp_total = {}
local cpu_pusage = {} local tmp_idle = {}
local i = 0 local tmp_usage = {}
-- CPU usage over all cpus -- Read input data
cpu_total[0] = 0 for v in string.gmatch(cp_times, "([%d]+)") do
for v in string.gmatch(kern.cp_time, "([%d]+)") do table.insert(matches, v)
cpu_total[0] = cpu_total[0] + tonumber(v)
cpu_idle[0] = tonumber(v)
end end
i = 0 -- Set first value of function tables
for v in string.gmatch(kern.cp_times, "([%d]+)") do if #cpu_total == 0 then -- check for empty table
local index = math.floor(i/5) + 1 for i = 1, #matches / 5 + 1 do
cpu_total[i] = 0
if i % 5 == 0 then cpu_idle[i] = 0
cpu_total[index] = 0 end
elseif i % 5 == 4 then end
cpu_idle[index] = tonumber(v) for i = 1, #matches / 5 + 1 do
tmp_total[i] = 0
tmp_idle[i] = 0
tmp_usage[i] = 0
end end
cpu_total[index] = cpu_total[index] + tonumber(v) -- CPU usage
for i, v in ipairs(matches) do
local index = math.floor((i-1) / 5) + 2 -- current cpu
i = i + 1 tmp_total[1] = tmp_total[1] + v
tmp_total[index] = tmp_total[index] + v
if (i-1) % 5 == 4 then
tmp_idle[1] = tmp_idle[1] + v
tmp_idle[index] = tmp_idle[index] + v
end
end end
for i = 0, #cpu_total do for i = 1, #tmp_usage do
print((cpu_total[i] - cpu_idle[i])/cpu_total[i]) tmp_usage[i] = tmp_total[i] - cpu_total[i]
cpu_pusage[i + 1] = math.floor((cpu_total[i] - cpu_idle[i])/cpu_total[i] * 100) tmp_usage[i] = math.floor((tmp_usage[i] - (tmp_idle[i] - cpu_idle[i])) / tmp_usage[i] * 100)
end end
return cpu_pusage cpu_total = tmp_total
cpu_idle = tmp_idle
return tmp_usage
end end
-- }}} -- }}}