Skip to content

Instantly share code, notes, and snippets.

@dizzyd
Created August 25, 2010 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dizzyd/549780 to your computer and use it in GitHub Desktop.
Save dizzyd/549780 to your computer and use it in GitHub Desktop.
-module(dm).
-compile(export_all).
%%
%% Identify the initial call of a PID; look first in pdict for the
%% value set by OTP-compliant procs, or fallback to lower-level info
%% if that's not available.
%%
ic(Pid) when is_pid(Pid) ->
case process_info(Pid, dictionary) of
undefined ->
undefined;
{dictionary, []} ->
{initial_call, Call} = process_info(Pid, initial_call),
Call;
{dictionary, Dict} ->
case orddict:find('$initial_call', orddict:from_list(Dict)) of
{ok, V} ->
V;
error ->
{initial_call, Call} = process_info(Pid, initial_call),
Call
end
end.
%%
%% Identify the initial call of a PID in string form. See also c:pid/3.
%%
ic(P0, P1, P2) ->
ic(c:pid(P0, P1, P2)).
%%
%% Convert a # of words to bytes
%%
words_to_bytes(Words) ->
erlang:system_info(wordsize) * Words.
%%
%% Return basic memory information for a PID, in bytes.
%%
proc_mi(Pid) when is_pid(Pid) ->
case process_info(Pid, [total_heap_size, heap_size]) of
undefined ->
undefined;
[{total_heap_size, Sz1}, {heap_size, Sz2}] ->
{words_to_bytes(Sz1), words_to_bytes(Sz2)}
end.
%%
%% Return basic memory information for a PID in string form. See also c:pid/3.
%%
proc_mi(P0, P1, P2) ->
proc_mi(c:pid(P0, P1, P2)).
%%
%% Identify most memory intensive processes; returns:
%% [{pid(), InitialCall, {TotalHeapSzBytes, HeapSzBytes}]
%%
heavy_procs(Count) ->
All = [{P, ic(P), proc_mi(P)} || P <- processes()],
Sorted = lists:reverse(lists:keysort(3, All)),
case Count of
all ->
Sorted;
Count ->
lists:sublist(Sorted, Count)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment