6 /* used during garbage collection only */
9 bool performing_compaction;
12 /* if true, we are collecting aging space for the second time, so if it is still
13 full, we go on to collect tenured */
14 bool collecting_aging_again;
16 /* in case a generation fills up in the middle of a gc, we jump back
17 up to try collecting the next generation. */
20 gc_stats stats[max_gen_count];
26 /* What generation was being collected when copy_code_heap_roots() was last
27 called? Until the next call to add_code_block(), future
28 collections of younger generations don't have to touch the code
30 cell last_code_heap_scan;
32 /* sometimes we grow the heap */
33 bool growing_data_heap;
34 data_heap *old_data_heap;
38 performing_gc = false;
39 last_code_heap_scan = data->nursery();
40 collecting_aging_again = false;
43 /* Given a pointer to oldspace, copy it to newspace */
44 static object *copy_untagged_object_impl(object *pointer, cell size)
46 if(newspace->here + size >= newspace->end)
48 object *newpointer = allot_zone(newspace,size);
50 gc_stats *s = &stats[collecting_gen];
52 s->bytes_copied += size;
54 memcpy(newpointer,pointer,size);
58 static object *copy_object_impl(object *untagged)
60 object *newpointer = copy_untagged_object_impl(untagged,untagged_object_size(untagged));
61 untagged->h.forward_to(newpointer);
65 static bool should_copy_p(object *untagged)
67 if(in_zone(newspace,untagged))
69 if(collecting_gen == data->tenured())
71 else if(data->have_aging_p() && collecting_gen == data->aging())
72 return !in_zone(&data->generations[data->tenured()],untagged);
73 else if(collecting_gen == data->nursery())
74 return in_zone(&nursery,untagged);
77 critical_error("Bug in should_copy_p",(cell)untagged);
82 /* Follow a chain of forwarding pointers */
83 static object *resolve_forwarding(object *untagged)
85 check_data_pointer(untagged);
87 /* is there another forwarding pointer? */
88 if(untagged->h.forwarding_pointer_p())
89 return resolve_forwarding(untagged->h.forwarding_pointer());
90 /* we've found the destination */
93 untagged->h.check_header();
94 if(should_copy_p(untagged))
95 return copy_object_impl(untagged);
101 template <typename T> static T *copy_untagged_object(T *untagged)
103 check_data_pointer(untagged);
105 if(untagged->h.forwarding_pointer_p())
106 untagged = (T *)resolve_forwarding(untagged->h.forwarding_pointer());
109 untagged->h.check_header();
110 untagged = (T *)copy_object_impl(untagged);
116 static cell copy_object(cell pointer)
118 return RETAG(copy_untagged_object(untag<object>(pointer)),TAG(pointer));
121 void copy_handle(cell *handle)
123 cell pointer = *handle;
125 if(!immediate_p(pointer))
127 object *obj = untag<object>(pointer);
128 check_data_pointer(obj);
129 if(should_copy_p(obj))
130 *handle = copy_object(pointer);
134 /* Scan all the objects in the card */
135 static void copy_card(card *ptr, cell gen, cell here)
137 cell card_scan = card_to_addr(ptr) + card_offset(ptr);
138 cell card_end = card_to_addr(ptr + 1);
143 copy_reachable_objects(card_scan,&card_end);
148 static void copy_card_deck(card_deck *deck, cell gen, card mask, card unmask)
150 card *first_card = deck_to_card(deck);
151 card *last_card = deck_to_card(deck + 1);
153 cell here = data->generations[gen].here;
156 u32 quad_mask = mask | (mask << 8) | (mask << 16) | (mask << 24);
158 for(quad_ptr = (u32 *)first_card; quad_ptr < (u32 *)last_card; quad_ptr++)
160 if(*quad_ptr & quad_mask)
162 card *ptr = (card *)quad_ptr;
165 for(card = 0; card < 4; card++)
169 copy_card(&ptr[card],gen,here);
170 ptr[card] &= ~unmask;
179 /* Copy all newspace objects referenced from marked cards to the destination */
180 static void copy_gen_cards(cell gen)
182 card_deck *first_deck = addr_to_deck(data->generations[gen].start);
183 card_deck *last_deck = addr_to_deck(data->generations[gen].end);
187 /* if we are collecting the nursery, we care about old->nursery pointers
188 but not old->aging pointers */
189 if(collecting_gen == data->nursery())
191 mask = card_points_to_nursery;
193 /* after the collection, no old->nursery pointers remain
194 anywhere, but old->aging pointers might remain in tenured
196 if(gen == data->tenured())
197 unmask = card_points_to_nursery;
198 /* after the collection, all cards in aging space can be
200 else if(data->have_aging_p() && gen == data->aging())
201 unmask = card_mark_mask;
204 critical_error("bug in copy_gen_cards",gen);
208 /* if we are collecting aging space into tenured space, we care about
209 all old->nursery and old->aging pointers. no old->aging pointers can
211 else if(data->have_aging_p() && collecting_gen == data->aging())
213 if(collecting_aging_again)
215 mask = card_points_to_aging;
216 unmask = card_mark_mask;
218 /* after we collect aging space into the aging semispace, no
219 old->nursery pointers remain but tenured space might still have
220 pointers to aging space. */
223 mask = card_points_to_aging;
224 unmask = card_points_to_nursery;
229 critical_error("bug in copy_gen_cards",gen);
235 for(ptr = first_deck; ptr < last_deck; ptr++)
239 copy_card_deck(ptr,gen,mask,unmask);
245 /* Scan cards in all generations older than the one being collected, copying
246 old->new references */
247 static void copy_cards()
249 u64 start = current_micros();
252 for(i = collecting_gen + 1; i < data->gen_count; i++)
255 card_scan_time += (current_micros() - start);
258 /* Copy all tagged pointers in a range of memory */
259 static void copy_stack_elements(segment *region, cell top)
261 cell ptr = region->start;
263 for(; ptr <= top; ptr += sizeof(cell))
264 copy_handle((cell*)ptr);
267 static void copy_registered_locals()
269 cell scan = gc_locals_region->start;
271 for(; scan <= gc_locals; scan += sizeof(cell))
272 copy_handle(*(cell **)scan);
275 static void copy_registered_bignums()
277 cell scan = gc_bignums_region->start;
279 for(; scan <= gc_bignums; scan += sizeof(cell))
281 bignum **handle = *(bignum ***)scan;
282 bignum *pointer = *handle;
286 check_data_pointer(pointer);
287 if(should_copy_p(pointer))
288 *handle = copy_untagged_object(pointer);
290 assert((*handle)->h.hi_tag() == BIGNUM_TYPE);
296 /* Copy roots over at the start of GC, namely various constants, stacks,
297 the user environment and extra roots registered by local_roots.hpp */
298 static void copy_roots()
301 copy_handle(&bignum_zero);
302 copy_handle(&bignum_pos_one);
303 copy_handle(&bignum_neg_one);
305 copy_registered_locals();
306 copy_registered_bignums();
308 if(!performing_compaction)
311 context *stacks = stack_chain;
315 copy_stack_elements(stacks->datastack_region,stacks->datastack);
316 copy_stack_elements(stacks->retainstack_region,stacks->retainstack);
318 copy_handle(&stacks->catchstack_save);
319 copy_handle(&stacks->current_callback_save);
321 mark_active_blocks(stacks);
323 stacks = stacks->next;
328 for(i = 0; i < USER_ENV; i++)
329 copy_handle(&userenv[i]);
332 static cell copy_next_from_nursery(cell scan)
334 cell *obj = (cell *)scan;
335 cell *end = (cell *)(scan + binary_payload_start((object *)scan));
341 cell nursery_start = nursery.start;
342 cell nursery_end = nursery.end;
344 for(; obj < end; obj++)
348 if(!immediate_p(pointer))
350 check_data_pointer((object *)pointer);
351 if(pointer >= nursery_start && pointer < nursery_end)
352 *obj = copy_object(pointer);
357 return scan + untagged_object_size((object *)scan);
360 static cell copy_next_from_aging(cell scan)
362 cell *obj = (cell *)scan;
363 cell *end = (cell *)(scan + binary_payload_start((object *)scan));
369 cell tenured_start = data->generations[data->tenured()].start;
370 cell tenured_end = data->generations[data->tenured()].end;
372 cell newspace_start = newspace->start;
373 cell newspace_end = newspace->end;
375 for(; obj < end; obj++)
379 if(!immediate_p(pointer))
381 check_data_pointer((object *)pointer);
382 if(!(pointer >= newspace_start && pointer < newspace_end)
383 && !(pointer >= tenured_start && pointer < tenured_end))
384 *obj = copy_object(pointer);
389 return scan + untagged_object_size((object *)scan);
392 static cell copy_next_from_tenured(cell scan)
394 cell *obj = (cell *)scan;
395 cell *end = (cell *)(scan + binary_payload_start((object *)scan));
401 cell newspace_start = newspace->start;
402 cell newspace_end = newspace->end;
404 for(; obj < end; obj++)
408 if(!immediate_p(pointer))
410 check_data_pointer((object *)pointer);
411 if(!(pointer >= newspace_start && pointer < newspace_end))
412 *obj = copy_object(pointer);
417 mark_object_code_block((object *)scan);
419 return scan + untagged_object_size((object *)scan);
422 void copy_reachable_objects(cell scan, cell *end)
424 if(collecting_gen == data->nursery())
427 scan = copy_next_from_nursery(scan);
429 else if(data->have_aging_p() && collecting_gen == data->aging())
432 scan = copy_next_from_aging(scan);
434 else if(collecting_gen == data->tenured())
437 scan = copy_next_from_tenured(scan);
441 /* Prepare to start copying reachable objects into an unused zone */
442 static void begin_gc(cell requested_bytes)
444 if(growing_data_heap)
446 if(collecting_gen != data->tenured())
447 critical_error("Invalid parameters to begin_gc",0);
449 old_data_heap = data;
450 set_data_heap(grow_data_heap(old_data_heap,requested_bytes));
451 newspace = &data->generations[data->tenured()];
453 else if(collecting_accumulation_gen_p())
455 /* when collecting one of these generations, rotate it
456 with the semispace */
457 zone z = data->generations[collecting_gen];
458 data->generations[collecting_gen] = data->semispaces[collecting_gen];
459 data->semispaces[collecting_gen] = z;
460 reset_generation(collecting_gen);
461 newspace = &data->generations[collecting_gen];
462 clear_cards(collecting_gen,collecting_gen);
463 clear_decks(collecting_gen,collecting_gen);
464 clear_allot_markers(collecting_gen,collecting_gen);
468 /* when collecting a younger generation, we copy
469 reachable objects to the next oldest generation,
470 so we set the newspace so the next generation. */
471 newspace = &data->generations[collecting_gen + 1];
475 static void end_gc(cell gc_elapsed)
477 gc_stats *s = &stats[collecting_gen];
480 s->gc_time += gc_elapsed;
481 if(s->max_gc_time < gc_elapsed)
482 s->max_gc_time = gc_elapsed;
484 if(growing_data_heap)
486 dealloc_data_heap(old_data_heap);
487 old_data_heap = NULL;
488 growing_data_heap = false;
491 if(collecting_accumulation_gen_p())
493 /* all younger generations except are now empty.
494 if collecting_gen == data->nursery() here, we only have 1 generation;
495 old-school Cheney collector */
496 if(collecting_gen != data->nursery())
497 reset_generations(data->nursery(),collecting_gen - 1);
499 else if(collecting_gen == data->nursery())
501 nursery.here = nursery.start;
505 /* all generations up to and including the one
506 collected are now empty */
507 reset_generations(data->nursery(),collecting_gen);
510 collecting_aging_again = false;
513 /* Collect gen and all younger generations.
514 If growing_data_heap_ is true, we must grow the data heap to such a size that
515 an allocation of requested_bytes won't fail */
516 void garbage_collection(cell gen,
517 bool growing_data_heap_,
518 cell requested_bytes)
522 critical_error("GC disabled",gen);
526 u64 start = current_micros();
528 performing_gc = true;
529 growing_data_heap = growing_data_heap_;
530 collecting_gen = gen;
532 /* we come back here if a generation is full */
535 /* We have no older generations we can try collecting, so we
536 resort to growing the data heap */
537 if(collecting_gen == data->tenured())
539 growing_data_heap = true;
541 /* see the comment in unmark_marked() */
542 unmark_marked(&code);
544 /* we try collecting aging space twice before going on to
546 else if(data->have_aging_p()
547 && collecting_gen == data->aging()
548 && !collecting_aging_again)
550 collecting_aging_again = true;
552 /* Collect the next oldest generation */
559 begin_gc(requested_bytes);
561 /* initialize chase pointer */
562 cell scan = newspace->here;
564 /* collect objects referenced from stacks and environment */
566 /* collect objects referenced from older generations */
569 /* do some tracing */
570 copy_reachable_objects(scan,&newspace->here);
572 /* don't scan code heap unless it has pointers to this
573 generation or younger */
574 if(collecting_gen >= last_code_heap_scan)
578 if(collecting_gen == data->tenured())
579 free_unmarked(&code,(heap_iterator)update_literal_and_word_references);
581 copy_code_heap_roots();
583 if(collecting_accumulation_gen_p())
584 last_code_heap_scan = collecting_gen;
586 last_code_heap_scan = collecting_gen + 1;
589 cell gc_elapsed = (current_micros() - start);
593 performing_gc = false;
598 garbage_collection(data->tenured(),false,0);
608 growable_array result;
611 u64 total_gc_time = 0;
613 for(i = 0; i < max_gen_count; i++)
615 gc_stats *s = &stats[i];
616 result.add(allot_cell(s->collections));
617 result.add(tag<bignum>(long_long_to_bignum(s->gc_time)));
618 result.add(tag<bignum>(long_long_to_bignum(s->max_gc_time)));
619 result.add(allot_cell(s->collections == 0 ? 0 : s->gc_time / s->collections));
620 result.add(allot_cell(s->object_count));
621 result.add(tag<bignum>(long_long_to_bignum(s->bytes_copied)));
623 total_gc_time += s->gc_time;
626 result.add(tag<bignum>(ulong_long_to_bignum(total_gc_time)));
627 result.add(tag<bignum>(ulong_long_to_bignum(cards_scanned)));
628 result.add(tag<bignum>(ulong_long_to_bignum(decks_scanned)));
629 result.add(tag<bignum>(ulong_long_to_bignum(card_scan_time)));
630 result.add(allot_cell(code_heap_scans));
633 dpush(result.elements.value());
636 void clear_gc_stats()
638 for(cell i = 0; i < max_gen_count; i++)
639 memset(&stats[i],0,sizeof(gc_stats));
647 PRIMITIVE(clear_gc_stats)
652 /* classes.tuple uses this to reshape tuples; tools.deploy.shaker uses this
653 to coalesce equal but distinct quotations and wrappers. */
656 array *new_objects = untag_check<array>(dpop());
657 array *old_objects = untag_check<array>(dpop());
659 cell capacity = array_capacity(new_objects);
660 if(capacity != array_capacity(old_objects))
661 critical_error("bad parameters to become",0);
665 for(i = 0; i < capacity; i++)
667 tagged<object> old_obj(array_nth(old_objects,i));
668 tagged<object> new_obj(array_nth(new_objects,i));
670 if(old_obj != new_obj)
671 old_obj->h.forward_to(new_obj.untagged());
676 /* If a word's definition quotation was in old_objects and the
677 quotation in new_objects is not compiled, we might leak memory
678 by referencing the old quotation unless we recompile all
679 unoptimized words. */
683 VM_ASM_API void inline_gc(cell *gc_roots_base, cell gc_roots_size)
685 for(cell i = 0; i < gc_roots_size; i++)
686 gc_local_push((cell)&gc_roots_base[i]);
688 garbage_collection(data->nursery(),false,0);
690 for(cell i = 0; i < gc_roots_size; i++)