cpuinf: rewritten and optimized

This commit is contained in:
Adrian C. (anrxc) 2010-03-13 03:24:37 +01:00
parent 96a8e557d3
commit 9338cb930b
1 changed files with 14 additions and 12 deletions

View File

@ -7,7 +7,7 @@
local tonumber = tonumber local tonumber = tonumber
local io = { lines = io.lines } local io = { lines = io.lines }
local setmetatable = setmetatable local setmetatable = setmetatable
local string = { match = string.match } local string = { gmatch = string.gmatch }
-- }}} -- }}}
@ -17,21 +17,23 @@ module("vicious.cpuinf")
-- {{{ CPU Information widget type -- {{{ CPU Information widget type
local function worker(format) local function worker(format)
local cpu_id = nil local id = nil
local cpu_info = {} local cpu_info = {}
-- Get CPU info -- Get CPU info
for line in io.lines("/proc/cpuinfo") do for line in io.lines("/proc/cpuinfo") do
if string.match(line, "^processor.*") then for k, v in string.gmatch(line, "([%a%s]+)[%s]+:[%s]([%d]+).-$") do
cpu_id = string.match(line, "([%d]+)") if k == "processor" then
elseif string.match(line, "^cpu MHz.*") then id = v
local cpu_speed = tonumber(string.match(line, "([%d]+)%.")) elseif k == "cpu MHz\t" or k == "cpu MHz" then
cpu_info["{cpu"..cpu_id.." mhz}"] = cpu_speed local speed = tonumber(v)
cpu_info["{cpu"..cpu_id.." ghz}"] = cpu_speed / 1000 cpu_info["{cpu"..id.." mhz}"] = speed
elseif string.match(line, "^cache size.*") then cpu_info["{cpu"..id.." ghz}"] = speed / 1000
local cpu_cache = tonumber(string.match(line, "([%d]+)[%s]KB")) elseif k == "cache size" then
cpu_info["{cpu"..cpu_id.." kb}"] = cpu_cache local cache = tonumber(v)
cpu_info["{cpu"..cpu_id.." mb}"] = cpu_cache / 1024 cpu_info["{cpu"..id.." kb}"] = cache
cpu_info["{cpu"..id.." mb}"] = cache / 1024
end
end end
end end