Auto-pull-request-on-2020-07-17 (#3599)

* chore: update app.src.script
* chore(alarms): Add timestamp for alarm
* perf(emqx_vm): make emqx_vm:get_memory/0 more efficiency
This commit is contained in:
Rory Z 2020-07-17 16:08:00 +08:00 committed by GitHub
parent a7c4266acb
commit 32effd632c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 29 deletions

View File

@ -13,13 +13,12 @@ end,
RemoveLeadingV = RemoveLeadingV =
fun(Tag) -> fun(Tag) ->
case re:run(Tag, "v\[0-9\]+\.\[0-9\]+\.*") of case re:run(Tag, "^[v|e]?[0-9]\.[0-9]\.([0-9]|(rc|beta|alpha)\.[0-9])", [{capture, none}]) of
nomatch -> nomatch ->
Tag; Tag;
{match, _} -> {match, _} ->
%% if it is a version number prefixed by 'v' then remove the 'v' %% if it is a version number prefixed by 'v' or 'e', then remove it
"v" ++ Vsn = Tag, re:replace(Tag, "[v|e]", "", [{return ,list}])
Vsn
end end
end, end,

View File

@ -105,7 +105,7 @@ init(_) ->
{ok, []}. {ok, []}.
handle_event({set_alarm, {AlarmId, AlarmDesc = #alarm{timestamp = undefined}}}, State) -> handle_event({set_alarm, {AlarmId, AlarmDesc = #alarm{timestamp = undefined}}}, State) ->
handle_event({set_alarm, {AlarmId, AlarmDesc#alarm{timestamp = erlang:system_time(millisecond)}}}, State); handle_event({set_alarm, {AlarmId, AlarmDesc#alarm{timestamp = erlang:system_time(second)}}}, State);
handle_event({set_alarm, Alarm = {AlarmId, AlarmDesc}}, State) -> handle_event({set_alarm, Alarm = {AlarmId, AlarmDesc}}, State) ->
?LOG(warning, "New Alarm: ~p, Alarm Info: ~p", [AlarmId, AlarmDesc]), ?LOG(warning, "New Alarm: ~p, Alarm Info: ~p", [AlarmId, AlarmDesc]),
case encode_alarm(Alarm) of case encode_alarm(Alarm) of
@ -114,7 +114,7 @@ handle_event({set_alarm, Alarm = {AlarmId, AlarmDesc}}, State) ->
{error, Reason} -> {error, Reason} ->
?LOG(error, "Failed to encode alarm: ~p", [Reason]) ?LOG(error, "Failed to encode alarm: ~p", [Reason])
end, end,
set_alarm_(AlarmId, AlarmDesc), set_alarm_(AlarmId, AlarmDesc, erlang:system_time(second)),
{ok, State}; {ok, State};
handle_event({clear_alarm, AlarmId}, State) -> handle_event({clear_alarm, AlarmId}, State) ->
?LOG(info, "Clear Alarm: ~p", [AlarmId]), ?LOG(info, "Clear Alarm: ~p", [AlarmId]),
@ -164,10 +164,12 @@ encode_alarm({AlarmId, #alarm{severity = Severity,
}); });
encode_alarm({AlarmId, undefined}) -> encode_alarm({AlarmId, undefined}) ->
emqx_json:safe_encode(#{id => maybe_to_binary(AlarmId)}); emqx_json:safe_encode(#{id => maybe_to_binary(AlarmId),
desc => #{timestamp => erlang:system_time(second)}});
encode_alarm({AlarmId, AlarmDesc}) -> encode_alarm({AlarmId, AlarmDesc}) ->
emqx_json:safe_encode(#{id => maybe_to_binary(AlarmId), emqx_json:safe_encode(#{id => maybe_to_binary(AlarmId),
desc => maybe_to_binary(AlarmDesc) desc => #{summary => maybe_to_binary(AlarmDesc),
timestamp => erlang:system_time(second)}
}). }).
alarm_msg(Topic, Payload) -> alarm_msg(Topic, Payload) ->
@ -185,8 +187,8 @@ maybe_to_binary(Data) when is_binary(Data) ->
maybe_to_binary(Data) -> maybe_to_binary(Data) ->
iolist_to_binary(io_lib:format("~p", [Data])). iolist_to_binary(io_lib:format("~p", [Data])).
set_alarm_(Id, Desc) -> set_alarm_(Id, Desc, Ts) ->
mnesia:dirty_write(?ALARM_TAB, #common_alarm{id = Id, desc = Desc}). mnesia:dirty_write(?ALARM_TAB, #common_alarm{id = Id, desc = {Desc, Ts}}).
clear_alarm_(Id) -> clear_alarm_(Id) ->
case mnesia:dirty_read(?ALARM_TAB, Id) of case mnesia:dirty_read(?ALARM_TAB, Id) of
@ -199,5 +201,5 @@ clear_alarm_(Id) ->
set_alarm_history(Id, Desc) -> set_alarm_history(Id, Desc) ->
His = #alarm_history{id = Id, His = #alarm_history{id = Id,
desc = Desc, desc = Desc,
clear_at = erlang:system_time(millisecond)}, clear_at = erlang:system_time(second)},
mnesia:dirty_write(?ALARM_HISTORY_TAB, His). mnesia:dirty_write(?ALARM_HISTORY_TAB, His).

View File

@ -22,6 +22,7 @@
, get_system_info/0 , get_system_info/0
, get_system_info/1 , get_system_info/1
, get_memory/0 , get_memory/0
, get_memory/2
, mem_info/0 , mem_info/0
, loads/0 , loads/0
]). ]).
@ -241,32 +242,47 @@ scheduler_usage_diff(First, Last) ->
end, lists:zip(lists:sort(First), lists:sort(Last))). end, lists:zip(lists:sort(First), lists:sort(Last))).
get_memory()-> get_memory()->
[{Key, get_memory(Key, current)} || Key <- [used, allocated, unused, usage]] ++ erlang:memory(). get_memory_once(current) ++ erlang:memory().
get_memory(Ks, Keyword) when is_list(Ks) ->
Ms = get_memory_once(Keyword) ++ erlang:memory(),
[M || M = {K, _} <- Ms, lists:member(K, Ks)];
get_memory(used, Keyword) -> get_memory(used, Keyword) ->
lists:sum(lists:map(fun({_, Prop}) -> lists:sum(lists:map(fun({_, Prop}) ->
container_size(Prop, Keyword, blocks_size) container_size(Prop, Keyword, blocks_size)
end, util_alloc())); end, util_alloc()));
get_memory(allocated, Keyword) -> get_memory(allocated, Keyword) ->
lists:sum(lists:map(fun({_, Prop})-> lists:sum(lists:map(fun({_, Prop}) ->
container_size(Prop, Keyword, carriers_size) container_size(Prop, Keyword, carriers_size)
end, util_alloc())); end, util_alloc()));
get_memory(unused, Keyword) -> get_memory(unused, Keyword) ->
get_memory(allocated, Keyword) - get_memory(used, Keyword); Ms = get_memory_once(Keyword),
proplists:get_value(allocated, Ms) - proplists:get_value(used, Ms);
get_memory(usage, Keyword) -> get_memory(usage, Keyword) ->
get_memory(used, Keyword) / get_memory(allocated, Keyword). Ms = get_memory_once(Keyword),
proplists:get_value(used, Ms) / proplists:get_value(allocated, Ms).
%% @private A more quickly function to calculate memory
get_memory_once(Keyword) ->
Calc = fun({_, Prop}, {N1, N2}) ->
{N1 + container_size(Prop, Keyword, blocks_size),
N2 + container_size(Prop, Keyword, carriers_size)}
end,
{Used, Allocated} = lists:foldl(Calc, {0, 0}, util_alloc()),
[{used, Used},
{allocated, Allocated},
{unused, Allocated - Used},
{usage, Used / Allocated}].
util_alloc()-> util_alloc()->
alloc(?UTIL_ALLOCATORS). alloc(?UTIL_ALLOCATORS).
alloc()->
{_Mem, Allocs} = snapshot_int(),
Allocs.
alloc(Type) -> alloc(Type) ->
[{{T, Instance}, Props} || {{T, Instance}, Props} <- alloc(), lists:member(T, Type)]. [{{T, Instance}, Props} || {{T, Instance}, Props} <- allocators(), lists:member(T, Type)].
snapshot_int() ->
{erlang:memory(), allocators()}.
allocators() -> allocators() ->
UtilAllocators = erlang:system_info(alloc_util_allocators), UtilAllocators = erlang:system_info(alloc_util_allocators),