Use 'erlang:system_time' to replace 'os:timestamp' (#3088)

Use 'erlang:system_time/1' to replace 'os:timestamp/0'
This commit is contained in:
Feng Lee 2019-12-09 16:27:25 +08:00 committed by tigercl
parent c7f1df0d0c
commit 0c377c67cd
6 changed files with 20 additions and 32 deletions

View File

@ -69,8 +69,8 @@
topic :: binary(), topic :: binary(),
%% Message Payload %% Message Payload
payload :: binary(), payload :: binary(),
%% Timestamp %% Timestamp (Unit: millisecond)
timestamp :: erlang:timestamp() timestamp :: integer()
}). }).
-record(delivery, { -record(delivery, {

View File

@ -67,14 +67,7 @@ next(NPid, Seq) ->
bin({Ts, NPid, Seq}) -> bin({Ts, NPid, Seq}) ->
<<Ts:64, NPid:48, Seq:16>>. <<Ts:64, NPid:48, Seq:16>>.
ts() -> ts() -> erlang:system_time(micro_seconds).
case erlang:function_exported(erlang, system_time, 1) of
true -> %% R18
erlang:system_time(micro_seconds);
false ->
{MegaSeconds, Seconds, MicroSeconds} = os:timestamp(),
(MegaSeconds * 1000000 + Seconds) * 1000000 + MicroSeconds
end.
%% Copied from https://github.com/okeuday/uuid.git. %% Copied from https://github.com/okeuday/uuid.git.
npid() -> npid() ->

View File

@ -89,7 +89,8 @@ make(From, QoS, Topic, Payload) when ?QOS_0 =< QoS, QoS =< ?QOS_2 ->
headers = #{}, headers = #{},
topic = Topic, topic = Topic,
payload = Payload, payload = Payload,
timestamp = os:timestamp()}. timestamp = erlang:system_time(millisecond)
}.
-spec(id(emqx_types:message()) -> maybe(binary())). -spec(id(emqx_types:message()) -> maybe(binary())).
id(#message{id = Id}) -> Id. id(#message{id = Id}) -> Id.
@ -106,7 +107,7 @@ topic(#message{topic = Topic}) -> Topic.
-spec(payload(emqx_types:message()) -> emqx_types:payload()). -spec(payload(emqx_types:message()) -> emqx_types:payload()).
payload(#message{payload = Payload}) -> Payload. payload(#message{payload = Payload}) -> Payload.
-spec(timestamp(emqx_types:message()) -> erlang:timestamp()). -spec(timestamp(emqx_types:message()) -> integer()).
timestamp(#message{timestamp = TS}) -> TS. timestamp(#message{timestamp = TS}) -> TS.
-spec(set_flags(map(), emqx_types:message()) -> emqx_types:message()). -spec(set_flags(map(), emqx_types:message()) -> emqx_types:message()).
@ -240,7 +241,8 @@ to_map(#message{
headers => Headers, headers => Headers,
topic => Topic, topic => Topic,
payload => Payload, payload => Payload,
timestamp => Timestamp}. timestamp => Timestamp
}.
%% @doc Message to tuple list %% @doc Message to tuple list
-spec(to_list(emqx_types:message()) -> map()). -spec(to_list(emqx_types:message()) -> map()).
@ -249,7 +251,7 @@ to_list(Msg) ->
%% MilliSeconds %% MilliSeconds
elapsed(Since) -> elapsed(Since) ->
max(0, timer:now_diff(os:timestamp(), Since) div 1000). max(0, erlang:system_time(millisecond) - Since).
format(#message{id = Id, qos = QoS, topic = Topic, from = From, flags = Flags, headers = Headers}) -> format(#message{id = Id, qos = QoS, topic = Topic, from = From, flags = Flags, headers = Headers}) ->
io_lib:format("Message(Id=~s, QoS=~w, Topic=~s, From=~p, Flags=~s, Headers=~s)", io_lib:format("Message(Id=~s, QoS=~w, Topic=~s, From=~p, Flags=~s, Headers=~s)",

View File

@ -340,8 +340,7 @@ return_with(Msg, {ok, Publishes, Session}) ->
pubrec(PacketId, Session = #session{inflight = Inflight}) -> pubrec(PacketId, Session = #session{inflight = Inflight}) ->
case emqx_inflight:lookup(PacketId, Inflight) of case emqx_inflight:lookup(PacketId, Inflight) of
{value, {Msg, _Ts}} when is_record(Msg, message) -> {value, {Msg, _Ts}} when is_record(Msg, message) ->
Inflight1 = emqx_inflight:update( Inflight1 = emqx_inflight:update(PacketId, with_ts(pubrel), Inflight),
PacketId, {pubrel, os:timestamp()}, Inflight),
{ok, Msg, Session#session{inflight = Inflight1}}; {ok, Msg, Session#session{inflight = Inflight1}};
{value, {pubrel, _Ts}} -> {value, {pubrel, _Ts}} ->
{error, ?RC_PACKET_IDENTIFIER_IN_USE}; {error, ?RC_PACKET_IDENTIFIER_IN_USE};
@ -510,7 +509,7 @@ enrich_subopts([{subid, SubId}|Opts], Msg, Session) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
await(PacketId, Msg, Session = #session{inflight = Inflight}) -> await(PacketId, Msg, Session = #session{inflight = Inflight}) ->
Inflight1 = emqx_inflight:insert(PacketId, {Msg, os:timestamp()}, Inflight), Inflight1 = emqx_inflight:insert(PacketId, with_ts(Msg), Inflight),
Session#session{inflight = Inflight1}. Session#session{inflight = Inflight1}.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -521,9 +520,8 @@ await(PacketId, Msg, Session = #session{inflight = Inflight}) ->
retry(Session = #session{inflight = Inflight}) -> retry(Session = #session{inflight = Inflight}) ->
case emqx_inflight:is_empty(Inflight) of case emqx_inflight:is_empty(Inflight) of
true -> {ok, Session}; true -> {ok, Session};
false -> false -> retry_delivery(emqx_inflight:to_list(sort_fun(), Inflight),
retry_delivery(emqx_inflight:to_list(sort_fun(), Inflight), [], erlang:system_time(millisecond), Session)
[], os:timestamp(), Session)
end. end.
retry_delivery([], Acc, _Now, Session = #session{retry_interval = Interval}) -> retry_delivery([], Acc, _Now, Session = #session{retry_interval = Interval}) ->
@ -561,7 +559,7 @@ retry_delivery(PacketId, pubrel, Now, Acc, Inflight) ->
expire(awaiting_rel, Session = #session{awaiting_rel = AwaitingRel}) -> expire(awaiting_rel, Session = #session{awaiting_rel = AwaitingRel}) ->
case maps:size(AwaitingRel) of case maps:size(AwaitingRel) of
0 -> {ok, Session}; 0 -> {ok, Session};
_ -> expire_awaiting_rel(os:timestamp(), Session) _ -> expire_awaiting_rel(erlang:system_time(millisecond), Session)
end. end.
expire_awaiting_rel(Now, Session = #session{awaiting_rel = AwaitingRel, expire_awaiting_rel(Now, Session = #session{awaiting_rel = AwaitingRel,
@ -623,7 +621,7 @@ next_pkt_id(Session = #session{next_pkt_id = Id}) ->
%% Helper functions %% Helper functions
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-compile({inline, [sort_fun/0, batch_n/1, age/2]}). -compile({inline, [sort_fun/0, batch_n/1, with_ts/1, age/2]}).
sort_fun() -> sort_fun() ->
fun({_, {_, Ts1}}, {_, {_, Ts2}}) -> Ts1 < Ts2 end. fun({_, {_, Ts1}}, {_, {_, Ts2}}) -> Ts1 < Ts2 end.
@ -634,7 +632,10 @@ batch_n(Inflight) ->
Sz -> Sz - emqx_inflight:size(Inflight) Sz -> Sz - emqx_inflight:size(Inflight)
end. end.
age(Now, Ts) -> timer:now_diff(Now, Ts) div 1000. with_ts(Msg) ->
{Msg, erlang:system_time(millisecond)}.
age(Now, Ts) -> Now - Ts.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% For CT tests %% For CT tests

View File

@ -18,7 +18,6 @@
-export([ schedulers/0 -export([ schedulers/0
, scheduler_usage/1 , scheduler_usage/1
, microsecs/0
, system_info_keys/0 , system_info_keys/0
, get_system_info/0 , get_system_info/0
, get_system_info/1 , get_system_info/1
@ -171,10 +170,6 @@
schedulers() -> schedulers() ->
erlang:system_info(schedulers). erlang:system_info(schedulers).
microsecs() ->
{Mega, Sec, Micro} = os:timestamp(),
(Mega * 1000000 + Sec) * 1000000 + Micro.
loads() -> loads() ->
[{load1, ftos(avg1()/256)}, [{load1, ftos(avg1()/256)},
{load5, ftos(avg5()/256)}, {load5, ftos(avg5()/256)},

View File

@ -95,9 +95,6 @@ t_scheduler_usage(_Config) ->
t_get_memory(_Config) -> t_get_memory(_Config) ->
emqx_vm:get_memory(). emqx_vm:get_memory().
t_microsecs(_Config) ->
emqx_vm:microsecs().
t_schedulers(_Config) -> t_schedulers(_Config) ->
emqx_vm:schedulers(). emqx_vm:schedulers().