Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/emqx.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@
topic :: binary(),
%% Message Payload
payload :: binary(),
%% Timestamp
timestamp :: erlang:timestamp()
}).
%% Timestamp (Unit: millisecond)
timestamp :: integer()
}).

-record(delivery, {
sender :: pid(), %% Sender of the delivery
Expand Down
9 changes: 1 addition & 8 deletions src/emqx_guid.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ next(NPid, Seq) ->
bin({Ts, NPid, Seq}) ->
<<Ts:64, NPid:48, Seq:16>>.

ts() ->
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.
ts() -> erlang:system_time(micro_seconds).

%% Copied from https://github.com/okeuday/uuid.git.
npid() ->
Expand Down
10 changes: 6 additions & 4 deletions src/emqx_message.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ make(From, QoS, Topic, Payload) when ?QOS_0 =< QoS, QoS =< ?QOS_2 ->
headers = #{},
topic = Topic,
payload = Payload,
timestamp = os:timestamp()}.
timestamp = erlang:system_time(millisecond)
}.

-spec(id(emqx_types:message()) -> maybe(binary())).
id(#message{id = Id}) -> Id.
Expand All @@ -106,7 +107,7 @@ topic(#message{topic = Topic}) -> Topic.
-spec(payload(emqx_types:message()) -> emqx_types:payload()).
payload(#message{payload = Payload}) -> Payload.

-spec(timestamp(emqx_types:message()) -> erlang:timestamp()).
-spec(timestamp(emqx_types:message()) -> integer()).
timestamp(#message{timestamp = TS}) -> TS.

-spec(set_flags(map(), emqx_types:message()) -> emqx_types:message()).
Expand Down Expand Up @@ -240,7 +241,8 @@ to_map(#message{
headers => Headers,
topic => Topic,
payload => Payload,
timestamp => Timestamp}.
timestamp => Timestamp
}.

%% @doc Message to tuple list
-spec(to_list(emqx_types:message()) -> map()).
Expand All @@ -249,7 +251,7 @@ to_list(Msg) ->

%% MilliSeconds
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}) ->
io_lib:format("Message(Id=~s, QoS=~w, Topic=~s, From=~p, Flags=~s, Headers=~s)",
Expand Down
19 changes: 10 additions & 9 deletions src/emqx_session.erl
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,7 @@ return_with(Msg, {ok, Publishes, Session}) ->
pubrec(PacketId, Session = #session{inflight = Inflight}) ->
case emqx_inflight:lookup(PacketId, Inflight) of
{value, {Msg, _Ts}} when is_record(Msg, message) ->
Inflight1 = emqx_inflight:update(
PacketId, {pubrel, os:timestamp()}, Inflight),
Inflight1 = emqx_inflight:update(PacketId, with_ts(pubrel), Inflight),
{ok, Msg, Session#session{inflight = Inflight1}};
{value, {pubrel, _Ts}} ->
{error, ?RC_PACKET_IDENTIFIER_IN_USE};
Expand Down Expand Up @@ -510,7 +509,7 @@ enrich_subopts([{subid, SubId}|Opts], Msg, Session) ->
%%--------------------------------------------------------------------

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}.

%%--------------------------------------------------------------------
Expand All @@ -521,9 +520,8 @@ await(PacketId, Msg, Session = #session{inflight = Inflight}) ->
retry(Session = #session{inflight = Inflight}) ->
case emqx_inflight:is_empty(Inflight) of
true -> {ok, Session};
false ->
retry_delivery(emqx_inflight:to_list(sort_fun(), Inflight),
[], os:timestamp(), Session)
false -> retry_delivery(emqx_inflight:to_list(sort_fun(), Inflight),
[], erlang:system_time(millisecond), Session)
end.

retry_delivery([], Acc, _Now, Session = #session{retry_interval = Interval}) ->
Expand Down Expand Up @@ -561,7 +559,7 @@ retry_delivery(PacketId, pubrel, Now, Acc, Inflight) ->
expire(awaiting_rel, Session = #session{awaiting_rel = AwaitingRel}) ->
case maps:size(AwaitingRel) of
0 -> {ok, Session};
_ -> expire_awaiting_rel(os:timestamp(), Session)
_ -> expire_awaiting_rel(erlang:system_time(millisecond), Session)
end.

expire_awaiting_rel(Now, Session = #session{awaiting_rel = AwaitingRel,
Expand Down Expand Up @@ -623,7 +621,7 @@ next_pkt_id(Session = #session{next_pkt_id = Id}) ->
%% 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() ->
fun({_, {_, Ts1}}, {_, {_, Ts2}}) -> Ts1 < Ts2 end.
Expand All @@ -634,7 +632,10 @@ batch_n(Inflight) ->
Sz -> Sz - emqx_inflight:size(Inflight)
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
Expand Down
5 changes: 0 additions & 5 deletions src/emqx_vm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

-export([ schedulers/0
, scheduler_usage/1
, microsecs/0
, system_info_keys/0
, get_system_info/0
, get_system_info/1
Expand Down Expand Up @@ -171,10 +170,6 @@
schedulers() ->
erlang:system_info(schedulers).

microsecs() ->
{Mega, Sec, Micro} = os:timestamp(),
(Mega * 1000000 + Sec) * 1000000 + Micro.

loads() ->
[{load1, ftos(avg1()/256)},
{load5, ftos(avg5()/256)},
Expand Down
3 changes: 0 additions & 3 deletions test/emqx_vm_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,6 @@ t_scheduler_usage(_Config) ->
t_get_memory(_Config) ->
emqx_vm:get_memory().

t_microsecs(_Config) ->
emqx_vm:microsecs().

t_schedulers(_Config) ->
emqx_vm:schedulers().

Expand Down