6 gc_event::gc_event(gc_op op_, factor_vm *parent) :
10 code_blocks_scanned(0),
11 start_time(nano_count()),
18 data_heap_before = parent->data_room();
19 code_heap_before = parent->code_room();
20 start_time = nano_count();
23 void gc_event::started_card_scan()
25 temp_time = nano_count();
28 void gc_event::ended_card_scan(cell cards_scanned_, cell decks_scanned_)
30 cards_scanned += cards_scanned_;
31 decks_scanned += decks_scanned_;
32 card_scan_time = (cell)(nano_count() - temp_time);
35 void gc_event::started_code_scan()
37 temp_time = nano_count();
40 void gc_event::ended_code_scan(cell code_blocks_scanned_)
42 code_blocks_scanned += code_blocks_scanned_;
43 code_scan_time = (cell)(nano_count() - temp_time);
46 void gc_event::started_data_sweep()
48 temp_time = nano_count();
51 void gc_event::ended_data_sweep()
53 data_sweep_time = (cell)(nano_count() - temp_time);
56 void gc_event::started_code_sweep()
58 temp_time = nano_count();
61 void gc_event::ended_code_sweep()
63 code_sweep_time = (cell)(nano_count() - temp_time);
66 void gc_event::started_compaction()
68 temp_time = nano_count();
71 void gc_event::ended_compaction()
73 compaction_time = (cell)(nano_count() - temp_time);
76 void gc_event::ended_gc(factor_vm *parent)
78 data_heap_after = parent->data_room();
79 code_heap_after = parent->code_room();
80 total_time = (cell)(nano_count() - start_time);
83 gc_state::gc_state(gc_op op_, factor_vm *parent) : op(op_)
87 event = new gc_event(op,parent);
88 start_time = nano_count();
103 void factor_vm::end_gc()
107 current_gc->event->ended_gc(this);
108 gc_events->push_back(*current_gc->event);
112 void factor_vm::start_gc_again()
116 switch(current_gc->op)
118 case collect_nursery_op:
119 /* Nursery collection can fail if aging does not have enough
120 free space to fit all live objects from nursery. */
121 current_gc->op = collect_aging_op;
123 case collect_aging_op:
124 /* Aging collection can fail if the aging semispace cannot fit
125 all the live objects from the other aging semispace and the
127 current_gc->op = collect_to_tenured_op;
130 /* Nothing else should fail mid-collection due to insufficient
131 space in the target generation. */
132 critical_error("Bad GC op",current_gc->op);
137 current_gc->event = new gc_event(current_gc->op,this);
140 void factor_vm::set_current_gc_op(gc_op op)
143 if(gc_events) current_gc->event->op = op;
146 void factor_vm::gc(gc_op op, cell requested_bytes, bool trace_contexts_p)
151 /* Important invariant: tenured space must have enough contiguous free
152 space to fit the entire contents of the aging space and nursery. This is
153 because when doing a full collection, objects from younger generations
154 are promoted before any unreachable tenured objects are freed. */
155 assert(!data->high_fragmentation_p());
157 current_gc = new gc_state(op,this);
159 /* Keep trying to GC higher and higher generations until we don't run
160 out of space in the target generation. */
165 if(gc_events) current_gc->event->op = current_gc->op;
167 switch(current_gc->op)
169 case collect_nursery_op:
172 case collect_aging_op:
173 /* We end up here if the above fails. */
175 if(data->high_fragmentation_p())
177 /* Change GC op so that if we fail again,
179 set_current_gc_op(collect_full_op);
180 collect_full(trace_contexts_p);
183 case collect_to_tenured_op:
184 /* We end up here if the above fails. */
185 collect_to_tenured();
186 if(data->high_fragmentation_p())
188 /* Change GC op so that if we fail again,
190 set_current_gc_op(collect_full_op);
191 collect_full(trace_contexts_p);
194 case collect_full_op:
195 collect_full(trace_contexts_p);
197 case collect_compact_op:
198 collect_compact(trace_contexts_p);
200 case collect_growing_heap_op:
201 collect_growing_heap(requested_bytes,trace_contexts_p);
204 critical_error("Bad GC op",current_gc->op);
210 catch(const must_start_gc_again &)
212 /* We come back here if the target generation is full. */
223 /* Check the invariant again, just in case. */
224 assert(!data->high_fragmentation_p());
227 /* primitive_minor_gc() is invoked by inline GC checks, and it needs to fill in
228 uninitialized stack locations before actually calling the GC. See the comment
229 in compiler.cfg.stacks.uninitialized for details. */
231 struct call_frame_scrubber {
235 explicit call_frame_scrubber(factor_vm *parent_, context *ctx_) :
236 parent(parent_), ctx(ctx_) {}
238 void operator()(stack_frame *frame)
240 cell return_address = parent->frame_offset(frame);
241 if(return_address == (cell)-1)
244 code_block *compiled = parent->frame_code(frame);
245 gc_info *info = compiled->block_gc_info();
247 assert(return_address < compiled->size());
248 cell index = info->return_address_index(return_address);
249 if(index != (cell)-1)
250 ctx->scrub_stacks(info,index);
254 void factor_vm::scrub_context(context *ctx)
256 call_frame_scrubber scrubber(this,ctx);
257 iterate_callstack(ctx,scrubber);
260 void factor_vm::scrub_contexts()
262 std::set<context *>::const_iterator begin = active_contexts.begin();
263 std::set<context *>::const_iterator end = active_contexts.end();
266 scrub_context(*begin);
271 void factor_vm::primitive_minor_gc()
275 gc(collect_nursery_op,
276 0, /* requested size */
277 true /* trace contexts? */);
280 void factor_vm::primitive_full_gc()
283 0, /* requested size */
284 true /* trace contexts? */);
287 void factor_vm::primitive_compact_gc()
289 gc(collect_compact_op,
290 0, /* requested size */
291 true /* trace contexts? */);
295 * It is up to the caller to fill in the object's fields in a meaningful
298 object *factor_vm::allot_large_object(cell type, cell size)
300 /* If tenured space does not have enough room, collect and compact */
301 if(!data->tenured->can_allot_p(size + data->high_water_mark()))
303 primitive_compact_gc();
305 /* If it still won't fit, grow the heap */
306 if(!data->tenured->can_allot_p(size))
308 gc(collect_growing_heap_op,
309 size, /* requested size */
310 true /* trace contexts? */);
314 object *obj = data->tenured->allot(size);
316 /* Allows initialization code to store old->new pointers
317 without hitting the write barrier in the common case of
318 a nursery allocation */
319 write_barrier(obj,size);
321 obj->initialize(type);
325 void factor_vm::primitive_enable_gc_events()
327 gc_events = new std::vector<gc_event>();
330 void factor_vm::primitive_disable_gc_events()
334 growable_array result(this);
336 std::vector<gc_event> *gc_events = this->gc_events;
337 this->gc_events = NULL;
339 std::vector<gc_event>::const_iterator iter = gc_events->begin();
340 std::vector<gc_event>::const_iterator end = gc_events->end();
342 for(; iter != end; iter++)
344 gc_event event = *iter;
345 byte_array *obj = byte_array_from_value(&event);
346 result.add(tag<byte_array>(obj));
350 ctx->push(result.elements.value());
352 delete this->gc_events;
355 ctx->push(false_object);