Skip to content

Instantly share code, notes, and snippets.

@slfritchie
Created November 27, 2013 14:16
Show Gist options
  • Save slfritchie/23121803af63d76eeca5 to your computer and use it in GitHub Desktop.
Save slfritchie/23121803af63d76eeca5 to your computer and use it in GitHub Desktop.
read_bin_trace_file.erl, a small example of reading a binary-formatted Erlang trace file and doing something simple with it.
-module(read_bin_trace_file).
-compile(export_all).
read(Path) ->
read(Path, 1).
read(Path, LatencyMS) ->
{ok, FH} = file:open(Path, [read, binary, raw]),
read(file:read(FH, 5), FH, LatencyMS, []).
read(eof, _FH, _, _) ->
ok;
read({ok, <<Size:40>>}, FH, LatencyMS, Hist) ->
{ok, Bin} = file:read(FH, Size),
case binary_to_term(Bin) of
{trace_ts, _, call, {M,F,A}, Time} ->
%%io:format("call MFA = ~p:~p/~p, ", [M, F, length(A)]),
read(file:read(FH, 5), FH, LatencyMS, [{{M,F,length(A)}, Time, A}|Hist]);
{trace_ts, _, return_from, MFA, Res, EndTime} ->
%%io:format("MFA ~p Hist ~p\n", [MFA, Hist]),
try
{value, {_, StartTime, A}, NewHist} = lists:keytake(MFA, 1, Hist),
MSec = timer:now_diff(EndTime, StartTime)/1000,
if MSec > LatencyMS ->
io:format("~p ~p msec\nArgs: (~p/~p) ~P\nRes: ~P\n\n",
[MFA, MSec,
erts_debug:flat_size(A), erts_debug:size(A),
A, 20, Res, 20]);
true ->
ok
end,
read(file:read(FH, 5), FH, LatencyMS, NewHist)
catch
error:{badmatch,false} ->
read(file:read(FH, 5), FH, LatencyMS, Hist);
X:Y ->
io:format("ERR ~p ~p @ ~p\n", [X, Y, erlang:get_stacktrace()]),
read(file:read(FH, 5), FH, LatencyMS, Hist)
end
end.
%% read(eof, _FH) ->
%% ok;
%% read({ok, <<Size:40>>}, FH) ->
%% {ok, Bin} = file:read(FH, Size),
%% io:format("~P\n", [binary_to_term(Bin), 15]),
%% read(file:read(FH, 5), FH).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment