Skip to content

Commit 229269b

Browse files
authored
Don't permalloc the pkgimgs, but with an option (#49940)
1 parent 01ddf80 commit 229269b

File tree

5 files changed

+47
-21
lines changed

5 files changed

+47
-21
lines changed

base/options.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct JLOptions
5454
rr_detach::Int8
5555
strip_metadata::Int8
5656
strip_ir::Int8
57+
permalloc_pkgimg::Int8
5758
heap_size_hint::UInt64
5859
end
5960

src/jloptions.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ JL_DLLEXPORT void jl_init_options(void)
8787
0, // rr-detach
8888
0, // strip-metadata
8989
0, // strip-ir
90+
0, // permalloc_pkgimg
9091
0, // heap-size-hint
9192
};
9293
jl_options_initialized = 1;
@@ -209,6 +210,7 @@ static const char opts_hidden[] =
209210
" --trace-compile={stderr,name}\n"
210211
" Print precompile statements for methods compiled during execution or save to a path\n"
211212
" --image-codegen Force generate code in imaging mode\n"
213+
" --permalloc-pkgimg={yes|no*} Copy the data section of package images into memory\n"
212214
;
213215

214216
JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
@@ -254,6 +256,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
254256
opt_strip_ir,
255257
opt_heap_size_hint,
256258
opt_gc_threads,
259+
opt_permalloc_pkgimg
257260
};
258261
static const char* const shortopts = "+vhqH:e:E:L:J:C:it:p:O:g:";
259262
static const struct option longopts[] = {
@@ -313,6 +316,7 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
313316
{ "rr-detach", no_argument, 0, opt_rr_detach },
314317
{ "strip-metadata", no_argument, 0, opt_strip_metadata },
315318
{ "strip-ir", no_argument, 0, opt_strip_ir },
319+
{ "permalloc-pkgimg",required_argument, 0, opt_permalloc_pkgimg },
316320
{ "heap-size-hint", required_argument, 0, opt_heap_size_hint },
317321
{ 0, 0, 0, 0 }
318322
};
@@ -827,6 +831,14 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
827831
jl_errorf("julia: --gcthreads=<n>; n must be an integer >= 1");
828832
jl_options.ngcthreads = (int16_t)ngcthreads;
829833
break;
834+
case opt_permalloc_pkgimg:
835+
if (!strcmp(optarg,"yes"))
836+
jl_options.permalloc_pkgimg = 1;
837+
else if (!strcmp(optarg,"no"))
838+
jl_options.permalloc_pkgimg = 0;
839+
else
840+
jl_errorf("julia: invalid argument to --permalloc-pkgimg={yes|no} (%s)", optarg);
841+
break;
830842
default:
831843
jl_errorf("julia: unhandled option -- %c\n"
832844
"This is a bug, please report it.", c);

src/jloptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ typedef struct {
5858
int8_t rr_detach;
5959
int8_t strip_metadata;
6060
int8_t strip_ir;
61+
int8_t permalloc_pkgimg;
6162
uint64_t heap_size_hint;
6263
} jl_options_t;
6364

src/staticdata.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ External links:
7171
*/
7272
#include <stdlib.h>
7373
#include <string.h>
74+
#include <stdbool.h>
7475
#include <stdio.h> // printf
7576
#include <inttypes.h> // PRIxPTR
7677

@@ -3364,7 +3365,7 @@ static jl_value_t *jl_validate_cache_file(ios_t *f, jl_array_t *depmods, uint64_
33643365
}
33653366

33663367
// TODO?: refactor to make it easier to create the "package inspector"
3367-
static jl_value_t *jl_restore_package_image_from_stream(ios_t *f, jl_image_t *image, jl_array_t *depmods, int completeinfo, const char *pkgname)
3368+
static jl_value_t *jl_restore_package_image_from_stream(ios_t *f, jl_image_t *image, jl_array_t *depmods, int completeinfo, const char *pkgname, bool needs_permalloc)
33683369
{
33693370
JL_TIMING(LOAD_IMAGE, LOAD_Pkgimg);
33703371
jl_timing_printf(JL_TIMING_DEFAULT_BLOCK, pkgname);
@@ -3377,7 +3378,7 @@ static jl_value_t *jl_restore_package_image_from_stream(ios_t *f, jl_image_t *im
33773378
return verify_fail;
33783379

33793380
assert(datastartpos > 0 && datastartpos < dataendpos);
3380-
3381+
needs_permalloc = jl_options.permalloc_pkgimg || needs_permalloc;
33813382
jl_value_t *restored = NULL;
33823383
jl_array_t *init_order = NULL, *extext_methods = NULL, *new_specializations = NULL, *method_roots_list = NULL, *ext_targets = NULL, *edges = NULL;
33833384
jl_svec_t *cachesizes_sv = NULL;
@@ -3389,14 +3390,22 @@ static jl_value_t *jl_restore_package_image_from_stream(ios_t *f, jl_image_t *im
33893390
ios_bufmode(f, bm_none);
33903391
JL_SIGATOMIC_BEGIN();
33913392
size_t len = dataendpos - datastartpos;
3392-
char *sysimg = (char*)jl_gc_perm_alloc(len, 0, 64, 0);
3393+
char *sysimg;
3394+
bool success = !needs_permalloc;
33933395
ios_seek(f, datastartpos);
3394-
if (ios_readall(f, sysimg, len) != len || jl_crc32c(0, sysimg, len) != (uint32_t)checksum) {
3395-
restored = jl_get_exceptionf(jl_errorexception_type, "Error reading system image file.");
3396+
if (needs_permalloc)
3397+
sysimg = (char*)jl_gc_perm_alloc(len, 0, 64, 0);
3398+
else
3399+
sysimg = &f->buf[f->bpos];
3400+
if (needs_permalloc)
3401+
success = ios_readall(f, sysimg, len) == len;
3402+
if (!success || jl_crc32c(0, sysimg, len) != (uint32_t)checksum) {
3403+
restored = jl_get_exceptionf(jl_errorexception_type, "Error reading package image file.");
33963404
JL_SIGATOMIC_END();
33973405
}
33983406
else {
3399-
ios_close(f);
3407+
if (needs_permalloc)
3408+
ios_close(f);
34003409
ios_static_buffer(f, sysimg, len);
34013410
pkgcachesizes cachesizes;
34023411
jl_restore_system_image_from_stream_(f, image, depmods, checksum, (jl_array_t**)&restored, &init_order, &extext_methods, &new_specializations, &method_roots_list, &ext_targets, &edges, &base, &ccallable_list, &cachesizes);
@@ -3442,11 +3451,11 @@ static void jl_restore_system_image_from_stream(ios_t *f, jl_image_t *image, uin
34423451
jl_restore_system_image_from_stream_(f, image, NULL, checksum | ((uint64_t)0xfdfcfbfa << 32), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
34433452
}
34443453

3445-
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 completeinfo, const char *pkgname)
3454+
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 completeinfo, const char *pkgname, bool needs_permalloc)
34463455
{
34473456
ios_t f;
34483457
ios_static_buffer(&f, (char*)buf, sz);
3449-
jl_value_t *ret = jl_restore_package_image_from_stream(&f, image, depmods, completeinfo, pkgname);
3458+
jl_value_t *ret = jl_restore_package_image_from_stream(&f, image, depmods, completeinfo, pkgname, needs_permalloc);
34503459
ios_close(&f);
34513460
return ret;
34523461
}
@@ -3459,7 +3468,7 @@ JL_DLLEXPORT jl_value_t *jl_restore_incremental(const char *fname, jl_array_t *d
34593468
"Cache file \"%s\" not found.\n", fname);
34603469
}
34613470
jl_image_t pkgimage = {};
3462-
jl_value_t *ret = jl_restore_package_image_from_stream(&f, &pkgimage, depmods, completeinfo, pkgname);
3471+
jl_value_t *ret = jl_restore_package_image_from_stream(&f, &pkgimage, depmods, completeinfo, pkgname, true);
34633472
ios_close(&f);
34643473
return ret;
34653474
}
@@ -3530,7 +3539,7 @@ JL_DLLEXPORT jl_value_t *jl_restore_package_image_from_file(const char *fname, j
35303539

35313540
jl_image_t pkgimage = jl_init_processor_pkgimg(pkgimg_handle);
35323541

3533-
jl_value_t* mod = jl_restore_incremental_from_buf(pkgimg_data, &pkgimage, *plen, depmods, completeinfo, pkgname);
3542+
jl_value_t* mod = jl_restore_incremental_from_buf(pkgimg_data, &pkgimage, *plen, depmods, completeinfo, pkgname, false);
35343543

35353544
return mod;
35363545
}

test/precompile.jl

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,17 +339,20 @@ precompile_test_harness(false) do dir
339339
cachedir = joinpath(dir, "compiled", "v$(VERSION.major).$(VERSION.minor)")
340340
cachedir2 = joinpath(dir2, "compiled", "v$(VERSION.major).$(VERSION.minor)")
341341
cachefile = joinpath(cachedir, "$Foo_module.ji")
342-
if Base.JLOptions().use_pkgimages == 1
343-
ocachefile = Base.ocachefile_from_cachefile(cachefile)
344-
else
345-
ocachefile = nothing
346-
end
347-
# use _require_from_serialized to ensure that the test fails if
348-
# the module doesn't reload from the image:
349-
@test_warn "@ccallable was already defined for this method name" begin
350-
@test_logs (:warn, "Replacing module `$Foo_module`") begin
351-
m = Base._require_from_serialized(Base.PkgId(Foo), cachefile, ocachefile)
352-
@test isa(m, Module)
342+
do_pkgimg = Base.JLOptions().use_pkgimages == 1 && Base.JLOptions().permalloc_pkgimg == 1
343+
if do_pkgimg || Base.JLOptions().use_pkgimages == 0
344+
if do_pkgimg
345+
ocachefile = Base.ocachefile_from_cachefile(cachefile)
346+
else
347+
ocachefile = nothing
348+
end
349+
# use _require_from_serialized to ensure that the test fails if
350+
# the module doesn't reload from the image:
351+
@test_warn "@ccallable was already defined for this method name" begin
352+
@test_logs (:warn, "Replacing module `$Foo_module`") begin
353+
m = Base._require_from_serialized(Base.PkgId(Foo), cachefile, ocachefile)
354+
@test isa(m, Module)
355+
end
353356
end
354357
end
355358

0 commit comments

Comments
 (0)