mmtk/plan/barriers.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
//! Read/Write barrier implementations.
use crate::vm::slot::{MemorySlice, Slot};
use crate::vm::ObjectModel;
use crate::{
util::{metadata::MetadataSpec, *},
vm::VMBinding,
};
use atomic::Ordering;
use downcast_rs::Downcast;
/// BarrierSelector describes which barrier to use.
///
/// This is used as an *indicator* for each plan to enable the correct barrier.
/// For example, immix can use this selector to enable different barriers for analysis.
///
/// VM bindings may also use this to enable the correct fast-path, if the fast-path is implemented in the binding.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum BarrierSelector {
/// No barrier is used.
NoBarrier,
/// Object remembering post-write barrier is used.
ObjectBarrier,
/// Object remembering pre-write barrier with weak reference loading barrier.
// TODO: We might be able to generalize this to object remembering pre-write barrier.
SATBBarrier,
}
impl BarrierSelector {
/// A const function to check if two barrier selectors are the same.
pub const fn equals(&self, other: BarrierSelector) -> bool {
// cast enum to u8 then compare. Otherwise, we cannot do it in a const fn.
*self as u8 == other as u8
}
}
/// A barrier is a combination of fast-path behaviour + slow-path semantics.
/// This trait exposes generic barrier interfaces. The implementations will define their
/// own fast-path code and slow-path semantics.
///
/// Normally, a binding will call these generic barrier interfaces (`object_reference_write` and `memory_region_copy`) for subsuming barrier calls.
///
/// If a subsuming barrier cannot be easily deployed due to platform limitations, the binding may chosse to call both `object_reference_write_pre` and `object_reference_write_post`
/// barrier before and after the store operation.
///
/// As a performance optimization, the binding may also choose to port the fast-path to the VM side,
/// and call the slow-path (`object_reference_write_slow`) only if necessary.
pub trait Barrier<VM: VMBinding>: 'static + Send + Downcast {
/// Flush thread-local states like buffers or remembered sets.
fn flush(&mut self) {}
/// Weak reference loading barrier. A mutator should call this when loading from a weak
/// reference field, for example, when executing `java.lang.ref.Reference.get()` in JVM, or
/// loading from a global weak table in CRuby.
///
/// Note: Merely loading from a field holding weak reference into a local variable will create a
/// strong reference from the stack to the referent, changing its reachablilty from weakly
/// reachable to strongly reachable. Concurrent garbage collectors may need to handle such
/// events specially. See [SATBBarrier::load_weak_reference] for a concrete example.
///
/// Arguments:
/// * `referent`: The referent object which the weak reference is pointing to.
fn load_weak_reference(&mut self, _referent: ObjectReference) {}
/// Subsuming barrier for object reference write
fn object_reference_write(
&mut self,
src: ObjectReference,
slot: VM::VMSlot,
target: ObjectReference,
) {
self.object_reference_write_pre(src, slot, Some(target));
slot.store(target);
self.object_reference_write_post(src, slot, Some(target));
}
/// Full pre-barrier for object reference write
fn object_reference_write_pre(
&mut self,
_src: ObjectReference,
_slot: VM::VMSlot,
_target: Option<ObjectReference>,
) {
}
/// Full post-barrier for object reference write
fn object_reference_write_post(
&mut self,
_src: ObjectReference,
_slot: VM::VMSlot,
_target: Option<ObjectReference>,
) {
}
/// Object reference write slow-path call.
/// This can be called either before or after the store, depend on the concrete barrier implementation.
fn object_reference_write_slow(
&mut self,
_src: ObjectReference,
_slot: VM::VMSlot,
_target: Option<ObjectReference>,
) {
}
/// Subsuming barrier for array copy
fn memory_region_copy(&mut self, src: VM::VMMemorySlice, dst: VM::VMMemorySlice) {
self.memory_region_copy_pre(src.clone(), dst.clone());
VM::VMMemorySlice::copy(&src, &dst);
self.memory_region_copy_post(src, dst);
}
/// Full pre-barrier for array copy
fn memory_region_copy_pre(&mut self, _src: VM::VMMemorySlice, _dst: VM::VMMemorySlice) {}
/// Full post-barrier for array copy
fn memory_region_copy_post(&mut self, _src: VM::VMMemorySlice, _dst: VM::VMMemorySlice) {}
/// A pre-barrier indicating that some fields of the object will probably be modified soon.
/// Specifically, the caller should ensure that:
/// * The barrier must called before any field modification.
/// * Some fields (unknown at the time of calling this barrier) might be modified soon, without a write barrier.
/// * There are no safepoints between the barrier call and the field writes.
///
/// **Example use case for mmtk-openjdk:**
///
/// The OpenJDK C2 slowpath allocation code
/// can do deoptimization after the allocation and before returning to C2 compiled code.
/// The deoptimization itself contains a safepoint. For generational plans, if a GC
/// happens at this safepoint, the allocated object will be promoted, and all the
/// subsequent field initialization should be recorded.
///
// TODO: Review any potential use cases for other VM bindings.
fn object_probable_write(&mut self, _obj: ObjectReference) {}
}
impl_downcast!(Barrier<VM> where VM: VMBinding);
/// Empty barrier implementation.
/// For GCs that do not need any barriers
///
/// Note that since NoBarrier noes nothing but the object field write itself, it has no slow-path semantics (i.e. an no-op slow-path).
pub struct NoBarrier;
impl<VM: VMBinding> Barrier<VM> for NoBarrier {}
/// A barrier semantics defines the barrier slow-path behaviour. For example, how an object barrier processes it's modbufs.
/// Specifically, it defines the slow-path call interfaces and a call to flush buffers.
///
/// A barrier is a combination of fast-path behaviour + slow-path semantics.
/// The fast-path code will decide whether to call the slow-path calls.
pub trait BarrierSemantics: 'static + Send {
type VM: VMBinding;
const UNLOG_BIT_SPEC: MetadataSpec =
*<Self::VM as VMBinding>::VMObjectModel::GLOBAL_LOG_BIT_SPEC.as_spec();
/// Flush thread-local buffers or remembered sets.
/// Normally this is called by the slow-path implementation whenever the thread-local buffers are full.
/// This will also be called externally by the VM, when the thread is being destroyed.
fn flush(&mut self);
/// Slow-path call for object field write operations.
fn object_reference_write_slow(
&mut self,
src: ObjectReference,
slot: <Self::VM as VMBinding>::VMSlot,
target: Option<ObjectReference>,
);
/// Slow-path call for mempry slice copy operations. For example, array-copy operations.
fn memory_region_copy_slow(
&mut self,
src: <Self::VM as VMBinding>::VMMemorySlice,
dst: <Self::VM as VMBinding>::VMMemorySlice,
);
/// Object will probably be modified
fn object_probable_write_slow(&mut self, _obj: ObjectReference) {}
/// Loading from a weak reference field
fn load_weak_reference(&mut self, _o: ObjectReference) {}
}
/// Generic object barrier with a type argument defining it's slow-path behaviour.
pub struct ObjectBarrier<S: BarrierSemantics> {
semantics: S,
}
impl<S: BarrierSemantics> ObjectBarrier<S> {
/// Create a new ObjectBarrier with the given semantics.
pub fn new(semantics: S) -> Self {
Self { semantics }
}
/// Attempt to atomically log an object.
/// Returns true if the object is not logged previously.
fn object_is_unlogged(&self, object: ObjectReference) -> bool {
unsafe { S::UNLOG_BIT_SPEC.load::<S::VM, u8>(object, None) != 0 }
}
/// Attempt to atomically log an object.
/// Returns true if the object is not logged previously.
fn log_object(&self, object: ObjectReference) -> bool {
#[cfg(all(feature = "vo_bit", feature = "extreme_assertions"))]
debug_assert!(
crate::util::metadata::vo_bit::is_vo_bit_set(object),
"object bit is unset"
);
loop {
let old_value =
S::UNLOG_BIT_SPEC.load_atomic::<S::VM, u8>(object, None, Ordering::SeqCst);
if old_value == 0 {
return false;
}
if S::UNLOG_BIT_SPEC
.compare_exchange_metadata::<S::VM, u8>(
object,
1,
0,
None,
Ordering::SeqCst,
Ordering::SeqCst,
)
.is_ok()
{
return true;
}
}
}
}
impl<S: BarrierSemantics> Barrier<S::VM> for ObjectBarrier<S> {
fn flush(&mut self) {
self.semantics.flush();
}
fn object_reference_write_post(
&mut self,
src: ObjectReference,
slot: <S::VM as VMBinding>::VMSlot,
target: Option<ObjectReference>,
) {
if self.object_is_unlogged(src) {
self.object_reference_write_slow(src, slot, target);
}
}
fn object_reference_write_slow(
&mut self,
src: ObjectReference,
slot: <S::VM as VMBinding>::VMSlot,
target: Option<ObjectReference>,
) {
if self.log_object(src) {
self.semantics
.object_reference_write_slow(src, slot, target);
}
}
fn memory_region_copy_post(
&mut self,
src: <S::VM as VMBinding>::VMMemorySlice,
dst: <S::VM as VMBinding>::VMMemorySlice,
) {
self.semantics.memory_region_copy_slow(src, dst);
}
fn object_probable_write(&mut self, obj: ObjectReference) {
if self.object_is_unlogged(obj) {
self.semantics.object_probable_write_slow(obj);
}
}
}
/// A SATB (Snapshot-At-The-Beginning) barrier implementation.
/// This barrier is basically a pre-write object barrier with a weak reference loading barrier.
pub struct SATBBarrier<S: BarrierSemantics> {
weak_ref_barrier_enabled: bool,
semantics: S,
}
impl<S: BarrierSemantics> SATBBarrier<S> {
/// Create a new SATBBarrier with the given semantics.
pub fn new(semantics: S) -> Self {
Self {
weak_ref_barrier_enabled: false,
semantics,
}
}
pub(crate) fn set_weak_ref_barrier_enabled(&mut self, value: bool) {
self.weak_ref_barrier_enabled = value;
}
fn object_is_unlogged(&self, object: ObjectReference) -> bool {
S::UNLOG_BIT_SPEC.load_atomic::<S::VM, u8>(object, None, Ordering::SeqCst) != 0
}
}
impl<S: BarrierSemantics> Barrier<S::VM> for SATBBarrier<S> {
fn flush(&mut self) {
self.semantics.flush();
}
fn load_weak_reference(&mut self, o: ObjectReference) {
if self.weak_ref_barrier_enabled {
self.semantics.load_weak_reference(o)
}
}
fn object_probable_write(&mut self, obj: ObjectReference) {
self.semantics.object_probable_write_slow(obj);
}
fn object_reference_write_pre(
&mut self,
src: ObjectReference,
slot: <S::VM as VMBinding>::VMSlot,
target: Option<ObjectReference>,
) {
if self.object_is_unlogged(src) {
self.semantics
.object_reference_write_slow(src, slot, target);
}
}
fn object_reference_write_post(
&mut self,
_src: ObjectReference,
_slot: <S::VM as VMBinding>::VMSlot,
_target: Option<ObjectReference>,
) {
unimplemented!()
}
fn object_reference_write_slow(
&mut self,
src: ObjectReference,
slot: <S::VM as VMBinding>::VMSlot,
target: Option<ObjectReference>,
) {
self.semantics
.object_reference_write_slow(src, slot, target);
}
fn memory_region_copy_pre(
&mut self,
src: <S::VM as VMBinding>::VMMemorySlice,
dst: <S::VM as VMBinding>::VMMemorySlice,
) {
self.semantics.memory_region_copy_slow(src, dst);
}
fn memory_region_copy_post(
&mut self,
_src: <S::VM as VMBinding>::VMMemorySlice,
_dst: <S::VM as VMBinding>::VMMemorySlice,
) {
unimplemented!()
}
}