Skip to content

Commit 5bf2335

Browse files
committed
Various changes
- Cleanup of state records - Sorting based on numbers 1-9 switched to 0-9 as we have 10 columns now - ChangeLog added and version in .app.src derived from ChangeLog - Added convenience Makefile
1 parent 0798307 commit 5bf2335

9 files changed

+132
-108
lines changed

ChangeLog

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
* Tue Jul 10 2018 Anthony Molinaro (djnym) 0.4.0
2+
- Support column resize on window resize (thanks Vagabond)
3+
- Support nodenames of the form [email protected] (thanks Vagabond)
4+
- Cleanup of state records
5+
- Sorting based on numbers 1-9 switched to 0-9 as we have 10 columns now
6+
- ChangeLog added and version in .app.src derived from ChangeLog
7+
- Added convenience Makefile
8+
9+
* Tue May 08 2018 Anthony Molinaro (djnym) 0.3.1
10+
- Update version of cecho
11+
12+
* Sun Aug 13 2017 Mazen Harake (mazenharake) 0.3.0
13+
- Update to use rebar3
14+
15+
* Sat Jun 11 2017 Mazen Harake (mazenharake) 0.2.0
16+
- License changed to BSD
17+
18+
* Mon Aug 16 2010 Mazen Harake (mazenharake) 0.0.1
19+
First release
20+
- Shows most common information which is needed when monitoring a remote
21+
Erlang node
22+
- Sorting can be done on any column and sorting can be reversed
23+
- Easy to extend by specifiying own callback modules

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
all:
2+
@rebar3 compile
3+
4+
check:
5+
@rebar3 as test do dialyzer,eunit,cover
6+
7+
clean:
8+
@rebar3 clean
9+
10+
maintainer-clean: clean
11+
rm -rf _build

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ entop's interface can be customized so this section only applies for the "built-
4040

4141
### Commands when running entop:
4242

43-
[1-N]:
44-
Sort on column number 1 through N. Starts with first column (1) and up to N
45-
where N is the last column.
43+
[0-9]:
44+
Sort on column number 0 through 9. Starts with first column (0)
45+
and up to the last column (9).
4646

4747
r:
4848
Toggles the sorting order from ascending to descending and vice versa.
@@ -54,7 +54,8 @@ Ctrl-C:
5454
Same as 'q'.
5555

5656
'<' and '>':
57-
Moves the sorting column to the left or right respectively (these are the lower/greater-than-tags; not arrow keys).
57+
Moves the sorting column to the left or right respectively
58+
(these are the lower/greater-than-tags; not arrow keys).
5859

5960
### GProc Support
6061

