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 void factor_vm::primitive_minor_gc()
199 gc(collect_nursery_op,
200 0, /* requested size */
201 true /* trace contexts? */);
204 void factor_vm::primitive_full_gc()
207 0, /* requested size */
208 true /* trace contexts? */);
211 void factor_vm::primitive_compact_gc()
213 gc(collect_compact_op,
214 0, /* requested size */
215 true /* trace contexts? */);
218 void factor_vm::inline_gc(cell gc_roots_)
220 cell stack_pointer = (cell)ctx->callstack_top;
222 if(to_boolean(gc_roots_))
224 tagged<array> gc_roots(gc_roots_);
226 cell capacity = array_capacity(gc_roots.untagged());
227 for(cell i = 0; i < capacity; i++)
229 cell spill_slot = untag_fixnum(array_nth(gc_roots.untagged(),i));
230 cell *address = (cell *)(spill_slot + stack_pointer);
231 data_roots.push_back(data_root_range(address,1));
234 primitive_minor_gc();
236 for(cell i = 0; i < capacity; i++)
237 data_roots.pop_back();
240 primitive_minor_gc();
243 VM_C_API void inline_gc(cell gc_roots, factor_vm *parent)
245 parent->inline_gc(gc_roots);
249 * It is up to the caller to fill in the object's fields in a meaningful
252 object *factor_vm::allot_large_object(cell type, cell size)
254 /* If tenured space does not have enough room, collect and compact */
255 if(!data->tenured->can_allot_p(size))
257 primitive_compact_gc();
259 /* If it still won't fit, grow the heap */
260 if(!data->tenured->can_allot_p(size))
262 gc(collect_growing_heap_op,
263 size, /* requested size */
264 true /* trace contexts? */);
268 object *obj = data->tenured->allot(size);
270 /* Allows initialization code to store old->new pointers
271 without hitting the write barrier in the common case of
272 a nursery allocation */
273 write_barrier(obj,size);
275 obj->initialize(type);
279 void factor_vm::primitive_enable_gc_events()
281 gc_events = new std::vector<gc_event>();
284 void factor_vm::primitive_disable_gc_events()
288 growable_array result(this);
290 std::vector<gc_event> *gc_events = this->gc_events;
291 this->gc_events = NULL;
293 std::vector<gc_event>::const_iterator iter = gc_events->begin();
294 std::vector<gc_event>::const_iterator end = gc_events->end();
296 for(; iter != end; iter++)
298 gc_event event = *iter;
299 byte_array *obj = byte_array_from_value(&event);
300 result.add(tag<byte_array>(obj));
304 ctx->push(result.elements.value());
306 delete this->gc_events;
309 ctx->push(false_object);