Fix request of next ETS key after deletion of previous
[beam_stats.git] / src / beam_stats_delta.erl
CommitLineData
b2c364fd
SK
1-module(beam_stats_delta).
2
3-export_type(
4 [ t/0
5 ]).
6
7-export(
8 [ start/0
9 , stop/1
140077ce 10 , gc/1
b2c364fd
SK
11 , of_context_switches/1
12 , of_io/1
11e1cb97 13 , of_reductions/1
7dbc59b6 14 , of_process_info_reductions/2
b2c364fd
SK
15 ]).
16
17-record(?MODULE,
18 { erlang_statistics :: ets:tid()
7dbc59b6 19 , erlang_process_info_reductions :: ets:tid()
b2c364fd
SK
20 }).
21
22-define(T, #?MODULE).
23
24-opaque t() ::
25 ?T{}.
26
27-spec start() ->
28 t().
29start() ->
30 Options =
31 [ set
32 , public
33 ],
34 ?T
35 { erlang_statistics = ets:new(beam_stats_delta_erlang_statistics, Options)
7dbc59b6
SK
36 , erlang_process_info_reductions =
37 ets:new(beam_stats_delta_erlang_process_info_reductions, Options)
b2c364fd
SK
38 }.
39
40-spec stop(t()) ->
41 {}.
42stop(?T
43 { erlang_statistics = TidErlangStatistics
7dbc59b6 44 , erlang_process_info_reductions = TidErlangProcessInfoReductions
b2c364fd
SK
45 }
46) ->
47 true = ets:delete(TidErlangStatistics),
7dbc59b6 48 true = ets:delete(TidErlangProcessInfoReductions),
b2c364fd
SK
49 {}.
50
140077ce
SK
51-spec gc(t()) ->
52 {}.
53gc(?T{erlang_process_info_reductions=Table}=T) ->
54 case ets:first(Table)
55 of '$end_of_table' ->
56 {}
57 ; FirstPid when is_pid(FirstPid) ->
58 gc(T, FirstPid)
59 end.
60
61-spec gc(t(), pid()) ->
62 {}.
63gc(?T{erlang_process_info_reductions=Table}=T, Pid) ->
9b2aef83 64 Next = ets:next(Table, Pid),
140077ce
SK
65 case beam_stats_source:erlang_is_process_alive(Pid)
66 of true -> true
67 ; false -> ets:delete(Table, Pid)
68 end,
9b2aef83 69 case Next
140077ce
SK
70 of '$end_of_table' ->
71 {}
72 ; NextPid when is_pid(NextPid) ->
73 gc(T, NextPid)
74 end.
75
b2c364fd
SK
76-spec of_context_switches(t()) ->
77 non_neg_integer().
78of_context_switches(?T{erlang_statistics=Table}) ->
79 Key = context_switches,
80 {Current, 0} = beam_stats_source:erlang_statistics(Key),
81 delta(Table, Key, Current).
82
83-spec of_io(t()) ->
84 { {io_bytes_in , non_neg_integer()}
85 , {io_bytes_out , non_neg_integer()}
86 }.
87of_io(?T{erlang_statistics=Table}) ->
88 Key = io,
89 { {input , CurrentIn}
90 , {output , CurrentOut}
91 } = beam_stats_source:erlang_statistics(Key),
92 DeltaIn = delta(Table, io_bytes_in , CurrentIn),
93 DeltaOut = delta(Table, io_bytes_out, CurrentOut),
94 { {io_bytes_in , DeltaIn}
95 , {io_bytes_out , DeltaOut}
96 }.
97
11e1cb97
SK
98% We can get between-calls-delta directly from erlang:statistics(reductions),
99% but then if some other process also calls it - we'll get incorrect data on
100% the next call.
101% Managing deltas ourselves here, will at least reduce the possible callers to
102% only those with knowledge of our table ID.
103-spec of_reductions(t()) ->
104 non_neg_integer().
105of_reductions(?T{erlang_statistics=Table}) ->
106 Key = reductions,
107 {Current, _} = beam_stats_source:erlang_statistics(Key),
108 delta(Table, Key, Current).
109
7dbc59b6
SK
110-spec of_process_info_reductions(t(), pid()) ->
111 hope_option:t(non_neg_integer()).
112of_process_info_reductions(?T{erlang_process_info_reductions=Table}, Pid) ->
113 case beam_stats_source:erlang_process_info(Pid, reductions)
114 of undefined ->
115 none
116 ; {reductions, Current} ->
117 Delta = delta(Table, Pid, Current),
118 {some, Delta}
119 end.
120
121-spec delta(ets:tid(), Key, non_neg_integer()) ->
122 non_neg_integer()
123 when Key :: atom() | pid().
b2c364fd
SK
124delta(Table, Key, CurrentTotal) ->
125 PreviousTotalOpt = find(Table, Key),
126 PreviousTotal = hope_option:get(PreviousTotalOpt, 0),
127 save(Table, Key, CurrentTotal),
128 CurrentTotal - PreviousTotal.
129
130-spec find(ets:tid(), term()) ->
131 hope_option:t(term()).
132find(Table, K) ->
133 case ets:lookup(Table, K)
134 of [] -> none
135 ; [{K, V}] -> {some, V}
136 end.
137
138-spec save(ets:tid(), term(), term()) ->
139 {}.
140save(Table, K, V) ->
141 true = ets:insert(Table, {K, V}),
142 {}.
This page took 0.033223 seconds and 4 git commands to generate.