Skip to content

Commit 86a94ca

Browse files
authored
Allow binding to implement GC trigger (#1083)
This PR allows bindings to create a delegated GC trigger. * Make `GCTriggerPolicy` public. Add a new type `SpaceStats` which simply wraps `Space`. * Add `Collection::create_gc_trigger`. This will be called when the `gc_trigger` option is set to `Delegated`.
1 parent 6d0760c commit 86a94ca

File tree

17 files changed

+90
-25
lines changed

17 files changed

+90
-25
lines changed

docs/userguide/src/tutorial/code/mygc_semispace/global.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::scheduler::*; // Modify
1313
use crate::util::alloc::allocators::AllocatorSelector;
1414
use crate::util::copy::*;
1515
use crate::util::heap::VMRequest;
16+
use crate::util::heap::gc_trigger::SpaceStats;
1617
use crate::util::metadata::side_metadata::SideMetadataContext;
1718
use crate::util::opaque_pointer::*;
1819
use crate::vm::VMBinding;
@@ -78,7 +79,7 @@ impl<VM: VMBinding> Plan for MyGC<VM> {
7879
// ANCHOR_END: schedule_collection
7980

8081
// ANCHOR: collection_required()
81-
fn collection_required(&self, space_full: bool, _space: Option<&dyn Space<Self::VM>>) -> bool {
82+
fn collection_required(&self, space_full: bool, _space: Option<SpaceStats<Self::VM>>) -> bool {
8283
self.base().collection_required(self, space_full)
8384
}
8485
// ANCHOR_END: collection_required()

src/mmtk.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ impl<VM: VMBinding> MMTK<VM> {
299299
self.state.is_emergency_collection()
300300
}
301301

302+
/// Return true if the current GC is trigger manually by the user/binding.
303+
pub fn is_user_triggered_collection(&self) -> bool {
304+
self.state.is_user_triggered_collection()
305+
}
306+
302307
/// The application code has requested a collection. This is just a GC hint, and
303308
/// we may ignore it.
304309
///

src/plan/generational/copying/global.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::policy::space::Space;
1616
use crate::scheduler::*;
1717
use crate::util::alloc::allocators::AllocatorSelector;
1818
use crate::util::copy::*;
19+
use crate::util::heap::gc_trigger::SpaceStats;
1920
use crate::util::heap::VMRequest;
2021
use crate::util::Address;
2122
use crate::util::ObjectReference;
@@ -64,7 +65,7 @@ impl<VM: VMBinding> Plan for GenCopy<VM> {
6465
}
6566
}
6667

67-
fn collection_required(&self, space_full: bool, space: Option<&dyn Space<Self::VM>>) -> bool
68+
fn collection_required(&self, space_full: bool, space: Option<SpaceStats<Self::VM>>) -> bool
6869
where
6970
Self: Sized,
7071
{

src/plan/generational/global.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::policy::copyspace::CopySpace;
66
use crate::policy::space::Space;
77
use crate::scheduler::*;
88
use crate::util::copy::CopySemantics;
9+
use crate::util::heap::gc_trigger::SpaceStats;
910
use crate::util::heap::VMRequest;
1011
use crate::util::statistics::counter::EventCounter;
1112
use crate::util::Address;
@@ -98,7 +99,7 @@ impl<VM: VMBinding> CommonGenPlan<VM> {
9899
&self,
99100
plan: &P,
100101
space_full: bool,
101-
space: Option<&dyn Space<VM>>,
102+
space: Option<SpaceStats<VM>>,
102103
) -> bool {
103104
let cur_nursery = self.nursery.reserved_pages();
104105
let max_nursery = self.common.base.options.get_max_nursery_pages();
@@ -120,7 +121,7 @@ impl<VM: VMBinding> CommonGenPlan<VM> {
120121
// - if space is none, it is not. Return false immediately.
121122
// - if space is some, we further check its descriptor.
122123
let is_triggered_by_nursery = space.map_or(false, |s| {
123-
s.common().descriptor == self.nursery.common().descriptor
124+
s.0.common().descriptor == self.nursery.common().descriptor
124125
});
125126
// If space is full and the GC is not triggered by nursery, next GC will be full heap GC.
126127
if space_full && !is_triggered_by_nursery {

src/plan/generational/immix/global.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::scheduler::GCWorkScheduler;
1717
use crate::scheduler::GCWorker;
1818
use crate::util::alloc::allocators::AllocatorSelector;
1919
use crate::util::copy::*;
20+
use crate::util::heap::gc_trigger::SpaceStats;
2021
use crate::util::heap::VMRequest;
2122
use crate::util::Address;
2223
use crate::util::ObjectReference;
@@ -90,7 +91,7 @@ impl<VM: VMBinding> Plan for GenImmix<VM> {
9091
)
9192
}
9293

93-
fn collection_required(&self, space_full: bool, space: Option<&dyn Space<Self::VM>>) -> bool
94+
fn collection_required(&self, space_full: bool, space: Option<SpaceStats<Self::VM>>) -> bool
9495
where
9596
Self: Sized,
9697
{

src/plan/global.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::scheduler::*;
1414
use crate::util::alloc::allocators::AllocatorSelector;
1515
use crate::util::copy::{CopyConfig, GCWorkerCopyContext};
1616
use crate::util::heap::gc_trigger::GCTrigger;
17+
use crate::util::heap::gc_trigger::SpaceStats;
1718
use crate::util::heap::layout::Mmapper;
1819
use crate::util::heap::layout::VMMap;
1920
use crate::util::heap::HeapMeta;
@@ -213,7 +214,7 @@ pub trait Plan: 'static + HasSpaces + Sync + Downcast {
213214
/// # Arguments
214215
/// * `space_full`: the allocation to a specific space failed, must recover pages within 'space'.
215216
/// * `space`: an option to indicate if there is a space that has failed in an allocation.
216-
fn collection_required(&self, space_full: bool, space: Option<&dyn Space<Self::VM>>) -> bool;
217+
fn collection_required(&self, space_full: bool, space: Option<SpaceStats<Self::VM>>) -> bool;
217218

218219
// Note: The following methods are about page accounting. The default implementation should
219220
// work fine for non-copying plans. For copying plans, the plan should override any of these methods

src/plan/immix/global.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::policy::space::Space;
1313
use crate::scheduler::*;
1414
use crate::util::alloc::allocators::AllocatorSelector;
1515
use crate::util::copy::*;
16+
use crate::util::heap::gc_trigger::SpaceStats;
1617
use crate::util::heap::VMRequest;
1718
use crate::util::metadata::side_metadata::SideMetadataContext;
1819
use crate::vm::VMBinding;
@@ -45,7 +46,7 @@ pub const IMMIX_CONSTRAINTS: PlanConstraints = PlanConstraints {
4546
};
4647

4748
impl<VM: VMBinding> Plan for Immix<VM> {
48-
fn collection_required(&self, space_full: bool, _space: Option<&dyn Space<Self::VM>>) -> bool {
49+
fn collection_required(&self, space_full: bool, _space: Option<SpaceStats<Self::VM>>) -> bool {
4950
self.base().collection_required(self, space_full)
5051
}
5152

src/plan/markcompact/global.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::scheduler::gc_work::*;
1515
use crate::scheduler::*;
1616
use crate::util::alloc::allocators::AllocatorSelector;
1717
use crate::util::copy::CopySemantics;
18+
use crate::util::heap::gc_trigger::SpaceStats;
1819
use crate::util::heap::VMRequest;
1920
use crate::util::metadata::side_metadata::SideMetadataContext;
2021
#[cfg(not(feature = "vo_bit"))]
@@ -159,7 +160,7 @@ impl<VM: VMBinding> Plan for MarkCompact<VM> {
159160
.add(crate::util::sanity::sanity_checker::ScheduleSanityGC::<Self>::new(self));
160161
}
161162

162-
fn collection_required(&self, space_full: bool, _space: Option<&dyn Space<Self::VM>>) -> bool {
163+
fn collection_required(&self, space_full: bool, _space: Option<SpaceStats<Self::VM>>) -> bool {
163164
self.base().collection_required(self, space_full)
164165
}
165166

src/plan/marksweep/global.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::plan::PlanConstraints;
1010
use crate::policy::space::Space;
1111
use crate::scheduler::GCWorkScheduler;
1212
use crate::util::alloc::allocators::AllocatorSelector;
13+
use crate::util::heap::gc_trigger::SpaceStats;
1314
use crate::util::heap::VMRequest;
1415
use crate::util::metadata::side_metadata::SideMetadataContext;
1516
use crate::util::VMWorkerThread;
@@ -64,7 +65,7 @@ impl<VM: VMBinding> Plan for MarkSweep<VM> {
6465
self.common.release(tls, true);
6566
}
6667

67-
fn collection_required(&self, space_full: bool, _space: Option<&dyn Space<Self::VM>>) -> bool {
68+
fn collection_required(&self, space_full: bool, _space: Option<SpaceStats<Self::VM>>) -> bool {
6869
self.base().collection_required(self, space_full)
6970
}
7071

src/plan/nogc/global.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::policy::immortalspace::ImmortalSpace;
99
use crate::policy::space::Space;
1010
use crate::scheduler::GCWorkScheduler;
1111
use crate::util::alloc::allocators::AllocatorSelector;
12+
use crate::util::heap::gc_trigger::SpaceStats;
1213
#[allow(unused_imports)]
1314
use crate::util::heap::VMRequest;
1415
use crate::util::metadata::side_metadata::SideMetadataContext;
@@ -45,7 +46,7 @@ impl<VM: VMBinding> Plan for NoGC<VM> {
4546
&NOGC_CONSTRAINTS
4647
}
4748

48-
fn collection_required(&self, space_full: bool, _space: Option<&dyn Space<Self::VM>>) -> bool {
49+
fn collection_required(&self, space_full: bool, _space: Option<SpaceStats<Self::VM>>) -> bool {
4950
self.base().collection_required(self, space_full)
5051
}
5152

0 commit comments

Comments
 (0)