5 gc_event::gc_event(gc_op op, factor_vm* parent)
9 code_blocks_scanned(0),
10 start_time(nano_count()),
12 data_heap_before = parent->data_room();
13 code_heap_before = parent->code->allocator->as_allocator_room();
14 start_time = nano_count();
17 void gc_event::reset_timer() { temp_time = nano_count(); }
19 void gc_event::ended_phase(gc_phase phase) {
20 times[phase] = (cell)(nano_count() - temp_time);
23 void gc_event::ended_gc(factor_vm* parent) {
24 data_heap_after = parent->data_room();
25 code_heap_after = parent->code->allocator->as_allocator_room();
26 total_time = (cell)(nano_count() - start_time);
29 gc_state::gc_state(gc_op op, factor_vm* parent) : op(op) {
30 if (parent->gc_events) {
31 event = new gc_event(op, parent);
32 start_time = nano_count();
37 gc_state::~gc_state() {
44 void factor_vm::start_gc_again() {
45 if (current_gc->op == COLLECT_NURSERY_OP) {
46 // Nursery collection can fail if aging does not have enough
47 // free space to fit all live objects from nursery.
48 current_gc->op = COLLECT_AGING_OP;
49 } else if (current_gc->op == COLLECT_AGING_OP) {
50 // Aging collection can fail if the aging semispace cannot fit
51 // all the live objects from the other aging semispace and the
53 current_gc->op = COLLECT_TO_TENURED_OP;
55 // Nothing else should fail mid-collection due to insufficient
56 // space in the target generation.
57 critical_error("in start_gc_again, bad GC op", current_gc->op);
61 void factor_vm::set_current_gc_op(gc_op op) {
64 current_gc->event->op = op;
67 void factor_vm::gc(gc_op op, cell requested_size) {
68 FACTOR_ASSERT(!gc_off);
69 FACTOR_ASSERT(!current_gc);
71 // Important invariant: tenured space must have enough contiguous free
72 // space to fit the entire contents of the aging space and nursery. This is
73 // because when doing a full collection, objects from younger generations
74 // are promoted before any unreachable tenured objects are freed.
75 FACTOR_ASSERT(!data->high_fragmentation_p());
77 current_gc = new gc_state(op, this);
79 ctx->callstack_seg->set_border_locked(false);
80 atomic::store(¤t_gc_p, true);
82 // Keep trying to GC higher and higher generations until we don't run
83 // out of space in the target generation.
87 current_gc->event->op = current_gc->op;
89 switch (current_gc->op) {
90 case COLLECT_NURSERY_OP:
93 case COLLECT_AGING_OP:
94 // We end up here if the above fails.
96 if (data->high_fragmentation_p()) {
97 // Change GC op so that if we fail again, we crash.
98 set_current_gc_op(COLLECT_FULL_OP);
102 case COLLECT_TO_TENURED_OP:
103 // We end up here if the above fails.
104 collect_to_tenured();
105 if (data->high_fragmentation_p()) {
106 // Change GC op so that if we fail again, we crash.
107 set_current_gc_op(COLLECT_FULL_OP);
111 case COLLECT_FULL_OP:
114 case COLLECT_COMPACT_OP:
117 case COLLECT_GROWING_DATA_HEAP_OP:
118 collect_growing_data_heap(requested_size);
121 critical_error("in gc, bad GC op", current_gc->op);
127 catch (const must_start_gc_again&) {
128 // We come back here if the target generation is full.
134 current_gc->event->ended_gc(this);
135 gc_events->push_back(*current_gc->event);
138 atomic::store(¤t_gc_p, false);
140 ctx->callstack_seg->set_border_locked(true);
144 // Check the invariant again, just in case.
145 FACTOR_ASSERT(!data->high_fragmentation_p());
148 void factor_vm::primitive_minor_gc() {
149 gc(COLLECT_NURSERY_OP, 0);
152 void factor_vm::primitive_full_gc() {
153 gc(COLLECT_FULL_OP, 0);
156 void factor_vm::primitive_compact_gc() {
157 gc(COLLECT_COMPACT_OP, 0);
160 void factor_vm::primitive_enable_gc_events() {
161 gc_events = new std::vector<gc_event>();
164 // Allocates memory (byte_array_from_value, result.add)
165 // XXX: Remember that growable_array has a data_root already
166 void factor_vm::primitive_disable_gc_events() {
168 growable_array result(this);
170 std::vector<gc_event>* gc_events = this->gc_events;
171 this->gc_events = NULL;
173 FACTOR_FOR_EACH(*gc_events) {
174 gc_event event = *iter;
175 byte_array* obj = byte_array_from_value(&event);
176 result.add(tag<byte_array>(obj));
180 ctx->push(result.elements.value());
182 delete this->gc_events;
184 ctx->push(false_object);