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