erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 line
2.3 KiB

4 年之前
  1. -module(memory_show).
  2. -compile(export_all).
  3. show(N) ->
  4. F = fun(P) ->
  5. case catch process_info(P, [memory, dictionary]) of
  6. [{_, Memory}, {_, Dict}] ->
  7. InitStart = util:prop_get_value('$initial_call', Dict, null),
  8. {InitStart, Memory};
  9. _ -> {null, 0}
  10. end
  11. end,
  12. Infos1 = lists:map(F, processes()),
  13. Infos2 = [{Name, M} || {Name, M} <- Infos1, Name =/= null],
  14. SortFun = fun({_, M1}, {_, M2}) -> M1 > M2 end,
  15. Infos3 = lists:sort(SortFun, Infos2),
  16. Infos4 = lists:sublist(Infos3, N),
  17. [io:format("~p : ~p ~n", [Name, M]) || {Name, M} <- Infos4],
  18. ok.
  19. show(N, SkipNames) ->
  20. F = fun(P) ->
  21. case catch process_info(P, [memory, dictionary]) of
  22. [{_, Memory}, {_, Dict}] ->
  23. InitStart = util:prop_get_value('$initial_call', Dict, null),
  24. case catch tuple_to_list(InitStart) of
  25. [Name | _] ->
  26. case lists:member(Name, SkipNames) of
  27. true -> {null, 0};
  28. false -> {InitStart, Memory}
  29. end;
  30. _ -> {null, 0}
  31. end;
  32. _ -> {null, 0}
  33. end
  34. end,
  35. Infos1 = lists:map(F, processes()),
  36. Infos2 = [{Name, M} || {Name, M} <- Infos1, Name =/= null],
  37. SortFun = fun({_, M1}, {_, M2}) -> M1 > M2 end,
  38. Infos3 = lists:sort(SortFun, Infos2),
  39. Infos4 = lists:sublist(Infos3, N),
  40. [io:format("~p : ~p ~n", [Name, M]) || {Name, M} <- Infos4],
  41. ok.
  42. show1(N) ->
  43. F = fun(P, Acc) ->
  44. case catch process_info(P, [memory, dictionary]) of
  45. [{_, Memory}, {_, Dict}] ->
  46. InitStart = util:prop_get_value('$initial_call', Dict, null),
  47. case lists:keyfind(InitStart, 1, Acc) of
  48. false -> [{InitStart, Memory, 1} | Acc];
  49. {InitStart, Memory1, Num} -> lists:keystore(InitStart, 1, Acc, {InitStart, Memory + Memory1, Num + 1})
  50. end;
  51. _ -> Acc
  52. end
  53. end,
  54. Infos1 = lists:foldl(F, [], processes()),
  55. Infos2 = [{Name, M, Num} || {Name, M, Num} <- Infos1, Name =/= null],
  56. SortFun = fun({_, M1, _}, {_, M2, _}) -> M1 > M2 end,
  57. Infos3 = lists:sort(SortFun, Infos2),
  58. Infos4 = lists:sublist(Infos3, N),
  59. [io:format("~p : per_memory=~p process_num=~p ~n", [Name, (M div Num), Num]) || {Name, M, Num} <- Infos4],
  60. ok.