Browse Source

新增提交

master
AICells 4 years ago
parent
commit
abf365f990
4 changed files with 321 additions and 1 deletions
  1. +31
    -0
      src/dataType/utString.erl
  2. +62
    -0
      src/dataType/utTuple.erl
  3. +61
    -1
      src/srvNodeMgr/u.erl
  4. +167
    -0
      src/srvNodeMgr/utVMInfo.erl

+ 31
- 0
src/dataType/utString.erl View File

@ -0,0 +1,31 @@
-module(utString).
%% API
-export([
toLowerStr/1,
toUpperStr/1
]).
toLowerStr(List) when is_list(List) ->
[begin
case C >= $A andalso C =< $Z of
true ->
C - $A + $a;
_ ->
C
end
end || C <- List];
toLowerStr(Bin) when is_binary(Bin) ->
toLowerStr(utTypeCast:toList(Bin)).
toUpperStr(List) when is_list(List) ->
[begin
case C >= $a andalso C =< $z of
true ->
C - $a + $A;
_ ->
C
end
end || C <- List];
toUpperStr(Bin) when is_binary(Bin) ->
toUpperStr(utTypeCast:toList(Bin)).

+ 62
- 0
src/dataType/utTuple.erl View File

@ -0,0 +1,62 @@
-module(utTuple).
-define(LIST_TO_RECORD(RecName, List), list_to_tuple([RecName])).
%% ====================================================================
%% API functions
%% ====================================================================
-export([
copy_elements/2,
update/2,
update_counter/2,
filter_value/2,
update/3
]).
copy_elements(DestTuple, SrcTuple) ->
DestList = tuple_to_list(DestTuple),
SrcList = tuple_to_list(SrcTuple),
List = util_list:copy_elements(DestList, SrcList),
list_to_tuple(List).
update([], _FunMap, Tuple) ->
Tuple;
update([{K, V} | T], FunMap, Tuple) ->
List = FunMap(K),
UpdateList = make_update_list(List, Tuple, []),
NewTuple = update1(UpdateList, V),
update(T, FunMap, NewTuple).
update1([], V) ->
V;
update1([{K, R} | T], V) ->
NV = setelement(K, R, V),
update1(T, NV).
make_update_list([], _R, List) ->
List;
make_update_list([K | T], R, List) ->
NR = element(K, R),
make_update_list(T, NR, [{K, R} | List]).
update(Tuple, UpList) ->
lists:foldl(fun({Index, Value}, TupleAcc) ->
setelement(Index, TupleAcc, Value)
end, Tuple, UpList).
update_counter(Tuple, UpList) ->
lists:foldl(fun({Index, Change}, TupleAcc) ->
Value = element(Index, TupleAcc),
setelement(Index, TupleAcc, Value + Change)
end, Tuple, UpList).
filter_value(Tuple, Value) ->
List = tuple_to_list(Tuple),
ListNew = lists:map(fun(Val) ->
case Val == Value of
true -> undefined;
false -> Val
end
end, List),
list_to_tuple(ListNew).

+ 61
- 1
src/srvNodeMgr/u.erl View File

@ -148,4 +148,64 @@ load([FileName | T]) ->
a() ->
c(),
u().
u().
%% @spec hotswap() -> ok
%% @doc
hotswap(NodeArg) ->
Node = util_data:to_atom(hd(NodeArg)),
net_adm:ping(Node),
rpc:call(Node, ?MODULE, do_hotswap, []).
do_hotswap() ->
CommandFile = "../../hotswap/hotswap_command.txt",
case filelib:is_file(CommandFile) andalso filelib:file_size(CommandFile) > 2 of
true ->
{{Y, M, D}, {H, I, S}} = erlang:localtime(),
TimeString = io_lib:format("[~w-~w-~w ~w:~w:~w]", [Y, M, D, H, I, S]),
try
info("===> command running ..."),
case file:open(CommandFile, read) of
{ok, IoDevice} ->
case file:eval(CommandFile) of
ok -> info("Update Time: ~s", [TimeString]);
{error, EvalErr} -> info("~s Eval Error:~w", [TimeString, EvalErr])
end,
info("command:"),
ResultData = parse(IoDevice, <<>>),
case byte_size(ResultData) < 5 of
true -> info("Empty Command (<5byte): ~s", [TimeString]);
false -> info("=> ~s", [ResultData])
end,
file:close(IoDevice),
file:delete(CommandFile);
{error, OpenErr} ->
info("~s Open Error:~w\n", [TimeString, OpenErr])
end
catch T : X ->
info("~s Error: ~w : ~w\nUpdate Server Is Stopped!\n", [TimeString, T, X])
end;
false -> ignore
end,
ok.
parse(IoDevice, D) ->
case io:get_line(IoDevice, '') of
eof ->
%% info("\n--- DONE ---"),
D;
Data ->
%% io:format("=> ~s", [Data]),
parse(IoDevice, list_to_binary([D, Data]))
end.
%% @doc
%% @spec make_sure_working_dir() -> any().
make_sure_working_dir() ->
Cwd = config:get_cwd(),
case file:get_cwd() == Cwd of
true ->
skip;
_ ->
c:cd(Cwd)
end.

+ 167
- 0
src/srvNodeMgr/utVMInfo.erl View File

