Track context_switches delta.
[beam_stats.git] / src / beam_stats_state.erl
CommitLineData
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
15-record(?MODULE,
16 { timestamp :: erlang:timestamp()
17 , node_id :: atom()
18 , memory :: [{atom(), non_neg_integer()}]
3fe887d7 19
b4e2333f
SK
20 , previous_io_bytes_in :: non_neg_integer()
21 , previous_io_bytes_out :: non_neg_integer()
22 , current_io_bytes_in :: non_neg_integer()
23 , current_io_bytes_out :: non_neg_integer()
3fe887d7
SK
24
25 , previous_context_switches :: non_neg_integer()
26 , current_context_switches :: non_neg_integer()
b4e2333f
SK
27 }).
28
29-define(T, #?MODULE).
30
31-opaque t() ::
32 ?T{}.
33
34-spec new() ->
35 t().
36new() ->
37 { {input , CurrentIOBytesIn}
38 , {output , CurrentIOBytesOut}
39 } = erlang:statistics(io),
3fe887d7 40 {CurrentContextSwitches, 0} = erlang:statistics(context_switches),
b4e2333f
SK
41 ?T
42 { timestamp = os:timestamp()
43 , node_id = erlang:node()
44 , memory = erlang:memory()
45 , previous_io_bytes_in = 0
46 , previous_io_bytes_out = 0
47 , current_io_bytes_in = CurrentIOBytesIn
48 , current_io_bytes_out = CurrentIOBytesOut
3fe887d7
SK
49 , previous_context_switches = 0
50 , current_context_switches = CurrentContextSwitches
b4e2333f
SK
51 }.
52
53-spec update(t()) ->
54 t().
55update(?T
56 { previous_io_bytes_in = PreviousIOBytesIn
57 , previous_io_bytes_out = PreviousIOBytesOut
3fe887d7 58 , previous_context_switches = PreviousContextSwitches
b4e2333f
SK
59 }
60) ->
61 { {input , CurrentIOBytesIn}
62 , {output , CurrentIOBytesOut}
63 } = erlang:statistics(io),
3fe887d7 64 {CurrentContextSwitches, 0} = erlang:statistics(context_switches),
b4e2333f
SK
65 ?T
66 { timestamp = os:timestamp()
67 , node_id = erlang:node()
68 , memory = erlang:memory()
69 , previous_io_bytes_in = PreviousIOBytesIn
70 , previous_io_bytes_out = PreviousIOBytesOut
71 , current_io_bytes_in = CurrentIOBytesIn
72 , current_io_bytes_out = CurrentIOBytesOut
3fe887d7
SK
73 , previous_context_switches = PreviousContextSwitches
74 , current_context_switches = CurrentContextSwitches
b4e2333f
SK
75 }.
76
77-spec export(t()) ->
78 beam_stats:t().
79export(
80 ?T
81 { timestamp = Timestamp
82 , node_id = NodeID
83 , memory = Memory
84 , previous_io_bytes_in = PreviousIOBytesIn
85 , previous_io_bytes_out = PreviousIOBytesOut
86 , current_io_bytes_in = CurrentIOBytesIn
87 , current_io_bytes_out = CurrentIOBytesOut
3fe887d7
SK
88 , previous_context_switches = PreviousContextSwitches
89 , current_context_switches = CurrentContextSwitches
b4e2333f
SK
90 }
91) ->
92 #beam_stats
93 { timestamp = Timestamp
94 , node_id = NodeID
95 , memory = Memory
96 , io_bytes_in = CurrentIOBytesIn - PreviousIOBytesIn
97 , io_bytes_out = CurrentIOBytesOut - PreviousIOBytesOut
3fe887d7 98 , context_switches = CurrentContextSwitches - PreviousContextSwitches
b4e2333f 99 }.
This page took 0.03996 seconds and 4 git commands to generate.