From 5a87a37694e42b6592d3b4164ae54e0e87e24e18 Mon Sep 17 00:00:00 2001 From: Petr Kozorezov Date: Thu, 5 Apr 2018 20:03:54 +0300 Subject: [PATCH] update to cgroups2; remove discovering (#1) --- src/cg_mon_app.erl | 42 ++++++--------------------- src/cg_mon_lib.erl | 72 ---------------------------------------------- 2 files changed, 8 insertions(+), 106 deletions(-) diff --git a/src/cg_mon_app.erl b/src/cg_mon_app.erl index e05a1a9..78f0657 100644 --- a/src/cg_mon_app.erl +++ b/src/cg_mon_app.erl @@ -48,7 +48,7 @@ metrics_table() -> start(_StartType, _StartArgs) -> SupervisorArgs = [ - cgroup_root(), cgroup_discovery_file(), handler_modules() + cgroup_root(), handler_modules() ], ets:new( metrics_table(), [public, named_table] ), SupervisorStartRes = supervisor:start_link({local, cg_mon_sup}, cg_mon_app, SupervisorArgs), @@ -64,32 +64,14 @@ stop(_State) -> ok. -init([CgroupRoot, CgroupDiscovery, AvailableHandlers]) -> +init([CgroupRoot, AvailableHandlers]) -> SupFlags = {one_for_one, 5, 3600}, - case cg_mon_lib:discover_cgroups( CgroupRoot, CgroupDiscovery ) of - {ok, CgroupsMap} -> - HandlersMap = orddict:from_list([ - {HandlerModule:provided_resource(), HandlerModule} - || HandlerModule <- AvailableHandlers - ]), - 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. - - - + Children = + [ + child_spec(HandlerModule, filename:join(CgroupRoot, atom_to_list(HandlerModule:provided_resource()))) + || HandlerModule <- AvailableHandlers + ], + {ok, {SupFlags, Children}}. %%%=================================================================== @@ -121,14 +103,6 @@ cgroup_root() -> cg_mon_lib:cgroup_root() 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() -> [cg_mem_sup, cg_cpu_sup]. diff --git a/src/cg_mon_lib.erl b/src/cg_mon_lib.erl index 293605e..bb9141d 100644 --- a/src/cg_mon_lib.erl +++ b/src/cg_mon_lib.erl @@ -10,11 +10,7 @@ %% API -export([ - discovery_file/0, cgroup_root/0, - discover_cgroups/0, - discover_cgroups/1, - discover_cgroups/2, read_cgroups_metrics/5 ]). @@ -27,57 +23,10 @@ -type metric_source() :: StatFile :: string() | { Key :: atom(), ValueFile :: string()}. --spec discovery_file() -> string(). -discovery_file() -> - "/proc/self/cgroup". - -spec cgroup_root() -> string(). cgroup_root() -> "/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()) -> any(). with_file(Fun, FileName) -> @@ -188,24 +137,6 @@ strip_line_ending(Line) -> -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_() -> [ @@ -226,9 +157,6 @@ read_stat_file_test_() -> ?_assertEqual(1, proplists:get_value("cache", Result)) ]. - - - read_cgroups_metrics_test_() -> KeyValueSources = [ "memory.stat",