1 -module(beam_stats_delta).
10 , of_context_switches/1
13 , of_process_info_reductions/2
17 { erlang_statistics :: ets:tid()
18 , erlang_process_info_reductions :: ets:tid()
34 { erlang_statistics = ets:new(beam_stats_delta_erlang_statistics, Options)
35 , erlang_process_info_reductions =
36 ets:new(beam_stats_delta_erlang_process_info_reductions, Options)
42 { erlang_statistics = TidErlangStatistics
43 , erlang_process_info_reductions = TidErlangProcessInfoReductions
46 true = ets:delete(TidErlangStatistics),
47 true = ets:delete(TidErlangProcessInfoReductions),
50 -spec of_context_switches(t()) ->
52 of_context_switches(?T{erlang_statistics=Table}) ->
53 Key = context_switches,
54 {Current, 0} = beam_stats_source:erlang_statistics(Key),
55 delta(Table, Key, Current).
58 { {io_bytes_in , non_neg_integer()}
59 , {io_bytes_out , non_neg_integer()}
61 of_io(?T{erlang_statistics=Table}) ->
64 , {output , CurrentOut}
65 } = beam_stats_source:erlang_statistics(Key),
66 DeltaIn = delta(Table, io_bytes_in , CurrentIn),
67 DeltaOut = delta(Table, io_bytes_out, CurrentOut),
68 { {io_bytes_in , DeltaIn}
69 , {io_bytes_out , DeltaOut}
72 % We can get between-calls-delta directly from erlang:statistics(reductions),
73 % but then if some other process also calls it - we'll get incorrect data on
75 % Managing deltas ourselves here, will at least reduce the possible callers to
76 % only those with knowledge of our table ID.
77 -spec of_reductions(t()) ->
79 of_reductions(?T{erlang_statistics=Table}) ->
81 {Current, _} = beam_stats_source:erlang_statistics(Key),
82 delta(Table, Key, Current).
84 -spec of_process_info_reductions(t(), pid()) ->
85 hope_option:t(non_neg_integer()).
86 of_process_info_reductions(?T{erlang_process_info_reductions=Table}, Pid) ->
87 case beam_stats_source:erlang_process_info(Pid, reductions)
90 ; {reductions, Current} ->
91 Delta = delta(Table, Pid, Current),
95 -spec delta(ets:tid(), Key, non_neg_integer()) ->
97 when Key :: atom() | pid().
98 delta(Table, Key, CurrentTotal) ->
99 PreviousTotalOpt = find(Table, Key),
100 PreviousTotal = hope_option:get(PreviousTotalOpt, 0),
101 save(Table, Key, CurrentTotal),
102 CurrentTotal - PreviousTotal.
104 -spec find(ets:tid(), term()) ->
105 hope_option:t(term()).
107 case ets:lookup(Table, K)
109 ; [{K, V}] -> {some, V}
112 -spec save(ets:tid(), term(), term()) ->
115 true = ets:insert(Table, {K, V}),