这是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的更多相关文章

  1. Erlang generic standard behaviours -- gen_server module

    在分析完gen module (http://www.cnblogs.com/--00/p/4271386.html)之后,就可以开始进入gen_server 的主体module 了.gen_serv ...

  2. Erlang generic standard behaviours -- gen_server terminate

    gen_server 主体 module 已经分析完了(http://www.cnblogs.com/--00/p/4271982.html),接着,分析下gen_server 中的terminate ...

  3. Erlang generic standard behaviours -- gen_server hibernate

    hibernate 主要用于在内存空闲时,通过整理进程的stack,回收进程的heap 来达到回收内存节省资源的效果. hibernate 可用于OTP 进程以及普通进程, hibernate 的官方 ...

  4. Erlang generic standard behaviours -- gen_server noblock call

    在Erlang 系统中,经常需要gen_server 进程来处理共享性的数据,也就是总希望一个gen_server 进程来为多个普通进程提供某种通用性的服务,这也是gen_server 设计的初衷.但 ...

  5. Erlang generic standard behaviours -- gen

    在分析 gen_server (或者是gen_fsm )之前,首先应该弄明白,gen 这个module . -module(gen). -compile({inline,[get_node/1]}). ...

  6. Erlang generic standard behaviours -- summary

    gen_server 相关的片段分析得也差不多了, 这篇作为一个简要的总结.这一系列相关的分析暂且告一段落(之后如有必要,还会回来的 ^^ ),下一个系列主要是以pool 相关, 包括但不仅限于开源项 ...

  7. erlang四大behaviour之一gen_server

      来源:http://www.cnblogs.com/puputu/articles/1701017.html erlang程序设计里面有个设计原则就是把你的进程构造成树,把共用代码提出来,特定功能 ...

  8. erlang四大behaviour之一gen_server(转载)

    erlang程序设计里面有个设计原则就是把你的进程构造成树,把共用代码提出来,特定功能用自己的module实现,这也就是behaviour了,应用behaviour可以减少与本身事务无关的代码量,设计 ...

  9. System.Collections.Generic.List<T> 与 System.Collections.ArrayList

    [推荐] System.Collections.Generic.List<T> [原因] 泛型集合类List<T>在操作值类型的集合时可以不进行 装箱/拆箱 处理. 使得性能较 ...

随机推荐

  1. Future Promise 模式(netty源码9)

    netty源码死磕9  Future Promise 模式详解 1. Future/Promise 模式 1.1. ChannelFuture的由来 由于Netty中的Handler 处理都是异步IO ...

  2. Moore-Penrose Matrix Inverse 摩尔-彭若斯广义逆 埃尔米特矩阵 Hermitian matrix

    http://mathworld.wolfram.com/Moore-PenroseMatrixInverse.html 显然,埃尔米特矩阵主对角线上的元素都是实数的,其特征值也是实数.对于只包含实数 ...

  3. Webpack探索【4】--- entry和output详解

    本文主要讲entry和output相关内容.

  4. 我的Android进阶之旅------>Android疯狂连连看游戏的实现之实现游戏逻辑(五)

    在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的 ...

  5. vmware 下的三种网络模式

    VMWare提供三种工作模式桥接(bridge).NAT(网络地址转换)和host-only(主机模式). 桥接模式 在桥接模式下,VMWare虚拟出来的操作系统就像是局域网中的一台独立的主机(主机和 ...

  6. LeetCode:救生艇【881】

    LeetCode:救生艇[881] 题目描述 第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit. 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit. ...

  7. SQL语法结构

    目录 一.增 1.增加字段 2.新建约束 二.删 1.删除字段 2.删除约束 三.改 1.修改字段 一.增 1.增加字段: ALTER TABLE [表名] ADD [字段名] NVARCHAR () ...

  8. 【leetcode刷题笔记】Convert Sorted List to Binary Search Tree

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  9. BootStrap实现左侧或右侧竖式tab选项卡

    BootStrap实现左侧或右侧竖式tab选项卡 代码如下: <div style="height: 100px;"> <div class="col- ...

  10. 什么是shell【TLCL】

    常用命令 date cal df——report file system disk space usage free——display amount of free and used memory i ...