|
51 | 51 | timer,
|
52 | 52 | alarmed,
|
53 | 53 | 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}). |
58 | 58 |
|
59 | 59 | -include("rabbit_memory.hrl").
|
60 | 60 |
|
@@ -142,10 +142,12 @@ get_memory_use(ratio) ->
|
142 | 142 | %% for details.
|
143 | 143 | -spec get_process_memory() -> Bytes :: integer().
|
144 | 144 | 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. |
149 | 151 |
|
150 | 152 | -spec get_memory_calculation_strategy() -> rss | erlang.
|
151 | 153 | get_memory_calculation_strategy() ->
|
@@ -178,25 +180,13 @@ start_link(MemFraction, AlarmSet, AlarmClear) ->
|
178 | 180 |
|
179 | 181 | init([MemFraction, AlarmFuns]) ->
|
180 | 182 | TRef = erlang:send_after(?DEFAULT_MEMORY_CHECK_INTERVAL, self(), update),
|
181 |
| - OsType = os:type(), |
182 |
| - OsPid = os:getpid(), |
183 | 183 | State0 = #state{timeout = ?DEFAULT_MEMORY_CHECK_INTERVAL,
|
184 | 184 | timer = TRef,
|
185 | 185 | alarmed = false,
|
186 |
| - alarm_funs = AlarmFuns, |
187 |
| - os_type = OsType, |
188 |
| - os_pid = OsPid}, |
| 186 | + alarm_funs = AlarmFuns}, |
189 | 187 | State1 = init_state_by_os(State0),
|
190 | 188 | {ok, set_mem_limits(State1, MemFraction)}.
|
191 | 189 |
|
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 |
| - |
200 | 190 | handle_call(get_vm_memory_high_watermark, _From,
|
201 | 191 | #state{memory_config_limit = MemLimit} = State) ->
|
202 | 192 | {reply, MemLimit, State};
|
@@ -248,18 +238,31 @@ code_change(_OldVsn, State, _Extra) ->
|
248 | 238 | %% Server Internals
|
249 | 239 | %%----------------------------------------------------------------------------
|
250 | 240 |
|
| 241 | +get_process_memory_uncached() -> |
| 242 | + TmpState = init_state_by_os(#state{}), |
| 243 | + TmpState#state.process_memory. |
| 244 | + |
251 | 245 | update_process_memory(State) ->
|
252 | 246 | Strategy = get_memory_calculation_strategy(),
|
253 | 247 | {ok, ProcMem} = get_process_memory_using_strategy(Strategy, State),
|
254 | 248 | State#state{process_memory = ProcMem}.
|
255 | 249 |
|
| 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 | + |
256 | 262 | get_process_memory_using_strategy(rss, #state{os_type = {unix, linux},
|
257 | 263 | page_size = PageSize,
|
258 | 264 | 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), |
263 | 266 | [_|[RssPagesStr|_]] = string:split(Data, " ", all),
|
264 | 267 | ProcMem = list_to_integer(RssPagesStr) * PageSize,
|
265 | 268 | {ok, ProcMem};
|
|
0 commit comments