Skip to content

Make MMTk a runtime option #14

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

Merged
merged 8 commits into from
Aug 3, 2022
Merged
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
977 changes: 534 additions & 443 deletions gc.c

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ VALUE rb_gc_disable_no_rest(void);

struct rb_thread_struct;

#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
#define MMTK_DEFAULT_PLAN "MarkSweep"
void rb_gc_init_collection();
void rb_gc_init_collection(void);
void rb_mmtk_pre_process_opts(int argc, char **argv);
void rb_mmtk_post_process_opts(const char *arg);
void rb_mmtk_post_process_opts_finish(bool enable);
#endif // USE_THIRD_PARTY_HEAP
void rb_mmtk_post_process_opts_finish(bool feature_enable);
bool rb_mmtk_enabled_p(void);
#endif

RUBY_SYMBOL_EXPORT_BEGIN

Expand Down
4 changes: 2 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
# undef RUBY_DEBUG_ENV
#endif

#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
#include "gc.h"
#endif

static int
rb_main(int argc, char **argv)
{
RUBY_INIT_STACK;
#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
rb_mmtk_pre_process_opts(argc, argv);
#endif
ruby_init();
Expand Down
12 changes: 11 additions & 1 deletion object.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
#include "ruby/assert.h"
#include "builtin.h"

#if USE_MMTK
#include "gc.h"
#endif

/*!
* \addtogroup object
* \{
Expand Down Expand Up @@ -284,7 +288,13 @@ init_copy(VALUE dest, VALUE obj)
}
RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR);
rb_copy_wb_protected_attribute(dest, obj);
#if USE_MMTK
if (!rb_mmtk_enabled_p()) {
#endif
rb_copy_wb_protected_attribute(dest, obj);
#if USE_MMTK
}
#endif
rb_copy_generic_ivar(dest, obj);
rb_gc_copy_finalizer(dest, obj);
if (RB_TYPE_P(obj, T_OBJECT)) {
Expand Down
14 changes: 7 additions & 7 deletions ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
#include "ruby/version.h"
#include "ruby/internal/error.h"

#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
#include "gc.h"
#endif

Expand Down Expand Up @@ -291,7 +291,7 @@ usage(const char *name, int help, int highlight, int columns)
#if YJIT_BUILD
M("--yjit", "", "enable in-process JIT compiler (experimental)"),
#endif
#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
M("--mmtk", "", "use MMTk for garbage collection (experimental)"),
#endif
M("-h", "", "show this message, --help for more info"),
Expand Down Expand Up @@ -328,7 +328,7 @@ usage(const char *name, int help, int highlight, int columns)
#if YJIT_BUILD
M("yjit", "", "in-process JIT compiler (default: disabled)"),
#endif
#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
M("mmtk", "", "MMTk garbage collection (default: disabled)"),
#endif
};
Expand All @@ -350,7 +350,7 @@ usage(const char *name, int help, int highlight, int columns)
M("--yjit-greedy-versioning", "", "Greedy versioning mode (default: disabled)"),
};
#endif
#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
static const struct ruby_opt_message mmtk_options[] = {
M("--mmtk-plan=name", "", "MMTk garbage collection plan to use (default: " MMTK_DEFAULT_PLAN ")"),
};
Expand Down Expand Up @@ -391,7 +391,7 @@ usage(const char *name, int help, int highlight, int columns)
for (i = 0; i < numberof(yjit_options); ++i)
SHOW(yjit_options[i]);
#endif
#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
printf("%s""MMTk options (experimental):%s\n", sb, se);
for (i = 0; i < numberof(mmtk_options); ++i)
SHOW(mmtk_options[i]);
Expand Down Expand Up @@ -1468,7 +1468,7 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt)
#endif
}
else if (is_option_with_optarg("mmtk", '-', true, false, false)) {
#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
FEATURE_SET(opt->features, FEATURE_BIT(mmtk));
rb_mmtk_post_process_opts(s);
#undef opt_match_noarg
Expand Down Expand Up @@ -1841,7 +1841,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
FEATURE_SET_RESTORE(opt->warn, warn);
}

#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
rb_mmtk_post_process_opts_finish(FEATURE_SET_P(opt->features, mmtk));
#endif

Expand Down
12 changes: 7 additions & 5 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
#include "ruby_assert.h"
#include "vm_sync.h"

#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
#include "mmtk.h"
#endif // USE_THIRD_PARTY_HEAP
#endif

#if defined HAVE_CRYPT_R
# if defined HAVE_CRYPT_H
Expand Down Expand Up @@ -475,9 +475,11 @@ fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t data, int exist
}
RBASIC(str)->flags |= RSTRING_FSTR;

#ifdef USE_THIRD_PARTY_HEAP
mmtk_register_finalizable((void *)str);
#endif // USE_THIRD_PARTY_HEAP
#if USE_MMTK
if (rb_mmtk_enabled_p()) {
mmtk_register_finalizable((void *)str);
}
#endif

*key = *value = arg->fstr = str;
return ST_CONTINUE;
Expand Down
2 changes: 1 addition & 1 deletion test/lib/jit_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def eval_with_jit_without_retry(env = nil, script, verbose: 0, min_calls: 5, sav
end

def supported?
return false if defined?(GC::MMTk)
return false if defined?(GC::MMTk) && GC::MMTk.enabled?
return @supported if defined?(@supported)
@supported = RbConfig::CONFIG["MJIT_SUPPORT"] != 'no' && UNSUPPORTED_COMPILERS.all? do |regexp|
!regexp.match?(RbConfig::CONFIG['MJIT_CC'])
Expand Down
10 changes: 5 additions & 5 deletions thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@
#include "vm_debug.h"
#include "vm_sync.h"

#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
#include "mmtk.h"
#endif // USE_THIRD_PARTY_HEAP
#endif

#ifndef USE_NATIVE_THREAD_PRIORITY
#define USE_NATIVE_THREAD_PRIORITY 0
Expand Down Expand Up @@ -650,12 +650,12 @@ thread_start_func_2(rb_thread_t *th, VALUE *stack_start)
VM_ASSERT(th != th->vm->ractor.main_thread);
RUBY_DEBUG_LOG("th:%u", rb_th_serial(th));

#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
// Threads may be reused, so we only initialize MMTk mutator once.
if (th->mutator == NULL) {
if (rb_mmtk_enabled_p() && th->mutator == NULL) {
th->mutator = mmtk_bind_mutator((MMTk_VMMutatorThread)th);
}
#endif // USE_THIRD_PARTY_HEAP
#endif

// setup native thread
thread_sched_to_running(TH_SCHED(th), th);
Expand Down
3 changes: 2 additions & 1 deletion tool/m4/mmtk_ruby.m4
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ AS_IF([test -n "$with_mmtk_ruby"], [
AC_MSG_ERROR([$MMTK_RUBY_SO_NAME does not exist. $mmtk_ruby_build_suggestion])
])

AC_DEFINE([USE_THIRD_PARTY_HEAP])
AC_DEFINE([USE_MMTK], [1])
AC_DEFINE([USE_TRANSIENT_HEAP], [0])

mmtk_ruby_so_realpath=$(realpath $mmtk_ruby_so_path)
Expand All @@ -78,6 +78,7 @@ AS_IF([test -n "$with_mmtk_ruby"], [
AC_SUBST([mmtk_ruby_lib_dir])
], [
AC_MSG_RESULT([no])
AC_DEFINE([USE_MMTK], [0])
gc_support="Ruby's built-in GC"
])

Expand Down
11 changes: 6 additions & 5 deletions version.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#include "yjit.h"
#include <stdio.h>

#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
#include "gc.h"
#include "mmtk.h"
#endif

Expand Down Expand Up @@ -115,10 +116,10 @@ Init_ruby_description(void)
ruby_description_pre,
MJIT_OPTS_ON ? " +MJIT" : "",
rb_yjit_enabled_p() ? " +YJIT" : "",
#ifdef USE_THIRD_PARTY_HEAP
" +MMTk(",
mmtk_plan_name(),
")",
#if USE_MMTK
rb_mmtk_enabled_p() ? " +MMTk(" : "",
rb_mmtk_enabled_p() ? mmtk_plan_name() : "",
rb_mmtk_enabled_p() ? ")" : "",
#else
"", "", "",
#endif
Expand Down
19 changes: 10 additions & 9 deletions vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -3072,13 +3072,12 @@ rb_execution_context_mark(const rb_execution_context_t *ec)
}

/* mark machine stack */
if (ec->machine.stack_start && ec->machine.stack_end &&
#ifdef USE_THIRD_PARTY_HEAP
true // When using MMTk, stacks are marked by a GC worker thread which doesn't have "current ec".
#else // USE_THIRD_PARTY_HEAP
if (ec->machine.stack_start && ec->machine.stack_end && (
#if USE_MMTK
rb_mmtk_enabled_p() || // When using MMTk, stacks are marked by a GC worker thread which doesn't have "current ec".
#endif
ec != GET_EC() /* marked for current ec at the first stage of marking */
#endif // USE_THIRD_PARTY_HEAP
) {
)) {
rb_gc_mark_machine_stack(ec);
rb_gc_mark_locations((VALUE *)&ec->machine.regs,
(VALUE *)(&ec->machine.regs) +
Expand Down Expand Up @@ -3927,9 +3926,11 @@ Init_BareVM(void)
rb_native_cond_initialize(&vm->ractor.sync.barrier_cond);
rb_native_cond_initialize(&vm->ractor.sync.terminate_cond);

#ifdef USE_THIRD_PARTY_HEAP
rb_gc_init_collection();
#endif // USE_THIRD_PARTY_HEAP
#if USE_MMTK
if (rb_mmtk_enabled_p()) {
rb_gc_init_collection();
}
#endif
}

void
Expand Down
18 changes: 13 additions & 5 deletions vm_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ extern int ruby_assert_critical_section_entered;

#include "ruby/thread_native.h"

#if USE_MMTK
#include "gc.h"
#endif

/*
* implementation selector of get_insn_info algorithm
* 0: linear search
Expand Down Expand Up @@ -1087,9 +1091,9 @@ typedef struct rb_thread_struct {

struct rb_ext_config ext_config;

#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
void* mutator;
#endif // USE_THIRD_PARTY_HEAP
#endif
} rb_thread_t;

static inline unsigned int
Expand Down Expand Up @@ -1417,9 +1421,13 @@ static inline VALUE
VM_ENV_ENVVAL(const VALUE *ep)
{
VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
#ifndef USE_THIRD_PARTY_HEAP
VM_ASSERT(VM_ENV_ESCAPED_P(ep));
#endif // USE_THIRD_PARTY_HEAP
#if USE_MMTK
if (!rb_mmtk_enabled_p()) {
#endif
VM_ASSERT(VM_ENV_ESCAPED_P(ep));
#if USE_MMTK
}
#endif
VM_ASSERT(vm_assert_env(envval));
return envval;
}
Expand Down
4 changes: 2 additions & 2 deletions vm_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "vm_core.h"
#include "ractor_core.h"

#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
#include "mmtk.h"
#endif

Expand Down Expand Up @@ -1194,7 +1194,7 @@ rb_vm_bugreport(const void *ctx)
#endif
}

#ifdef USE_THIRD_PARTY_HEAP
#if USE_MMTK
fprintf(stderr, "* MMTk:\n\n");
fprintf(stderr, " mmtk_plan_name: %s\n", mmtk_plan_name());
fprintf(stderr, " mmtk_free_bytes: %zu\n", mmtk_free_bytes());
Expand Down