| 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()}] |
| 19 | |
| 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() |
| 24 | |
| 25 | , previous_context_switches :: non_neg_integer() |
| 26 | , current_context_switches :: non_neg_integer() |
| 27 | |
| 28 | , reductions :: non_neg_integer() |
| 29 | }). |
| 30 | |
| 31 | -define(T, #?MODULE). |
| 32 | |
| 33 | -opaque t() :: |
| 34 | ?T{}. |
| 35 | |
| 36 | -spec new() -> |
| 37 | t(). |
| 38 | new() -> |
| 39 | { {input , CurrentIOBytesIn} |
| 40 | , {output , CurrentIOBytesOut} |
| 41 | } = erlang:statistics(io), |
| 42 | {CurrentContextSwitches, 0} = erlang:statistics(context_switches), |
| 43 | {_ReductionsTotal, ReductionsDelta} = erlang:statistics(reductions), |
| 44 | ?T |
| 45 | { timestamp = os:timestamp() |
| 46 | , node_id = erlang:node() |
| 47 | , memory = erlang:memory() |
| 48 | , previous_io_bytes_in = 0 |
| 49 | , previous_io_bytes_out = 0 |
| 50 | , current_io_bytes_in = CurrentIOBytesIn |
| 51 | , current_io_bytes_out = CurrentIOBytesOut |
| 52 | , previous_context_switches = 0 |
| 53 | , current_context_switches = CurrentContextSwitches |
| 54 | , reductions = ReductionsDelta |
| 55 | }. |
| 56 | |
| 57 | -spec update(t()) -> |
| 58 | t(). |
| 59 | update(?T |
| 60 | { previous_io_bytes_in = PreviousIOBytesIn |
| 61 | , previous_io_bytes_out = PreviousIOBytesOut |
| 62 | , previous_context_switches = PreviousContextSwitches |
| 63 | } |
| 64 | ) -> |
| 65 | { {input , CurrentIOBytesIn} |
| 66 | , {output , CurrentIOBytesOut} |
| 67 | } = erlang:statistics(io), |
| 68 | {CurrentContextSwitches, 0} = erlang:statistics(context_switches), |
| 69 | {_ReductionsTotal, ReductionsDelta} = erlang:statistics(reductions), |
| 70 | ?T |
| 71 | { timestamp = os:timestamp() |
| 72 | , node_id = erlang:node() |
| 73 | , memory = erlang:memory() |
| 74 | , previous_io_bytes_in = PreviousIOBytesIn |
| 75 | , previous_io_bytes_out = PreviousIOBytesOut |
| 76 | , current_io_bytes_in = CurrentIOBytesIn |
| 77 | , current_io_bytes_out = CurrentIOBytesOut |
| 78 | , previous_context_switches = PreviousContextSwitches |
| 79 | , current_context_switches = CurrentContextSwitches |
| 80 | , reductions = ReductionsDelta |
| 81 | }. |
| 82 | |
| 83 | -spec export(t()) -> |
| 84 | beam_stats:t(). |
| 85 | export( |
| 86 | ?T |
| 87 | { timestamp = Timestamp |
| 88 | , node_id = NodeID |
| 89 | , memory = Memory |
| 90 | , previous_io_bytes_in = PreviousIOBytesIn |
| 91 | , previous_io_bytes_out = PreviousIOBytesOut |
| 92 | , current_io_bytes_in = CurrentIOBytesIn |
| 93 | , current_io_bytes_out = CurrentIOBytesOut |
| 94 | , previous_context_switches = PreviousContextSwitches |
| 95 | , current_context_switches = CurrentContextSwitches |
| 96 | , reductions = Reductions |
| 97 | } |
| 98 | ) -> |
| 99 | #beam_stats |
| 100 | { timestamp = Timestamp |
| 101 | , node_id = NodeID |
| 102 | , memory = Memory |
| 103 | , io_bytes_in = CurrentIOBytesIn - PreviousIOBytesIn |
| 104 | , io_bytes_out = CurrentIOBytesOut - PreviousIOBytesOut |
| 105 | , context_switches = CurrentContextSwitches - PreviousContextSwitches |
| 106 | , reductions = Reductions |
| 107 | }. |