factor/vm/data_gc.cpp

807 lines
20 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(data_heap *data_, bool growing_data_heap_, cell collecting_gen_) :
data(data_),
growing_data_heap(growing_data_heap_),
collecting_gen(collecting_gen_),
collecting_aging_again(false),
start_time(current_micros()) { }
gc_state::~gc_state() { }
2009-10-06 20:15:54 -04:00
template<typename Strategy> object *factor_vm::resolve_forwarding(object *untagged, Strategy &strategy)
{
check_data_pointer(untagged);
/* is there another forwarding pointer? */
while(untagged->h.forwarding_pointer_p())
untagged = untagged->h.forwarding_pointer();
/* we've found the destination */
untagged->h.check_header();
return untagged;
}
2009-10-05 23:16:08 -04:00
template<typename Strategy> void factor_vm::trace_handle(cell *handle, Strategy &strategy)
{
2009-05-04 05:50:24 -04:00
cell pointer = *handle;
if(!immediate_p(pointer))
{
2009-10-06 20:15:54 -04:00
object *untagged = untag<object>(pointer);
if(strategy.should_copy_p(untagged))
{
object *forwarding = resolve_forwarding(untagged,strategy);
2009-10-06 21:45:47 -04:00
2009-10-06 20:15:54 -04:00
if(forwarding == untagged)
2009-10-06 21:45:47 -04:00
untagged = strategy.copy_object(untagged);
2009-10-06 20:15:54 -04:00
else if(strategy.should_copy_p(forwarding))
2009-10-06 21:45:47 -04:00
untagged = strategy.copy_object(forwarding);
2009-10-06 20:15:54 -04:00
else
2009-10-06 21:45:47 -04:00
untagged = forwarding;
*handle = RETAG(untagged,TAG(pointer));
2009-10-06 20:15:54 -04:00
}
}
}
template<typename Strategy> void factor_vm::trace_slots(object *ptr, Strategy &strategy)
{
cell *slot = (cell *)ptr;
cell *end = (cell *)((cell)ptr + binary_payload_start(ptr));
if(slot != end)
{
slot++;
for(; slot < end; slot++) trace_handle(slot,strategy);
}
}
2009-10-06 21:45:47 -04:00
template<typename Strategy> object *factor_vm::promote_object(object *untagged, Strategy &strategy)
{
cell size = untagged_object_size(untagged);
object *newpointer = strategy.allot(size);
if(!newpointer) longjmp(current_gc->gc_unwind,1);
2009-10-07 09:40:28 -04:00
generation_statistics *s = &gc_stats.generations[current_gc->collecting_gen];
2009-10-06 21:45:47 -04:00
s->object_count++;
s->bytes_copied += size;
memcpy(newpointer,untagged,size);
untagged->h.forward_to(newpointer);
return newpointer;
}
template<typename Strategy> void factor_vm::trace_card(card *ptr, cell here, Strategy &strategy)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
cell card_scan = card_to_addr(ptr) + card_offset(ptr);
cell card_end = card_to_addr(ptr + 1);
2009-05-02 05:04:19 -04:00
if(here < card_end) card_end = here;
2009-05-02 05:04:19 -04:00
2009-10-05 23:16:08 -04:00
strategy.copy_reachable_objects(card_scan,&card_end);
2009-05-02 05:04:19 -04:00
2009-10-07 09:40:28 -04:00
gc_stats.cards_scanned++;
2009-05-02 05:04:19 -04:00
}
template<typename Strategy> void factor_vm::trace_card_deck(card_deck *deck, cell here, card mask, card unmask, Strategy &strategy)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
card *first_card = deck_to_card(deck);
card *last_card = deck_to_card(deck + 1);
2009-05-02 05:04:19 -04:00
u32 *quad_ptr;
u32 quad_mask = mask | (mask << 8) | (mask << 16) | (mask << 24);
for(quad_ptr = (u32 *)first_card; quad_ptr < (u32 *)last_card; quad_ptr++)
{
if(*quad_ptr & quad_mask)
{
2009-05-04 05:50:24 -04:00
card *ptr = (card *)quad_ptr;
2009-05-02 05:04:19 -04:00
int card;
for(card = 0; card < 4; card++)
{
if(ptr[card] & mask)
{
trace_card(&ptr[card],here,strategy);
2009-05-02 05:04:19 -04:00
ptr[card] &= ~unmask;
}
}
}
}
2009-10-07 09:40:28 -04:00
gc_stats.decks_scanned++;
2009-05-02 05:04:19 -04:00
}
/* Trace all objects referenced from marked cards */
template<typename Strategy> void factor_vm::trace_cards(cell gen, zone *z, Strategy &strategy)
2009-05-02 05:04:19 -04:00
{
u64 start_time = current_micros();
card_deck *first_deck = addr_to_deck(z->start);
card_deck *last_deck = addr_to_deck(z->end);
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
card mask, unmask;
2009-05-02 05:04:19 -04:00
/* if we are collecting the nursery, we care about old->nursery pointers
but not old->aging pointers */
if(current_gc->collecting_nursery_p())
2009-05-02 05:04:19 -04:00
{
mask = card_points_to_nursery;
2009-05-02 05:04:19 -04:00
/* after the collection, no old->nursery pointers remain
anywhere, but old->aging pointers might remain in tenured
space */
2009-10-07 09:33:54 -04:00
if(gen == tenured_gen)
unmask = card_points_to_nursery;
2009-05-02 05:04:19 -04:00
/* after the collection, all cards in aging space can be
cleared */
2009-10-07 09:33:54 -04:00
else if(gen == aging_gen)
unmask = card_mark_mask;
2009-05-02 05:04:19 -04:00
else
{
critical_error("bug in trace_generation_cards",gen);
2009-05-02 05:04:19 -04:00
return;
}
}
/* if we are collecting aging space into tenured space, we care about
all old->nursery and old->aging pointers. no old->aging pointers can
remain */
else if(current_gc->collecting_aging_p())
2009-05-02 05:04:19 -04:00
{
if(current_gc->collecting_aging_again)
2009-05-02 05:04:19 -04:00
{
mask = card_points_to_aging;
unmask = card_mark_mask;
2009-05-02 05:04:19 -04:00
}
/* after we collect aging space into the aging semispace, no
old->nursery pointers remain but tenured space might still have
pointers to aging space. */
else
{
mask = card_points_to_aging;
unmask = card_points_to_nursery;
2009-05-02 05:04:19 -04:00
}
}
else
{
critical_error("bug in trace_generation_cards",gen);
2009-05-02 05:04:19 -04:00
return;
}
2009-05-04 05:50:24 -04:00
card_deck *ptr;
2009-05-02 05:04:19 -04:00
for(ptr = first_deck; ptr < last_deck; ptr++)
{
if(*ptr & mask)
{
trace_card_deck(ptr,z->here,mask,unmask,strategy);
2009-05-02 05:04:19 -04:00
*ptr &= ~unmask;
}
}
gc_stats.card_scan_time += (current_micros() - start_time);
2009-05-02 05:04:19 -04:00
}
/* Copy all tagged pointers in a range of memory */
2009-10-05 23:16:08 -04:00
template<typename Strategy> void factor_vm::trace_stack_elements(segment *region, cell top, Strategy &strategy)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
cell ptr = region->start;
2009-05-02 05:04:19 -04:00
2009-05-04 05:50:24 -04:00
for(; ptr <= top; ptr += sizeof(cell))
2009-10-05 23:16:08 -04:00
trace_handle((cell*)ptr,strategy);
2009-05-02 05:04:19 -04:00
}
2009-10-05 23:16:08 -04:00
template<typename Strategy> void factor_vm::trace_registered_locals(Strategy &strategy)
{
std::vector<cell>::const_iterator iter = gc_locals.begin();
std::vector<cell>::const_iterator end = gc_locals.end();
for(; iter < end; iter++)
2009-10-05 23:16:08 -04:00
trace_handle((cell *)(*iter),strategy);
}
2009-10-05 23:16:08 -04:00
template<typename Strategy> void factor_vm::trace_registered_bignums(Strategy &strategy)
2009-05-02 05:04:19 -04:00
{
std::vector<cell>::const_iterator iter = gc_bignums.begin();
std::vector<cell>::const_iterator end = gc_bignums.end();
2009-05-02 05:04:19 -04:00
for(; iter < end; iter++)
{
2009-10-06 20:15:54 -04:00
cell *handle = (cell *)(*iter);
2009-10-06 20:15:54 -04:00
if(*handle)
{
2009-10-06 20:15:54 -04:00
*handle |= BIGNUM_TYPE;
trace_handle(handle,strategy);
*handle &= ~BIGNUM_TYPE;
}
}
2009-05-02 05:04:19 -04:00
}
/* Copy roots over at the start of GC, namely various constants, stacks,
2009-05-02 10:19:09 -04:00
the user environment and extra roots registered by local_roots.hpp */
2009-10-05 23:16:08 -04:00
template<typename Strategy> void factor_vm::trace_roots(Strategy &strategy)
2009-05-02 05:04:19 -04:00
{
2009-10-05 23:16:08 -04:00
trace_handle(&T,strategy);
trace_handle(&bignum_zero,strategy);
trace_handle(&bignum_pos_one,strategy);
trace_handle(&bignum_neg_one,strategy);
2009-05-02 05:04:19 -04:00
2009-10-05 23:16:08 -04:00
trace_registered_locals(strategy);
trace_registered_bignums(strategy);
2009-05-02 05:04:19 -04:00
int i;
for(i = 0; i < USER_ENV; i++)
2009-10-05 23:16:08 -04:00
trace_handle(&userenv[i],strategy);
}
template<typename Strategy> struct stack_frame_marker {
factor_vm *myvm;
Strategy &strategy;
explicit stack_frame_marker(factor_vm *myvm_, Strategy &strategy_) :
myvm(myvm_), strategy(strategy_) {}
void operator()(stack_frame *frame)
{
myvm->mark_code_block(myvm->frame_code(frame),strategy);
}
};
/* Mark code blocks executing in currently active stack frames. */
template<typename Strategy> void factor_vm::mark_active_blocks(context *stacks, Strategy &strategy)
{
if(current_gc->collecting_tenured_p())
{
cell top = (cell)stacks->callstack_top;
cell bottom = (cell)stacks->callstack_bottom;
stack_frame_marker<Strategy> marker(this,strategy);
iterate_callstack(top,bottom,marker);
}
}
2009-05-02 05:04:19 -04:00
2009-10-05 23:16:08 -04:00
template<typename Strategy> void factor_vm::mark_object_code_block(object *object, Strategy &strategy)
{
switch(object->h.hi_tag())
{
case WORD_TYPE:
{
word *w = (word *)object;
if(w->code)
mark_code_block(w->code,strategy);
if(w->profiling)
mark_code_block(w->profiling,strategy);
break;
}
case QUOTATION_TYPE:
{
quotation *q = (quotation *)object;
if(q->code)
mark_code_block(q->code,strategy);
break;
}
case CALLSTACK_TYPE:
{
callstack *stack = (callstack *)object;
stack_frame_marker<Strategy> marker(this,strategy);
iterate_callstack_object(stack,marker);
break;
}
}
}
template<typename Strategy> void factor_vm::trace_contexts(Strategy &strategy)
{
save_stacks();
context *stacks = stack_chain;
2009-05-02 05:04:19 -04:00
while(stacks)
{
2009-10-05 23:16:08 -04:00
trace_stack_elements(stacks->datastack_region,stacks->datastack,strategy);
trace_stack_elements(stacks->retainstack_region,stacks->retainstack,strategy);
2009-05-02 05:04:19 -04:00
2009-10-05 23:16:08 -04:00
trace_handle(&stacks->catchstack_save,strategy);
trace_handle(&stacks->current_callback_save,strategy);
2009-05-02 05:04:19 -04:00
2009-10-05 23:16:08 -04:00
mark_active_blocks(stacks,strategy);
2009-05-02 05:04:19 -04:00
stacks = stacks->next;
}
2009-05-02 05:04:19 -04:00
}
/* Trace all literals referenced from a code block. Only for aging and nursery collections */
2009-10-05 23:16:08 -04:00
template<typename Strategy> void factor_vm::trace_literal_references(code_block *compiled, Strategy &strategy)
2009-05-02 05:04:19 -04:00
{
trace_handle(&compiled->owner,strategy);
trace_handle(&compiled->literals,strategy);
trace_handle(&compiled->relocation,strategy);
2009-05-02 05:04:19 -04:00
}
/* Trace literals referenced from all code blocks. Only for aging and nursery collections */
2009-10-05 23:16:08 -04:00
template<typename Strategy> void factor_vm::trace_code_heap_roots(Strategy &strategy)
{
if(current_gc->collecting_gen >= code->youngest_referenced_generation)
2009-10-06 01:13:54 -04:00
{
unordered_map<code_block *,cell>::const_iterator iter = code->remembered_set.begin();
unordered_map<code_block *,cell>::const_iterator end = code->remembered_set.end();
2009-05-02 05:04:19 -04:00
for(; iter != end; iter++)
{
if(current_gc->collecting_gen >= iter->second)
trace_literal_references(iter->first,strategy);
}
2009-10-06 01:13:54 -04:00
2009-10-07 09:40:28 -04:00
gc_stats.code_heap_scans++;
2009-10-06 01:13:54 -04:00
}
2009-10-05 23:16:08 -04:00
}
2009-05-02 05:04:19 -04:00
2009-10-05 23:16:08 -04:00
/* Mark all literals referenced from a word XT. Only for tenured
collections */
template<typename Strategy> void factor_vm::mark_code_block(code_block *compiled, Strategy &strategy)
{
check_code_address((cell)compiled);
2009-05-02 05:04:19 -04:00
2009-10-05 23:16:08 -04:00
code->mark_block(compiled);
trace_literal_references(compiled,strategy);
2009-05-02 05:04:19 -04:00
}
struct literal_and_word_reference_updater {
factor_vm *myvm;
literal_and_word_reference_updater(factor_vm *myvm_) : myvm(myvm_) {}
void operator()(heap_block *block)
{
code_block *compiled = (code_block *)block;
myvm->update_literal_references(compiled);
myvm->update_word_references(compiled);
}
};
void factor_vm::free_unmarked_code_blocks()
{
literal_and_word_reference_updater updater(this);
code->free_unmarked(updater);
code->remembered_set.clear();
2009-10-07 09:33:54 -04:00
code->youngest_referenced_generation = tenured_gen;
}
void factor_vm::update_dirty_code_blocks()
{
/* The youngest generation that any code block can now reference */
cell gen;
if(current_gc->collecting_accumulation_gen_p())
gen = current_gc->collecting_gen;
else
gen = current_gc->collecting_gen + 1;
unordered_map<code_block *,cell>::iterator iter = code->remembered_set.begin();
unordered_map<code_block *,cell>::iterator end = code->remembered_set.end();
for(; iter != end; iter++)
{
if(current_gc->collecting_gen >= iter->second)
{
check_code_address((cell)iter->first);
update_literal_references(iter->first);
iter->second = gen;
}
}
code->youngest_referenced_generation = gen;
}
2009-10-06 01:13:54 -04:00
template<typename Strategy>
copying_collector<Strategy>::copying_collector(factor_vm *myvm_, zone *newspace_)
: myvm(myvm_), current_gc(myvm_->current_gc), newspace(newspace_)
2009-10-05 23:16:08 -04:00
{
scan = newspace->here;
2009-10-06 01:13:54 -04:00
}
template<typename Strategy> Strategy &copying_collector<Strategy>::strategy()
{
return static_cast<Strategy &>(*this);
}
2009-10-06 21:45:47 -04:00
template<typename Strategy> object *copying_collector<Strategy>::allot(cell size)
2009-10-06 01:13:54 -04:00
{
2009-10-06 21:45:47 -04:00
if(newspace->here + size <= newspace->end)
2009-10-07 09:33:54 -04:00
{
object *obj = newspace->allot(size);
myvm->allot_barrier(obj);
return obj;
}
2009-10-06 21:45:47 -04:00
else
return NULL;
2009-10-06 01:13:54 -04:00
}
2009-10-06 21:45:47 -04:00
template<typename Strategy> object *copying_collector<Strategy>::copy_object(object *untagged)
2009-10-06 01:13:54 -04:00
{
2009-10-06 21:45:47 -04:00
return myvm->promote_object(untagged,strategy());
2009-10-06 01:13:54 -04:00
}
template<typename Strategy> bool copying_collector<Strategy>::should_copy_p(object *pointer)
{
return strategy().should_copy_p(pointer);
}
template<typename Strategy> cell copying_collector<Strategy>::trace_next(cell scan)
2009-10-06 01:13:54 -04:00
{
2009-10-06 20:15:54 -04:00
object *obj = (object *)scan;
myvm->trace_slots(obj,strategy());
return scan + myvm->untagged_object_size(obj);
2009-10-06 01:13:54 -04:00
}
template<typename Strategy> void copying_collector<Strategy>::go()
{
strategy().copy_reachable_objects(scan,&newspace->here);
2009-10-06 01:13:54 -04:00
}
struct nursery_collector : copying_collector<nursery_collector>
{
explicit nursery_collector(factor_vm *myvm_, zone *newspace_) :
copying_collector<nursery_collector>(myvm_,newspace_) {}
2009-10-05 23:16:08 -04:00
bool should_copy_p(object *untagged)
{
2009-10-06 07:36:43 -04:00
return myvm->nursery.contains_p(untagged);
2009-10-05 23:16:08 -04:00
}
2009-10-06 01:13:54 -04:00
2009-10-05 23:16:08 -04:00
void copy_reachable_objects(cell scan, cell *end)
{
while(scan < *end) scan = trace_next(scan);
2009-10-05 23:16:08 -04:00
}
};
2009-10-06 01:13:54 -04:00
struct aging_collector : copying_collector<aging_collector>
2009-10-05 23:16:08 -04:00
{
2009-10-06 05:57:44 -04:00
zone *tenured;
explicit aging_collector(factor_vm *myvm_, zone *newspace_) :
2009-10-06 05:57:44 -04:00
copying_collector<aging_collector>(myvm_,newspace_),
tenured(myvm->data->tenured) {}
2009-10-05 23:16:08 -04:00
bool should_copy_p(object *untagged)
{
if(newspace->contains_p(untagged))
2009-10-05 23:16:08 -04:00
return false;
else
2009-10-06 05:57:44 -04:00
return !tenured->contains_p(untagged);
2009-10-05 23:16:08 -04:00
}
void copy_reachable_objects(cell scan, cell *end)
{
while(scan < *end) scan = trace_next(scan);
2009-10-05 23:16:08 -04:00
}
};
struct aging_again_collector : copying_collector<aging_again_collector>
2009-10-05 23:16:08 -04:00
{
explicit aging_again_collector(factor_vm *myvm_, zone *newspace_) :
copying_collector<aging_again_collector>(myvm_,newspace_) {}
2009-10-05 23:16:08 -04:00
bool should_copy_p(object *untagged)
{
return !newspace->contains_p(untagged);
}
void copy_reachable_objects(cell scan, cell *end)
{
while(scan < *end) scan = trace_next(scan);
}
};
2009-10-05 23:16:08 -04:00
2009-10-06 01:13:54 -04:00
struct tenured_collector : copying_collector<tenured_collector>
2009-10-05 23:16:08 -04:00
{
explicit tenured_collector(factor_vm *myvm_, zone *newspace_) :
copying_collector<tenured_collector>(myvm_,newspace_) {}
2009-10-05 23:16:08 -04:00
bool should_copy_p(object *untagged)
{
return !newspace->contains_p(untagged);
2009-10-05 23:16:08 -04:00
}
void copy_reachable_objects(cell scan, cell *end)
{
2009-10-06 01:13:54 -04:00
while(scan < *end)
2009-10-05 23:16:08 -04:00
{
myvm->mark_object_code_block(myvm->untag<object>(scan),*this);
scan = trace_next(scan);
2009-10-05 23:16:08 -04:00
}
}
};
void factor_vm::collect_nursery()
{
nursery_collector collector(this,data->aging);
trace_roots(collector);
trace_contexts(collector);
trace_cards(tenured_gen,data->tenured,collector);
trace_cards(aging_gen,data->aging,collector);
trace_code_heap_roots(collector);
collector.go();
update_dirty_code_blocks();
nursery.here = nursery.start;
}
void factor_vm::collect_aging()
{
std::swap(data->aging,data->aging_semispace);
reset_generation(data->aging);
aging_collector collector(this,data->aging);
trace_roots(collector);
trace_contexts(collector);
trace_cards(tenured_gen,data->tenured,collector);
trace_code_heap_roots(collector);
collector.go();
update_dirty_code_blocks();
2009-10-06 09:55:42 -04:00
nursery.here = nursery.start;
}
void factor_vm::collect_aging_again()
{
aging_again_collector collector(this,data->tenured);
trace_roots(collector);
trace_contexts(collector);
trace_cards(tenured_gen,data->tenured,collector);
trace_code_heap_roots(collector);
collector.go();
update_dirty_code_blocks();
reset_generation(data->aging);
2009-10-06 09:55:42 -04:00
nursery.here = nursery.start;
}
void factor_vm::collect_tenured(cell requested_bytes, bool trace_contexts_)
2009-10-05 23:16:08 -04:00
{
if(current_gc->growing_data_heap)
{
current_gc->old_data_heap = data;
set_data_heap(grow_data_heap(current_gc->old_data_heap,requested_bytes));
}
else
{
std::swap(data->tenured,data->tenured_semispace);
reset_generation(data->tenured);
}
tenured_collector collector(this,data->tenured);
2009-10-05 23:16:08 -04:00
trace_roots(collector);
2009-10-06 01:13:54 -04:00
if(trace_contexts_) trace_contexts(collector);
collector.go();
2009-10-05 23:16:08 -04:00
free_unmarked_code_blocks();
reset_generation(data->aging);
2009-10-06 09:55:42 -04:00
nursery.here = nursery.start;
if(current_gc->growing_data_heap)
delete current_gc->old_data_heap;
2009-10-05 23:16:08 -04:00
}
void factor_vm::record_gc_stats()
2009-05-02 05:04:19 -04:00
{
2009-10-07 09:40:28 -04:00
generation_statistics *s = &gc_stats.generations[current_gc->collecting_gen];
cell gc_elapsed = (current_micros() - current_gc->start_time);
2009-05-02 05:04:19 -04:00
s->collections++;
s->gc_time += gc_elapsed;
if(s->max_gc_time < gc_elapsed)
s->max_gc_time = gc_elapsed;
}
/* Collect gen and all younger generations.
If growing_data_heap_ is true, we must grow the data heap to such a size that
an allocation of requested_bytes won't fail */
void factor_vm::garbage_collection(cell collecting_gen_, bool growing_data_heap_, bool trace_contexts_, cell requested_bytes)
2009-05-02 05:04:19 -04:00
{
assert(!gc_off);
assert(!current_gc);
2009-05-02 05:04:19 -04:00
current_gc = new gc_state(data,growing_data_heap_,collecting_gen_);
2009-05-02 05:04:19 -04:00
/* Keep trying to GC higher and higher generations until we don't run out
of space */
if(setjmp(current_gc->gc_unwind))
{
/* We come back here if a generation is full */
/* We have no older generations we can try collecting, so we
resort to growing the data heap */
if(current_gc->collecting_tenured_p())
{
current_gc->growing_data_heap = true;
/* see the comment in unmark_marked() */
code->unmark_marked();
}
/* we try collecting aging space twice before going on to
collect tenured */
else if(current_gc->collecting_aging_p()
&& !current_gc->collecting_aging_again)
{
current_gc->collecting_aging_again = true;
}
/* Collect the next oldest generation */
else
{
current_gc->collecting_gen++;
}
}
2009-10-05 23:16:08 -04:00
if(current_gc->collecting_nursery_p())
collect_nursery();
else if(current_gc->collecting_aging_p())
{
if(current_gc->collecting_aging_again)
collect_aging_again();
else
collect_aging();
}
2009-10-05 23:16:08 -04:00
else if(current_gc->collecting_tenured_p())
collect_tenured(requested_bytes,trace_contexts_);
record_gc_stats();
2009-05-02 05:04:19 -04:00
delete current_gc;
current_gc = NULL;
2009-05-02 05:04:19 -04:00
}
2009-09-23 14:05:46 -04:00
void factor_vm::gc()
2009-05-02 05:04:19 -04:00
{
2009-10-07 09:33:54 -04:00
garbage_collection(tenured_gen,false,true,0);
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_gc()
2009-05-02 05:04:19 -04:00
{
gc();
}
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
2009-05-04 05:50:24 -04:00
cell i;
2009-05-02 05:04:19 -04:00
u64 total_gc_time = 0;
for(i = 0; i < gen_count; i++)
2009-05-02 05:04:19 -04:00
{
2009-10-07 09:40:28 -04:00
generation_statistics *s = &gc_stats.generations[i];
2009-05-04 05:50:24 -04:00
result.add(allot_cell(s->collections));
result.add(tag<bignum>(long_long_to_bignum(s->gc_time)));
result.add(tag<bignum>(long_long_to_bignum(s->max_gc_time)));
result.add(allot_cell(s->collections == 0 ? 0 : s->gc_time / s->collections));
result.add(allot_cell(s->object_count));
result.add(tag<bignum>(long_long_to_bignum(s->bytes_copied)));
2009-05-02 05:04:19 -04:00
total_gc_time += s->gc_time;
}
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)));
result.add(allot_cell(gc_stats.code_heap_scans));
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
}
gc();
/* 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]);
2009-10-07 09:33:54 -04:00
garbage_collection(nursery_gen,false,true,0);
for(cell i = 0; i < gc_roots_size; i++)
gc_locals.pop_back();
}
2009-05-04 02:46:13 -04:00
2009-09-25 13:29:07 -04:00
VM_C_API void inline_gc(cell *gc_roots_base, cell gc_roots_size, factor_vm *myvm)
2009-08-17 16:37:05 -04:00
{
ASSERTVM();
VM_PTR->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)
gc();
#endif
object *obj;
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)
2009-10-07 09:33:54 -04:00
garbage_collection(nursery_gen,false,true,0);
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 */
if(data->tenured->here + size > data->tenured->end)
2009-09-29 14:53:10 -04:00
gc();
/* If it still won't fit, grow the heap */
if(data->tenured->here + size > data->tenured->end)
2009-10-07 09:33:54 -04:00
garbage_collection(tenured_gen,true,true,size);
2009-09-29 14:53:10 -04:00
obj = data->tenured->allot(size);
2009-10-07 09:33:54 -04:00
allot_barrier(obj);
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 */
write_barrier(obj);
}
obj->h = header;
return obj;
}
2009-05-04 02:46:13 -04:00
}