Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
57 changes: 57 additions & 0 deletions gc/mmtk/mmtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ struct objspace {
pthread_cond_t cond_world_started;
size_t start_the_world_count;

struct {
bool gc_thread_crashed;
char crash_msg[256];
} crash_context;

struct rb_gc_vm_context vm_context;

unsigned int fork_hook_vm_lock_lev;
Expand Down Expand Up @@ -72,6 +77,8 @@ struct MMTk_final_job {

#ifdef RB_THREAD_LOCAL_SPECIFIER
RB_THREAD_LOCAL_SPECIFIER struct MMTk_GCThreadTLS *rb_mmtk_gc_thread_tls;

RB_THREAD_LOCAL_SPECIFIER VALUE marking_parent_object;
#else
# error We currently need language-supported TLS
#endif
Expand Down Expand Up @@ -167,6 +174,10 @@ rb_mmtk_block_for_gc(MMTk_VMMutatorThread mutator)
pthread_cond_wait(&objspace->cond_world_started, &objspace->mutex);
}

if (RB_UNLIKELY(objspace->crash_context.gc_thread_crashed)) {
rb_bug("%s", objspace->crash_context.crash_msg);
}

if (objspace->measure_gc_time) {
struct timespec gc_end_time;
clock_gettime(CLOCK_MONOTONIC, &gc_end_time);
Expand Down Expand Up @@ -270,26 +281,34 @@ rb_mmtk_update_object_references(MMTk_ObjectReference mmtk_object)
VALUE object = (VALUE)mmtk_object;

if (!RB_FL_TEST(object, RUBY_FL_WEAK_REFERENCE)) {
marking_parent_object = object;
rb_gc_update_object_references(rb_gc_get_objspace(), object);
marking_parent_object = 0;
}
}

static void
rb_mmtk_call_gc_mark_children(MMTk_ObjectReference object)
{
marking_parent_object = (VALUE)object;
rb_gc_mark_children(rb_gc_get_objspace(), (VALUE)object);
marking_parent_object = 0;
}

static void
rb_mmtk_handle_weak_references(MMTk_ObjectReference mmtk_object, bool moving)
{
VALUE object = (VALUE)mmtk_object;

marking_parent_object = object;

rb_gc_handle_weak_references(object);

if (moving) {
rb_gc_update_object_references(rb_gc_get_objspace(), object);
}

marking_parent_object = 0;
}

static void
Expand Down Expand Up @@ -414,6 +433,32 @@ rb_mmtk_special_const_p(MMTk_ObjectReference object)
return RB_SPECIAL_CONST_P(obj);
}

RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 2)
static void
rb_mmtk_gc_thread_bug(const char *msg, ...)
{
struct objspace *objspace = rb_gc_get_objspace();

objspace->crash_context.gc_thread_crashed = true;

va_list args;
va_start(args, msg);
vsnprintf(objspace->crash_context.crash_msg, sizeof(objspace->crash_context.crash_msg), msg, args);
va_end(args);

rb_mmtk_resume_mutators();

sleep(5);

rb_bug("rb_mmtk_gc_thread_bug");
}

static void
rb_mmtk_gc_thread_panic_handler(void)
{
rb_mmtk_gc_thread_bug("MMTk GC thread panicked");
}

static void
rb_mmtk_mutator_thread_panic_handler(void)
{
Expand Down Expand Up @@ -444,6 +489,7 @@ MMTk_RubyUpcalls ruby_upcalls = {
rb_mmtk_update_finalizer_table,
rb_mmtk_special_const_p,
rb_mmtk_mutator_thread_panic_handler,
rb_mmtk_gc_thread_panic_handler,
};

// Use max 80% of the available memory by default for MMTk
Expand Down Expand Up @@ -797,6 +843,17 @@ void rb_gc_impl_adjust_memory_usage(void *objspace_ptr, ssize_t diff) { }
static inline VALUE
rb_mmtk_call_object_closure(VALUE obj, bool pin)
{
if (RB_UNLIKELY(RB_BUILTIN_TYPE(obj) == T_NONE)) {
const size_t info_size = 256;
char obj_info_buf[info_size];
rb_raw_obj_info(obj_info_buf, info_size, obj);

char parent_obj_info_buf[info_size];
rb_raw_obj_info(parent_obj_info_buf, info_size, marking_parent_object);

rb_mmtk_gc_thread_bug("try to mark T_NONE object (obj: %s, parent: %s)", obj_info_buf, parent_obj_info_buf);
}

return (VALUE)rb_mmtk_gc_thread_tls->object_closure.c_function(
rb_mmtk_gc_thread_tls->object_closure.rust_closure,
rb_mmtk_gc_thread_tls->gc_context,
Expand Down
1 change: 1 addition & 0 deletions gc/mmtk/mmtk.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ typedef struct MMTk_RubyUpcalls {
void (*update_finalizer_table)(void);
bool (*special_const_p)(MMTk_ObjectReference object);
void (*mutator_thread_panic_handler)(void);
void (*gc_thread_panic_handler)(void);
} MMTk_RubyUpcalls;

typedef struct MMTk_RawVecOfObjRef {
Expand Down
1 change: 1 addition & 0 deletions gc/mmtk/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ pub struct RubyUpcalls {
pub update_finalizer_table: extern "C" fn(),
pub special_const_p: extern "C" fn(object: ObjectReference) -> bool,
pub mutator_thread_panic_handler: extern "C" fn(),
pub gc_thread_panic_handler: extern "C" fn(),
}

unsafe impl Sync for RubyUpcalls {}
Expand Down
4 changes: 2 additions & 2 deletions gc/mmtk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ fn handle_gc_thread_panic(panic_info: &PanicHookInfo) {
eprintln!("Unknown backtrace status: {s:?}");
}
}

std::process::abort();
}

pub(crate) fn set_panic_hook() {
Expand All @@ -140,6 +138,8 @@ pub(crate) fn set_panic_hook() {
std::panic::set_hook(Box::new(move |panic_info| {
if is_gc_thread(std::thread::current().id()) {
handle_gc_thread_panic(panic_info);

(crate::binding().upcalls().gc_thread_panic_handler)();
} else {
old_hook(panic_info);
(crate::MUTATOR_THREAD_PANIC_HANDLER
Expand Down
2 changes: 1 addition & 1 deletion test/ruby/test_settracefunc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,7 @@ def method_for_test_thread_add_trace_func
def test_thread_add_trace_func
events = []
base_line = __LINE__
q = Thread::Queue.new
q = []
t = Thread.new{
Thread.current.add_trace_func proc{|ev, file, line, *args|
events << [ev, line] if file == __FILE__
Expand Down
Loading