diff --git a/README.md b/README.md index a0a85da..9958b9c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/widgets/cpu_freebsd.lua b/widgets/cpu_freebsd.lua new file mode 100644 index 0000000..5a0fc9c --- /dev/null +++ b/widgets/cpu_freebsd.lua @@ -0,0 +1,61 @@ +--------------------------------------------------- +-- Licensed under the GNU General Public License v2 +-- * (c) 2011, Adrian C. +-- * (c) 2009, Lucas de Vries +-- * (c) 2011, Jörg Thalheim +--------------------------------------------------- + +-- {{{ 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 }) diff --git a/widgets/cpu.lua b/widgets/cpu_linux.lua similarity index 94% rename from widgets/cpu.lua rename to widgets/cpu_linux.lua index fe6754a..99ec1b2 100644 --- a/widgets/cpu.lua +++ b/widgets/cpu_linux.lua @@ -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 })