[Erlang 0013]抓取Erlang进程运行时信息

来源:互联网 发布:如何制作抽奖软件 编辑:程序博客网 时间:2024/06/10 01:30

     [Erlang 0012]Erlang Process input queue 一文我们提到使用erlang:process_info/1抓取进程运行时状态信息,有时我们需要把节点内所有的进程的信息都抓取下来,便于分析整个节点内进程的运行状态,特别是挑出一些异常的进程:比如有些进程的reduction值和其它进程相比大出好几个数量级,那这个进程需要好好检查一下了。

    下面的代码就是把当前节点内所有进程遍历一遍,把进程状态写入到文本里面:

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()].      
 
运行一下看看,我们还要学会读这些数据:
[{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}]}, %%% erlang的垃圾回收是mark sweep方式。 major做全量的gc, minor就是不够内存的时候,缺多少就去回收下,力度比较小。
   {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}

最小堆大小

{min_bin_vheap_size, MinBinVHeapSize}

进程最小二进制虚拟堆大小

{monitored_by, Pids}

当前进程的监控进程列表

{priority, Level}

进程优先级,通过 process_flag(priority, Level).设置

{reductions, Number}

这个参数是进程负载的粗略评估指标.常备缩写为REds

Reduction is an approximate measure of how much CPU time they have used.

 

这个是一个计数,是Erlang虚拟机用来调度进程的一个依据,保证公平调度。
比如某个进程执行一个函数,reduction就会增加,执行gc reduction会进行减少。

 已回复 4月 13日 作者: litaocheng

{registered_name, Atom}

进程注册的名称.

{stack_size, Size}

进程栈大小(单位是word)

{status, Status}

进程状态,有exiting, garbage_collecting, waiting (for a message), running, runnable (ready to run, but another process is running), or suspended 

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

{trap_exit, Boolean}

是否为系统进程

 

 

 详情参见:http://www.erlang.org/doc/man/erlang.html#process_info-2

 

单个Erlang Process 占用多少内存?

An Erlang process is lightweight compared to operating systems threads and processes.

A newly spawned Erlang process uses 309 words of memory in the non-SMP emulator without HiPE support. (SMP support and HiPE support will both add to this size.) The size can be found out like this:

Erlang (BEAM) emulator version 5.6 [async-threads:0] [kernel-poll:false]Eshell V5.6  (abort with ^G)1> Fun = fun() -> receive after infinity -> ok end end.#Fun<...>2> {_,Bytes} = process_info(spawn(Fun), memory).{memory,1232}3> Bytes div erlang:system_info(wordsize).309

The size includes 233 words for the heap area (which includes the stack). The garbage collector will increase the heap as needed.

 

Link:http://www.erlang.org/doc/efficiency_guide/processes.html

 

 

原创粉丝点击