Skip to content

Commit 255162c

Browse files
Precompile the @time_imports printing so it doesn't confuse reports (#55729)
Makes functions for the report printing that can be precompiled into the sysimage.
1 parent bee75f7 commit 255162c

File tree

2 files changed

+66
-37
lines changed

2 files changed

+66
-37
lines changed

base/loading.jl

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,22 +1254,9 @@ function _include_from_serialized(pkg::PkgId, path::String, ocachepath::Union{No
12541254
if parentmodule(M) === M && PkgId(M) == pkg
12551255
register && register_root_module(M)
12561256
if timing_imports
1257-
elapsed = round((time_ns() - t_before) / 1e6, digits = 1)
1257+
elapsed_time = time_ns() - t_before
12581258
comp_time, recomp_time = cumulative_compile_time_ns() .- t_comp_before
1259-
print(lpad(elapsed, 9), " ms ")
1260-
ext_parent = extension_parent_name(M)
1261-
if ext_parent !== nothing
1262-
print(ext_parent::String, "")
1263-
end
1264-
print(pkg.name)
1265-
if comp_time > 0
1266-
printstyled(" ", Ryu.writefixed(Float64(100 * comp_time / (elapsed * 1e6)), 2), "% compilation time", color = Base.info_color())
1267-
end
1268-
if recomp_time > 0
1269-
perc = Float64(100 * recomp_time / comp_time)
1270-
printstyled(" (", perc < 1 ? "<1" : Ryu.writefixed(perc, 0), "% recompilation)", color = Base.warn_color())
1271-
end
1272-
println()
1259+
print_time_imports_report(M, elapsed_time, comp_time, recomp_time)
12731260
end
12741261
return M
12751262
end
@@ -1281,6 +1268,52 @@ function _include_from_serialized(pkg::PkgId, path::String, ocachepath::Union{No
12811268
end
12821269
end
12831270

1271+
# printing functions for @time_imports
1272+
# note that the time inputs are UInt64 on all platforms. Give default values here so that we don't have
1273+
# confusing UInt64 types in generate_precompile.jl
1274+
function print_time_imports_report(
1275+
mod::Module,
1276+
elapsed_time::UInt64=UInt64(1),
1277+
comp_time::UInt64=UInt64(1),
1278+
recomp_time::UInt64=UInt64(1)
1279+
)
1280+
print(lpad(round(elapsed_time / 1e6, digits=1), 9), " ms ")
1281+
ext_parent = extension_parent_name(mod)
1282+
if ext_parent !== nothing
1283+
print(ext_parent::String, "")
1284+
end
1285+
print(string(mod))
1286+
if comp_time > 0
1287+
perc = Ryu.writefixed(Float64(100 * comp_time / (elapsed_time)), 2)
1288+
printstyled(" $perc% compilation time", color = Base.info_color())
1289+
end
1290+
if recomp_time > 0
1291+
perc = Float64(100 * recomp_time / comp_time)
1292+
perc_show = perc < 1 ? "<1" : Ryu.writefixed(perc, 0)
1293+
printstyled(" ($perc_show% recompilation)", color = Base.warn_color())
1294+
end
1295+
println()
1296+
end
1297+
function print_time_imports_report_init(
1298+
mod::Module, i::Int=1,
1299+
elapsed_time::UInt64=UInt64(1),
1300+
comp_time::UInt64=UInt64(1),
1301+
recomp_time::UInt64=UInt64(1)
1302+
)
1303+
connector = i > 1 ? "" : ""
1304+
printstyled(" $connector ", color = :light_black)
1305+
print("$(round(elapsed_time / 1e6, digits=1)) ms $mod.__init__() ")
1306+
if comp_time > 0
1307+
perc = Ryu.writefixed(Float64(100 * (comp_time) / elapsed_time), 2)
1308+
printstyled("$perc% compilation time", color = Base.info_color())
1309+
end
1310+
if recomp_time > 0
1311+
perc = Float64(100 * recomp_time / comp_time)
1312+
printstyled(" ($(perc < 1 ? "<1" : Ryu.writefixed(perc, 0))% recompilation)", color = Base.warn_color())
1313+
end
1314+
println()
1315+
end
1316+
12841317
# if M is an extension, return the string name of the parent. Otherwise return nothing
12851318
function extension_parent_name(M::Module)
12861319
rootmodule = moduleroot(M)
@@ -1338,31 +1371,18 @@ function run_module_init(mod::Module, i::Int=1)
13381371
# `i` informs ordering for the `@time_imports` report formatting
13391372
if TIMING_IMPORTS[] == 0
13401373
ccall(:jl_init_restored_module, Cvoid, (Any,), mod)
1341-
else
1342-
if isdefined(mod, :__init__)
1343-
connector = i > 1 ? "" : ""
1344-
printstyled(" $connector ", color = :light_black)
1345-
1346-
elapsedtime = time_ns()
1347-
cumulative_compile_timing(true)
1348-
compile_elapsedtimes = cumulative_compile_time_ns()
1374+
elseif isdefined(mod, :__init__)
1375+
elapsed_time = time_ns()
1376+
cumulative_compile_timing(true)
1377+
compile_elapsedtimes = cumulative_compile_time_ns()
13491378

1350-
ccall(:jl_init_restored_module, Cvoid, (Any,), mod)
1379+
ccall(:jl_init_restored_module, Cvoid, (Any,), mod)
13511380

1352-
elapsedtime = (time_ns() - elapsedtime) / 1e6
1353-
cumulative_compile_timing(false);
1354-
comp_time, recomp_time = (cumulative_compile_time_ns() .- compile_elapsedtimes) ./ 1e6
1381+
elapsed_time = time_ns() - elapsed_time
1382+
cumulative_compile_timing(false);
1383+
comp_time, recomp_time = cumulative_compile_time_ns() .- compile_elapsedtimes
13551384

1356-
print("$(round(elapsedtime, digits=1)) ms $mod.__init__() ")
1357-
if comp_time > 0
1358-
printstyled(Ryu.writefixed(Float64(100 * comp_time / elapsedtime), 2), "% compilation time", color = Base.info_color())
1359-
end
1360-
if recomp_time > 0
1361-
perc = Float64(100 * recomp_time / comp_time)
1362-
printstyled(" ($(perc < 1 ? "<1" : Ryu.writefixed(perc, 0))% recompilation)", color = Base.warn_color())
1363-
end
1364-
println()
1365-
end
1385+
print_time_imports_report_init(mod, i, elapsed_time, comp_time, recomp_time)
13661386
end
13671387
end
13681388

contrib/generate_precompile.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ precompile(Base.__require_prelocked, (Base.PkgId, Nothing))
3939
precompile(Base._require, (Base.PkgId, Nothing))
4040
precompile(Base.indexed_iterate, (Pair{Symbol, Union{Nothing, String}}, Int))
4141
precompile(Base.indexed_iterate, (Pair{Symbol, Union{Nothing, String}}, Int, Int))
42+
precompile(Tuple{typeof(Base.Threads.atomic_add!), Base.Threads.Atomic{Int}, Int})
43+
precompile(Tuple{typeof(Base.Threads.atomic_sub!), Base.Threads.Atomic{Int}, Int})
4244
4345
# Pkg loading
4446
precompile(Tuple{typeof(Base.Filesystem.normpath), String, String, Vararg{String}})
@@ -161,6 +163,8 @@ for match = Base._methods(+, (Int, Int), -1, Base.get_world_counter())
161163
push!(Expr[], Expr(:return, false))
162164
vcat(String[], String[])
163165
k, v = (:hello => nothing)
166+
Base.print_time_imports_report(Base)
167+
Base.print_time_imports_report_init(Base)
164168
165169
# Preferences uses these
166170
get(Dict{String,Any}(), "missing", nothing)
@@ -172,6 +176,11 @@ for match = Base._methods(+, (Int, Int), -1, Base.get_world_counter())
172176
# interactive startup uses this
173177
write(IOBuffer(), "")
174178
179+
# not critical, but helps hide unrelated compilation from @time when using --trace-compile
180+
foo() = rand(2,2) * rand(2,2)
181+
@time foo()
182+
@time foo()
183+
175184
break # only actually need to do this once
176185
end
177186
"""

0 commit comments

Comments
 (0)