@ -0,0 +1,167 @@
-module(utVMInfo).
-compile([export_all, nowarn_export_all]).
%%
show_cache() ->
io:format("table name | memory | size~n", []),
lists:reverse(lists:keysort(2, [{T, ets:info(T, memory), ets:info(T, size)} || T <- ets:all()])).
%%
show_process() ->
lists:reverse(lists:keysort(2, [{erlang:process_info(P, registered_name), erlang:process_info(P, heap_size)} || P <- erlang:processes()])).
%%
show_process_count() ->
length(erlang:processes()).
%%
%% 线reltools没掌握好
decompile(Mod) ->
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(code:which(Mod), [abstract_code]),
io:format("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
%%
%% jstackhang住等问题用到
pstack(Reg) when is_atom(Reg) ->
case whereis(Reg) of
undefined -> undefined;
Pid -> pstack(Pid)
end;
pstack(Pid) ->
io:format("~s~n", [element(2, process_info(Pid, backtrace))]).
%% ====================================================================
%% etop
%% cpu占用进程使w进程node 使
%% CPU占用排名
%% --------------------------------------------------------------------
etop() ->
spawn(fun() -> etop:start([{output, text}, {interval, 10}, {lines, 20}, {sort, reductions}]) end).
%% Mem占用排名
etop_mem() ->
spawn(fun() -> etop:start([{output, text}, {interval, 10}, {lines, 20}, {sort, memory}]) end).
%% etop
etop_stop() ->
etop:stop().
%% ====================================================================
%% process做gc
%% gc不过来
gc_all() ->
[erlang:garbage_collect(Pid) || Pid <- processes()].
%% MFA
%% :
%% fprof
fprof(M, F, A) ->
fprof:start(),
fprof:apply(M, F, A),
fprof:profile(),
fprof:analyse(),
fprof:stop().
%% eprof, eprof 线,!
%% TimeoutSec<10s< 1000crash
%% :
%% mod调用执行时间
%% mod - Fun
eprof_all(TimeoutSec) ->
eprof(processes() -- [whereis(eprof)], TimeoutSec).
eprof(Pids, TimeoutSec) ->
eprof:start(),
eprof:start_profiling(Pids),
timer:sleep(TimeoutSec),
eprof:stop_profiling(),
eprof:analyze(total),
eprof:stop().
%% scheduler usage
%% 1s每个调度器CPU的实际利用率(spin wait, usage top显示低很多)
scheduler_usage() ->
scheduler_usage(1000).
scheduler_usage(RunMs) ->
erlang:system_flag(scheduler_wall_time, true),
Ts0 = lists:sort(erlang:statistics(scheduler_wall_time)),
timer:sleep(RunMs),
Ts1 = lists:sort(erlang:statistics(scheduler_wall_time)),
erlang:system_flag(scheduler_wall_time, false),
Cores = lists:map(fun({{_I, A0, T0}, {I, A1, T1}}) ->
{I, (A1 - A0) / (T1 - T0)} end, lists:zip(Ts0, Ts1)),
{A, T} = lists:foldl(fun({{_, A0, T0}, {_, A1, T1}}, {Ai,Ti}) ->
{Ai + (A1 - A0), Ti + (T1 - T0)} end, {0, 0}, lists:zip(Ts0, Ts1)),
Total = A/T,
io:format("~p~n", [[{total, Total} | Cores]]).
%%
%% 1s内调度进程数量()
scheduler_stat() ->
scheduler_stat(1000).
scheduler_stat(RunMs) ->
erlang:system_flag(scheduling_statistics, enable),
Ts0 = erlang:system_info(total_scheduling_statistics),
timer:sleep(RunMs),
Ts1 = erlang:system_info(total_scheduling_statistics),
erlang:system_flag(scheduling_statistics, disable),
lists:map(fun({{_Key, In0, Out0}, {Key, In1, Out1}}) ->
{Key, In1 - In0, Out1 - Out0} end, lists:zip(Ts0, Ts1)).
%% ====================================================================
%% trace
%% mod MFA log args
%% trace Mod
%% --------------------------------------------------------------------
trace(Mod) ->
dbg:tracer(),
dbg:tpl(Mod, '_', []),
dbg:p(all, c).
%% trace Node上指定 Mod , shell
trace(Node, Mod) ->
dbg:tracer(),
dbg:n(Node),
dbg:tpl(Mod, '_', []),
dbg:p(all, c).
%% trace
trace_stop() ->
dbg:stop_clear().
%% ====================================================================
%% OOM
%% etop 10w+ , proc后通过pstackmessage_queu_len
proc_mem_all(SizeLimitKb) ->
Procs = [{undefined, Pid} || Pid<- erlang:processes()],
proc_mem(Procs, SizeLimitKb).
proc_mem(SizeLimitKb) ->
Procs = [{Name, Pid} || {_, Name, Pid, _} <- release_handler_1:get_supervised_procs(),
is_process_alive(Pid)],
proc_mem(Procs, SizeLimitKb).
proc_mem(Procs, SizeLimitKb) ->
SizeLimit = SizeLimitKb * 1024,
{R, Total} = lists:foldl(fun({Name, Pid}, {Acc, TotalSize}) ->
case erlang:process_info(Pid, total_heap_size) of
{_, Size0} ->
Size = Size0*8,
case Size > SizeLimit of
true -> {[{Name, Pid, Size} | Acc], TotalSize+Size};
false -> {Acc, TotalSize}
end;
_ -> {Acc, TotalSize}
end
end, {[], 0}, Procs),
R1 = lists:keysort(3, R),
{Total, lists:reverse(R1)}.
%% ====================================================================
%% Internal functions
%% ====================================================================

Loading…
Cancel
Save