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