nickva commented on a change in pull request #761: Add a debugging utilities 
for listing processes
URL: https://github.com/apache/couchdb/pull/761#discussion_r134269131
 
 

 ##########
 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:
   There is a lists:mapfold function at least since R16:
   
   http://erldocs.com/R16B03-1/stdlib/lists.html?i=0&search=mapfold#mapfoldl/3
   
   Could it be possible to use that function here instead of a custom one?
 
----------------------------------------------------------------
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

Reply via email to