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

Commit 9a58adf

Browse files
Revert "Merge branch 'rabbitmq-common-224' into stable"
This reverts commit eb3ed71, reversing changes made to 50fa0ef. This needs more work on the server end: recon must be started by the time vm_memory_monitor runs.
1 parent eb3ed71 commit 9a58adf

File tree

2 files changed

+70
-28
lines changed

2 files changed

+70
-28
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ define PROJECT_APP_EXTRA_KEYS
1919
]}
2020
endef
2121

22-
DEPS = recon
2322
LOCAL_DEPS = compiler syntax_tools xmerl
2423

2524
# FIXME: Use erlang.mk patched for RabbitMQ, while waiting for PRs to be

src/vm_memory_monitor.erl

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -130,35 +130,30 @@ 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.
140133
-spec get_process_memory() -> Bytes :: integer().
141134
get_process_memory() ->
142-
get_process_memory_using_strategy(get_memory_calculation_strategy()).
143-
144-
get_process_memory_using_strategy(allocated) ->
145-
recon_alloc:memory(allocated);
146-
%% backwards compatibility
147-
get_process_memory_using_strategy(erlang) ->
148-
erlang:memory(total);
149-
get_process_memory_using_strategy(legacy) ->
150-
erlang:memory(total);
151-
%% backwards compatibility
152-
get_process_memory_using_strategy(rss) ->
153-
recon_alloc:memory(allocated).
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.
154149

155150
-spec get_memory_calculation_strategy() -> rss | erlang.
156151
get_memory_calculation_strategy() ->
157152
case rabbit_misc:get_env(rabbit, vm_memory_calculation_strategy, rss) of
158-
allocated -> allocated;
159-
erlang -> erlang;
160-
legacy -> legacy;
161-
rss -> rss;
153+
erlang ->
154+
erlang;
155+
rss ->
156+
rss;
162157
UnsupportedValue ->
163158
rabbit_log:warning(
164159
"Unsupported value '~p' for vm_memory_calculation_strategy. "
@@ -169,6 +164,54 @@ get_memory_calculation_strategy() ->
169164
rss
170165
end.
171166

167+
-spec get_system_process_resident_memory() -> {ok, Bytes :: integer()} | {error, term()}.
168+
get_system_process_resident_memory() ->
169+
try
170+
get_system_process_resident_memory(os:type())
171+
catch _:Error ->
172+
{error, {"Failed to get process resident memory", Error}}
173+
end.
174+
175+
get_system_process_resident_memory({unix,darwin}) ->
176+
get_ps_memory();
177+
178+
get_system_process_resident_memory({unix, linux}) ->
179+
get_ps_memory();
180+
181+
get_system_process_resident_memory({unix,freebsd}) ->
182+
get_ps_memory();
183+
184+
get_system_process_resident_memory({unix,openbsd}) ->
185+
get_ps_memory();
186+
187+
get_system_process_resident_memory({win32,_OSname}) ->
188+
%% Note: 3.6.12 shipped with code that used wmic.exe to get the
189+
%% WorkingSetSize value for the running erl.exe process. Unfortunately
190+
%% even with a moderate invocation rate of 1 ops/second that uses more
191+
%% CPU resources than some Windows users are willing to tolerate.
192+
%% See rabbitmq/rabbitmq-server#1343 for details.
193+
{ok, erlang:memory(total)};
194+
195+
get_system_process_resident_memory({unix, sunos}) ->
196+
get_ps_memory();
197+
198+
get_system_process_resident_memory({unix, aix}) ->
199+
get_ps_memory();
200+
201+
get_system_process_resident_memory(_OsType) ->
202+
{error, not_implemented_for_os}.
203+
204+
get_ps_memory() ->
205+
OsPid = os:getpid(),
206+
Cmd = "ps -p " ++ OsPid ++ " -o rss=",
207+
CmdOutput = os:cmd(Cmd),
208+
case re:run(CmdOutput, "[0-9]+", [{capture, first, list}]) of
209+
{match, [Match]} ->
210+
{ok, list_to_integer(Match) * 1024};
211+
_ ->
212+
{error, {unexpected_output_from_command, Cmd, CmdOutput}}
213+
end.
214+
172215
%%----------------------------------------------------------------------------
173216
%% gen_server callbacks
174217
%%----------------------------------------------------------------------------
@@ -381,18 +424,18 @@ cmd(Command) ->
381424
%% Windows and Freebsd code based on: memsup:get_memory_usage/1
382425
%% Original code was part of OTP and released under "Erlang Public License".
383426

384-
get_total_memory({unix, darwin}) ->
427+
get_total_memory({unix,darwin}) ->
385428
sysctl("hw.memsize");
386429

387-
get_total_memory({unix, freebsd}) ->
430+
get_total_memory({unix,freebsd}) ->
388431
PageSize = sysctl("vm.stats.vm.v_page_size"),
389432
PageCount = sysctl("vm.stats.vm.v_page_count"),
390433
PageCount * PageSize;
391434

392-
get_total_memory({unix, openbsd}) ->
435+
get_total_memory({unix,openbsd}) ->
393436
sysctl("hw.usermem");
394437

395-
get_total_memory({win32, _OSname}) ->
438+
get_total_memory({win32,_OSname}) ->
396439
[Result|_] = os_mon_sysinfo:get_mem_info(),
397440
{ok, [_MemLoad, TotPhys, _AvailPhys, _TotPage, _AvailPage, _TotV, _AvailV],
398441
_RestStr} =

0 commit comments

Comments
 (0)