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

Commit 1882d89

Browse files
committed
Add some additional calculation strategy options
1 parent 4b8a2fa commit 1882d89

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

src/vm_memory_monitor.erl

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,33 @@ get_memory_use(ratio) ->
130130
%% be equal to the total size of all pages mapped to the emulator,
131131
%% according to http://erlang.org/doc/man/erlang.html#memory-0
132132
%% erlang:memory(total) under-reports memory usage by around 20%
133+
%%
134+
%% Win32 Note: 3.6.12 shipped with code that used wmic.exe to get the
135+
%% WorkingSetSize value for the running erl.exe process. Unfortunately
136+
%% even with a moderate invocation rate of 1 ops/second that uses more
137+
%% CPU resources than some Windows users are willing to tolerate.
138+
%% See rabbitmq/rabbitmq-server#1343 and rabbitmq/rabbitmq-common#224
139+
%% for details.
133140
-spec get_process_memory() -> Bytes :: integer().
134141
get_process_memory() ->
135-
case get_memory_calculation_strategy() of
136-
rss ->
137-
case get_system_process_resident_memory() of
138-
{ok, MemInBytes} ->
139-
MemInBytes;
140-
{error, Reason} ->
141-
rabbit_log:debug("Unable to get system memory used. Reason: ~p."
142-
" Falling back to erlang memory reporting",
143-
[Reason]),
144-
erlang:memory(total)
145-
end;
146-
erlang ->
147-
erlang:memory(total)
148-
end.
142+
get_process_memory_using_strategy(get_memory_calculation_strategy()).
143+
144+
get_process_memory_using_strategy(allocated) ->
145+
recon_alloc:memory(allocated);
146+
get_process_memory_using_strategy(erlang) ->
147+
erlang:memory(total);
148+
get_process_memory_using_strategy(legacy) ->
149+
erlang:memory(total);
150+
get_process_memory_using_strategy(rss) ->
151+
recon_alloc:memory(allocated).
149152

150153
-spec get_memory_calculation_strategy() -> rss | erlang.
151154
get_memory_calculation_strategy() ->
152155
case rabbit_misc:get_env(rabbit, vm_memory_calculation_strategy, rss) of
153-
erlang ->
154-
erlang;
155-
rss ->
156-
rss;
156+
allocated -> allocated;
157+
erlang -> erlang;
158+
legacy -> legacy;
159+
rss -> rss;
157160
UnsupportedValue ->
158161
rabbit_log:warning(
159162
"Unsupported value '~p' for vm_memory_calculation_strategy. "
@@ -164,20 +167,6 @@ get_memory_calculation_strategy() ->
164167
rss
165168
end.
166169

167-
%% Win32 Note: 3.6.12 shipped with code that used wmic.exe to get the
168-
%% WorkingSetSize value for the running erl.exe process. Unfortunately
169-
%% even with a moderate invocation rate of 1 ops/second that uses more
170-
%% CPU resources than some Windows users are willing to tolerate.
171-
%% See rabbitmq/rabbitmq-server#1343 and rabbitmq/rabbitmq-common#224
172-
%% for details.
173-
-spec get_system_process_resident_memory() -> {ok, Bytes :: integer()} | {error, term()}.
174-
get_system_process_resident_memory() ->
175-
try
176-
{ok, recon_alloc:memory(allocated)}
177-
catch _:Error ->
178-
{error, {"Failed to get process resident memory", Error}}
179-
end.
180-
181170
%%----------------------------------------------------------------------------
182171
%% gen_server callbacks
183172
%%----------------------------------------------------------------------------
@@ -390,18 +379,18 @@ cmd(Command) ->
390379
%% Windows and Freebsd code based on: memsup:get_memory_usage/1
391380
%% Original code was part of OTP and released under "Erlang Public License".
392381

393-
get_total_memory({unix,darwin}) ->
382+
get_total_memory({unix, darwin}) ->
394383
sysctl("hw.memsize");
395384

396-
get_total_memory({unix,freebsd}) ->
385+
get_total_memory({unix, freebsd}) ->
397386
PageSize = sysctl("vm.stats.vm.v_page_size"),
398387
PageCount = sysctl("vm.stats.vm.v_page_count"),
399388
PageCount * PageSize;
400389

401-
get_total_memory({unix,openbsd}) ->
390+
get_total_memory({unix, openbsd}) ->
402391
sysctl("hw.usermem");
403392

404-
get_total_memory({win32,_OSname}) ->
393+
get_total_memory({win32, _OSname}) ->
405394
[Result|_] = os_mon_sysinfo:get_mem_info(),
406395
{ok, [_MemLoad, TotPhys, _AvailPhys, _TotPage, _AvailPage, _TotV, _AvailV],
407396
_RestStr} =

0 commit comments

Comments
 (0)