Skip to content

Commit 47a6ad0

Browse files
authored
Adding stock gc heap resizing heuristics to MMTk (#57147)
This PR sets MMTk to use the stock GC heuristics for heap resizing by default. It also updates the `mmtk-julia` version to incorporate the changes that support the new gc triggering heuristics (see mmtk/mmtk-julia#222).
1 parent 0625b3a commit 47a6ad0

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

contrib/refresh_checksums.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ CLANG_TRIPLETS=$(filter %-darwin %-freebsd,$(TRIPLETS))
2424
NON_CLANG_TRIPLETS=$(filter-out %-darwin %-freebsd,$(TRIPLETS))
2525

2626
# These are the projects currently using BinaryBuilder; both GCC-expanded and non-GCC-expanded:
27-
BB_PROJECTS=openssl libssh2 nghttp2 mpfr curl libgit2 pcre libuv unwind llvmunwind dsfmt objconv p7zip zlib libsuitesparse openlibm blastrampoline libtracyclient
27+
BB_PROJECTS=openssl libssh2 nghttp2 mpfr curl libgit2 pcre libuv unwind llvmunwind dsfmt objconv p7zip zlib libsuitesparse openlibm blastrampoline libtracyclient mmtk_julia
2828
BB_GCC_EXPANDED_PROJECTS=openblas csl
2929
BB_CXX_EXPANDED_PROJECTS=gmp llvm clang llvm-tools lld
3030
# These are non-BB source-only deps

deps/checksums/mmtk_julia

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ mmtk_julia-f07d66aafc86af84ea988b35335acc9bbc770fa1.tar.gz/md5/38afb5db6d8c55413
44
mmtk_julia-f07d66aafc86af84ea988b35335acc9bbc770fa1.tar.gz/sha512/78525582a46a6baf8d33df7b622e55cf244439afcd7192ba55489c1bc18393d1237d2903d517c610484bf9e2a7338ad31435a9cbf70889d6bcf87c40cec829e5
55
mmtk_julia.v0.30.3+1.x86_64-linux-gnu.tar.gz/md5/631b204574da7062802dac501a4b711f
66
mmtk_julia.v0.30.3+1.x86_64-linux-gnu.tar.gz/sha512/daaed59d08fc49621479ed638dea0aac0cba123986e486571447e8e21e9a098776ce2e87fbd92ddea276782fc44621f23d40fa213296b28e1d4480553c7de4f7
7+
mmtk_julia-c9e046baf3a0d52fe75d6c8b28f6afd69b045d95.tar.gz/md5/73a8fbea71edce30a39a30f31969dd8e
8+
mmtk_julia-c9e046baf3a0d52fe75d6c8b28f6afd69b045d95.tar.gz/sha512/374848b7696b565dea66daa208830581f92c1fcb0138e7a7ab88564402e94bc79c54b6ed370ec68473e31e2bd411bf82c97793796c31d39aafbbfffea9c05588
9+
mmtk_julia.v0.30.4+0.x86_64-linux-gnu.tar.gz/md5/8cdeb14fd69945f64308be49f6912f9c
10+
mmtk_julia.v0.30.4+0.x86_64-linux-gnu.tar.gz/sha512/3692502f65dec8c0971b56b9bf8178641892b390d520cbcd69880d75b7500e6341534d87882246e68998f590f824ec54c18f4b8fb4aa09b8f313de065c48450e

deps/mmtk_julia.version

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MMTK_JULIA_BRANCH = master
2-
MMTK_JULIA_SHA1 = f07d66aafc86af84ea988b35335acc9bbc770fa1
2+
MMTK_JULIA_SHA1 = c9e046baf3a0d52fe75d6c8b28f6afd69b045d95
33
MMTK_JULIA_GIT_URL := https://github.com/mmtk/mmtk-julia.git
4-
MMTK_JULIA_TAR_URL = https://github.com/mmtk/mmtk-julia/archive/refs/tags/v0.30.3.tar.gz
5-
MMTK_JULIA_JLL_VER := 0.30.3+1
4+
MMTK_JULIA_TAR_URL = https://github.com/mmtk/mmtk-julia/archive/refs/tags/v0.30.4.tar.gz
5+
MMTK_JULIA_JLL_VER := 0.30.4+0
66
MMTK_JULIA_JLL_NAME := mmtk_julia

src/gc-mmtk.c

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,37 @@ void jl_gc_init(void) {
6464

6565
arraylist_new(&to_finalize, 0);
6666
arraylist_new(&finalizer_list_marked, 0);
67-
67+
gc_num.interval = default_collect_interval;
6868
gc_num.allocd = 0;
6969
gc_num.max_pause = 0;
7070
gc_num.max_memory = 0;
7171

72+
// Necessary if we want to use Julia heap resizing heuristics
73+
uint64_t mem_reserve = 250*1024*1024; // LLVM + other libraries need some amount of memory
74+
uint64_t min_heap_size_hint = mem_reserve + 1*1024*1024;
75+
uint64_t hint = jl_options.heap_size_hint;
76+
77+
// check if heap size specified on command line
78+
if (jl_options.heap_size_hint == 0) {
79+
char *cp = getenv(HEAP_SIZE_HINT);
80+
if (cp)
81+
hint = parse_heap_size_hint(cp, "JULIA_HEAP_SIZE_HINT=\"<size>[<unit>]\"");
82+
}
83+
#ifdef _P64
84+
if (hint == 0) {
85+
uint64_t constrained_mem = uv_get_constrained_memory();
86+
if (constrained_mem > 0 && constrained_mem < uv_get_total_memory())
87+
hint = constrained_mem;
88+
}
89+
#endif
90+
if (hint) {
91+
if (hint < min_heap_size_hint)
92+
hint = min_heap_size_hint;
93+
jl_gc_set_max_memory(hint - mem_reserve);
94+
}
95+
96+
// MMTK supports setting the heap size using the
97+
// MMTK_MIN_HSIZE and MMTK_MAX_HSIZE environment variables
7298
long long min_heap_size;
7399
long long max_heap_size;
74100
char* min_size_def = getenv("MMTK_MIN_HSIZE");
@@ -77,7 +103,8 @@ void jl_gc_init(void) {
77103
char* max_size_def = getenv("MMTK_MAX_HSIZE");
78104
char* max_size_gb = getenv("MMTK_MAX_HSIZE_G");
79105

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

93-
// default max heap currently set as 70% the free memory in the system
94120
if (max_size_def != NULL) {
95121
char *p;
96122
double max_size = strtod(max_size_def, &p);
@@ -100,7 +126,7 @@ void jl_gc_init(void) {
100126
double max_size = strtod(max_size_gb, &p);
101127
max_heap_size = (long) 1024 * 1024 * 1024 * max_size;
102128
} else {
103-
max_heap_size = uv_get_free_memory() * 70 / 100;
129+
max_heap_size = 0;
104130
}
105131

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

161187
JL_DLLEXPORT void jl_gc_set_max_memory(uint64_t max_mem) {
162-
// MMTk currently does not allow setting the heap size at runtime
188+
#ifdef _P32
189+
max_mem = max_mem < MAX32HEAP ? max_mem : MAX32HEAP;
190+
#endif
191+
max_total_memory = max_mem;
192+
}
193+
194+
JL_DLLEXPORT uint64_t jl_gc_get_max_memory(void)
195+
{
196+
// FIXME: We should return the max heap size set in MMTk
197+
// when not using Julia's heap resizing heuristics
198+
return max_total_memory;
163199
}
164200

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

418-
JL_DLLEXPORT uint64_t jl_gc_get_max_memory(void)
419-
{
420-
// FIXME: should probably return MMTk's heap size
421-
return max_total_memory;
422-
}
423-
424454
// These are needed to collect MMTk statistics from a Julia program using ccall
425455
JL_DLLEXPORT void (jl_mmtk_harness_begin)(void)
426456
{

0 commit comments

Comments
 (0)