1、erlang:process_info(Pid).
erlang:process_info(pid(0,33,0)).
獲取erlang進程的信息,運行下看看返回值:
[{registered_name,rex},
{current_function,{gen_server,loop,6}},
{initial_call,{proc_lib,init_p,5}},
{status,waiting},
{message_queue_len,0},
{messages,[]},
{links,[<0.10.0>]},
{dictionary,[{'$ancestors',[kernel_sup,<0.9.0>]},
{'$initial_call',{rpc,init,1}}]},
{trap_exit,true},
{error_handler,error_handler},
{priority,normal},
{group_leader,<0.8.0>},
{total_heap_size,28657},
{heap_size,10946},
{stack_size,9},
{reductions,13905},
{garbage_collection,[{min_bin_vheap_size,10946},
{min_heap_size,10946},
{fullsweep_after,65535},
{minor_gcs,2}]},
{suspending,[]}]
這些字段的含義:
一、{current_function, {Module, Function, Args}}
當前進程調用的方法M F A
二、{dictionary, Dictionary}
當前進程的進程字典數據,調試過程當中咱們能夠爲進程添加一些特殊標記來識別進程
三、{garbage_collection, GCInfo}
GCInfo is a list which contains miscellaneous information about garbage collection for this process. The content of GCInfo may be changed without prior notice.
四、{group_leader, GroupLeader}
GroupLeader決定了最終的信息輸出在什麼地方,好比rpc會把遠程執行的結果採集過來在當前 shell顯示,就用到這個GroupLeader
五、{heap_size, Size}
Size is the size in words of youngest heap generation of the process. This generation currently include the stack of the process. This information is highly implementation dependent, and may change if the implementation change.
六、{initial_call, {Module, Function, Arity}}
Module, Function, Arity is the initial function call with which the process was spawned.
七、{links, Pids}
關聯進程列表
八、{last_calls, false|Calls}
The value is false if call saving is not active for the process (see process_flag/3). If call saving is active, a list is returned, in which the last element is the most recent called.
{memory, Size}
Size is the size in bytes of the process. This includes call stack, heap and internal structures.
九、{message_binary, BinInfo}
BinInfo is a list containing miscellaneous information about binaries currently being referred to by the message area. This InfoTuple is only valid on an emulator using the hybrid heap type. This InfoTuple may be changed or removed without prior notice.
十、{message_queue_len, MessageQueueLen}
這就是上文中咱們特別關注的消息隊列的長度
十一、{messages, MessageQueue}
堆積消息列表
十二、{min_heap_size, MinHeapSize}
最小堆大小
1三、{min_bin_vheap_size, MinBinVHeapSize}
進程最小二進制虛擬堆大小
1四、{monitored_by, Pids}
當前進程的監控進程列表
1五、{priority, Level}
進程優先級,經過 process_flag(priority, Level).設置
1六、{reductions, Number}
這個參數是進程負載的粗略評估指標.常備縮寫爲REds
1七、{registered_name, Atom}
進程註冊的名稱.
1七、{stack_size, Size}
進程棧大小(單位是word)
1八、{status, Status}
進程狀態,有exiting, garbage_collecting, waiting (for a message), running, runnable (ready to run, but another process is running), or suspended
1九、{total_heap_size, Size}
Size is the total size in words of all heap fragments of the process. This currently include the stack of the process.
20、{trap_exit, Boolean}
是否爲系統進程
下面的代碼就是把當前節點內全部進程遍歷一遍,把進程狀態寫入到文本里面:
process_infos() ->
filelib:ensure_dir("./log/"),
File = "./log/processes_infos.log",
{ok, Fd} = file:open(File, [write, raw, binary, append]),
Fun = fun(Pi) ->
Info = io_lib:format("=>~p \n\n",[Pi]),
case filelib:is_file(File) of
true -> file:write(Fd, Info);
false ->
file:close(Fd),
{ok, NewFd} = file:open(File, [write, raw, binary, append]),
file:write(NewFd, Info)
end,
timer:sleep(20)
end,
[ Fun(erlang:process_info(P)) || P <- erlang:processes()].
2、
獲取Erlang系統信息的代碼片斷
一、查看進程數目是否正常? erlang:system_info(process_count).
二、查看節點的內存消耗在什麼地方? > erlang:memory(). [{total,2099813400}, {processes,1985444264}, {processes_used,1985276128}, {system,114369136}, {atom,4479545}, {atom_used,4477777}, {binary,22756952}, {code,10486554}, {ets,47948808}]
三、查看哪些進程佔用內存最高? > spawn(fun() -> etop:start([{output, text}, {interval, 1}, {lines, 20}, {sort, memory}]) end). (以輸出text方式啓動etop,其間隔爲1秒,輸出行數爲20行,按照內存排序. 這裏spawn一個新進程,目的是輸出etop數據時不影響erlang shell 輸入.) etop輸出有點亂,超過必定範圍變成了**,不過咱們已經找到了內存佔用最高的進程.
四、查看佔用內存最高的進程狀態 > erlang:process_info(pid(0,12571,0)). [{current_function,{mod_player,send_msg,2}}, {initial_call,{erlang,apply,2}}, {status,waiting}, {message_queue_len,0}, {messages,[]}, {links,[<0.12570.0>]}, {dictionary,[]}, {trap_exit,false}, {error_handler,error_handler}, {priority,normal}, {group_leader,<0.46.0>}, {total_heap_size,12538050}, {heap_size,12538050}, {stack_size,10122096}, {reductions,3795950}, {garbage_collection,[{min_bin_vheap_size,46368}, {min_heap_size,233}, {fullsweep_after,65535}, {minor_gcs,0}]}, {suspending,[]}] 其中」 {total_heap_size,12538050},」 表示佔用內存爲 12358050 words(32位系統word size爲4,64位系統word size爲8, 能夠經過erlang:system_info(wordsize) 查看),在64位系統下將近100M, 太誇張了!
五、手動gc回收,但願問題能夠解決 > erlang:garbage_collect(pid(0,12571,0)). true
六、 erlang:process_info(self(), memory).
{memory,1244}
七、SchedId = erlang:system_info(scheduler_id), SchedNum = erlang:system_info(schedulers), ProcCount = erlang:system_info(process_count), ProcLimit = erlang:system_info(process_limit), ProcMemUsed = erlang:memory(processes_used), ProcMemAlloc = erlang:memory(processes), MemTot = erlang:memory(total), io:format("abormal termination: " "~n Scheduler id: ~p" "~n Num scheduler: ~p" "~n Process count: ~p" "~n Process limit: ~p" "~n Memory used by erlang processes: ~p" "~n Memory allocated by erlang processes: ~p" "~n The total amount of memory allocated: ~p" "~n ", [SchedId, SchedNum, ProcCount, ProcLimit, ProcMemUsed, ProcMemAlloc, MemTot ]),
ok.