Commit 891f838b by Dwight Engen Committed by Serge Hallyn

lua: fix stats collection using get_cgroup_item

Previously, the lua stats collection was building its own paths to the cgroup files, which could be wrong depending on what --with-cgroup-pattern was passed to configure. Fix it to use the get_cgroup_item api so it always finds the files. Remove cgroup_path_get since it is not used anymore. Signed-off-by: 's avatarDwight Engen <dwight.engen@oracle.com> Signed-off-by: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com>
parent 80ee2228
...@@ -29,7 +29,6 @@ local io = require("io") ...@@ -29,7 +29,6 @@ local io = require("io")
module("lxc", package.seeall) module("lxc", package.seeall)
local lxc_path local lxc_path
local cgroup_path
local log_level = 3 local log_level = 3
-- lua 5.1 compat -- lua 5.1 compat
...@@ -70,31 +69,6 @@ function string:split(delim, max_cols) ...@@ -70,31 +69,6 @@ function string:split(delim, max_cols)
return cols return cols
end end
function cgroup_path_get()
local f,line,cgroup_path
f = io.open("/proc/mounts", "r")
if (f) then
while true do
local c
line = f:read()
if line == nil then
break
end
c = line:split(" ", 6)
if (c[1] == "cgroup") then
cgroup_path = core.dirname(c[2])
break
end
end
f:close()
end
if (not cgroup_path) then
cgroup_path = "/sys/fs/cgroup"
end
return cgroup_path
end
-- container class -- container class
container = {} container = {}
container_mt = {} container_mt = {}
...@@ -261,20 +235,19 @@ end ...@@ -261,20 +235,19 @@ end
-- methods for stats collection from various cgroup files -- methods for stats collection from various cgroup files
-- read integers at given coordinates from a cgroup file -- read integers at given coordinates from a cgroup file
function container:stat_get_ints(controller, item, coords) function container:stat_get_ints(item, coords)
local f = io.open(cgroup_path.."/"..controller.."/"..self.ctname.."/"..item, "r")
local lines = {} local lines = {}
local result = {} local result = {}
local flines = self:get_cgroup_item(item)
if (not f) then if (flines == nil) then
for k,c in ipairs(coords) do for k,c in ipairs(coords) do
table.insert(result, 0) table.insert(result, 0)
end end
else else
for line in f:lines() do for line in flines:gmatch("[^\r\n]+") do
table.insert(lines, line) table.insert(lines, line)
end end
f:close()
for k,c in ipairs(coords) do for k,c in ipairs(coords) do
local col local col
...@@ -287,27 +260,18 @@ function container:stat_get_ints(controller, item, coords) ...@@ -287,27 +260,18 @@ function container:stat_get_ints(controller, item, coords)
end end
-- read an integer from a cgroup file -- read an integer from a cgroup file
function container:stat_get_int(controller, item) function container:stat_get_int(item)
local f = io.open(cgroup_path.."/"..controller.."/"..self.ctname.."/"..item, "r") local line = self:get_cgroup_item(item)
if (not f) then
return 0
end
local line = f:read()
f:close()
-- if line is nil (on an error like Operation not supported because -- if line is nil (on an error like Operation not supported because
-- CONFIG_MEMCG_SWAP_ENABLED isn't enabled) return 0 -- CONFIG_MEMCG_SWAP_ENABLED isn't enabled) return 0
return tonumber(line) or 0 return tonumber(line) or 0
end end
function container:stat_match_get_int(controller, item, match, column) function container:stat_match_get_int(item, match, column)
local val local val
local f = io.open(cgroup_path.."/"..controller.."/"..self.ctname.."/"..item, "r") local lines = self:get_cgroup_item(item)
if (not f) then
return 0
end
for line in f:lines() do for line in lines:gmatch("[^\r\n]+") do
if (string.find(line, match)) then if (string.find(line, match)) then
local col local col
...@@ -315,20 +279,20 @@ function container:stat_match_get_int(controller, item, match, column) ...@@ -315,20 +279,20 @@ function container:stat_match_get_int(controller, item, match, column)
val = tonumber(col[column]) or 0 val = tonumber(col[column]) or 0
end end
end end
f:close()
return val return val
end end
function container:stats_get(total) function container:stats_get(total)
local stat = {} local stat = {}
stat.mem_used = self:stat_get_int("memory", "memory.usage_in_bytes") stat.mem_used = self:stat_get_int("memory.usage_in_bytes")
stat.mem_limit = self:stat_get_int("memory", "memory.limit_in_bytes") stat.mem_limit = self:stat_get_int("memory.limit_in_bytes")
stat.memsw_used = self:stat_get_int("memory", "memory.memsw.usage_in_bytes") stat.memsw_used = self:stat_get_int("memory.memsw.usage_in_bytes")
stat.memsw_limit = self:stat_get_int("memory", "memory.memsw.limit_in_bytes") stat.memsw_limit = self:stat_get_int("memory.memsw.limit_in_bytes")
stat.cpu_use_nanos = self:stat_get_int("cpuacct", "cpuacct.usage") stat.cpu_use_nanos = self:stat_get_int("cpuacct.usage")
stat.cpu_use_user, stat.cpu_use_user,
stat.cpu_use_sys = self:stat_get_ints("cpuacct", "cpuacct.stat", {{1, 2}, {2, 2}}) stat.cpu_use_sys = self:stat_get_ints("cpuacct.stat", {{1, 2}, {2, 2}})
stat.blkio = self:stat_match_get_int("blkio", "blkio.throttle.io_service_bytes", "Total", 2) stat.blkio = self:stat_match_get_int("blkio.throttle.io_service_bytes", "Total", 2)
if (total) then if (total) then
total.mem_used = total.mem_used + stat.mem_used total.mem_used = total.mem_used + stat.mem_used
...@@ -427,6 +391,5 @@ function M.default_config_path_get() ...@@ -427,6 +391,5 @@ function M.default_config_path_get()
end end
lxc_path = core.default_config_path_get() lxc_path = core.default_config_path_get()
cgroup_path = cgroup_path_get()
return M return M
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment