Skip to content

Commit

Permalink
Cache packages and their compilecache across invocations.
Browse files Browse the repository at this point in the history
Hopefully this improves the package load time a bit.
  • Loading branch information
maleadt committed Jul 29, 2022
1 parent 5b74dc9 commit 48fa239
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/PkgEval.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function __init__()
mkpath(joinpath(download_dir, "srccache"))

global storage_dir = @get_scratch!("storage")
mkpath(joinpath(storage_dir, "artifacts"))
mkpath(joinpath(storage_dir, "packages"))

# read Packages.toml
packages = TOML.parsefile(joinpath(dirname(@__DIR__), "Packages.toml"))
Expand Down
34 changes: 26 additions & 8 deletions src/evaluate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ const reasons = Dict(
:inactivity => "tests became inactive",
)

const compiled_lock = ReentrantLock()
const compiled_cache = Dict()
function get_compilecache(config::Configuration)
lock(compiled_lock) do
key = (config.julia, config.buildflags,
config.distro, config.uid, config.user, config.gid, config.group, config.home)
dir = get(compiled_cache, key, nothing)
if dir === nothing || !isdir(dir)
compiled_cache[key] = mktempdir()
end
return compiled_cache[key]
end
end

"""
sandboxed_julia(config::Configuration, args=``; env=Dict(), mounts=Dict(), wait=true,
stdin=stdin, stdout=stdout, stderr=stderr, kwargs...)
Expand Down Expand Up @@ -87,16 +101,20 @@ function sandboxed_julia_cmd(config::Configuration, executor, args=``;
mounts::Dict{String,String}=Dict{String,String}())
rootfs = create_rootfs(config)
install = install_julia(config)
registries = joinpath(first(DEPOT_PATH), "registries")
read_only_maps = Dict(
"/" => rootfs,
config.julia_install_dir => install,
"/usr/local/share/julia/registries" => joinpath(first(DEPOT_PATH), "registries"),
"/" => rootfs,
config.julia_install_dir => install,
"/usr/local/share/julia/registries" => registries
)

artifacts_path = joinpath(storage_dir, "artifacts")
mkpath(artifacts_path)
compiled = get_compilecache(config)
packages = joinpath(storage_dir, "packages")
artifacts = joinpath(storage_dir, "artifacts")
read_write_maps = merge(mounts, Dict(
joinpath(config.home, ".julia/artifacts") => artifacts_path
joinpath(config.home, ".julia", "compiled") => compiled,
joinpath(config.home, ".julia", "packages") => packages,
joinpath(config.home, ".julia", "artifacts") => artifacts
))

env = merge(env, Dict(
Expand All @@ -107,7 +125,7 @@ function sandboxed_julia_cmd(config::Configuration, executor, args=``;

# use the provided registry
# NOTE: putting a registry in a non-primary depot entry makes Pkg use it as-is,
# without needingb to set Pkg.UPDATED_REGISTRY_THIS_SESSION.
# without needing to set Pkg.UPDATED_REGISTRY_THIS_SESSION.
"JULIA_DEPOT_PATH" => "::/usr/local/share/julia",

# some essential env vars (since we don't run from a shell)
Expand Down Expand Up @@ -396,7 +414,7 @@ function sandboxed_test(config::Configuration, pkg::Package; kwargs...)
status, reason, log = sandboxed_script(config, script, args; mounts, env, kwargs...)

# pick up the installed package version from the log
version_match = match(Regex("Installed $(pkg.name) .+ v(.+)"), log)
version_match = match(Regex("\\+ $(pkg.name) v(\\S+)"), log)
version = if version_match !== nothing
try
VersionNumber(version_match.captures[1])
Expand Down
22 changes: 19 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
isdebug(group) =
Base.CoreLogging.current_logger_for_env(Base.CoreLogging.Debug, group, PkgEval) !== nothing

"""
PkgEval.purge()
Remove temporary files and folders that are unlikely to be re-used in the future, e.g.,
temporary Julia installs or compilation cache of packages.
Artifacts that are more likely to be re-used in the future, e.g., downloaded Julia builds
or check-outs of Git repositories, are saved in scratch spaces instead.
"""
function purge()
lock(rootfs_lock) do
for dir in values(rootfs_cache)
rm(dir; recursive=true)
end
empty!(rootfs_cache)
end

lock(julia_lock) do
for dir in values(julia_cache)
rm(dir; recursive=true)
end
empty!(julia_cache)
end

lock(rootfs_lock) do
for dir in values(rootfs_cache)
lock(compiled_lock) do
for dir in values(compiled_cache)
rm(dir; recursive=true)
end
empty!(rootfs_cache)
empty!(compiled_cache)
end

return
Expand Down

0 comments on commit 48fa239

Please sign in to comment.