Skip to content

Commit 705407b

Browse files
committed
Share profiling code in ParallelCompiler
1 parent 63aa570 commit 705407b

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed

lib/elixir/lib/kernel/parallel_compiler.ex

+28-33
Original file line numberDiff line numberDiff line change
@@ -366,32 +366,33 @@ defmodule Kernel.ParallelCompiler do
366366
end
367367

368368
defp maybe_check_modules(result, runtime_modules, state) do
369-
%{profile: profile, checker: checker} = state
370-
371369
compiled_modules =
372370
for {{:module, module}, binary} when is_binary(binary) <- result,
373371
do: module
374372

375-
profile_checker(profile, compiled_modules, runtime_modules, fn ->
376-
Module.ParallelChecker.verify(checker, runtime_modules)
377-
end)
373+
profile(
374+
state,
375+
fn ->
376+
num_modules = length(compiled_modules) + length(runtime_modules)
377+
"group pass check of #{num_modules} modules"
378+
end,
379+
fn -> Module.ParallelChecker.verify(state.checker, runtime_modules) end
380+
)
378381
end
379382

380383
defp profile_init(:time), do: {:time, System.monotonic_time(), 0}
381384
defp profile_init(nil), do: :none
382385

383-
defp profile_checker({:time, _, _}, compiled_modules, runtime_modules, fun) do
386+
defp profile(%{profile: :none}, _what, fun), do: fun.()
387+
388+
defp profile(%{profile: {:time, _, _}}, what, fun) do
384389
{time, result} = :timer.tc(fun)
385390
time = div(time, 1000)
386-
num_modules = length(compiled_modules) + length(runtime_modules)
387-
IO.puts(:stderr, "[profile] Finished group pass check of #{num_modules} modules in #{time}ms")
391+
what = if is_binary(what), do: what, else: what.()
392+
IO.puts(:stderr, "[profile] Finished #{what} in #{time}ms")
388393
result
389394
end
390395

391-
defp profile_checker(:none, _compiled_modules, _runtime_modules, fun) do
392-
fun.()
393-
end
394-
395396
## Compiler worker spawning
396397

397398
# We already have n=schedulers currently running, don't spawn new ones
@@ -475,7 +476,10 @@ defmodule Kernel.ParallelCompiler do
475476
end)
476477

477478
[] = files
478-
cycle_return = measure_timing(state, "each_cycle callback", fn -> each_cycle_return(state.each_cycle.()) end)
479+
480+
cycle_return =
481+
profile(state, "cycle resolution", fn -> each_cycle_return(state.each_cycle.()) end)
482+
479483
state = cycle_timing(result, state)
480484

481485
case cycle_return do
@@ -519,23 +523,23 @@ defmodule Kernel.ParallelCompiler do
519523
# Finally, note there is no difference between hard and raise, the
520524
# difference is where the raise is happening, inside the compiler
521525
# or in the caller.
522-
measure_timing(state, "spawn_workers/8 in potential deadlock situation", fn ->
523-
waiting_list = Map.to_list(waiting)
526+
deadlocked =
527+
profile(state, "deadlock resolution", fn ->
528+
waiting_list = Map.to_list(waiting)
524529

525-
deadlocked =
526530
deadlocked(waiting_list, :soft, false) ||
527531
deadlocked(waiting_list, :soft, true) ||
528532
deadlocked(waiting_list, :hard, false) ||
529533
without_definition(waiting_list, files)
534+
end)
530535

531-
if deadlocked do
532-
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, errors, state)
533-
else
534-
return_error(warnings, errors, state, fn ->
535-
handle_deadlock(waiting, files)
536-
end)
537-
end
538-
end)
536+
if deadlocked do
537+
spawn_workers(deadlocked, spawned, waiting, files, result, warnings, errors, state)
538+
else
539+
return_error(warnings, errors, state, fn ->
540+
handle_deadlock(waiting, files)
541+
end)
542+
end
539543
end
540544

541545
# No more queue, but spawned and map_size(waiting) do not match
@@ -560,15 +564,6 @@ defmodule Kernel.ParallelCompiler do
560564
end
561565
end
562566

563-
defp measure_timing(%{profile: :none}, _what, fun), do: fun.()
564-
defp measure_timing(%{profile: {:time, _, _}}, what, fun) do
565-
{time, result} = :timer.tc(fun)
566-
time = div(time, 1000)
567-
568-
IO.puts(:stderr, "[profile] Executed #{what} in #{time}ms")
569-
result
570-
end
571-
572567
defp cycle_timing(_result, %{profile: :none} = state) do
573568
state
574569
end

0 commit comments

Comments
 (0)