Skip to content

Switch to kB, MB, GB (as opposed to kiB, MiB, GiB) in vm_memory_monitor where possible #1235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 14 additions & 8 deletions src/vm_memory_monitor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@

-define(SERVER, ?MODULE).
-define(DEFAULT_MEMORY_CHECK_INTERVAL, 1000).
-define(ONE_MB, 1048576).
-define(ONE_MB, 1000000).

%% For an unknown OS, we assume that we have 1GB of memory. It'll be
%% wrong. Scale by vm_memory_high_watermark in configuration to get a
%% sensible value.
-define(MEMORY_SIZE_FOR_UNKNOWN_OS, 1073741824).
-define(MEMORY_SIZE_FOR_UNKNOWN_OS, ?ONE_MB * 1000).
-define(DEFAULT_VM_MEMORY_HIGH_WATERMARK, 0.4).

-record(state, {total_memory,
Expand Down Expand Up @@ -258,16 +258,16 @@ start_timer(Timeout) ->
%% Windows has 2GB and 8TB of address space for 32 and 64 bit accordingly.
get_vm_limit({win32,_OSname}) ->
case erlang:system_info(wordsize) of
4 -> 2*1024*1024*1024; %% 2 GB for 32 bits 2^31
8 -> 8*1024*1024*1024*1024 %% 8 TB for 64 bits 2^42
4 -> 2*1000*1000*1000; %% 2 GB for 32 bits 2^31
8 -> 8*1000*1000*1000*1000 %% 8 TB for 64 bits 2^42
end;

%% On a 32-bit machine, if you're using more than 2 gigs of RAM you're
%% in big trouble anyway.
get_vm_limit(_OsType) ->
case erlang:system_info(wordsize) of
4 -> 2*1024*1024*1024; %% 2 GB for 32 bits 2^31
8 -> 256*1024*1024*1024*1024 %% 256 TB for 64 bits 2^48
4 -> 2*1000*1000*1000; %% 2 GB for 32 bits 2^31
8 -> 256*1000*1000*1000*1000 %% 256 TB for 64 bits 2^48
%%http://en.wikipedia.org/wiki/X86-64#Virtual_address_space_details
end.

Expand Down Expand Up @@ -346,6 +346,10 @@ parse_line_mach(Line) ->

%% A line looks like "MemTotal: 502968 kB"
%% or (with broken OS/modules) "Readahead 123456 kB"
%%
%% Note that while /proc/meminfo output claims to be in kilobytes (kB, 1000 bytes),
%% it is actually in kibibytes (1024 bytes) for historical reasons, according
%% to RHEL documentation: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/s2-proc-meminfo.html.
parse_line_linux(Line) ->
{Name, Value, UnitRest} =
case string:tokens(Line, ":") of
Expand All @@ -360,6 +364,8 @@ parse_line_linux(Line) ->
end,
Value1 = case UnitRest of
[] -> list_to_integer(Value); %% no units
%% the value is actually in kebibytes, see
%% https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/s2-proc-meminfo.html
["kB"] -> list_to_integer(Value) * 1024
end,
{list_to_atom(Name), Value1}.
Expand All @@ -371,11 +377,11 @@ parse_line_sunos(Line) ->
[Value1 | UnitsRest] = string:tokens(RHS, " "),
Value2 = case UnitsRest of
["Gigabytes"] ->
list_to_integer(Value1) * ?ONE_MB * 1024;
list_to_integer(Value1) * ?ONE_MB * 1000;
["Megabytes"] ->
list_to_integer(Value1) * ?ONE_MB;
["Kilobytes"] ->
list_to_integer(Value1) * 1024;
list_to_integer(Value1) * 1000;
_ ->
Value1 ++ UnitsRest %% no known units
end,
Expand Down