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

Adding stock gc heap resizing heuristics to MMTk #57147

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion contrib/refresh_checksums.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CLANG_TRIPLETS=$(filter %-darwin %-freebsd,$(TRIPLETS))
NON_CLANG_TRIPLETS=$(filter-out %-darwin %-freebsd,$(TRIPLETS))

# These are the projects currently using BinaryBuilder; both GCC-expanded and non-GCC-expanded:
BB_PROJECTS=openssl libssh2 nghttp2 mpfr curl libgit2 pcre libuv unwind llvmunwind dsfmt objconv p7zip zlib libsuitesparse openlibm blastrampoline libtracyclient
BB_PROJECTS=openssl libssh2 nghttp2 mpfr curl libgit2 pcre libuv unwind llvmunwind dsfmt objconv p7zip zlib libsuitesparse openlibm blastrampoline libtracyclient mmtk_julia
BB_GCC_EXPANDED_PROJECTS=openblas csl
BB_CXX_EXPANDED_PROJECTS=gmp llvm clang llvm-tools lld
# These are non-BB source-only deps
Expand Down
4 changes: 4 additions & 0 deletions deps/checksums/mmtk_julia
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ mmtk_julia-f07d66aafc86af84ea988b35335acc9bbc770fa1.tar.gz/md5/38afb5db6d8c55413
mmtk_julia-f07d66aafc86af84ea988b35335acc9bbc770fa1.tar.gz/sha512/78525582a46a6baf8d33df7b622e55cf244439afcd7192ba55489c1bc18393d1237d2903d517c610484bf9e2a7338ad31435a9cbf70889d6bcf87c40cec829e5
mmtk_julia.v0.30.3+1.x86_64-linux-gnu.tar.gz/md5/631b204574da7062802dac501a4b711f
mmtk_julia.v0.30.3+1.x86_64-linux-gnu.tar.gz/sha512/daaed59d08fc49621479ed638dea0aac0cba123986e486571447e8e21e9a098776ce2e87fbd92ddea276782fc44621f23d40fa213296b28e1d4480553c7de4f7
mmtk_julia-c9e046baf3a0d52fe75d6c8b28f6afd69b045d95.tar.gz/md5/73a8fbea71edce30a39a30f31969dd8e
mmtk_julia-c9e046baf3a0d52fe75d6c8b28f6afd69b045d95.tar.gz/sha512/374848b7696b565dea66daa208830581f92c1fcb0138e7a7ab88564402e94bc79c54b6ed370ec68473e31e2bd411bf82c97793796c31d39aafbbfffea9c05588
mmtk_julia.v0.30.4+0.x86_64-linux-gnu.tar.gz/md5/8cdeb14fd69945f64308be49f6912f9c
mmtk_julia.v0.30.4+0.x86_64-linux-gnu.tar.gz/sha512/3692502f65dec8c0971b56b9bf8178641892b390d520cbcd69880d75b7500e6341534d87882246e68998f590f824ec54c18f4b8fb4aa09b8f313de065c48450e
6 changes: 3 additions & 3 deletions deps/mmtk_julia.version
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MMTK_JULIA_BRANCH = master
MMTK_JULIA_SHA1 = f07d66aafc86af84ea988b35335acc9bbc770fa1
MMTK_JULIA_SHA1 = c9e046baf3a0d52fe75d6c8b28f6afd69b045d95
MMTK_JULIA_GIT_URL := https://github.com/mmtk/mmtk-julia.git
MMTK_JULIA_TAR_URL = https://github.com/mmtk/mmtk-julia/archive/refs/tags/v0.30.3.tar.gz
MMTK_JULIA_JLL_VER := 0.30.3+1
MMTK_JULIA_TAR_URL = https://github.com/mmtk/mmtk-julia/archive/refs/tags/v0.30.4.tar.gz
MMTK_JULIA_JLL_VER := 0.30.4+0
MMTK_JULIA_JLL_NAME := mmtk_julia
54 changes: 42 additions & 12 deletions src/gc-mmtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,37 @@ void jl_gc_init(void) {

arraylist_new(&to_finalize, 0);
arraylist_new(&finalizer_list_marked, 0);

gc_num.interval = default_collect_interval;
gc_num.allocd = 0;
gc_num.max_pause = 0;
gc_num.max_memory = 0;

// Necessary if we want to use Julia heap resizing heuristics
uint64_t mem_reserve = 250*1024*1024; // LLVM + other libraries need some amount of memory
uint64_t min_heap_size_hint = mem_reserve + 1*1024*1024;
uint64_t hint = jl_options.heap_size_hint;

// check if heap size specified on command line
if (jl_options.heap_size_hint == 0) {
char *cp = getenv(HEAP_SIZE_HINT);
if (cp)
hint = parse_heap_size_hint(cp, "JULIA_HEAP_SIZE_HINT=\"<size>[<unit>]\"");
}
#ifdef _P64
if (hint == 0) {
uint64_t constrained_mem = uv_get_constrained_memory();
if (constrained_mem > 0 && constrained_mem < uv_get_total_memory())
hint = constrained_mem;
}
#endif
if (hint) {
if (hint < min_heap_size_hint)
hint = min_heap_size_hint;
jl_gc_set_max_memory(hint - mem_reserve);
}

// MMTK supports setting the heap size using the
// MMTK_MIN_HSIZE and MMTK_MAX_HSIZE environment variables
long long min_heap_size;
long long max_heap_size;
char* min_size_def = getenv("MMTK_MIN_HSIZE");
Expand All @@ -77,7 +103,8 @@ void jl_gc_init(void) {
char* max_size_def = getenv("MMTK_MAX_HSIZE");
char* max_size_gb = getenv("MMTK_MAX_HSIZE_G");

// default min heap currently set as Julia's default_collect_interval
// If min and max values are not specified, set them to 0 here
// and use stock heuristics as defined in the binding
if (min_size_def != NULL) {
char *p;
double min_size = strtod(min_size_def, &p);
Expand All @@ -87,10 +114,9 @@ void jl_gc_init(void) {
double min_size = strtod(min_size_gb, &p);
min_heap_size = (long) 1024 * 1024 * 1024 * min_size;
} else {
min_heap_size = default_collect_interval;
min_heap_size = 0;
}

// default max heap currently set as 70% the free memory in the system
if (max_size_def != NULL) {
char *p;
double max_size = strtod(max_size_def, &p);
Expand All @@ -100,7 +126,7 @@ void jl_gc_init(void) {
double max_size = strtod(max_size_gb, &p);
max_heap_size = (long) 1024 * 1024 * 1024 * max_size;
} else {
max_heap_size = uv_get_free_memory() * 70 / 100;
max_heap_size = 0;
}

// Assert that the number of stock GC threads is 0; MMTK uses the number of threads in jl_options.ngcthreads
Expand Down Expand Up @@ -159,7 +185,17 @@ void jl_free_thread_gc_state(struct _jl_tls_states_t *ptls) {
}

JL_DLLEXPORT void jl_gc_set_max_memory(uint64_t max_mem) {
// MMTk currently does not allow setting the heap size at runtime
#ifdef _P32
max_mem = max_mem < MAX32HEAP ? max_mem : MAX32HEAP;
#endif
max_total_memory = max_mem;
}

JL_DLLEXPORT uint64_t jl_gc_get_max_memory(void)
{
// FIXME: We should return the max heap size set in MMTk
// when not using Julia's heap resizing heuristics
return max_total_memory;
}

STATIC_INLINE void maybe_collect(jl_ptls_t ptls)
Expand Down Expand Up @@ -415,12 +451,6 @@ JL_DLLEXPORT void jl_gc_get_total_bytes(int64_t *bytes) JL_NOTSAFEPOINT
*bytes = (num.total_allocd + num.deferred_alloc + num.allocd);
}

JL_DLLEXPORT uint64_t jl_gc_get_max_memory(void)
{
// FIXME: should probably return MMTk's heap size
return max_total_memory;
}

// These are needed to collect MMTk statistics from a Julia program using ccall
JL_DLLEXPORT void (jl_mmtk_harness_begin)(void)
{
Expand Down
Loading