5 gc_event::gc_event(gc_op op, factor_vm* parent)
9 code_blocks_scanned(0),
10 start_time(nano_count()),
16 data_heap_before = parent->data_room();
17 code_heap_before = parent->code->allocator->as_allocator_room();
18 start_time = nano_count();
21 void gc_event::started_card_scan() { temp_time = nano_count(); }
23 void gc_event::ended_card_scan(cell cards_scanned_, cell decks_scanned_) {
24 cards_scanned += cards_scanned_;
25 decks_scanned += decks_scanned_;
26 card_scan_time = (cell)(nano_count() - temp_time);
29 void gc_event::started_code_scan() { temp_time = nano_count(); }
31 void gc_event::ended_code_scan(cell code_blocks_scanned_) {
32 code_blocks_scanned += code_blocks_scanned_;
33 code_scan_time = (cell)(nano_count() - temp_time);
36 void gc_event::started_data_sweep() { temp_time = nano_count(); }
38 void gc_event::ended_data_sweep() {
39 data_sweep_time = (cell)(nano_count() - temp_time);
42 void gc_event::started_code_sweep() { temp_time = nano_count(); }
44 void gc_event::ended_code_sweep() {
45 code_sweep_time = (cell)(nano_count() - temp_time);
48 void gc_event::started_compaction() { temp_time = nano_count(); }
50 void gc_event::ended_compaction() {
51 compaction_time = (cell)(nano_count() - temp_time);
54 void gc_event::ended_gc(factor_vm* parent) {
55 data_heap_after = parent->data_room();
56 code_heap_after = parent->code->allocator->as_allocator_room();
57 total_time = (cell)(nano_count() - start_time);
60 gc_state::gc_state(gc_op op, factor_vm* parent) : op(op) {
61 if (parent->gc_events) {
62 event = new gc_event(op, parent);
63 start_time = nano_count();
68 gc_state::~gc_state() {
75 void factor_vm::end_gc() {
77 current_gc->event->ended_gc(this);
78 gc_events->push_back(*current_gc->event);
82 void factor_vm::start_gc_again() {
85 switch (current_gc->op) {
86 case collect_nursery_op:
87 /* Nursery collection can fail if aging does not have enough
88 free space to fit all live objects from nursery. */
89 current_gc->op = collect_aging_op;
91 case collect_aging_op:
92 /* Aging collection can fail if the aging semispace cannot fit
93 all the live objects from the other aging semispace and the
95 current_gc->op = collect_to_tenured_op;
98 /* Nothing else should fail mid-collection due to insufficient
99 space in the target generation. */
100 critical_error("Bad GC op", current_gc->op);
105 current_gc->event = new gc_event(current_gc->op, this);
108 void factor_vm::set_current_gc_op(gc_op op) {
111 current_gc->event->op = op;
114 void factor_vm::gc(gc_op op, cell requested_size, bool trace_contexts_p) {
115 FACTOR_ASSERT(!gc_off);
116 FACTOR_ASSERT(!current_gc);
118 /* Important invariant: tenured space must have enough contiguous free
119 space to fit the entire contents of the aging space and nursery. This is
120 because when doing a full collection, objects from younger generations
121 are promoted before any unreachable tenured objects are freed. */
122 FACTOR_ASSERT(!data->high_fragmentation_p());
124 current_gc = new gc_state(op, this);
125 atomic::store(¤t_gc_p, true);
127 /* Keep trying to GC higher and higher generations until we don't run
128 out of space in the target generation. */
132 current_gc->event->op = current_gc->op;
134 switch (current_gc->op) {
135 case collect_nursery_op:
138 case collect_aging_op:
139 /* We end up here if the above fails. */
141 if (data->high_fragmentation_p()) {
142 /* Change GC op so that if we fail again, we crash. */
143 set_current_gc_op(collect_full_op);
144 collect_full(trace_contexts_p);
147 case collect_to_tenured_op:
148 /* We end up here if the above fails. */
149 collect_to_tenured();
150 if (data->high_fragmentation_p()) {
151 /* Change GC op so that if we fail again, we crash. */
152 set_current_gc_op(collect_full_op);
153 collect_full(trace_contexts_p);
156 case collect_full_op:
157 collect_full(trace_contexts_p);
159 case collect_compact_op:
160 collect_compact(trace_contexts_p);
162 case collect_growing_heap_op:
163 collect_growing_heap(requested_size, trace_contexts_p);
166 critical_error("Bad GC op", current_gc->op);
172 catch (const must_start_gc_again&) {
173 /* We come back here if the target generation is full. */
181 atomic::store(¤t_gc_p, false);
185 /* Check the invariant again, just in case. */
186 FACTOR_ASSERT(!data->high_fragmentation_p());
189 void factor_vm::primitive_minor_gc() {
190 gc(collect_nursery_op, 0, /* requested size */
191 true /* trace contexts? */);
194 void factor_vm::primitive_full_gc() {
195 gc(collect_full_op, 0, /* requested size */
196 true /* trace contexts? */);
199 void factor_vm::primitive_compact_gc() {
200 gc(collect_compact_op, 0, /* requested size */
201 true /* trace contexts? */);
205 * It is up to the caller to fill in the object's fields in a meaningful
208 /* Allocates memory */
209 object* factor_vm::allot_large_object(cell type, cell size) {
210 /* If tenured space does not have enough room, collect and compact */
211 cell requested_size = size + data->high_water_mark();
212 if (!data->tenured->can_allot_p(requested_size)) {
213 primitive_compact_gc();
215 /* If it still won't fit, grow the heap */
216 if (!data->tenured->can_allot_p(requested_size)) {
217 gc(collect_growing_heap_op, size, /* requested size */
218 true /* trace contexts? */);
222 object* obj = data->tenured->allot(size);
224 /* Allows initialization code to store old->new pointers
225 without hitting the write barrier in the common case of
226 a nursery allocation */
227 write_barrier(obj, size);
229 obj->initialize(type);
233 void factor_vm::primitive_enable_gc_events() {
234 gc_events = new std::vector<gc_event>();
237 /* Allocates memory (byte_array_from_value, result.add) */
238 /* XXX: Remember that growable_array has a data_root already */
239 void factor_vm::primitive_disable_gc_events() {
241 growable_array result(this);
243 std::vector<gc_event>* gc_events = this->gc_events;
244 this->gc_events = NULL;
246 std::vector<gc_event>::const_iterator iter = gc_events->begin();
247 std::vector<gc_event>::const_iterator end = gc_events->end();
249 for (; iter != end; iter++) {
250 gc_event event = *iter;
251 byte_array* obj = byte_array_from_value(&event);
252 result.add(tag<byte_array>(obj));
256 ctx->push(result.elements.value());
258 delete this->gc_events;
260 ctx->push(false_object);