mmtk/plan/pageprotect/
global.rs

1use super::gc_work::PPGCWorkContext;
2use super::mutator::ALLOCATOR_MAPPING;
3use crate::plan::global::CreateGeneralPlanArgs;
4use crate::plan::global::CreateSpecificPlanArgs;
5use crate::plan::AllocationSemantics;
6use crate::plan::Plan;
7use crate::plan::PlanConstraints;
8use crate::policy::space::Space;
9use crate::scheduler::*;
10use crate::util::alloc::allocators::AllocatorSelector;
11use crate::util::heap::gc_trigger::SpaceStats;
12use crate::util::heap::VMRequest;
13use crate::util::metadata::side_metadata::SideMetadataContext;
14use crate::{plan::global::BasePlan, vm::VMBinding};
15use crate::{
16    plan::global::CommonPlan, policy::largeobjectspace::LargeObjectSpace,
17    util::opaque_pointer::VMWorkerThread,
18};
19use enum_map::EnumMap;
20
21use mmtk_macros::{HasSpaces, PlanTraceObject};
22
23#[derive(HasSpaces, PlanTraceObject)]
24pub struct PageProtect<VM: VMBinding> {
25    #[space]
26    pub space: LargeObjectSpace<VM>,
27    #[parent]
28    pub common: CommonPlan<VM>,
29}
30
31/// The plan constraints for the page protect plan.
32pub const CONSTRAINTS: PlanConstraints = PlanConstraints {
33    moves_objects: false,
34    ..PlanConstraints::default()
35};
36
37impl<VM: VMBinding> Plan for PageProtect<VM> {
38    fn constraints(&self) -> &'static PlanConstraints {
39        &CONSTRAINTS
40    }
41
42    fn schedule_collection(&'static self, scheduler: &GCWorkScheduler<VM>) {
43        scheduler.schedule_common_work::<PPGCWorkContext<VM>>(self);
44    }
45
46    fn get_allocator_mapping(&self) -> &'static EnumMap<AllocationSemantics, AllocatorSelector> {
47        &ALLOCATOR_MAPPING
48    }
49
50    fn prepare(&mut self, tls: VMWorkerThread) {
51        self.common.prepare(tls, true);
52        self.space.prepare(true);
53    }
54
55    fn release(&mut self, tls: VMWorkerThread) {
56        self.common.release(tls, true);
57        self.space.release(true);
58    }
59
60    fn end_of_gc(&mut self, tls: VMWorkerThread) {
61        self.common.end_of_gc(tls);
62    }
63
64    fn collection_required(&self, space_full: bool, _space: Option<SpaceStats<Self::VM>>) -> bool {
65        self.base().collection_required(self, space_full)
66    }
67
68    fn current_gc_may_move_object(&self) -> bool {
69        false
70    }
71
72    fn get_used_pages(&self) -> usize {
73        self.space.reserved_pages() + self.common.get_used_pages()
74    }
75
76    fn base(&self) -> &BasePlan<VM> {
77        &self.common.base
78    }
79
80    fn base_mut(&mut self) -> &mut BasePlan<Self::VM> {
81        &mut self.common.base
82    }
83
84    fn common(&self) -> &CommonPlan<VM> {
85        &self.common
86    }
87}
88
89impl<VM: VMBinding> PageProtect<VM> {
90    pub fn new(args: CreateGeneralPlanArgs<VM>) -> Self {
91        // Warn users that the plan may fail due to maximum mapping allowed.
92        warn!(
93            "PageProtect uses a high volume of memory mappings. \
94            If you encounter failures in memory protect/unprotect in this plan,\
95            consider increase the maximum mapping allowed by the OS{}.",
96            if cfg!(target_os = "linux") {
97                " (e.g. sudo sysctl -w vm.max_map_count=655300)"
98            } else {
99                ""
100            }
101        );
102
103        let mut plan_args = CreateSpecificPlanArgs {
104            global_args: args,
105            constraints: &CONSTRAINTS,
106            global_side_metadata_specs: SideMetadataContext::new_global_specs(&[]),
107        };
108
109        let ret = PageProtect {
110            space: LargeObjectSpace::new(
111                plan_args.get_normal_space_args(
112                    "pageprotect",
113                    true,
114                    false,
115                    VMRequest::discontiguous(),
116                ),
117                true,
118                false, // PageProtect does not use log bit
119            ),
120            common: CommonPlan::new(plan_args),
121        };
122
123        ret.verify_side_metadata_sanity();
124
125        ret
126    }
127}