mmtk/plan/concurrent/immix/
global.rs

1use crate::plan::concurrent::concurrent_marking_work::ProcessRootSlots;
2use crate::plan::concurrent::global::ConcurrentPlan;
3use crate::plan::concurrent::immix::gc_work::ConcurrentImmixGCWorkContext;
4use crate::plan::concurrent::immix::gc_work::ConcurrentImmixSTWGCWorkContext;
5use crate::plan::concurrent::Pause;
6use crate::plan::global::BasePlan;
7use crate::plan::global::CommonPlan;
8use crate::plan::global::CreateGeneralPlanArgs;
9use crate::plan::global::CreateSpecificPlanArgs;
10use crate::plan::immix::mutator::ALLOCATOR_MAPPING;
11use crate::plan::AllocationSemantics;
12use crate::plan::Plan;
13use crate::plan::PlanConstraints;
14use crate::policy::immix::defrag::StatsForDefrag;
15use crate::policy::immix::ImmixSpaceArgs;
16use crate::policy::immix::TRACE_KIND_DEFRAG;
17use crate::policy::immix::TRACE_KIND_FAST;
18use crate::policy::space::Space;
19use crate::scheduler::gc_work::Release;
20use crate::scheduler::gc_work::StopMutators;
21use crate::scheduler::gc_work::UnsupportedProcessEdges;
22use crate::scheduler::gc_work::VMProcessWeakRefs;
23use crate::scheduler::*;
24use crate::util::alloc::allocators::AllocatorSelector;
25use crate::util::copy::*;
26use crate::util::heap::gc_trigger::SpaceStats;
27use crate::util::heap::VMRequest;
28use crate::util::metadata::log_bit::UnlogBitsOperation;
29use crate::util::metadata::side_metadata::SideMetadataContext;
30use crate::vm::ObjectModel;
31use crate::vm::VMBinding;
32use crate::{policy::immix::ImmixSpace, util::opaque_pointer::VMWorkerThread};
33use std::sync::atomic::AtomicBool;
34
35use atomic::Atomic;
36use atomic::Ordering;
37use enum_map::EnumMap;
38
39use mmtk_macros::{HasSpaces, PlanTraceObject};
40
41/// A concurrent Immix plan. The plan supports concurrent collection (strictly non-moving) and STW full heap collection (which may do defrag).
42/// The concurrent GC consists of two STW pauses (initial mark and final mark) with concurrent marking in between.
43#[derive(HasSpaces, PlanTraceObject)]
44pub struct ConcurrentImmix<VM: VMBinding> {
45    #[post_scan]
46    #[space]
47    #[copy_semantics(CopySemantics::DefaultCopy)]
48    pub immix_space: ImmixSpace<VM>,
49    #[parent]
50    pub common: CommonPlan<VM>,
51    last_gc_was_defrag: AtomicBool,
52    current_pause: Atomic<Option<Pause>>,
53    previous_pause: Atomic<Option<Pause>>,
54    should_do_full_gc: AtomicBool,
55    concurrent_marking_active: AtomicBool,
56}
57
58/// The plan constraints for the concurrent immix plan.
59pub const CONCURRENT_IMMIX_CONSTRAINTS: PlanConstraints = PlanConstraints {
60    // If we disable moving in Immix, this is a non-moving plan.
61    moves_objects: !cfg!(feature = "immix_non_moving"),
62    // Max immix object size is half of a block.
63    max_non_los_default_alloc_bytes: crate::policy::immix::MAX_IMMIX_OBJECT_SIZE,
64    needs_prepare_mutator: true,
65    barrier: crate::BarrierSelector::SATBBarrier,
66    needs_log_bit: true,
67    ..PlanConstraints::default()
68};
69
70impl<VM: VMBinding> Plan for ConcurrentImmix<VM> {
71    fn collection_required(&self, space_full: bool, _space: Option<SpaceStats<Self::VM>>) -> bool {
72        if self.base().collection_required(self, space_full) {
73            self.should_do_full_gc.store(true, Ordering::Release);
74            info!("Triggering full GC");
75            return true;
76        }
77
78        // Check stw for final mark
79        let concurrent_marking_in_progress = self.concurrent_marking_in_progress();
80        if concurrent_marking_in_progress
81            && self.common.base.scheduler.work_buckets[WorkBucketStage::Concurrent].is_drained()
82        {
83            // After the Concurrent bucket is drained during concurrent marking,
84            // we trigger the FinalMark pause at the next poll() site (here).
85            // FIXME: Immediately trigger FinalMark when the Concurrent bucket is drained.
86            return true;
87        }
88
89        // Check stw for initial mark
90
91        // If concurrent marking is disbled, no need to check further.
92        if self.concurrent_marking_is_disabled() {
93            return false;
94        }
95
96        let threshold = self.get_total_pages() >> 1;
97        let used_pages_after_last_gc = self.common.base.global_state.get_used_pages_after_last_gc();
98        let used_pages_now = self.get_used_pages();
99        let allocated = used_pages_now.saturating_sub(used_pages_after_last_gc);
100        if !concurrent_marking_in_progress && allocated > threshold {
101            info!("Allocated {allocated} pages since last GC ({used_pages_now} - {used_pages_after_last_gc} > {threshold}): Do concurrent marking");
102            debug_assert!(
103                self.common.base.scheduler.work_buckets[WorkBucketStage::Concurrent].is_empty()
104            );
105            debug_assert!(!self.concurrent_marking_in_progress());
106            debug_assert_ne!(self.previous_pause(), Some(Pause::InitialMark));
107            return true;
108        }
109
110        false
111    }
112
113    fn last_collection_was_exhaustive(&self) -> bool {
114        self.immix_space
115            .is_last_gc_exhaustive(self.last_gc_was_defrag.load(Ordering::Relaxed))
116    }
117
118    fn constraints(&self) -> &'static PlanConstraints {
119        &CONCURRENT_IMMIX_CONSTRAINTS
120    }
121
122    fn create_copy_config(&'static self) -> CopyConfig<Self::VM> {
123        use enum_map::enum_map;
124        CopyConfig {
125            copy_mapping: enum_map! {
126                CopySemantics::DefaultCopy => CopySelector::Immix(0),
127                _ => CopySelector::Unused,
128            },
129            space_mapping: vec![(CopySelector::Immix(0), &self.immix_space)],
130            constraints: &CONCURRENT_IMMIX_CONSTRAINTS,
131        }
132    }
133
134    fn schedule_collection(&'static self, scheduler: &GCWorkScheduler<VM>) {
135        // If concurrent marking is disabled, force a full GC.
136        // Though we have checked in collection_required to not trigger a concurrent GC, it is still possible
137        // that a GC is triggered without going through collection_required, e.g. a user triggered GC, or a GC trigger
138        // implemented at the binding side without calling collection_required.
139        // In those cases, we also want to force a full GC.
140        if self.concurrent_marking_is_disabled() {
141            self.should_do_full_gc.store(true, Ordering::SeqCst);
142        }
143
144        let pause = if self.concurrent_marking_in_progress() {
145            // FIXME: Currently it is unsafe to bypass `FinalMark` and go directly from `InitialMark` to `Full`.
146            // It is related to defragmentation.  See https://github.com/mmtk/mmtk-core/issues/1357 for more details.
147            // We currently force `FinalMark` to happen if the last pause is `InitialMark`.
148            Pause::FinalMark
149        } else if self.should_do_full_gc.load(Ordering::SeqCst)
150            // For user-triggered GCs, we don't want a simple initial pause which reclaims nothing.
151            // We do a full STW collection for user triggered collection instead.
152            || self.base().global_state.is_user_triggered_collection()
153        {
154            Pause::Full
155        } else {
156            Pause::InitialMark
157        };
158
159        self.current_pause.store(Some(pause), Ordering::SeqCst);
160
161        probe!(mmtk, concurrent_pause_determined, pause as usize);
162
163        match pause {
164            Pause::Full => {
165                // Ref closure buckets is disabled by initial mark, and needs to be re-enabled for full GC before
166                // we reuse the normal Immix scheduling.
167                self.set_ref_closure_buckets_enabled(true);
168                crate::plan::immix::global::Immix::schedule_immix_full_heap_collection::<
169                    ConcurrentImmix<VM>,
170                    ConcurrentImmixSTWGCWorkContext<VM, TRACE_KIND_FAST>,
171                    ConcurrentImmixSTWGCWorkContext<VM, TRACE_KIND_DEFRAG>,
172                >(self, &self.immix_space, scheduler);
173            }
174            Pause::InitialMark => self.schedule_concurrent_marking_initial_pause(scheduler),
175            Pause::FinalMark => self.schedule_concurrent_marking_final_pause(scheduler),
176        }
177    }
178
179    fn get_allocator_mapping(&self) -> &'static EnumMap<AllocationSemantics, AllocatorSelector> {
180        &ALLOCATOR_MAPPING
181    }
182
183    fn prepare(&mut self, tls: VMWorkerThread) {
184        let pause = self.current_pause().unwrap();
185        match pause {
186            Pause::Full => {
187                self.common.prepare(tls, true);
188                self.immix_space.prepare(
189                    true,
190                    Some(StatsForDefrag::new(self)),
191                    // Ignore unlog bits in full GCs because unlog bits should be all 0.
192                    UnlogBitsOperation::NoOp,
193                );
194            }
195            Pause::InitialMark => {
196                self.immix_space.prepare(
197                    true,
198                    Some(StatsForDefrag::new(self)),
199                    // Bulk set log bits so SATB barrier will be triggered on the existing objects.
200                    UnlogBitsOperation::BulkSet,
201                );
202
203                self.common.prepare(tls, true);
204                // Bulk set log bits so SATB barrier will be triggered on the existing objects.
205                self.common
206                    .schedule_unlog_bits_op(UnlogBitsOperation::BulkSet);
207            }
208            Pause::FinalMark => (),
209        }
210    }
211
212    fn release(&mut self, tls: VMWorkerThread) {
213        let pause = self.current_pause().unwrap();
214        match pause {
215            Pause::InitialMark => (),
216            Pause::Full | Pause::FinalMark => {
217                self.immix_space.release(
218                    true,
219                    // Bulk clear log bits so SATB barrier will not be triggered.
220                    UnlogBitsOperation::BulkClear,
221                );
222
223                self.common.release(tls, true);
224
225                if pause == Pause::FinalMark {
226                    // Bulk clear log bits so SATB barrier will not be triggered.
227                    self.common
228                        .schedule_unlog_bits_op(UnlogBitsOperation::BulkClear);
229                } else {
230                    // Full pauses didn't set unlog bits in the first place,
231                    // so there is no need to clear them.
232                    // TODO: Currently InitialMark must be followed by a FinalMark.
233                    // If we allow upgrading a concurrent GC to a full STW GC,
234                    // we will need to clear the unlog bits at an appropriate place.
235                }
236            }
237        }
238    }
239
240    fn end_of_gc(&mut self, _tls: VMWorkerThread) {
241        self.last_gc_was_defrag
242            .store(self.immix_space.end_of_gc(), Ordering::Relaxed);
243
244        let pause = self.current_pause().unwrap();
245        if pause == Pause::InitialMark {
246            self.set_concurrent_marking_state(true);
247        }
248        self.previous_pause.store(Some(pause), Ordering::SeqCst);
249        self.current_pause.store(None, Ordering::SeqCst);
250        if pause != Pause::FinalMark {
251            self.should_do_full_gc.store(false, Ordering::SeqCst);
252        } else {
253            // FIXME: Currently it is unsafe to trigger full GC during concurrent marking.
254            // See `Self::schedule_collection`.
255            // We keep the value of `self.should_do_full_gc` so that if full GC is triggered,
256            // the next GC will be full GC.
257        }
258        info!("{:?} end", pause);
259    }
260
261    fn current_gc_may_move_object(&self) -> bool {
262        self.immix_space.in_defrag()
263    }
264
265    fn get_collection_reserved_pages(&self) -> usize {
266        self.immix_space.defrag_headroom_pages()
267    }
268
269    fn get_used_pages(&self) -> usize {
270        self.immix_space.reserved_pages() + self.common.get_used_pages()
271    }
272
273    fn base(&self) -> &BasePlan<VM> {
274        &self.common.base
275    }
276
277    fn base_mut(&mut self) -> &mut BasePlan<Self::VM> {
278        &mut self.common.base
279    }
280
281    fn common(&self) -> &CommonPlan<VM> {
282        &self.common
283    }
284
285    fn notify_mutators_paused(&self, _scheduler: &GCWorkScheduler<VM>) {
286        use crate::vm::ActivePlan;
287        let pause = self.current_pause().unwrap();
288        match pause {
289            Pause::Full => {
290                self.set_concurrent_marking_state(false);
291            }
292            Pause::InitialMark => {
293                debug_assert!(
294                    !self.concurrent_marking_in_progress(),
295                    "prev pause: {:?}",
296                    self.previous_pause().unwrap()
297                );
298            }
299            Pause::FinalMark => {
300                debug_assert!(self.concurrent_marking_in_progress());
301                // Flush barrier buffers
302                for mutator in <VM as VMBinding>::VMActivePlan::mutators() {
303                    mutator.barrier.flush();
304                }
305                self.set_concurrent_marking_state(false);
306            }
307        }
308        info!("{:?} start", pause);
309    }
310
311    fn concurrent(&self) -> Option<&dyn ConcurrentPlan<VM = VM>> {
312        Some(self)
313    }
314}
315
316impl<VM: VMBinding> ConcurrentImmix<VM> {
317    pub fn new(args: CreateGeneralPlanArgs<VM>) -> Self {
318        if *args.options.concurrent_immix_disable_concurrent_marking {
319            warn!("Option 'concurrent_immix_disable_concurrent_marking' is set to true. Concurrent marking is disabled for ConcurrentImmix. This will make ConcurrentImmix behave exactly like full heap Immix.");
320        }
321
322        let spec = crate::util::metadata::extract_side_metadata(&[
323            *VM::VMObjectModel::GLOBAL_LOG_BIT_SPEC,
324        ]);
325
326        let mut plan_args = CreateSpecificPlanArgs {
327            global_args: args,
328            constraints: &CONCURRENT_IMMIX_CONSTRAINTS,
329            global_side_metadata_specs: SideMetadataContext::new_global_specs(&spec),
330        };
331
332        let immix_args = ImmixSpaceArgs {
333            mixed_age: false,
334            never_move_objects: false,
335        };
336
337        // These buckets are not used in an Immix plan. We can simply disable them.
338        // TODO: We should be more systmatic on this, and disable unnecessary buckets for other plans as well.
339        let scheduler = &plan_args.global_args.scheduler;
340        scheduler.work_buckets[WorkBucketStage::VMRefForwarding].set_enabled(false);
341        scheduler.work_buckets[WorkBucketStage::CalculateForwarding].set_enabled(false);
342        scheduler.work_buckets[WorkBucketStage::SecondRoots].set_enabled(false);
343        scheduler.work_buckets[WorkBucketStage::RefForwarding].set_enabled(false);
344        scheduler.work_buckets[WorkBucketStage::FinalizableForwarding].set_enabled(false);
345        scheduler.work_buckets[WorkBucketStage::Compact].set_enabled(false);
346
347        ConcurrentImmix {
348            immix_space: ImmixSpace::new(
349                plan_args.get_normal_space_args("immix", true, false, VMRequest::discontiguous()),
350                immix_args,
351            ),
352            common: CommonPlan::new(plan_args),
353            last_gc_was_defrag: AtomicBool::new(false),
354            current_pause: Atomic::new(None),
355            previous_pause: Atomic::new(None),
356            should_do_full_gc: AtomicBool::new(false),
357            concurrent_marking_active: AtomicBool::new(false),
358        }
359    }
360
361    fn set_ref_closure_buckets_enabled(&self, do_closure: bool) {
362        let scheduler = &self.common.base.scheduler;
363        scheduler.work_buckets[WorkBucketStage::VMRefClosure].set_enabled(do_closure);
364        scheduler.work_buckets[WorkBucketStage::WeakRefClosure].set_enabled(do_closure);
365        scheduler.work_buckets[WorkBucketStage::FinalRefClosure].set_enabled(do_closure);
366        scheduler.work_buckets[WorkBucketStage::SoftRefClosure].set_enabled(do_closure);
367        scheduler.work_buckets[WorkBucketStage::PhantomRefClosure].set_enabled(do_closure);
368    }
369
370    pub(crate) fn schedule_concurrent_marking_initial_pause(
371        &'static self,
372        scheduler: &GCWorkScheduler<VM>,
373    ) {
374        use crate::scheduler::gc_work::Prepare;
375
376        self.set_ref_closure_buckets_enabled(false);
377
378        scheduler.work_buckets[WorkBucketStage::Unconstrained].add(StopMutators::<
379            ConcurrentImmixGCWorkContext<ProcessRootSlots<VM, Self, TRACE_KIND_FAST>>,
380        >::new());
381        scheduler.work_buckets[WorkBucketStage::Prepare].add(Prepare::<
382            ConcurrentImmixGCWorkContext<UnsupportedProcessEdges<VM>>,
383        >::new(self));
384    }
385
386    fn schedule_concurrent_marking_final_pause(&'static self, scheduler: &GCWorkScheduler<VM>) {
387        self.set_ref_closure_buckets_enabled(true);
388
389        // Skip root scanning in the final mark
390        scheduler.work_buckets[WorkBucketStage::Unconstrained].add(StopMutators::<
391            ConcurrentImmixGCWorkContext<ProcessRootSlots<VM, Self, TRACE_KIND_FAST>>,
392        >::new_no_scan_roots());
393
394        scheduler.work_buckets[WorkBucketStage::Release].add(Release::<
395            ConcurrentImmixGCWorkContext<UnsupportedProcessEdges<VM>>,
396        >::new(self));
397
398        // Deal with weak ref and finalizers
399        // TODO: Check against schedule_common_work and see if we are still missing any work packet
400        type RefProcessingEdges<VM> =
401            crate::scheduler::gc_work::PlanProcessEdges<VM, ConcurrentImmix<VM>, TRACE_KIND_FAST>;
402        // Reference processing
403        if !*self.base().options.no_reference_types {
404            use crate::util::reference_processor::{
405                PhantomRefProcessing, SoftRefProcessing, WeakRefProcessing,
406            };
407            scheduler.work_buckets[WorkBucketStage::SoftRefClosure]
408                .add(SoftRefProcessing::<RefProcessingEdges<VM>>::new());
409            scheduler.work_buckets[WorkBucketStage::WeakRefClosure]
410                .add(WeakRefProcessing::<VM>::new());
411            scheduler.work_buckets[WorkBucketStage::PhantomRefClosure]
412                .add(PhantomRefProcessing::<VM>::new());
413
414            use crate::util::reference_processor::RefEnqueue;
415            scheduler.work_buckets[WorkBucketStage::Release].add(RefEnqueue::<VM>::new());
416        }
417
418        // Finalization
419        if !*self.base().options.no_finalizer {
420            use crate::util::finalizable_processor::Finalization;
421            // finalization
422            scheduler.work_buckets[WorkBucketStage::FinalRefClosure]
423                .add(Finalization::<RefProcessingEdges<VM>>::new());
424        }
425
426        // VM-specific weak ref processing
427        // Note that ConcurrentImmix does not have a separate forwarding stage,
428        // so we don't schedule the `VMForwardWeakRefs` work packet.
429        scheduler.work_buckets[WorkBucketStage::VMRefClosure]
430            .set_sentinel(Box::new(VMProcessWeakRefs::<RefProcessingEdges<VM>>::new()));
431    }
432
433    pub fn concurrent_marking_in_progress(&self) -> bool {
434        self.concurrent_marking_active.load(Ordering::Acquire)
435    }
436
437    fn set_concurrent_marking_state(&self, active: bool) {
438        use crate::plan::global::HasSpaces;
439
440        // Tell the spaces to allocate new objects as live
441        let allocate_object_as_live = active;
442        self.for_each_space(&mut |space: &dyn Space<VM>| {
443            space.set_allocate_as_live(allocate_object_as_live);
444        });
445
446        // Store the state.
447        self.concurrent_marking_active
448            .store(active, Ordering::SeqCst);
449
450        // We also set SATB barrier as active -- this is done in Mutator prepare/release.
451    }
452
453    pub(super) fn is_concurrent_marking_active(&self) -> bool {
454        self.concurrent_marking_active.load(Ordering::SeqCst)
455    }
456
457    fn previous_pause(&self) -> Option<Pause> {
458        self.previous_pause.load(Ordering::SeqCst)
459    }
460
461    fn concurrent_marking_is_disabled(&self) -> bool {
462        *self
463            .base()
464            .options
465            .concurrent_immix_disable_concurrent_marking
466    }
467}
468
469impl<VM: VMBinding> ConcurrentPlan for ConcurrentImmix<VM> {
470    fn current_pause(&self) -> Option<Pause> {
471        self.current_pause.load(Ordering::SeqCst)
472    }
473
474    fn concurrent_work_in_progress(&self) -> bool {
475        self.concurrent_marking_in_progress()
476    }
477}