Skip to content

Commit

Permalink
Merge pull request erlang#8085 from josevalim/jv-tprof-compiler
Browse files Browse the repository at this point in the history
Add call_time/call_memory profiling to erlc
  • Loading branch information
jhogberg authored Feb 8, 2024
2 parents 6c527b5 + a757fbc commit ddabd60
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
33 changes: 29 additions & 4 deletions lib/compiler/src/compile.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1201,13 +1201,27 @@ runner(#compile{options=Opts}) ->
false ->
Run0
end,
case keyfind(eprof, 1, Opts) of
{eprof,EprofPass} ->
case keyfind(call_time, 1, Opts) of
{call_time,Pass} ->
fun(P, Code, St) ->
run_eprof(P, Code, EprofPass, St)
run_tprof(P, Code, Pass, call_time, St)
end;
false ->
Run1
case keyfind(call_memory, 1, Opts) of
{call_memory,Pass} ->
fun(P, Code, St) ->
run_tprof(P, Code, Pass, call_memory, St)
end;
false ->
case keyfind(eprof, 1, Opts) of
{eprof,EprofPass} ->
fun(P, Code, St) ->
run_eprof(P, Code, EprofPass, St)
end;
false ->
Run1
end
end
end.

run_tc({Name,Fun}, Code, St) ->
Expand Down Expand Up @@ -1271,6 +1285,17 @@ run_eprof({Name,Fun}, Code, Name, St) ->
run_eprof({_,Fun}, Code, _, St) ->
Fun(Code, St).

run_tprof({Name,Fun}, Code, Name, Measurement, St) ->
io:format("~p: Profiling ~ts\n", [Name, Measurement]),
Opts = #{type => Measurement, report => return},
Args = [erlang, apply, [Fun, [Code, St]], Opts],
{Result, ProfileData} = c:appcall(tools, tprof, profile, Args),
InspectData = c:appcall(tools, tprof, inspect, [ProfileData]),
c:appcall(tools, tprof, format, [InspectData]),
Result;
run_tprof({_,Fun}, Code, _, _, St) ->
Fun(Code, St).

comp_ret_ok(Code, #compile{warnings=Warn0,module=Mod,options=Opts}=St) ->
Warn1 = filter_warnings(Warn0, Opts),
case werror(St) of
Expand Down
2 changes: 2 additions & 0 deletions lib/compiler/test/compile_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ file_1(Config) when is_list(Config) ->

{ok,simple} = compile:file(Simple, [no_line_info]), %Coverage
{ok,simple} = compile:file(Simple, [{eprof,beam_z}]), %Coverage
{ok,simple} = compile:file(Simple, [{call_time,beam_z}]), %Coverage
{ok,simple} = compile:file(Simple, [{call_memory,beam_z}]), %Coverage

%% Cover option not in a list (undocumented feature).
{ok,simple} = compile:file(Simple, no_postopt),
Expand Down

0 comments on commit ddabd60

Please sign in to comment.