Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 026250c

Browse files
committed
Handle edge case where gen_server is not running
1 parent 2172c03 commit 026250c

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

src/vm_memory_monitor.erl

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@
5151
timer,
5252
alarmed,
5353
alarm_funs,
54-
os_type,
55-
os_pid,
56-
page_size,
57-
proc_file}).
54+
os_type = undefined,
55+
os_pid = undefined,
56+
page_size = undefined,
57+
proc_file = undefined}).
5858

5959
-include("rabbit_memory.hrl").
6060

@@ -142,10 +142,13 @@ get_memory_use(ratio) ->
142142
%% for details.
143143
-spec get_process_memory() -> Bytes :: integer().
144144
get_process_memory() ->
145-
%% TODO LRB rabbitmq/rabbitmq-common#227
146-
%% This will cause a noproc unless this gen_server is started
147-
{ProcessMemory, _} = get_memory_use(bytes),
148-
ProcessMemory.
145+
try
146+
{ProcMem, _} = get_memory_use(bytes),
147+
ProcMem
148+
catch exit:{noproc, Error} ->
149+
rabbit_log:warning("Memory monitor process not yet started: ~p~n", [Error]),
150+
get_process_memory_uncached()
151+
end.
149152

150153
-spec get_memory_calculation_strategy() -> rss | erlang.
151154
get_memory_calculation_strategy() ->
@@ -178,25 +181,13 @@ start_link(MemFraction, AlarmSet, AlarmClear) ->
178181

179182
init([MemFraction, AlarmFuns]) ->
180183
TRef = erlang:send_after(?DEFAULT_MEMORY_CHECK_INTERVAL, self(), update),
181-
OsType = os:type(),
182-
OsPid = os:getpid(),
183184
State0 = #state{timeout = ?DEFAULT_MEMORY_CHECK_INTERVAL,
184185
timer = TRef,
185186
alarmed = false,
186-
alarm_funs = AlarmFuns,
187-
os_type = OsType,
188-
os_pid = OsPid},
187+
alarm_funs = AlarmFuns},
189188
State1 = init_state_by_os(State0),
190189
{ok, set_mem_limits(State1, MemFraction)}.
191190

192-
init_state_by_os(State0 = #state{os_type = {unix, linux}, os_pid = OsPid}) ->
193-
PageSize = get_linux_pagesize(),
194-
ProcFile = io_lib:format("/proc/~s/statm", [OsPid]),
195-
State1 = State0#state{page_size = PageSize, proc_file = ProcFile},
196-
update_process_memory(State1);
197-
init_state_by_os(State) ->
198-
update_process_memory(State).
199-
200191
handle_call(get_vm_memory_high_watermark, _From,
201192
#state{memory_config_limit = MemLimit} = State) ->
202193
{reply, MemLimit, State};
@@ -248,18 +239,31 @@ code_change(_OldVsn, State, _Extra) ->
248239
%% Server Internals
249240
%%----------------------------------------------------------------------------
250241

242+
get_process_memory_uncached() ->
243+
TmpState = init_state_by_os(#state{}),
244+
TmpState#state.process_memory.
245+
251246
update_process_memory(State) ->
252247
Strategy = get_memory_calculation_strategy(),
253248
{ok, ProcMem} = get_process_memory_using_strategy(Strategy, State),
254249
State#state{process_memory = ProcMem}.
255250

251+
init_state_by_os(State = #state{os_type = undefined}) ->
252+
OsType = os:type(),
253+
OsPid = os:getpid(),
254+
init_state_by_os(State#state{os_type = OsType, os_pid = OsPid});
255+
init_state_by_os(State0 = #state{os_type = {unix, linux}, os_pid = OsPid}) ->
256+
PageSize = get_linux_pagesize(),
257+
ProcFile = io_lib:format("/proc/~s/statm", [OsPid]),
258+
State1 = State0#state{page_size = PageSize, proc_file = ProcFile},
259+
update_process_memory(State1);
260+
init_state_by_os(State) ->
261+
update_process_memory(State).
262+
256263
get_process_memory_using_strategy(rss, #state{os_type = {unix, linux},
257264
page_size = PageSize,
258265
proc_file = ProcFile}) ->
259-
{ok, F} = file:open(ProcFile, [read, raw]),
260-
{ok, Data} = file:read(F, 1024), %% {ok,"1028083 6510 1698 990 0 21103 0\n"}
261-
eof = file:read(F, 1024),
262-
ok = file:close(F),
266+
Data = read_proc_file(ProcFile),
263267
[_|[RssPagesStr|_]] = string:split(Data, " ", all),
264268
ProcMem = list_to_integer(RssPagesStr) * PageSize,
265269
{ok, ProcMem};
@@ -275,11 +279,11 @@ get_process_memory_using_strategy(rss, #state{os_type = {unix, _},
275279
{error, {unexpected_output_from_command, Cmd, CmdOutput}}
276280
end;
277281
get_process_memory_using_strategy(rss, _State) ->
278-
recon_alloc:memory(allocated);
282+
{ok, recon_alloc:memory(allocated)};
279283
get_process_memory_using_strategy(allocated, _State) ->
280-
recon_alloc:memory(allocated);
284+
{ok, recon_alloc:memory(allocated)};
281285
get_process_memory_using_strategy(erlang, _State) ->
282-
erlang:memory(total).
286+
{ok, erlang:memory(total)}.
283287

284288
get_total_memory_from_os() ->
285289
try

0 commit comments

Comments
 (0)