diff --git a/SnoopCompileCore/src/snoopi_deep.jl b/SnoopCompileCore/src/snoopi_deep.jl index 3719cf84..563b9c9f 100644 --- a/SnoopCompileCore/src/snoopi_deep.jl +++ b/SnoopCompileCore/src/snoopi_deep.jl @@ -1,3 +1,7 @@ +const have_clear_and_fetch_timings = isdefined(Core.Compiler, :Timings) && isdefined(Core.Compiler.Timings, :clear_and_fetch_timings) + +using Core.Compiler.Timings: ROOT, ROOTmi, InferenceFrameInfo, Timing + struct InferenceTiming mi_info::Core.Compiler.Timings.InferenceFrameInfo inclusive_time::Float64 @@ -70,17 +74,34 @@ function addchildren!(parent::InferenceTimingNode, t::Core.Compiler.Timings.Timi end end -function start_deep_timing() - Core.Compiler.Timings.reset_timings() - Core.Compiler.__set_measure_typeinf(true) -end -function stop_deep_timing() - Core.Compiler.__set_measure_typeinf(false) - Core.Compiler.Timings.close_current_timer() -end - -function finish_snoopi_deep() - return InferenceTimingNode(Core.Compiler.Timings._timings[1]) +if have_clear_and_fetch_timings + function start_deep_timing() + Core.Compiler.__set_measure_typeinf(true) + end + function stop_deep_timing() + Core.Compiler.__set_measure_typeinf(false) + end + function finish_snoopi_deep() + # Construct a dummy Timing for ROOT(), for backwards compatibility with the old API. + root = Timing( + InferenceFrameInfo(ROOTmi, 0x0, Any[], Any[Core.Const(ROOT)], 1), + 0x0) + root.children = Core.Compiler.Timings.clear_and_fetch_timings() + + return InferenceTimingNode(root) + end +else + function start_deep_timing() + Core.Compiler.Timings.reset_timings() + Core.Compiler.__set_measure_typeinf(true) + end + function stop_deep_timing() + Core.Compiler.__set_measure_typeinf(false) + Core.Compiler.Timings.close_current_timer() + end + function finish_snoopi_deep() + return InferenceTimingNode(Core.Compiler.Timings._timings[1]) + end end function _snoopi_deep(cmd::Expr) diff --git a/SnoopPrecompile/src/SnoopPrecompile.jl b/SnoopPrecompile/src/SnoopPrecompile.jl index d0dcd6b3..3f128287 100644 --- a/SnoopPrecompile/src/SnoopPrecompile.jl +++ b/SnoopPrecompile/src/SnoopPrecompile.jl @@ -5,6 +5,7 @@ export @precompile_all_calls, @precompile_setup const verbose = Ref(false) # if true, prints all the precompiles const have_inference_tracking = isdefined(Core.Compiler, :__set_measure_typeinf) const have_force_compile = isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("#@force_compile")) +const have_clear_and_fetch_timings = isdefined(Core.Compiler, :Timings) && isdefined(Core.Compiler.Timings, :clear_and_fetch_timings) function precompile_roots(roots) @assert have_inference_tracking @@ -59,16 +60,29 @@ macro precompile_all_calls(ex::Expr) end end if have_inference_tracking - ex = quote - Core.Compiler.Timings.reset_timings() - Core.Compiler.__set_measure_typeinf(true) - try - $ex - finally - Core.Compiler.__set_measure_typeinf(false) - Core.Compiler.Timings.close_current_timer() + if have_clear_and_fetch_timings + # use new thread-safe timings API if it's available in this version of Julia + ex = quote + Core.Compiler.__set_measure_typeinf(true) + try + $ex + finally + Core.Compiler.__set_measure_typeinf(false) + end + $SnoopPrecompile.precompile_roots(Core.Compiler.Timings.clear_and_fetch_timings()) + end + else + ex = quote + Core.Compiler.Timings.reset_timings() + Core.Compiler.__set_measure_typeinf(true) + try + $ex + finally + Core.Compiler.__set_measure_typeinf(false) + Core.Compiler.Timings.close_current_timer() + end + $SnoopPrecompile.precompile_roots(Core.Compiler.Timings._timings[1].children) end - $SnoopPrecompile.precompile_roots(Core.Compiler.Timings._timings[1].children) end end return esc(quote