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