Merge pull request #7139 from zhongwencool/improve-uptime-func

Improve uptime/0
This commit is contained in:
zhongwencool 2022-02-25 16:05:27 +08:00 committed by GitHub
commit 829bd8258a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 18 deletions

View File

@ -159,7 +159,7 @@ ensure_system_memory_alarm(HW) ->
case erlang:whereis(memsup) of
undefined -> ok;
_Pid ->
{Allocated, Total, _Worst} = memsup:get_memory_data(),
{Total, Allocated, _Worst} = memsup:get_memory_data(),
case Total =/= 0 andalso Allocated/Total * 100 > HW of
true -> emqx_alarm:activate(high_system_memory_usage, #{high_watermark => HW});
false -> ok

View File

@ -52,10 +52,8 @@
-import(emqx_misc, [start_timer/2]).
-record(state,
{ start_time :: erlang:timestamp()
, heartbeat :: maybe(reference())
{heartbeat :: maybe(reference())
, ticker :: maybe(reference())
, version :: binary()
, sysdescr :: binary()
}).
@ -91,7 +89,8 @@ sysdescr() -> emqx_app:get_description().
%% @doc Get sys uptime
-spec(uptime() -> Milliseconds :: integer()).
uptime() ->
gen_server:call(?SYS, uptime).
{TotalWallClock, _} = erlang:statistics(wall_clock),
TotalWallClock.
%% @doc Get sys datetime
-spec(datetime() -> string()).
@ -120,9 +119,7 @@ info() ->
%%------------------------------------------------------------------------------
init([]) ->
State = #state{start_time = erlang:timestamp(),
version = iolist_to_binary(version()),
sysdescr = iolist_to_binary(sysdescr())},
State = #state{sysdescr = iolist_to_binary(sysdescr())},
{ok, heartbeat(tick(State))}.
heartbeat(State) ->
@ -130,9 +127,6 @@ heartbeat(State) ->
tick(State) ->
State#state{ticker = start_timer(sys_interval(), tick)}.
handle_call(uptime, _From, State) ->
{reply, uptime(State), State};
handle_call(Req, _From, State) ->
?SLOG(error, #{msg => "unexpected_call", call => Req}),
{reply, ignored, State}.
@ -142,13 +136,12 @@ handle_cast(Msg, State) ->
{noreply, State}.
handle_info({timeout, TRef, heartbeat}, State = #state{heartbeat = TRef}) ->
publish_any(uptime, integer_to_binary(uptime(State))),
publish_any(uptime, integer_to_binary(uptime())),
publish_any(datetime, iolist_to_binary(datetime())),
{noreply, heartbeat(State)};
handle_info({timeout, TRef, tick},
State = #state{ticker = TRef, version = Version, sysdescr = Descr}) ->
publish_any(version, Version),
handle_info({timeout, TRef, tick}, State = #state{ticker = TRef, sysdescr = Descr}) ->
publish_any(version, version()),
publish_any(sysdescr, Descr),
publish_any(brokers, mria_mnesia:running_nodes()),
publish_any(stats, emqx_stats:getstats()),
@ -166,9 +159,6 @@ terminate(_Reason, #state{heartbeat = TRef1, ticker = TRef2}) ->
%% Internal functions
%%-----------------------------------------------------------------------------
uptime(#state{start_time = Ts}) ->
timer:now_diff(erlang:timestamp(), Ts) div 1000.
publish_any(Name, Value) ->
_ = publish(Name, Value),
ok.