From cac5b108a7121a3bd450a53fc2050e46ddd4fde0 Mon Sep 17 00:00:00 2001 From: Tom Williams Date: Thu, 9 Nov 2017 23:44:16 +0000 Subject: [PATCH] ControlHub : Fix compilation errors and deprecation warnings in Erlang R19; refs #65 --- controlhub/ebin/controlhub.app | 2 +- controlhub/rebar.config | 6 ++++- controlhub/src/ch_device_client.erl | 27 +++++++++++++++++------ controlhub/src/ch_transaction_manager.erl | 2 +- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/controlhub/ebin/controlhub.app b/controlhub/ebin/controlhub.app index f8d23087f..7a589ac25 100644 --- a/controlhub/ebin/controlhub.app +++ b/controlhub/ebin/controlhub.app @@ -25,7 +25,7 @@ ch_sup, ch_tcp_listener]}, {applications, [stdlib, kernel, sasl, - appmon, + observer, % For lager syslog backend syslog, lager_syslog, % For lager, which depends on goldrush diff --git a/controlhub/rebar.config b/controlhub/rebar.config index c58843a98..d0da90641 100644 --- a/controlhub/rebar.config +++ b/controlhub/rebar.config @@ -19,4 +19,8 @@ %%%%%%%%%%%%%%%%%% % COMPILER FLAGS -{erl_opts, [{parse_transform, lager_transform}]}. +{erl_opts, [{parse_transform, lager_transform}, + % Use new time API from Erlang version 18 + % (Erlang releases from 17 onwards do not put R in front of name + {platform_define, "^(R|17).*", old_erlang_time_api} + ]}. diff --git a/controlhub/src/ch_device_client.erl b/controlhub/src/ch_device_client.erl index 80c21d03c..2632ccc2c 100644 --- a/controlhub/src/ch_device_client.erl +++ b/controlhub/src/ch_device_client.erl @@ -61,6 +61,19 @@ ). +-ifdef(old_erlang_time_api). + +-define(GET_MONOTONIC_TIME, erlang:now() ). +-define(CALC_MONOTONIC_TIME_DIFF(T2,T1), timer:now_diff(T2,T1) ). + +-else. + +-define(GET_MONOTONIC_TIME, erlang:monotonic_time(microsecond) ). +-define(CALC_MONOTONIC_TIME_DIFF(T2,T1), (T2 - T1) ). + +-endif. + + %%% ==================================================================== %%% API functions (public interface) %%% ==================================================================== @@ -283,11 +296,11 @@ device_client_loop(timeout, S) -> device_client_loop(send, NewState); {error, NewState, MsgForTransactionManager} -> S#state.udp_pid ! {print_incident_packets_then_exit}, - Now = erlang:now(), + Now = ?GET_MONOTONIC_TIME, {LostPktId, NrInFlight, RecoveryInfoList} = NewState#state.last_timeout, ch_utils:log({error,log_prefix(S)}, "Irrecoverable timeout for control packet ID ~w, with ~w in flight. ~w recovery attempts, timeout ~wms~s", [LostPktId, NrInFlight, length(RecoveryInfoList), S#state.timeout, - lists:flatten([io_lib:format("; ~.1fms ago, ~w lost", [timer:now_diff(Now,Timestamp) / 1000, Type]) || {Timestamp, Type, _} <- RecoveryInfoList])]), + lists:flatten([io_lib:format("; ~.1fms ago, ~w lost", [?CALC_MONOTONIC_TIME_DIFF(Now,Timestamp) / 1000, Type]) || {Timestamp, Type, _} <- RecoveryInfoList])]), ReqPkt = case S#state.ipbus_v of {1, 3} -> element(2, element(2, S#state.in_flight)); @@ -364,7 +377,7 @@ forward_reply(Pkt, S) -> "No previous timeouts."; {LostPktId, NrInFlight, [ {Timestamp, Type, _StatusReplyPkt} | AttemptInfoTail]} -> io_lib:format("Last timeout was ~.1fms ago, ~w packet ID ~w lost, ~w in flight, ~w recovery attempts.", - [timer:now_diff(erlang:now(),Timestamp) / 1000, Type, LostPktId, NrInFlight, length(AttemptInfoTail)+1]); + [?CALC_MONOTONIC_TIME_DIFF(?GET_MONOTONIC_TIME,Timestamp) / 1000, Type, LostPktId, NrInFlight, length(AttemptInfoTail)+1]); {LostPktId, NrInFlight, []} -> io_lib:format("Last timeout was for packet ID ~w, ~w in flight.", [LostPktId, NrInFlight]) end, @@ -376,10 +389,10 @@ recover_from_timeout(NrFailedAttempts, S = #state{socket=Socket, ip_tuple=IP, po ch_utils:log({debug,log_prefix(S)}, "IPbus 1.3 target, so wait an extra ~w ms for reply packet to come.", [S#state.timeout]), NewS = case NrFailedAttempts of 0 -> - S#state{last_timeout={0, 1, [{erlang:now(), unknown, <<>>}]}}; + S#state{last_timeout={0, 1, [{?GET_MONOTONIC_TIME, unknown, <<>>}]}}; _ -> {_, _, AttemptInfoList} = S#state.last_timeout, - S#state{last_timeout={0, 1, [{erlang:now(), unknown, <<>>} | AttemptInfoList]}} + S#state{last_timeout={0, 1, [{?GET_MONOTONIC_TIME, unknown, <<>>} | AttemptInfoList]}} end, receive {udp, Socket, IP, Port, Pkt} -> @@ -423,10 +436,10 @@ recover_from_timeout(NrFailedAttempts, S = #state{next_id=NextId, in_flight={NrI end, NewS = case NrFailedAttempts of 0 -> - S#state{last_timeout={NextIdMinusN, NrInFlight, [{erlang:now(), TypeLost, StatusReplyPkt}]}}; + S#state{last_timeout={NextIdMinusN, NrInFlight, [{?GET_MONOTONIC_TIME, TypeLost, StatusReplyPkt}]}}; _ -> {_, _, AttemptInfoList} = S#state.last_timeout, - S#state{last_timeout={NextIdMinusN, NrInFlight, [{erlang:now(), TypeLost, StatusReplyPkt} | AttemptInfoList]}} + S#state{last_timeout={NextIdMinusN, NrInFlight, [{?GET_MONOTONIC_TIME, TypeLost, StatusReplyPkt} | AttemptInfoList]}} end, % Check whether recovered from timeout or not {value, {_, _, ExpdHdr, _}} = queue:peek(InFlightQ), diff --git a/controlhub/src/ch_transaction_manager.erl b/controlhub/src/ch_transaction_manager.erl index ff4d13c41..f02c228fd 100644 --- a/controlhub/src/ch_transaction_manager.erl +++ b/controlhub/src/ch_transaction_manager.erl @@ -29,7 +29,7 @@ target_ip_u32 :: non_neg_integer(), target_port :: non_neg_integer(), nr_in_flight :: non_neg_integer(), - q_nr_reqs_per_tcp :: queue(), + q_nr_reqs_per_tcp, % :: queue(), reply_io_list :: list(), nr_replies_acc :: non_neg_integer(), stats_table