Skip to content

Commit

Permalink
ControlHub : Fix compilation errors and deprecation warnings in Erlan…
Browse files Browse the repository at this point in the history
…g R19; refs #65
  • Loading branch information
tswilliams committed Nov 10, 2017
1 parent da4e508 commit cac5b10
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion controlhub/ebin/controlhub.app
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion controlhub/rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -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}
]}.
27 changes: 20 additions & 7 deletions controlhub/src/ch_device_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
%%% ====================================================================
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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,
Expand All @@ -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} ->
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion controlhub/src/ch_transaction_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cac5b10

Please sign in to comment.