Skip to content

Commit 06caa59

Browse files
eightbitraptormatzbot
authored andcommitted
[ruby/mmtk] Introduce support for ractor_belonging.
This is a debug mode in Ruby where an extra word is used after each object to store the address of the Ractor that owns the object, used for debug purposes only. While we're working on Ractors, we also need to be able to test with MMTk enabled, so we should introduce support for this to the MMTk binding as well. As implemented we'll default the binding options to have everything disabled and hardcoded to 0, as was always the case, but if RACTOR_CHECK_MODE is enabled, we'll build and pass a valid RubyBinding object to MMTk. ruby/mmtk@83cb291313
1 parent 9fefb48 commit 06caa59

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

gc/mmtk/mmtk.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
#include <sys/sysctl.h>
1717
#endif
1818

19+
#ifndef VM_CHECK_MODE
20+
# define VM_CHECK_MODE RUBY_DEBUG
21+
#endif
22+
23+
// From ractor_core.h
24+
#ifndef RACTOR_CHECK_MODE
25+
# define RACTOR_CHECK_MODE (VM_CHECK_MODE || RUBY_DEBUG) && (SIZEOF_UINT64_T == SIZEOF_VALUE)
26+
#endif
27+
28+
#if RACTOR_CHECK_MODE
29+
# define RVALUE_SUFFIX_SIZE sizeof(VALUE)
30+
void rb_ractor_setup_belonging(VALUE obj);
31+
#else
32+
# define RVALUE_SUFFIX_SIZE 0
33+
#endif
34+
1935
struct objspace {
2036
bool measure_gc_time;
2137
bool gc_stress;
@@ -557,7 +573,11 @@ void *
557573
rb_gc_impl_objspace_alloc(void)
558574
{
559575
MMTk_Builder *builder = rb_mmtk_builder_init();
560-
mmtk_init_binding(builder, NULL, &ruby_upcalls);
576+
MMTk_RubyBindingOptions binding_options = {
577+
.ractor_check_mode = RACTOR_CHECK_MODE != 0,
578+
.suffix_size = RVALUE_SUFFIX_SIZE,
579+
};
580+
mmtk_init_binding(builder, &binding_options, &ruby_upcalls);
561581

562582
return calloc(1, sizeof(struct objspace));
563583
}
@@ -885,15 +905,16 @@ rb_gc_impl_new_obj(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags
885905
mmtk_handle_user_collection_request(ractor_cache, false, false);
886906
}
887907

888-
alloc_size += sizeof(VALUE);
908+
// Layout: [hidden size header (sizeof(VALUE))][payload (alloc_size)][suffix (RVALUE_SUFFIX_SIZE)]
909+
alloc_size += sizeof(VALUE) + RVALUE_SUFFIX_SIZE;
889910

890911
VALUE *alloc_obj = (VALUE *)rb_mmtk_alloc_fast_path(objspace, ractor_cache, alloc_size);
891912
if (!alloc_obj) {
892913
alloc_obj = mmtk_alloc(ractor_cache->mutator, alloc_size, MMTk_MIN_OBJ_ALIGN, 0, MMTK_ALLOCATION_SEMANTICS_DEFAULT);
893914
}
894915

895916
alloc_obj++;
896-
alloc_obj[-1] = alloc_size - sizeof(VALUE);
917+
alloc_obj[-1] = alloc_size - sizeof(VALUE) - RVALUE_SUFFIX_SIZE;
897918
alloc_obj[0] = flags;
898919
alloc_obj[1] = klass;
899920

@@ -905,6 +926,10 @@ rb_gc_impl_new_obj(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags
905926

906927
objspace->total_allocated_objects++;
907928

929+
#if RACTOR_CHECK_MODE
930+
rb_ractor_setup_belonging((VALUE)alloc_obj);
931+
#endif
932+
908933
return (VALUE)alloc_obj;
909934
}
910935

gc/mmtk/mmtk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ bool mmtk_is_reachable(MMTk_ObjectReference object);
9595
MMTk_Builder *mmtk_builder_default(void);
9696

9797
void mmtk_init_binding(MMTk_Builder *builder,
98-
const struct MMTk_RubyBindingOptions *_binding_options,
98+
const struct MMTk_RubyBindingOptions *binding_options,
9999
const struct MMTk_RubyUpcalls *upcalls);
100100

101101
void mmtk_initialize_collection(MMTk_VMThread tls);

gc/mmtk/src/api.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub extern "C" fn mmtk_builder_default() -> *mut MMTKBuilder {
181181
#[no_mangle]
182182
pub unsafe extern "C" fn mmtk_init_binding(
183183
builder: *mut MMTKBuilder,
184-
_binding_options: *const RubyBindingOptions,
184+
binding_options: *const RubyBindingOptions,
185185
upcalls: *const RubyUpcalls,
186186
) {
187187
crate::MUTATOR_THREAD_PANIC_HANDLER
@@ -191,9 +191,13 @@ pub unsafe extern "C" fn mmtk_init_binding(
191191
crate::set_panic_hook();
192192

193193
let builder: Box<MMTKBuilder> = unsafe { Box::from_raw(builder) };
194-
let binding_options = RubyBindingOptions {
195-
ractor_check_mode: false,
196-
suffix_size: 0,
194+
let binding_options = if binding_options.is_null() {
195+
RubyBindingOptions {
196+
ractor_check_mode: false,
197+
suffix_size: 0,
198+
}
199+
} else {
200+
unsafe { (*binding_options).clone() }
197201
};
198202
let mmtk_boxed = mmtk_init(&builder);
199203
let mmtk_static = Box::leak(Box::new(mmtk_boxed));

0 commit comments

Comments
 (0)