forked from zhongwencool/observer_cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver_cli_lib.erl
More file actions
390 lines (351 loc) · 14.5 KB
/
observer_cli_lib.erl
File metadata and controls
390 lines (351 loc) · 14.5 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
%%% @author zhongwen <zhongwencool@gmail.com>
-module(observer_cli_lib).
-include("observer_cli.hrl").
-compile(inline).
%% API
-export([parse_cmd/2, print/3]).
-export([uptime/0]).
-export([to_percent/1]).
-export([to_list/1]).
-export([green/1]).
-export([to_byte/1]).
-export([mfa_to_list/1]).
-export([render/1]).
-export([next_redraw/2]).
-export([render_menu/2]).
-export([get_terminal_rows/1]).
-export([select/1]).
-export([unselect/1]).
-export([parse_integer/1]).
-export([render_last_line/1]).
-export([exit_processes/1]).
-export([update_page_pos/3]).
-export([get_pos/4]).
-export([sublist/3]).
-export([sbcs_to_mbcs/2]).
%% the number 35 from 13' mbp
-define(DEFAULT_ROW_SIZE, application:get_env(observer_cli, default_row_size, 35)).
-define(select(Title), ?RED_BG, Title, ?RESET_BG).
-define(unselect(Title), ?L_GRAY_BG, Title, ?RESET_BG).
-spec uptime() -> list().
uptime() ->
{UpTime, _} = erlang:statistics(wall_clock),
{D, {H, M, S}} = calendar:seconds_to_daystime(UpTime div 1000),
Time = [
integer_to_list(D),
"Days ",
integer_to_list(H),
":",
integer_to_list(M),
":",
integer_to_list(S)
],
[?W(?GREEN, Time, 16)].
%% @doc 0.982342 -> 98.23%, 1 -> 100.0%
-spec to_percent(float()) -> string().
to_percent(Float) when Float < 0.1 -> [$0, erlang:float_to_list(Float * 100, [{decimals, 2}]), $%];
to_percent(Float) when Float < 1 -> [erlang:float_to_list(Float * 100, [{decimals, 2}]), $%];
to_percent(undefined) -> "******";
to_percent(_) -> "100.0%".
-spec to_list(term()) -> list().
to_list(Atom) when is_atom(Atom) -> atom_to_list(Atom);
to_list(Integer) when is_integer(Integer) -> integer_to_list(Integer);
to_list(Pid) when is_pid(Pid) -> erlang:pid_to_list(Pid);
to_list(Binary) when is_binary(Binary) -> erlang:binary_to_list(Binary);
to_list(Port) when is_port(Port) -> erlang:port_to_list(Port);
to_list(Ref) when is_reference(Ref) -> erlang:ref_to_list(Ref);
to_list(Float) when is_float(Float) -> erlang:float_to_list(Float, [{decimals, 4}]);
to_list(Val) -> Val.
get_menu_title(home, MnesiaTitle) ->
[?select("Home(H)"), "|", ?unselect("Network(N)"), "|", ?unselect("System(S)"), "|", ?unselect("Ets(E)"),
unselect(MnesiaTitle), "|", ?unselect("App(A)"), "|", ?unselect("Doc(D)"), "|", ?unselect("Plugin(P)")];
get_menu_title(ets, MnesiaTitle) ->
[?unselect("Home(H)"), "|", ?unselect("Network(N)"), "|", ?unselect("System(S)"), "|", ?select("Ets(E)"),
unselect(MnesiaTitle), "|", ?unselect("App(A)"), "|", ?unselect("Doc(D)"), "|", ?unselect("Plugin(P)")];
get_menu_title(allocator, MnesiaTitle) ->
[?unselect("Home(H)"), "|", ?unselect("Network(N)"), "|", ?select("System(S)"), "|", ?unselect("Ets(E)"),
unselect(MnesiaTitle), "|", ?unselect("App(A)"), "|", ?unselect("Doc(D)"), "|", ?unselect("Plugin(P)")];
get_menu_title(doc, MnesiaTitle) ->
[?unselect("Home(H)"), "|", ?unselect("Network(N)"), "|", ?unselect("System(S)"), "|", ?unselect("Ets(E)"),
unselect(MnesiaTitle), "|", ?unselect("App(A)"), "|", ?select("Doc(D)"), "|", ?unselect("Plugin(P)")];
get_menu_title(inet, MnesiaTitle) ->
[?unselect("Home(H)"), "|", ?select("Network(N)"), "|", ?unselect("System(S)"), "|", ?unselect("Ets(E)"),
unselect(MnesiaTitle), "|", ?unselect("App(A)"), "|", ?unselect("Doc(D)"), "|", ?unselect("Plugin(P)")];
get_menu_title(mnesia, MnesiaTitle) ->
[?unselect("Home(H)"), "|", ?unselect("Network(N)"), "|", ?unselect("System(S)"), "|", ?unselect("Ets(E)"),
select(MnesiaTitle), "|", ?unselect("App(A)"), "|", ?unselect("Doc(D)"), "|", ?unselect("Plugin(P)")];
get_menu_title(app, MnesiaTitle) ->
[?unselect("Home(H)"), "|", ?unselect("Network(N)"), "|", ?unselect("System(S)"), "|", ?unselect("Ets(E)"),
unselect(MnesiaTitle), "|", ?select("App(A)"), "|", ?unselect("Doc(D)"), "|", ?unselect("Plugin(P)")].
-spec select(string()) -> list().
select(Title) -> [?RED_BG, Title, ?RESET_BG].
-spec unselect(string()) -> list().
unselect(Title) -> [?L_GRAY_BG, Title, ?RESET_BG].
-spec green(list()) -> list().
green(String) -> "\e[32;1m" ++ String ++ "\e[0m".
-spec to_byte(pos_integer()) -> list().
to_byte(Byte) when is_integer(Byte), Byte < 1024 -> %% byte
[erlang:integer_to_list(Byte), $ , $B];
to_byte(Byte) when Byte < 1024 * 1024 -> %% kilobyte
[erlang:float_to_list(Byte / 1024, [{decimals, 4}]), $ , $K, $B];
to_byte(Byte) when Byte < 1024 * 1024 * 1024 -> %% megabyte
[erlang:float_to_list(Byte / (1024 * 1024), [{decimals, 4}]), $ , $M, $B];
to_byte(Byte) when is_integer(Byte) -> %% megabyte
[erlang:float_to_list(Byte / (1024 * 1024 * 1024), [{decimals, 4}]), $ , $G, $B];
to_byte(Byte) -> %% process died
[to_list(Byte)].
-spec mfa_to_list({atom(), atom(), integer()}|term()) -> nonempty_string().
mfa_to_list({Module, Fun, Arg}) ->
[atom_to_list(Module), ":", atom_to_list(Fun), "/", integer_to_list(Arg)];
mfa_to_list(Function) ->
io_lib:format("~p", [Function]).
-spec render(list()) -> iolist().
render(FA) ->
{F, A} = tidy_format_args([" \e[0m|~n" | lists:reverse(["|" | FA])], true, [], []),
io_lib:format(erlang:iolist_to_binary(F), A).
%{erlang:iolist_to_binary(F), A}.
-spec render_menu(atom(), string()) -> iolist().
render_menu(Type, Text) ->
MnesiaTitle = case ets:info(schema, owner) of undefined -> ""; _ -> "/Mnesia(M)" end,
Title = get_menu_title(Type, MnesiaTitle),
UpTime = uptime(),
TitleWidth = ?COLUMN + 151 - erlang:length(UpTime),
?render([?W([Title | Text], TitleWidth) | UpTime]).
tidy_format_args([], _NeedLine, FAcc, AAcc) -> {FAcc, AAcc};
tidy_format_args([{extend, A, W} | Rest], true, FAcc, AAcc) ->
WBin = erlang:integer_to_binary(W),
F = <<"~-", WBin/binary, ".", WBin/binary, "ts">>,
tidy_format_args(Rest, false, [F | FAcc], [to_str(A) | AAcc]);
tidy_format_args([{extend, A, W} | Rest], false, FAcc, AAcc) ->
WBin = erlang:integer_to_binary(W),
F = <<"~-", WBin/binary, ".", WBin/binary, "ts", ?I/binary>>,
tidy_format_args(Rest, false, [F | FAcc], [to_str(A) | AAcc]);
tidy_format_args([{extend_color, C, A, W} | Rest], true, FAcc, AAcc) ->
WBin = erlang:integer_to_binary(W),
F = <<C/binary, "~-", WBin/binary, ".", WBin/binary, "ts">>,
tidy_format_args(Rest, false, [F | FAcc], [to_str(A) | AAcc]);
tidy_format_args([{extend_color, C, A, W} | Rest], false, FAcc, AAcc) ->
WBin = erlang:integer_to_binary(W),
F = <<C/binary, "~-", WBin/binary, ".", WBin/binary, "ts", ?I2/binary>>,
tidy_format_args(Rest, false, [F | FAcc], [to_str(A) | AAcc]);
tidy_format_args([{extend_color_2, C, A, W} | Rest], true, FAcc, AAcc) ->
WBin = erlang:integer_to_binary(W),
F = <<C/binary, "~-", WBin/binary, ".", WBin/binary, "ts">>,
tidy_format_args(Rest, false, [F | FAcc], [to_str(A) | AAcc]);
tidy_format_args([{extend_color_2, C, A, W} | Rest], false, FAcc, AAcc) ->
WBin = erlang:integer_to_binary(W),
F = <<C/binary, "~-", WBin/binary, ".", WBin/binary, "ts", ?RESET/binary, ?I2/binary>>,
tidy_format_args(Rest, false, [F | FAcc], [to_str(A) | AAcc]);
tidy_format_args([F | Rest], NeedLine, FAcc, AAcc) ->
tidy_format_args(Rest, NeedLine, [F | FAcc], AAcc).
to_str({byte, Bytes}) -> to_byte(Bytes);
to_str(Term) -> to_list(Term).
-spec parse_cmd(#view_opts{}, [pid()]) -> atom()|string().
%when we have a print fun we assume we also do not fun in io mode and expect the actions as messages
parse_cmd(#view_opts{home = #home{printFun = PrintFun}}=ViewOpts, Pids)when is_function(PrintFun) ->
receive
"ic\n" -> inet_count;
"iw\n" -> inet_window;
"rc\n" -> recv_cnt;
"ro\n" -> recv_oct;
"sc\n" -> send_cnt;
"so\n" -> send_oct;
"cnt\n" -> cnt;
"oct\n" -> oct;
%% menu view
"H\n" ->
exit_processes(Pids),
observer_cli:start(ViewOpts);
"S\n" ->
exit_processes(Pids),
observer_cli_system:start(ViewOpts);
"A\n" ->
exit_processes(Pids),
observer_cli_application:start(ViewOpts);
"N\n" ->
exit_processes(Pids),
observer_cli_inet:start(ViewOpts);
"M\n" ->
exit_processes(Pids),
observer_cli_mnesia:start(ViewOpts);
"E\n" ->
exit_processes(Pids),
observer_cli_ets:start(ViewOpts);
"D\n" ->
exit_processes(Pids),
observer_cli_help:start(ViewOpts);
"P\n" ->
exit_processes(Pids),
observer_cli_plugin:start(ViewOpts);
"q\n" -> quit;
"Q\n" -> quit;
"pu\n" -> page_up_top_n; %% backward
"pd\n" -> page_down_top_n; %% forward
"PU\n" -> page_up_top_n; %% backward
"PD\n" -> page_down_top_n; %% forward
"B\n" -> page_up_top_n; %% backward
"F\n" -> page_down_top_n; %% forward
%% home
"p\n" -> pause_or_resume;
"r\n" -> {func, proc_count, reductions};
"b\n" -> {func, proc_count, binary_memory};
"t\n" -> {func, proc_count, total_heap_size};
"m\n" -> {func, proc_count, memory};
"mq\n" -> {func, proc_count, message_queue_len};
"rr\n" -> {func, proc_window, reductions};
"bb\n" -> {func, proc_window, binary_memory};
"tt\n" -> {func, proc_window, total_heap_size};
"mm\n" -> {func, proc_window, memory};
"mmq\n" -> {func, proc_window, message_queue_len};
"\n" -> jump;
"s\n" -> size;
"hide\n" -> hide;
Number ->
parse_integer(Number)
end;
%regular iolib version
parse_cmd(ViewOpts, Pids) ->
case to_list(io:get_line("")) of
%% inet
"ic\n" -> inet_count;
"iw\n" -> inet_window;
"rc\n" -> recv_cnt;
"ro\n" -> recv_oct;
"sc\n" -> send_cnt;
"so\n" -> send_oct;
"cnt\n" -> cnt;
"oct\n" -> oct;
%% menu view
"H\n" ->
exit_processes(Pids),
observer_cli:start(ViewOpts);
"S\n" ->
exit_processes(Pids),
observer_cli_system:start(ViewOpts);
"A\n" ->
exit_processes(Pids),
observer_cli_application:start(ViewOpts);
"N\n" ->
exit_processes(Pids),
observer_cli_inet:start(ViewOpts);
"M\n" ->
exit_processes(Pids),
observer_cli_mnesia:start(ViewOpts);
"E\n" ->
exit_processes(Pids),
observer_cli_ets:start(ViewOpts);
"D\n" ->
exit_processes(Pids),
observer_cli_help:start(ViewOpts);
"P\n" ->
exit_processes(Pids),
observer_cli_plugin:start(ViewOpts);
"q\n" -> quit;
"Q\n" -> quit;
"pu\n" -> page_up_top_n; %% backward
"pd\n" -> page_down_top_n; %% forward
"PU\n" -> page_up_top_n; %% backward
"PD\n" -> page_down_top_n; %% forward
"B\n" -> page_up_top_n; %% backward
"F\n" -> page_down_top_n; %% forward
%% home
"p\n" -> pause_or_resume;
"r\n" -> {func, proc_count, reductions};
"b\n" -> {func, proc_count, binary_memory};
"t\n" -> {func, proc_count, total_heap_size};
"m\n" -> {func, proc_count, memory};
"mq\n" -> {func, proc_count, message_queue_len};
"rr\n" -> {func, proc_window, reductions};
"bb\n" -> {func, proc_window, binary_memory};
"tt\n" -> {func, proc_window, total_heap_size};
"mm\n" -> {func, proc_window, memory};
"mmq\n" -> {func, proc_window, message_queue_len};
"\n" -> jump;
"s\n" -> size;
"hide\n" -> hide;
Number ->
parse_integer(Number)
end.
-spec next_redraw(reference(), pos_integer()) -> reference().
next_redraw(LastTimeRef, Interval) ->
LastTimeRef =/= ?INIT_TIME_REF andalso erlang:cancel_timer(LastTimeRef),
erlang:send_after(Interval, self(), redraw).
-spec get_terminal_rows(boolean()) -> integer().
get_terminal_rows(_AutoRow = false) -> ?DEFAULT_ROW_SIZE;
get_terminal_rows(_AutoRow = true) ->
case io:rows() of
{error, _} -> ?DEFAULT_ROW_SIZE;
{ok, Rows} -> Rows
end.
-spec parse_integer(string()) -> {term(), term()}.
parse_integer(Number) ->
case string:to_integer(Number) of
{error, _Reason} ->
{input_str, Number -- "\n"};
{Integer, _} ->
if Integer >= ?MIN_INTERVAL ->
{new_interval, Integer};
Integer > 0 ->
{jump, Integer};
true ->
{input_str, Number -- "\n"}
end
end.
-spec render_last_line(string()) -> list().
render_last_line(Text) ->
?render([?UNDERLINE, ?GRAY_BG, ?W(Text, ?COLUMN + 2)]).
-spec exit_processes(list()) -> ok.
exit_processes(List) ->
[erlang:exit(Pid, stop) || Pid <- List],
flush(),
ok.
-spec update_page_pos(pid() | pos_integer(), pos_integer(), list()) -> list().
update_page_pos(StorePid, Page, Pages) when is_pid(StorePid) ->
Pos =
case lists:keyfind(Page, 1, Pages) of
false ->
Row = observer_cli_store:lookup_row(StorePid),
(Page - 1) * Row + 1;
{_, P} -> P
end,
update_page_pos(Page, Pos, Pages);
update_page_pos(Page, Pos, Pages) ->
[{Page, Pos} | lists:keydelete(Page, 1, Pages)].
-spec get_pos(pos_integer(), pos_integer(), list(), pos_integer()) ->
{pos_integer(), pos_integer()}.
get_pos(_Page, _PageRow, _Pages, 0) -> {1, 1};
get_pos(Page, PageRow, Pages, TopLen) ->
Start = erlang:min((Page - 1) * PageRow + 1, TopLen),
case lists:keyfind(Page, 1, Pages) of
{_, P} when P >= Start andalso P =< Start + PageRow -> {Start, P};
_ -> {Start, Start}
end.
flush() ->
receive _Msg ->
flush()
after 100 -> ok
end.
-spec sublist(list(), integer(), integer()) -> list().
sublist(AllEts, Rows, CurPage) ->
SortEts = recon_lib:sublist_top_n_attrs(AllEts, Rows * CurPage),
Start = Rows * (CurPage - 1) + 1,
case erlang:length(SortEts) >= Start of
true ->
lists:sublist(SortEts, Start, Rows);
false -> []
end.
-spec sbcs_to_mbcs(list(), list()) -> list().
sbcs_to_mbcs(TypeList, STMList) ->
FoldlFun =
fun({{Type, _}, New}, Acc) when is_number(New) ->
case lists:member(Type, TypeList) of
true -> maps:update_with(Type, fun(V) -> V + New end, 0, Acc);
false -> Acc
end;
(_, Acc) -> Acc
end,
maps:to_list(lists:foldl(FoldlFun, #{}, STMList)).
print(Fun, Format, Args)when is_function(Fun) ->
Fun(Format, Args);
print(Fun,Format,Args)->
io:format(Format,Args).