]> gitweb.factorcode.org Git - factor.git/blob - vm/gc.cpp
vm: We don't want ambiguity for which print triggered on a gc error. Differentiate...
[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::started_card_scan() { 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::started_code_scan() { temp_time = nano_count(); }
30
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);
34 }
35
36 void gc_event::started_data_sweep() { temp_time = nano_count(); }
37
38 void gc_event::ended_data_sweep() {
39   data_sweep_time = (cell)(nano_count() - temp_time);
40 }
41
42 void gc_event::started_code_sweep() { temp_time = nano_count(); }
43
44 void gc_event::ended_code_sweep() {
45   code_sweep_time = (cell)(nano_count() - temp_time);
46 }
47
48 void gc_event::started_compaction() { temp_time = nano_count(); }
49
50 void gc_event::ended_compaction() {
51   compaction_time = (cell)(nano_count() - temp_time);
52 }
53
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);
58 }
59
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();
64   } else
65     event = NULL;
66 }
67
68 gc_state::~gc_state() {
69   if (event) {
70     delete event;
71     event = NULL;
72   }
73 }
74
75 void factor_vm::end_gc() {
76   if (gc_events) {
77     current_gc->event->ended_gc(this);
78     gc_events->push_back(*current_gc->event);
79   }
80 }
81
82 void factor_vm::start_gc_again() {
83   end_gc();
84
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;
90       break;
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
94          nursery. */
95       current_gc->op = collect_to_tenured_op;
96       break;
97     default:
98       /* Nothing else should fail mid-collection due to insufficient
99          space in the target generation. */
100       critical_error("in start_gc_again, bad GC op", current_gc->op);
101       break;
102   }
103
104   if (gc_events)
105     current_gc->event = new gc_event(current_gc->op, this);
106 }
107
108 void factor_vm::set_current_gc_op(gc_op op) {
109   current_gc->op = op;
110   if (gc_events)
111     current_gc->event->op = op;
112 }
113
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);
117
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());
123
124   current_gc = new gc_state(op, this);
125   atomic::store(&current_gc_p, true);
126
127   /* Keep trying to GC higher and higher generations until we don't run
128      out of space in the target generation. */
129   for (;;) {
130     try {
131       if (gc_events)
132         current_gc->event->op = current_gc->op;
133
134       switch (current_gc->op) {
135         case collect_nursery_op:
136           collect_nursery();
137           break;
138         case collect_aging_op:
139           /* We end up here if the above fails. */
140           collect_aging();
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);
145           }
146           break;
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);
154           }
155           break;
156         case collect_full_op:
157           collect_full(trace_contexts_p);
158           break;
159         case collect_compact_op:
160           collect_compact(trace_contexts_p);
161           break;
162         case collect_growing_heap_op:
163           collect_growing_heap(requested_size, trace_contexts_p);
164           break;
165         default:
166           critical_error("in gc, bad GC op", current_gc->op);
167           break;
168       }
169
170       break;
171     }
172     catch (const must_start_gc_again&) {
173       /* We come back here if the target generation is full. */
174       start_gc_again();
175       continue;
176     }
177   }
178
179   end_gc();
180
181   atomic::store(&current_gc_p, false);
182   delete current_gc;
183   current_gc = NULL;
184
185   /* Check the invariant again, just in case. */
186   FACTOR_ASSERT(!data->high_fragmentation_p());
187 }
188
189 void factor_vm::primitive_minor_gc() {
190   gc(collect_nursery_op, 0, /* requested size */
191      true /* trace contexts? */);
192 }
193
194 void factor_vm::primitive_full_gc() {
195   gc(collect_full_op, 0, /* requested size */
196      true /* trace contexts? */);
197 }
198
199 void factor_vm::primitive_compact_gc() {
200   gc(collect_compact_op, 0, /* requested size */
201      true /* trace contexts? */);
202 }
203
204 /*
205  * It is up to the caller to fill in the object's fields in a meaningful
206  * fashion!
207  */
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();
214
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? */);
219     }
220   }
221
222   object* obj = data->tenured->allot(size);
223
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);
228
229   obj->initialize(type);
230   return obj;
231 }
232
233 void factor_vm::primitive_enable_gc_events() {
234   gc_events = new std::vector<gc_event>();
235 }
236
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() {
240   if (gc_events) {
241     growable_array result(this);
242
243     std::vector<gc_event>* gc_events = this->gc_events;
244     this->gc_events = NULL;
245
246     std::vector<gc_event>::const_iterator iter = gc_events->begin();
247     std::vector<gc_event>::const_iterator end = gc_events->end();
248
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));
253     }
254
255     result.trim();
256     ctx->push(result.elements.value());
257
258     delete this->gc_events;
259   } else
260     ctx->push(false_object);
261 }
262
263 }