From 6ffbc0949dc8b142ef5901b8ca4df718f30733b4 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Fri, 10 Jul 2020 21:36:51 +0800 Subject: [PATCH] perf(emqx_vm): make emqx_vm:get_memory/0 more efficiency --- src/emqx_vm.erl | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/emqx_vm.erl b/src/emqx_vm.erl index b4a36f410..52f3a8e37 100644 --- a/src/emqx_vm.erl +++ b/src/emqx_vm.erl @@ -22,6 +22,7 @@ , get_system_info/0 , get_system_info/1 , get_memory/0 + , get_memory/2 , mem_info/0 , loads/0 ]). @@ -241,32 +242,47 @@ scheduler_usage_diff(First, Last) -> end, lists:zip(lists:sort(First), lists:sort(Last))). get_memory()-> - [{Key, get_memory(Key, current)} || Key <- [used, allocated, unused, usage]] ++ erlang:memory(). + get_memory_once(current) ++ erlang:memory(). + +get_memory(Ks, Keyword) when is_list(Ks) -> + Ms = get_memory_once(Keyword) ++ erlang:memory(), + [M || M = {K, _} <- Ms, lists:member(K, Ks)]; get_memory(used, Keyword) -> lists:sum(lists:map(fun({_, Prop}) -> container_size(Prop, Keyword, blocks_size) end, util_alloc())); + get_memory(allocated, Keyword) -> - lists:sum(lists:map(fun({_, Prop})-> + lists:sum(lists:map(fun({_, Prop}) -> container_size(Prop, Keyword, carriers_size) end, util_alloc())); + get_memory(unused, Keyword) -> - get_memory(allocated, Keyword) - get_memory(used, Keyword); + Ms = get_memory_once(Keyword), + proplists:get_value(allocated, Ms) - proplists:get_value(used, Ms); + get_memory(usage, Keyword) -> - get_memory(used, Keyword) / get_memory(allocated, Keyword). + Ms = get_memory_once(Keyword), + proplists:get_value(used, Ms) / proplists:get_value(allocated, Ms). + +%% @private A more quickly function to calculate memory +get_memory_once(Keyword) -> + Calc = fun({_, Prop}, {N1, N2}) -> + {N1 + container_size(Prop, Keyword, blocks_size), + N2 + container_size(Prop, Keyword, carriers_size)} + end, + {Used, Allocated} = lists:foldl(Calc, {0, 0}, util_alloc()), + [{used, Used}, + {allocated, Allocated}, + {unused, Allocated - Used}, + {usage, Used / Allocated}]. util_alloc()-> alloc(?UTIL_ALLOCATORS). -alloc()-> - {_Mem, Allocs} = snapshot_int(), - Allocs. alloc(Type) -> - [{{T, Instance}, Props} || {{T, Instance}, Props} <- alloc(), lists:member(T, Type)]. - -snapshot_int() -> - {erlang:memory(), allocators()}. + [{{T, Instance}, Props} || {{T, Instance}, Props} <- allocators(), lists:member(T, Type)]. allocators() -> UtilAllocators = erlang:system_info(alloc_util_allocators),