< prev index next > src/hotspot/share/gc/shenandoah/shenandoahFullGC.cpp
Print this page
#include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
#include "gc/shenandoah/shenandoahConcurrentGC.hpp"
#include "gc/shenandoah/shenandoahCollectionSet.hpp"
#include "gc/shenandoah/shenandoahFreeSet.hpp"
#include "gc/shenandoah/shenandoahFullGC.hpp"
+ #include "gc/shenandoah/shenandoahGeneration.hpp"
#include "gc/shenandoah/shenandoahPhaseTimings.hpp"
#include "gc/shenandoah/shenandoahMark.inline.hpp"
#include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
#include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
#include "gc/shenandoah/shenandoahHeap.inline.hpp"
#include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
#include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
#include "gc/shenandoah/shenandoahMetrics.hpp"
+ #include "gc/shenandoah/shenandoahOldGeneration.hpp"
#include "gc/shenandoah/shenandoahOopClosures.inline.hpp"
#include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
#include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
#include "gc/shenandoah/shenandoahSTWMark.hpp"
#include "gc/shenandoah/shenandoahUtils.hpp"
#include "gc/shenandoah/shenandoahVerifier.hpp"
#include "gc/shenandoah/shenandoahVMOperations.hpp"
#include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
+ #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
#include "memory/metaspaceUtils.hpp"
#include "memory/universe.hpp"
#include "oops/compressedOops.inline.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/javaThread.hpp"
#include "runtime/vmThread.hpp"
#include "utilities/copy.hpp"
#include "utilities/events.hpp"
#include "utilities/growableArray.hpp"
+ // After Full GC is done, reconstruct the remembered set by iterating over OLD regions,
+ // registering all objects between bottom() and top(), and setting remembered set cards to
+ // DIRTY if they hold interesting pointers.
+ class ShenandoahReconstructRememberedSetTask : public WorkerTask {
+ private:
+ ShenandoahRegionIterator _regions;
+
+ public:
+ ShenandoahReconstructRememberedSetTask() :
+ WorkerTask("Shenandoah Reset Bitmap") { }
+
+ void work(uint worker_id) {
+ ShenandoahParallelWorkerSession worker_session(worker_id);
+ ShenandoahHeapRegion* r = _regions.next();
+ ShenandoahHeap* heap = ShenandoahHeap::heap();
+ RememberedScanner* scanner = heap->card_scan();
+ ShenandoahSetRememberedCardsToDirtyClosure dirty_cards_for_interesting_pointers;
+
+ while (r != nullptr) {
+ if (r->is_old() && r->is_active()) {
+ HeapWord* obj_addr = r->bottom();
+ if (r->is_humongous_start()) {
+ // First, clear the remembered set
+ oop obj = cast_to_oop(obj_addr);
+ size_t size = obj->size();
+ HeapWord* end_object = r->bottom() + size;
+
+ // First, clear the remembered set for all spanned humongous regions
+ size_t num_regions = (size + ShenandoahHeapRegion::region_size_words() - 1) / ShenandoahHeapRegion::region_size_words();
+ size_t region_span = num_regions * ShenandoahHeapRegion::region_size_words();
+ scanner->reset_remset(r->bottom(), region_span);
+ size_t region_index = r->index();
+ ShenandoahHeapRegion* humongous_region = heap->get_region(region_index);
+ while (num_regions-- != 0) {
+ scanner->reset_object_range(humongous_region->bottom(), humongous_region->end());
+ region_index++;
+ humongous_region = heap->get_region(region_index);
+ }
+
+ // Then register the humongous object and DIRTY relevant remembered set cards
+ scanner->register_object_wo_lock(obj_addr);
+ obj->oop_iterate(&dirty_cards_for_interesting_pointers);
+ } else if (!r->is_humongous()) {
+ // First, clear the remembered set
+ scanner->reset_remset(r->bottom(), ShenandoahHeapRegion::region_size_words());
+ scanner->reset_object_range(r->bottom(), r->end());
+
+ // Then iterate over all objects, registering object and DIRTYing relevant remembered set cards
+ HeapWord* t = r->top();
+ while (obj_addr < t) {
+ oop obj = cast_to_oop(obj_addr);
+ size_t size = obj->size();
+ scanner->register_object_wo_lock(obj_addr);
+ obj_addr += obj->oop_iterate_size(&dirty_cards_for_interesting_pointers);
+ }
+ } // else, ignore humongous continuation region
+ }
+ // else, this region is FREE or YOUNG or inactive and we can ignore it.
+ r = _regions.next();
+ }
+ }
+ };
+
ShenandoahFullGC::ShenandoahFullGC() :
_gc_timer(ShenandoahHeap::heap()->gc_timer()),
_preserved_marks(new PreservedMarksSet(true)) {}
ShenandoahFullGC::~ShenandoahFullGC() {
op_full(cause);
}
void ShenandoahFullGC::op_full(GCCause::Cause cause) {
ShenandoahMetricsSnapshot metrics;
metrics.snap_before();
// Perform full GC
do_it(cause);
metrics.snap_after();
!
if (metrics.is_good_progress()) {
ShenandoahHeap::heap()->notify_gc_progress();
} else {
// Nothing to do. Tell the allocation path that we have failed to make
// progress, and it can finally fail.
op_full(cause);
}
void ShenandoahFullGC::op_full(GCCause::Cause cause) {
+ ShenandoahHeap* const heap = ShenandoahHeap::heap();
ShenandoahMetricsSnapshot metrics;
metrics.snap_before();
// Perform full GC
do_it(cause);
metrics.snap_after();
! if (heap->mode()->is_generational()) {
+ heap->log_heap_status("At end of Full GC");
+
+ // Since we allow temporary violation of these constraints during Full GC, we want to enforce that the assertions are
+ // made valid by the time Full GC completes.
+ assert(heap->old_generation()->used_regions_size() <= heap->old_generation()->adjusted_capacity(),
+ "Old generation affiliated regions must be less than capacity");
+ assert(heap->young_generation()->used_regions_size() <= heap->young_generation()->adjusted_capacity(),
+ "Young generation affiliated regions must be less than capacity");
+ }
if (metrics.is_good_progress()) {
ShenandoahHeap::heap()->notify_gc_progress();
} else {
// Nothing to do. Tell the allocation path that we have failed to make
// progress, and it can finally fail.
}
}
void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
ShenandoahHeap* heap = ShenandoahHeap::heap();
+ // Since we may arrive here from degenerated GC failure of either young or old, establish generation as GLOBAL.
+ heap->set_gc_generation(heap->global_generation());
+
+ if (heap->mode()->is_generational()) {
+ // Defer unadjust_available() invocations until after Full GC finishes its efforts because Full GC makes use
+ // of young-gen memory that may have been loaned from old-gen.
+
+ // No need to old_gen->increase_used(). That was done when plabs were allocated, accounting for both old evacs and promotions.
+
+ heap->set_alloc_supplement_reserve(0);
+ heap->set_young_evac_reserve(0);
+ heap->set_old_evac_reserve(0);
+ heap->reset_old_evac_expended();
+ heap->set_promoted_reserve(0);
+
+ // Full GC supersedes any marking or coalescing in old generation.
+ heap->cancel_old_gc();
+ }
if (ShenandoahVerify) {
heap->verifier()->verify_before_fullgc();
}
if (heap->is_update_refs_in_progress()) {
heap->set_update_refs_in_progress(false);
}
assert(!heap->is_update_refs_in_progress(), "sanity");
! // b. Cancel concurrent mark, if in progress
if (heap->is_concurrent_mark_in_progress()) {
! ShenandoahConcurrentGC::cancel();
- heap->set_concurrent_mark_in_progress(false);
}
assert(!heap->is_concurrent_mark_in_progress(), "sanity");
// c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots.
if (has_forwarded_objects) {
update_roots(true /*full_gc*/);
}
// d. Reset the bitmaps for new marking
! heap->reset_mark_bitmap();
assert(heap->marking_context()->is_bitmap_clear(), "sanity");
! assert(!heap->marking_context()->is_complete(), "sanity");
// e. Abandon reference discovery and clear all discovered references.
! ShenandoahReferenceProcessor* rp = heap->ref_processor();
rp->abandon_partial_discovery();
// f. Sync pinned region status from the CP marks
heap->sync_pinned_region_status();
if (heap->is_update_refs_in_progress()) {
heap->set_update_refs_in_progress(false);
}
assert(!heap->is_update_refs_in_progress(), "sanity");
! // b. Cancel all concurrent marks, if in progress
if (heap->is_concurrent_mark_in_progress()) {
! heap->cancel_concurrent_mark();
}
assert(!heap->is_concurrent_mark_in_progress(), "sanity");
// c. Update roots if this full GC is due to evac-oom, which may carry from-space pointers in roots.
if (has_forwarded_objects) {
update_roots(true /*full_gc*/);
}
// d. Reset the bitmaps for new marking
! heap->global_generation()->reset_mark_bitmap();
assert(heap->marking_context()->is_bitmap_clear(), "sanity");
! assert(!heap->global_generation()->is_mark_complete(), "sanity");
// e. Abandon reference discovery and clear all discovered references.
! ShenandoahReferenceProcessor* rp = heap->global_generation()->ref_processor();
rp->abandon_partial_discovery();
// f. Sync pinned region status from the CP marks
heap->sync_pinned_region_status();
assert(heap->has_forwarded_objects() == has_forwarded_objects, "This should not change");
}
if (UseTLAB) {
+ // TODO: Do we need to explicitly retire PLABs?
heap->gclabs_retire(ResizeTLAB);
heap->tlabs_retire(ResizeTLAB);
}
OrderAccess::fence();
{
// Epilogue
_preserved_marks->restore(heap->workers());
_preserved_marks->reclaim();
}
// Resize metaspace
MetaspaceGC::compute_new_size();
// Free worker slices
for (uint i = 0; i < heap->max_workers(); i++) {
delete worker_slices[i];
}
FREE_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, worker_slices);
heap->set_full_gc_move_in_progress(false);
heap->set_full_gc_in_progress(false);
if (ShenandoahVerify) {
! heap->verifier()->verify_after_fullgc();
}
if (VerifyAfterGC) {
Universe::verify();
}
{
{
// Epilogue
_preserved_marks->restore(heap->workers());
_preserved_marks->reclaim();
+
+ if (heap->mode()->is_generational()) {
+ ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_reconstruct_remembered_set);
+ ShenandoahReconstructRememberedSetTask task;
+ heap->workers()->run_task(&task);
+ }
}
// Resize metaspace
MetaspaceGC::compute_new_size();
+ heap->adjust_generation_sizes();
+
// Free worker slices
for (uint i = 0; i < heap->max_workers(); i++) {
delete worker_slices[i];
}
FREE_C_HEAP_ARRAY(ShenandoahHeapRegionSet*, worker_slices);
heap->set_full_gc_move_in_progress(false);
heap->set_full_gc_in_progress(false);
if (ShenandoahVerify) {
! if (heap->mode()->is_generational()) {
+ heap->verifier()->verify_after_generational_fullgc();
+ } else {
+ heap->verifier()->verify_after_fullgc();
+ }
}
+ // Having reclaimed all dead memory, it is now safe to restore capacities to original values.
+ heap->young_generation()->unadjust_available();
+ heap->old_generation()->unadjust_available();
+
if (VerifyAfterGC) {
Universe::verify();
}
{
public:
ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
void heap_region_do(ShenandoahHeapRegion *r) {
! _ctx->capture_top_at_mark_start(r);
! r->clear_live_data();
}
};
void ShenandoahFullGC::phase1_mark_heap() {
GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
ShenandoahHeap* heap = ShenandoahHeap::heap();
ShenandoahPrepareForMarkClosure cl;
! heap->heap_region_iterate(&cl);
! heap->set_unload_classes(heap->heuristics()->can_unload_classes());
! ShenandoahReferenceProcessor* rp = heap->ref_processor();
// enable ("weak") refs discovery
rp->set_soft_reference_policy(true); // forcefully purge all soft references
! ShenandoahSTWMark mark(true /*full_gc*/);
mark.mark();
heap->parallel_cleaning(true /* full_gc */);
}
class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
private:
PreservedMarks* const _preserved_marks;
ShenandoahHeap* const _heap;
GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
public:
ShenandoahPrepareForMarkClosure() : _ctx(ShenandoahHeap::heap()->marking_context()) {}
void heap_region_do(ShenandoahHeapRegion *r) {
! if (r->affiliation() != FREE) {
! _ctx->capture_top_at_mark_start(r);
+ r->clear_live_data();
+ }
}
+
+ bool is_thread_safe() { return true; }
};
void ShenandoahFullGC::phase1_mark_heap() {
GCTraceTime(Info, gc, phases) time("Phase 1: Mark live objects", _gc_timer);
ShenandoahGCPhase mark_phase(ShenandoahPhaseTimings::full_gc_mark);
ShenandoahHeap* heap = ShenandoahHeap::heap();
ShenandoahPrepareForMarkClosure cl;
! heap->parallel_heap_region_iterate(&cl);
! heap->set_unload_classes(heap->global_generation()->heuristics()->can_unload_classes());
! ShenandoahReferenceProcessor* rp = heap->global_generation()->ref_processor();
// enable ("weak") refs discovery
rp->set_soft_reference_policy(true); // forcefully purge all soft references
! ShenandoahSTWMark mark(heap->global_generation(), true /*full_gc*/);
mark.mark();
heap->parallel_cleaning(true /* full_gc */);
}
+ class ShenandoahPrepareForCompactionTask : public WorkerTask {
+ private:
+ PreservedMarksSet* const _preserved_marks;
+ ShenandoahHeap* const _heap;
+ ShenandoahHeapRegionSet** const _worker_slices;
+ size_t const _num_workers;
+
+ public:
+ ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices,
+ size_t num_workers);
+
+ static bool is_candidate_region(ShenandoahHeapRegion* r) {
+ // Empty region: get it into the slice to defragment the slice itself.
+ // We could have skipped this without violating correctness, but we really
+ // want to compact all live regions to the start of the heap, which sometimes
+ // means moving them into the fully empty regions.
+ if (r->is_empty()) return true;
+
+ // Can move the region, and this is not the humongous region. Humongous
+ // moves are special cased here, because their moves are handled separately.
+ return r->is_stw_move_allowed() && !r->is_humongous();
+ }
+
+ void work(uint worker_id);
+ };
+
+ class ShenandoahPrepareForGenerationalCompactionObjectClosure : public ObjectClosure {
+ private:
+ ShenandoahPrepareForCompactionTask* _compactor;
+ PreservedMarks* const _preserved_marks;
+ ShenandoahHeap* const _heap;
+
+ // _empty_regions is a thread-local list of heap regions that have been completely emptied by this worker thread's
+ // compaction efforts. The worker thread that drives these efforts adds compacted regions to this list if the
+ // region has not been compacted onto itself.
+ GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
+ int _empty_regions_pos;
+ ShenandoahHeapRegion* _old_to_region;
+ ShenandoahHeapRegion* _young_to_region;
+ ShenandoahHeapRegion* _from_region;
+ ShenandoahRegionAffiliation _from_affiliation;
+ HeapWord* _old_compact_point;
+ HeapWord* _young_compact_point;
+ uint _worker_id;
+
+ public:
+ ShenandoahPrepareForGenerationalCompactionObjectClosure(ShenandoahPrepareForCompactionTask* compactor,
+ PreservedMarks* preserved_marks,
+ GrowableArray<ShenandoahHeapRegion*>& empty_regions,
+ ShenandoahHeapRegion* old_to_region,
+ ShenandoahHeapRegion* young_to_region, uint worker_id) :
+ _compactor(compactor),
+ _preserved_marks(preserved_marks),
+ _heap(ShenandoahHeap::heap()),
+ _empty_regions(empty_regions),
+ _empty_regions_pos(0),
+ _old_to_region(old_to_region),
+ _young_to_region(young_to_region),
+ _from_region(nullptr),
+ _old_compact_point((old_to_region != nullptr)? old_to_region->bottom(): nullptr),
+ _young_compact_point((young_to_region != nullptr)? young_to_region->bottom(): nullptr),
+ _worker_id(worker_id) {}
+
+ void set_from_region(ShenandoahHeapRegion* from_region) {
+ _from_region = from_region;
+ _from_affiliation = from_region->affiliation();
+ if (_from_region->has_live()) {
+ if (_from_affiliation == ShenandoahRegionAffiliation::OLD_GENERATION) {
+ if (_old_to_region == nullptr) {
+ _old_to_region = from_region;
+ _old_compact_point = from_region->bottom();
+ }
+ } else {
+ assert(_from_affiliation == ShenandoahRegionAffiliation::YOUNG_GENERATION, "from_region must be OLD or YOUNG");
+ if (_young_to_region == nullptr) {
+ _young_to_region = from_region;
+ _young_compact_point = from_region->bottom();
+ }
+ }
+ } // else, we won't iterate over this _from_region so we don't need to set up to region to hold copies
+ }
+
+ void finish() {
+ finish_old_region();
+ finish_young_region();
+ }
+
+ void finish_old_region() {
+ if (_old_to_region != nullptr) {
+ log_debug(gc)("Planned compaction into Old Region " SIZE_FORMAT ", used: " SIZE_FORMAT " tabulated by worker %u",
+ _old_to_region->index(), _old_compact_point - _old_to_region->bottom(), _worker_id);
+ _old_to_region->set_new_top(_old_compact_point);
+ _old_to_region = nullptr;
+ }
+ }
+
+ void finish_young_region() {
+ if (_young_to_region != nullptr) {
+ log_debug(gc)("Worker %u planned compaction into Young Region " SIZE_FORMAT ", used: " SIZE_FORMAT,
+ _worker_id, _young_to_region->index(), _young_compact_point - _young_to_region->bottom());
+ _young_to_region->set_new_top(_young_compact_point);
+ _young_to_region = nullptr;
+ }
+ }
+
+ bool is_compact_same_region() {
+ return (_from_region == _old_to_region) || (_from_region == _young_to_region);
+ }
+
+ int empty_regions_pos() {
+ return _empty_regions_pos;
+ }
+
+ void do_object(oop p) {
+ assert(_from_region != nullptr, "must set before work");
+ assert((_from_region->bottom() <= cast_from_oop<HeapWord*>(p)) && (cast_from_oop<HeapWord*>(p) < _from_region->top()),
+ "Object must reside in _from_region");
+ assert(_heap->complete_marking_context()->is_marked(p), "must be marked");
+ assert(!_heap->complete_marking_context()->allocated_after_mark_start(p), "must be truly marked");
+
+ size_t obj_size = p->size();
+ uint from_region_age = _from_region->age();
+ uint object_age = p->age();
+
+ bool promote_object = false;
+ if ((_from_affiliation == ShenandoahRegionAffiliation::YOUNG_GENERATION) &&
+ (from_region_age + object_age >= InitialTenuringThreshold)) {
+ if ((_old_to_region != nullptr) && (_old_compact_point + obj_size > _old_to_region->end())) {
+ finish_old_region();
+ _old_to_region = nullptr;
+ }
+ if (_old_to_region == nullptr) {
+ if (_empty_regions_pos < _empty_regions.length()) {
+ ShenandoahHeapRegion* new_to_region = _empty_regions.at(_empty_regions_pos);
+ _empty_regions_pos++;
+ new_to_region->set_affiliation(OLD_GENERATION);
+ _old_to_region = new_to_region;
+ _old_compact_point = _old_to_region->bottom();
+ promote_object = true;
+ }
+ // Else this worker thread does not yet have any empty regions into which this aged object can be promoted so
+ // we leave promote_object as false, deferring the promotion.
+ } else {
+ promote_object = true;
+ }
+ }
+
+ if (promote_object || (_from_affiliation == ShenandoahRegionAffiliation::OLD_GENERATION)) {
+ assert(_old_to_region != nullptr, "_old_to_region should not be nullptr when evacuating to OLD region");
+ if (_old_compact_point + obj_size > _old_to_region->end()) {
+ ShenandoahHeapRegion* new_to_region;
+
+ log_debug(gc)("Worker %u finishing old region " SIZE_FORMAT ", compact_point: " PTR_FORMAT ", obj_size: " SIZE_FORMAT
+ ", &compact_point[obj_size]: " PTR_FORMAT ", region end: " PTR_FORMAT, _worker_id, _old_to_region->index(),
+ p2i(_old_compact_point), obj_size, p2i(_old_compact_point + obj_size), p2i(_old_to_region->end()));
+
+ // Object does not fit. Get a new _old_to_region.
+ finish_old_region();
+ if (_empty_regions_pos < _empty_regions.length()) {
+ new_to_region = _empty_regions.at(_empty_regions_pos);
+ _empty_regions_pos++;
+ new_to_region->set_affiliation(OLD_GENERATION);
+ } else {
+ // If we've exhausted the previously selected _old_to_region, we know that the _old_to_region is distinct
+ // from _from_region. That's because there is always room for _from_region to be compacted into itself.
+ // Since we're out of empty regions, let's use _from_region to hold the results of its own compaction.
+ new_to_region = _from_region;
+ }
+
+ assert(new_to_region != _old_to_region, "must not reuse same OLD to-region");
+ assert(new_to_region != nullptr, "must not be nullptr");
+ _old_to_region = new_to_region;
+ _old_compact_point = _old_to_region->bottom();
+ }
+
+ // Object fits into current region, record new location:
+ assert(_old_compact_point + obj_size <= _old_to_region->end(), "must fit");
+ shenandoah_assert_not_forwarded(nullptr, p);
+ _preserved_marks->push_if_necessary(p, p->mark());
+ p->forward_to(cast_to_oop(_old_compact_point));
+ _old_compact_point += obj_size;
+ } else {
+ assert(_from_affiliation == ShenandoahRegionAffiliation::YOUNG_GENERATION,
+ "_from_region must be OLD_GENERATION or YOUNG_GENERATION");
+ assert(_young_to_region != nullptr, "_young_to_region should not be nullptr when compacting YOUNG _from_region");
+
+ // After full gc compaction, all regions have age 0. Embed the region's age into the object's age in order to preserve
+ // tenuring progress.
+ if (_heap->is_aging_cycle()) {
+ _heap->increase_object_age(p, from_region_age + 1);
+ } else {
+ _heap->increase_object_age(p, from_region_age);
+ }
+
+ if (_young_compact_point + obj_size > _young_to_region->end()) {
+ ShenandoahHeapRegion* new_to_region;
+
+ log_debug(gc)("Worker %u finishing young region " SIZE_FORMAT ", compact_point: " PTR_FORMAT ", obj_size: " SIZE_FORMAT
+ ", &compact_point[obj_size]: " PTR_FORMAT ", region end: " PTR_FORMAT, _worker_id, _young_to_region->index(),
+ p2i(_young_compact_point), obj_size, p2i(_young_compact_point + obj_size), p2i(_young_to_region->end()));
+
+ // Object does not fit. Get a new _young_to_region.
+ finish_young_region();
+ if (_empty_regions_pos < _empty_regions.length()) {
+ new_to_region = _empty_regions.at(_empty_regions_pos);
+ _empty_regions_pos++;
+ new_to_region->set_affiliation(YOUNG_GENERATION);
+ } else {
+ // If we've exhausted the previously selected _young_to_region, we know that the _young_to_region is distinct
+ // from _from_region. That's because there is always room for _from_region to be compacted into itself.
+ // Since we're out of empty regions, let's use _from_region to hold the results of its own compaction.
+ new_to_region = _from_region;
+ }
+
+ assert(new_to_region != _young_to_region, "must not reuse same OLD to-region");
+ assert(new_to_region != nullptr, "must not be nullptr");
+ _young_to_region = new_to_region;
+ _young_compact_point = _young_to_region->bottom();
+ }
+
+ // Object fits into current region, record new location:
+ assert(_young_compact_point + obj_size <= _young_to_region->end(), "must fit");
+ shenandoah_assert_not_forwarded(nullptr, p);
+ _preserved_marks->push_if_necessary(p, p->mark());
+ p->forward_to(cast_to_oop(_young_compact_point));
+ _young_compact_point += obj_size;
+ }
+ }
+ };
+
+
class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
private:
PreservedMarks* const _preserved_marks;
ShenandoahHeap* const _heap;
GrowableArray<ShenandoahHeapRegion*>& _empty_regions;
_from_region = from_region;
}
void finish_region() {
assert(_to_region != nullptr, "should not happen");
+ assert(!_heap->mode()->is_generational(), "Generational GC should use different Closure");
_to_region->set_new_top(_compact_point);
}
bool is_compact_same_region() {
return _from_region == _to_region;
p->forward_to(cast_to_oop(_compact_point));
_compact_point += obj_size;
}
};
- class ShenandoahPrepareForCompactionTask : public WorkerTask {
- private:
- PreservedMarksSet* const _preserved_marks;
- ShenandoahHeap* const _heap;
- ShenandoahHeapRegionSet** const _worker_slices;
! public:
! ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks, ShenandoahHeapRegionSet **worker_slices) :
WorkerTask("Shenandoah Prepare For Compaction"),
! _preserved_marks(preserved_marks),
! _heap(ShenandoahHeap::heap()), _worker_slices(worker_slices) {
}
! static bool is_candidate_region(ShenandoahHeapRegion* r) {
! // Empty region: get it into the slice to defragment the slice itself.
! // We could have skipped this without violating correctness, but we really
- // want to compact all live regions to the start of the heap, which sometimes
- // means moving them into the fully empty regions.
- if (r->is_empty()) return true;
! // Can move the region, and this is not the humongous region. Humongous
- // moves are special cased here, because their moves are handled separately.
- return r->is_stw_move_allowed() && !r->is_humongous();
- }
! void work(uint worker_id) {
! ShenandoahParallelWorkerSession worker_session(worker_id);
! ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
! ShenandoahHeapRegionSetIterator it(slice);
! ShenandoahHeapRegion* from_region = it.next();
! // No work?
! if (from_region == nullptr) {
! return;
}
! // Sliding compaction. Walk all regions in the slice, and compact them.
! // Remember empty regions and reuse them as needed.
! ResourceMark rm;
!
! GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
!
ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
-
while (from_region != nullptr) {
assert(is_candidate_region(from_region), "Sanity");
-
cl.set_from_region(from_region);
if (from_region->has_live()) {
_heap->marked_object_iterate(from_region, &cl);
}
p->forward_to(cast_to_oop(_compact_point));
_compact_point += obj_size;
}
};
! ShenandoahPrepareForCompactionTask::ShenandoahPrepareForCompactionTask(PreservedMarksSet *preserved_marks,
! ShenandoahHeapRegionSet **worker_slices,
+ size_t num_workers) :
WorkerTask("Shenandoah Prepare For Compaction"),
! _preserved_marks(preserved_marks), _heap(ShenandoahHeap::heap()),
! _worker_slices(worker_slices), _num_workers(num_workers) { }
+
+
+ void ShenandoahPrepareForCompactionTask::work(uint worker_id) {
+ ShenandoahParallelWorkerSession worker_session(worker_id);
+ ShenandoahHeapRegionSet* slice = _worker_slices[worker_id];
+ ShenandoahHeapRegionSetIterator it(slice);
+ ShenandoahHeapRegion* from_region = it.next();
+ // No work?
+ if (from_region == nullptr) {
+ return;
}
! // Sliding compaction. Walk all regions in the slice, and compact them.
! // Remember empty regions and reuse them as needed.
! ResourceMark rm;
! GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
! if (_heap->mode()->is_generational()) {
! ShenandoahHeapRegion* old_to_region = (from_region->is_old())? from_region: nullptr;
! ShenandoahHeapRegion* young_to_region = (from_region->is_young())? from_region: nullptr;
! ShenandoahPrepareForGenerationalCompactionObjectClosure cl(this, _preserved_marks->get(worker_id), empty_regions,
! old_to_region, young_to_region, worker_id);
! while (from_region != nullptr) {
! assert(is_candidate_region(from_region), "Sanity");
! log_debug(gc)("Worker %u compacting %s Region " SIZE_FORMAT " which had used " SIZE_FORMAT " and %s live",
+ worker_id, affiliation_name(from_region->affiliation()),
+ from_region->index(), from_region->used(), from_region->has_live()? "has": "does not have");
+ cl.set_from_region(from_region);
+ if (from_region->has_live()) {
+ _heap->marked_object_iterate(from_region, &cl);
+ }
+ // Compacted the region to somewhere else? From-region is empty then.
+ if (!cl.is_compact_same_region()) {
+ empty_regions.append(from_region);
+ }
+ from_region = it.next();
}
+ cl.finish();
! // Mark all remaining regions as empty
! for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
! ShenandoahHeapRegion* r = empty_regions.at(pos);
! r->set_new_top(r->bottom());
! }
! } else {
ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
while (from_region != nullptr) {
assert(is_candidate_region(from_region), "Sanity");
cl.set_from_region(from_region);
if (from_region->has_live()) {
_heap->marked_object_iterate(from_region, &cl);
}
for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
ShenandoahHeapRegion* r = empty_regions.at(pos);
r->set_new_top(r->bottom());
}
}
! };
void ShenandoahFullGC::calculate_target_humongous_objects() {
ShenandoahHeap* heap = ShenandoahHeap::heap();
// Compute the new addresses for humongous objects. We need to do this after addresses
for (int pos = cl.empty_regions_pos(); pos < empty_regions.length(); ++pos) {
ShenandoahHeapRegion* r = empty_regions.at(pos);
r->set_new_top(r->bottom());
}
}
! }
void ShenandoahFullGC::calculate_target_humongous_objects() {
ShenandoahHeap* heap = ShenandoahHeap::heap();
// Compute the new addresses for humongous objects. We need to do this after addresses
// detected, then sliding restarts towards that non-movable region.
size_t to_begin = heap->num_regions();
size_t to_end = heap->num_regions();
+ log_debug(gc)("Full GC calculating target humongous objects from end " SIZE_FORMAT, to_end);
for (size_t c = heap->num_regions(); c > 0; c--) {
ShenandoahHeapRegion *r = heap->get_region(c - 1);
if (r->is_humongous_continuation() || (r->new_top() == r->bottom())) {
// To-region candidate: record this, and continue scan
to_begin = r->index();
ShenandoahHeap* const _heap;
public:
ShenandoahEnsureHeapActiveClosure() : _heap(ShenandoahHeap::heap()) {}
void heap_region_do(ShenandoahHeapRegion* r) {
+ bool is_generational = _heap->mode()->is_generational();
if (r->is_trash()) {
r->recycle();
}
if (r->is_cset()) {
+ // Leave afffiliation unchanged.
r->make_regular_bypass();
}
if (r->is_empty_uncommitted()) {
r->make_committed_bypass();
}
ShenandoahTrashImmediateGarbageClosure() :
_heap(ShenandoahHeap::heap()),
_ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
void heap_region_do(ShenandoahHeapRegion* r) {
! if (r->is_humongous_start()) {
! oop humongous_obj = cast_to_oop(r->bottom());
! if (!_ctx->is_marked(humongous_obj)) {
! assert(!r->has_live(),
! "Region " SIZE_FORMAT " is not marked, should not have live", r->index());
! _heap->trash_humongous_region_at(r);
! } else {
! assert(r->has_live(),
! "Region " SIZE_FORMAT " should have live", r->index());
! }
! } else if (r->is_humongous_continuation()) {
! // If we hit continuation, the non-live humongous starts should have been trashed already
! assert(r->humongous_start_region()->has_live(),
! "Region " SIZE_FORMAT " should have live", r->index());
! } else if (r->is_regular()) {
! if (!r->has_live()) {
! r->make_trash_immediate();
}
}
}
};
void ShenandoahFullGC::distribute_slices(ShenandoahHeapRegionSet** worker_slices) {
ShenandoahHeap* heap = ShenandoahHeap::heap();
ShenandoahTrashImmediateGarbageClosure() :
_heap(ShenandoahHeap::heap()),
_ctx(ShenandoahHeap::heap()->complete_marking_context()) {}
void heap_region_do(ShenandoahHeapRegion* r) {
! if (r->affiliation() != FREE) {
! if (r->is_humongous_start()) {
! oop humongous_obj = cast_to_oop(r->bottom());
! if (!_ctx->is_marked(humongous_obj)) {
! assert(!r->has_live(),
! "Humongous Start %s Region " SIZE_FORMAT " is not marked, should not have live",
! affiliation_name(r->affiliation()), r->index());
! log_debug(gc)("Trashing immediate humongous region " SIZE_FORMAT " because not marked", r->index());
! _heap->trash_humongous_region_at(r);
! } else {
! assert(r->has_live(),
! "Humongous Start %s Region " SIZE_FORMAT " should have live", affiliation_name(r->affiliation()), r->index());
! }
! } else if (r->is_humongous_continuation()) {
! // If we hit continuation, the non-live humongous starts should have been trashed already
! assert(r->humongous_start_region()->has_live(),
! "Humongous Continuation %s Region " SIZE_FORMAT " should have live", affiliation_name(r->affiliation()), r->index());
+ } else if (r->is_regular()) {
+ if (!r->has_live()) {
+ log_debug(gc)("Trashing immediate regular region " SIZE_FORMAT " because has no live", r->index());
+ r->make_trash_immediate();
+ }
}
}
+ // else, ignore this FREE region.
+ // TODO: change iterators so they do not process FREE regions.
}
};
void ShenandoahFullGC::distribute_slices(ShenandoahHeapRegionSet** worker_slices) {
ShenandoahHeap* heap = ShenandoahHeap::heap();
// This is needed because we are potentially sliding the data through them.
ShenandoahEnsureHeapActiveClosure ecl;
heap->heap_region_iterate(&ecl);
}
// Compute the new addresses for regular objects
{
ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
distribute_slices(worker_slices);
! ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices);
heap->workers()->run_task(&task);
}
// Compute the new addresses for humongous objects
{
// This is needed because we are potentially sliding the data through them.
ShenandoahEnsureHeapActiveClosure ecl;
heap->heap_region_iterate(&ecl);
}
+ if (heap->mode()->is_generational()) {
+ heap->young_generation()->clear_used();
+ heap->old_generation()->clear_used();
+ }
+
// Compute the new addresses for regular objects
{
ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
distribute_slices(worker_slices);
! size_t num_workers = heap->max_workers();
+
+ ResourceMark rm;
+ ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices, num_workers);
heap->workers()->run_task(&task);
}
// Compute the new addresses for humongous objects
{
ShenandoahHeapRegion* r = _regions.next();
while (r != nullptr) {
if (!r->is_humongous_continuation() && r->has_live()) {
_heap->marked_object_iterate(r, &obj_cl);
}
+ if (r->is_pinned() && r->is_old() && r->is_active() && !r->is_humongous()) {
+ // Pinned regions are not compacted so they may still hold unmarked objects with
+ // reference to reclaimed memory. Remembered set scanning will crash if it attempts
+ // to iterate the oops in these objects.
+ r->begin_preemptible_coalesce_and_fill();
+ r->oop_fill_and_coalesce_wo_cancel();
+ }
r = _regions.next();
}
}
};
_heap->free_set()->clear();
}
void heap_region_do(ShenandoahHeapRegion* r) {
assert (!r->is_cset(), "cset regions should have been demoted already");
+ bool is_generational = _heap->mode()->is_generational();
// Need to reset the complete-top-at-mark-start pointer here because
// the complete marking bitmap is no longer valid. This ensures
// size-based iteration in marked_object_iterate().
// NOTE: See blurb at ShenandoahMCResetCompleteBitmapTask on why we need to skip
size_t live = r->used();
// Make empty regions that have been allocated into regular
if (r->is_empty() && live > 0) {
+ if (!is_generational) {
+ r->make_young_maybe();
+ }
+ // else, generational mode compaction has already established affiliation.
r->make_regular_bypass();
}
// Reclaim regular regions that became empty
if (r->is_regular() && live == 0) {
if (r->is_trash()) {
live = 0;
r->recycle();
}
+ // Update final usage for generations
+ if (is_generational && live != 0) {
+ if (r->is_young()) {
+ _heap->young_generation()->increase_used(live);
+ } else if (r->is_old()) {
+ _heap->old_generation()->increase_used(live);
+ }
+ }
+
r->set_live_data(live);
r->reset_alloc_metadata();
_live += live;
}
size_t new_start = heap->heap_region_index_containing(old_obj->forwardee());
size_t new_end = new_start + num_regions - 1;
assert(old_start != new_start, "must be real move");
assert(r->is_stw_move_allowed(), "Region " SIZE_FORMAT " should be movable", r->index());
! Copy::aligned_conjoint_words(r->bottom(), heap->get_region(new_start)->bottom(), words_size);
! ContinuationGCSupport::relativize_stack_chunk(cast_to_oop<HeapWord*>(r->bottom()));
oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom());
new_obj->init_mark();
{
for (size_t c = old_start; c <= old_end; c++) {
ShenandoahHeapRegion* r = heap->get_region(c);
r->make_regular_bypass();
r->set_top(r->bottom());
}
for (size_t c = new_start; c <= new_end; c++) {
ShenandoahHeapRegion* r = heap->get_region(c);
if (c == new_start) {
! r->make_humongous_start_bypass();
} else {
! r->make_humongous_cont_bypass();
}
// Trailing region may be non-full, record the remainder there
size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
if ((c == new_end) && (remainder != 0)) {
size_t new_start = heap->heap_region_index_containing(old_obj->forwardee());
size_t new_end = new_start + num_regions - 1;
assert(old_start != new_start, "must be real move");
assert(r->is_stw_move_allowed(), "Region " SIZE_FORMAT " should be movable", r->index());
! ContinuationGCSupport::relativize_stack_chunk(cast_to_oop<HeapWord*>(heap->get_region(old_start)->bottom()));
! log_debug(gc)("Full GC compaction moves humongous object from region " SIZE_FORMAT " to region " SIZE_FORMAT,
+ old_start, new_start);
+
+ Copy::aligned_conjoint_words(heap->get_region(old_start)->bottom(),
+ heap->get_region(new_start)->bottom(),
+ words_size);
oop new_obj = cast_to_oop(heap->get_region(new_start)->bottom());
new_obj->init_mark();
{
+ ShenandoahRegionAffiliation original_affiliation = r->affiliation();
for (size_t c = old_start; c <= old_end; c++) {
ShenandoahHeapRegion* r = heap->get_region(c);
+ // Leave humongous region affiliation unchanged.
r->make_regular_bypass();
r->set_top(r->bottom());
}
for (size_t c = new_start; c <= new_end; c++) {
ShenandoahHeapRegion* r = heap->get_region(c);
if (c == new_start) {
! r->make_humongous_start_bypass(original_affiliation);
} else {
! r->make_humongous_cont_bypass(original_affiliation);
}
// Trailing region may be non-full, record the remainder there
size_t remainder = words_size & ShenandoahHeapRegion::region_size_words_mask();
if ((c == new_end) && (remainder != 0)) {
// Bring regions in proper states after the collection, and set heap properties.
{
ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);
ShenandoahPostCompactClosure post_compact;
heap->heap_region_iterate(&post_compact);
heap->set_used(post_compact.get_live());
heap->collection_set()->clear();
heap->free_set()->rebuild();
}
! heap->clear_cancelled_gc();
}
// Bring regions in proper states after the collection, and set heap properties.
{
ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_rebuild);
+ if (heap->mode()->is_generational()) {
+ heap->young_generation()->clear_used();
+ heap->old_generation()->clear_used();
+ }
+
ShenandoahPostCompactClosure post_compact;
heap->heap_region_iterate(&post_compact);
heap->set_used(post_compact.get_live());
+ if (heap->mode()->is_generational()) {
+ log_info(gc)("FullGC done: GLOBAL usage: " SIZE_FORMAT ", young usage: " SIZE_FORMAT ", old usage: " SIZE_FORMAT,
+ post_compact.get_live(), heap->young_generation()->used(), heap->old_generation()->used());
+ }
heap->collection_set()->clear();
heap->free_set()->rebuild();
}
! heap->clear_cancelled_gc(true /* clear oom handler */);
}
< prev index next >