From 3b9cc5b374a2c3edd54e3250bdcac5f707fdfb30 Mon Sep 17 00:00:00 2001 From: Yoshihiro Tanaka Date: Fri, 8 Jun 2018 16:59:10 -0700 Subject: [PATCH 1/3] Add literal memory usage stat --- ChangeLog | 3 +++ src/mondemand_vmstats.erl | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/ChangeLog b/ChangeLog index fb58591..9ebb788 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +* Fri Jun 8 2018 Yoshihiro Tanaka (yoshi) 6.10.0 +- Add literal memory usage stat + * Tue May 15 2018 Anthony Molinaro (djnym) 6.9.4 - have version in .app.src come from ChangeLog diff --git a/src/mondemand_vmstats.erl b/src/mondemand_vmstats.erl index d1b476e..6a8e6d5 100644 --- a/src/mondemand_vmstats.erl +++ b/src/mondemand_vmstats.erl @@ -59,6 +59,7 @@ memory_atom, memory_binary, memory_ets, + memory_literal, process_count, process_limit, port_count, @@ -112,6 +113,7 @@ metric_from_sample (Metric, Sample) -> memory_atom -> {Metric, Sample#vm_sample.memory_atom}; memory_binary -> {Metric, Sample#vm_sample.memory_binary}; memory_ets -> {Metric, Sample#vm_sample.memory_ets}; + memory_literal -> {Metric, Sample#vm_sample.memory_literal}; process_count -> {Metric, Sample#vm_sample.process_count}; process_limit -> {Metric, Sample#vm_sample.process_limit}; port_count -> {Metric, Sample#vm_sample.port_count}; @@ -268,6 +270,7 @@ collect_sample (Legacy, CollectSchedulerStats) -> AtomUsed = proplists:get_value(atom_used, Memory), BinaryMemory = proplists:get_value(binary, Memory), EtsMemory = proplists:get_value(ets, Memory), + LiteralMemory = get_literal_memory_size(), ProcessCount = erlang:system_info(process_count), ProcessLimit = erlang:system_info(process_limit), @@ -302,6 +305,7 @@ collect_sample (Legacy, CollectSchedulerStats) -> memory_atom = AtomUsed, memory_binary = BinaryMemory, memory_ets = EtsMemory, + memory_literal = LiteralMemory, process_count = ProcessCount, process_limit = ProcessLimit, port_count = PortCount, @@ -338,6 +342,7 @@ to_mondemand (#vm_sample { memory_atom = AtomUsed, memory_binary = BinaryMemory, memory_ets = EtsMemory, + memory_literal = LiteralMemory, process_count = ProcessCount, process_limit = ProcessLimit, port_count = PortCount, @@ -361,6 +366,7 @@ to_mondemand (#vm_sample { { gauge, memory_atom, AtomUsed }, { gauge, memory_binary, BinaryMemory }, { gauge, memory_ets, EtsMemory }, + { gauge, memory_literal, LiteralMemory }, { gauge, process_count, ProcessCount }, { gauge, process_limit, ProcessLimit }, { gauge, port_count, PortCount }, @@ -382,10 +388,39 @@ scheduler_wall_time_diff (PrevSchedWallTime, SchedWallTime) -> <- lists:zip(lists:sort(PrevSchedWallTime),lists:sort(SchedWallTime)) ]. +% Sum of current carriersizes(mcbs+scbs) for all instances although +% currently there is only one global instance for literal_alloc. +get_literal_memory_size () -> + lists:foldl( + fun + ({instance, _N, Instance}, AccLiteralSize) when is_list(Instance) -> + CarrierSizeForInstance = + lists:map( + fun(CarrierSizeType) -> + case lists:keyfind(CarrierSizeType, 1, Instance) of + {CarrierSizeType, MemDataList} when is_list(MemDataList) -> + case lists:keyfind(carriers_size, 1, MemDataList) of + { + carriers_size, + CurrentSize, _LastMaxSize, _MaxSize + } -> CurrentSize; + _Else -> 0 % Ignore unknown format + end; + _Else -> 0 % Ignore unknown format + end + end, [mbcs, sbcs]), + lists:sum(CarrierSizeForInstance) + AccLiteralSize + ;(_UnknownFormat, AccLiteralSize) -> AccLiteralSize + end, 0, erlang:system_info({allocator, literal_alloc})). + %%-------------------------------------------------------------------- %%% Test functions %%-------------------------------------------------------------------- -ifdef (TEST). -include_lib ("eunit/include/eunit.hrl"). +literal_memory_test () -> + Size = get_literal_memory_size(), + ?assertEqual(true, is_integer(Size) andalso Size =/= 0). + -endif. From 700454f5a0a171cd176874f3b80e6e9aeb1d9d95 Mon Sep 17 00:00:00 2001 From: Yoshihiro Tanaka Date: Tue, 12 Jun 2018 18:52:24 +0000 Subject: [PATCH 2/3] Handle OTP18 or before case - Skip calling erlang:system_info/1 with '{allocator, literal_alloc}'. - Return zero if OTP18 or before from get_literal_memory_size/0. --- rebar.config | 7 ++++++- src/mondemand_vmstats.erl | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/rebar.config b/rebar.config index feb5771..5acd6d3 100644 --- a/rebar.config +++ b/rebar.config @@ -6,7 +6,12 @@ { cover_print_enabled, true }. %% always include debug info so AST is included in beams -{erl_opts, [debug_info]}. +{erl_opts, + [ + debug_info, + {platform_define, "^(19|2)", namespaced_types_19_later} + ] +}. {clean_files, ["ebin", "doc"]}. diff --git a/src/mondemand_vmstats.erl b/src/mondemand_vmstats.erl index 6a8e6d5..c863c79 100644 --- a/src/mondemand_vmstats.erl +++ b/src/mondemand_vmstats.erl @@ -390,6 +390,9 @@ scheduler_wall_time_diff (PrevSchedWallTime, SchedWallTime) -> % Sum of current carriersizes(mcbs+scbs) for all instances although % currently there is only one global instance for literal_alloc. +% Note: This function returns constant value zero when OTP version +% is 18 or before. +-ifdef (namespaced_types_19_later). get_literal_memory_size () -> lists:foldl( fun @@ -412,6 +415,10 @@ get_literal_memory_size () -> lists:sum(CarrierSizeForInstance) + AccLiteralSize ;(_UnknownFormat, AccLiteralSize) -> AccLiteralSize end, 0, erlang:system_info({allocator, literal_alloc})). +-else. +get_literal_memory_size () -> 0. +-endif. + %%-------------------------------------------------------------------- %%% Test functions @@ -419,8 +426,13 @@ get_literal_memory_size () -> -ifdef (TEST). -include_lib ("eunit/include/eunit.hrl"). +-ifdef(namespaced_types_19_later). literal_memory_test () -> Size = get_literal_memory_size(), ?assertEqual(true, is_integer(Size) andalso Size =/= 0). +-else. +literal_memory_test () -> + ?assertEqual(0, get_literal_memory_size()). +-endif. -endif. From 5bc1c5e33d6779aeafd2d5770348921a8fc78f86 Mon Sep 17 00:00:00 2001 From: Yoshihiro Tanaka Date: Wed, 13 Jun 2018 20:34:32 +0000 Subject: [PATCH 3/3] Update the name of ifdef macro --- rebar.config | 2 +- src/mondemand_vmstats.erl | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/rebar.config b/rebar.config index 5acd6d3..5d8b641 100644 --- a/rebar.config +++ b/rebar.config @@ -9,7 +9,7 @@ {erl_opts, [ debug_info, - {platform_define, "^(19|2)", namespaced_types_19_later} + {platform_define, "^(19|2)", allocator_stats_available} ] }. diff --git a/src/mondemand_vmstats.erl b/src/mondemand_vmstats.erl index c863c79..2f650c0 100644 --- a/src/mondemand_vmstats.erl +++ b/src/mondemand_vmstats.erl @@ -390,9 +390,9 @@ scheduler_wall_time_diff (PrevSchedWallTime, SchedWallTime) -> % Sum of current carriersizes(mcbs+scbs) for all instances although % currently there is only one global instance for literal_alloc. -% Note: This function returns constant value zero when OTP version -% is 18 or before. --ifdef (namespaced_types_19_later). +% Note: This function returns constant value zero when allocator stats are +% not available. +-ifdef (allocator_stats_available). get_literal_memory_size () -> lists:foldl( fun @@ -426,7 +426,7 @@ get_literal_memory_size () -> 0. -ifdef (TEST). -include_lib ("eunit/include/eunit.hrl"). --ifdef(namespaced_types_19_later). +-ifdef(allocator_stats_available). literal_memory_test () -> Size = get_literal_memory_size(), ?assertEqual(true, is_integer(Size) andalso Size =/= 0).