From 1686757b304b66050f54db8e0ce5ab758bde96af Mon Sep 17 00:00:00 2001 From: Jay Nelson Date: Mon, 25 Jan 2010 11:01:32 -0800 Subject: Add count_children/1 to supervisor.erl to determine the number of children being managed without the memory impact of which_children/1 The function which_children/1 returns a list of the child processes currently being supervised, but it has the penalty of creating a new list thereby consuming more memory. In low memory situations it is often desirable to know which supervisor may have generated many processes, but the act of discovering the culprit should not cause the node to crash (or worse a different node if the kernel kills one randomly). The new function count_children/1 can give an indication of which supervisor is taxing resources the most without adding to the burden. Rather than creating a new list, it walks the supervisor's internal children structure using an accumulator function so that any used memory can be incrementally collected yet the resulting count can still be obtained. The return result of count_children/1 is a property list of counts containing: - {specs, Total_Num_Child_Specs} - {active, Num_Active_Child_Processes_Of_Supervisor_Or_Worker_Type} - {supervisors, Num_Supervisor_Type_Children_Including_Dead_Processes} - {workers, Num_Worker_Type_Children_Including_Dead_Processes} This patch was made in response to mailing list discussions of the problem diagnosing heavily taxed production systems. I cannot find the original request, but http://www.erlang.org/cgi-bin/ezmlm-cgi/4/35060 is my original post of the patch. --- lib/stdlib/src/supervisor.erl | 49 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'lib/stdlib/src/supervisor.erl') diff --git a/lib/stdlib/src/supervisor.erl b/lib/stdlib/src/supervisor.erl index fb1303d1eb..342b71f2da 100644 --- a/lib/stdlib/src/supervisor.erl +++ b/lib/stdlib/src/supervisor.erl @@ -24,7 +24,7 @@ -export([start_link/2,start_link/3, start_child/2, restart_child/2, delete_child/2, terminate_child/2, - which_children/1, + which_children/1, count_children/1, check_childspecs/1]). -export([behaviour_info/1]). @@ -95,6 +95,9 @@ terminate_child(Supervisor, Name) -> which_children(Supervisor) -> call(Supervisor, which_children). +count_children(Supervisor) -> + call(Supervisor, count_children). + call(Supervisor, Req) -> gen_server:call(Supervisor, Req, infinity). @@ -297,7 +300,49 @@ handle_call(which_children, _From, State) -> {Name, Pid, ChildType, Mods} end, State#state.children), - {reply, Resp, State}. + {reply, Resp, State}; + +handle_call(count_children, _From, State) when ?is_simple(State) -> + [#child{child_type = CT}] = State#state.children, + {Active, Count} = + ?DICT:fold(fun(Pid, _Val, {Alive, Tot}) -> + if is_pid(Pid) -> {Alive+1, Tot +1}; + true -> {Alive, Tot + 1} end + end, {0, 0}, State#state.dynamics), + Reply = case CT of + supervisor -> [{specs, 1}, {active, Active}, + {supervisors, Count}, {workers, 0}]; + worker -> [{specs, 1}, {active, Active}, + {supervisors, 0}, {workers, Count}] + end, + {reply, Reply, State}; + +handle_call(count_children, _From, State) -> + + %% Specs and children are together on the children list... + {Specs, Active, Supers, Workers} = + lists:foldl(fun(Child, Counts) -> + count_child(Child, Counts) + end, {0,0,0,0}, State#state.children), + + %% Reformat counts to a property list. + Reply = [{specs, Specs}, {active, Active}, + {supervisors, Supers}, {workers, Workers}], + {reply, Reply, State}. + + +count_child(#child{pid = Pid, child_type = worker}, + {Specs, Active, Supers, Workers}) -> + case is_pid(Pid) andalso is_process_alive(Pid) of + true -> {Specs+1, Active+1, Supers, Workers+1}; + false -> {Specs+1, Active, Supers, Workers+1} + end; +count_child(#child{pid = Pid, child_type = supervisor}, + {Specs, Active, Supers, Workers}) -> + case is_pid(Pid) andalso is_process_alive(Pid) of + true -> {Specs+1, Active+1, Supers+1, Workers}; + false -> {Specs+1, Active, Supers+1, Workers} + end. %%% Hopefully cause a function-clause as there is no API function -- cgit v1.2.3