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

Use recon_alloc:memory to get process RSS #225

Merged
merged 4 commits into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ define PROJECT_APP_EXTRA_KEYS
]}
endef

DEPS = recon
LOCAL_DEPS = compiler syntax_tools xmerl

# FIXME: Use erlang.mk patched for RabbitMQ, while waiting for PRs to be
Expand Down
97 changes: 27 additions & 70 deletions src/vm_memory_monitor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -130,30 +130,35 @@ get_memory_use(ratio) ->
%% be equal to the total size of all pages mapped to the emulator,
%% according to http://erlang.org/doc/man/erlang.html#memory-0
%% erlang:memory(total) under-reports memory usage by around 20%
%%
%% Win32 Note: 3.6.12 shipped with code that used wmic.exe to get the
%% WorkingSetSize value for the running erl.exe process. Unfortunately
%% even with a moderate invocation rate of 1 ops/second that uses more
%% CPU resources than some Windows users are willing to tolerate.
%% See rabbitmq/rabbitmq-server#1343 and rabbitmq/rabbitmq-common#224
%% for details.
-spec get_process_memory() -> Bytes :: integer().
get_process_memory() ->
case get_memory_calculation_strategy() of
rss ->
case get_system_process_resident_memory() of
{ok, MemInBytes} ->
MemInBytes;
{error, Reason} ->
rabbit_log:debug("Unable to get system memory used. Reason: ~p."
" Falling back to erlang memory reporting",
[Reason]),
erlang:memory(total)
end;
erlang ->
erlang:memory(total)
end.
get_process_memory_using_strategy(get_memory_calculation_strategy()).

get_process_memory_using_strategy(allocated) ->
recon_alloc:memory(allocated);
%% backwards compatibility
get_process_memory_using_strategy(erlang) ->
erlang:memory(total);
get_process_memory_using_strategy(legacy) ->
erlang:memory(total);
%% backwards compatibility
get_process_memory_using_strategy(rss) ->
recon_alloc:memory(allocated).

-spec get_memory_calculation_strategy() -> rss | erlang.
get_memory_calculation_strategy() ->
case rabbit_misc:get_env(rabbit, vm_memory_calculation_strategy, rss) of
erlang ->
erlang;
rss ->
rss;
allocated -> allocated;
erlang -> erlang;
legacy -> legacy;
rss -> rss;
UnsupportedValue ->
rabbit_log:warning(
"Unsupported value '~p' for vm_memory_calculation_strategy. "
Expand All @@ -164,54 +169,6 @@ get_memory_calculation_strategy() ->
rss
end.

-spec get_system_process_resident_memory() -> {ok, Bytes :: integer()} | {error, term()}.
get_system_process_resident_memory() ->
try
get_system_process_resident_memory(os:type())
catch _:Error ->
{error, {"Failed to get process resident memory", Error}}
end.

get_system_process_resident_memory({unix,darwin}) ->
get_ps_memory();

get_system_process_resident_memory({unix, linux}) ->
get_ps_memory();

get_system_process_resident_memory({unix,freebsd}) ->
get_ps_memory();

get_system_process_resident_memory({unix,openbsd}) ->
get_ps_memory();

get_system_process_resident_memory({win32,_OSname}) ->
%% Note: 3.6.12 shipped with code that used wmic.exe to get the
%% WorkingSetSize value for the running erl.exe process. Unfortunately
%% even with a moderate invocation rate of 1 ops/second that uses more
%% CPU resources than some Windows users are willing to tolerate.
%% See rabbitmq/rabbitmq-server#1343 for details.
{ok, erlang:memory(total)};

get_system_process_resident_memory({unix, sunos}) ->
get_ps_memory();

get_system_process_resident_memory({unix, aix}) ->
get_ps_memory();

get_system_process_resident_memory(_OsType) ->
{error, not_implemented_for_os}.

get_ps_memory() ->
OsPid = os:getpid(),
Cmd = "ps -p " ++ OsPid ++ " -o rss=",
CmdOutput = os:cmd(Cmd),
case re:run(CmdOutput, "[0-9]+", [{capture, first, list}]) of
{match, [Match]} ->
{ok, list_to_integer(Match) * 1024};
_ ->
{error, {unexpected_output_from_command, Cmd, CmdOutput}}
end.

%%----------------------------------------------------------------------------
%% gen_server callbacks
%%----------------------------------------------------------------------------
Expand Down Expand Up @@ -424,18 +381,18 @@ cmd(Command) ->
%% Windows and Freebsd code based on: memsup:get_memory_usage/1
%% Original code was part of OTP and released under "Erlang Public License".

get_total_memory({unix,darwin}) ->
get_total_memory({unix, darwin}) ->
sysctl("hw.memsize");

get_total_memory({unix,freebsd}) ->
get_total_memory({unix, freebsd}) ->
PageSize = sysctl("vm.stats.vm.v_page_size"),
PageCount = sysctl("vm.stats.vm.v_page_count"),
PageCount * PageSize;

get_total_memory({unix,openbsd}) ->
get_total_memory({unix, openbsd}) ->
sysctl("hw.usermem");

get_total_memory({win32,_OSname}) ->
get_total_memory({win32, _OSname}) ->
[Result|_] = os_mon_sysinfo:get_mem_info(),
{ok, [_MemLoad, TotPhys, _AvailPhys, _TotPage, _AvailPage, _TotV, _AvailV],
_RestStr} =
Expand Down