src/entop.app.src

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{application, entop,
22
[{description, "A top-like tool for monitoring an erlang node"},
3-
{vsn, "0.3.1"},
3+
{vsn, {cmd, "/bin/bash -c 'awk \"match(\\$0, /[0-9]+\\.[0-9]+(\\.[0-9]+)+/){print substr(\\$0, RSTART,RLENGTH); exit}\" ChangeLog'"}},
44
{registered, []},
55
{applications,
66
[kernel,
@@ -10,6 +10,6 @@
1010
{env,[]},
1111
{modules, [entop, entop_collector, entop_format, entop_view, entop_net]},
1212
{maintainers, []},
13-
{licenses, ["Apache 2.0"]},
13+
{licenses, ["BSD"]},
1414
{links, []}
1515
]}.

src/entop.erl

+12-17
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
-module(entop).
2828

29-
-include("entop.hrl").
3029
-include_lib("cecho/include/cecho.hrl").
3130

3231
%% escript bits
@@ -65,14 +64,12 @@ main([NodeIn,CookieIn]) ->
6564
{ok, _} ->
6665
case maybe_set_cookie(Node,Cookie) of
6766
ok ->
68-
case net_kernel:connect(Node) of
69-
true ->
70-
ViewPid = entop_view:start(#state{ node = Node,
71-
connected = true }),
72-
control(ViewPid);
73-
false ->
67+
case start(Node) of
68+
{error, cant_connect} ->
7469
io:format("Unable to connect to '~p', check nodename, cookie and network~n",[Node]),
75-
halt(101)
70+
halt(101);
71+
Started ->
72+
Started
7673
end;
7774
CE ->
7875
io:format("Cookie ~p is malformed, got error ~p~n", [Cookie,CE]),
@@ -118,19 +115,17 @@ name_type (Node) when is_list(Node) ->
118115
%% Application API
119116
%% =============================================================================
120117
start(Node) ->
121-
State = #state{ node = Node },
122-
case net_kernel:connect(Node) of
123-
true ->
124-
ViewPid = entop_view:start(State#state{ connected = true }),
125-
control(ViewPid);
126-
false ->
127-
halt(101)
128-
end.
118+
case entop_view:start(Node) of
119+
{ok, ViewPid} -> control(ViewPid);
120+
E -> E
121+
end.
129122

130123
control(ViewPid) ->
131124
P = cecho:getch(),
132125
case P of
133-
N when N >= 49 andalso N =< 57 -> ViewPid ! {sort, N - 48}, control(ViewPid);
126+
N when N >= 48 andalso N =< 57 ->
127+
% allow 0-9 to sort based on those columns
128+
ViewPid ! {sort, N - 48}, control(ViewPid);
134129
$> -> ViewPid ! {sort, next}, control(ViewPid);
135130
$< -> ViewPid ! {sort, prev}, control(ViewPid);
136131
$r -> ViewPid ! reverse_sort, control(ViewPid);

src/entop.hrl

-43
This file was deleted.

src/entop_format.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ init(Node) ->
6060
{"Stack Size", 11, [{align, right}]},
6161
{"Heap Size", 12, [{align, right}]}
6262
],
63-
{ok, {Columns, 6}, #state{ node = Node, default_columns=Columns }}.
63+
{ok, {Columns, 5}, #state{ node = Node, default_columns=Columns }}.
6464

6565
resize(Width, State) ->
6666
Columns = State#state.default_columns,

src/entop_net.erl

-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
-module(entop_net).
2828

29-
-include("entop.hrl").
30-
3129
%% Module API
3230
-export([fetch_data/2, reconnect/2, lookup_name/3]).
3331

src/entop_view.erl

+78-39
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
-module(entop_view).
2828

29-
-include("entop.hrl").
3029
-include_lib("cecho/include/cecho.hrl").
3130

3231
%% Module API
@@ -35,16 +34,41 @@
3534
%% Defines
3635
-define(MAX_HLINE, 300).
3736

37+
%% Records
38+
-record(state, { callback = entop_format,
39+
remote_module = entop_collector,
40+
maxyx,
41+
columns,
42+
cbstate,
43+
node,
44+
otp_version,
45+
erts_version,
46+
os_fam,
47+
os,
48+
os_version,
49+
node_flags,
50+
interval = 2000,
51+
reverse_sort = true,
52+
sort = 0,
53+
connected = false,
54+
last_reductions = dict:new() }).
55+
3856
%% =============================================================================
3957
%% Module API
4058
%% =============================================================================
41-
start(State) ->
59+
start(Node) ->
4260
Parent = self(),
43-
NState = load_remote_static_data(State),
44-
remote_load_code(NState#state.remote_module, State#state.node),
45-
ViewPid = erlang:spawn(fun() -> init(Parent, NState) end),
46-
receive continue -> ok end,
47-
ViewPid.
61+
case net_kernel:connect(Node) of
62+
true ->
63+
State = #state { node = Node, connected = true },
64+
NState = load_remote_static_data(State),
65+
remote_load_code(NState#state.remote_module, State#state.node),
66+
ViewPid = erlang:spawn(fun() -> init(Parent, NState) end),
67+
receive continue -> ok end,
68+
{ok, ViewPid};
69+
false ->
70+
{error, cant_connect}
71+
end.
4872

4973
reload(ViewPid) ->
5074
ViewPid ! reload.
@@ -70,28 +94,32 @@ remote_load_code(Module, Node) ->
7094
{_, Binary, Filename} = code:get_object_code(Module),
7195
rpc:call(Node, code, load_binary, [Module, Filename, Binary]).
7296

73-
init(Parent, State) ->
97+
init(Parent, State0) ->
7498
process_flag(trap_exit, true),
7599
application:start(cecho),
76100
ok = cecho:cbreak(),
77101
ok = cecho:noecho(),
78102
ok = cecho:curs_set(?ceCURS_INVISIBLE),
79103
ok = cecho:keypad(?ceSTDSCR, true),
80-
NState = init_callback(State),
81-
print_nodeinfo(NState),
104+
% keep track of the max screen size so we can resize if necessary
105+
MaxYX = cecho:getmaxyx(),
106+
State1 = State0#state { maxyx = MaxYX },
107+
108+
State2 = init_callback(State1),
109+
print_nodeinfo(State2),
82110
Parent ! continue,
83111
self() ! time_update,
84-
loop(Parent, NState).
112+
loop(Parent, State2).
85113

86114
init_callback(State) ->
87115
case (State#state.callback):init(State#state.node) of
88-
{ok, {Columns, DefaultSort}, CBState} when DefaultSort =< length(Columns)
89-
andalso DefaultSort >= 1 ->
90-
NSort = DefaultSort;
116+
{ok, {Columns, DefaultSort}, CBState}
117+
when DefaultSort >= 0 andalso DefaultSort < length(Columns) ->
118+
% sorting is based on 0 indices so all columns can be sorted on
119+
State#state{ columns = Columns, cbstate = CBState, sort = DefaultSort };
91120
{ok, {Columns, _}, CBState} ->
92-
NSort = 1
93-
end,
94-
State#state{ columns = Columns, cbstate = CBState, sort = NSort }.
121+
State#state{ columns = Columns, cbstate = CBState, sort = 0 }
122+
end.
95123

96124
print_nodeinfo(State) ->
97125
cecho:move(0, 0),
@@ -139,10 +167,11 @@ loop(Parent, State) ->
139167
State2 = update_sort_screen(State, N),
140168
loop(Parent, State2);
141169
{sort, Direction} ->
142-
case Direction of
143-
next -> State2 = update_sort_screen(State, State#state.sort + 1);
144-
prev -> State2 = update_sort_screen(State, State#state.sort - 1)
145-
end,
170+
State2 =
171+
case Direction of
172+
next -> update_sort_screen(State, State#state.sort + 1);
173+
prev -> update_sort_screen(State, State#state.sort - 1)
174+
end,
146175
loop(Parent, State2);
147176
reverse_sort ->
148177
State2 = fetch_and_update(State#state{ reverse_sort = (not State#state.reverse_sort) }, true),
@@ -171,29 +200,39 @@ fetch_and_update(State, IsForced) ->
171200
end.
172201

173202
update_sort_screen(State, N) ->
174-
if N >= 1 andalso N =< length(State#state.columns) ->
175-
fetch_and_update(State#state{ sort = N }, true);
176-
true -> State
203+
case N >= 0 andalso N < length(State#state.columns) of
204+
true -> fetch_and_update(State#state{ sort = N }, true);
205+
false -> State
177206
end.
178207

179-
update_screen(Time, HeaderData, RowDataList, State) ->
180-
print_nodeinfo(State),
181-
draw_title_bar(State),
182-
print_showinfo(State, Time),
183-
{Headers, State1} = process_header_data(HeaderData, State),
208+
update_screen(Time, HeaderData, RowDataList, State0) ->
209+
% check to see if the screen has changed and if it has resize it
210+
NewMaxYX = {MaxY, MaxX} = cecho:getmaxyx(),
211+
State1 =
212+
case NewMaxYX =:= State0#state.maxyx of
213+
true -> State0;
214+
false ->
215+
% then resize the columns if it has
216+
{ok, Columns} =
217+
(State0#state.callback):resize(MaxX, State0#state.cbstate),
218+
State0#state{columns = Columns}
219+
end,
220+
221+
print_nodeinfo(State1),
222+
draw_title_bar(State1),
223+
print_showinfo(State1, Time),
224+
{Headers, State2} = process_header_data(HeaderData, State1),
184225
lists:foldl(fun(Header, Y) ->
185226
cecho:hline($ , ?MAX_HLINE),
186227
cecho:mvaddstr(Y, 0, Header), Y + 1
187228
end, 1, Headers),
188-
{RowList, State2} = process_row_data(RowDataList, State1),
189-
SortedRowList = sort(RowList, State),
190-
{Y, X} = cecho:getmaxyx(),
191-
{ok, Columns} = (State#state.callback):resize(X, State2#state.cbstate),
192-
StartY = (Y-(Y-8)),
193-
lists:foreach(fun(N) -> cecho:move(N, 0), cecho:hline($ , ?MAX_HLINE) end, lists:seq(StartY, Y)),
194-
update_rows(SortedRowList, Columns, StartY, Y),
229+
{RowList, State3} = process_row_data(RowDataList, State2),
230+
SortedRowList = sort(RowList, State3),
231+
StartY = (MaxY-(MaxY-8)),
232+
lists:foreach(fun(N) -> cecho:move(N, 0), cecho:hline($ , ?MAX_HLINE) end, lists:seq(StartY, MaxY)),
233+
update_rows(SortedRowList, State3#state.columns, StartY, MaxY),
195234
cecho:refresh(),
196-
State2#state{columns=Columns}.
235+
State3.
197236

198237
draw_title_bar(State) ->
199238
cecho:move(7, 0),
@@ -211,7 +250,7 @@ draw_title_bar([{Title, Width, Options}|Rest], Offset) ->
211250
print_showinfo(State, RoundTripTime) ->
212251
cecho:move(6, 0),
213252
cecho:hline($ , ?MAX_HLINE),
214-
ColName = element(1,lists:nth(State#state.sort, State#state.columns)),
253+
ColName = element(1,lists:nth(State#state.sort+1, State#state.columns)),
215254
SortName = if State#state.reverse_sort -> "Descending"; true -> "Ascending" end,
216255
Showing = io_lib:format("Interval ~pms, Sorting on ~p (~s), Retrieved in ~pms",
217256
[State#state.interval, ColName, SortName, RoundTripTime div 1000]),
@@ -241,7 +280,7 @@ prd([RowData|Rest], #state{last_reductions = LRD} = State, FullAcc = {Acc, LRDAc
241280
end.
242281

243282
sort(ProcList, State) ->
244-
Sorted = lists:keysort(State#state.sort, ProcList),
283+
Sorted = lists:keysort(State#state.sort+1, ProcList),
245284
case State#state.reverse_sort of
246285
true ->
247286
lists:reverse(Sorted);

0 commit comments

Comments
 (0)