added cpu widget (not affected by bug; usable)

This commit is contained in:
mutlusun 2017-01-01 16:11:33 +01:00
parent fcfe274e46
commit 6ef9a39702
3 changed files with 74 additions and 8 deletions

View File

@ -160,6 +160,17 @@ Supported platforms: Linux, FreeBSD.
* FreeBSD: see Linux, but wear level is not supported right now and always
set to `0`
**vicious.widgets.cpu**
Provides CPU usage for all available CPUs/cores.
Supported platforms: Linux, FreeBSD.
- Arguments:
* None
- Returns:
* returns 1st value as usage of all CPUs/cores, 2nd as usage of first
CPU/core, 3rd as usage of second CPU/core etc.
**vicious.widgets.date**
Provides access to os.date, with optional time formatting provided as the
@ -245,12 +256,6 @@ Supported platforms: Linux, FreeBSD.
* returns 1st value as the volume level and 2nd as the mute state of the
requested channel
**vicious.widgets.cpu**
- provides CPU usage for all available CPUs/cores
- returns 1st value as usage of all CPUs/cores, 2nd as usage of
first CPU/core, 3rd as usage of second CPU/core etc.
**vicious.widgets.cpuinf**
- provides speed and cache information for all available CPUs/cores

61
widgets/cpu_freebsd.lua Normal file
View File

@ -0,0 +1,61 @@
---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2011, Adrian C. <anrxc@sysphere.org>
-- * (c) 2009, Lucas de Vries <lucas@glacicle.com>
-- * (c) 2011, Jörg Thalheim <jthalheim@gmail.com>
---------------------------------------------------
-- {{{ Grab environment
local helpers = require("vicious.helpers")
local tonumber = tonumber
local setmetatable = setmetatable
local math = { floor = math.floor }
local string = { gmatch = string.gmatch }
-- }}}
-- Cpu: provides CPU usage for all available CPUs/cores
-- vicious.widgets.cpu_freebsd
local cpu_freebsd = {}
-- {{{ CPU widget type
local function worker(format)
local kern = helpers.sysctl_table("kern")
local cpu_total = {}
local cpu_idle = {}
local cpu_pusage = {}
local i = 0
-- CPU usage over all cpus
cpu_total[0] = 0
for v in string.gmatch(kern.cp_time, "([%d]+)") do
cpu_total[0] = cpu_total[0] + tonumber(v)
cpu_idle[0] = tonumber(v)
end
i = 0
for v in string.gmatch(kern.cp_times, "([%d]+)") do
local index = math.floor(i/5) + 1
if i % 5 == 0 then
cpu_total[index] = 0
elseif i % 5 == 4 then
cpu_idle[index] = tonumber(v)
end
cpu_total[index] = cpu_total[index] + tonumber(v)
i = i + 1
end
for i = 0, #cpu_total do
print((cpu_total[i] - cpu_idle[i])/cpu_total[i])
cpu_pusage[i + 1] = math.floor((cpu_total[i] - cpu_idle[i])/cpu_total[i] * 100)
end
return cpu_pusage
end
-- }}}
return setmetatable(cpu_freebsd, { __call = function(_, ...) return worker(...) end })

View File

@ -20,7 +20,7 @@ local string = {
-- Cpu: provides CPU usage for all available CPUs/cores
-- vicious.widgets.cpu
local cpu = {}
local cpu_linux = {}
-- Initialize function tables
@ -77,4 +77,4 @@ local function worker(format)
end
-- }}}
return setmetatable(cpu, { __call = function(_, ...) return worker(...) end })
return setmetatable(cpu_linux, { __call = function(_, ...) return worker(...) end })