| 1 | -module(beam_stats_state). |
| 2 | |
| 3 | -include("include/beam_stats.hrl"). |
| 4 | -include("include/beam_stats_process.hrl"). |
| 5 | -include("include/beam_stats_processes.hrl"). |
| 6 | |
| 7 | -export_type( |
| 8 | [ t/0 |
| 9 | ]). |
| 10 | |
| 11 | -export( |
| 12 | [ new/0 |
| 13 | , update/1 |
| 14 | , export/1 |
| 15 | ]). |
| 16 | |
| 17 | -record(snapshots, |
| 18 | { memory :: [{atom(), non_neg_integer()}] |
| 19 | , processes :: beam_stats_processes:t() |
| 20 | , run_queue :: non_neg_integer() |
| 21 | , ets :: beam_stats_ets:t() |
| 22 | }). |
| 23 | |
| 24 | -type snapshots() :: |
| 25 | #snapshots{}. |
| 26 | |
| 27 | -record(deltas, |
| 28 | { reductions :: non_neg_integer() |
| 29 | }). |
| 30 | |
| 31 | -type deltas() :: |
| 32 | #deltas{}. |
| 33 | |
| 34 | -record(totals, |
| 35 | { io_bytes_in :: non_neg_integer() |
| 36 | , io_bytes_out :: non_neg_integer() |
| 37 | , context_switches :: non_neg_integer() |
| 38 | }). |
| 39 | |
| 40 | -type totals() :: |
| 41 | #totals{}. |
| 42 | |
| 43 | -record(?MODULE, |
| 44 | { timestamp :: erlang:timestamp() |
| 45 | , node_id :: atom() |
| 46 | , snapshots :: snapshots() % Current state |
| 47 | , deltas :: deltas() % Accumulated since last check |
| 48 | , totals_previous :: totals() % Accumulated since VM start, as of last state |
| 49 | , totals_current :: totals() % Accumulated since VM start, as of this state |
| 50 | }). |
| 51 | |
| 52 | -define(T, #?MODULE). |
| 53 | |
| 54 | -opaque t() :: |
| 55 | ?T{}. |
| 56 | |
| 57 | -spec new() -> |
| 58 | t(). |
| 59 | new() -> |
| 60 | TotalsPrevious = totals_empty(), |
| 61 | new(TotalsPrevious). |
| 62 | |
| 63 | -spec new(TotalsPrevious :: totals()) -> |
| 64 | t(). |
| 65 | new(#totals{}=TotalsPrevious) -> |
| 66 | ?T |
| 67 | { timestamp = os:timestamp() |
| 68 | , node_id = erlang:node() |
| 69 | , snapshots = snapshots_new() |
| 70 | , deltas = deltas_new() |
| 71 | , totals_previous = TotalsPrevious |
| 72 | , totals_current = totals_new() |
| 73 | }. |
| 74 | |
| 75 | -spec update(t()) -> |
| 76 | t(). |
| 77 | update(?T{totals_current=TotalsPrevious}) -> |
| 78 | new(TotalsPrevious). |
| 79 | |
| 80 | -spec export(t()) -> |
| 81 | beam_stats:t(). |
| 82 | export( |
| 83 | ?T |
| 84 | { timestamp = Timestamp |
| 85 | , node_id = NodeID |
| 86 | , snapshots = |
| 87 | #snapshots |
| 88 | { memory = Memory |
| 89 | , processes = Processes |
| 90 | , run_queue = RunQueue |
| 91 | , ets = ETS |
| 92 | } |
| 93 | , deltas = |
| 94 | #deltas |
| 95 | { reductions = Reductions |
| 96 | } |
| 97 | , totals_previous = |
| 98 | #totals |
| 99 | { io_bytes_in = PreviousIOBytesIn |
| 100 | , io_bytes_out = PreviousIOBytesOut |
| 101 | , context_switches = PreviousContextSwitches |
| 102 | } |
| 103 | , totals_current = |
| 104 | #totals |
| 105 | { io_bytes_in = CurrentIOBytesIn |
| 106 | , io_bytes_out = CurrentIOBytesOut |
| 107 | , context_switches = CurrentContextSwitches |
| 108 | } |
| 109 | } |
| 110 | ) -> |
| 111 | #beam_stats |
| 112 | { timestamp = Timestamp |
| 113 | , node_id = NodeID |
| 114 | , memory = Memory |
| 115 | , io_bytes_in = CurrentIOBytesIn - PreviousIOBytesIn |
| 116 | , io_bytes_out = CurrentIOBytesOut - PreviousIOBytesOut |
| 117 | , context_switches = CurrentContextSwitches - PreviousContextSwitches |
| 118 | , reductions = Reductions |
| 119 | , run_queue = RunQueue |
| 120 | , ets = ETS |
| 121 | , processes = Processes |
| 122 | }. |
| 123 | |
| 124 | snapshots_new() -> |
| 125 | #snapshots |
| 126 | { memory = erlang:memory() |
| 127 | , processes = beam_stats_processes:collect() |
| 128 | , run_queue = erlang:statistics(run_queue) |
| 129 | , ets = beam_stats_ets:collect() |
| 130 | }. |
| 131 | |
| 132 | deltas_new() -> |
| 133 | {_ReductionsTotal, ReductionsDelta} = erlang:statistics(reductions), |
| 134 | #deltas |
| 135 | { reductions = ReductionsDelta |
| 136 | }. |
| 137 | |
| 138 | totals_new() -> |
| 139 | { {input , IOBytesIn} |
| 140 | , {output , IOBytesOut} |
| 141 | } = erlang:statistics(io), |
| 142 | {ContextSwitches, 0} = erlang:statistics(context_switches), |
| 143 | #totals |
| 144 | { io_bytes_in = IOBytesIn |
| 145 | , io_bytes_out = IOBytesOut |
| 146 | , context_switches = ContextSwitches |
| 147 | }. |
| 148 | |
| 149 | totals_empty() -> |
| 150 | #totals |
| 151 | { io_bytes_in = 0 |
| 152 | , io_bytes_out = 0 |
| 153 | , context_switches = 0 |
| 154 | }. |