Skip to content

Commit 2fb0aa6

Browse files
authored
Merge pull request #2 from chrisseaton/mmtk-default-heap-size
Use 80% of physical memory by default
2 parents 4c614d3 + cebf8e9 commit 2fb0aa6

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

gc.c

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,10 @@ static int rgengc_remember(rb_objspace_t *objspace, VALUE obj);
14781478
static void rgengc_mark_and_rememberset_clear(rb_objspace_t *objspace, rb_heap_t *heap);
14791479
static void rgengc_rememberset_mark(rb_objspace_t *objspace, rb_heap_t *heap);
14801480

1481+
#ifdef USE_THIRD_PARTY_HEAP
1482+
static size_t rb_mmtk_heap_limit(void);
1483+
#endif
1484+
14811485
static inline int
14821486
RVALUE_FLAGS_AGE(VALUE flags)
14831487
{
@@ -1859,18 +1863,10 @@ rb_objspace_alloc(void)
18591863
dont_gc_on();
18601864

18611865
#ifdef USE_THIRD_PARTY_HEAP
1862-
const char *envval;
1863-
long heap_size;
1864-
if ((envval = getenv("THIRD_PARTY_HEAP_LIMIT")) != 0) {
1865-
heap_size = atol(envval);
1866-
} else {
1867-
heap_size = gc_params.heap_init_slots * sizeof(RVALUE);
1868-
}
1869-
1870-
// Note: this limit is currently broken for NoGC, but we still attempt to
1866+
// Note: the limit is currently broken for NoGC, but we still attempt to
18711867
// initialise it properly regardless.
18721868
// See https://github.com/mmtk/mmtk-core/issues/214
1873-
mmtk_init_binding(heap_size, &ruby_upcalls);
1869+
mmtk_init_binding(rb_mmtk_heap_limit(), &ruby_upcalls);
18741870
#endif
18751871

18761872
return objspace;
@@ -15047,4 +15043,43 @@ RubyUpcalls ruby_upcalls = {
1504715043
rb_mmtk_scan_object_ruby_style,
1504815044
};
1504915045

15046+
// Use up to 80% of memory for the heap
15047+
static const int rb_mmtk_heap_limit_percentage = 80;
15048+
15049+
static size_t rb_mmtk_system_physical_memory(void)
15050+
{
15051+
#ifdef __linux__
15052+
const long physical_pages = sysconf(_SC_PHYS_PAGES);
15053+
const long page_size = sysconf(_SC_PAGE_SIZE);
15054+
if (physical_pages == -1 || page_size == -1)
15055+
{
15056+
rb_bug("failed to get system physical memory size");
15057+
}
15058+
return (size_t) physical_pages * (size_t) page_size;
15059+
#else
15060+
#error no implementation of rb_mmtk_system_physical_memory on this platform
15061+
#endif
15062+
}
15063+
15064+
static size_t rb_mmtk_available_system_memory(void)
15065+
{
15066+
/*
15067+
* If we're in a container, we should use the maximum container memory,
15068+
* otherwise each container will try to use all system memory. There's
15069+
* example logic for this in the JVM and SVM (see CgroupV1Subsystem
15070+
* and CgroupV2Subsystem).
15071+
*/
15072+
15073+
return rb_mmtk_system_physical_memory();
15074+
}
15075+
15076+
size_t rb_mmtk_heap_limit(void) {
15077+
const char *envval;
15078+
if ((envval = getenv("THIRD_PARTY_HEAP_LIMIT")) != 0) {
15079+
return atol(envval);
15080+
} else {
15081+
return rb_mmtk_available_system_memory() / 100 * rb_mmtk_heap_limit_percentage;
15082+
}
15083+
}
15084+
1505015085
#endif

0 commit comments

Comments
 (0)