[Erlang 0101] Gproc:扩展进程注册机制

简介:
Erlang 进程注册机制目前的限制是:
  • names只能是atom
  • 一个进程只能注册一个name
  • 不能进行高效的搜索和遍历,进程信息的检索是通过遍历检查进程的元数据完成的.
 Ulf T. Wiger的开源项目  Gproc 就是解决上面问题的,难得的是这个项目的文档,范例,测试代码相当完整,还有 专门的论文讲述整个项目的来龙去脉,设计取舍.
 
Gproc是Erlang进程注册机制的加强版,提供了如下原生进程注册没有的功能:
  • 使用任意Erlang Term作为进程的别名
  • 一个进程注册多个别名
  • 支持QLC和match specification高效查询
  • 自动移交已经注册的names和属性到另外的进程
  • .....
那它是怎么做的呢?这些额外的信息是维护在哪里,是ETS吗?动手操练一下,验证想法:
 
复制代码
Eshell V5.9  (abort with ^G)
1> application:start(gproc).
ok
2> ets:i().
id              name              type  size   mem      owner
----------------------------------------------------------------------------
13              code              set   265    10683    code_server
4110            code_names        set   53     7018     code_server
8207            shell_records     ordered_set 0      73       <0.26.0>
ac_tab          ac_tab            set   9      929      application_controller
file_io_servers file_io_servers   set   0      283      file_server_2
global_locks    global_locks      set   0      283      global_name_server
global_names    global_names      set   0      283      global_name_server
global_names_ext global_names_ext  set   0      283      global_name_server
global_pid_ids  global_pid_ids    bag   0      283      global_name_server
global_pid_names global_pid_names  bag   0      283      global_name_server
gproc           gproc             ordered_set 0      73       gproc_sup
gproc_monitor   gproc_monitor     ordered_set 0      73       gproc_monitor
inet_cache      inet_cache        bag   0      283      inet_db
inet_db         inet_db           set   29     554      inet_db
inet_hosts_byaddr inet_hosts_byaddr bag   0      283      inet_db
inet_hosts_byname inet_hosts_byname bag   0      283      inet_db
inet_hosts_file_byaddr inet_hosts_file_byaddr bag   0      283      inet_db
inet_hosts_file_byname inet_hosts_file_byname bag   0      283      inet_db
ok
复制代码

 

 
   应用程序启动之后我们查看ETS表的信息,发现多出来两个表:gproc和gproc_monitor;下面我们就使用Shell进程完成测试,为当前的Shell进程创建别名"Shell",并向它发送消息dvd,之后再shell中flush()查看已经接收到的消息.

复制代码
5> gproc:reg({p,l,"Shell"}).
true
6> gproc:send({p,l,"Shell"},dvd).
dvd
7> flush().
Shell got dvd
ok
复制代码

 

 现在查看一下ETS表的内容,发现里面已经记录了当前Shell进程的注册信息;
 
9> ets:i(gproc).
<1   > {{<0.43.0>,l}}
<2   > {{<0.43.0>,{p,l,"Shell"}},[]}
<3   > {{{p,l,"Shell"},<0.43.0>},<0.43.0>,undefined}
EOT  (q)uit (p)Digits (k)ill /Regexp -->q
ok

 

紧接上面我们为Shell再创建一个别名,然后查看ETS
 
复制代码
10> gproc:reg({p,l,"Shell_alia"}).
true
11> ets:i(gproc).
<1   > {{<0.43.0>,l}}
<2   > {{<0.43.0>,{p,l,"Shell"}},[]}
<3   > {{<0.43.0>,{p,l,"Shell_alia"}},[]}
<4   > {{{p,l,"Shell"},<0.43.0>},<0.43.0>,undefined}
<5   > {{{p,l,"Shell_alia"},<0.43.0>},<0.43.0>,undefined}
EOT  (q)uit (p)Digits (k)ill /Regexp -->q
ok
 
复制代码

 

