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

0 commit comments

Comments
 (0)