]> gitweb.factorcode.org Git - factor.git/blob - vm/gc.cpp
VM: all the started_<blah>() replaced with reset_timer()
[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::end_gc() {
68   if (gc_events) {
69     current_gc->event->ended_gc(this);
70     gc_events->push_back(*current_gc->event);
71   }
72 }
73
74 void factor_vm::start_gc_again() {
75   end_gc();
76
77   switch (current_gc->op) {
78     case collect_nursery_op:
79       /* Nursery collection can fail if aging does not have enough
80          free space to fit all live objects from nursery. */
81       current_gc->op = collect_aging_op;
82       break;
83     case collect_aging_op:
84       /* Aging collection can fail if the aging semispace cannot fit
85          all the live objects from the other aging semispace and the
86          nursery. */
87       current_gc->op = collect_to_tenured_op;
88       break;
89     default:
90       /* Nothing else should fail mid-collection due to insufficient
91          space in the target generation. */
92       critical_error("in start_gc_again, bad GC op", current_gc->op);
93       break;
94   }
95
96   if (gc_events)
97     current_gc->event = new gc_event(current_gc->op, this);
98 }
99
100 void factor_vm::set_current_gc_op(gc_op op) {
101   current_gc->op = op;
102   if (gc_events)
103     current_gc->event->op = op;
104 }
105
106 void factor_vm::gc(gc_op op, cell requested_size) {
107   FACTOR_ASSERT(!gc_off);
108   FACTOR_ASSERT(!current_gc);
109
110   /* Important invariant: tenured space must have enough contiguous free
111      space to fit the entire contents of the aging space and nursery. This is
112      because when doing a full collection, objects from younger generations
113      are promoted before any unreachable tenured objects are freed. */
114   FACTOR_ASSERT(!data->high_fragmentation_p());
115
116   current_gc = new gc_state(op, this);
117   if (ctx)
118     ctx->callstack_seg->set_border_locked(false);
119   atomic::store(&current_gc_p, true);
120
121   /* Keep trying to GC higher and higher generations until we don't run
122      out of space in the target generation. */
123   for (;;) {
124     try {
125       if (gc_events)
126         current_gc->event->op = current_gc->op;
127
128       switch (current_gc->op) {
129         case collect_nursery_op:
130           collect_nursery();
131           break;
132         case collect_aging_op:
133           /* We end up here if the above fails. */
134           collect_aging();
135           if (data->high_fragmentation_p()) {
136             /* Change GC op so that if we fail again, we crash. */
137             set_current_gc_op(collect_full_op);
138             collect_full();
139           }
140           break;
141         case collect_to_tenured_op:
142           /* We end up here if the above fails. */
143           collect_to_tenured();
144           if (data->high_fragmentation_p()) {
145             /* Change GC op so that if we fail again, we crash. */
146             set_current_gc_op(collect_full_op);
147             collect_full();
148           }
149           break;
150         case collect_full_op:
151           collect_full();
152           break;
153         case collect_compact_op:
154           collect_compact();
155           break;
156         case collect_growing_heap_op:
157           collect_growing_heap(requested_size);
158           break;
159         default:
160           critical_error("in gc, bad GC op", current_gc->op);
161           break;
162       }
163
164       break;
165     }
166     catch (const must_start_gc_again&) {
167       /* We come back here if the target generation is full. */
168       start_gc_again();
169       continue;
170     }
171   }
172
173   end_gc();
174
175   atomic::store(&current_gc_p, false);
176   if (ctx)
177     ctx->callstack_seg->set_border_locked(true);
178   delete current_gc;
179   current_gc = NULL;
180
181   /* Check the invariant again, just in case. */
182   FACTOR_ASSERT(!data->high_fragmentation_p());
183 }
184
185 void factor_vm::primitive_minor_gc() {
186   gc(collect_nursery_op, 0);
187 }
188
189 void factor_vm::primitive_full_gc() {
190   gc(collect_full_op, 0);
191 }
192
193 void factor_vm::primitive_compact_gc() {
194   gc(collect_compact_op, 0);
195 }
196
197 /*
198  * It is up to the caller to fill in the object's fields in a meaningful
199  * fashion!
200  */
201 /* Allocates memory */
202 object* factor_vm::allot_large_object(cell type, cell size) {
203   /* If tenured space does not have enough room, collect and compact */
204   cell requested_size = size + data->high_water_mark();
205   if (!data->tenured->can_allot_p(requested_size)) {
206     primitive_compact_gc();
207
208     /* If it still won't fit, grow the heap */
209     if (!data->tenured->can_allot_p(requested_size)) {
210       gc(collect_growing_heap_op, size);
211     }
212   }
213
214   object* obj = data->tenured->allot(size);
215
216   /* Allows initialization code to store old->new pointers
217      without hitting the write barrier in the common case of
218      a nursery allocation */
219   write_barrier(obj, size);
220
221   obj->initialize(type);
222   return obj;
223 }
224
225 void factor_vm::primitive_enable_gc_events() {
226   gc_events = new std::vector<gc_event>();
227 }
228
229 /* Allocates memory (byte_array_from_value, result.add) */
230 /* XXX: Remember that growable_array has a data_root already */
231 void factor_vm::primitive_disable_gc_events() {
232   if (gc_events) {
233     growable_array result(this);
234
235     std::vector<gc_event>* gc_events = this->gc_events;
236     this->gc_events = NULL;
237
238     FACTOR_FOR_EACH(*gc_events) {
239       gc_event event = *iter;
240       byte_array* obj = byte_array_from_value(&event);
241       result.add(tag<byte_array>(obj));
242     }
243
244     result.trim();
245     ctx->push(result.elements.value());
246
247     delete this->gc_events;
248   } else
249     ctx->push(false_object);
250 }
251
252 }