Skip to content

Commit

Permalink
Merge pull request #9 from hirotnk/memory-vm-stats
Browse files Browse the repository at this point in the history
Add literal memory usage stat
  • Loading branch information
djnym authored Jun 19, 2018
2 parents 1e10bda + 5bc1c5e commit 670122e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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

Expand Down
7 changes: 6 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -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)", allocator_stats_available}
]
}.

{clean_files, ["ebin", "doc"]}.

Expand Down
47 changes: 47 additions & 0 deletions src/mondemand_vmstats.erl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
memory_atom,
memory_binary,
memory_ets,
memory_literal,
process_count,
process_limit,
port_count,
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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),

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 },
Expand All @@ -382,10 +388,51 @@ 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.
% Note: This function returns constant value zero when allocator stats are
% not available.
-ifdef (allocator_stats_available).
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})).
-else.
get_literal_memory_size () -> 0.
-endif.


%%--------------------------------------------------------------------
%%% Test functions
%%--------------------------------------------------------------------
-ifdef (TEST).
-include_lib ("eunit/include/eunit.hrl").

-ifdef(allocator_stats_available).
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.

0 comments on commit 670122e

Please sign in to comment.