/
bigwig_stats_sender.erl
71 lines (54 loc) · 2.11 KB
/
bigwig_stats_sender.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
%% Periodically broadcasts stats about the VM to clients
%%
-module(bigwig_stats_sender).
-behaviour(gen_server).
-define(SERVER, ?MODULE).
-define(INTERVAL, 2000).
%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------
-export([start_link/0]).
%% ------------------------------------------------------------------
%% gen_server Function Exports
%% ------------------------------------------------------------------
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-record(state, {}).
%% ------------------------------------------------------------------
%% API Function Definitions
%% ------------------------------------------------------------------
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%% ------------------------------------------------------------------
%% gen_server Function Definitions
%% ------------------------------------------------------------------
init([]) ->
timer:send_interval(?INTERVAL, calculate),
{ok, #state{}}.
handle_call(_Request, _From, State) ->
{noreply, ok, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(calculate, State) ->
Msg = {bigwig_stats_sender, calc_stats()},
bigwig_pubsubhub:notify(Msg),
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% ------------------------------------------------------------------
%% Internal Function Definitions
%% ------------------------------------------------------------------
%% Generate a proplist of jsx_to_term compatible data about VM state
calc_stats() ->
{{input, InBytes}, {output, OutBytes}} = erlang:statistics(io),
{RedsTot, _RedsDiff} = erlang:statistics(reductions),
{Uptime , _} = erlang:statistics(wall_clock),
[
{process_count, erlang:system_info(process_count)},
{reductions, RedsTot},
{bytes_in, InBytes},
{bytes_out, OutBytes},
{run_queue, erlang:statistics(run_queue)},
{uptime, Uptime}
].