X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=src%2Fbeam_stats_state.erl;fp=src%2Fbeam_stats_state.erl;h=552b5b15f859492a46f6b852493f1ffcb367eb02;hb=b4e2333fc5fd9f32c8a0a39db4c6faacdbb15a91;hp=0000000000000000000000000000000000000000;hpb=263d0e35581f65a71c7a5ffeecfd25543e48b862;p=beam_stats.git diff --git a/src/beam_stats_state.erl b/src/beam_stats_state.erl new file mode 100644 index 0000000..552b5b1 --- /dev/null +++ b/src/beam_stats_state.erl @@ -0,0 +1,85 @@ +-module(beam_stats_state). + +-include("include/beam_stats.hrl"). + +-export_type( + [ t/0 + ]). + +-export( + [ new/0 + , update/1 + , export/1 + ]). + +-record(?MODULE, + { timestamp :: erlang:timestamp() + , node_id :: atom() + , memory :: [{atom(), non_neg_integer()}] + , 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() + }). + +-define(T, #?MODULE). + +-opaque t() :: + ?T{}. + +-spec new() -> + t(). +new() -> + { {input , CurrentIOBytesIn} + , {output , CurrentIOBytesOut} + } = erlang:statistics(io), + ?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 + }. + +-spec update(t()) -> + t(). +update(?T + { previous_io_bytes_in = PreviousIOBytesIn + , previous_io_bytes_out = PreviousIOBytesOut + } +) -> + { {input , CurrentIOBytesIn} + , {output , CurrentIOBytesOut} + } = erlang:statistics(io), + ?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 + }. + +-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 + } +) -> + #beam_stats + { timestamp = Timestamp + , node_id = NodeID + , memory = Memory + , io_bytes_in = CurrentIOBytesIn - PreviousIOBytesIn + , io_bytes_out = CurrentIOBytesOut - PreviousIOBytesOut + }.