Skip to content

Commit cf8e768

Browse files
authored
Make MMTk a runtime option (#14)
This commit makes MMTk a runtime option. Code related to MMTk is still by a macro (USE_MMTK, previously USE_THIRD_PARTY_HEAP), but MMTk also needs to be enabled at run time using command line options (`--mmtk` or `--enable-mmtk`), or environment variables (`RUBYOPT=--mmtk` or `MMTK_PLAN=...`)
1 parent e478bd3 commit cf8e768

File tree

13 files changed

+609
-491
lines changed

13 files changed

+609
-491
lines changed

gc.c

Lines changed: 538 additions & 444 deletions
Large diffs are not rendered by default.

gc.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,14 @@ VALUE rb_gc_disable_no_rest(void);
118118

119119
struct rb_thread_struct;
120120

121-
#ifdef USE_THIRD_PARTY_HEAP
121+
#if USE_MMTK
122122
#define MMTK_DEFAULT_PLAN "MarkSweep"
123-
void rb_gc_init_collection();
123+
void rb_gc_init_collection(void);
124124
void rb_mmtk_pre_process_opts(int argc, char **argv);
125125
void rb_mmtk_post_process_opts(const char *arg);
126-
void rb_mmtk_post_process_opts_finish(bool enable);
127-
#endif // USE_THIRD_PARTY_HEAP
126+
void rb_mmtk_post_process_opts_finish(bool feature_enable);
127+
bool rb_mmtk_enabled_p(void);
128+
#endif
128129

129130
RUBY_SYMBOL_EXPORT_BEGIN
130131

main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
# undef RUBY_DEBUG_ENV
3131
#endif
3232

33-
#ifdef USE_THIRD_PARTY_HEAP
33+
#if USE_MMTK
3434
#include "gc.h"
3535
#endif
3636

3737
static int
3838
rb_main(int argc, char **argv)
3939
{
4040
RUBY_INIT_STACK;
41-
#ifdef USE_THIRD_PARTY_HEAP
41+
#if USE_MMTK
4242
rb_mmtk_pre_process_opts(argc, argv);
4343
#endif
4444
ruby_init();

object.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
#include "ruby/assert.h"
4141
#include "builtin.h"
4242

43+
#if USE_MMTK
44+
#include "gc.h"
45+
#endif
46+
4347
/*!
4448
* \addtogroup object
4549
* \{
@@ -284,7 +288,13 @@ init_copy(VALUE dest, VALUE obj)
284288
}
285289
RBASIC(dest)->flags &= ~(T_MASK|FL_EXIVAR);
286290
RBASIC(dest)->flags |= RBASIC(obj)->flags & (T_MASK|FL_EXIVAR);
287-
rb_copy_wb_protected_attribute(dest, obj);
291+
#if USE_MMTK
292+
if (!rb_mmtk_enabled_p()) {
293+
#endif
294+
rb_copy_wb_protected_attribute(dest, obj);
295+
#if USE_MMTK
296+
}
297+
#endif
288298
rb_copy_generic_ivar(dest, obj);
289299
rb_gc_copy_finalizer(dest, obj);
290300
if (RB_TYPE_P(obj, T_OBJECT)) {

ruby.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
#include "ruby/version.h"
6262
#include "ruby/internal/error.h"
6363

64-
#ifdef USE_THIRD_PARTY_HEAP
64+
#if USE_MMTK
6565
#include "gc.h"
6666
#endif
6767

@@ -291,7 +291,7 @@ usage(const char *name, int help, int highlight, int columns)
291291
#if YJIT_BUILD
292292
M("--yjit", "", "enable in-process JIT compiler (experimental)"),
293293
#endif
294-
#ifdef USE_THIRD_PARTY_HEAP
294+
#if USE_MMTK
295295
M("--mmtk", "", "use MMTk for garbage collection (experimental)"),
296296
#endif
297297
M("-h", "", "show this message, --help for more info"),
@@ -328,7 +328,7 @@ usage(const char *name, int help, int highlight, int columns)
328328
#if YJIT_BUILD
329329
M("yjit", "", "in-process JIT compiler (default: disabled)"),
330330
#endif
331-
#ifdef USE_THIRD_PARTY_HEAP
331+
#if USE_MMTK
332332
M("mmtk", "", "MMTk garbage collection (default: disabled)"),
333333
#endif
334334
};
@@ -350,7 +350,7 @@ usage(const char *name, int help, int highlight, int columns)
350350
M("--yjit-greedy-versioning", "", "Greedy versioning mode (default: disabled)"),
351351
};
352352
#endif
353-
#ifdef USE_THIRD_PARTY_HEAP
353+
#if USE_MMTK
354354
static const struct ruby_opt_message mmtk_options[] = {
355355
M("--mmtk-plan=name", "", "MMTk garbage collection plan to use (default: " MMTK_DEFAULT_PLAN ")"),
356356
};
@@ -391,7 +391,7 @@ usage(const char *name, int help, int highlight, int columns)
391391
for (i = 0; i < numberof(yjit_options); ++i)
392392
SHOW(yjit_options[i]);
393393
#endif
394-
#ifdef USE_THIRD_PARTY_HEAP
394+
#if USE_MMTK
395395
printf("%s""MMTk options (experimental):%s\n", sb, se);
396396
for (i = 0; i < numberof(mmtk_options); ++i)
397397
SHOW(mmtk_options[i]);
@@ -1468,7 +1468,7 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt)
14681468
#endif
14691469
}
14701470
else if (is_option_with_optarg("mmtk", '-', true, false, false)) {
1471-
#ifdef USE_THIRD_PARTY_HEAP
1471+
#if USE_MMTK
14721472
FEATURE_SET(opt->features, FEATURE_BIT(mmtk));
14731473
rb_mmtk_post_process_opts(s);
14741474
#undef opt_match_noarg
@@ -1841,7 +1841,7 @@ process_options(int argc, char **argv, ruby_cmdline_options_t *opt)
18411841
FEATURE_SET_RESTORE(opt->warn, warn);
18421842
}
18431843

1844-
#ifdef USE_THIRD_PARTY_HEAP
1844+
#if USE_MMTK
18451845
rb_mmtk_post_process_opts_finish(FEATURE_SET_P(opt->features, mmtk));
18461846
#endif
18471847

string.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646
#include "ruby_assert.h"
4747
#include "vm_sync.h"
4848

49-
#ifdef USE_THIRD_PARTY_HEAP
49+
#if USE_MMTK
5050
#include "mmtk.h"
51-
#endif // USE_THIRD_PARTY_HEAP
51+
#endif
5252

5353
#if defined HAVE_CRYPT_R
5454
# if defined HAVE_CRYPT_H
@@ -475,9 +475,11 @@ fstr_update_callback(st_data_t *key, st_data_t *value, st_data_t data, int exist
475475
}
476476
RBASIC(str)->flags |= RSTRING_FSTR;
477477

478-
#ifdef USE_THIRD_PARTY_HEAP
479-
mmtk_register_finalizable((void *)str);
480-
#endif // USE_THIRD_PARTY_HEAP
478+
#if USE_MMTK
479+
if (rb_mmtk_enabled_p()) {
480+
mmtk_register_finalizable((void *)str);
481+
}
482+
#endif
481483

482484
*key = *value = arg->fstr = str;
483485
return ST_CONTINUE;

test/lib/jit_support.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def eval_with_jit_without_retry(env = nil, script, verbose: 0, min_calls: 5, sav
5656
end
5757

5858
def supported?
59-
return false if defined?(GC::MMTk)
59+
return false if defined?(GC::MMTk) && GC::MMTk.enabled?
6060
return @supported if defined?(@supported)
6161
@supported = RbConfig::CONFIG["MJIT_SUPPORT"] != 'no' && UNSUPPORTED_COMPILERS.all? do |regexp|
6262
!regexp.match?(RbConfig::CONFIG['MJIT_CC'])

thread.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@
100100
#include "vm_debug.h"
101101
#include "vm_sync.h"
102102

103-
#ifdef USE_THIRD_PARTY_HEAP
103+
#if USE_MMTK
104104
#include "mmtk.h"
105-
#endif // USE_THIRD_PARTY_HEAP
105+
#endif
106106

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

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

660660
// setup native thread
661661
thread_sched_to_running(TH_SCHED(th), th);

tool/m4/mmtk_ruby.m4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ AS_IF([test -n "$with_mmtk_ruby"], [
6666
AC_MSG_ERROR([$MMTK_RUBY_SO_NAME does not exist. $mmtk_ruby_build_suggestion])
6767
])
6868
69-
AC_DEFINE([USE_THIRD_PARTY_HEAP])
69+
AC_DEFINE([USE_MMTK], [1])
7070
AC_DEFINE([USE_TRANSIENT_HEAP], [0])
7171
7272
mmtk_ruby_so_realpath=$(realpath $mmtk_ruby_so_path)
@@ -78,6 +78,7 @@ AS_IF([test -n "$with_mmtk_ruby"], [
7878
AC_SUBST([mmtk_ruby_lib_dir])
7979
], [
8080
AC_MSG_RESULT([no])
81+
AC_DEFINE([USE_MMTK], [0])
8182
gc_support="Ruby's built-in GC"
8283
])
8384

version.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
#include "yjit.h"
1717
#include <stdio.h>
1818

19-
#ifdef USE_THIRD_PARTY_HEAP
19+
#if USE_MMTK
20+
#include "gc.h"
2021
#include "mmtk.h"
2122
#endif
2223

@@ -115,10 +116,10 @@ Init_ruby_description(void)
115116
ruby_description_pre,
116117
MJIT_OPTS_ON ? " +MJIT" : "",
117118
rb_yjit_enabled_p() ? " +YJIT" : "",
118-
#ifdef USE_THIRD_PARTY_HEAP
119-
" +MMTk(",
120-
mmtk_plan_name(),
121-
")",
119+
#if USE_MMTK
120+
rb_mmtk_enabled_p() ? " +MMTk(" : "",
121+
rb_mmtk_enabled_p() ? mmtk_plan_name() : "",
122+
rb_mmtk_enabled_p() ? ")" : "",
122123
#else
123124
"", "", "",
124125
#endif

vm.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3072,13 +3072,12 @@ rb_execution_context_mark(const rb_execution_context_t *ec)
30723072
}
30733073

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

3930-
#ifdef USE_THIRD_PARTY_HEAP
3931-
rb_gc_init_collection();
3932-
#endif // USE_THIRD_PARTY_HEAP
3929+
#if USE_MMTK
3930+
if (rb_mmtk_enabled_p()) {
3931+
rb_gc_init_collection();
3932+
}
3933+
#endif
39333934
}
39343935

39353936
void

vm_core.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ extern int ruby_assert_critical_section_entered;
102102

103103
#include "ruby/thread_native.h"
104104

105+
#if USE_MMTK
106+
#include "gc.h"
107+
#endif
108+
105109
/*
106110
* implementation selector of get_insn_info algorithm
107111
* 0: linear search
@@ -1087,9 +1091,9 @@ typedef struct rb_thread_struct {
10871091

10881092
struct rb_ext_config ext_config;
10891093

1090-
#ifdef USE_THIRD_PARTY_HEAP
1094+
#if USE_MMTK
10911095
void* mutator;
1092-
#endif // USE_THIRD_PARTY_HEAP
1096+
#endif
10931097
} rb_thread_t;
10941098

10951099
static inline unsigned int
@@ -1417,9 +1421,13 @@ static inline VALUE
14171421
VM_ENV_ENVVAL(const VALUE *ep)
14181422
{
14191423
VALUE envval = ep[VM_ENV_DATA_INDEX_ENV];
1420-
#ifndef USE_THIRD_PARTY_HEAP
1421-
VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1422-
#endif // USE_THIRD_PARTY_HEAP
1424+
#if USE_MMTK
1425+
if (!rb_mmtk_enabled_p()) {
1426+
#endif
1427+
VM_ASSERT(VM_ENV_ESCAPED_P(ep));
1428+
#if USE_MMTK
1429+
}
1430+
#endif
14231431
VM_ASSERT(vm_assert_env(envval));
14241432
return envval;
14251433
}

vm_dump.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include "vm_core.h"
3838
#include "ractor_core.h"
3939

40-
#ifdef USE_THIRD_PARTY_HEAP
40+
#if USE_MMTK
4141
#include "mmtk.h"
4242
#endif
4343

@@ -1194,7 +1194,7 @@ rb_vm_bugreport(const void *ctx)
11941194
#endif
11951195
}
11961196

1197-
#ifdef USE_THIRD_PARTY_HEAP
1197+
#if USE_MMTK
11981198
fprintf(stderr, "* MMTk:\n\n");
11991199
fprintf(stderr, " mmtk_plan_name: %s\n", mmtk_plan_name());
12001200
fprintf(stderr, " mmtk_free_bytes: %zu\n", mmtk_free_bytes());

0 commit comments

Comments
 (0)