Skip to content

Instantly share code, notes, and snippets.

@slfritchie
Created November 27, 2013 14:24
Show Gist options
  • Save slfritchie/007b399675fb76f2d4f0 to your computer and use it in GitHub Desktop.
Save slfritchie/007b399675fb76f2d4f0 to your computer and use it in GitHub Desktop.
Report every `Interval` seconds the count of `init()` function calls for the following Riak KV FSM modules: riak_kv_buckets_fsm, riak_kv_exchange_fsm, riak_kv_get_fsm, riak_kv_index_fsm, riak_kv_keys_fsm, riak_kv_put_fsm
%% Stolen from: https://gist.github.com/2656289
-module(fsm_init_tracer).
-compile(export_all).
start() ->
start(1*1000).
start(Interval) ->
%%% Count the get, put, buckets, keys, exchange, and index FSM init() calls
dbg:tracer(process, {fun trace/2, new_stats()}),
dbg:p(all, call),
[dbg:tpl(Mod, init, 1, [{'_', [], []}]) ||
Mod <- [riak_kv_buckets_fsm, riak_kv_exchange_fsm, riak_kv_get_fsm, riak_kv_index_fsm, riak_kv_keys_fsm, riak_kv_put_fsm]],
dbg:tpl(riak_kv_put_fsm, start_link, 3, [{'_', [], []}]),
%% Don't need return_trace events for this use case, but here's
%% how to do it if needed.
%%dbg:tpl(bitcask, merge_single_entry, 6, [{'_', [], [{return_trace}]}]).
{ok, TPid} = dbg:get_tracer(),
io:format("Tracer pid: ~p, use ~p:stop() to stop\n", [TPid, ?MODULE]),
timer:send_interval(Interval, TPid, print_report),
{started, TPid}.
stop() ->
dbg:stop_clear(),
catch exit(element(2,dbg:get_tracer()), kill),
stopped.
trace({trace, _Pid, call, {riak_kv_put_fsm, start_link, _}},
{Pstart_link, R, P, B, E, I, K}) ->
{Pstart_link+1, R, P, B, E, I, K};
trace({trace, _Pid, call, {riak_kv_get_fsm, init, _}},
{Pstart_link, R, P, B, E, I, K}) ->
{Pstart_link, R+1, P, B, E, I, K};
trace({trace, _Pid, call, {riak_kv_put_fsm, init, _}},
{Pstart_link, R, P, B, E, I, K}) ->
{Pstart_link, R, P+1, B, E, I, K};
trace({trace, _Pid, call, {riak_kv_buckets_fsm, init, _}},
{Pstart_link, R, P, B, E, I, K}) ->
{Pstart_link, R, P, B+1, E, I, K};
trace({trace, _Pid, call, {riak_kv_exchange_fsm, init, _}},
{Pstart_link, R, P, B, E, I, K}) ->
{Pstart_link, R, P, B, E+1, I, K};
trace({trace, _Pid, call, {riak_kv_index_fsm, init, _}},
{Pstart_link, R, P, B, E, I, K}) ->
{Pstart_link, R, P, B, E, I+1, K};
trace({trace, _Pid, call, {riak_kv_keys_fsm, init, _}},
{Pstart_link, R, P, B, E, I, K}) ->
{Pstart_link, R, P, B, E, I, K+1};
trace(print_report, Stats) ->
print_stats(Stats),
new_stats();
trace(Unknown, Stats) ->
erlang:display(wha),
io:format("Unknown! ~P\n", [Unknown, 20]),
Stats.
new_stats() ->
{0,
0, 0, 0, 0, 0, 0}.
print_stats({_Pstart_link, _Read, _Put, _Buckets, _Exchange, _Index, _Keys} = Stats) ->
io:format("~p ~p: ~p\n", [date(), time(), Stats]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment