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

Don't permalloc the pkgimgs unnecessarily #48215

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/staticdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ External links:
*/
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h> // printf
#include <inttypes.h> // PRIxPTR

Expand Down Expand Up @@ -3209,7 +3210,7 @@ static jl_value_t *jl_validate_cache_file(ios_t *f, jl_array_t *depmods, uint64_
}

// TODO?: refactor to make it easier to create the "package inspector"
static jl_value_t *jl_restore_package_image_from_stream(ios_t *f, jl_image_t *image, jl_array_t *depmods, int complete)
static jl_value_t *jl_restore_package_image_from_stream(ios_t *f, jl_image_t *image, jl_array_t *depmods, int complete, bool needs_permalloc)
{
uint64_t checksum = 0;
int64_t dataendpos = 0;
Expand All @@ -3228,18 +3229,26 @@ static jl_value_t *jl_restore_package_image_from_stream(ios_t *f, jl_image_t *im
arraylist_t ccallable_list;
JL_GC_PUSH8(&restored, &init_order, &extext_methods, &new_specializations, &method_roots_list, &ext_targets, &edges, &cachesizes_sv);

{ // make a permanent in-memory copy of f (excluding the header)
{ // make a permanent in-memory copy of f (excluding the header) if needed
ios_bufmode(f, bm_none);
JL_SIGATOMIC_BEGIN();
size_t len = dataendpos - datastartpos;
char *sysimg = (char*)jl_gc_perm_alloc(len, 0, 64, 0);
char *sysimg;
bool success = !needs_permalloc;
ios_seek(f, datastartpos);
if (ios_readall(f, sysimg, len) != len || jl_crc32c(0, sysimg, len) != (uint32_t)checksum) {
restored = jl_get_exceptionf(jl_errorexception_type, "Error reading system image file.");
if (needs_permalloc)
sysimg = (char*)jl_gc_perm_alloc(len, 0, 64, 0);
else
sysimg = &f->buf[f->bpos];
if (needs_permalloc)
success = ios_readall(f, sysimg, len) == len;
if (!success || jl_crc32c(0, sysimg, len) != (uint32_t)checksum) {
restored = jl_get_exceptionf(jl_errorexception_type, "Error reading package image file.");
JL_SIGATOMIC_END();
}
else {
ios_close(f);
if (needs_permalloc)
ios_close(f);
ios_static_buffer(f, sysimg, len);
htable_new(&new_code_instance_validate, 0);
pkgcachesizes cachesizes;
Expand Down Expand Up @@ -3285,11 +3294,11 @@ static void jl_restore_system_image_from_stream(ios_t *f, jl_image_t *image)
jl_restore_system_image_from_stream_(f, image, NULL, checksum, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
}

JL_DLLEXPORT jl_value_t *jl_restore_incremental_from_buf(const char *buf, jl_image_t *image, size_t sz, jl_array_t *depmods, int complete)
JL_DLLEXPORT jl_value_t *jl_restore_incremental_from_buf(const char *buf, jl_image_t *image, size_t sz, jl_array_t *depmods, int complete, bool needs_permalloc)
{
ios_t f;
ios_static_buffer(&f, (char*)buf, sz);
jl_value_t *ret = jl_restore_package_image_from_stream(&f, image, depmods, complete);
jl_value_t *ret = jl_restore_package_image_from_stream(&f, image, depmods, complete, needs_permalloc);
ios_close(&f);
return ret;
}
Expand All @@ -3302,7 +3311,7 @@ JL_DLLEXPORT jl_value_t *jl_restore_incremental(const char *fname, jl_array_t *d
"Cache file \"%s\" not found.\n", fname);
}
jl_image_t pkgimage = {};
jl_value_t *ret = jl_restore_package_image_from_stream(&f, &pkgimage, depmods, complete);
jl_value_t *ret = jl_restore_package_image_from_stream(&f, &pkgimage, depmods, complete, true);
ios_close(&f);
return ret;
}
Expand Down Expand Up @@ -3402,7 +3411,7 @@ JL_DLLEXPORT jl_value_t *jl_restore_package_image_from_file(const char *fname, j
}
#endif

jl_value_t* mod = jl_restore_incremental_from_buf(pkgimg_data, &pkgimage, *plen, depmods, complete);
jl_value_t* mod = jl_restore_incremental_from_buf(pkgimg_data, &pkgimage, *plen, depmods, complete, false);

return mod;
}
Expand Down
21 changes: 21 additions & 0 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1079,5 +1079,26 @@ end
end
end

@testset "--pkgimages=no" begin
old_depot_path = copy(DEPOT_PATH)
try
tmp = mktempdir()
push!(empty!(DEPOT_PATH), joinpath(tmp, "depot"))

proj = joinpath(@__DIR__, "project", "Extensions", "HasDepWithExtensions.jl")
for compile in (`--pkgimages=no`, `--pkgimages=no`) # Once when requiring precomilation, once where it is already precompiled
cmd = `$(Base.julia_cmd()) $compile --project=$proj --startup-file=no -e '
begin
using HasExtensions
using HasDepWithExtensions
end
'`
@test success(cmd)
end
finally
copy!(DEPOT_PATH, old_depot_path)
end
end

empty!(Base.DEPOT_PATH)
append!(Base.DEPOT_PATH, original_depot_path)
12 changes: 6 additions & 6 deletions test/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,12 @@ precompile_test_harness(false) do dir
end
# use _require_from_serialized to ensure that the test fails if
# the module doesn't reload from the image:
@test_warn "@ccallable was already defined for this method name" begin
@test_logs (:warn, "Replacing module `$Foo_module`") begin
m = Base._require_from_serialized(Base.PkgId(Foo), cachefile, ocachefile)
@test isa(m, Module)
end
end
# @test_warn "@ccallable was already defined for this method name" begin
gbaraldi marked this conversation as resolved.
Show resolved Hide resolved
# @test_logs (:warn, "Replacing module `$Foo_module`") begin
# m = Base._require_from_serialized(Base.PkgId(Foo), cachefile, ocachefile)
# @test isa(m, Module)
# end
# end

@test_throws MethodError Foo.foo(17) # world shouldn't be visible yet
Base.invokelatest() do # use invokelatest to see the results of loading the compile
Expand Down