iilyak commented on a change in pull request #761: Add a debugging utilities
for listing processes
URL: https://github.com/apache/couchdb/pull/761#discussion_r134300955
##########
File path: src/couch/src/couch_debug.erl
##########
@@ -50,3 +62,215 @@ opened_files_contains(FileNameFragment) ->
lists:filter(fun({_Port, _Pid, _Fd, Path}) ->
string:str(Path, FileNameFragment) > 0
end, couch_debug:opened_files()).
+
+process_name(Pid) ->
+ case process_info(Pid, registered_name) of
+ {registered_name, Name} ->
+ iolist_to_list(io_lib:format("~s[~p]", [Name, Pid]));
+ _ ->
+ {dictionary, Dict} = process_info(Pid, dictionary),
+ case proplists:get_value('$initial_call', Dict) of
+ undefined ->
+ {initial_call, {M, F, A}} = process_info(Pid,
initial_call),
+ iolist_to_list(io_lib:format("~p:~p/~p[~p]", [M, F, A,
Pid]));
+ {M, F, A} ->
+ iolist_to_list(io_lib:format("~p:~p/~p[~p]", [M, F, A,
Pid]))
+ end
+ end.
+
+iolist_to_list(List) ->
+ binary_to_list(iolist_to_binary(List)).
+
+link_tree(RootPid) ->
+ link_tree(RootPid, []).
+
+link_tree(RootPid, Info) ->
+ link_tree(RootPid, Info, fun(_, Props) -> Props end).
+
+link_tree(RootPid, Info, Fun) ->
+ {_, Result} = link_tree(
+ RootPid, [links | Info], gb_trees:empty(), 0, [RootPid], Fun),
+ Result.
+
+link_tree(RootPid, Info, Visited0, Pos, [Pid | Rest], Fun) ->
+ case gb_trees:lookup(Pid, Visited0) of
+ {value, Props} ->
+ {Visited0, [{Pos, {Pid, Fun(Pid, Props), []}}]};
+ none when RootPid =< Pid ->
+ Props = info(Pid, Info),
+ Visited1 = gb_trees:insert(Pid, Props, Visited0),
+ {links, Children} = lists:keyfind(links, 1, Props),
+ {Visited2, NewTree} = link_tree(
+ RootPid, Info, Visited1, Pos + 1, Children, Fun),
+ {Visited3, Result} = link_tree(
+ RootPid, Info, Visited2, Pos, Rest, Fun),
+ {Visited3, [{Pos, {Pid, Fun(Pid, Props), NewTree}}] ++ Result};
+ none ->
+ Props = info(Pid, Info),
+ Visited1 = gb_trees:insert(Pid, Props, Visited0),
+ {Visited2, Result} = link_tree(
+ RootPid, Info, Visited1, Pos, Rest, Fun),
+ {Visited2, [{Pos, {Pid, Fun(Pid, Props), []}}] ++ Result}
+ end;
+link_tree(_RootPid, _Info, Visited, _Pos, [], _Fun) ->
+ {Visited, []}.
+
+
+info(Pid, Info) when is_pid(Pid) ->
+ ValidProps = [
+ backtrace,
+ binary,
+ catchlevel,
+ current_function,
+ current_location,
+ current_stacktrace,
+ dictionary,
+ error_handler,
+ garbage_collection,
+ garbage_collection_info,
+ group_leader,
+ heap_size,
+ initial_call,
+ links,
+ last_calls,
+ memory,
+ message_queue_len,
+ messages,
+ min_heap_size,
+ min_bin_vheap_size,
+ monitored_by,
+ monitors,
+ message_queue_data,
+ priority,
+ reductions,
+ registered_name,
+ sequential_trace_token,
+ stack_size,
+ status,
+ suspending,
+ total_heap_size,
+ trace,
+ trap_exit
+ ],
+ Validated = lists:filter(fun(P) -> lists:member(P, ValidProps) end, Info),
+ process_info(Pid, lists:usort(Validated));
+info(Port, Info) when is_port(Port) ->
+ ValidProps = [
+ registered_name,
+ id,
+ connected,
+ links,
+ name,
+ input,
+ output,
+ os_pid
+ ],
+ Validated = lists:filter(fun(P) -> lists:member(P, ValidProps) end, Info),
+ erlang:port_info(Port, lists:usort(Validated)).
+
+mapfold([], Acc, _Fun) ->
Review comment:
this custom `mapfold` traverses the tree not a list. The tree nodes are
`{Pos, {Key, Value, SubTree0}}`. Where `Pos` is a level which is useful to do
indented printout for example. `Value` is information collected by callback
function passed to `link_tree/2`. I'll add some documentation. The reason we
pass the collection function is due to transient nature of processes. It is
better to collect all info about the process at once. The worse alternative is
to get list of processes and then call process_info when we try to print the
result. By that time some of the processes might cease to exist.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services