Просмотр исходного кода

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

Use 'erlang:system_time/1' to replace 'os:timestamp/0'
Feng Lee 6 лет назад
Родитель
Сommit
0c377c67cd
6 измененных файлов с 20 добавлено и 32 удалено
  1. 3 3
      include/emqx.hrl
  2. 1 8
      src/emqx_guid.erl
  3. 6 4
      src/emqx_message.erl
  4. 10 9
      src/emqx_session.erl
  5. 0 5
      src/emqx_vm.erl
  6. 0 3
      test/emqx_vm_SUITE.erl

+ 3 - 3
include/emqx.hrl

@@ -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

+ 1 - 8
src/emqx_guid.erl

@@ -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() ->

+ 6 - 4
src/emqx_message.erl

@@ -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.
@@ -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()).
@@ -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()).
@@ -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)",

+ 10 - 9
src/emqx_session.erl

@@ -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};
@@ -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}.
 
 %%--------------------------------------------------------------------
@@ -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}) ->
@@ -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,
@@ -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.
@@ -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

+ 0 - 5
src/emqx_vm.erl

@@ -18,7 +18,6 @@
 
 -export([ schedulers/0
         , scheduler_usage/1
-        , microsecs/0
         , system_info_keys/0
         , get_system_info/0
         , get_system_info/1
@@ -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)},

+ 0 - 3
test/emqx_vm_SUITE.erl

@@ -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().