-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocs_csv.erl
170 lines (151 loc) · 4.65 KB
/
procs_csv.erl
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
-module(procs_csv).
-export([do/2, links/2]).
-define(SEP, "§").
%% ---------
links(N, Cookie) ->
trycreate([procs]),
true = erlang:set_cookie(N, Cookie),
true = net_kernel:connect(N),
Procs = rpc:call(N, erlang, processes, []),
Infos=[links, registered_name],
{ok, FPID} = file:open("process_links.csv", [write]),
% fetch the remote data
lists:foreach(fun(Pid) ->
case rpc:call(N, erlang, process_info, [Pid, Infos]) of
undefined ->
[];
[{links,Links},{registered_name,RegName}] ->
Regname2 =
case RegName of
[] -> Pid;
R -> R
end,
true = ets:insert(procs, {Pid, Regname2, Links})
end
end, Procs),
% create heading
ok = file:write(FPID,
create_line([{source, "source"},
{target, "target"},
{value, "value"}])),
% create the data lines
ok =
lists:foreach(fun
([]) ->
ok;
({_Pid, RegName, Links}) ->
lists:foreach(fun(Link) when is_port(Link) ->
ok;
(Link) ->
case ets:lookup(procs, Link) of
[{Link, TargetRegName, _ }] ->
%% Links are bi-directional, so you could potentially
%% delete a entry once' you've looked it up.
L = create_line([{source,ensure_list(RegName)},
{target,ensure_list(TargetRegName)},
{value,"1"}]),
ok = file:write(FPID, L);
[] ->
% io:format("Cannot find :~p\n", [Link])
end
end, Links)
end, ets:tab2list(procs)),
file:close(FPID).
%% ---------
do(N, Cookie) ->
true = erlang:set_cookie(N, Cookie),
true = net_kernel:connect(N),
Procs = rpc:call(N, erlang, processes, []),
Infos=[
%%Bbacktrace,
% binary,
% catchlevel,
% current_function,
% current_location,
% current_stacktrace,
% dictionary,
% error_handler,
% garbage_collection,
% group_leader,
% heap_size,
% initial_call,
% links,
% last_calls,
memory
%%,
% message_queue_len,
% messages,
% min_heap_size,
% min_bin_vheap_size,
% monitored_by,
% monitors,
% priority,
% reductions,
% registered_name,
% sequential_trace_token,
% stack_size,
% status,
% suspending,
% total_heap_size,
% trace,
% trap_exit
],
{ok, FPID} = file:open("process_info_results.csv", [write]),
% fetch the remote data
ProcsAndInfo =
lists:map(fun(Pid) ->
case rpc:call(N, erlang, process_info, [Pid, Infos]) of
undefined ->
[];
ProcInfos ->
{Pid,ProcInfos}
end
end, Procs),
% create heading
file:write(FPID,
create_line( [{pid,pid}] ++ lists:zip(Infos, Infos) )),
% create the data lines
ok =
lists:foreach(fun
([]) ->
ok;
({Pid,ProcInfos}) ->
ok = file:write(FPID,
create_line([{pid,Pid}] ++ ProcInfos))
end, ProcsAndInfo),
file:close(FPID).
create_line(Is) ->
create_line(Is,[]).
create_line([], R) ->
lists:reverse(R) ++ "\n";
create_line([{_Info, InfoVal}|T=[]], R) ->
create_line(T, [ensure_list(InfoVal)|R]);
% create_line([[]|T], R) ->
% create_line(T,R);
create_line([{_Info, InfoVal}|T], R) ->
create_line(T, [ensure_list(InfoVal) ++ ?SEP|R]).
ensure_list(A) when is_atom(A) ->
atom_to_list(A);
ensure_list(A) when is_integer(A) ->
integer_to_list(A);
ensure_list(A) when is_tuple(A) ->
tuple_to_list(A);
ensure_list(A) when is_pid(A) ->
pid_to_list(A);
ensure_list(A) when is_binary(A) ->
binary_to_list(A);
ensure_list(A) when is_port(A) ->
erlang:port_to_list(A);
ensure_list(A) when is_reference(A) ->
erlang:ref_to_list(A);
ensure_list(A) when is_map(A) ->
maps:to_list(A);
ensure_list(A) when is_list(A) ->
A.
trycreate(Tbls) ->
lists:foreach(fun(TblName) ->
case ets:info(TblName) of
undefined -> ets:new(TblName, [named_table, ordered_set]);
_Props -> ok
end
end, Tbls).