Home > English, Erlang > Erlang hints

Erlang hints


Erlang is a wonderful tool…but sometimes it is awkward to manage your stuff (processes or ets/mnesia tables) just by using the shell. Let’s see how to exploit Erlang modules to make our lifes easier!

Many times I was looking for a specific Erlang process using one of the bifs you can see below:

1> registered().
[init,global_group,erl_prim_loader,user,error_logger,rex,
 standard_error_sup,kernel_sup,global_name_server,inet_db,
 file_server_2,code_server,user_drv,application_controller,
 standard_error,kernel_safe_sup]

Or

2> processes().
[<0.0.0>,<0.3.0>,<0.5.0>,<0.6.0>,<0.8.0>,<0.9.0>,<0.10.0>,
 <0.11.0>,<0.12.0>,<0.13.0>,<0.14.0>,<0.15.0>,<0.16.0>,
 <0.17.0>,<0.18.0>,<0.19.0>,<0.20.0>,<0.21.0>,<0.22.0>,
 <0.23.0>,<0.24.0>,<0.25.0>,<0.26.0>,<0.27.0>,<0.28.0>,
 <0.34.0>]

For me it can be painful to use this kind of approach, so I geeked around to search some new ways to follow processes; here are two useful modules I found.

The first one is etop, a module which works pretty much as the unix application etop.

4> etop:start([{output, text}]).  

========================================================================================
 nonode@nohost                                                             08:26:31
 Load:  cpu         0               Memory:  total        3961    binary         13
        procs      27                        processes     582    code         1888
        runq        0                        atom          294    ets           132

Pid            Name or Initial Func    Time    Reds  Memory    MsgQ Current Function
----------------------------------------------------------------------------------------
<0.3.0>        erl_prim_loader          '-'  265995   23512       0 erl_prim_loader:loop
<0.19.0>       code_server              '-'  109125   71224       0 code_server:loop/1
<0.34.0>       etop_server              '-'    7164   21072       0 etop:data_handler/2
<0.6.0>        application_controll     '-'    6931  212992       0 gen_server:loop/6
<0.26.0>       erlang:apply/2           '-'    5151   17184       0 shell:shell_rep/4
<0.0.0>        init                     '-'    2501    8316       0 init:loop/1
<0.25.0>       group:server/3           '-'    1820   12304       0 group:server_loop/3
<0.10.0>       kernel_sup               '-'    1289    6096       0 gen_server:loop/6
<0.23.0>       user_drv                 '-'    1278    9308       0 user_drv:server_loop
<0.27.0>       proc_lib:init_p/5        '-'     268    1372       0 gen_server:loop/6
========================================================================================

The second is the bif regs():

2> regs().

** Registered procs on node nonode@nohost **
Name                  Pid          Initial Call                      Reds Msgs
application_controlle <0.6.0>      erlang:apply/2                     444    0
code_server           <0.19.0>     erlang:apply/2                  104722    0
erl_prim_loader       <0.3.0>      erlang:apply/2                  167826    0
error_logger          <0.5.0>      gen_event:init_it/6                226    0
file_server_2         <0.18.0>     file_server:init/1                  86    0
global_group          <0.17.0>     global_group:init/1                 60    0
global_name_server    <0.12.0>     global:init/1                       53    0
inet_db               <0.16.0>     inet_db:init/1                     130    0
init                  <0.0.0>      otp_ring0:start/2                 2450    0
kernel_safe_sup       <0.28.0>     supervisor:kernel/1                 57    0
kernel_sup            <0.10.0>     supervisor:kernel/1               1289    0
rex                   <0.11.0>     rpc:init/1                          36    0
standard_error        <0.21.0>     standard_error:server/2              7    0
standard_error_sup    <0.20.0>     supervisor_bridge:standar           40    0
user                  <0.24.0>     group:server/3                      38    0
user_drv              <0.23.0>     user_drv:server/2                  625    0

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

The third is pman, a graphical process manager very useful to inspect the Erlang processes executing either locally or on remote nodes. It is also possible to trace events in the individual processes.

3> pman:start().

pman on windows7

And that was for processes! But, since many times you have also to work with Mnesia/ets tables, I think that this can be also useful to you. Let’s say you want to see what tables you have in your system, in this case you will probably use:

1> ets:all().
[8207,4110,13,file_io_servers,inet_hosts_file_byaddr,
 inet_hosts_file_byname,inet_hosts_byaddr,inet_hosts_byname,
 inet_cache,inet_db,global_pid_ids,global_pid_names,
 global_names_ext,global_names,global_locks,ac_tab]

Well. I suggest you to use tv, that graphically examines ETS and Mnesia tables

tv on windows7

That was all! Stay tuned!

Categories: English, Erlang Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: