/
brick_itimer.erl
259 lines (229 loc) · 8.98 KB
/
brick_itimer.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
%%%-------------------------------------------------------------------
%%% Copyright (c) 2009-2015 Hibari developers. All rights reserved.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%
%%% File : brick_itimer.erl
%%% Purpose : Interval timer
%%%-------------------------------------------------------------------
%% @doc To replace the use of timer:send_interval().
%%
%% The OTP timer server doesn't scale to many hundreds of timers in the
%% send_interval style, which GDSS uses extensively.
%%
%% Using 1/2 the tables of the NTT-R app, with 54 chains of length 3 each,
%% the timer servier is an obvious bottleneck, roughly 1.5 orders of magnitude
%% more reductions than the next process.
%%
-module(brick_itimer).
-behaviour(gen_server).
-include("brick.hrl").
%% API
-export([start_link/0, send_interval/2, send_interval/3, cancel/1,
dump/0]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
%% Internal export
-export([start_interval_loop/1, interval_loop/1]).
-ifdef(namespaced_dict_and_queue).
-type hibari_dict() :: dict:dict().
-else.
-type hibari_dict() :: dict().
-endif.
-define(SERVER, ?MODULE).
-record(state, {
interval_d=dict:new() :: hibari_dict() % key: interval size, val: pid
}).
%%%===================================================================
%%% API
%%%===================================================================
-spec start_link() -> ignore | {error, term()} | {ok,pid()}.
-spec send_interval(integer(),term()) -> any().
-spec send_interval(integer(),pid(),term()) -> any().
-spec cancel(term()) -> any().
%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
send_interval(Interval, Msg) ->
send_interval(Interval, self(), Msg).
send_interval(Interval, Pid, Msg) ->
gen_server:call(?SERVER, {send_interval, Interval, Pid, Msg}).
cancel(MRef) ->
gen_server:call(?SERVER, {cancel, MRef}).
dump() ->
gen_server:cast(?SERVER, dump).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initiates the server
%%
%% @spec init(Args) -> {ok, State} |
%% {ok, State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
init([]) ->
{ok, #state{}}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @spec handle_call(Request, From, State) ->
%% {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_call({send_interval, Interval, Pid, Msg}, From, State) ->
NewState = do_send_interval(From, Interval, Pid, Msg, State),
{noreply, NewState};
handle_call({cancel, MRef}, _From, State) ->
Parent = self(),
Pids = [begin Pid ! {cancel, MRef, Parent}, Pid end ||
{_Interval, Pid} <- dict:to_list(State#state.interval_d)],
Xs = [X || Pid <- Pids, X <- [receive {cancel_reply, Pid, Res} -> Res end],
X /= sorry],
case Xs of
[_] ->
{reply, ok, State};
_Hrm ->
{reply, error, State}
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_cast(dump, State) ->
_ = [begin
?E_INFO("~s: dump: interval ~p, pid ~p\n", [?MODULE, Interval, Pid]),
Pid ! dump
end || {Interval, Pid} <- dict:to_list(State#state.interval_d)],
{noreply, State};
handle_cast(_Msg, State) ->
?E_ERROR("~s: handle_cast: ~P\n", [?MODULE, _Msg, 20]),
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
?E_ERROR("~s: handle_info: ~P\n", [?MODULE, _Info, 20]),
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, State) ->
_ = [Pid ! stop || {_I, Pid} <- dict:to_list(State#state.interval_d)],
ok.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
-spec do_send_interval(pid(), integer(), pid(), term(), #state{}) -> #state{}.
do_send_interval(From, Interval, Pid, Msg, #state{interval_d=IntD}=S) ->
case dict:find(Interval, IntD) of
{ok, Server} ->
Server ! {send_interval, From, Pid, Msg},
S;
error ->
Server = start_interval_server(Interval),
NewIntD = dict:store(Interval, Server, IntD),
do_send_interval(From, Interval, Pid, Msg, S#state{interval_d=NewIntD})
end.
-spec start_interval_server(integer()) -> pid().
start_interval_server(Interval) ->
spawn_link(?MODULE, start_interval_loop, [Interval]).
-spec start_interval_loop(integer()) -> no_return().
start_interval_loop(Interval) ->
put(my_interval, Interval),
{ok, _} = timer:send_interval(Interval, tick),
interval_loop([]).
-spec interval_loop([{pid(), term(), term()}]) -> no_return().
interval_loop(Clients) ->
receive
tick ->
_ = [catch (Pid ! Msg) || {Pid, Msg, _MRef} <- Clients],
?MODULE:interval_loop(Clients);
{send_interval, From, Pid, Msg} ->
case gmt_util:make_monitor(Pid) of
{ok, MRef} = Reply ->
gen_server:reply(From, Reply),
?MODULE:interval_loop([{Pid, Msg, MRef}|Clients]);
Err ->
gen_server:reply(From, Err),
?MODULE:interval_loop(Clients)
end;
{cancel, MRef, Parent} ->
case lists:keytake(MRef, 3, Clients) of
{value, _, NewClients} ->
Parent ! {cancel_reply, self(), ok},
?MODULE:interval_loop(NewClients);
false ->
Parent ! {cancel_reply, self(), sorry},
?MODULE:interval_loop(Clients)
end;
{'DOWN', MRef, _Type, _Pid, _Info} ->
NewClients = lists:keydelete(MRef, 3, Clients),
?MODULE:interval_loop(NewClients);
dump ->
UMsgs = lists:usort([Msg || {_Pid, Msg, _MRef} <- Clients]),
?E_INFO("~s: pid ~p, clients ~p, types ~p\n",
[?MODULE, self(), length(Clients), UMsgs]),
?MODULE:interval_loop(Clients);
stop ->
exit(normal)
end.