File 7511-Add-call_time-call_memory-profiling-to-erlc.patch of Package erlang

From a757fbc6956b6511d220accbe9c7b66d86fa3e40 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Valim?= <jose.valim@dashbit.co>
Date: Sun, 4 Feb 2024 10:44:30 +0100
Subject: [PATCH] Add call_time/call_memory profiling to erlc

eprof is still supported so it can be used
as a reference for improvements and mismatches.
---
 lib/compiler/src/compile.erl        | 33 +++++++++++++++++++++++++----
 lib/compiler/test/compile_SUITE.erl |  2 ++
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/lib/compiler/src/compile.erl b/lib/compiler/src/compile.erl
index 05bed940d7..180ae3dbc3 100644
--- a/lib/compiler/src/compile.erl
+++ b/lib/compiler/src/compile.erl
@@ -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) ->
@@ -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
diff --git a/lib/compiler/test/compile_SUITE.erl b/lib/compiler/test/compile_SUITE.erl
index 8093b731c3..570a541ad4 100644
--- a/lib/compiler/test/compile_SUITE.erl
+++ b/lib/compiler/test/compile_SUITE.erl
@@ -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),
-- 
2.35.3

openSUSE Build Service is sponsored by