Skip to content
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

Remove coordinator and support forking #53

Merged
merged 14 commits into from
Apr 9, 2024
34 changes: 17 additions & 17 deletions mmtk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions mmtk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ edition = "2021"
# Metadata for the Ruby repository
[package.metadata.ci-repos.ruby]
repo = "mmtk/ruby" # This is used by actions/checkout, so the format is "owner/repo", not URL.
rev = "6d1596f7a4401d267d2b208c42521edc35020a8e "
rev = "e173f879e6e4f1566cca83513e7bfeb4f39331a8"

[lib]
name = "mmtk_ruby"
Expand All @@ -37,7 +37,7 @@ features = ["is_mmtk_object", "object_pinning"]

# Uncomment the following lines to use mmtk-core from the official repository.
git = "https://github.com/mmtk/mmtk-core.git"
rev = "dd84218933deab50196fc77828cc1446cb829881"
rev = "5ab62f96c006475285b00b6d20a8b1bf83b74a4d"

# Uncomment the following line to use mmtk-core from a local repository.
#path = "../../mmtk-core"
Expand Down
9 changes: 2 additions & 7 deletions mmtk/src/abi.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::api::RubyMutator;
use crate::{upcalls, Ruby};
use mmtk::scheduler::{GCController, GCWorker};
use mmtk::scheduler::GCWorker;
use mmtk::util::{Address, ObjectReference, VMMutatorThread, VMWorkerThread};

// For the C binding
pub const OBJREF_OFFSET: usize = 8;
pub const MIN_OBJ_ALIGN: usize = 8; // Even on 32-bit machine. A Ruby object is at least 40 bytes large.

pub const GC_THREAD_KIND_CONTROLLER: libc::c_int = 0;
pub const GC_THREAD_KIND_WORKER: libc::c_int = 1;

const HAS_MOVED_GIVTBL: usize = 1 << 63;
Expand Down Expand Up @@ -244,10 +243,6 @@ impl GCThreadTLS {
}
}

pub fn for_controller(gc_context: *mut GCController<Ruby>) -> Self {
Self::new(GC_THREAD_KIND_CONTROLLER, gc_context as *mut libc::c_void)
}

pub fn for_worker(gc_context: *mut GCWorker<Ruby>) -> Self {
Self::new(GC_THREAD_KIND_WORKER, gc_context as *mut libc::c_void)
}
Expand All @@ -266,7 +261,7 @@ impl GCThreadTLS {
let result = &mut *ptr;
debug_assert!({
let kind = result.kind;
kind == GC_THREAD_KIND_CONTROLLER || kind == GC_THREAD_KIND_WORKER
kind == GC_THREAD_KIND_WORKER
});
result
}
Expand Down
11 changes: 11 additions & 0 deletions mmtk/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ pub extern "C" fn mmtk_initialize_collection(tls: VMThread) {
memory_manager::initialize_collection(mmtk(), tls)
}

#[no_mangle]
pub extern "C" fn mmtk_prepare_to_fork() {
mmtk().prepare_to_fork();
binding().join_all_gc_threads();
}

#[no_mangle]
pub extern "C" fn mmtk_after_fork(tls: VMThread) {
mmtk().after_fork(tls);
}

#[no_mangle]
pub extern "C" fn mmtk_enable_collection() {
BINDING_FAST.gc_enabled.store(true, Ordering::Relaxed);
Expand Down
19 changes: 19 additions & 0 deletions mmtk/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::ffi::CString;
use std::sync::atomic::AtomicBool;
use std::sync::Mutex;
use std::thread::JoinHandle;

use libc::c_void;
use mmtk::util::ObjectReference;
Expand Down Expand Up @@ -50,6 +51,7 @@ pub struct RubyBinding {
pub weak_proc: WeakProcessor,
pub ppp_registry: PPPRegistry,
pub(crate) moved_givtbl: Mutex<HashMap<ObjectReference, MovedGIVTblEntry>>,
pub gc_thread_join_handles: Mutex<Vec<JoinHandle<()>>>,
}

unsafe impl Sync for RubyBinding {}
Expand All @@ -72,6 +74,7 @@ impl RubyBinding {
weak_proc: WeakProcessor::new(),
ppp_registry: PPPRegistry::new(),
moved_givtbl: Default::default(),
gc_thread_join_handles: Default::default(),
}
}

