Skip to content

Commit 52680f9

Browse files
authored
Update MMTk core PR #817 (alternative approach) (#220)
This is an alternative approach for #218 that addresses the upstream API change in mmtk/mmtk-core#817. This PR simply saves the mutator pointers into a `VecDeque` and embeds it into the iterator.
1 parent b80877c commit 52680f9

File tree

7 files changed

+125
-87
lines changed

7 files changed

+125
-87
lines changed

mmtk/Cargo.lock

Lines changed: 61 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mmtk/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ once_cell = "1.10.0"
3030
# - change branch
3131
# - change repo name
3232
# But other changes including adding/removing whitespaces in commented lines may break the CI.
33-
mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "df146b7af6cf41cc7d6996e1ca538fd2b32950f5" }
33+
mmtk = { git = "https://github.com/mmtk/mmtk-core.git", rev = "de8bbfe623eee5e915f32870de8f81128df8ad41" }
3434
# Uncomment the following to build locally
3535
# mmtk = { path = "../repos/mmtk-core" }
3636

mmtk/src/active_plan.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
1+
use crate::MutatorClosure;
12
use crate::OpenJDK;
23
use crate::SINGLETON;
34
use crate::UPCALLS;
45
use mmtk::util::opaque_pointer::*;
56
use mmtk::vm::ActivePlan;
67
use mmtk::Mutator;
78
use mmtk::Plan;
8-
use std::sync::Mutex;
9+
use std::collections::VecDeque;
10+
use std::marker::PhantomData;
11+
12+
struct OpenJDKMutatorIterator<'a> {
13+
mutators: VecDeque<&'a mut Mutator<OpenJDK>>,
14+
phantom_data: PhantomData<&'a ()>,
15+
}
16+
17+
impl<'a> OpenJDKMutatorIterator<'a> {
18+
fn new() -> Self {
19+
let mut mutators = VecDeque::new();
20+
unsafe {
21+
((*UPCALLS).get_mutators)(MutatorClosure::from_rust_closure(&mut |mutator| {
22+
mutators.push_back(mutator);
23+
}));
24+
}
25+
Self {
26+
mutators,
27+
phantom_data: PhantomData,
28+
}
29+
}
30+
}
31+
32+
impl<'a> Iterator for OpenJDKMutatorIterator<'a> {
33+
type Item = &'a mut Mutator<OpenJDK>;
34+
35+
fn next(&mut self) -> Option<Self::Item> {
36+
self.mutators.pop_front()
37+
}
38+
}
939

1040
pub struct VMActivePlan {}
1141

@@ -25,29 +55,11 @@ impl ActivePlan<OpenJDK> for VMActivePlan {
2555
}
2656
}
2757

28-
fn reset_mutator_iterator() {
29-
unsafe {
30-
((*UPCALLS).reset_mutator_iterator)();
31-
}
32-
}
33-
34-
fn get_next_mutator() -> Option<&'static mut Mutator<OpenJDK>> {
35-
let _guard = MUTATOR_ITERATOR_LOCK.lock().unwrap();
36-
unsafe {
37-
let m = ((*UPCALLS).get_next_mutator)();
38-
if m.is_null() {
39-
None
40-
} else {
41-
Some(&mut *m)
42-
}
43-
}
58+
fn mutators<'a>() -> Box<dyn Iterator<Item = &'a mut Mutator<OpenJDK>> + 'a> {
59+
Box::new(OpenJDKMutatorIterator::new())
4460
}
4561

4662
fn number_of_mutators() -> usize {
4763
unsafe { ((*UPCALLS).number_of_mutators)() }
4864
}
4965
}
50-
51-
lazy_static! {
52-
pub static ref MUTATOR_ITERATOR_LOCK: Mutex<()> = Mutex::new(());
53-
}

mmtk/src/collection.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,6 @@ use crate::{MutatorClosure, OpenJDK};
88

99
pub struct VMCollection {}
1010

