Getting Process Information of Erlang Runtime System

In an erlang shell we can get process information by calling i().

1> i().
Pid       Initial Call                        Heap    Reds  Msgs  Registered             Current Function                Stack
   otp_ring0:start/2                    610    2419     0  init                   init:loop/1                         2
   erlang:apply/2                      1597  169165     0  erl_prim_loader        erl_prim_loader:loop/3              6
   gen_event:init_it/6                  377     226     0  error_logger           gen_event:fetch_msg/5               8
   erlang:apply/2                      1597     444     0  application_controlle  gen_server:loop/6                   7
   application_master:init/4            377      44     0                         application_master:main_loop/2      6
   application_master:start_it/4        233      72     0                         application_master:loop_it/4        5
  supervisor:kernel/1                  987    1413     0  kernel_sup             gen_server:loop/6                   9
  rpc:init/1                           233      36     0  rex                    gen_server:loop/6                   9
  global:init/1                        233      53     0  global_name_server     gen_server:loop/6                   9
  erlang:apply/2                       233      21     0                         global:loop_the_locker/1            4
  erlang:apply/2                       233       4     0                         global:collect_deletions/2          6
  erlang:apply/2                       233       3     0                         global:loop_the_registrar/0         2
  inet_db:init/1                       233     255     0  inet_db                gen_server:loop/6                   9
  global_group:init/1                  233      60     0  global_group           gen_server:loop/6                   9
  file_server:init/1                   233      86     0  file_server_2          gen_server:loop/6                   9
  erlang:apply/2                      6765   99301     0  code_server            code_server:loop/1                  3
  supervisor_bridge:standard_error/    233      40     0  standard_error_sup     gen_server:loop/6                   9
  standard_error:server/2              233       7     0  standard_error         standard_error:server_loop/1        2
  supervisor_bridge:user_sup/1         233      59     0                         gen_server:loop/6                   9
  user_drv:server/2                    987     596     1  user_drv               user_drv:server_loop/5              8
  group:server/3                       233      38     0  user                   group:server_loop/3                 4
  group:server/3                      2584   11212     0                         group:server_loop/3                 4
  erlang:apply/2                     17711    4410     0                         shell:shell_rep/4                  17
  kernel_config:init/1                 233     268     0                         gen_server:loop/6                   9
  supervisor:kernel/1                  233      57     0  kernel_safe_sup        gen_server:loop/6                   9
  erlang:apply/2                      1597   10157     0                         c:pinfo/1                          50
Total                                        38684  300446     1                                                           224
ok

The function i() obtains a pid list by calling erlang:processes/0. Then it calls erlang:process_info/1 on each pid.
[erlang:process_info(Pid)|| Pid <- erlang:processes()].

The function erlang:process_info/1 provides some information about a process.

2> erlang:process_info(pid(0,0,0)).
[{registered_name,init},
 {current_function,{init,loop,1}},
 {initial_call,{otp_ring0,start,2}},
 {status,waiting},
 {message_queue_len,0},
 {messages,[]},
 {links,[,,]},
 {dictionary,[]},
 {trap_exit,true},
 {error_handler,error_handler},
 {priority,normal},
 {group_leader,},
 {total_heap_size,987},
 {heap_size,610},
 {stack_size,2},
 {reductions,2419},
 {garbage_collection,[{min_bin_vheap_size,46368},
                      {min_heap_size,233},
                      {fullsweep_after,65535},
                      {minor_gcs,6}]},
 {suspending,[]}]

The following fields is extraced from the tuple list returned from erlang:process_info/1:
initial_call,heap_size,reductions,messages,registered_name,current_function,stack_size
After that these fields along with the process id are formated into a table and then written to stdout.

Another function ni() does similar work, however, it shows processes running on all nodes (including local node). Processes running on a remote node can be easily distinguished from their pids. Let

  A = pid_to_list(Pid).
  B = lists:sublist(A,2,length(A)-2).
  [C,_,_] = [list_to_integer(S) || S <- string:tokens(B,[$.])].

Then C is always 0 for a local process and greater than 0 for a remote pid.

To show all registered processes, use the function regs(). This function prints registered ports too.

3> regs().

** Registered procs on node nonode@nohost **
Name                  Pid          Initial Call                      Reds Msgs
application_controlle       erlang:apply/2                     444    0
code_server                erlang:apply/2                  102634    0
erl_prim_loader             erlang:apply/2                  174063    0
error_logger                gen_event:init_it/6                226    0
file_server_2              file_server:init/1                  86    0
global_group               global_group:init/1                 60    0
global_name_server         global:init/1                       53    0
inet_db                    inet_db:init/1                     255    0
init                        otp_ring0:start/2                 2419    0
kernel_safe_sup            supervisor:kernel/1                 57    0
kernel_sup                 supervisor:kernel/1               1413    0
rex                        rpc:init/1                          36    0
standard_error             standard_error:server/2              7    0
standard_error_sup         supervisor_bridge:standar           40    0
user                       group:server/3                      38    0
user_drv                   user_drv:server/2                10988    0

** Registered ports on node nonode@nohost **
Name                  Id              Command
ok

The function regs() gets a list of registered process names by calling erlang:registered(). Then it calls erlang:whereis/1 to translate each name into a pid. The remaining job is almost the same as i(), but regs() also collects information of ports by using erlang:port_info/1. There is a distributed version of regs, say nregs(), just as ni() to i().

Refer to OTP source code otp_src_R14B01/lib/stdlib/src/c.erl

Advertisements
This entry was posted in erlang and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s