]> gitweb.factorcode.org Git - factor.git/blob - vm/gc.cpp
VM: rename of collect_growing_heap_op and collect_growing_heap()
[factor.git] / vm / gc.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 gc_event::gc_event(gc_op op, factor_vm* parent)
6     : op(op),
7       cards_scanned(0),
8       decks_scanned(0),
9       code_blocks_scanned(0),
10       start_time(nano_count()),
11       card_scan_time(0),
12       code_scan_time(0),
13       data_sweep_time(0),
14       code_sweep_time(0),
15       compaction_time(0) {
16   data_heap_before = parent->data_room();
17   code_heap_before = parent->code->allocator->as_allocator_room();
18   start_time = nano_count();
19 }
20
21 void gc_event::reset_timer() { temp_time = nano_count(); }
22
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);
27 }
28
29 void gc_event::ended_code_scan(cell code_blocks_scanned_) {
30   code_blocks_scanned += code_blocks_scanned_;
31   code_scan_time = (cell)(nano_count() - temp_time);
32 }
33
34 void gc_event::ended_data_sweep() {
35   data_sweep_time = (cell)(nano_count() - temp_time);
36 }
37
38 void gc_event::ended_code_sweep() {
39   code_sweep_time = (cell)(nano_count() - temp_time);
40 }
41
42 void gc_event::ended_compaction() {
43   compaction_time = (cell)(nano_count() - temp_time);
44 }
45
46 void gc_event::ended_gc(factor_vm* parent) {
47   data_heap_after = parent->data_room();
48   code_heap_after = parent->code->allocator->as_allocator_room();
49   total_time = (cell)(nano_count() - start_time);
50 }
51
52 gc_state::gc_state(gc_op op, factor_vm* parent) : op(op) {
53   if (parent->gc_events) {
54     event = new gc_event(op, parent);
55     start_time = nano_count();
56   } else
57     event = NULL;
58 }
59
60 gc_state::~gc_state() {
61   if (event) {
62     delete event;
63     event = NULL;
64   }
65 }
66
67 void factor_vm::start_gc_again() {
68   if (current_gc->op == collect_nursery_op) {
69     // Nursery collection can fail if aging does not have enough
70     // free space to fit all live objects from nursery.
71     current_gc->op = collect_aging_op;
72   } else if (current_gc->op == collect_aging_op) {
73     // Aging collection can fail if the aging semispace cannot fit
74     // all the live objects from the other aging semispace and the
75     // nursery.
76     current_gc->op = collect_to_tenured_op;
77   } else {
78     // Nothing else should fail mid-collection due to insufficient
79     // space in the target generation.
80     critical_error("in start_gc_again, bad GC op", current_gc->op);
81   }
82 }
83
84 void factor_vm::set_current_gc_op(gc_op op) {
85   current_gc->op = op;
86   if (gc_events)
87     current_gc->event->op = op;
88 }
89
90 void factor_vm::gc(gc_op op, cell requested_size) {
91   FACTOR_ASSERT(!gc_off);
92   FACTOR_ASSERT(!current_gc);
93
94   // Important invariant: tenured space must have enough contiguous free
95   // space to fit the entire contents of the aging space and nursery. This is
96   // because when doing a full collection, objects from younger generations
97   // are promoted before any unreachable tenured objects are freed.
98   FACTOR_ASSERT(!data->high_fragmentation_p());
99
100   current_gc = new gc_state(op, this);
101   if (ctx)
102     ctx->callstack_seg->set_border_locked(false);
103   atomic::store(&current_gc_p, true);
104
105   // Keep trying to GC higher and higher generations until we don't run
106   // out of space in the target generation.
107   for (;;) {
108     try {
109       if (gc_events)
110         current_gc->event->op = current_gc->op;
111
112       switch (current_gc->op) {
113         case collect_nursery_op:
114           collect_nursery();
115           break;
116         case collect_aging_op:
117           // We end up here if the above fails.
118           collect_aging();
119           if (data->high_fragmentation_p()) {
120             // Change GC op so that if we fail again, we crash.
121             set_current_gc_op(collect_full_op);
122             collect_full();
123           }
124           break;
125         case collect_to_tenured_op:
126           // We end up here if the above fails.
127           collect_to_tenured();
128           if (data->high_fragmentation_p()) {
129             // Change GC op so that if we fail again, we crash.
130             set_current_gc_op(collect_full_op);
131             collect_full();
132           }
133           break;
134         case collect_full_op:
135           collect_full();
136           break;
137         case collect_compact_op:
138           collect_compact();
139           break;
140         case collect_growing_data_heap_op:
141           collect_growing_data_heap(requested_size);
142           break;
143         default:
144           critical_error("in gc, bad GC op", current_gc->op);
145           break;
146       }
147
148       break;
149     }
150     catch (const must_start_gc_again&) {
151       // We come back here if the target generation is full.
152       start_gc_again();
153     }
154   }
155
156   if (gc_events) {
157     current_gc->event->ended_gc(this);
158     gc_events->push_back(*current_gc->event);
159   }
160
161   atomic::store(&current_gc_p, false);
162   if (ctx)
163     ctx->callstack_seg->set_border_locked(true);
164   delete current_gc;
165   current_gc = NULL;
166
167   // Check the invariant again, just in case.
168   FACTOR_ASSERT(!data->high_fragmentation_p());
169 }
170
171 void factor_vm::primitive_minor_gc() {
172   gc(collect_nursery_op, 0);
173 }
174
175 void factor_vm::primitive_full_gc() {
176   gc(collect_full_op, 0);
177 }
178
179 void factor_vm::primitive_compact_gc() {
180   gc(collect_compact_op, 0);
181 }
182
183 void factor_vm::primitive_enable_gc_events() {
184   gc_events = new std::vector<gc_event>();
185 }
186
187 // Allocates memory (byte_array_from_value, result.add)
188 // XXX: Remember that growable_array has a data_root already
189 void factor_vm::primitive_disable_gc_events() {
190   if (gc_events) {
191     growable_array result(this);
192
193     std::vector<gc_event>* gc_events = this->gc_events;
194     this->gc_events = NULL;
195
196     FACTOR_FOR_EACH(*gc_events) {
197       gc_event event = *iter;
198       byte_array* obj = byte_array_from_value(&event);
199       result.add(tag<byte_array>(obj));
200     }
201
202     result.trim();
203     ctx->push(result.elements.value());
204
205     delete this->gc_events;
206   } else
207     ctx->push(false_object);
208 }
209
210 }