aboutsummaryrefslogtreecommitdiff
path: root/cpuinf.lua
blob: 3604dee9fe6c87fbe3162521c95e4ddf591c1f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
---------------------------------------------------
-- Licensed under the GNU General Public License v2
--  * (c) 2010, Adrian C. <anrxc@sysphere.org>
---------------------------------------------------

-- {{{ Grab environment
local tonumber = tonumber
local io = { lines = io.lines }
local setmetatable = setmetatable
local string = { match = string.match }
-- }}}


-- Cpuinf: provides speed and cache information for all available CPUs/cores
module("vicious.cpuinf")


-- {{{ CPU Information widget type
local function worker(format)
    local cpu_id   = nil
    local cpu_info = {}

    -- Get CPU info
    for line in io.lines("/proc/cpuinfo") do
        if string.match(line, "^processor.*") then
            cpu_id = string.match(line, "([%d]+)")
        elseif string.match(line, "^cpu MHz.*") then
            local cpu_speed = tonumber(string.match(line, "([%d]+)%."))
            cpu_info["{cpu"..cpu_id.." mhz}"] = cpu_speed
            cpu_info["{cpu"..cpu_id.." ghz}"] = cpu_speed / 1000
        elseif string.match(line, "^cache size.*") then
            local cpu_cache = tonumber(string.match(line, "([%d]+)[%s]KB"))
            cpu_info["{cpu"..cpu_id.." kb}"] = cpu_cache
            cpu_info["{cpu"..cpu_id.." mb}"] = cpu_cache / 1024
        end
    end

    return cpu_info
end
-- }}}

setmetatable(_M, { __call = function(_, ...) return worker(...) end })