update to cgroups2; remove discovering (#1)

This commit is contained in:
Petr Kozorezov 2018-04-05 20:03:54 +03:00 committed by GitHub
parent d8c3f460d3
commit 5a87a37694
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 106 deletions

View File

@ -48,7 +48,7 @@ metrics_table() ->
start(_StartType, _StartArgs) -> start(_StartType, _StartArgs) ->
SupervisorArgs = [ SupervisorArgs = [
cgroup_root(), cgroup_discovery_file(), handler_modules() cgroup_root(), handler_modules()
], ],
ets:new( metrics_table(), [public, named_table] ), ets:new( metrics_table(), [public, named_table] ),
SupervisorStartRes = supervisor:start_link({local, cg_mon_sup}, cg_mon_app, SupervisorArgs), SupervisorStartRes = supervisor:start_link({local, cg_mon_sup}, cg_mon_app, SupervisorArgs),
@ -64,32 +64,14 @@ stop(_State) ->
ok. ok.
init([CgroupRoot, CgroupDiscovery, AvailableHandlers]) -> init([CgroupRoot, AvailableHandlers]) ->
SupFlags = {one_for_one, 5, 3600}, SupFlags = {one_for_one, 5, 3600},
case cg_mon_lib:discover_cgroups( CgroupRoot, CgroupDiscovery ) of Children =
{ok, CgroupsMap} -> [
HandlersMap = orddict:from_list([ child_spec(HandlerModule, filename:join(CgroupRoot, atom_to_list(HandlerModule:provided_resource())))
{HandlerModule:provided_resource(), HandlerModule} || HandlerModule <- AvailableHandlers
|| HandlerModule <- AvailableHandlers ],
]), {ok, {SupFlags, Children}}.
Children =
lists:foldl(
fun({Resource, Path}, Acc) ->
case orddict:find(Resource, HandlersMap) of
{ok, HandlerModule} ->
[ child_spec(HandlerModule, Path) | Acc ];
_ ->
Acc
end
end, [], CgroupsMap
),
{ok, {SupFlags, Children}};
{error, _Reason} ->
ignore
end.
%%%=================================================================== %%%===================================================================
@ -121,14 +103,6 @@ cgroup_root() ->
cg_mon_lib:cgroup_root() cg_mon_lib:cgroup_root()
end. end.
cgroup_discovery_file() ->
case application:get_env(cg_mon, cgroup_discovery_file) of
{ok, Value} when is_list(Value) ->
Value;
_ ->
cg_mon_lib:discovery_file()
end.
handler_modules() -> handler_modules() ->
[cg_mem_sup, cg_cpu_sup]. [cg_mem_sup, cg_cpu_sup].

View File

@ -10,11 +10,7 @@
%% API %% API
-export([ -export([
discovery_file/0,
cgroup_root/0, cgroup_root/0,
discover_cgroups/0,
discover_cgroups/1,
discover_cgroups/2,
read_cgroups_metrics/5 read_cgroups_metrics/5
]). ]).
@ -27,57 +23,10 @@
-type metric_source() :: StatFile :: string() | { Key :: atom(), ValueFile :: string()}. -type metric_source() :: StatFile :: string() | { Key :: atom(), ValueFile :: string()}.
-spec discovery_file() -> string().
discovery_file() ->
"/proc/self/cgroup".
-spec cgroup_root() -> string(). -spec cgroup_root() -> string().
cgroup_root() -> cgroup_root() ->
"/sys/fs/cgroup". "/sys/fs/cgroup".
-spec discover_cgroups() ->
{ok, cgroup_map()}
| {error, Reason :: atom()}.
discover_cgroups() ->
discover_cgroups(cgroup_root(), discovery_file()).
-spec discover_cgroups(Root :: string()) ->
{ok, cgroup_map()}
| {error, Reason :: atom()}.
discover_cgroups(Root) ->
discover_cgroups(Root, discovery_file()).
-spec discover_cgroups(Root :: string(), FileName :: string()) ->
{ok, cgroup_map()}
| {error, Reason :: atom()}.
discover_cgroups(Root, FileName) ->
with_file(
fun(DiscoveryFile) ->
parse_discovery_file(DiscoveryFile, Root)
end,
FileName
).
-spec parse_discovery_file(File :: file:io_device(), CgroupRoot :: string()) ->
{ok, cgroup_map()}
| {error, Reason :: atom()}.
parse_discovery_file(File, CgroupRoot) ->
foldl_lines(
fun(Line, Acc) ->
[_, Resources, GroupName] = string:tokens(Line, ":"),
ResourcesList = string:tokens(Resources, ","),
RelativeName = fixup_group_name(GroupName),
FullName = filename:join([CgroupRoot, Resources]) ++ RelativeName,
lists:foldl(
fun(ResourceName, TmpAcc) ->
[ {list_to_atom(ResourceName), FullName} | TmpAcc]
end, Acc, ResourcesList
)
end, [], File
).
-spec with_file(fun( (File :: file:io_device()) -> any() ), Filename :: string()) -> -spec with_file(fun( (File :: file:io_device()) -> any() ), Filename :: string()) ->
any(). any().
with_file(Fun, FileName) -> with_file(Fun, FileName) ->
@ -188,24 +137,6 @@ strip_line_ending(Line) ->
-include_lib("cgmon/include/test_utils.hrl"). -include_lib("cgmon/include/test_utils.hrl").
discovery_error_test() ->
Res = discover_cgroups(cgroup_root(), ?TEST_FILE("non-existent-file")),
?assertEqual(Res, {error, enoent}).
discovery_denied_test() ->
Res = discover_cgroups(cgroup_root(), "/root/some-not-permitted-file"),
?assertEqual(Res, {error, eacces}).
discovery_test_() ->
{ok, Res} = discover_cgroups(cgroup_root(), ?TEST_FILE("cgroup.discovery")),
[
?_assertEqual(4, length(Res)),
?_assertEqual("/sys/fs/cgroup/memory/foo/", proplists:get_value(memory, Res)),
?_assertEqual("/sys/fs/cgroup/cpu,cpuacct/bar/", proplists:get_value(cpu, Res)),
?_assertEqual("/sys/fs/cgroup/cpu,cpuacct/bar/", proplists:get_value(cpuacct, Res)),
?_assertEqual("/sys/fs/cgroup/cpuset/", proplists:get_value(cpuset, Res))
].
read_stat_file_error_test_() -> read_stat_file_error_test_() ->
[ [
@ -226,9 +157,6 @@ read_stat_file_test_() ->
?_assertEqual(1, proplists:get_value("cache", Result)) ?_assertEqual(1, proplists:get_value("cache", Result))
]. ].
read_cgroups_metrics_test_() -> read_cgroups_metrics_test_() ->
KeyValueSources = [ KeyValueSources = [
"memory.stat", "memory.stat",