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

Unique object enqueuing option #1255

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
55 changes: 53 additions & 2 deletions src/policy/marksweepspace/native_ms/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,58 @@ impl<VM: VMBinding> MarkSweepSpace<VM> {
}
}

/// Mark an object non-atomically. If multiple GC worker threads attempt to mark the same
/// object, more than one of them may return `true`.
fn attempt_mark_non_atomic(&self, object: ObjectReference) -> bool {
if !VM::VMObjectModel::LOCAL_MARK_BIT_SPEC.is_marked::<VM>(object, Ordering::SeqCst) {
VM::VMObjectModel::LOCAL_MARK_BIT_SPEC.mark::<VM>(object, Ordering::SeqCst);
true
} else {
false
}
}

/// Mark an object atomically.
fn attempt_mark_atomic(&self, object: ObjectReference) -> bool {
let mark_state = 1u8;

loop {
let old_value = VM::VMObjectModel::LOCAL_MARK_BIT_SPEC.load_atomic::<VM, u8>(
object,
None,
Ordering::SeqCst,
);
if old_value == mark_state {
return false;
}

if VM::VMObjectModel::LOCAL_MARK_BIT_SPEC
.compare_exchange_metadata::<VM, u8>(
object,
old_value,
mark_state,
None,
Ordering::SeqCst,
Ordering::SeqCst,
)
.is_ok()
{
break;
}
}
true
}

/// Mark an object. Return `true` if the object is newly marked. Return `false` if the object
/// was already marked.
fn attempt_mark(&self, object: ObjectReference) -> bool {
if VM::UNIQUE_OBJECT_ENQUEUING {
self.attempt_mark_atomic(object)
} else {
self.attempt_mark_non_atomic(object)
}
}

fn trace_object<Q: ObjectQueue>(
&self,
queue: &mut Q,
Expand All @@ -339,8 +391,7 @@ impl<VM: VMBinding> MarkSweepSpace<VM> {
"Cannot mark an object {} that was not alloced by free list allocator.",
object,
);
if !VM::VMObjectModel::LOCAL_MARK_BIT_SPEC.is_marked::<VM>(object, Ordering::SeqCst) {
VM::VMObjectModel::LOCAL_MARK_BIT_SPEC.mark::<VM>(object, Ordering::SeqCst);
if self.attempt_mark(object) {
let block = Block::containing(object);
block.set_state(BlockState::Marked);
queue.enqueue(object);
Expand Down
15 changes: 15 additions & 0 deletions src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,19 @@ where
/// Note that MMTk does not attempt to do anything to align the cursor to this value, but
/// it merely asserts with this constant.
const ALLOC_END_ALIGNMENT: usize = 1;

/// When set to `true`, all plans will guarantee that during each GC, each live object is
/// enqueued at most once, and therefore scanned (by either [`Scanning::scan_object`] or
/// [`Scanning::scan_object_and_trace_edges`]) at most once.
///
/// When set to `false`, MMTk may enqueue an object multiple times due to optimizations, such as
/// using non-atomic operatios to mark objects. Consequently, an object may be scanned multiple
/// times during a GC.
///
/// The default value is `false` because duplicated object-enqueuing is benign for most VMs, and
/// related optimizations, such as non-atomic marking, can improve GC speed. VM bindings can
/// override this if they need. For example, some VMs piggyback on object-scanning to visit
/// objects during a GC, but may have data race if multiple GC workers visit the same object at
/// the same time. Such VMs can set this constant to `true` to workaround this problem.
const UNIQUE_OBJECT_ENQUEUING: bool = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move this to the Scanning trait. It seems only relevant for scanning.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I moved it to the Scanning trait.

}
Loading