aboutsummaryrefslogtreecommitdiff
path: root/cpufreq.lua
blob: b94676cd5695fb53ec4480687508b5b5e8a38870 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
----------------------------------------------------------
-- Licensed under the GNU General Public License version 2
--  * Copyright (C) 2009 Adrian C. <anrxc_sysphere_org>
----------------------------------------------------------

-- {{{ Grab environment
local io = { open = io.open }
local setmetatable = setmetatable
local string = {
    find = string.find,
    match = string.match
}
-- }}}


-- Cpufreq: provides freq, voltage and governor info for a requested CPU
module("vicious.cpufreq")


-- {{{ CPU frequency widget type
local function worker(format, cpuid)
    --local governor_state = {
    --    ["ondemand"] = "↯",
    --    ["powersave"] = "⌁",
    --    ["userspace"] = "°",
    --    ["performance"] = "⚡",
    --    ["conservative"] = "↯"
    --}

    -- Get the current frequency
    local ffreq = io.open("/sys/devices/system/cpu/"..cpuid.."/cpufreq/scaling_cur_freq")
    local freq = ffreq:read("*line")
    ffreq:close()

    -- Calculate MHz and GHz
    local freqmhz = freq / 1000
    local freqghz = freqmhz / 1000


    -- Get the current voltage
    local fvolt = io.open("/sys/devices/system/cpu/"..cpuid.."/cpufreq/scaling_voltages")
    for line in fvolt:lines() do
        if line:find("^"..freq) then
            voltagemv = line:match("[%d]+[%s]([%d]+)")
            break
        end
    end
    fvolt:close()

    -- Calculate voltage from mV
    local voltagev = voltagemv / 1000


    -- Get the current governor
    local fgov = io.open("/sys/devices/system/cpu/"..cpuid.."/cpufreq/scaling_governor")
    local governor = fgov:read("*line")
    fgov:close()

    -- Represent the governor as a symbol
    --local governor = governor_state[governor] or governor

    return {freqmhz, freqghz, voltagemv, voltagev, governor}
end
-- }}}

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