erlang 系统的相关管理

一、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,[]}]
这些字段的含义:
1、{current_function, {Module, Function, Args}}
当前进程调用的方法M F A
2、{dictionary, Dictionary}
当前进程的进程字典数据,调试过程中我们可以为进程添加一些特殊标记来识别进程
3、{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.
4、{group_leader, GroupLeader}
GroupLeader决定了最终的信息输出在什么地方,比如rpc会把远程执行的结果采集过来在当前 shell显示,就用到这个GroupLeader
5、{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.
6、{initial_call, {Module, Function, Arity}}
Module, Function, Arity is the initial function call with which the process was spawned.
7、{links, Pids}
关联进程列表
8、{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.
9、{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.
10、{message_queue_len, MessageQueueLen}
这就是上文中我们特别关注的消息队列的长度
11、{messages, MessageQueue}
堆积消息列表
12、{min_heap_size, MinHeapSize}
最小堆大小
13、{min_bin_vheap_size, MinBinVHeapSize}
进程最小二进制虚拟堆大小
14、{monitored_by, Pids}
当前进程的监控进程列表
15、{priority, Level}
进程优先级,通过 process_flag(priority, Level).设置
16、{reductions, Number}
这个参数是进程负载的粗略评估指标.常备缩写为REds
17、{registered_name, Atom}
进程注册的名称.
17、{stack_size, Size}
进程栈大小(单位是word)
18、{status, Status}
进程状态,有exiting, garbage_collecting, waiting (for a message), running, runnable (ready to run, but another process is running), or suspended 
19、{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()].   

二、 获取Erlang系统信息的代码片段

1、查看进程数目是否正常? erlang:system_info(process_count).
2、查看节点的内存消耗在什么地方? > erlang:memory(). [{total,2099813400}, {processes,1985444264}, {processes_used,1985276128}, {system,114369136}, {atom,4479545}, {atom_used,4477777}, {binary,22756952}, {code,10486554}, {ets,47948808}]
3、查看哪些进程占用内存最高? > spawn(fun() -> etop:start([{output, text}, {interval, 1}, {lines, 20}, {sort, memory}]) end). (以输出text方式启动etop,其间隔为1秒,输出行数为20行,按照内存排序. 这里spawn一个新进程,目的是输出etop数据时不影响erlang shell 输入.) etop输出有点乱,超过一定范围变成了**,不过我们已经找到了内存占用最高的进程.

4、查看占用内存最高的进程状态 > 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, 太夸张了!

5、手动gc回收,希望问题可以解决 > erlang:garbage_collect(pid(0,12571,0)). true

6、 erlang:process_info(self(), memory). 
        {memory,1244}
7、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.   

你可能感兴趣的:(erlang 系统的相关管理)