|
| 1 | +use super::gc_work::PPProcessEdges; |
| 2 | +use super::mutator::ALLOCATOR_MAPPING; |
| 3 | +use crate::mmtk::MMTK; |
| 4 | +use crate::plan::global::GcStatus; |
| 5 | +use crate::plan::AllocationSemantics; |
| 6 | +use crate::plan::Plan; |
| 7 | +use crate::plan::PlanConstraints; |
| 8 | +use crate::policy::space::Space; |
| 9 | +use crate::scheduler::gc_work::*; |
| 10 | +use crate::scheduler::*; |
| 11 | +use crate::util::alloc::allocators::AllocatorSelector; |
| 12 | +#[cfg(feature = "analysis")] |
| 13 | +use crate::util::analysis::GcHookWork; |
| 14 | +use crate::util::heap::layout::heap_layout::Mmapper; |
| 15 | +use crate::util::heap::layout::heap_layout::VMMap; |
| 16 | +use crate::util::heap::layout::vm_layout_constants::{HEAP_END, HEAP_START}; |
| 17 | +use crate::util::heap::HeapMeta; |
| 18 | +use crate::util::heap::VMRequest; |
| 19 | +use crate::util::metadata::side_metadata::SideMetadataContext; |
| 20 | +use crate::util::options::UnsafeOptionsWrapper; |
| 21 | +#[cfg(feature = "sanity")] |
| 22 | +use crate::util::sanity::sanity_checker::*; |
| 23 | +use crate::{plan::global::BasePlan, vm::VMBinding}; |
| 24 | +use crate::{ |
| 25 | + plan::global::{CommonPlan, NoCopy}, |
| 26 | + policy::largeobjectspace::LargeObjectSpace, |
| 27 | + util::opaque_pointer::VMWorkerThread, |
| 28 | +}; |
| 29 | +use enum_map::EnumMap; |
| 30 | +use std::sync::Arc; |
| 31 | + |
| 32 | +pub struct PageProtect<VM: VMBinding> { |
| 33 | + pub space: LargeObjectSpace<VM>, |
| 34 | + pub common: CommonPlan<VM>, |
| 35 | +} |
| 36 | + |
| 37 | +pub const CONSTRAINTS: PlanConstraints = PlanConstraints { |
| 38 | + moves_objects: false, |
| 39 | + ..PlanConstraints::default() |
| 40 | +}; |
| 41 | + |
| 42 | +impl<VM: VMBinding> Plan for PageProtect<VM> { |
| 43 | + type VM = VM; |
| 44 | + |
| 45 | + fn constraints(&self) -> &'static PlanConstraints { |
| 46 | + &CONSTRAINTS |
| 47 | + } |
| 48 | + |
| 49 | + fn create_worker_local( |
| 50 | + &self, |
| 51 | + tls: VMWorkerThread, |
| 52 | + mmtk: &'static MMTK<Self::VM>, |
| 53 | + ) -> GCWorkerLocalPtr { |
| 54 | + let mut c = NoCopy::new(mmtk); |
| 55 | + c.init(tls); |
| 56 | + GCWorkerLocalPtr::new(c) |
| 57 | + } |
| 58 | + |
| 59 | + fn gc_init( |
| 60 | + &mut self, |
| 61 | + heap_size: usize, |
| 62 | + vm_map: &'static VMMap, |
| 63 | + scheduler: &Arc<MMTkScheduler<VM>>, |
| 64 | + ) { |
| 65 | + // Warn users that the plan may fail due to maximum mapping allowed. |
| 66 | + warn!( |
| 67 | + "PageProtect uses a high volume of memory mappings. \ |
| 68 | + If you encounter failures in memory protect/unprotect in this plan,\ |
| 69 | + consider increase the maximum mapping allowed by the OS{}.", |
| 70 | + if cfg!(target_os = "linux") { |
| 71 | + " (e.g. sudo sysctl -w vm.max_map_count=655300)" |
| 72 | + } else { |
| 73 | + "" |
| 74 | + } |
| 75 | + ); |
| 76 | + self.common.gc_init(heap_size, vm_map, scheduler); |
| 77 | + self.space.init(&vm_map); |
| 78 | + } |
| 79 | + |
| 80 | + fn schedule_collection(&'static self, scheduler: &MMTkScheduler<VM>) { |
| 81 | + self.base().set_collection_kind(); |
| 82 | + self.base().set_gc_status(GcStatus::GcPrepare); |
| 83 | + self.common() |
| 84 | + .schedule_common::<PPProcessEdges<VM>>(&CONSTRAINTS, scheduler); |
| 85 | + // Stop & scan mutators (mutator scanning can happen before STW) |
| 86 | + scheduler.work_buckets[WorkBucketStage::Unconstrained] |
| 87 | + .add(StopMutators::<PPProcessEdges<VM>>::new()); |
| 88 | + // Prepare global/collectors/mutators |
| 89 | + scheduler.work_buckets[WorkBucketStage::Prepare] |
| 90 | + .add(Prepare::<Self, NoCopy<VM>>::new(self)); |
| 91 | + // Release global/collectors/mutators |
| 92 | + scheduler.work_buckets[WorkBucketStage::Release] |
| 93 | + .add(Release::<Self, NoCopy<VM>>::new(self)); |
| 94 | + // Scheduling all the gc hooks of analysis routines. It is generally recommended |
| 95 | + // to take advantage of the scheduling system we have in place for more performance |
| 96 | + #[cfg(feature = "analysis")] |
| 97 | + scheduler.work_buckets[WorkBucketStage::Unconstrained].add(GcHookWork); |
| 98 | + // Resume mutators |
| 99 | + #[cfg(feature = "sanity")] |
| 100 | + scheduler.work_buckets[WorkBucketStage::Final] |
| 101 | + .add(ScheduleSanityGC::<Self, NoCopy<VM>>::new(self)); |
| 102 | + scheduler.set_finalizer(Some(EndOfGC)); |
| 103 | + } |
| 104 | + |
| 105 | + fn get_allocator_mapping(&self) -> &'static EnumMap<AllocationSemantics, AllocatorSelector> { |
| 106 | + &*ALLOCATOR_MAPPING |
| 107 | + } |
| 108 | + |
| 109 | + fn prepare(&mut self, tls: VMWorkerThread) { |
| 110 | + self.common.prepare(tls, true); |
| 111 | + self.space.prepare(true); |
| 112 | + } |
| 113 | + |
| 114 | + fn release(&mut self, tls: VMWorkerThread) { |
| 115 | + self.common.release(tls, true); |
| 116 | + self.space.release(true); |
| 117 | + } |
| 118 | + |
| 119 | + fn collection_required(&self, space_full: bool, space: &dyn Space<Self::VM>) -> bool { |
| 120 | + self.base().collection_required(self, space_full, space) |
| 121 | + } |
| 122 | + |
| 123 | + fn get_collection_reserve(&self) -> usize { |
| 124 | + 0 |
| 125 | + } |
| 126 | + |
| 127 | + fn get_pages_used(&self) -> usize { |
| 128 | + self.space.reserved_pages() + self.common.get_pages_used() |
| 129 | + } |
| 130 | + |
| 131 | + fn base(&self) -> &BasePlan<VM> { |
| 132 | + &self.common.base |
| 133 | + } |
| 134 | + |
| 135 | + fn common(&self) -> &CommonPlan<VM> { |
| 136 | + &self.common |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl<VM: VMBinding> PageProtect<VM> { |
| 141 | + pub fn new( |
| 142 | + vm_map: &'static VMMap, |
| 143 | + mmapper: &'static Mmapper, |
| 144 | + options: Arc<UnsafeOptionsWrapper>, |
| 145 | + ) -> Self { |
| 146 | + let mut heap = HeapMeta::new(HEAP_START, HEAP_END); |
| 147 | + let global_metadata_specs = SideMetadataContext::new_global_specs(&[]); |
| 148 | + |
| 149 | + PageProtect { |
| 150 | + space: LargeObjectSpace::new( |
| 151 | + "los", |
| 152 | + true, |
| 153 | + VMRequest::discontiguous(), |
| 154 | + global_metadata_specs.clone(), |
| 155 | + vm_map, |
| 156 | + mmapper, |
| 157 | + &mut heap, |
| 158 | + &CONSTRAINTS, |
| 159 | + true, |
| 160 | + ), |
| 161 | + common: CommonPlan::new( |
| 162 | + vm_map, |
| 163 | + mmapper, |
| 164 | + options, |
| 165 | + heap, |
| 166 | + &CONSTRAINTS, |
| 167 | + global_metadata_specs, |
| 168 | + ), |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments