dbg:p/2方法分析
承接上文erlang火焰图
dbg:p()方法分析
- dbg模块是什么,有什么用?
- dbg:p()方法是什么?
- dbg:p()是怎么实现的?
dbg模块是什么,有什么用
模块dbg是基于文本跟踪的一个工具,用它可以基于文本的方式跟踪函数调用,进程,消息收发,从而指导系统运行时的状态。
dbg:p()方法是什么
dbg:p/1和dbg:p/2方法是用来开始跟踪进程的。p/2有两个入参,Item和Flags,第一个参数决定跟踪谁,第二个参数决定如何跟踪(收集哪些参数)
-
Item含义如下:
- 如果Item是pid(),就指定跟踪这个进程;
- 如果Item是all,追踪所有的进程包括以后新建立的进程;
- 如果Item是new,追踪所有新建立的进程;
- 其它更多请参考文档
-
Flags
- s(send),跟踪进程发送的消息
- r(receive),跟踪进程接受的消息
- 其它更多参考文档
-
调用dbg:p方法之前,需要首先dbg:tracer/0或者dbg:tracer/2启动一个收集消息的进程,然后跟踪的消息发送到这个进程即可。
dbg:p()方法如何实现
代码不多,直接上源码分析。
- 首先封装
<pre>
p(Pid) ->
p(Pid, [m]).
p(Pid, Flags) when is_atom(Flags) ->
p(Pid, [Flags]);
p(Pid, Flags) ->
req({p,Pid,Flags}).
</pre>
可以看出,最后调用到req()方法。 - req方法
<pre>
req(R) ->
P = ensure(), % The pid or perhaps the name of the server
Mref = erlang:monitor(process, P),
catch P ! {self(), R}, % May crash if P = atom() and server died
receive
{'DOWN', Mref, _, _, _} -> % If server died
exit(dbg_server_crash);
{dbg, Reply} ->
erlang:demonitor(Mref, [flush]),
Reply
end.
</pre>
P ! {self(), R}这里就是向dbg收集消息的server发送消息,R开头的tag就是p。
ensure()方法保证又一个dbg收集消息的server,没有就启动一个。ensure()方法相关如下
<pre>
%%% Server implementation.
start() ->
start(no_tracer).
start(TracerFun) ->
S = self(),
case whereis(dbg) of
undefined ->
Dbg = spawn(fun() -> init(S) end),
receive {Dbg,started} -> ok end,
case TracerFun of
no_tracer ->
{ok, Dbg};
Fun when is_function(Fun) ->
req({tracer,TracerFun})
end;
Pid when is_pid(Pid), is_function(TracerFun) ->
req({tracer,TracerFun})
end.
init(Parent) ->
process_flag(trap_exit, true),
register(dbg, self()),
Parent ! {self(),started},
loop({[],[]},[]).
</pre>
- 看看dbg server是如何处理p消息的
<pre>
loop({C,T}=SurviveLinks, Table) ->
receive
......
{From,{p,Pid,Flags}} ->
reply(From, trace_process(Pid, Flags)),
loop(SurviveLinks, Table);
......
</pre>
看看trace_process(Pid,Flags),实际上最后调用的如下
<pre>
trac(Node, {_Relay, Tracer}, AtomPid, How, Flags) ->
case rpc:call(Node, ?MODULE, erlang_trace,
[AtomPid, How, [{tracer, Tracer} | Flags]]) of
N when is_integer(N) ->
{matched, Node, N};
{badrpc,Reason} ->
{matched, Node, 0, Reason};
Else ->
{matched, Node, 0, Else}
end.
</pre>
其实调用的是erlang_trace()方法
- erlang:trace(PidSpec, How, FlagList) -> integer()
第一个参数决定跟踪对象,How就是true和false,表示是否进行跟踪,FlagList就是跟踪的信息。FlagList加上{tracer, Tracer},表示把跟踪信息发送给Tracer。消息类似
<pre>
{trace, Pid, 'receive', Msg}
</pre>
更多参考文档。
其他:
凡事涉及到系统trace,有两个概念:cpu time和wall_clock_time, 它们的概念和区别在这里
Wall-clock_time,请自行科学上网。具体说来,就是这段话
In computing, wall-clock time can also mean the actual time by a computer to complete a task.
It is the sum of three terms: CPU time, I/O time, and the communication channel delay (e.g. if data are scattered on multiple machines). In contrast to CPU time, which measures only the time during which the processor is actively working on a certain task, wall time measures the total time for the process to complete. The difference between the two consists of time that passes due to programmed delays or waiting for resources to become available.