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


##########
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:
   I replaced the `lists:reverse(...` line with the following:
   
   ```
       lists:reverse(lists:sort(fun({_, A}, {_, B}) -> %% do we always have 
tuple with 2 elements?
           lists:keyfind(InfoKey, 1, A) < lists:keyfind(InfoKey, 1, B)
       end, Tuples)).
   ```
   
   The results are:
   
   ```
   Top 10 by heap_size
   |                        id                        |           heap_size|    
           delta
   |             erlang:apply/2[<0.1657.0>]           |                4185|    
          314002
   |             erlang:apply/2[<0.1658.0>]           |               10958|    
          185692
   |         couch_prometheus_server[<0.280.0>]       |               46422|    
           28691
   |                 chttpd[<0.868.0>]                |                 987|    
            1599
   |            couch_file:init/1[<0.459.0>]          |                 987|    
             611
   |                   init[<0.0.0>]                  |                1598|    
               0
   |             erts_code_purger[<0.1.0>]            |                 233|    
               0
   |    erts_literal_area_collector:start/0[<0.2.0>]  |                 233|    
               0
   |  rts_dirty_process_signal_handler:start/0[<0.3.0>|                 233|    
               0
   |  rts_dirty_process_signal_handler:start/0[<0.4.0>|                 233|    
               0
   ```
   



-- 
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