X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=src%2Fbeam_stats_state.erl;h=d514f95f8a98342299d622c55c5ae0b091bed9b7;hb=6c4d94fd5465a1952a9fa28a21fd264226487945;hp=1fff6b0d77c98c637623d69d5c70e7477a77e713;hpb=deefeb3c22860ae87538cdbde852a3f8ba0ee7c0;p=beam_stats.git diff --git a/src/beam_stats_state.erl b/src/beam_stats_state.erl index 1fff6b0..d514f95 100644 --- a/src/beam_stats_state.erl +++ b/src/beam_stats_state.erl @@ -1,6 +1,8 @@ -module(beam_stats_state). -include("include/beam_stats.hrl"). +-include("include/beam_stats_process.hrl"). +-include("include/beam_stats_processes.hrl"). -export_type( [ t/0 @@ -12,21 +14,39 @@ , export/1 ]). --record(?MODULE, - { timestamp :: erlang:timestamp() - , node_id :: atom() - , memory :: [{atom(), non_neg_integer()}] +-record(snapshots, + { memory :: [{atom(), non_neg_integer()}] + , processes :: beam_stats_processes:t() + , run_queue :: non_neg_integer() + , ets :: beam_stats_ets:t() + }). + +-type snapshots() :: + #snapshots{}. - , previous_io_bytes_in :: non_neg_integer() - , previous_io_bytes_out :: non_neg_integer() - , current_io_bytes_in :: non_neg_integer() - , current_io_bytes_out :: non_neg_integer() +-record(deltas, + { reductions :: non_neg_integer() + }). - , previous_context_switches :: non_neg_integer() - , current_context_switches :: non_neg_integer() +-type deltas() :: + #deltas{}. - , reductions :: non_neg_integer() - , run_queue :: non_neg_integer() +-record(totals, + { io_bytes_in :: non_neg_integer() + , io_bytes_out :: non_neg_integer() + , context_switches :: non_neg_integer() + }). + +-type totals() :: + #totals{}. + +-record(?MODULE, + { timestamp :: erlang:timestamp() + , node_id :: atom() + , snapshots :: snapshots() % Current state + , deltas :: deltas() % Accumulated since last check + , totals_previous :: totals() % Accumulated since VM start, as of last state + , totals_current :: totals() % Accumulated since VM start, as of this state }). -define(T, #?MODULE). @@ -37,78 +57,98 @@ -spec new() -> t(). new() -> - { {input , CurrentIOBytesIn} - , {output , CurrentIOBytesOut} - } = erlang:statistics(io), - {CurrentContextSwitches, 0} = erlang:statistics(context_switches), - {_ReductionsTotal, ReductionsDelta} = erlang:statistics(reductions), - RunQueue = erlang:statistics(run_queue), + TotalsPrevious = totals_empty(), + new(TotalsPrevious). + +-spec new(TotalsPrevious :: totals()) -> + t(). +new(#totals{}=TotalsPrevious) -> ?T - { timestamp = os:timestamp() - , node_id = erlang:node() - , memory = erlang:memory() - , previous_io_bytes_in = 0 - , previous_io_bytes_out = 0 - , current_io_bytes_in = CurrentIOBytesIn - , current_io_bytes_out = CurrentIOBytesOut - , previous_context_switches = 0 - , current_context_switches = CurrentContextSwitches - , reductions = ReductionsDelta - , run_queue = RunQueue + { timestamp = beam_stats_source:os_timestamp() + , node_id = beam_stats_source:erlang_node() + , snapshots = snapshots_new() + , deltas = deltas_new() + , totals_previous = TotalsPrevious + , totals_current = totals_new() }. -spec update(t()) -> t(). -update(?T - { previous_io_bytes_in = PreviousIOBytesIn - , previous_io_bytes_out = PreviousIOBytesOut - , previous_context_switches = PreviousContextSwitches - } -) -> - { {input , CurrentIOBytesIn} - , {output , CurrentIOBytesOut} - } = erlang:statistics(io), - {CurrentContextSwitches, 0} = erlang:statistics(context_switches), - {_ReductionsTotal, ReductionsDelta} = erlang:statistics(reductions), - RunQueue = erlang:statistics(run_queue), - ?T - { timestamp = os:timestamp() - , node_id = erlang:node() - , memory = erlang:memory() - , previous_io_bytes_in = PreviousIOBytesIn - , previous_io_bytes_out = PreviousIOBytesOut - , current_io_bytes_in = CurrentIOBytesIn - , current_io_bytes_out = CurrentIOBytesOut - , previous_context_switches = PreviousContextSwitches - , current_context_switches = CurrentContextSwitches - , reductions = ReductionsDelta - , run_queue = RunQueue - }. +update(?T{totals_current=TotalsPrevious}) -> + new(TotalsPrevious). -spec export(t()) -> beam_stats:t(). export( ?T - { timestamp = Timestamp - , node_id = NodeID - , memory = Memory - , previous_io_bytes_in = PreviousIOBytesIn - , previous_io_bytes_out = PreviousIOBytesOut - , current_io_bytes_in = CurrentIOBytesIn - , current_io_bytes_out = CurrentIOBytesOut - , previous_context_switches = PreviousContextSwitches - , current_context_switches = CurrentContextSwitches - , reductions = Reductions - , run_queue = RunQueue + { timestamp = Timestamp + , node_id = NodeID + , snapshots = + #snapshots + { memory = Memory + , processes = Processes + , run_queue = RunQueue + , ets = ETS + } + , deltas = + #deltas + { reductions = Reductions + } + , totals_previous = + #totals + { io_bytes_in = PreviousIOBytesIn + , io_bytes_out = PreviousIOBytesOut + , context_switches = PreviousContextSwitches + } + , totals_current = + #totals + { io_bytes_in = CurrentIOBytesIn + , io_bytes_out = CurrentIOBytesOut + , context_switches = CurrentContextSwitches + } } ) -> #beam_stats - { timestamp = Timestamp - , node_id = NodeID - , memory = Memory - , io_bytes_in = CurrentIOBytesIn - PreviousIOBytesIn - , io_bytes_out = CurrentIOBytesOut - PreviousIOBytesOut + { timestamp = Timestamp + , node_id = NodeID + , memory = Memory + , io_bytes_in = CurrentIOBytesIn - PreviousIOBytesIn + , io_bytes_out = CurrentIOBytesOut - PreviousIOBytesOut , context_switches = CurrentContextSwitches - PreviousContextSwitches , reductions = Reductions , run_queue = RunQueue + , ets = ETS + , processes = Processes + }. + +snapshots_new() -> + #snapshots + { memory = beam_stats_source:erlang_memory() + , processes = beam_stats_processes:collect() + , run_queue = beam_stats_source:erlang_statistics(run_queue) + , ets = beam_stats_ets:collect() + }. + +deltas_new() -> + {_ReductionsTotal, ReductionsDelta} = beam_stats_source:erlang_statistics(reductions), + #deltas + { reductions = ReductionsDelta + }. + +totals_new() -> + { {input , IOBytesIn} + , {output , IOBytesOut} + } = beam_stats_source:erlang_statistics(io), + {ContextSwitches, 0} = beam_stats_source:erlang_statistics(context_switches), + #totals + { io_bytes_in = IOBytesIn + , io_bytes_out = IOBytesOut + , context_switches = ContextSwitches + }. + +totals_empty() -> + #totals + { io_bytes_in = 0 + , io_bytes_out = 0 + , context_switches = 0 }.