| 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 | |
| 29 | -define(T, #?MODULE). |
| 30 | |
| 31 | -opaque t() :: |
| 32 | ?T{}. |
| 33 | |
| 34 | -spec new() -> |
| 35 | t(). |
| 36 | new() -> |
| 37 | { {input , CurrentIOBytesIn} |
| 38 | , {output , CurrentIOBytesOut} |
| 39 | } = erlang:statistics(io), |
| 40 | {CurrentContextSwitches, 0} = erlang:statistics(context_switches), |
| 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 |
| 49 | , previous_context_switches = 0 |
| 50 | , current_context_switches = CurrentContextSwitches |
| 51 | }. |
| 52 | |
| 53 | -spec update(t()) -> |
| 54 | t(). |
| 55 | update(?T |
| 56 | { previous_io_bytes_in = PreviousIOBytesIn |
| 57 | , previous_io_bytes_out = PreviousIOBytesOut |
| 58 | , previous_context_switches = PreviousContextSwitches |
| 59 | } |
| 60 | ) -> |
| 61 | { {input , CurrentIOBytesIn} |
| 62 | , {output , CurrentIOBytesOut} |
| 63 | } = erlang:statistics(io), |
| 64 | {CurrentContextSwitches, 0} = erlang:statistics(context_switches), |
| 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 |
| 73 | , previous_context_switches = PreviousContextSwitches |
| 74 | , current_context_switches = CurrentContextSwitches |
| 75 | }. |
| 76 | |
| 77 | -spec export(t()) -> |
| 78 | beam_stats:t(). |
| 79 | export( |
| 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 |
| 88 | , previous_context_switches = PreviousContextSwitches |
| 89 | , current_context_switches = CurrentContextSwitches |
| 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 |
| 98 | , context_switches = CurrentContextSwitches - PreviousContextSwitches |
| 99 | }. |