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

Commit 2b59b5f

Browse files
committed
Handle edge case where gen_server is not running
1 parent 723962c commit 2b59b5f

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

src/vm_memory_monitor.erl

Lines changed: 28 additions & 25 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,12 @@ 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+
get_memory_use(bytes)
147+
catch exit:{noproc, Error} ->
148+
rabbit_log:warning("Memory monitor process not yet started: ~p~n", [Error]),
149+
get_process_memory_uncached()
150+
end.
149151

150152
-spec get_memory_calculation_strategy() -> rss | erlang.
151153
get_memory_calculation_strategy() ->
@@ -178,25 +180,13 @@ start_link(MemFraction, AlarmSet, AlarmClear) ->
178180

179181
init([MemFraction, AlarmFuns]) ->
180182
TRef = erlang:send_after(?DEFAULT_MEMORY_CHECK_INTERVAL, self(), update),
181-
OsType = os:type(),
182-
OsPid = os:getpid(),
183183
State0 = #state{timeout = ?DEFAULT_MEMORY_CHECK_INTERVAL,
184184
timer = TRef,
185185
alarmed = false,
186-
alarm_funs = AlarmFuns,
187-
os_type = OsType,
188-
os_pid = OsPid},
186+
alarm_funs = AlarmFuns},
189187
State1 = init_state_by_os(State0),
190188
{ok, set_mem_limits(State1, MemFraction)}.
191189

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-
200190
handle_call(get_vm_memory_high_watermark, _From,
201191
#state{memory_config_limit = MemLimit} = State) ->
202192
{reply, MemLimit, State};
@@ -248,18 +238,31 @@ code_change(_OldVsn, State, _Extra) ->
248238
%% Server Internals
249239
%%----------------------------------------------------------------------------
250240

241+
get_process_memory_uncached() ->
242+
TmpState = init_state_by_os(#state{}),
243+
TmpState#state.process_memory.
244+
251245
update_process_memory(State) ->
252246
Strategy = get_memory_calculation_strategy(),
253247
{ok, ProcMem} = get_process_memory_using_strategy(Strategy, State),
254248
State#state{process_memory = ProcMem}.
255249

250+
init_state_by_os(State = #state{os_type = undefined}) ->
251+
OsType = os:type(),
252+
OsPid = os:getpid(),
253+
init_state_by_os(State#state{os_type = OsType, os_pid = OsPid});
254+
init_state_by_os(State0 = #state{os_type = {unix, linux}, os_pid = OsPid}) ->
255+
PageSize = get_linux_pagesize(),
256+
ProcFile = io_lib:format("/proc/~s/statm", [OsPid]),
257+
State1 = State0#state{page_size = PageSize, proc_file = ProcFile},
258+
update_process_memory(State1);
259+
init_state_by_os(State) ->
260+
update_process_memory(State).
261+
256262
get_process_memory_using_strategy(rss, #state{os_type = {unix, linux},
257263
page_size = PageSize,
258264
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),
265+
Data = read_proc_file(ProcFile),
263266
[_|[RssPagesStr|_]] = string:split(Data, " ", all),
264267
ProcMem = list_to_integer(RssPagesStr) * PageSize,
265268
{ok, ProcMem};

0 commit comments

Comments
 (0)