11-
extern "C" fn report_mutator_stop<F>(
12-
mutator: *mut Mutator<OpenJDK>,
13-
callback_ptr: *mut libc::c_void,
14-
) where
15-
F: FnMut(&'static mut Mutator<OpenJDK>),
16-
{
17-
let callback: &mut F = unsafe { &mut *(callback_ptr as *mut F) };
18-
callback(unsafe { &mut *mutator });
19-
}
20-
21-
fn to_mutator_closure<F>(callback: &mut F) -> MutatorClosure
22-
where
23-
F: FnMut(&'static mut Mutator<OpenJDK>),
24-
{
25-
MutatorClosure {
26-
func: report_mutator_stop::<F>,
27-
data: callback as *mut F as *mut libc::c_void,
28-
}
29-
}
30-
3111
const GC_THREAD_KIND_CONTROLLER: libc::c_int = 0;
3212
const GC_THREAD_KIND_WORKER: libc::c_int = 1;
3313

@@ -43,7 +23,7 @@ impl Collection<OpenJDK> for VMCollection {
4323
((*UPCALLS).stop_all_mutators)(
4424
tls,
4525
scan_mutators_in_safepoint,
46-
to_mutator_closure(&mut mutator_visitor),
26+
MutatorClosure::from_rust_closure(&mut mutator_visitor),
4727
);
4828
}
4929
}

mmtk/src/lib.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ pub struct MutatorClosure {
3939
pub data: *mut libc::c_void,
4040
}
4141

42+
impl MutatorClosure {
43+
fn from_rust_closure<F>(callback: &mut F) -> Self
44+
where
45+
F: FnMut(&'static mut Mutator<OpenJDK>),
46+
{
47+
Self {
48+
func: Self::call_rust_closure::<F>,
49+
data: callback as *mut F as *mut libc::c_void,
50+
}
51+
}
52+
53+
extern "C" fn call_rust_closure<F>(
54+
mutator: *mut Mutator<OpenJDK>,
55+
callback_ptr: *mut libc::c_void,
56+
) where
57+
F: FnMut(&'static mut Mutator<OpenJDK>),
58+
{
59+
let callback: &mut F = unsafe { &mut *(callback_ptr as *mut F) };
60+
callback(unsafe { &mut *mutator });
61+
}
62+
}
63+
4264
/// A closure for reporting root edges. The C++ code should pass `data` back as the last argument.
4365
#[repr(C)]
4466
pub struct EdgesClosure {
@@ -62,8 +84,7 @@ pub struct OpenJDK_Upcalls {
6284
pub spawn_gc_thread: extern "C" fn(tls: VMThread, kind: libc::c_int, ctx: *mut libc::c_void),
6385
pub block_for_gc: extern "C" fn(),
6486
pub out_of_memory: extern "C" fn(tls: VMThread, err_kind: AllocationError),
65-
pub get_next_mutator: extern "C" fn() -> *mut Mutator<OpenJDK>,
66-
pub reset_mutator_iterator: extern "C" fn(),
87+
pub get_mutators: extern "C" fn(closure: MutatorClosure),
6788
pub scan_object: extern "C" fn(trace: *mut c_void, object: ObjectReference, tls: OpaquePointer),
6889
pub dump_object: extern "C" fn(object: ObjectReference),
6990
pub get_object_size: extern "C" fn(object: ObjectReference) -> usize,

openjdk/mmtk.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ typedef struct {
149149
void (*spawn_gc_thread) (void *tls, int kind, void *ctx);
150150
void (*block_for_gc) ();
151151
void (*out_of_memory) (void *tls, MMTkAllocationError err_kind);
152-
void* (*get_next_mutator) ();
153-
void (*reset_mutator_iterator) ();
152+
void (*get_mutators) (MutatorClosure closure);
154153
void (*scan_object) (void* trace, void* object, void* tls);
155154
void (*dump_object) (void* object);
156155
size_t (*get_object_size) (void* object);

openjdk/mmtkUpcalls.cpp

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -177,40 +177,13 @@ static bool mmtk_is_mutator(void* tls) {
177177
return ((Thread*) tls)->third_party_heap_collector == NULL;
178178
}
179179

180-
template <class T>
181-
struct MaybeUninit {
182-
MaybeUninit() {}
183-
T* operator->() {
184-
return (T*) &_data;
180+
static void mmtk_get_mutators(MutatorClosure closure) {
181+
JavaThread *thr;
182+
for (JavaThreadIteratorWithHandle jtiwh; thr = jtiwh.next();) {
183+
closure.invoke(&thr->third_party_heap_mutator);
185184
}
186-
T& operator*() {
187-
return *((T*) &_data);
188-
}
189-
private:
190-
char _data[sizeof(T)];
191-
};
192-
193-
static MaybeUninit<JavaThreadIteratorWithHandle> jtiwh;
194-
static bool mutator_iteration_start = true;
195-
196-
static void* mmtk_get_next_mutator() {
197-
if (mutator_iteration_start) {
198-
*jtiwh = JavaThreadIteratorWithHandle();
199-
mutator_iteration_start = false;
200-
}
201-
JavaThread *thr = jtiwh->next();
202-
if (thr == NULL) {
203-
mutator_iteration_start = true;
204-
return NULL;
205-
}
206-
return (void*) &thr->third_party_heap_mutator;
207-
}
208-
209-
static void mmtk_reset_mutator_iterator() {
210-
mutator_iteration_start = true;
211185
}
212186

213-
214187
static void mmtk_scan_all_thread_roots(EdgesClosure closure) {
215188
MMTkRootsClosure2 cl(closure);
216189
MMTkHeap::heap()->scan_thread_roots(cl);
@@ -347,8 +320,7 @@ OpenJDK_Upcalls mmtk_upcalls = {
347320
mmtk_spawn_gc_thread,
348321
mmtk_block_for_gc,
349322
mmtk_out_of_memory,
350-
mmtk_get_next_mutator,
351-
mmtk_reset_mutator_iterator,
323+
mmtk_get_mutators,
352324
mmtk_scan_object,
353325
mmtk_dump_object,
354326
mmtk_get_object_size,

0 commit comments

Comments
 (0)