这是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 相关的内容,至于热更会放在之后的热更新系列中进行说明.
1 decode_msg(Msg, Parent, Name, State, Mod, Time, Debug, Hib) -> 2 case Msg of 3 {system, From, Req} -> 4 sys:handle_system_msg(Req, From, Parent, ?MODULE, Debug, 5 [Name, State, Mod, Time], Hib); 6 {'EXIT', Parent, Reason} -> 7 terminate(Reason, Name, Msg, Mod, State, Debug); 8 _Msg when Debug =:= [] -> 9 handle_msg(Msg, Parent, Name, State, Mod); 10 _Msg -> 11 Debug1 = sys:handle_debug(Debug, fun print_event/3, 12 Name, {in, Msg}), 13 handle_msg(Msg, Parent, Name, State, Mod, Debug1) 14 end.
由上面的代码片段中,可以看出gen_server 的 MAIN loop 会接收{system, From, Req} 消息(L3), 并且调用sys:handle_system_msg/7 函数(L4)进行处理. 此外还会根据是否有Debug option参数(L8)而调用sys:handle_debug/4 函数进行处理(L11).
gen_server MAIL loop 中处理的{system, From, Req} 消息是来自sys module, 代码片段见下:
1 %%----------------------------------------------------------------- 2 %% All system messages sent are on the form {system, From, Msg} 3 %% The receiving side should send Msg to handle_system_msg/5. 4 %%----------------------------------------------------------------- 5 send_system_msg(Name, Request) -> 6 case catch gen:call(Name, system, Request) of 7 {ok,Res} -> Res; 8 {'EXIT', Reason} -> exit({Reason, mfa(Name, Request)}) 9 end. 10 11 send_system_msg(Name, Request, Timeout) -> 12 case catch gen:call(Name, system, Request, Timeout) of 13 {ok,Res} -> Res; 14 {'EXIT', Reason} -> exit({Reason, mfa(Name, Request, Timeout)}) 15 end.
这个代码片段的注释中的信息已经说的很明白.
而log trace statistics 相关的辅助功能的实现,都是通过调用send_system_msg 函数,向gen_server 进程发送system 消息, 如:
1 trace(Name, Flag) -> 2 send_system_msg(Name, {debug, {trace, Flag}}). 3 statistics(Name, Flag) -> 4 send_system_msg(Name, {debug, {statistics, Flag}}).
L1 是trace 函数, L3 是statistics 函数.
当gen_server MAIN loop 接收到system 消息时, 调用sys:handle_system_msg/7 函数进行处理, sys:handle_system_msg/7 函数的实现:
1 handle_system_msg(SysState, Msg, From, Parent, Mod, Debug, Misc, Hib) -> 2 case do_cmd(SysState, Msg, Parent, Mod, Debug, Misc) of 3 {suspended, Reply, NDebug, NMisc} -> 4 _ = gen:reply(From, Reply), 5 suspend_loop(suspended, Parent, Mod, NDebug, NMisc, Hib); 6 {running, Reply, NDebug, NMisc} -> 7 _ = gen:reply(From, Reply), 8 Mod:system_continue(Parent, NDebug, NMisc) 9 end.
也就是, handle_system_msg 函数调用了(L2)do_cmd 函数, 处理各种类型的system 消息
1 do_cmd(_, suspend, _Parent, _Mod, Debug, Misc) -> 2 {suspended, ok, Debug, Misc}; 3 do_cmd(_, resume, _Parent, _Mod, Debug, Misc) -> 4 {running, ok, Debug, Misc}; 5 do_cmd(SysState, get_state, _Parent, Mod, Debug, Misc) -> 6 {SysState, do_get_state(Mod, Misc), Debug, Misc}; 7 do_cmd(SysState, {replace_state, StateFun}, _Parent, Mod, Debug, Misc) -> 8 {Res, NMisc} = do_replace_state(StateFun, Mod, Misc), 9 {SysState, Res, Debug, NMisc}; 10 do_cmd(SysState, get_status, Parent, Mod, Debug, Misc) -> 11 Res = get_status(SysState, Parent, Mod, Debug, Misc), 12 {SysState, Res, Debug, Misc}; 13 do_cmd(SysState, {debug, What}, _Parent, _Mod, Debug, Misc) -> 14 {Res, NDebug} = debug_cmd(What, Debug), 15 {SysState, Res, NDebug, Misc}; 16 do_cmd(suspended, {change_code, Module, Vsn, Extra}, _Parent, 17 Mod, Debug, Misc) -> 18 {Res, NMisc} = do_change_code(Mod, Module, Vsn, Extra, Misc), 19 {suspended, Res, Debug, NMisc}; 20 do_cmd(SysState, Other, _Parent, _Mod, Debug, Misc) -> 21 {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 进程是可拔插的.
1 debug_cmd({trace, true}, Debug) -> 2 {ok, install_debug(trace, true, Debug)}; 3 debug_cmd({trace, false}, Debug) -> 4 {ok, remove_debug(trace, Debug)};
仅以trace 功能举例,true false 表示该函数功能的生效与否.而install_debug 的实现就是lists [|] 的操作, remove_debug 调用的是lists:keydelete/3 函数实现.
在gen_server 的MAIN loop 中,等Debug option 参数时, 在decode_msg, handle_msg, handle_common_reply, reply 函数处理时,即会调用sys:handle_debug 函数, 继而触发debug.
1 %%----------------------------------------------------------------- 2 %% Func: handle_debug/4 3 %% Purpose: Called by a process that wishes to debug an event. 4 %% Func is a formatting function, called as Func(Device, Event). 5 %% Returns: [debug_opts()] 6 %%----------------------------------------------------------------- 7 -spec handle_debug(Debug, FormFunc, Extra, Event) -> [dbg_opt()] when 8 Debug :: [dbg_opt()], 9 FormFunc :: format_fun(), 10 Extra :: term(), 11 Event :: system_event(). 12 handle_debug([{trace, true} | T], FormFunc, State, Event) -> 13 print_event({Event, State, FormFunc}), 14 [{trace, true} | handle_debug(T, FormFunc, State, Event)]; 15 handle_debug([{log, {N, LogData}} | T], FormFunc, State, Event) -> 16 NLogData = [{Event, State, FormFunc} | trim(N, LogData)], 17 [{log, {N, NLogData}} | handle_debug(T, FormFunc, State, Event)]; 18 handle_debug([{log_to_file, Fd} | T], FormFunc, State, Event) -> 19 print_event(Fd, {Event, State, FormFunc}), 20 [{log_to_file, Fd} | handle_debug(T, FormFunc, State, Event)]; 21 handle_debug([{statistics, StatData} | T], FormFunc, State, Event) -> 22 NStatData = stat(Event, StatData), 23 [{statistics, NStatData} | handle_debug(T, FormFunc, State, Event)]; 24 handle_debug([{Func, FuncState} | T], FormFunc, State, Event) -> 25 case catch Func(FuncState, Event, State) of 26 done -> handle_debug(T, FormFunc, State, Event); 27 {'EXIT', _} -> handle_debug(T, FormFunc, State, Event); 28 NFuncState -> 29 [{Func, NFuncState} | handle_debug(T, FormFunc, State, Event)] 30 end; 31 handle_debug([], _FormFunc, _State, _Event) -> 32 [].
1, trace 会调用print_event 函数,将trace 信息打印在console
2, log_to_file 同样调用print_event 函数, 将trace 信息写入到指定文件
3, 而statistics 是将统计信息更新至gen_server 进程的status
在Erlang 的设计与源码实现中, 这种功能完备利于debug且可拔插的周边tools, 总能让人兴奋.