| 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 | , previous_io_bytes_in :: non_neg_integer() |
| 20 | , previous_io_bytes_out :: non_neg_integer() |
| 21 | , current_io_bytes_in :: non_neg_integer() |
| 22 | , current_io_bytes_out :: non_neg_integer() |
| 23 | }). |
| 24 | |
| 25 | -define(T, #?MODULE). |
| 26 | |
| 27 | -opaque t() :: |
| 28 | ?T{}. |
| 29 | |
| 30 | -spec new() -> |
| 31 | t(). |
| 32 | new() -> |
| 33 | { {input , CurrentIOBytesIn} |
| 34 | , {output , CurrentIOBytesOut} |
| 35 | } = erlang:statistics(io), |
| 36 | ?T |
| 37 | { timestamp = os:timestamp() |
| 38 | , node_id = erlang:node() |
| 39 | , memory = erlang:memory() |
| 40 | , previous_io_bytes_in = 0 |
| 41 | , previous_io_bytes_out = 0 |
| 42 | , current_io_bytes_in = CurrentIOBytesIn |
| 43 | , current_io_bytes_out = CurrentIOBytesOut |
| 44 | }. |
| 45 | |
| 46 | -spec update(t()) -> |
| 47 | t(). |
| 48 | update(?T |
| 49 | { previous_io_bytes_in = PreviousIOBytesIn |
| 50 | , previous_io_bytes_out = PreviousIOBytesOut |
| 51 | } |
| 52 | ) -> |
| 53 | { {input , CurrentIOBytesIn} |
| 54 | , {output , CurrentIOBytesOut} |
| 55 | } = erlang:statistics(io), |
| 56 | ?T |
| 57 | { timestamp = os:timestamp() |
| 58 | , node_id = erlang:node() |
| 59 | , memory = erlang:memory() |
| 60 | , previous_io_bytes_in = PreviousIOBytesIn |
| 61 | , previous_io_bytes_out = PreviousIOBytesOut |
| 62 | , current_io_bytes_in = CurrentIOBytesIn |
| 63 | , current_io_bytes_out = CurrentIOBytesOut |
| 64 | }. |
| 65 | |
| 66 | -spec export(t()) -> |
| 67 | beam_stats:t(). |
| 68 | export( |
| 69 | ?T |
| 70 | { timestamp = Timestamp |
| 71 | , node_id = NodeID |
| 72 | , memory = Memory |
| 73 | , previous_io_bytes_in = PreviousIOBytesIn |
| 74 | , previous_io_bytes_out = PreviousIOBytesOut |
| 75 | , current_io_bytes_in = CurrentIOBytesIn |
| 76 | , current_io_bytes_out = CurrentIOBytesOut |
| 77 | } |
| 78 | ) -> |
| 79 | #beam_stats |
| 80 | { timestamp = Timestamp |
| 81 | , node_id = NodeID |
| 82 | , memory = Memory |
| 83 | , io_bytes_in = CurrentIOBytesIn - PreviousIOBytesIn |
| 84 | , io_bytes_out = CurrentIOBytesOut - PreviousIOBytesOut |
| 85 | }. |