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


##########
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:
   The sorting is done by `InfoKey` instead of `Delta` because the sorting 
occurs in `resource_hoggers_data`: 
https://github.com/apache/couchdb/pull/3985/files#diff-4806d6fee449d8007647f577455783157f17ce2c886eb66b1fa9f60acb05cc40R713



-- 
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: notifications-unsubscr...@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to