Commit | Line | Data |
---|---|---|
b4e2333f SK |
1 | -module(beam_stats_state). |
2 | ||
3 | -include("include/beam_stats.hrl"). | |
4 | ||
5 | -export_type( | |
6 | [ t/0 | |
7 | ]). | |
8 | ||
9 | -export( | |
10 | [ new/0 | |
11 | , update/1 | |
12 | , export/1 | |
13 | ]). | |
14 | ||
28c1d03a SK |
15 | -record(snapshots, |
16 | { memory :: [{atom(), non_neg_integer()}] | |
17 | , run_queue :: non_neg_integer() | |
a8d431d1 | 18 | , ets :: beam_stats_ets:t() |
28c1d03a | 19 | }). |
3fe887d7 | 20 | |
28c1d03a SK |
21 | -type snapshots() :: |
22 | #snapshots{}. | |
23 | ||
24 | -record(deltas, | |
25 | { reductions :: non_neg_integer() | |
26 | }). | |
27 | ||
28 | -type deltas() :: | |
29 | #deltas{}. | |
30 | ||
31 | -record(totals, | |
32 | { io_bytes_in :: non_neg_integer() | |
33 | , io_bytes_out :: non_neg_integer() | |
34 | , context_switches :: non_neg_integer() | |
35 | }). | |
3fe887d7 | 36 | |
28c1d03a SK |
37 | -type totals() :: |
38 | #totals{}. | |
142c0796 | 39 | |
28c1d03a SK |
40 | -record(?MODULE, |
41 | { timestamp :: erlang:timestamp() | |
42 | , node_id :: atom() | |
43 | , snapshots :: snapshots() % Current state | |
44 | , deltas :: deltas() % Accumulated since last check | |
45 | , totals_previous :: totals() % Accumulated since VM start, as of last state | |
46 | , totals_current :: totals() % Accumulated since VM start, as of this state | |
b4e2333f SK |
47 | }). |
48 | ||
49 | -define(T, #?MODULE). | |
50 | ||
51 | -opaque t() :: | |
52 | ?T{}. | |
53 | ||
54 | -spec new() -> | |
55 | t(). | |
56 | new() -> | |
28c1d03a SK |
57 | TotalsPrevious = totals_empty(), |
58 | new(TotalsPrevious). | |
59 | ||
60 | -spec new(TotalsPrevious :: totals()) -> | |
61 | t(). | |
62 | new(#totals{}=TotalsPrevious) -> | |
b4e2333f | 63 | ?T |
28c1d03a SK |
64 | { timestamp = os:timestamp() |
65 | , node_id = erlang:node() | |
66 | , snapshots = snapshots_new() | |
67 | , deltas = deltas_new() | |
68 | , totals_previous = TotalsPrevious | |
69 | , totals_current = totals_new() | |
b4e2333f SK |
70 | }. |
71 | ||
72 | -spec update(t()) -> | |
73 | t(). | |
28c1d03a SK |
74 | update(?T{totals_current=TotalsPrevious}) -> |
75 | new(TotalsPrevious). | |
b4e2333f SK |
76 | |
77 | -spec export(t()) -> | |
78 | beam_stats:t(). | |
79 | export( | |
80 | ?T | |
28c1d03a SK |
81 | { timestamp = Timestamp |
82 | , node_id = NodeID | |
83 | , snapshots = | |
84 | #snapshots | |
85 | { memory = Memory | |
86 | , run_queue = RunQueue | |
a8d431d1 | 87 | , ets = ETS |
28c1d03a SK |
88 | } |
89 | , deltas = | |
90 | #deltas | |
91 | { reductions = Reductions | |
92 | } | |
93 | , totals_previous = | |
94 | #totals | |
95 | { io_bytes_in = PreviousIOBytesIn | |
96 | , io_bytes_out = PreviousIOBytesOut | |
97 | , context_switches = PreviousContextSwitches | |
98 | } | |
99 | , totals_current = | |
100 | #totals | |
101 | { io_bytes_in = CurrentIOBytesIn | |
102 | , io_bytes_out = CurrentIOBytesOut | |
103 | , context_switches = CurrentContextSwitches | |
104 | } | |
b4e2333f SK |
105 | } |
106 | ) -> | |
107 | #beam_stats | |
28c1d03a SK |
108 | { timestamp = Timestamp |
109 | , node_id = NodeID | |
110 | , memory = Memory | |
111 | , io_bytes_in = CurrentIOBytesIn - PreviousIOBytesIn | |
112 | , io_bytes_out = CurrentIOBytesOut - PreviousIOBytesOut | |
3fe887d7 | 113 | , context_switches = CurrentContextSwitches - PreviousContextSwitches |
142c0796 | 114 | , reductions = Reductions |
deefeb3c | 115 | , run_queue = RunQueue |
a8d431d1 | 116 | , ets = ETS |
b4e2333f | 117 | }. |
28c1d03a SK |
118 | |
119 | snapshots_new() -> | |
120 | #snapshots | |
121 | { memory = erlang:memory() | |
122 | , run_queue = erlang:statistics(run_queue) | |
a8d431d1 | 123 | , ets = beam_stats_ets:collect() |
28c1d03a SK |
124 | }. |
125 | ||
126 | deltas_new() -> | |
127 | {_ReductionsTotal, ReductionsDelta} = erlang:statistics(reductions), | |
128 | #deltas | |
129 | { reductions = ReductionsDelta | |
130 | }. | |
131 | ||
132 | totals_new() -> | |
133 | { {input , IOBytesIn} | |
134 | , {output , IOBytesOut} | |
135 | } = erlang:statistics(io), | |
136 | {ContextSwitches, 0} = erlang:statistics(context_switches), | |
137 | #totals | |
138 | { io_bytes_in = IOBytesIn | |
139 | , io_bytes_out = IOBytesOut | |
140 | , context_switches = ContextSwitches | |
141 | }. | |
142 | ||
143 | totals_empty() -> | |
144 | #totals | |
145 | { io_bytes_in = 0 | |
146 | , io_bytes_out = 0 | |
147 | , context_switches = 0 | |
148 | }. |