noahshaw11 commented on code in PR #3985:
URL: https://github.com/apache/couchdb/pull/3985#discussion_r877430718


##########
src/couch/src/couch_debug.erl:
##########
@@ -620,6 +676,90 @@ info_size(InfoKV) ->
         {binary, BinInfos} -> lists:sum([S || {_, S, _} <- BinInfos]);
         {_, V} -> V
     end.
+resource_hoggers(MemoryInfo, InfoKey) ->
+    KeyFun = fun
+        ({_Pid, _Id, undefined}) -> undefined;
+        ({_Pid, Id, DataMap}) -> {Id, [{InfoKey, maps:get(InfoKey, DataMap)}]}
+    end,
+    resource_hoggers(MemoryInfo, InfoKey, KeyFun).
+
+resource_hoggers(MemoryInfo, InfoKey, KeyFun) ->
+    HoggersData = resource_hoggers_data(MemoryInfo, InfoKey, KeyFun),
+    TableSpec = [
+        {50, centre, id},
+        {20, centre, InfoKey}
+    ],
+    print_table(HoggersData, TableSpec).
+
+resource_hoggers_data(MemoryInfo, InfoKey, KeyFun) when is_atom(InfoKey) ->
+    resource_hoggers_data(MemoryInfo, InfoKey, KeyFun, 20).
+
+resource_hoggers_data(MemoryInfo, InfoKey, KeyFun, N) when is_atom(InfoKey) 
and is_integer(N) ->
+    SortedTuples = resource_hoggers_data(MemoryInfo, InfoKey, KeyFun, 
undefined),
+    {TopN, _} = lists:split(N, SortedTuples),
+    TopN;
+resource_hoggers_data(MemoryInfo, InfoKey, KeyFun, undefined) when 
is_atom(InfoKey) ->
+    Tuples = lists:filtermap(
+        fun(Tuple) ->
+            case KeyFun(Tuple) of
+                undefined ->
+                    false;
+                Value ->
+                    {true, Value}
+            end
+        end,
+        MemoryInfo
+    ),
+    lists:reverse(lists:keysort(2, Tuples)).
+
+resource_hoggers_snapshot({N, MemoryInfo, InfoKeys} = _Snapshot) ->
+    Data = lists:filtermap(
+        fun({Pid, Id, Data}) ->
+            case memory_info(Pid, InfoKeys) of
+                {Pid, undefined, undefined} ->
+                    false;
+                {_, _, DataMap} ->
+                    {true, {Pid, Id, update_delta(Data, DataMap)}}
+            end
+        end,
+        MemoryInfo
+    ),
+    {N + 1, Data, InfoKeys};
+resource_hoggers_snapshot([]) ->
+    [];
+resource_hoggers_snapshot([{_Pid, _Id, Data} | _] = MemoryInfo) ->
+    resource_hoggers_snapshot({0, MemoryInfo, maps:keys(Data)}).
+
+update_delta({_, InitialDataMap}, DataMap) ->
+    update_delta(InitialDataMap, DataMap);
+update_delta(InitialDataMap, DataMap) ->
+    Delta = maps:fold(
+        fun(Key, Value, AccIn) ->
+            maps:put(Key, maps:get(Key, DataMap, Value) - Value, AccIn)
+        end,
+        maps:new(),
+        InitialDataMap
+    ),
+    {Delta, InitialDataMap}.
+
+analyze_resource_hoggers({N, Data, InfoKeys}, TopN) ->
+    io:format("Number of snapshots: ~p~n", [N]),
+    lists:map(
+        fun(InfoKey) ->
+            KeyFun = fun
+                ({_Pid, _Id, undefined}) -> undefined;
+                ({_Pid, Id, {Delta, _DataMap}}) -> {Id, [{InfoKey, 
maps:get(InfoKey, Delta)}]}

Review Comment:
   Tested it locally:
   ```
   
   ([email protected])6> couch_debug:analyze_resource_hoggers(RHS, 3).
   Number of snapshots: 1
   Top 3 by change in binary
   |                        id                        |              binary|    
           delta
   |             erl_prim_loader[<0.10.0>]            |               34373|    
           27493
   |                   init[<0.0.0>]                  |                   0|    
               0
   |             erts_code_purger[<0.1.0>]            |                   0|    
               0
   Top 3 by change in dictionary
   |                        id                        |          dictionary|    
           delta
   |             erlang:apply/2[<0.1205.0>]           |                   1|    
               4
   |                   init[<0.0.0>]                  |                   0|    
               0
   |             erts_code_purger[<0.1.0>]            |                   0|    
               0
   Top 3 by change in heap_size
   |                        id                        |           heap_size|    
           delta
   |             erlang:apply/2[<0.1205.0>]           |                4185|    
          192465
   |      couch_replicator_rate_limiter[<0.517.0>]    |                 233|    
            1365
   |                   init[<0.0.0>]                  |                2586|    
               0
   Top 3 by change in links
   |                        id                        |               links|    
           delta
   |                   init[<0.0.0>]                  |                   3|    
               0
   |             erts_code_purger[<0.1.0>]            |                   0|    
               0
   |    erts_literal_area_collector:start/0[<0.2.0>]  |                   0|    
               0
   Top 3 by change in memory
   |                        id                        |              memory|    
           delta
   |             erlang:apply/2[<0.1205.0>]           |              264040|    
         1311072
   |      couch_replicator_rate_limiter[<0.517.0>]    |                2832|    
           31608
   |    folsom_sample_slide_server:init/1[<0.235.0>]  |                2752|    
            3008
   Top 3 by change in message_queue_len
   |                        id                        |   message_queue_len|    
           delta
   |                   init[<0.0.0>]                  |                   0|    
               0
   |             erts_code_purger[<0.1.0>]            |                   0|    
               0
   |    erts_literal_area_collector:start/0[<0.2.0>]  |                   0|    
               0
   Top 3 by change in monitored_by
   |                        id                        |        monitored_by|    
           delta
   |                   init[<0.0.0>]                  |                   0|    
               0
   |             erts_code_purger[<0.1.0>]            |                   0|    
               0
   |    erts_literal_area_collector:start/0[<0.2.0>]  |                   0|    
               0
   Top 3 by change in monitors
   |                        id                        |            monitors|    
           delta
   |                   init[<0.0.0>]                  |                   0|    
               0
   |             erts_code_purger[<0.1.0>]            |                   0|    
               0
   |    erts_literal_area_collector:start/0[<0.2.0>]  |                   0|    
               0
   Top 3 by change in stack_size
   |                        id                        |          stack_size|    
           delta
   |                   init[<0.0.0>]                  |                   3|    
               0
   |             erts_code_purger[<0.1.0>]            |                   2|    
               0
   |    erts_literal_area_collector:start/0[<0.2.0>]  |                   6|    
               0
   Top 3 by change in total_heap_size
   |                        id                        |     total_heap_size|    
           delta
   |             erlang:apply/2[<0.1205.0>]           |               32875|    
          163884
   |      couch_replicator_rate_limiter[<0.517.0>]    |                 233|    
            3951
   |    folsom_sample_slide_server:init/1[<0.235.0>]  |                 233|    
             376
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to