Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to snoop commands to control spawning separate process vs eval'ing in existing process #252

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions SnoopCompileCore/src/SnoopCompileCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ if VERSION >= v"1.6.0-DEV.1192" # https://github.com/JuliaLang/julia/pull/37136
include("snoopl.jl")
end

if VERSION >= v"1.6.0-DEV.1192" # https://github.com/JuliaLang/julia/pull/37136
include("snoop_all.jl")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just include this macro into SnoopCompileCore/src/snoopl.jl? It's the same condition for including it that we use for@snoopl.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good idea. I've just moved it into the same if-block. I guess it doesn't necessarily belong in snoopl.jl because it will snoop all three types of compilation, which is why I put it in its own file.

Can you have another look and let me know if this looks okay? :)

end

end
11 changes: 11 additions & 0 deletions SnoopCompileCore/src/snoop_all.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TODO: Add `@snoopc` into the mix
macro snoop_all(csv_f, yaml_f, commands)
esc(quote
v = @snoopi_deep begin
@snoopl pspawn=false $csv_f $yaml_f begin
$commands
end
end;
v
end)
end
44 changes: 31 additions & 13 deletions SnoopCompileCore/src/snoopl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Serialization

"""
```
@snoopl "func_names.csv" "llvm_timings.yaml" begin
@snoopl [jlflags="..."] [pspawn=true] "func_names.csv" "llvm_timings.yaml" begin
# Commands to execute, in a new process
end
```
Expand All @@ -14,16 +14,26 @@ be used for the input to `SnoopCompile.read_snoopl("func_names.csv", "llvm_timin

The logs contain the amount of time spent optimizing each "llvm module", and information
about each module, where a module is a collection of functions being optimized together.

If `pspawn=false`, the commands will be run in the same julia process, via `eval()` in
the current module. This will only report LLVM optimizations for _new_ compilations, that
haven't already been cached in this process, which can be (carefully) used to pruned down
the results to only the code you are interested in.
"""
macro snoopl(flags, func_file, llvm_file, commands)
return :(snoopl($(esc(flags)), $(esc(func_file)), $(esc(llvm_file)), $(QuoteNode(commands))))
macro snoopl(args...)
@assert length(args) >= 3 """Usage: @snoopl [args...] "snoopl.csv" "snoopl.yaml" commands"""
flags, (func_file, llvm_file, commands) = args[1:end-3], args[end-2:end]
flags = [esc(e) for e in flags]
return :(snoopl($(esc(func_file)), $(esc(llvm_file)), $(QuoteNode(commands)), $__module__; $(flags...)))
end
macro snoopl(func_file, llvm_file, commands)
return :(snoopl(String[], $(esc(func_file)), $(esc(llvm_file)), $(QuoteNode(commands))))
return :(snoopl($(esc(func_file)), $(esc(llvm_file)), $(QuoteNode(commands)), $__module__))
end

function snoopl(flags, func_file, llvm_file, commands)
println("Launching new julia process to run commands...")
function snoopl(func_file, llvm_file, commands, _module; pspawn=true, jlflags="")
if pspawn
println("Launching new julia process to run commands...")
end
# addprocs will run the unmodified version of julia, so we
# launch it as a command.
code_object = """
Expand All @@ -32,23 +42,31 @@ function snoopl(flags, func_file, llvm_file, commands)
Core.eval(Main, deserialize(stdin))
end
"""
process = open(`$(Base.julia_cmd()) $flags --eval $code_object`, stdout, write=true)
serialize(process, quote
record_and_run_quote = quote
let func_io = open($func_file, "w"), llvm_io = open($llvm_file, "w")
ccall(:jl_dump_emitted_mi_name, Nothing, (Ptr{Nothing},), func_io.handle)
ccall(:jl_dump_llvm_opt, Nothing, (Ptr{Nothing},), llvm_io.handle)
try
$commands
@eval $commands
finally
ccall(:jl_dump_emitted_mi_name, Nothing, (Ptr{Nothing},), C_NULL)
ccall(:jl_dump_llvm_opt, Nothing, (Ptr{Nothing},), C_NULL)
close(func_io)
close(llvm_io)
end
end
exit()
end)
wait(process)
println("done.")
end

if pspawn
process = open(`$(Base.julia_cmd()) $jlflags --eval $code_object`, stdout, write=true)
serialize(process, quote
$record_and_run_quote
exit()
end)
wait(process)
println("done.")
else
Core.eval(_module, record_and_run_quote)
end
nothing
end