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_), start_time(nano_count())
85 event = new gc_event(op,parent);
94 void factor_vm::end_gc()
96 current_gc->event->ended_gc(this);
97 if(gc_events) gc_events->push_back(*current_gc->event);
98 delete current_gc->event;
99 current_gc->event = NULL;
102 void factor_vm::start_gc_again()
106 switch(current_gc->op)
108 case collect_nursery_op:
109 current_gc->op = collect_aging_op;
111 case collect_aging_op:
112 current_gc->op = collect_to_tenured_op;
114 case collect_to_tenured_op:
115 current_gc->op = collect_full_op;
117 case collect_full_op:
118 case collect_compact_op:
119 current_gc->op = collect_growing_heap_op;
122 critical_error("Bad GC op",current_gc->op);
126 current_gc->event = new gc_event(current_gc->op,this);
129 void factor_vm::gc(gc_op op, cell requested_bytes, bool trace_contexts_p)
134 current_gc = new gc_state(op,this);
136 /* Keep trying to GC higher and higher generations until we don't run out
142 current_gc->event->op = current_gc->op;
144 switch(current_gc->op)
146 case collect_nursery_op:
149 case collect_aging_op:
151 if(data->high_fragmentation_p())
153 current_gc->op = collect_full_op;
154 current_gc->event->op = collect_full_op;
155 collect_full(trace_contexts_p);
158 case collect_to_tenured_op:
159 collect_to_tenured();
160 if(data->high_fragmentation_p())
162 current_gc->op = collect_full_op;
163 current_gc->event->op = collect_full_op;
164 collect_full(trace_contexts_p);
167 case collect_full_op:
168 collect_full(trace_contexts_p);
170 case collect_compact_op:
171 collect_compact(trace_contexts_p);
173 case collect_growing_heap_op:
174 collect_growing_heap(requested_bytes,trace_contexts_p);
177 critical_error("Bad GC op",current_gc->op);
183 catch(const must_start_gc_again &)
185 /* We come back here if a generation is full */
197 /* primitive_minor_gc() is invoked by inline GC checks, and it needs to fill in
198 uninitialized stack locations before actually calling the GC. See the comment
199 in compiler.cfg.stacks.uninitialized for details. */
201 struct call_frame_scrubber {
205 explicit call_frame_scrubber(factor_vm *parent_, context *ctx_) :
206 parent(parent_), ctx(ctx_) {}
208 void operator()(stack_frame *frame)
210 cell return_address = parent->frame_offset(frame);
211 if(return_address == (cell)-1)
214 code_block *compiled = parent->frame_code(frame);
215 gc_info *info = compiled->block_gc_info();
217 assert(return_address < compiled->size());
218 int index = info->return_address_index(return_address);
220 ctx->scrub_stacks(info,index);
224 void factor_vm::scrub_context(context *ctx)
226 call_frame_scrubber scrubber(this,ctx);
227 iterate_callstack(ctx,scrubber);
230 void factor_vm::scrub_contexts()
232 std::set<context *>::const_iterator begin = active_contexts.begin();
233 std::set<context *>::const_iterator end = active_contexts.end();
236 scrub_context(*begin);
241 void factor_vm::primitive_minor_gc()
245 gc(collect_nursery_op,
246 0, /* requested size */
247 true /* trace contexts? */);
250 void factor_vm::primitive_full_gc()
253 0, /* requested size */
254 true /* trace contexts? */);
257 void factor_vm::primitive_compact_gc()
259 gc(collect_compact_op,
260 0, /* requested size */
261 true /* trace contexts? */);
265 * It is up to the caller to fill in the object's fields in a meaningful
268 object *factor_vm::allot_large_object(cell type, cell size)
270 /* If tenured space does not have enough room, collect and compact */
271 if(!data->tenured->can_allot_p(size))
273 primitive_compact_gc();
275 /* If it still won't fit, grow the heap */
276 if(!data->tenured->can_allot_p(size))
278 gc(collect_growing_heap_op,
279 size, /* requested size */
280 true /* trace contexts? */);
284 object *obj = data->tenured->allot(size);
286 /* Allows initialization code to store old->new pointers
287 without hitting the write barrier in the common case of
288 a nursery allocation */
289 write_barrier(obj,size);
291 obj->initialize(type);
295 void factor_vm::primitive_enable_gc_events()
297 gc_events = new std::vector<gc_event>();
300 void factor_vm::primitive_disable_gc_events()
304 growable_array result(this);
306 std::vector<gc_event> *gc_events = this->gc_events;
307 this->gc_events = NULL;
309 std::vector<gc_event>::const_iterator iter = gc_events->begin();
310 std::vector<gc_event>::const_iterator end = gc_events->end();
312 for(; iter != end; iter++)
314 gc_event event = *iter;
315 byte_array *obj = byte_array_from_value(&event);
316 result.add(tag<byte_array>(obj));
320 ctx->push(result.elements.value());
322 delete this->gc_events;
325 ctx->push(false_object);