factor/vm/gc.cpp

276 lines
6.7 KiB
C++
Raw Normal View History

2009-05-02 05:04:19 -04:00
#include "master.hpp"
2009-05-04 02:46:13 -04:00
namespace factor
{
gc_state::gc_state(gc_op op_) : op(op_), start_time(current_micros()) {}
gc_state::~gc_state() {}
void factor_vm::update_code_heap_for_minor_gc(std::set<code_block *> *remembered_set)
{
/* The youngest generation that any code block can now reference */
2009-10-09 00:39:54 -04:00
std::set<code_block *>::const_iterator iter = remembered_set->begin();
std::set<code_block *>::const_iterator end = remembered_set->end();
for(; iter != end; iter++) update_literal_references(*iter);
}
void factor_vm::record_gc_stats(generation_statistics *stats)
2009-05-02 05:04:19 -04:00
{
cell gc_elapsed = (current_micros() - current_gc->start_time);
stats->collections++;
stats->gc_time += gc_elapsed;
if(stats->max_gc_time < gc_elapsed)
stats->max_gc_time = gc_elapsed;
2009-05-02 05:04:19 -04:00
}
void factor_vm::gc(gc_op op,
cell requested_bytes,
bool trace_contexts_p,
bool compact_code_heap_p)
2009-05-02 05:04:19 -04:00
{
assert(!gc_off);
assert(!current_gc);
2009-05-02 05:04:19 -04:00
save_stacks();
current_gc = new gc_state(op);
2009-05-02 05:04:19 -04:00
if(verbosegc)
std::cout << "GC requested, op=" << op << std::endl;
/* Keep trying to GC higher and higher generations until we don't run out
of space */
2009-10-09 03:57:04 -04:00
if(setjmp(current_gc->gc_unwind))
{
/* We come back here if a generation is full */
switch(current_gc->op)
2009-10-09 03:57:04 -04:00
{
case collect_nursery_op:
current_gc->op = collect_aging_op;
break;
case collect_aging_op:
current_gc->op = collect_to_tenured_op;
break;
case collect_to_tenured_op:
current_gc->op = collect_full_op;
break;
case collect_full_op:
current_gc->op = collect_growing_heap_op;
break;
default:
critical_error("Bad GC op\n",op);
break;
2009-10-09 03:57:04 -04:00
}
if(verbosegc)
std::cout << "GC rewind, op=" << op << std::endl;
2009-10-09 03:57:04 -04:00
}
switch(current_gc->op)
{
case collect_nursery_op:
collect_nursery();
record_gc_stats(&gc_stats.nursery_stats);
break;
case collect_aging_op:
collect_aging();
record_gc_stats(&gc_stats.aging_stats);
break;
case collect_to_tenured_op:
collect_to_tenured();
record_gc_stats(&gc_stats.aging_stats);
break;
case collect_full_op:
collect_full(trace_contexts_p,compact_code_heap_p);
record_gc_stats(&gc_stats.full_stats);
break;
case collect_growing_heap_op:
collect_growing_heap(requested_bytes,trace_contexts_p,compact_code_heap_p);
record_gc_stats(&gc_stats.full_stats);
break;
default:
critical_error("Bad GC op\n",op);
break;
}
2009-05-02 05:04:19 -04:00
delete current_gc;
current_gc = NULL;
if(verbosegc)
std::cout << "GC done, op=" << op << std::endl;
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_minor_gc()
2009-05-02 05:04:19 -04:00
{
gc(collect_full_op,
0, /* requested size */
true, /* trace contexts? */
false /* compact code heap? */);
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_full_gc()
2009-05-02 05:04:19 -04:00
{
gc(collect_full_op,
0, /* requested size */
true, /* trace contexts? */
false /* compact code heap? */);
}
void factor_vm::primitive_compact_gc()
{
gc(collect_full_op,
0, /* requested size */
true, /* trace contexts? */
true /* compact code heap? */);
2009-05-02 05:04:19 -04:00
}
void factor_vm::add_gc_stats(generation_statistics *stats, growable_array *result)
{
result->add(allot_cell(stats->collections));
result->add(tag<bignum>(long_long_to_bignum(stats->gc_time)));
result->add(tag<bignum>(long_long_to_bignum(stats->max_gc_time)));
result->add(allot_cell(stats->collections == 0 ? 0 : stats->gc_time / stats->collections));
result->add(allot_cell(stats->object_count));
result->add(tag<bignum>(long_long_to_bignum(stats->bytes_copied)));
}
void factor_vm::primitive_gc_stats()
2009-05-02 05:04:19 -04:00
{
growable_array result(this);
2009-05-02 05:04:19 -04:00
add_gc_stats(&gc_stats.nursery_stats,&result);
add_gc_stats(&gc_stats.aging_stats,&result);
add_gc_stats(&gc_stats.full_stats,&result);
2009-05-02 05:04:19 -04:00
u64 total_gc_time =
gc_stats.nursery_stats.gc_time +
gc_stats.aging_stats.gc_time +
gc_stats.full_stats.gc_time;
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
result.add(tag<bignum>(ulong_long_to_bignum(total_gc_time)));
2009-10-07 09:40:28 -04:00
result.add(tag<bignum>(ulong_long_to_bignum(gc_stats.cards_scanned)));
result.add(tag<bignum>(ulong_long_to_bignum(gc_stats.decks_scanned)));
result.add(tag<bignum>(ulong_long_to_bignum(gc_stats.card_scan_time)));
2009-10-09 00:39:54 -04:00
result.add(allot_cell(gc_stats.code_blocks_scanned));
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
result.trim();
dpush(result.elements.value());
2009-05-02 05:04:19 -04:00
}
2009-09-23 14:05:46 -04:00
void factor_vm::clear_gc_stats()
2009-05-02 05:04:19 -04:00
{
2009-10-07 09:40:28 -04:00
memset(&gc_stats,0,sizeof(gc_statistics));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_clear_gc_stats()
2009-08-17 16:37:05 -04:00
{
2009-08-17 16:37:16 -04:00
clear_gc_stats();
2009-08-17 16:37:05 -04:00
}
2009-05-02 05:04:19 -04:00
/* classes.tuple uses this to reshape tuples; tools.deploy.shaker uses this
to coalesce equal but distinct quotations and wrappers. */
void factor_vm::primitive_become()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
array *new_objects = untag_check<array>(dpop());
array *old_objects = untag_check<array>(dpop());
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
cell capacity = array_capacity(new_objects);
2009-05-02 05:04:19 -04:00
if(capacity != array_capacity(old_objects))
critical_error("bad parameters to become",0);
2009-05-04 05:50:24 -04:00
cell i;
2009-05-02 05:04:19 -04:00
for(i = 0; i < capacity; i++)
{
2009-05-04 05:50:24 -04:00
tagged<object> old_obj(array_nth(old_objects,i));
tagged<object> new_obj(array_nth(new_objects,i));
2009-05-02 05:04:19 -04:00
if(old_obj != new_obj)
2009-05-04 05:50:24 -04:00
old_obj->h.forward_to(new_obj.untagged());
2009-05-02 05:04:19 -04:00
}
primitive_full_gc();
2009-05-02 05:04:19 -04:00
/* If a word's definition quotation was in old_objects and the
quotation in new_objects is not compiled, we might leak memory
by referencing the old quotation unless we recompile all
unoptimized words. */
compile_all_words();
}
2009-09-23 14:05:46 -04:00
void factor_vm::inline_gc(cell *gc_roots_base, cell gc_roots_size)
{
for(cell i = 0; i < gc_roots_size; i++)
gc_locals.push_back((cell)&gc_roots_base[i]);
primitive_minor_gc();
for(cell i = 0; i < gc_roots_size; i++)
gc_locals.pop_back();
}
2009-05-04 02:46:13 -04:00
VM_C_API void inline_gc(cell *gc_roots_base, cell gc_roots_size, factor_vm *parent)
2009-08-17 16:37:05 -04:00
{
parent->inline_gc(gc_roots_base,gc_roots_size);
2009-08-17 16:37:05 -04:00
}
2009-09-29 14:53:10 -04:00
/*
* It is up to the caller to fill in the object's fields in a meaningful
* fashion!
*/
object *factor_vm::allot_object(header header, cell size)
{
#ifdef GC_DEBUG
if(!gc_off)
primitive_full_gc();
2009-09-29 14:53:10 -04:00
#endif
object *obj;
/* If the object is smaller than the nursery, allocate it in the nursery,
after a GC if needed */
if(nursery.size > size)
2009-09-29 14:53:10 -04:00
{
/* If there is insufficient room, collect the nursery */
if(nursery.here + size > nursery.end)
primitive_minor_gc();
2009-09-29 14:53:10 -04:00
2009-10-07 09:33:54 -04:00
obj = nursery.allot(size);
2009-09-29 14:53:10 -04:00
}
/* If the object is bigger than the nursery, allocate it in
tenured space */
else
{
/* If tenured space does not have enough room, collect */
//XXX
//if(data->tenured->here + size > data->tenured->end)
primitive_full_gc();
2009-09-29 14:53:10 -04:00
/* If it still won't fit, grow the heap */
//XXX
//if(data->tenured->here + size > data->tenured->end)
{
gc(collect_growing_heap_op,
size, /* requested size */
true, /* trace contexts? */
false /* compact code heap? */);
}
2009-09-29 14:53:10 -04:00
obj = data->tenured->allot(size);
2009-09-29 14:53:10 -04:00
/* Allows initialization code to store old->new pointers
without hitting the write barrier in the common case of
a nursery allocation */
char *start = (char *)obj;
for(cell offset = 0; offset < size; offset += card_size)
write_barrier((cell *)(start + offset));
2009-09-29 14:53:10 -04:00
}
obj->h = header;
return obj;
}
2009-05-04 02:46:13 -04:00
}