dbg:p/2方法分析

2016-05-17  本文已影响127人  randyjia

承接上文erlang火焰图

dbg:p()方法分析

dbg模块是什么,有什么用

模块dbg是基于文本跟踪的一个工具,用它可以基于文本的方式跟踪函数调用,进程,消息收发,从而指导系统运行时的状态。

dbg:p()方法是什么

dbg:p/1和dbg:p/2方法是用来开始跟踪进程的。p/2有两个入参,Item和Flags,第一个参数决定跟踪谁,第二个参数决定如何跟踪(收集哪些参数)

dbg:p()方法如何实现

代码不多,直接上源码分析。

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>

<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()方法

其他:
凡事涉及到系统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.

上一篇下一篇

猜你喜欢

热点阅读