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,13 @@ 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
+ {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 .
149
152
150
153
-spec get_memory_calculation_strategy () -> rss | erlang .
151
154
get_memory_calculation_strategy () ->
@@ -178,25 +181,13 @@ start_link(MemFraction, AlarmSet, AlarmClear) ->
178
181
179
182
init ([MemFraction , AlarmFuns ]) ->
180
183
TRef = erlang :send_after (? DEFAULT_MEMORY_CHECK_INTERVAL , self (), update ),
181
- OsType = os :type (),
182
- OsPid = os :getpid (),
183
184
State0 = # state {timeout = ? DEFAULT_MEMORY_CHECK_INTERVAL ,
184
185
timer = TRef ,
185
186
alarmed = false ,
186
- alarm_funs = AlarmFuns ,
187
- os_type = OsType ,
188
- os_pid = OsPid },
187
+ alarm_funs = AlarmFuns },
189
188
State1 = init_state_by_os (State0 ),
190
189
{ok , set_mem_limits (State1 , MemFraction )}.
191
190
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
191
handle_call (get_vm_memory_high_watermark , _From ,
201
192
# state {memory_config_limit = MemLimit } = State ) ->
202
193
{reply , MemLimit , State };
@@ -248,18 +239,31 @@ code_change(_OldVsn, State, _Extra) ->
248
239
% % Server Internals
249
240
% %----------------------------------------------------------------------------
250
241
242
+ get_process_memory_uncached () ->
243
+ TmpState = init_state_by_os (# state {}),
244
+ TmpState # state .process_memory .
245
+
251
246
update_process_memory (State ) ->
252
247
Strategy = get_memory_calculation_strategy (),
253
248
{ok , ProcMem } = get_process_memory_using_strategy (Strategy , State ),
254
249
State # state {process_memory = ProcMem }.
255
250
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
+
256
263
get_process_memory_using_strategy (rss , # state {os_type = {unix , linux },
257
264
page_size = PageSize ,
258
265
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 ),
263
267
[_ |[RssPagesStr |_ ]] = string :split (Data , " " , all ),
264
268
ProcMem = list_to_integer (RssPagesStr ) * PageSize ,
265
269
{ok , ProcMem };
@@ -275,11 +279,11 @@ get_process_memory_using_strategy(rss, #state{os_type = {unix, _},
275
279
{error , {unexpected_output_from_command , Cmd , CmdOutput }}
276
280
end ;
277
281
get_process_memory_using_strategy (rss , _State ) ->
278
- recon_alloc :memory (allocated );
282
+ { ok , recon_alloc :memory (allocated )} ;
279
283
get_process_memory_using_strategy (allocated , _State ) ->
280
- recon_alloc :memory (allocated );
284
+ { ok , recon_alloc :memory (allocated )} ;
281
285
get_process_memory_using_strategy (erlang , _State ) ->
282
- erlang :memory (total ).
286
+ { ok , erlang :memory (total )} .
283
287
284
288
get_total_memory_from_os () ->
285
289
try
0 commit comments