Erlang generic standard behaviours -- gen_server system msg
这是Erlang generic standard behaviors gen_server 分析的系列的最后一篇,主要分析gen_server module 辅助性的功能函数.
在gen_server 的MAIN loop 流程中,除了处理Parent 的'EXIT' 消息, user module 常规消息之外, 还处理了一类 system 消息. 这一类system 消息的来源是一个sys 的module,在Erlang OTP体系中,sys module 主要有两大类的作用,一个是热更,另一个是trace log statistics 相关的辅助功能.
这次主要分析trace log statistics 相关的内容,至于热更会放在之后的热更新系列中进行说明.
decode_msg(Msg, Parent, Name, State, Mod, Time, Debug, Hib) ->
case Msg of
{system, From, Req} ->
sys:handle_system_msg(Req, From, Parent, ?MODULE, Debug,
[Name, State, Mod, Time], Hib);
{'EXIT', Parent, Reason} ->
terminate(Reason, Name, Msg, Mod, State, Debug);
_Msg when Debug =:= [] ->
handle_msg(Msg, Parent, Name, State, Mod);
_Msg ->
Debug1 = sys:handle_debug(Debug, fun print_event/3,
Name, {in, Msg}),
handle_msg(Msg, Parent, Name, State, Mod, Debug1)
end.
由上面的代码片段中,可以看出gen_server 的 MAIN loop 会接收{system, From, Req} 消息(L3), 并且调用sys:handle_system_msg/7 函数(L4)进行处理. 此外还会根据是否有Debug option参数(L8)而调用sys:handle_debug/4 函数进行处理(L11).
system 消息来源
gen_server MAIL loop 中处理的{system, From, Req} 消息是来自sys module, 代码片段见下:
%%-----------------------------------------------------------------
%% All system messages sent are on the form {system, From, Msg}
%% The receiving side should send Msg to handle_system_msg/5.
%%-----------------------------------------------------------------
send_system_msg(Name, Request) ->
case catch gen:call(Name, system, Request) of
{ok,Res} -> Res;
{'EXIT', Reason} -> exit({Reason, mfa(Name, Request)})
end. send_system_msg(Name, Request, Timeout) ->
case catch gen:call(Name, system, Request, Timeout) of
{ok,Res} -> Res;
{'EXIT', Reason} -> exit({Reason, mfa(Name, Request, Timeout)})
end.
这个代码片段的注释中的信息已经说的很明白.
而log trace statistics 相关的辅助功能的实现,都是通过调用send_system_msg 函数,向gen_server 进程发送system 消息, 如:
trace(Name, Flag) ->
send_system_msg(Name, {debug, {trace, Flag}}).
statistics(Name, Flag) ->
send_system_msg(Name, {debug, {statistics, Flag}}).
L1 是trace 函数, L3 是statistics 函数.
system 消息处理
当gen_server MAIN loop 接收到system 消息时, 调用sys:handle_system_msg/7 函数进行处理, sys:handle_system_msg/7 函数的实现:
handle_system_msg(SysState, Msg, From, Parent, Mod, Debug, Misc, Hib) ->
case do_cmd(SysState, Msg, Parent, Mod, Debug, Misc) of
{suspended, Reply, NDebug, NMisc} ->
_ = gen:reply(From, Reply),
suspend_loop(suspended, Parent, Mod, NDebug, NMisc, Hib);
{running, Reply, NDebug, NMisc} ->
_ = gen:reply(From, Reply),
Mod:system_continue(Parent, NDebug, NMisc)
end.
也就是, handle_system_msg 函数调用了(L2)do_cmd 函数, 处理各种类型的system 消息
do_cmd(_, suspend, _Parent, _Mod, Debug, Misc) ->
{suspended, ok, Debug, Misc};
do_cmd(_, resume, _Parent, _Mod, Debug, Misc) ->
{running, ok, Debug, Misc};
do_cmd(SysState, get_state, _Parent, Mod, Debug, Misc) ->
{SysState, do_get_state(Mod, Misc), Debug, Misc};
do_cmd(SysState, {replace_state, StateFun}, _Parent, Mod, Debug, Misc) ->
{Res, NMisc} = do_replace_state(StateFun, Mod, Misc),
{SysState, Res, Debug, NMisc};
do_cmd(SysState, get_status, Parent, Mod, Debug, Misc) ->
Res = get_status(SysState, Parent, Mod, Debug, Misc),
{SysState, Res, Debug, Misc};
do_cmd(SysState, {debug, What}, _Parent, _Mod, Debug, Misc) ->
{Res, NDebug} = debug_cmd(What, Debug),
{SysState, Res, NDebug, Misc};
do_cmd(suspended, {change_code, Module, Vsn, Extra}, _Parent,
Mod, Debug, Misc) ->
{Res, NMisc} = do_change_code(Mod, Module, Vsn, Extra, Misc),
{suspended, Res, Debug, NMisc};
do_cmd(SysState, Other, _Parent, _Mod, Debug, Misc) ->
{SysState, {error, {unknown_system_msg, Other}}, Debug, Misc}.
L1,L3,L16 的system 消息和热更相关.
L5,L7 的system 消息和gen_server 进程的state 相关.
L10 是获取gen_server 进程的所有状态信息, 包括但不仅限trace statistics state 信息.
L13 是trace log statistics 相关辅助函数的开关. 这些辅助函数功能对于gen_server 进程是可拔插的.
debug_cmd({trace, true}, Debug) ->
{ok, install_debug(trace, true, Debug)};
debug_cmd({trace, false}, Debug) ->
{ok, remove_debug(trace, Debug)};
仅以trace 功能举例,true false 表示该函数功能的生效与否.而install_debug 的实现就是lists [|] 的操作, remove_debug 调用的是lists:keydelete/3 函数实现.
触发debug
在gen_server 的MAIN loop 中,等Debug option 参数时, 在decode_msg, handle_msg, handle_common_reply, reply 函数处理时,即会调用sys:handle_debug 函数, 继而触发debug.
%%-----------------------------------------------------------------
%% Func: handle_debug/4
%% Purpose: Called by a process that wishes to debug an event.
%% Func is a formatting function, called as Func(Device, Event).
%% Returns: [debug_opts()]
%%-----------------------------------------------------------------
-spec handle_debug(Debug, FormFunc, Extra, Event) -> [dbg_opt()] when
Debug :: [dbg_opt()],
FormFunc :: format_fun(),
Extra :: term(),
Event :: system_event().
handle_debug([{trace, true} | T], FormFunc, State, Event) ->
print_event({Event, State, FormFunc}),
[{trace, true} | handle_debug(T, FormFunc, State, Event)];
handle_debug([{log, {N, LogData}} | T], FormFunc, State, Event) ->
NLogData = [{Event, State, FormFunc} | trim(N, LogData)],
[{log, {N, NLogData}} | handle_debug(T, FormFunc, State, Event)];
handle_debug([{log_to_file, Fd} | T], FormFunc, State, Event) ->
print_event(Fd, {Event, State, FormFunc}),
[{log_to_file, Fd} | handle_debug(T, FormFunc, State, Event)];
handle_debug([{statistics, StatData} | T], FormFunc, State, Event) ->
NStatData = stat(Event, StatData),
[{statistics, NStatData} | handle_debug(T, FormFunc, State, Event)];
handle_debug([{Func, FuncState} | T], FormFunc, State, Event) ->
case catch Func(FuncState, Event, State) of
done -> handle_debug(T, FormFunc, State, Event);
{'EXIT', _} -> handle_debug(T, FormFunc, State, Event);
NFuncState ->
[{Func, NFuncState} | handle_debug(T, FormFunc, State, Event)]
end;
handle_debug([], _FormFunc, _State, _Event) ->
[].
1, trace 会调用print_event 函数,将trace 信息打印在console
2, log_to_file 同样调用print_event 函数, 将trace 信息写入到指定文件
3, 而statistics 是将统计信息更新至gen_server 进程的status
总结
在Erlang 的设计与源码实现中, 这种功能完备利于debug且可拔插的周边tools, 总能让人兴奋.
Erlang generic standard behaviours -- gen_server system msg的更多相关文章
- Erlang generic standard behaviours -- gen_server module
在分析完gen module (http://www.cnblogs.com/--00/p/4271386.html)之后,就可以开始进入gen_server 的主体module 了.gen_serv ...
- Erlang generic standard behaviours -- gen_server terminate
gen_server 主体 module 已经分析完了(http://www.cnblogs.com/--00/p/4271982.html),接着,分析下gen_server 中的terminate ...
- Erlang generic standard behaviours -- gen_server hibernate
hibernate 主要用于在内存空闲时,通过整理进程的stack,回收进程的heap 来达到回收内存节省资源的效果. hibernate 可用于OTP 进程以及普通进程, hibernate 的官方 ...
- Erlang generic standard behaviours -- gen_server noblock call
在Erlang 系统中,经常需要gen_server 进程来处理共享性的数据,也就是总希望一个gen_server 进程来为多个普通进程提供某种通用性的服务,这也是gen_server 设计的初衷.但 ...
- Erlang generic standard behaviours -- gen
在分析 gen_server (或者是gen_fsm )之前,首先应该弄明白,gen 这个module . -module(gen). -compile({inline,[get_node/1]}). ...
- Erlang generic standard behaviours -- summary
gen_server 相关的片段分析得也差不多了, 这篇作为一个简要的总结.这一系列相关的分析暂且告一段落(之后如有必要,还会回来的 ^^ ),下一个系列主要是以pool 相关, 包括但不仅限于开源项 ...
- erlang四大behaviour之一gen_server
来源:http://www.cnblogs.com/puputu/articles/1701017.html erlang程序设计里面有个设计原则就是把你的进程构造成树,把共用代码提出来,特定功能 ...
- erlang四大behaviour之一gen_server(转载)
erlang程序设计里面有个设计原则就是把你的进程构造成树,把共用代码提出来,特定功能用自己的module实现,这也就是behaviour了,应用behaviour可以减少与本身事务无关的代码量,设计 ...
- System.Collections.Generic.List<T> 与 System.Collections.ArrayList
[推荐] System.Collections.Generic.List<T> [原因] 泛型集合类List<T>在操作值类型的集合时可以不进行 装箱/拆箱 处理. 使得性能较 ...
随机推荐
- mybatis generator的用法
1 自动生成代码 配置数据库 自动生成三个文件: 第一,java bean文件: 第二,java bean对应的dao文件,但是这里的dao只是一个接口: 第三,mybatis需要的Mapper文件: ...
- [Delphi]解决Delphi Distiller运行报错"HKEY_CURRENT_USER\\" is of wrong kind or size
最近终于决心将使用多年的Delphi 7升级到Delphi 2007,虽然目前Delphi最高版本已经是XE8,但对于只做VCL开发的话还是喜欢2007这个经典的版本. 安装Delphi 2007一切 ...
- nginx搭建服务
1.当然首先是安装nginx :http://nginx.org/en/download.html 2.一波安装之后 3.在nginx 文件夹打开命令行:cmd 4.开启nginx 服务命令:star ...
- ASP获取上月本月下月的第一天和最后一天
上月第一天:<%=dateadd("m",-1,year(date)&"-"&month(date)&"-1" ...
- linux c编程:线程互斥二 线程死锁
死锁就是不同的程序在运行时因为某种原因发生了阻塞,进而导致程序不能正常运行.阻塞程序的原因通常都是由于程序没有正确使用临界资源. 我们举个日常生活中的例子来比喻死锁.我们把马路上行驶的汽车比作运行着的 ...
- Visual Studio 2017 扩展推荐
ReSharper : 首先的是Resharper,这个基本是目前是我开发过程中必备的工具集,唯一的缺点就是吃内存,所以你的内存要是低于8G,就不要使用它了.它的特点可以快速重构.高亮显示错误.导航和 ...
- 关于用JAVA开发短信方面的知识
现在流行的网络业务莫过于短信了.网易新浪等都因此而盈利,股价上涨.我凭自己的经验和公司支持,也就乘着东风来研究一下了! 首先,你要选择一台移动或者联通的短信服务器做你们的发送短信接口.这是最关键的 ...
- 每天一个Linux命令(32)date命令
date命令是显示或设置系统时间与日期. (1)用法: 用法: date [选项] [参数] (2)功能: 功能: 根据指定格式显示 ...
- mac manpages 汉化
默认在终端进行man命令,如:man ls,会显示英文的帮助文档.本文教你如何查看中文文档. 资源:1.manpages-zh-1.5.2.tar.bz22.groff-1.21.tar.gz - ...
- 0423 hashlib模块、logging模块、configparse模块、collections模块
一.hashlib模块补充 1,密文验证 import hashlib #引入模块 m =hashlib.md5() # 创建了一个md5算法的对象 m.update(b') print(m.hexd ...