Skip to content

Commit

Permalink
Warn when emulator is not optimised (#40)
Browse files Browse the repository at this point in the history
Warn when emulator is not optimised

Supported with OTP24 and above.
  • Loading branch information
max-au authored May 18, 2024
1 parent 4aab3af commit e2daef3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/erlperf.erl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@
-type system_information() :: #{
os := {unix | win32, atom()},
system_version := string(),
debug => boolean(), %% true if the emulator has been debug-compiled, otherwise false
emu_type => atom(), %% see system_info(emu_type), since OTP 24
emu_flavor => atom(), %% see system_info(emu_flavor), since OTP 24
dynamic_trace => atom(),%% see system_info(dynamic_trace), since OTP 24
cpu => string()
}.
%% System information, as returned by {@link erlang:system_info/1}
Expand Down Expand Up @@ -779,14 +783,21 @@ report_stats(Samples) ->

system_report() ->
OSType = erlang:system_info(os_type),
Guaranteed = #{
Guaranteed = detect_feature([emu_type, emu_flavor, dynamic_trace], #{
os => OSType,
system_version => string:trim(erlang:system_info(system_version), trailing)
},
}),
try Guaranteed#{cpu => string:trim(detect_cpu(OSType), both)}
catch _:_ -> Guaranteed
end.

detect_feature([], System) ->
System;
detect_feature([F | T], System) ->
try detect_feature(T, System#{F => erlang:system_info(F)})
catch error:badarg -> detect_feature(T, System)
end.

detect_cpu({unix, freebsd}) ->
os:cmd("sysctl -n hw.model");
detect_cpu({unix, darwin}) ->
Expand Down
19 changes: 14 additions & 5 deletions src/erlperf_cli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -372,23 +372,23 @@ color(info, Text) -> Text.

%% Report formatter
format_report(full, [#{system := System} | _] = Reports, Width) ->
[format_system(System), format_report(extended, Reports, Width)];
warn_system(System) ++ [format_system(System), format_report(extended, Reports, Width)];

format_report(extended, Reports, Width) ->
format_report(extended, [#{system := System} | _] = Reports, Width) ->
Sorted = sort_by(Reports),
#{result := #{average := MaxAvg}} = hd(Sorted),
Header = ["Code", " ||", " Samples", " Avg", " StdDev", " Median", " P99", " Iteration", " Rel"],
Data = [format_report_line(MaxAvg, ReportLine, extended) || ReportLine <- Sorted],
format_table(remove_relative_column([Header | Data]), Width);
warn_system(System) ++ format_table(remove_relative_column([Header | Data]), Width);

format_report(basic, Reports, Width) ->
format_report(basic, [#{system := System} | _] = Reports, Width) ->
Sorted = sort_by(Reports),
#{result := #{average := MaxAvg}} = hd(Sorted),
Header = ["Code", " ||", " QPS", " Time", " Rel"],
Data0 = [format_report_line(MaxAvg, ReportLine, basic) || ReportLine <- Sorted],
%% remove columns that should not be displayed in basic mode
Data = [[C1, C2, C3, C4, C5] || [C1, C2, _, C3, _, _, _, C4, C5] <- Data0],
format_table(remove_relative_column([Header | Data]), Width).
warn_system(System) ++ format_table(remove_relative_column([Header | Data]), Width).

sort_by([#{mode := timed} | _] = Reports) ->
lists:sort(fun (#{result := #{average := L}}, #{result := #{average := R}}) -> L < R end, Reports);
Expand Down Expand Up @@ -475,6 +475,15 @@ format_code(Code) when is_list(Code) ->
format_code(Code) when is_binary(Code) ->
binary_to_list(Code).

warn_system(#{dynamic_trace := Trace} = System) when Trace =/= none ->
[io_lib:format("WARNING: Dynamic Trace Probes enabled (~s detected)~n", [Trace]) | warn_system(maps:remove(dynamic_trace, System))];
warn_system(#{emu_type := Type} = System) when Type =/= opt ->
[io_lib:format("WARNING: Emulator is not optimised (~s detected)~n", [Type]) | warn_system(maps:remove(emu_type, System))];
warn_system(#{emu_flavor := Flavor} = System) when Flavor =/= jit ->
[io_lib:format("WARNING: Emulator is not JIT (~s detected)~n", [Flavor]) | warn_system(maps:remove(emu_flavor, System))];
warn_system(_) ->
[].

format_system(#{os := OSType, system_version := SystemVsn} = System) ->
OS = io_lib:format("OS : ~s~n", [format_os(OSType)]),
CPU = if is_map_key(cpu, System) -> io_lib:format("CPU: ~s~n", [maps:get(cpu, System)]); true -> "" end,
Expand Down

0 comments on commit e2daef3

Please sign in to comment.