1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2002-2009. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(etop_tr).
-author('[email protected]').
%%-compile(export_all).
-export([setup_tracer/1,stop_tracer/1,reader/1]).
-import(etop,[getopt/2]).
-include("etop_defs.hrl").
setup_tracer(Config) ->
TraceNode = getopt(node,Config),
RHost = rpc:call(TraceNode, net_adm, localhost, []),
Store = ets:new(?MODULE, [set, public]),
%% We can only trace one process anyway kill the old one.
case erlang:whereis(dbg) of
undefined ->
case rpc:call(TraceNode, erlang, whereis, [dbg]) of
undefined -> fine;
Pid ->
exit(Pid, kill)
end;
Pid ->
exit(Pid,kill)
end,
dbg:tracer(TraceNode,port,dbg:trace_port(ip,{getopt(port,Config),5000})),
dbg:p(all,[running,timestamp]),
T = dbg:get_tracer(TraceNode),
Config#opts{tracer=T,host=RHost,store=Store}.
stop_tracer(_Config) ->
dbg:p(all,clear),
dbg:stop(),
ok.
reader(Config) ->
Host = getopt(host, Config),
Port = getopt(port, Config),
{ok, Sock} = gen_tcp:connect(Host, Port, [{active, false}]),
spawn_link(fun() -> reader_init(Sock,getopt(store,Config),[]) end).
%%%%%%%%%%%%%% Socket reader %%%%%%%%%%%%%%%%%%%%%%%%%%%
reader_init(Sock, Store, Last) ->
process_flag(priority, high),
reader(Sock, Store, Last).
reader(Sock, Store, Last) ->
Data = get_data(Sock),
New = handle_data(Last, Data, Store),
reader(Sock, Store, New).
handle_data(Last, {_, Pid, in, _, Time}, _) ->
[{Pid,Time}|Last];
handle_data([], {_, _, out, _, _}, _Store) ->
%% ignore - there was probably just a 'drop'
[];
handle_data(Last, {_, Pid, out, _, Time2} = G, Store) ->
case lists:keytake(Pid, 1, Last) of
{_, {_, Time1}, New} ->
Elapsed = elapsed(Time1, Time2),
case ets:member(Store,Pid) of
true -> ets:update_counter(Store, Pid, Elapsed);
false -> ets:insert(Store,{Pid,Elapsed})
end,
New;
false ->
io:format("Erlang top got garbage ~p~n", [G]),
Last
end;
handle_data(_W, {drop, D}, _) -> %% Error case we are missing data here!
io:format("Erlang top dropped data ~p~n", [D]),
[];
handle_data(Last, G, _) ->
io:format("Erlang top got garbage ~p~n", [G]),
Last.
elapsed({Me1, S1, Mi1}, {Me2, S2, Mi2}) ->
Me = (Me2 - Me1) * 1000000,
S = (S2 - S1 + Me) * 1000000,
Mi2 - Mi1 + S.
%%%%%% Socket helpers %%%%
get_data(Sock) ->
[Op | BESiz] = my_ip_read(Sock, 5),
Siz = get_be(BESiz),
case Op of
0 ->
B = list_to_binary(my_ip_read(Sock, Siz)),
binary_to_term(B);
1 ->
{drop, Siz};
Else ->
exit({'bad trace tag', Else})
end.
get_be([A,B,C,D]) ->
A * 16777216 + B * 65536 + C * 256 + D.
my_ip_read(Sock,N) ->
case gen_tcp:recv(Sock, N) of
{ok, Data} ->
case length(Data) of
N ->
Data;
X ->
Data ++ my_ip_read(Sock, N - X)
end;
_Else ->
exit(eof)
end.
|