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 current_gc->op = collect_aging_op;
121 case collect_aging_op:
122 current_gc->op = collect_to_tenured_op;
124 case collect_to_tenured_op:
125 current_gc->op = collect_full_op;
127 case collect_full_op:
128 case collect_compact_op:
129 current_gc->op = collect_growing_heap_op;
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)
148 /* Save and reset FPU state before, restore it after, so that
149 nano_count() doesn't bomb on Windows if inexact traps are enabled
151 cell fpu_state = get_fpu_state();
156 current_gc = new gc_state(op,this);
158 /* Keep trying to GC higher and higher generations until we don't run out
164 if(gc_events) current_gc->event->op = current_gc->op;
166 switch(current_gc->op)
168 case collect_nursery_op:
171 case collect_aging_op:
173 if(data->high_fragmentation_p())
175 set_current_gc_op(collect_full_op);
176 collect_full(trace_contexts_p);
179 case collect_to_tenured_op:
180 collect_to_tenured();
181 if(data->high_fragmentation_p())
183 set_current_gc_op(collect_full_op);
184 collect_full(trace_contexts_p);
187 case collect_full_op:
188 collect_full(trace_contexts_p);
190 case collect_compact_op:
191 collect_compact(trace_contexts_p);
193 case collect_growing_heap_op:
194 collect_growing_heap(requested_bytes,trace_contexts_p);
197 critical_error("Bad GC op",current_gc->op);
203 catch(const must_start_gc_again &)
205 /* We come back here if a generation is full */
216 set_fpu_state(fpu_state);
219 /* primitive_minor_gc() is invoked by inline GC checks, and it needs to fill in
220 uninitialized stack locations before actually calling the GC. See the comment
221 in compiler.cfg.stacks.uninitialized for details. */
223 struct call_frame_scrubber {
227 explicit call_frame_scrubber(factor_vm *parent_, context *ctx_) :
228 parent(parent_), ctx(ctx_) {}
230 void operator()(stack_frame *frame)
232 cell return_address = parent->frame_offset(frame);
233 if(return_address == (cell)-1)
236 code_block *compiled = parent->frame_code(frame);
237 gc_info *info = compiled->block_gc_info();
239 assert(return_address < compiled->size());
240 int index = info->return_address_index(return_address);
242 ctx->scrub_stacks(info,index);
246 void factor_vm::scrub_context(context *ctx)
248 call_frame_scrubber scrubber(this,ctx);
249 iterate_callstack(ctx,scrubber);
252 void factor_vm::scrub_contexts()
254 std::set<context *>::const_iterator begin = active_contexts.begin();
255 std::set<context *>::const_iterator end = active_contexts.end();
258 scrub_context(*begin);
263 void factor_vm::primitive_minor_gc()
267 gc(collect_nursery_op,
268 0, /* requested size */
269 true /* trace contexts? */);
272 void factor_vm::primitive_full_gc()
275 0, /* requested size */
276 true /* trace contexts? */);
279 void factor_vm::primitive_compact_gc()
281 gc(collect_compact_op,
282 0, /* requested size */
283 true /* trace contexts? */);
287 * It is up to the caller to fill in the object's fields in a meaningful
290 object *factor_vm::allot_large_object(cell type, cell size)
292 /* If tenured space does not have enough room, collect and compact */
293 if(!data->tenured->can_allot_p(size))
295 primitive_compact_gc();
297 /* If it still won't fit, grow the heap */
298 if(!data->tenured->can_allot_p(size))
300 gc(collect_growing_heap_op,
301 size, /* requested size */
302 true /* trace contexts? */);
306 object *obj = data->tenured->allot(size);
308 /* Allows initialization code to store old->new pointers
309 without hitting the write barrier in the common case of
310 a nursery allocation */
311 write_barrier(obj,size);
313 obj->initialize(type);
317 void factor_vm::primitive_enable_gc_events()
319 gc_events = new std::vector<gc_event>();
322 void factor_vm::primitive_disable_gc_events()
326 growable_array result(this);
328 std::vector<gc_event> *gc_events = this->gc_events;
329 this->gc_events = NULL;
331 std::vector<gc_event>::const_iterator iter = gc_events->begin();
332 std::vector<gc_event>::const_iterator end = gc_events->end();
334 for(; iter != end; iter++)
336 gc_event event = *iter;
337 byte_array *obj = byte_array_from_value(&event);
338 result.add(tag<byte_array>(obj));
342 ctx->push(result.elements.value());
344 delete this->gc_events;
347 ctx->push(false_object);