下面简单演示一下QLC:
 
复制代码
40>  Q5 = qlc:q([P || {{p,l,'_'},P,C} <- gproc:table( )]).
{qlc_handle,{qlc_lc,#Fun<erl_eval.20.111823515>,
                    {qlc_opt,false,false,-1,any,[],any,524288,allowed}}}
41>
41> qlc:eval(Q5).
[<0.65.0>,<0.61.0>]
42> qlc:e(qlc:q([N || N <- gproc:table()])).
[{{p,l,"Hello"},<0.65.0>,undefined},
{{p,l,"NEW_Process"},<0.61.0>,undefined}]
复制代码

 

上面的几段代码基本上包含了它最重要的feature,我们看下实现,注册name的过程实际上是把注册信息写到了ETS;而发送消息的第一步就是从ETS表中查询name对应的Pid,然后进行发送.以发送为例看一下代码实现:
 
复制代码
https://github.com/esl/gproc/blob/master/src/gproc.erl
 
%% If Key belongs to a unique object (name or aggregated counter), this
%% function will send a message to the corresponding process, or fail if there
%% is no such process. If Key is for a non-unique object type (counter or
%% property), Msg will be send to all processes that have such an object.
%% @end
%%
send(Key, Msg) ->
    ?CATCH_GPROC_ERROR(send1(Key, Msg), [Key, Msg]).

send1({T,C,_} = Key, Msg) when C==l; C==g ->
    if T == n orelse T == a ->
            case ets:lookup(?TAB, {Key, T}) of
                [{_, Pid, _}] ->
                    Pid ! Msg;
                _ ->
                    ?THROW_GPROC_ERROR(badarg)
            end;
       T==p orelse T==c ->
            %% BUG - if the key part contains select wildcards, we may end up
            %% sending multiple messages to the same pid
            lists:foreach(fun(Pid) ->
                                  Pid ! Msg
                          end, lookup_pids(Key)),
            Msg;
       true ->
            erlang:error(badarg)
    end;
send1(_, _) ->
    ?THROW_GPROC_ERROR(badarg).
 
复制代码

 

 
细致的实现
 
 下面的测试我们通过gproc:info查看进程信息,下面的结果注意一下current function属性值,是不是有疑问?
 
 
复制代码
17> P2=spawn(fun() -> gproc:reg({p,l,"NEW_Process"}),receive a -> a end end ).
<0.61.0>
18> ets:i(gproc).
<1   > {{<0.61.0>,l}}
<2   > {{<0.61.0>,{p,l,"NEW_Process"}},[]}
<3   > {{{p,l,"NEW_Process"},<0.61.0>},<0.61.0>,undefined}
EOT  (q)uit (p)Digits (k)ill /Regexp -->q
ok
19> gproc:info(P2).
[{gproc,[{{p,l,"NEW_Process"},undefined}]},
{current_function,{erl_eval,receive_clauses,6}},
{initial_call,{erlang,apply,2}},
{status,waiting},
{message_queue_len,0},
{messages,[]},
{links,[]},
{dictionary,[]},
{trap_exit,false},
{error_handler,error_handler},
{priority,normal},
{group_leader,<0.25.0>},
{total_heap_size,610},
{heap_size,233},
{stack_size,8},
{reductions,100},
{garbage_collection,[{min_bin_vheap_size,46368},
                      {min_heap_size,233},
                      {fullsweep_after,65535},
                      {minor_gcs,1}]},
{suspending,[]}]
复制代码

 

 
这里内部实现还是做得非常细致的,不是简单的把信息附加在Process_info,比如对current function信息做了修正.
 
复制代码
https://github.com/esl/gproc/blob/master/src/gproc.erl
 