Expand All @@ -89,4 +92,20 @@ impl RubyBinding {
}
plan_name.as_deref().unwrap().as_ptr()
}

pub fn join_all_gc_threads(&self) {
let handles = {
let mut guard = self.gc_thread_join_handles.lock().unwrap();
std::mem::take(&mut *guard)
};

debug!("Joining GC threads...");
let total = handles.len();
let mut joined = 0;
for handle in handles {
handle.join().unwrap();
joined += 1;
debug!("{joined}/{total} GC threads joined.");
}
}
}
76 changes: 30 additions & 46 deletions mmtk/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,53 +37,37 @@ impl Collection<Ruby> for VMCollection {
}

fn spawn_gc_thread(_tls: VMThread, ctx: GCThreadContext<Ruby>) {
match ctx {
GCThreadContext::Controller(mut controller) => {
thread::Builder::new()
.name("MMTk Controller Thread".to_string())
.spawn(move || {
debug!("Hello! This is MMTk Controller Thread running!");
crate::register_gc_thread(thread::current().id());
let ptr_controller = &mut *controller as *mut GCController<Ruby>;
let gc_thread_tls =
Box::into_raw(Box::new(GCThreadTLS::for_controller(ptr_controller)));
(upcalls().init_gc_worker_thread)(gc_thread_tls);
memory_manager::start_control_collector(
mmtk(),
GCThreadTLS::to_vwt(gc_thread_tls),
&mut controller,
);
let join_handle = match ctx {
GCThreadContext::Worker(mut worker) => thread::Builder::new()
.name("MMTk Worker Thread".to_string())
.spawn(move || {
let ordinal = worker.ordinal;
debug!(
"Hello! This is MMTk Worker Thread running! ordinal: {}",
ordinal
);
crate::register_gc_thread(thread::current().id());
let ptr_worker = &mut *worker as *mut GCWorker<Ruby>;
let gc_thread_tls =
Box::into_raw(Box::new(GCThreadTLS::for_worker(ptr_worker)));
(upcalls().init_gc_worker_thread)(gc_thread_tls);
memory_manager::start_worker(
mmtk(),
GCThreadTLS::to_vwt(gc_thread_tls),
worker,
);
debug!(
"An MMTk Worker Thread is quitting. Good bye! ordinal: {}",
ordinal
);
crate::unregister_gc_thread(thread::current().id());
})
.unwrap(),
};

// Currently the MMTk controller thread should run forever.
// This is an unlikely event, but we log it anyway.
warn!("The MMTk Controller Thread is quitting!");
crate::unregister_gc_thread(thread::current().id());
})
.unwrap();
}
GCThreadContext::Worker(mut worker) => {
thread::Builder::new()
.name("MMTk Worker Thread".to_string())
.spawn(move || {
debug!("Hello! This is MMTk Worker Thread running!");
crate::register_gc_thread(thread::current().id());
let ptr_worker = &mut *worker as *mut GCWorker<Ruby>;
let gc_thread_tls =
Box::into_raw(Box::new(GCThreadTLS::for_worker(ptr_worker)));
(upcalls().init_gc_worker_thread)(gc_thread_tls);
memory_manager::start_worker(
mmtk(),
GCThreadTLS::to_vwt(gc_thread_tls),
&mut worker,
);

// Currently all MMTk worker threads should run forever.
// This is an unlikely event, but we log it anyway.
warn!("An MMTk Worker Thread is quitting!");
crate::unregister_gc_thread(thread::current().id());
})
.unwrap();
}
{
let mut handles = crate::binding().gc_thread_join_handles.lock().unwrap();
handles.push(join_handle);
}
}

Expand Down
Loading