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


##########
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:
   In fact maybe there is a way to show both (current and delta). This is what 
I tried locally:
   
   ```erlang
   diff --git a/src/couch/src/couch_debug.erl b/src/couch/src/couch_debug.erl
   index f93759503..c39cbf5f6 100644
   --- a/src/couch/src/couch_debug.erl
   +++ b/src/couch/src/couch_debug.erl
   @@ -709,13 +709,17 @@ analyze_resource_hoggers({N, Data, InfoKeys}, TopN) ->
            fun(InfoKey) ->
                KeyFun = fun
                    ({_Pid, _Id, undefined}) -> undefined;
   -                ({_Pid, Id, {_Delta, DataMap}}) -> {Id, [{InfoKey, 
maps:get(InfoKey, DataMap)}]}
   +                ({_Pid, Id, {Delta, DataMap}}) -> {Id, [
   +                    {InfoKey, maps:get(InfoKey, DataMap)},
   +                    {delta, maps:get(InfoKey, Delta)}
   +                ]}
                end,
                io:format("Top ~p by ~p~n", [TopN, InfoKey]),
   -            HoggersData = resource_hoggers_data(Data, InfoKey, KeyFun, 
TopN),
   +            HoggersData = resource_hoggers_data(Data, delta, KeyFun, TopN),
                TableSpec = [
                    {50, centre, id},
   -                {20, centre, InfoKey}
   +                {20, right, InfoKey},
   +                {20, right, delta}
                ],
                print_table(HoggersData, TableSpec)
            end,
   ```
   
   Unfortunately there is a bug somewhere. Because sorting is done by `InfoKey` 
not `delta`.
   
   ```
   MI = couch_debug:memory_info(erlang:processes()).
   timer:sleep(3000).
   S0 = couch_debug:resource_hoggers_snapshot(MI).
   timer:sleep(3000).
   S1 = couch_debug:resource_hoggers_snapshot(S0).
   couch_debug:analyze_resource_hoggers(S1, 10).
   ```
   
   ```
   Top 10 by total_heap_size
   |                        id                        |     total_heap_size|    
           delta
   |         couch_prometheus_server[<0.280.0>]       |              150226|    
          -28690
   |                 config[<0.137.0>]                |              139267|    
               0
   |             erlang:apply/2[<0.1232.0>]           |               86201|    
          428636
   |               code_server[<0.51.0>]              |               86071|    
               0
   |            couch_log_server[<0.168.0>]           |               36259|    
               0
   |         couch_stats_aggregator[<0.184.0>]        |               35462|    
               0
   |             erlang:apply/2[<0.1231.0>]           |               32932|    
          800170
   |  ouch_epi_functions_gen_chttpd_handlers[<0.146.0>|               32875|    
               0
   |             erl_prim_loader[<0.10.0>]            |               24503|    
               0
   |     couch_epi_data_gen_flags_config[<0.159.0>]   |               21920|    
            2585
   ```



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