mmtk/plan/immix/
global.rs

1use super::gc_work::ImmixGCWorkContext;
2use super::mutator::ALLOCATOR_MAPPING;
3use crate::plan::global::BasePlan;
4use crate::plan::global::CommonPlan;
5use crate::plan::global::CreateGeneralPlanArgs;
6use crate::plan::global::CreateSpecificPlanArgs;
7use crate::plan::AllocationSemantics;
8use crate::plan::Plan;
9use crate::plan::PlanConstraints;
10use crate::policy::immix::ImmixSpaceArgs;
11use crate::policy::immix::{TRACE_KIND_DEFRAG, TRACE_KIND_FAST};
12use crate::policy::space::Space;
13use crate::scheduler::*;
14use crate::util::alloc::allocators::AllocatorSelector;
15use crate::util::copy::*;
16use crate::util::heap::gc_trigger::SpaceStats;
17use crate::util::heap::VMRequest;
18use crate::util::metadata::log_bit::UnlogBitsOperation;
19use crate::util::metadata::side_metadata::SideMetadataContext;
20use crate::vm::VMBinding;
21use crate::{policy::immix::ImmixSpace, util::opaque_pointer::VMWorkerThread};
22use std::sync::atomic::AtomicBool;
23
24use atomic::Ordering;
25use enum_map::EnumMap;
26
27use mmtk_macros::{HasSpaces, PlanTraceObject};
28
29#[derive(HasSpaces, PlanTraceObject)]
30pub struct Immix<VM: VMBinding> {
31    #[post_scan]
32    #[space]
33    #[copy_semantics(CopySemantics::DefaultCopy)]
34    pub immix_space: ImmixSpace<VM>,
35    #[parent]
36    pub common: CommonPlan<VM>,
37    last_gc_was_defrag: AtomicBool,
38}
39
40/// The plan constraints for the immix plan.
41pub const IMMIX_CONSTRAINTS: PlanConstraints = PlanConstraints {
42    // If we disable moving in Immix, this is a non-moving plan.
43    moves_objects: !cfg!(feature = "immix_non_moving"),
44    // Max immix object size is half of a block.
45    max_non_los_default_alloc_bytes: crate::policy::immix::MAX_IMMIX_OBJECT_SIZE,
46    ..PlanConstraints::default()
47};
48
49impl<VM: VMBinding> Plan for Immix<VM> {
50    fn collection_required(&self, space_full: bool, _space: Option<SpaceStats<Self::VM>>) -> bool {
51        self.base().collection_required(self, space_full)
52    }
53
54    fn last_collection_was_exhaustive(&self) -> bool {
55        self.immix_space
56            .is_last_gc_exhaustive(self.last_gc_was_defrag.load(Ordering::Relaxed))
57    }
58
59    fn constraints(&self) -> &'static PlanConstraints {
60        &IMMIX_CONSTRAINTS
61    }
62
63    fn create_copy_config(&'static self) -> CopyConfig<Self::VM> {
64        use enum_map::enum_map;
65        CopyConfig {
66            copy_mapping: enum_map! {
67                CopySemantics::DefaultCopy => CopySelector::Immix(0),
68                _ => CopySelector::Unused,
69            },
70            space_mapping: vec![(CopySelector::Immix(0), &self.immix_space)],
71            constraints: &IMMIX_CONSTRAINTS,
72        }
73    }
74
75    fn schedule_collection(&'static self, scheduler: &GCWorkScheduler<VM>) {
76        Self::schedule_immix_full_heap_collection::<
77            Immix<VM>,
78            ImmixGCWorkContext<VM, TRACE_KIND_FAST>,
79            ImmixGCWorkContext<VM, TRACE_KIND_DEFRAG>,
80        >(self, &self.immix_space, scheduler)
81    }
82
83    fn get_allocator_mapping(&self) -> &'static EnumMap<AllocationSemantics, AllocatorSelector> {
84        &ALLOCATOR_MAPPING
85    }
86
87    fn prepare(&mut self, tls: VMWorkerThread) {
88        self.prepare_inner(tls, UnlogBitsOperation::NoOp)
89    }
90
91    fn release(&mut self, tls: VMWorkerThread) {
92        self.release_inner(tls, UnlogBitsOperation::NoOp);
93    }
94
95    fn end_of_gc(&mut self, tls: VMWorkerThread) {
96        self.last_gc_was_defrag
97            .store(self.immix_space.end_of_gc(), Ordering::Relaxed);
98        self.common.end_of_gc(tls);
99    }
100
101    fn current_gc_may_move_object(&self) -> bool {
102        self.immix_space.in_defrag()
103    }
104
105    fn get_collection_reserved_pages(&self) -> usize {
106        self.immix_space.defrag_headroom_pages()
107    }
108
109    fn get_used_pages(&self) -> usize {
110        self.immix_space.reserved_pages() + self.common.get_used_pages()
111    }
112
113    fn base(&self) -> &BasePlan<VM> {
114        &self.common.base
115    }
116
117    fn base_mut(&mut self) -> &mut BasePlan<Self::VM> {
118        &mut self.common.base
119    }
120
121    fn common(&self) -> &CommonPlan<VM> {
122        &self.common
123    }
124}
125
126impl<VM: VMBinding> Immix<VM> {
127    pub fn new(args: CreateGeneralPlanArgs<VM>) -> Self {
128        let plan_args = CreateSpecificPlanArgs {
129            global_args: args,
130            constraints: &IMMIX_CONSTRAINTS,
131            global_side_metadata_specs: SideMetadataContext::new_global_specs(&[]),
132        };
133        Self::new_with_args(
134            plan_args,
135            ImmixSpaceArgs {
136                mixed_age: false,
137                never_move_objects: false,
138            },
139        )
140    }
141
142    pub fn new_with_args(
143        mut plan_args: CreateSpecificPlanArgs<VM>,
144        space_args: ImmixSpaceArgs,
145    ) -> Self {
146        Immix {
147            immix_space: ImmixSpace::new(
148                if space_args.mixed_age {
149                    plan_args.get_mixed_age_space_args(
150                        "immix",
151                        true,
152                        false,
153                        VMRequest::discontiguous(),
154                    )
155                } else {
156                    plan_args.get_normal_space_args(
157                        "immix",
158                        true,
159                        false,
160                        VMRequest::discontiguous(),
161                    )
162                },
163                space_args,
164            ),
165            common: CommonPlan::new(plan_args),
166            last_gc_was_defrag: AtomicBool::new(false),
167        }
168    }
169
170    /// Schedule a full heap immix collection. This method is used by immix/genimmix/stickyimmix
171    /// to schedule a full heap collection. A plan must call set_collection_kind and set_gc_status before this method.
172    pub(crate) fn schedule_immix_full_heap_collection<
173        PlanType: Plan<VM = VM>,
174        FastContext: GCWorkContext<VM = VM, PlanType = PlanType>,
175        DefragContext: GCWorkContext<VM = VM, PlanType = PlanType>,
176    >(
177        plan: &'static DefragContext::PlanType,
178        immix_space: &ImmixSpace<VM>,
179        scheduler: &GCWorkScheduler<VM>,
180    ) {
181        let in_defrag = immix_space.decide_whether_to_defrag(
182            plan.base().global_state.is_emergency_collection(),
183            true,
184            plan.base()
185                .global_state
186                .cur_collection_attempts
187                .load(Ordering::SeqCst),
188            plan.base().global_state.is_user_triggered_collection(),
189            *plan.base().options.full_heap_system_gc,
190        );
191
192        if in_defrag {
193            scheduler.schedule_common_work::<DefragContext>(plan);
194        } else {
195            scheduler.schedule_common_work::<FastContext>(plan);
196        }
197    }
198
199    pub(in crate::plan) fn set_last_gc_was_defrag(&self, defrag: bool, order: Ordering) {
200        self.last_gc_was_defrag.store(defrag, order)
201    }
202
203    /// Prepare with unlog-bit operation.
204    /// Some Immix-derived plans may need to set/clear unlog bits when preparing.
205    pub(in crate::plan) fn prepare_inner(
206        &mut self,
207        tls: VMWorkerThread,
208        unlog_bits_op: UnlogBitsOperation,
209    ) {
210        self.common.prepare(tls, true);
211        self.immix_space.prepare(
212            true,
213            Some(crate::policy::immix::defrag::StatsForDefrag::new(self)),
214            unlog_bits_op,
215        );
216    }
217
218    /// Release with unlog-bit operation.
219    /// Some Immix-derived plans may need to set/clear unlog bits when releasing.
220    pub(in crate::plan) fn release_inner(
221        &mut self,
222        tls: VMWorkerThread,
223        unlog_bits_op: UnlogBitsOperation,
224    ) {
225        self.common.release(tls, true);
226        // release the collected region
227        self.immix_space.release(true, unlog_bits_op);
228    }
229}