-
Notifications
You must be signed in to change notification settings - Fork 68
/
global.rs
249 lines (214 loc) · 7.97 KB
/
global.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use super::gc_work::GenCopyGCWorkContext;
use super::gc_work::GenCopyNurseryGCWorkContext;
use super::mutator::ALLOCATOR_MAPPING;
use crate::plan::generational::global::Gen;
use crate::plan::global::BasePlan;
use crate::plan::global::CommonPlan;
use crate::plan::global::CreateGeneralPlanArgs;
use crate::plan::global::CreateSpecificPlanArgs;
use crate::plan::global::GcStatus;
use crate::plan::AllocationSemantics;
use crate::plan::Plan;
use crate::plan::PlanConstraints;
use crate::policy::copyspace::CopySpace;
use crate::policy::space::Space;
use crate::scheduler::*;
use crate::util::alloc::allocators::AllocatorSelector;
use crate::util::copy::*;
use crate::util::heap::VMRequest;
use crate::util::metadata::side_metadata::SideMetadataSanity;
use crate::util::VMWorkerThread;
use crate::vm::*;
use enum_map::EnumMap;
use std::sync::atomic::{AtomicBool, Ordering};
use mmtk_macros::PlanTraceObject;
#[derive(PlanTraceObject)]
pub struct GenCopy<VM: VMBinding> {
#[fallback_trace]
pub gen: Gen<VM>,
pub hi: AtomicBool,
#[trace(CopySemantics::Mature)]
pub copyspace0: CopySpace<VM>,
#[trace(CopySemantics::Mature)]
pub copyspace1: CopySpace<VM>,
}
pub const GENCOPY_CONSTRAINTS: PlanConstraints = crate::plan::generational::GEN_CONSTRAINTS;
impl<VM: VMBinding> Plan for GenCopy<VM> {
type VM = VM;
fn constraints(&self) -> &'static PlanConstraints {
&GENCOPY_CONSTRAINTS
}
fn create_copy_config(&'static self) -> CopyConfig<Self::VM> {
use enum_map::enum_map;
CopyConfig {
copy_mapping: enum_map! {
CopySemantics::Mature => CopySelector::CopySpace(0),
CopySemantics::PromoteToMature => CopySelector::CopySpace(0),
_ => CopySelector::Unused,
},
space_mapping: vec![
// The tospace argument doesn't matter, we will rebind before a GC anyway.
(CopySelector::CopySpace(0), self.tospace()),
],
constraints: &GENCOPY_CONSTRAINTS,
}
}
fn collection_required(&self, space_full: bool, space: Option<&dyn Space<Self::VM>>) -> bool
where
Self: Sized,
{
self.gen.collection_required(self, space_full, space)
}
fn force_full_heap_collection(&self) {
self.gen.force_full_heap_collection()
}
fn last_collection_full_heap(&self) -> bool {
self.gen.last_collection_full_heap()
}
fn get_spaces(&self) -> Vec<&dyn Space<Self::VM>> {
let mut ret = self.gen.get_spaces();
ret.push(&self.copyspace0);
ret.push(&self.copyspace1);
ret
}
fn schedule_collection(&'static self, scheduler: &GCWorkScheduler<VM>) {
let is_full_heap = self.requires_full_heap_collection();
self.base().set_collection_kind::<Self>(self);
self.base().set_gc_status(GcStatus::GcPrepare);
if is_full_heap {
scheduler.schedule_common_work::<GenCopyGCWorkContext<VM>>(self);
} else {
scheduler.schedule_common_work::<GenCopyNurseryGCWorkContext<VM>>(self);
}
}
fn get_allocator_mapping(&self) -> &'static EnumMap<AllocationSemantics, AllocatorSelector> {
&*ALLOCATOR_MAPPING
}
fn prepare(&mut self, tls: VMWorkerThread) {
let full_heap = !self.is_current_gc_nursery();
self.gen.prepare(tls);
if full_heap {
self.hi
.store(!self.hi.load(Ordering::SeqCst), Ordering::SeqCst); // flip the semi-spaces
}
let hi = self.hi.load(Ordering::SeqCst);
self.copyspace0.prepare(hi);
self.copyspace1.prepare(!hi);
self.fromspace_mut()
.set_copy_for_sft_trace(Some(CopySemantics::Mature));
self.tospace_mut().set_copy_for_sft_trace(None);
}
fn prepare_worker(&self, worker: &mut GCWorker<Self::VM>) {
unsafe { worker.get_copy_context_mut().copy[0].assume_init_mut() }.rebind(self.tospace());
}
fn release(&mut self, tls: VMWorkerThread) {
let full_heap = !self.is_current_gc_nursery();
self.gen.release(tls);
if full_heap {
self.fromspace().release();
}
// TODO: Refactor so that we set the next_gc_full_heap in gen.release(). Currently have to fight with Rust borrow checker
// NOTE: We have to take care that the `Gen::should_next_gc_be_full_heap()` function is
// called _after_ all spaces have been released (including ones in `gen`) as otherwise we
// may get incorrect results since the function uses values such as available pages that
// will change dependant on which spaces have been released
self.gen
.set_next_gc_full_heap(Gen::should_next_gc_be_full_heap(self));
}
fn get_collection_reserved_pages(&self) -> usize {
self.gen.get_collection_reserved_pages() + self.tospace().reserved_pages()
}
fn get_used_pages(&self) -> usize {
self.gen.get_used_pages() + self.tospace().reserved_pages()
}
/// Return the number of pages available for allocation. Assuming all future allocations goes to nursery.
fn get_available_pages(&self) -> usize {
// super.get_available_pages() / 2 to reserve pages for copying
(self
.get_total_pages()
.saturating_sub(self.get_reserved_pages()))
>> 1
}
fn get_mature_physical_pages_available(&self) -> usize {
self.tospace().available_physical_pages()
}
fn base(&self) -> &BasePlan<VM> {
&self.gen.common.base
}
fn common(&self) -> &CommonPlan<VM> {
&self.gen.common
}
fn generational(&self) -> &Gen<VM> {
&self.gen
}
fn is_current_gc_nursery(&self) -> bool {
!self.gen.gc_full_heap.load(Ordering::SeqCst)
}
}
impl<VM: VMBinding> GenCopy<VM> {
pub fn new(args: CreateGeneralPlanArgs<VM>) -> Self {
let mut plan_args = CreateSpecificPlanArgs {
global_args: args,
constraints: &GENCOPY_CONSTRAINTS,
global_side_metadata_specs:
crate::plan::generational::new_generational_global_metadata_specs::<VM>(),
};
let copyspace0 = CopySpace::new(
plan_args.get_space_args("copyspace0", true, VMRequest::discontiguous()),
false,
);
let copyspace1 = CopySpace::new(
plan_args.get_space_args("copyspace1", true, VMRequest::discontiguous()),
true,
);
let res = GenCopy {
gen: Gen::new(plan_args),
hi: AtomicBool::new(false),
copyspace0,
copyspace1,
};
// Use SideMetadataSanity to check if each spec is valid. This is also needed for check
// side metadata in extreme_assertions.
{
let mut side_metadata_sanity_checker = SideMetadataSanity::new();
res.gen
.verify_side_metadata_sanity(&mut side_metadata_sanity_checker);
res.copyspace0
.verify_side_metadata_sanity(&mut side_metadata_sanity_checker);
res.copyspace1
.verify_side_metadata_sanity(&mut side_metadata_sanity_checker);
}
res
}
fn requires_full_heap_collection(&self) -> bool {
self.gen.requires_full_heap_collection(self)
}
pub fn tospace(&self) -> &CopySpace<VM> {
if self.hi.load(Ordering::SeqCst) {
&self.copyspace1
} else {
&self.copyspace0
}
}
pub fn tospace_mut(&mut self) -> &mut CopySpace<VM> {
if self.hi.load(Ordering::SeqCst) {
&mut self.copyspace1
} else {
&mut self.copyspace0
}
}
pub fn fromspace(&self) -> &CopySpace<VM> {
if self.hi.load(Ordering::SeqCst) {
&self.copyspace0
} else {
&self.copyspace1
}
}
pub fn fromspace_mut(&mut self) -> &mut CopySpace<VM> {
if self.hi.load(Ordering::SeqCst) {
&mut self.copyspace0
} else {
&mut self.copyspace1
}
}
}