mmtk/plan/lxr/
global.rs

1use super::block_allocation::BlockAllocation;
2use super::gc_work::nursery_sweeping::ReleaseLOSNursery;
3use super::gc_work::prepare::FastRCPrepare;
4use super::gc_work::rc::ProcessDecs;
5use super::gc_work::LXRGCWorkContext;
6use super::mature_evac::MatureEvacuationSet;
7use super::mutator::ALLOCATOR_MAPPING;
8use super::{LazySweepingJobsCounter, LAZY_SWEEPING_JOBS};
9use crate::plan::concurrent::global::ConcurrentPlan;
10use crate::plan::concurrent::Pause;
11use crate::plan::global::CommonPlan;
12use crate::plan::global::{BasePlan, CreateGeneralPlanArgs, CreateSpecificPlanArgs};
13use crate::plan::lxr::gc_work::mature_sweeping::{RCSweepMatureAfterSATBLOS, SweepDeadCycles};
14use crate::plan::lxr::gc_work::nursery_sweeping::SweepBlocksAfterDecs;
15use crate::plan::lxr::gc_work::prepare::{ConcurrentChunkMetadataZeroing, PrepareChunksForFullGC};
16use crate::plan::lxr::mature_evac::MatureEvecRemSet;
17use crate::plan::AllocationSemantics;
18use crate::plan::MutatorContext;
19use crate::plan::Plan;
20use crate::plan::PlanConstraints;
21use crate::policy::immix::block::Block;
22use crate::policy::immix::ImmixSpaceArgs;
23use crate::policy::largeobjectspace::LargeObjectSpace;
24use crate::policy::space::Space;
25use crate::scheduler::gc_work::*;
26use crate::util::alloc::allocators::AllocatorSelector;
27#[cfg(feature = "analysis")]
28use crate::util::analysis::GcHookWork;
29use crate::util::constants::*;
30use crate::util::copy::*;
31use crate::util::heap::{SpaceStats, VMRequest};
32use crate::util::metadata::side_metadata::SideMetadataContext;
33use crate::util::metadata::MetadataSpec;
34use crate::util::rc::{RefCountHelper, RC_TABLE};
35#[cfg(feature = "sanity")]
36use crate::util::sanity::sanity_checker::*;
37use crate::util::{metadata, Address, ObjectReference};
38use crate::vm::ActivePlan;
39use crate::vm::{Collection, ObjectModel, VMBinding};
40use crate::BarrierSelector;
41use crate::{policy::immix::ImmixSpace, util::opaque_pointer::VMWorkerThread};
42use crate::{scheduler::*, MMTK};
43use atomic::{Atomic, Ordering};
44use crossbeam::queue::SegQueue;
45use enum_map::EnumMap;
46use spin::Lazy;
47use std::sync::atomic::{AtomicBool, AtomicUsize};
48use std::sync::{Condvar, Mutex, RwLock};
49
50const LOG_CONSERVATIVE_SURVIVAL_RATIO_MULTIPLER: usize = 1;
51
52static HEAP_AFTER_GC: AtomicUsize = AtomicUsize::new(0);
53
54use mmtk_macros::{HasSpaces, PlanTraceObject};
55
56#[derive(HasSpaces, PlanTraceObject)]
57pub struct LXR<VM: VMBinding> {
58    #[post_scan]
59    #[space]
60    #[copy_semantics(CopySemantics::DefaultCopy)]
61    pub immix_space: ImmixSpace<VM>,
62    #[parent]
63    pub common: CommonPlan<VM>,
64    /// Always true for non-rc immix.
65    /// For RC immix, this is used for enable backup tracing.
66    perform_cycle_collection: AtomicBool,
67    current_pause: Atomic<Option<Pause>>,
68    previous_pause: Atomic<Option<Pause>>,
69    hint_cycle_gc: AtomicBool,
70    hint_emergency_gc: AtomicBool,
71    avail_pages_at_end_of_last_gc: AtomicUsize,
72    zeroing_packets_scheduled: AtomicBool,
73    decide_cycle_collection: (Mutex<bool>, Condvar),
74    in_concurrent_marking: AtomicBool,
75    pub prev_roots: RwLock<SegQueue<Vec<ObjectReference>>>,
76    pub curr_roots: RwLock<SegQueue<Vec<ObjectReference>>>,
77    pub rc: RefCountHelper<VM>,
78    block_allocation: BlockAllocation<VM>,
79    pub(super) evac_set: MatureEvacuationSet,
80    pub(super) mature_evac_remset: MatureEvecRemSet<VM>,
81    pub(super) num_clean_blocks_released_lazy: AtomicUsize,
82    pub(super) possibly_dead_mature_blocks: SegQueue<(Block, bool)>,
83}
84
85pub static LXR_CONSTRAINTS: Lazy<PlanConstraints> = Lazy::new(|| PlanConstraints {
86    moves_objects: super::NURSERY_EVACUATION || super::MATURE_EVACUATION,
87    // Max immix object size is half of a block.
88    max_non_los_default_alloc_bytes: crate::policy::immix::MAX_IMMIX_OBJECT_SIZE,
89    barrier: BarrierSelector::FieldBarrier,
90    needs_log_bit: true,
91    needs_field_log_bit: true,
92    rc_enabled: true,
93    needs_prepare_mutator: false,
94    ..PlanConstraints::default()
95});
96
97impl<VM: VMBinding> Plan for LXR<VM> {
98    fn current_gc_may_move_object(&self) -> bool {
99        true
100    }
101
102    fn collection_required(&self, space_full: bool, _space: Option<SpaceStats<Self::VM>>) -> bool {
103        // Spaces or heap full
104        if self.base().collection_required(self, space_full) {
105            return true;
106        }
107        // SATB is finished
108        if self.concurrent_work_in_progress() && super::concurrent_marking_packets_drained() {
109            return true;
110        }
111        // Bound the pause by bounding the work it has to do (default to usize::MAX - disabled)
112        if self.rc.inc_buffer_size() >= *self.base().options.lxr_inc_buffer_limit {
113            return true;
114        }
115        // Survival limits
116        let total_young_alloc_pages =
117            self.block_allocation.total_young_allocation_in_bytes() >> LOG_BYTES_IN_MBYTE;
118        // Use copy promotion ratio, or just total promotion ratio.
119        let ratio = if LXR_CONSTRAINTS.moves_objects {
120            super::SURVIVAL_RATIO_PREDICTOR.copy_promote_ratio()
121        } else {
122            super::SURVIVAL_RATIO_PREDICTOR.promote_ratio()
123        };
124        let predicted_survival_mb: usize = ((total_young_alloc_pages as f64 * ratio) as usize)
125            << LOG_CONSERVATIVE_SURVIVAL_RATIO_MULTIPLER;
126        if predicted_survival_mb >= *self.base().options.lxr_max_survival_mb {
127            return true;
128        }
129        if !self.immix_space.common().contiguous {
130            let available_to_space = self.get_total_pages() - self.get_used_pages();
131            if predicted_survival_mb >= available_to_space {
132                return true;
133            }
134        }
135        false
136    }
137
138    fn last_collection_was_exhaustive(&self) -> bool {
139        self.previous_pause.load(Ordering::SeqCst) == Some(Pause::Full)
140    }
141
142    fn constraints(&self) -> &'static PlanConstraints {
143        &LXR_CONSTRAINTS
144    }
145
146    fn create_copy_config(&'static self) -> CopyConfig<VM> {
147        use enum_map::enum_map;
148        CopyConfig {
149            copy_mapping: enum_map! {
150                CopySemantics::DefaultCopy => CopySelector::Immix(0),
151                _ => CopySelector::Unused,
152            },
153            space_mapping: vec![(CopySelector::Immix(0), &self.immix_space)],
154            constraints: &LXR_CONSTRAINTS,
155        }
156    }
157
158    fn schedule_collection(&'static self, scheduler: &GCWorkScheduler<VM>) {
159        if !super::LazySweepingJobs::all_finished() {
160            warn!("LXR Lazy Sweeping Not Finished");
161        }
162        let pause = self.select_collection_kind();
163        // Wait for concurrent packets
164        // Mark table zeroing
165        if pause == Pause::InitialMark || pause == Pause::Full {
166            self.schedule_mark_table_zeroing_tasks(Some(pause))
167        }
168        self.zeroing_packets_scheduled
169            .store(false, Ordering::SeqCst);
170        // Set current pause kind
171        self.current_pause.store(Some(pause), Ordering::SeqCst);
172        self.perform_cycle_collection
173            .store(pause != Pause::RefCount, Ordering::SeqCst);
174        // Schedule work
175        match pause {
176            Pause::Full => self.schedule_emergency_full_heap_collection(scheduler),
177            Pause::RefCount => self.schedule_rc_collection(scheduler),
178            Pause::InitialMark => self.schedule_concurrent_marking_initial_pause(scheduler),
179            Pause::FinalMark => self.schedule_concurrent_marking_final_pause(scheduler),
180        }
181        // Analysis routine that is ran. It is generally recommended to take advantage
182        // of the scheduling system we have in place for more performance
183        #[cfg(feature = "analysis")]
184        scheduler.work_buckets[WorkBucketStage::Unconstrained].add(GcHookWork);
185        // Resume mutators
186        if pause == Pause::Full || pause == Pause::FinalMark {
187            #[cfg(feature = "sanity")]
188            scheduler.work_buckets[WorkBucketStage::Final].add(ScheduleSanityGC::<Self>::new(self));
189        }
190    }
191
192    fn get_allocator_mapping(&self) -> &'static EnumMap<AllocationSemantics, AllocatorSelector> {
193        &ALLOCATOR_MAPPING
194    }
195
196    fn prepare(&mut self, _tls: VMWorkerThread) {
197        let pause = self.current_pause().unwrap();
198        if pause == Pause::FinalMark || pause == Pause::Full {
199            self.common.los.is_end_of_satb_or_full_gc = true;
200            // release nursery memory before mature evacuation, to reduce the chance of to-space overflow.
201            self.immix_space.scheduler().work_buckets[WorkBucketStage::Unconstrained]
202                .add(ReleaseLOSNursery);
203        }
204
205        let starts_mark_cycle = pause == Pause::Full || pause == Pause::InitialMark;
206        // Only do prepare if we start a new mark cycle. Otherwise, let thoe spaces have sticky mark bits.
207        if starts_mark_cycle {
208            // We have tested that the following spaces -- they are used by Julia
209            self.common.immortal.prepare();
210            #[cfg(feature = "vm_space")]
211            self.common.base.vm_space.prepare();
212            // TODO: We haven't tested these spaces. But ideally they should be handled in the same way here.
213            self.common.prepare_nonmoving_space(starts_mark_cycle);
214            #[cfg(feature = "code_space")]
215            self.common.base.code_space.prepare();
216            #[cfg(feature = "code_space")]
217            self.common.base.code_lo_space.prepare();
218            #[cfg(feature = "ro_space")]
219            self.common.base.ro_space.prepare();
220        }
221        // LOS is aware of LXR. Call its prepare unconditionally.
222        self.common.los.prepare(starts_mark_cycle);
223
224        if super::MATURE_EVACUATION && (pause == Pause::FinalMark || pause == Pause::Full) {
225            self.process_mature_evacuation_remset();
226        }
227        if super::MATURE_EVACUATION && (pause == Pause::InitialMark || pause == Pause::Full) {
228            // Select mature evacuation set
229            self.schedule_defrag_selection_packets();
230        }
231        self.num_clean_blocks_released_lazy
232            .store(0, Ordering::SeqCst);
233        self.immix_space.prepare_rc(pause);
234        self.block_allocation
235            .reset_block_mark_for_mutator_reused_blocks(pause);
236    }
237
238    fn release(&mut self, tls: VMWorkerThread) {
239        super::SURVIVAL_RATIO_PREDICTOR.update_ratios();
240        let pause = self.current_pause().unwrap();
241        if pause == Pause::FinalMark || pause == Pause::Full {
242            VM::VMCollection::update_weak_processor(false);
243        }
244        <VM as VMBinding>::VMCollection::vm_release();
245        self.common.los.is_end_of_satb_or_full_gc = false;
246        self.common
247            .release(tls, pause == Pause::Full || pause == Pause::FinalMark);
248        self.block_allocation
249            .sweep_nursery_blocks(self.immix_space.scheduler(), pause);
250        self.block_allocation.sweep_mutator_reused_blocks(pause);
251        // Check if we want to do all decs and sweeping in the pause
252        if super::disable_lasy_dec_for_current_gc() {
253            self.immix_space
254                .scheduler()
255                .process_concurrent_packets_in_pause();
256        } else {
257            debug_assert_ne!(pause, Pause::Full);
258        }
259        self.immix_space.release_rc();
260        self.schedule_mature_sweeping(pause);
261        // swap roots
262        let mut prev_roots = self.prev_roots.write().unwrap();
263        let mut curr_roots = self.curr_roots.write().unwrap();
264        std::mem::swap::<SegQueue<_>>(&mut prev_roots, &mut curr_roots);
265        debug_assert!(curr_roots.is_empty());
266    }
267
268    fn get_collection_reserved_pages(&self) -> usize {
269        let survival = {
270            let predicted_survival = (self.block_allocation.clean_nursery_mb() as f64
271                * super::SURVIVAL_RATIO_PREDICTOR.copy_promote_ratio())
272                as usize;
273            predicted_survival << LOG_CONSERVATIVE_SURVIVAL_RATIO_MULTIPLER
274        };
275        survival + self.immix_space.defrag_headroom_pages()
276    }
277
278    fn get_used_pages(&self) -> usize {
279        self.immix_space.reserved_pages() + self.common.get_used_pages()
280    }
281
282    fn base(&self) -> &BasePlan<VM> {
283        &self.common.base
284    }
285
286    fn base_mut(&mut self) -> &mut BasePlan<VM> {
287        &mut self.common.base
288    }
289
290    fn common(&self) -> &CommonPlan<VM> {
291        &self.common
292    }
293
294    /// Get a mutable reference to the common plan. See [`Self::common`].
295    fn common_mut(&mut self) -> &mut CommonPlan<Self::VM> {
296        &mut self.common
297    }
298
299    fn on_pause_start(&self, mmtk: &'static MMTK<Self::VM>) {
300        super::NO_EVAC.store(false, Ordering::SeqCst);
301        let pause = self.current_pause().unwrap();
302
303        // Individual RC pauses that don't overlap with concurrent tracing consist of a GC cycle.
304        // Concurrent tracing, including RC pauses in between, counts as one GC cycle.
305        // A Full GC counts as a GC cycle.
306        if pause == Pause::RefCount && !self.concurrent_work_in_progress()
307            || pause == Pause::InitialMark
308            || pause == Pause::Full
309        {
310            mmtk.gc_trigger.policy.on_gc_start(mmtk);
311        }
312
313        super::SURVIVAL_RATIO_PREDICTOR
314            .set_alloc_size(self.block_allocation.total_young_allocation_in_bytes());
315
316        if pause == Pause::Full || pause == Pause::InitialMark {
317            // Reset block mark and object mark table.
318            let work_packets = self.generate_full_trace_prepare_tasks();
319            self.immix_space.scheduler().work_buckets[WorkBucketStage::RCProcessIncs]
320                .bulk_add(work_packets);
321        }
322
323        for mutator in <VM as VMBinding>::VMActivePlan::mutators() {
324            mutator.flush();
325        }
326
327        if pause == Pause::FinalMark {
328            self.set_concurrent_marking_state(false);
329        }
330    }
331
332    fn on_pause_end(&mut self, mmtk: &'static MMTK<Self::VM>, tls: VMWorkerThread) {
333        super::DISABLE_LASY_DEC_FOR_CURRENT_GC.store(false, Ordering::SeqCst);
334        // self.immix_space.flush_page_resource();
335        let pause = self.current_pause().unwrap();
336        if pause == Pause::InitialMark {
337            self.set_concurrent_marking_state(true);
338        }
339        self.previous_pause.store(Some(pause), Ordering::SeqCst);
340        self.current_pause.store(None, Ordering::SeqCst);
341        LAZY_SWEEPING_JOBS.write().swap();
342        if super::LAZY_DECREMENTS {
343            let perform_cycle_collection =
344                self.get_available_pages() < super::CYCLE_TRIGGER_THRESHOLD;
345            self.hint_cycle_gc
346                .store(perform_cycle_collection, Ordering::SeqCst);
347            self.hint_emergency_gc.store(false, Ordering::SeqCst);
348            self.perform_cycle_collection.store(false, Ordering::SeqCst);
349        }
350        self.avail_pages_at_end_of_last_gc
351            .store(self.get_available_pages(), Ordering::SeqCst);
352        HEAP_AFTER_GC.store(self.get_reserved_pages(), Ordering::SeqCst);
353
354        self.common_mut().on_pause_end(tls);
355
356        // Individual RC pauses that don't overlap with concurrent tracing consist of a GC cycle.
357        // Concurrent tracing, including RC pauses in between, counts as one GC cycle.
358        // A Full GC counts as a GC cycle.
359        if pause == Pause::RefCount && !self.concurrent_work_in_progress()
360            || pause == Pause::FinalMark
361            || pause == Pause::Full
362        {
363            mmtk.gc_trigger.policy.on_gc_end(mmtk);
364        }
365    }
366
367    fn root_scanning_stage(&self) -> WorkBucketStage {
368        WorkBucketStage::RCProcessIncsNonMoving
369    }
370
371    fn concurrent(&self) -> Option<&dyn ConcurrentPlan<VM = VM>> {
372        Some(self)
373    }
374}
375
376impl<VM: VMBinding> ConcurrentPlan for LXR<VM> {
377    fn current_pause(&self) -> Option<Pause> {
378        self.current_pause.load(Ordering::SeqCst)
379    }
380
381    fn concurrent_work_in_progress(&self) -> bool {
382        self.in_concurrent_marking.load(Ordering::Acquire)
383    }
384
385    fn on_concurrent_work_interrupted(&self) {
386        // Do nothing
387    }
388}
389
390impl<VM: VMBinding> LXR<VM> {
391    pub fn new(args: CreateGeneralPlanArgs<VM>) -> Box<Self> {
392        assert!(
393            VM::VMObjectModel::LOCAL_FORWARDING_BITS_SPEC.is_in_header(),
394            "LXR does not support placing forwarding bits on the side."
395        );
396        let num_workers = args.scheduler.num_workers();
397        #[allow(unused_mut)]
398        let mut specs = vec![
399            MetadataSpec::OnSide(RC_TABLE),
400            MetadataSpec::OnSide(
401                *VM::VMObjectModel::GLOBAL_FIELD_UNLOG_BIT_SPEC
402                    .as_spec()
403                    .extract_side_spec(),
404            ),
405        ];
406        // The per-object log bit has to be registered too, not just the per-field one. LXR's
407        // own barrier only consults the field bits, this can be an issue for the probable write API (no field given).
408        // With `lxr_object_log`, the probable write API also logs the object bit.
409        // TODO: We should examine if we can steal a bit from the field log its as the 'logical' object log bit.
410        // We potentially could use the field log bit at the object start, or (object ref - lower bound) -- there should
411        // be no field at those addresses.
412        #[cfg(feature = "lxr_object_log")]
413        specs.push(*VM::VMObjectModel::GLOBAL_LOG_BIT_SPEC.as_spec());
414        let immix_specs = metadata::extract_side_metadata(&specs);
415        let global_side_metadata_specs = SideMetadataContext::new_global_specs(&immix_specs);
416        let mut plan_args = CreateSpecificPlanArgs {
417            global_args: args,
418            constraints: &LXR_CONSTRAINTS,
419            global_side_metadata_specs,
420        };
421        let immix_space = ImmixSpace::new(
422            plan_args.get_mature_space_args("immix", true, false, VMRequest::discontiguous()),
423            ImmixSpaceArgs {
424                never_move_objects: false,
425                mixed_age: false,
426            },
427        );
428        let mut lxr = Box::new(LXR {
429            immix_space,
430            common: CommonPlan::new(plan_args),
431            perform_cycle_collection: AtomicBool::new(false),
432            hint_cycle_gc: AtomicBool::new(false),
433            hint_emergency_gc: AtomicBool::new(false),
434            current_pause: Atomic::new(None),
435            previous_pause: Atomic::new(None),
436            avail_pages_at_end_of_last_gc: AtomicUsize::new(0),
437            zeroing_packets_scheduled: AtomicBool::new(false),
438            decide_cycle_collection: (Mutex::new(true), Condvar::new()),
439            in_concurrent_marking: AtomicBool::new(false),
440            prev_roots: Default::default(),
441            curr_roots: Default::default(),
442            rc: RefCountHelper::NEW,
443            block_allocation: BlockAllocation::new(),
444            evac_set: MatureEvacuationSet::default(),
445            mature_evac_remset: MatureEvecRemSet::new(num_workers),
446            possibly_dead_mature_blocks: Default::default(),
447            num_clean_blocks_released_lazy: Default::default(),
448        });
449
450        lxr.gc_init();
451
452        // Note: `verify_side_metadata_sanity` is invoked later by `MMTK::new`, after the dynamic
453        // side metadata base address has been initialized. It must not be called here during plan
454        // construction, as the side metadata layout is not yet registered at this point.
455
456        lxr
457    }
458
459    pub fn cm_enabled(&self) -> bool {
460        !cfg!(feature = "lxr_no_cm")
461    }
462
463    fn schedule_defrag_selection_packets(&self) {
464        self.evac_set
465            .schedule_defrag_selection_packets(&self.immix_space)
466    }
467
468    /// Generate chunk sweep work packets.
469    fn generate_dead_cycle_sweep_tasks(&self) -> Vec<Box<dyn GCWork<VM>>> {
470        self.immix_space.chunk_map.generate_tasks_batched(
471            self.immix_space.scheduler().num_workers(),
472            |chunks| {
473                Box::new(SweepDeadCycles::new(
474                    chunks,
475                    LazySweepingJobsCounter::new_decs(),
476                ))
477            },
478        )
479    }
480
481    fn schedule_mature_sweeping(&self, pause: Pause) {
482        if pause == Pause::Full || pause == Pause::FinalMark {
483            self.evac_set
484                .sweep_mature_evac_candidates(&self.immix_space);
485            let disable_lasy_dec_for_current_gc =
486                crate::plan::lxr::disable_lasy_dec_for_current_gc();
487            let dead_cycle_sweep_packets = self.generate_dead_cycle_sweep_tasks();
488            let sweep_los = RCSweepMatureAfterSATBLOS::new(LazySweepingJobsCounter::new_decs());
489            if super::LAZY_DECREMENTS && !disable_lasy_dec_for_current_gc {
490                debug_assert_ne!(pause, Pause::Full);
491                let concurrent_bucket =
492                    &self.immix_space.scheduler().work_buckets[WorkBucketStage::Concurrent];
493                concurrent_bucket.bulk_add_deferred(dead_cycle_sweep_packets);
494                concurrent_bucket.add_deferred(Box::new(sweep_los));
495            } else {
496                self.immix_space.scheduler().work_buckets[WorkBucketStage::STWRCDecsAndSweep]
497                    .bulk_add(dead_cycle_sweep_packets);
498                self.immix_space.scheduler().work_buckets[WorkBucketStage::STWRCDecsAndSweep]
499                    .add(sweep_los);
500            }
501        }
502    }
503
504    /// Generate chunk sweep work packets.
505    fn generate_full_trace_prepare_tasks(&self) -> Vec<Box<dyn GCWork<VM>>> {
506        self.immix_space
507            .chunk_map
508            .generate_tasks_batched(self.immix_space.scheduler().num_workers(), |chunks| {
509                Box::new(PrepareChunksForFullGC { chunks })
510            })
511    }
512
513    fn schedule_rc_block_sweeping_tasks(&self, counter: LazySweepingJobsCounter) {
514        // while let Some(x) = self.last_mutator_recycled_blocks.pop() {
515        //     x.set_state(BlockState::Marked);
516        // }
517        // This may happen either within a pause, or in concurrent.
518        let size = self.possibly_dead_mature_blocks.len();
519        let num_bins = self.immix_space.scheduler().num_workers();
520        let bin_cap = size / num_bins + if size % num_bins == 0 { 0 } else { 1 };
521        let mut bins = (0..num_bins)
522            .map(|_| Vec::with_capacity(bin_cap))
523            .collect::<Vec<Vec<(Block, bool)>>>();
524        'out: for bin in bins.iter_mut() {
525            for _ in 0..bin_cap {
526                if let Some(block) = self.possibly_dead_mature_blocks.pop() {
527                    bin.push(block);
528                } else {
529                    break 'out;
530                }
531            }
532        }
533        let packets = bins
534            .into_iter()
535            .map::<Box<dyn GCWork<VM>>, _>(|blocks| {
536                Box::new(SweepBlocksAfterDecs::new(blocks, counter.clone()))
537            })
538            .collect();
539        self.immix_space.scheduler().work_buckets[WorkBucketStage::Unconstrained].bulk_add(packets);
540    }
541
542    pub(super) fn process_mature_evacuation_remset(&self) {
543        self.mature_evac_remset.flush_all();
544        let packets = self.mature_evac_remset.take_global_packets();
545        self.immix_space.scheduler().work_buckets[WorkBucketStage::RCEvacuateMature]
546            .bulk_add(packets);
547    }
548
549    pub(super) fn add_to_possibly_dead_mature_blocks(&self, block: Block, is_defrag_source: bool) {
550        if block.log() {
551            self.possibly_dead_mature_blocks
552                .push((block, is_defrag_source));
553        }
554    }
555
556    fn next_gc_is_emergency_gc(
557        &self,
558        total_pages: usize,
559        mature_space_pages: usize,
560        emergency_threshold: usize,
561    ) -> bool {
562        let min_avail_pages = usize::min(total_pages * emergency_threshold / 100, 1 << 30 >> 12);
563        total_pages < min_avail_pages + mature_space_pages
564    }
565
566    fn next_gc_is_cycle_gc(&self, mature_space_pages: usize, pause: Pause) -> bool {
567        if pause == Pause::FinalMark || pause == Pause::Full {
568            super::MATURE_LIVE_PREDICTOR.update(mature_space_pages);
569        }
570        let live_mature_pages = super::MATURE_LIVE_PREDICTOR.live_pages() as usize;
571        let garbage = mature_space_pages.saturating_sub(live_mature_pages);
572        let total_pages = self.get_total_pages();
573        !self.concurrent_work_in_progress()
574            && (self.cm_enabled() && garbage * 100 >= super::TRACE_THRESHOLD * total_pages)
575    }
576
577    fn decide_next_gc_may_perform_cycle_collection(&self, pause: Pause) {
578        let (lock, cvar) = &self.decide_cycle_collection;
579        let notify = || {
580            let mut decide_cycle_collection = lock.lock().unwrap();
581            *decide_cycle_collection = true;
582            cvar.notify_one();
583        };
584        // Reset states
585        self.hint_cycle_gc.store(false, Ordering::SeqCst);
586        self.hint_emergency_gc.store(false, Ordering::SeqCst);
587        let emergency_threshold = super::RC_STOP_PERCENT;
588        // Calculate mature space size
589        let total_pages = self.get_total_pages();
590        let mature_space_pages = {
591            let released_los_pages = self.los().num_pages_released_lazy.load(Ordering::SeqCst);
592            HEAP_AFTER_GC
593                .load(Ordering::SeqCst)
594                .saturating_sub(
595                    self.num_clean_blocks_released_lazy.load(Ordering::SeqCst) << Block::LOG_PAGES,
596                )
597                .saturating_sub(released_los_pages)
598        };
599        // Decide next GC kind
600        let hint_cycle_gc = self.next_gc_is_cycle_gc(mature_space_pages, pause);
601        let hint_emergency_gc =
602            self.next_gc_is_emergency_gc(total_pages, mature_space_pages, emergency_threshold);
603        // Update states
604        self.hint_cycle_gc.store(hint_cycle_gc, Ordering::SeqCst);
605        self.hint_emergency_gc
606            .store(hint_emergency_gc, Ordering::SeqCst);
607        // Eager mark-table zeroing
608        if !cfg!(feature = "sanity") && hint_cycle_gc {
609            self.schedule_mark_table_zeroing_tasks(None);
610        }
611        notify();
612    }
613
614    fn schedule_mark_table_zeroing_tasks(&self, pause: Option<Pause>) {
615        if let Some(pause) = pause {
616            assert!(pause == Pause::InitialMark || pause == Pause::Full);
617            if self.zeroing_packets_scheduled.load(Ordering::SeqCst) {
618                return;
619            }
620        }
621        let work_packets = self
622            .immix_space
623            .chunk_map
624            .generate_tasks_batched(self.immix_space.scheduler().num_workers(), |chunks| {
625                Box::new(ConcurrentChunkMetadataZeroing { chunks })
626            });
627        self.immix_space.scheduler().work_buckets[WorkBucketStage::Unconstrained]
628            .bulk_add(work_packets);
629        self.zeroing_packets_scheduled.store(true, Ordering::SeqCst);
630    }
631
632    fn wait_for_decide_cycle_collection(&self) {
633        let (lock, cvar) = &self.decide_cycle_collection;
634        let mut decide_cycle_collection = lock.lock().unwrap();
635        while !*decide_cycle_collection {
636            decide_cycle_collection = cvar.wait(decide_cycle_collection).unwrap();
637        }
638        *decide_cycle_collection = false;
639    }
640
641    fn select_collection_kind(&self) -> Pause {
642        self.wait_for_decide_cycle_collection();
643
644        let emergency = self.base().global_state.is_emergency_collection();
645        let user_triggered = self.base().global_state.is_user_triggered_collection();
646        let cm_in_progress = self.concurrent_work_in_progress();
647        let cm_packets_drained = super::concurrent_marking_packets_drained();
648        let hint_cycle_gc = self.hint_cycle_gc.load(Ordering::SeqCst);
649        let hint_emergency_gc = self.hint_emergency_gc.load(Ordering::SeqCst);
650        // If CM is finished, do a final mark pause
651        if cm_in_progress && cm_packets_drained {
652            return Pause::FinalMark;
653        }
654
655        // Either final mark pause or full pause for emergency GC
656        if emergency || user_triggered || hint_emergency_gc {
657            return if cm_in_progress {
658                Pause::FinalMark
659            } else {
660                Pause::Full
661            };
662        }
663
664        // Should trigger CM?
665        if hint_cycle_gc && !cm_in_progress {
666            if self.cm_enabled() {
667                Pause::InitialMark
668            } else {
669                Pause::Full
670            }
671        } else {
672            Pause::RefCount
673        }
674    }
675
676    fn disable_unnecessary_buckets(&'static self, scheduler: &GCWorkScheduler<VM>, pause: Pause) {
677        // Set conditional buckets
678        scheduler.work_buckets[WorkBucketStage::RCProcessIncsNonMoving].set_enabled(true);
679        scheduler.work_buckets[WorkBucketStage::RCProcessIncs].set_enabled(true);
680        scheduler.work_buckets[WorkBucketStage::Prepare].set_enabled(pause != Pause::RefCount);
681        let final_mark_or_full = pause == Pause::FinalMark || pause == Pause::Full;
682        // Marks roots reported as objects, before `Closure` can evacuate anything.
683        scheduler.work_buckets[WorkBucketStage::PinningRootsTrace].set_enabled(final_mark_or_full);
684        scheduler.work_buckets[WorkBucketStage::Closure].set_enabled(final_mark_or_full);
685        scheduler.work_buckets[WorkBucketStage::WeakRefClosure].set_enabled(final_mark_or_full);
686        scheduler.work_buckets[WorkBucketStage::FinalRefClosure].set_enabled(final_mark_or_full);
687        scheduler.work_buckets[WorkBucketStage::PhantomRefClosure].set_enabled(final_mark_or_full);
688        scheduler.work_buckets[WorkBucketStage::STWRCDecsAndSweep]
689            .set_enabled(!(super::LAZY_DECREMENTS && pause != Pause::Full));
690        // Always enabled
691        scheduler.work_buckets[WorkBucketStage::Concurrent].set_enabled(true);
692        scheduler.work_buckets[WorkBucketStage::ConcurrentResumable].set_enabled(true);
693        // Always disabled
694        // LXR never routes work here: it has no transitively pinning closure. Transitive
695        // pinning roots, where accepted at all, take the ordinary node-root path instead.
696        scheduler.work_buckets[WorkBucketStage::TPinningClosure].set_enabled(false);
697        scheduler.work_buckets[WorkBucketStage::VMRefClosure].set_enabled(false);
698        scheduler.work_buckets[WorkBucketStage::VMRefForwarding].set_enabled(false);
699        scheduler.work_buckets[WorkBucketStage::SoftRefClosure].set_enabled(false);
700        scheduler.work_buckets[WorkBucketStage::CalculateForwarding].set_enabled(false);
701        scheduler.work_buckets[WorkBucketStage::SecondRoots].set_enabled(false);
702        scheduler.work_buckets[WorkBucketStage::RefForwarding].set_enabled(false);
703        scheduler.work_buckets[WorkBucketStage::FinalizableForwarding].set_enabled(false);
704        scheduler.work_buckets[WorkBucketStage::Compact].set_enabled(false);
705    }
706
707    fn schedule_rc_collection(&'static self, scheduler: &GCWorkScheduler<VM>) {
708        log::info!("Scheduling RC collection...");
709        self.disable_unnecessary_buckets(scheduler, Pause::RefCount);
710        // Before start yielding, wrap all the roots from the previous GC with work-packets.
711        self.process_prev_roots(scheduler);
712        // Stop & scan mutators (mutator scanning can happen before STW)
713        scheduler.work_buckets[WorkBucketStage::Unconstrained]
714            .add(StopMutators::<LXRGCWorkContext<VM>>::new_with_flush());
715        // Prepare global/collectors/mutators
716        scheduler.work_buckets[WorkBucketStage::RCProcessIncs].add(FastRCPrepare);
717        // Release global/collectors/mutators
718        scheduler.work_buckets[WorkBucketStage::Release]
719            .add(Release::<LXRGCWorkContext<VM>>::new(self));
720    }
721
722    fn schedule_concurrent_marking_initial_pause(&'static self, scheduler: &GCWorkScheduler<VM>) {
723        log::info!("Scheduling concurrent marking initial pause...");
724        self.disable_unnecessary_buckets(scheduler, Pause::InitialMark);
725        self.process_prev_roots(scheduler);
726        scheduler.work_buckets[WorkBucketStage::Unconstrained]
727            .add(StopMutators::<LXRGCWorkContext<VM>>::new_with_flush());
728        scheduler.work_buckets[WorkBucketStage::Prepare]
729            .add(Prepare::<LXRGCWorkContext<VM>>::new(self));
730        scheduler.work_buckets[WorkBucketStage::Release]
731            .add(Release::<LXRGCWorkContext<VM>>::new(self));
732    }
733
734    fn schedule_concurrent_marking_final_pause(&'static self, scheduler: &GCWorkScheduler<VM>) {
735        log::info!("Scheduling concurrent marking final pause...");
736        self.disable_unnecessary_buckets(scheduler, Pause::FinalMark);
737        self.process_prev_roots(scheduler);
738        scheduler.work_buckets[WorkBucketStage::Unconstrained]
739            .add(StopMutators::<LXRGCWorkContext<VM>>::new_with_flush());
740
741        scheduler.work_buckets[WorkBucketStage::Prepare]
742            .add(Prepare::<LXRGCWorkContext<VM>>::new(self));
743        scheduler.work_buckets[WorkBucketStage::Release]
744            .add(Release::<LXRGCWorkContext<VM>>::new(self));
745    }
746
747    fn schedule_emergency_full_heap_collection(&'static self, scheduler: &GCWorkScheduler<VM>) {
748        log::info!("Scheduling emergency full-heap collection...");
749        super::DISABLE_LASY_DEC_FOR_CURRENT_GC.store(true, Ordering::SeqCst);
750        self.disable_unnecessary_buckets(scheduler, Pause::Full);
751        // Before start yielding, wrap all the roots from the previous GC with work-packets.
752        self.process_prev_roots(scheduler);
753        // Stop & scan mutators (mutator scanning can happen before STW)
754        scheduler.work_buckets[WorkBucketStage::Unconstrained]
755            .add(StopMutators::<LXRGCWorkContext<VM>>::new_with_flush());
756        // Prepare global/collectors/mutators
757        scheduler.work_buckets[WorkBucketStage::Prepare]
758            .add(Prepare::<LXRGCWorkContext<VM>>::new(self));
759        // Release global/collectors/mutators
760        scheduler.work_buckets[WorkBucketStage::Release]
761            .add(Release::<LXRGCWorkContext<VM>>::new(self));
762    }
763
764    fn process_prev_roots(&self, scheduler: &GCWorkScheduler<VM>) {
765        let prev_roots = self.prev_roots.read().unwrap();
766        let mut work_packets: Vec<Box<dyn GCWork<VM>>> = Vec::with_capacity(prev_roots.len());
767        while let Some(decs) = prev_roots.pop() {
768            work_packets.push(Box::new(ProcessDecs::new(
769                decs,
770                LazySweepingJobsCounter::new_decs(),
771            )))
772        }
773        if work_packets.is_empty() {
774            work_packets.push(Box::new(ProcessDecs::new(
775                vec![],
776                LazySweepingJobsCounter::new_decs(),
777            )));
778        }
779        if super::LAZY_DECREMENTS {
780            scheduler.work_buckets[WorkBucketStage::Concurrent].bulk_add_deferred(work_packets);
781        } else {
782            scheduler.work_buckets[WorkBucketStage::STWRCDecsAndSweep].bulk_add(work_packets);
783        }
784    }
785
786    pub fn current_pause(&self) -> Option<Pause> {
787        self.current_pause.load(Ordering::SeqCst)
788    }
789
790    pub fn previous_pause(&self) -> Option<Pause> {
791        self.previous_pause.load(Ordering::SeqCst)
792    }
793
794    pub fn in_defrag(&self, o: ObjectReference) -> bool {
795        self.immix_space.in_space(o) && Block::in_defrag_block(o)
796    }
797
798    pub fn address_in_defrag(&self, a: Address) -> bool {
799        self.immix_space.address_in_space(a) && Block::address_in_defrag_block(a)
800    }
801
802    pub fn mark(&self, o: ObjectReference) -> bool {
803        if self.immix_space.in_space(o) {
804            self.immix_space.attempt_mark(o)
805        } else if self.common.los.in_space(o) {
806            self.common.los.attempt_mark(o)
807        } else {
808            // TODO: We need to properly handle this case.
809            // This is a temporary solution for Julia -- the only other spaces it uses are immortal space and vm space, where objects won't die.
810            debug_assert!(o.is_live());
811            false
812        }
813    }
814
815    pub fn is_marked(&self, o: ObjectReference) -> bool {
816        if self.immix_space.in_space(o) {
817            self.immix_space.is_marked(o)
818        } else if self.common.los.in_space(o) {
819            self.common.los.is_marked(o)
820        } else {
821            // TODO: We need to properly handle this case.
822            // This is a temporary solution for Julia -- the only other spaces it uses are immortal space and vm space, where objects won't die.
823            debug_assert!(o.is_live());
824            true
825        }
826    }
827
828    pub const fn los(&self) -> &LargeObjectSpace<VM> {
829        &self.common.los
830    }
831
832    /// Whether `o` lives in a space LXR reference-counts (immix space or LOS). Objects
833    /// elsewhere (e.g. Julia's sysimage in the immortal/VM space) carry no reference count.
834    pub fn is_rc_object(&self, o: ObjectReference) -> bool {
835        self.immix_space.in_space(o) || self.common.los.in_space(o)
836    }
837
838    fn on_lazy_decs_finished(&self, c: LazySweepingJobsCounter) {
839        self.schedule_rc_block_sweeping_tasks(c);
840    }
841
842    fn on_lazy_sweeping_finished(&self) {
843        self.immix_space.flush_page_resource();
844        // Update counters
845        if !super::LAZY_DECREMENTS {
846            HEAP_AFTER_GC.store(self.get_used_pages(), Ordering::SeqCst);
847        }
848        let pause = match self.current_pause() {
849            Some(p) => p,
850            None => self.previous_pause().unwrap(),
851        };
852        self.decide_next_gc_may_perform_cycle_collection(pause);
853    }
854
855    fn gc_init(&mut self) {
856        self.immix_space.rc_enabled = true;
857        self.common.los.rc_enabled = true;
858        unsafe {
859            let me: &'static Self = &*(self as *const Self);
860            me.block_allocation.init(&me.immix_space, me);
861            me.immix_space.install_hooks(&me.block_allocation);
862        }
863        let mut lazy_sweeping_jobs = LAZY_SWEEPING_JOBS.write();
864        lazy_sweeping_jobs.swap();
865        let lxr_ptr = self as *const Self as usize;
866        lazy_sweeping_jobs.end_of_decs = Some(Box::new(move |c| {
867            let lxr = unsafe { &*(lxr_ptr as *const Self) };
868            lxr.on_lazy_decs_finished(c);
869        }));
870        lazy_sweeping_jobs.end_of_lazy = Some(Box::new(move || {
871            let lxr = unsafe { &*(lxr_ptr as *const Self) };
872            lxr.on_lazy_sweeping_finished();
873        }));
874    }
875
876    fn set_concurrent_marking_state(&self, active: bool) {
877        self.in_concurrent_marking.store(active, Ordering::SeqCst);
878        self.common
879            .los
880            .bump_page_reuse_count
881            .store(active, Ordering::SeqCst);
882    }
883}