Skip to content

Commit 420d5a2

Browse files
vtjnashKristofferC
authored andcommitted
remove log spamming from precompilepkg code (#59652)
Improves consistency between serial and parallel code and reduces useless log spamming. Logging messages should convey useful content actionable in some form by the user. Neither non-interactive mode nor simple progress meters achieve either goal. Fix #59527 (cherry picked from commit 42a33bb)
1 parent fb80f24 commit 420d5a2

File tree

3 files changed

+55
-46
lines changed

3 files changed

+55
-46
lines changed

base/loading.jl

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,34 +2622,37 @@ function __require_prelocked(pkg::PkgId, env)
26222622
if JLOptions().use_compiled_modules == 1
26232623
if !generating_output(#=incremental=#false)
26242624
project = active_project()
2625-
if !generating_output() && !parallel_precompile_attempted && !disable_parallel_precompile && @isdefined(Precompilation) && project !== nothing &&
2626-
isfile(project) && project_file_manifest_path(project) !== nothing
2627-
parallel_precompile_attempted = true
2628-
unlock(require_lock)
2629-
try
2630-
Precompilation.precompilepkgs([pkg.name]; _from_loading=true, ignore_loaded=false)
2631-
finally
2632-
lock(require_lock)
2633-
end
2634-
@goto load_from_cache
2635-
end
26362625
# spawn off a new incremental pre-compile task for recursive `require` calls
26372626
loaded = let path = path, reasons = reasons
26382627
maybe_cachefile_lock(pkg, path) do
26392628
# double-check the search now that we have lock
26402629
m = _require_search_from_serialized(pkg, path, UInt128(0), true)
26412630
m isa Module && return m
2642-
triggers = get(EXT_PRIMED, pkg, nothing)
2643-
loadable_exts = nothing
2644-
if triggers !== nothing # extension
2645-
loadable_exts = PkgId[]
2646-
for (ext′, triggers′) in EXT_PRIMED
2647-
if triggers′ triggers
2648-
push!(loadable_exts, ext′)
2631+
2632+
verbosity = isinteractive() ? CoreLogging.Info : CoreLogging.Debug
2633+
@logmsg verbosity "Precompiling $(repr("text/plain", pkg))$(list_reasons(reasons))"
2634+
2635+
unlock(require_lock)
2636+
try
2637+
if !generating_output() && !parallel_precompile_attempted && !disable_parallel_precompile && @isdefined(Precompilation)
2638+
parallel_precompile_attempted = true
2639+
Precompilation.precompilepkgs([pkg]; _from_loading=true, ignore_loaded=false)
2640+
return
2641+
end
2642+
triggers = get(EXT_PRIMED, pkg, nothing)
2643+
loadable_exts = nothing
2644+
if triggers !== nothing # extension
2645+
loadable_exts = PkgId[]
2646+
for (ext′, triggers′) in EXT_PRIMED
2647+
if triggers′ triggers
2648+
push!(loadable_exts, ext′)
2649+
end
26492650
end
26502651
end
2652+
return compilecache(pkg, path; loadable_exts)
2653+
finally
2654+
lock(require_lock)
26512655
end
2652-
return compilecache(pkg, path; reasons, loadable_exts)
26532656
end
26542657
end
26552658
loaded isa Module && return loaded
@@ -3150,18 +3153,18 @@ This can be used to reduce package load times. Cache files are stored in
31503153
`DEPOT_PATH[1]/compiled`. See [Module initialization and precompilation](@ref)
31513154
for important notes.
31523155
"""
3153-
function compilecache(pkg::PkgId, internal_stderr::IO = stderr, internal_stdout::IO = stdout; flags::Cmd=``, cacheflags::CacheFlags=CacheFlags(), reasons::Union{Dict{String,Int},Nothing}=Dict{String,Int}(), loadable_exts::Union{Vector{PkgId},Nothing}=nothing)
3156+
function compilecache(pkg::PkgId, internal_stderr::IO = stderr, internal_stdout::IO = stdout; flags::Cmd=``, cacheflags::CacheFlags=CacheFlags(), loadable_exts::Union{Vector{PkgId},Nothing}=nothing)
31543157
@nospecialize internal_stderr internal_stdout
31553158
path = locate_package(pkg)
31563159
path === nothing && throw(ArgumentError("$(repr("text/plain", pkg)) not found during precompilation"))
3157-
return compilecache(pkg, path, internal_stderr, internal_stdout; flags, cacheflags, reasons, loadable_exts)
3160+
return compilecache(pkg, path, internal_stderr, internal_stdout; flags, cacheflags, loadable_exts)
31583161
end
31593162

31603163
const MAX_NUM_PRECOMPILE_FILES = Ref(10)
31613164

31623165
function compilecache(pkg::PkgId, path::String, internal_stderr::IO = stderr, internal_stdout::IO = stdout,
31633166
keep_loaded_modules::Bool = true; flags::Cmd=``, cacheflags::CacheFlags=CacheFlags(),
3164-
reasons::Union{Dict{String,Int},Nothing}=Dict{String,Int}(), loadable_exts::Union{Vector{PkgId},Nothing}=nothing)
3167+
loadable_exts::Union{Vector{PkgId},Nothing}=nothing)
31653168

31663169
@nospecialize internal_stderr internal_stdout
31673170
# decide where to put the resulting cache file
@@ -3179,8 +3182,6 @@ function compilecache(pkg::PkgId, path::String, internal_stderr::IO = stderr, in
31793182
concrete_deps = empty(_concrete_dependencies)
31803183
end
31813184
# run the expression and cache the result
3182-
verbosity = isinteractive() ? CoreLogging.Info : CoreLogging.Debug
3183-
@logmsg verbosity "Precompiling $(repr("text/plain", pkg)) $(list_reasons(reasons))"
31843185

31853186
# create a temporary file in `cachepath` directory, write the cache in it,
31863187
# write the checksum, _and then_ atomically move the file to `cachefile`.
@@ -3878,7 +3879,7 @@ end
38783879
record_reason(::Nothing, ::String) = nothing
38793880
function list_reasons(reasons::Dict{String,Int})
38803881
isempty(reasons) && return ""
3881-
return "(cache misses: $(join(("$k ($v)" for (k,v) in reasons), ", ")))"
3882+
return " (cache misses: $(join(("$k ($v)" for (k,v) in reasons), ", ")))"
38823883
end
38833884
list_reasons(::Nothing) = ""
38843885

base/precompilation.jl

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Precompilation
22

3-
using Base: PkgId, UUID, SHA1, parsed_toml, project_file_name_uuid, project_names,
3+
using Base: CoreLogging, PkgId, UUID, SHA1, parsed_toml, project_file_name_uuid, project_names,
44
project_file_manifest_path, get_deps, preferences_names, isaccessibledir, isfile_casesensitive,
55
base_project, isdefined
66

@@ -499,9 +499,16 @@ function _precompilepkgs(pkgs::Vector{String},
499499
num_tasks = parse(Int, get(ENV, "JULIA_NUM_PRECOMPILE_TASKS", string(default_num_tasks)))
500500
parallel_limiter = Base.Semaphore(num_tasks)
501501

502-
# suppress passive loading printing in julia test suite. `JULIA_TESTS` is set in Base.runtests
503-
io = (_from_loading && !Sys.isinteractive() && Base.get_bool_env("JULIA_TESTS", false)) ? IOContext{IO}(devnull) : _io
504-
502+
# suppress precompilation progress messages when precompiling for loading packages, except during interactive sessions
503+
# or when specified by logging heuristics that explicitly require it
504+
# since the complicated IO implemented here can have somewhat disastrous consequences when happening in the background (e.g. #59599)
505+
io = _io
506+
logcalls = nothing
507+
if _from_loading && !isinteractive()
508+
io = IOContext{IO}(devnull)
509+
fancyprint = false
510+
logcalls = isinteractive() ? CoreLogging.Info : CoreLogging.Debug # sync with Base.compilecache
511+
end
505512

506513
nconfigs = length(configs)
507514
hascolor = get(io, :color, false)::Bool
@@ -572,8 +579,6 @@ function _precompilepkgs(pkgs::Vector{String},
572579
# consider exts of project deps to be project deps so that errors are reported
573580
append!(project_deps, keys(filter(d->last(d).name in keys(env.project_deps), ext_to_parent)))
574581

575-
@debug "precompile: deps collected"
576-
577582
# An extension effectively depends on another extension if it has a strict superset of its triggers
578583
for ext_a in keys(ext_to_parent)
579584
for ext_b in keys(ext_to_parent)
@@ -628,7 +633,6 @@ function _precompilepkgs(pkgs::Vector{String},
628633
end
629634
end
630635
end
631-
@debug "precompile: extensions collected"
632636

633637
# return early if no deps
634638
if isempty(direct_deps)
@@ -656,7 +660,6 @@ function _precompilepkgs(pkgs::Vector{String},
656660
was_recompiled[pkg_config] = false
657661
end
658662
end
659-
@debug "precompile: signalling initialized"
660663

661664
# find and guard against circular deps
662665
cycles = Vector{Base.PkgId}[]
@@ -683,7 +686,6 @@ function _precompilepkgs(pkgs::Vector{String},
683686
if !isempty(circular_deps)
684687
@warn excluded_circular_deps_explanation(io, ext_to_parent, circular_deps, cycles)
685688
end
686-
@debug "precompile: circular dep check done"
687689

688690
if !manifest
689691
if isempty(pkgs)
@@ -723,7 +725,6 @@ function _precompilepkgs(pkgs::Vector{String},
723725
else
724726
target[] = "for $nconfigs compilation configurations..."
725727
end
726-
@debug "precompile: packages filtered"
727728

728729
pkg_queue = PkgConfig[]
729730
failed_deps = Dict{PkgConfig, String}()
@@ -949,16 +950,23 @@ function _precompilepkgs(pkgs::Vector{String},
949950
return
950951
end
951952
try
952-
# allows processes to wait if another process is precompiling a given package to
953-
# a functionally identical package cache (except for preferences, which may differ)
954-
t = @elapsed ret = precompile_pkgs_maybe_cachefile_lock(io, print_lock, fancyprint, pkg_config, pkgspidlocked, hascolor, parallel_limiter, ignore_loaded) do
955-
Base.with_logger(Base.NullLogger()) do
956-
# whether to respect already loaded dependency versions
957-
keep_loaded_modules = !ignore_loaded
958-
# for extensions, any extension in our direct dependencies is one we have a right to load
959-
# for packages, we may load any extension (all possible triggers are accounted for above)
960-
loadable_exts = haskey(ext_to_parent, pkg) ? filter((dep)->haskey(ext_to_parent, dep), direct_deps[pkg]) : nothing
961-
Base.compilecache(pkg, sourcepath, std_pipe, std_pipe, keep_loaded_modules;
953+
# for extensions, any extension in our direct dependencies is one we have a right to load
954+
# for packages, we may load any extension (all possible triggers are accounted for above)
955+
loadable_exts = haskey(ext_to_parent, pkg) ? filter((dep)->haskey(ext_to_parent, dep), direct_deps[pkg]) : nothing
956+
if _from_loading && pkg in requested_pkgids
957+
# loading already took the cachefile_lock and printed logmsg for its explicit requests
958+
t = @elapsed ret = begin
959+
Base.compilecache(pkg, sourcepath, std_pipe, std_pipe, !ignore_loaded;
960+
flags, cacheflags, loadable_exts)
961+
end
962+
else
963+
# allows processes to wait if another process is precompiling a given package to
964+
# a functionally identical package cache (except for preferences, which may differ)
965+
t = @elapsed ret = precompile_pkgs_maybe_cachefile_lock(io, print_lock, fancyprint, pkg_config, pkgspidlocked, hascolor, parallel_limiter, ignore_loaded) do
966+
logcalls === nothing || @lock print_lock begin
967+
Base.@logmsg logcalls "Precompiling $(repr("text/plain", pkg))"
968+
end
969+
Base.compilecache(pkg, sourcepath, std_pipe, std_pipe, !ignore_loaded;
962970
flags, cacheflags, loadable_exts)
963971
end
964972
end

test/precompile.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ precompile_test_harness(false) do dir
642642
end
643643
""")
644644

645-
cachefile, _ = @test_logs (:debug, r"Precompiling FooBar") min_level=Logging.Debug match_mode=:any Base.compilecache(Base.PkgId("FooBar"))
645+
cachefile, _ = @test_logs (:debug, r"Generating object cache file for FooBar") min_level=Logging.Debug match_mode=:any Base.compilecache(Base.PkgId("FooBar"))
646646
empty_prefs_hash = Base.get_preferences_hash(nothing, String[])
647647
@test cachefile == Base.compilecache_path(Base.PkgId("FooBar"), empty_prefs_hash)
648648
@test isfile(joinpath(cachedir, "FooBar.ji"))

0 commit comments

Comments
 (0)