Skip to content

Commit

Permalink
Use MMTk builder API (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
qinsoon authored Jul 21, 2022
1 parent d1a1c16 commit a03bdf2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion mmtk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ log = {version = "0.4", features = ["max_level_trace", "release_max_level_off"]
# - change branch/rev
# - change repo name
# But other changes including adding/removing whitespaces in commented lines may break the CI.
mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "f0fed5fc01438162115120c7551d4eb96e888bc2" }
mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "9a3ebff00d006388fb6369dd7c78a9b2cb74f647" }
# Uncomment the following to build locally - if you change the path locally, do not commit the change in a PR
# mmtk = { path = "../repos/mmtk-core" }

Expand Down
38 changes: 30 additions & 8 deletions mmtk/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use mmtk::util::{Address, ObjectReference};
use mmtk::vm::{ReferenceGlue, VMBinding};
use mmtk::AllocationSemantics;
use mmtk::Mutator;
use mmtk::MMTK;
use std::ffi::CStr;
use std::sync::atomic::Ordering;
use JikesRVM;
use BUILDER;
use JTOC_BASE;
use SINGLETON;

Expand All @@ -29,12 +30,32 @@ pub extern "C" fn jikesrvm_gc_init(jtoc: *mut c_void, heap_size: usize) {
JTOC_BASE = Address::from_mut_ptr(jtoc);
BOOT_THREAD = OpaquePointer::from_address(VMCollection::thread_from_id(1));
}
// MMTk should not be used before gc_init, and gc_init is single threaded. It is fine we get a mutable reference from the singleton.
#[allow(clippy::cast_ref_to_mut)]
let singleton_mut =
unsafe { &mut *(&*SINGLETON as *const MMTK<JikesRVM> as *mut MMTK<JikesRVM>) };
memory_manager::gc_init(singleton_mut, heap_size);
debug_assert!(731 == JikesRVM::mm_entrypoint_test(21, 34, 9, 8));

{
use mmtk::util::options::PlanSelector;
// set heap size
let mut builder = BUILDER.lock().unwrap();
let success = builder.options.heap_size.set(heap_size);
assert!(success, "Failed to set heap size to {}", heap_size);

// set plan based on features.
let plan = if cfg!(feature = "nogc") {
PlanSelector::NoGC
} else if cfg!(feature = "semispace") {
PlanSelector::SemiSpace
} else if cfg!(feature = "marksweep") {
PlanSelector::MarkSweep
} else {
panic!("No plan feature is enabled for JikesRVM. JikesRVM requires one plan feature to build.")
};
let success = builder.options.plan.set(plan);
assert!(success, "Failed to set plan to {:?}", plan);
}

// Make sure that we haven't initialized MMTk (by accident) yet
assert!(!crate::MMTK_INITIALIZED.load(Ordering::Relaxed));
// Make sure we initialize MMTk here
lazy_static::initialize(&SINGLETON);
}

#[no_mangle]
Expand Down Expand Up @@ -215,8 +236,9 @@ pub extern "C" fn harness_end(_tls: OpaquePointer) {
pub extern "C" fn process(name: *const c_char, value: *const c_char) -> i32 {
let name_str: &CStr = unsafe { CStr::from_ptr(name) };
let value_str: &CStr = unsafe { CStr::from_ptr(value) };
let mut builder = BUILDER.lock().unwrap();
memory_manager::process(
&SINGLETON,
&mut builder,
name_str.to_str().unwrap(),
value_str.to_str().unwrap(),
) as i32
Expand Down
21 changes: 13 additions & 8 deletions mmtk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern crate log;
use mmtk::plan::PlanConstraints;
use mmtk::util::address::Address;
use mmtk::vm::VMBinding;
use mmtk::MMTKBuilder;
use mmtk::MMTK;

use collection::BOOT_THREAD;
Expand Down Expand Up @@ -76,15 +77,19 @@ pub const SELECTED_CONSTRAINTS: PlanConstraints = mmtk::plan::SS_CONSTRAINTS;
#[cfg(feature = "marksweep")]
pub const SELECTED_CONSTRAINTS: PlanConstraints = mmtk::plan::MS_CONSTRAINTS;

use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::Mutex;

pub static MMTK_INITIALIZED: AtomicBool = AtomicBool::new(false);

lazy_static! {
pub static ref BUILDER: Mutex<MMTKBuilder> = Mutex::new(MMTKBuilder::new());
pub static ref SINGLETON: MMTK<JikesRVM> = {
#[cfg(feature = "nogc")]
std::env::set_var("MMTK_PLAN", "NoGC");
#[cfg(feature = "semispace")]
std::env::set_var("MMTK_PLAN", "SemiSpace");
#[cfg(feature = "marksweep")]
std::env::set_var("MMTK_PLAN", "MarkSweep");

MMTK::new()
let builder = BUILDER.lock().unwrap();
assert!(!MMTK_INITIALIZED.load(Ordering::SeqCst));
let ret = mmtk::memory_manager::mmtk_init(&builder);
MMTK_INITIALIZED.store(true, std::sync::atomic::Ordering::SeqCst);
*ret
};
}

0 comments on commit a03bdf2

Please sign in to comment.