%% We don't want to return the internal gproc:info() function as current
%% function, so we grab the 'backtrace' and extract the call stack from it,
%% filtering out the functions gproc:info/_ and gproc:'-info/1-lc...' entries.
%%
%% This is really an indication that wrapping the process_info() BIF was a
%% bad idea to begin with... :P
%%
info_cur_f(T, Default) ->
    {match, Matches} = re:run(T,<<"\\(([^\\)]+):(.+)/([0-9]+)">>,
                     [global,{capture,[1,2,3],list}]),
    case lists:dropwhile(fun(["gproc","info",_]) -> true;
                   (["gproc","'-info/1-lc" ++ _, _]) -> true;
                   (_) -> false
               end, Matches) of
     [] ->
         Default;
     [[M,F,A]|_] ->
         {current_function,
          {to_atom(M), to_atom(F), list_to_integer(A)}}
    end.
复制代码

 

 
好了今天就到这里.
 
 
最后小图一张:
 
目录
相关文章
|
1月前
|
消息中间件 Unix Linux
Linux进程间通信(IPC)介绍:详细解析IPC的执行流程、状态和通信机制
Linux进程间通信(IPC)介绍:详细解析IPC的执行流程、状态和通信机制
51 1
|
3月前
|
缓存 负载均衡 Linux
内核:进程与调度机制(笔记)
内核:进程与调度机制(笔记)
59 0
|
5月前
|
消息中间件 弹性计算 网络协议
120 Storm进程间通信机制
120 Storm进程间通信机制
22 0
|
1月前
|
资源调度 算法 Linux
Linux进程/线程的调度机制介绍:详细解析Linux系统中进程/线程的调度优先级规则
Linux进程/线程的调度机制介绍:详细解析Linux系统中进程/线程的调度优先级规则
70 0
|
7天前
|
算法 Linux 调度
深入理解Linux内核的进程调度机制
【4月更文挑战第17天】在多任务操作系统中,进程调度是核心功能之一,它决定了处理机资源的分配。本文旨在剖析Linux操作系统内核的进程调度机制,详细讨论其调度策略、调度算法及实现原理,并探讨了其对系统性能的影响。通过分析CFS(完全公平调度器)和实时调度策略,揭示了Linux如何在保证响应速度与公平性之间取得平衡。文章还将评估最新的调度技术趋势,如容器化和云计算环境下的调度优化。
|
12天前
|
算法 Linux 调度
深度解析:Linux内核的进程调度机制
【4月更文挑战第12天】 在多任务操作系统如Linux中,进程调度机制是系统的核心组成部分之一,它决定了处理器资源如何分配给多个竞争的进程。本文深入探讨了Linux内核中的进程调度策略和相关算法,包括其设计哲学、实现原理及对系统性能的影响。通过分析进程调度器的工作原理,我们能够理解操作系统如何平衡效率、公平性和响应性,进而优化系统表现和用户体验。
20 3
|
存储 监控 安全
深度剖析Linux进程的内部机制:一探/proc/pid的奥秘
深度剖析Linux进程的内部机制:一探/proc/pid的奥秘
82 0
|
5月前
|
负载均衡 算法 Linux
深入理解Linux内核进程CPU负载均衡机制(上)
深入理解Linux内核进程CPU负载均衡机制
|
4月前
|
算法 Linux 调度
Linux进程调度机制
Linux进程调度机制
62 0
|
5月前
|
消息中间件 算法 网络协议
深入探讨进程间通信的重要性:理解不同的通信机制(下)
本文旨在探讨进程间通信的重要性,并介绍了不同的通信机制,如管道、消息队列、共享内存、信号量、信号和套接字。通过理解这些通信机制的特点和应用场景,可以更好地实现进程间的高效数据共享。同时,本文还强调了同步和互斥机制的重要性,以确保数据的一致性和正确性。最后,还介绍了套接字作为一种跨网络和同一主机上进程间通信的通信机制,为读者提供了更全面的了解。通过阅读本文,读者将能够深入理解进程间通信的概念和不同机制,为实现有效的数据共享提供指导。
深入探讨进程间通信的重要性:理解不同的通信机制(下)