< prev index next > src/hotspot/share/gc/shenandoah/shenandoahFullGC.cpp
Print this page
/*
* Copyright (c) 2014, 2021, Red Hat, Inc. All rights reserved.
+ * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
#include "gc/shared/gcTraceTime.inline.hpp"
#include "gc/shared/preservedMarks.inline.hpp"
#include "gc/shared/tlab_globals.hpp"
#include "gc/shared/workerThread.hpp"
#include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
+ #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
#include "gc/shenandoah/shenandoahConcurrentGC.hpp"
#include "gc/shenandoah/shenandoahCollectionSet.hpp"
+ #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
#include "gc/shenandoah/shenandoahFreeSet.hpp"
#include "gc/shenandoah/shenandoahFullGC.hpp"
+ #include "gc/shenandoah/shenandoahGenerationalFullGC.hpp"
+ #include "gc/shenandoah/shenandoahGlobalGeneration.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/shenandoahWorkerPolicy.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/orderAccess.hpp"
#include "runtime/vmThread.hpp"
#include "utilities/copy.hpp"
#include "utilities/events.hpp"
#include "utilities/growableArray.hpp"
metrics.snap_before();
// Perform full GC
do_it(cause);
+ ShenandoahHeap* const heap = ShenandoahHeap::heap();
+
+ if (heap->mode()->is_generational()) {
+ ShenandoahGenerationalFullGC::handle_completion(heap);
+ }
+
metrics.snap_after();
if (metrics.is_good_progress()) {
- ShenandoahHeap::heap()->notify_gc_progress();
+ heap->notify_gc_progress();
} else {
// Nothing to do. Tell the allocation path that we have failed to make
// progress, and it can finally fail.
- ShenandoahHeap::heap()->notify_gc_no_progress();
+ heap->notify_gc_no_progress();
}
+
+ // Regardless if progress was made, we record that we completed a "successful" full GC.
+ heap->global_generation()->heuristics()->record_success_full();
+ heap->shenandoah_policy()->record_success_full();
}
void ShenandoahFullGC::do_it(GCCause::Cause gc_cause) {
ShenandoahHeap* heap = ShenandoahHeap::heap();
+ if (heap->mode()->is_generational()) {
+ ShenandoahGenerationalFullGC::prepare();
+ }
+
if (ShenandoahVerify) {
heap->verifier()->verify_before_fullgc();
}
if (VerifyBeforeGC) {
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
+ // b. Cancel all concurrent marks, if in progress
if (heap->is_concurrent_mark_in_progress()) {
- ShenandoahConcurrentGC::cancel();
- heap->set_concurrent_mark_in_progress(false);
+ // TODO: Send cancel_concurrent_mark upstream? Does it really not have it already?
+ 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->reset_mark_bitmap();
+ heap->global_generation()->reset_mark_bitmap();
assert(heap->marking_context()->is_bitmap_clear(), "sanity");
- assert(!heap->marking_context()->is_complete(), "sanity");
+ assert(!heap->global_generation()->is_mark_complete(), "sanity");
// e. Abandon reference discovery and clear all discovered references.
- ShenandoahReferenceProcessor* rp = heap->ref_processor();
+ 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();
+ if (heap->mode()->is_generational()) {
+ ShenandoahGenerationalFullGC::restore_top_before_promote(heap);
+ }
+
// The rest of prologue:
_preserved_marks->init(heap->workers()->active_workers());
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();
OrderAccess::fence();
phase3_update_references();
phase4_compact_objects(worker_slices);
- }
- {
- // Epilogue
- _preserved_marks->restore(heap->workers());
- _preserved_marks->reclaim();
+ phase5_epilog();
}
// Resize metaspace
MetaspaceGC::compute_new_size();
if (ShenandoahVerify) {
heap->verifier()->verify_after_fullgc();
}
+ // Humongous regions are promoted on demand and are accounted for by normal Full GC mechanisms.
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();
+ // TODO: Add API to heap to skip free regions
+ if (r->is_affiliated()) {
+ _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->heap_region_iterate(&cl);
+ heap->parallel_heap_region_iterate(&cl);
- heap->set_unload_classes(heap->heuristics()->can_unload_classes());
+ heap->set_unload_classes(heap->global_generation()->heuristics()->can_unload_classes());
- ShenandoahReferenceProcessor* rp = heap->ref_processor();
+ ShenandoahReferenceProcessor* rp = heap->global_generation()->ref_processor();
// enable ("weak") refs discovery
rp->set_soft_reference_policy(true); // forcefully purge all soft references
- ShenandoahSTWMark mark(true /*full_gc*/);
+ ShenandoahSTWMark mark(heap->global_generation(), true /*full_gc*/);
mark.mark();
heap->parallel_cleaning(true /* full_gc */);
+
+ if (ShenandoahHeap::heap()->mode()->is_generational()) {
+ ShenandoahGenerationalFullGC::log_live_in_old(heap);
+ }
}
class ShenandoahPrepareForCompactionObjectClosure : public ObjectClosure {
private:
PreservedMarks* const _preserved_marks;
void set_from_region(ShenandoahHeapRegion* from_region) {
_from_region = from_region;
}
- void finish_region() {
+ void finish() {
assert(_to_region != nullptr, "should not happen");
_to_region->set_new_top(_compact_point);
}
bool is_compact_same_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();
if (_compact_point + obj_size > _to_region->end()) {
- finish_region();
+ finish();
// Object doesn't fit. Pick next empty region and start compacting there.
ShenandoahHeapRegion* new_to_region;
if (_empty_regions_pos < _empty_regions.length()) {
new_to_region = _empty_regions.at(_empty_regions_pos);
// 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;
- }
+ void work(uint worker_id) override;
+ private:
+ template<typename ClosureType>
+ void prepare_for_compaction(ClosureType& cl,
+ GrowableArray<ShenandoahHeapRegion*>& empty_regions,
+ ShenandoahHeapRegionSetIterator& it,
+ ShenandoahHeapRegion* from_region);
+ };
- // Sliding compaction. Walk all regions in the slice, and compact them.
- // Remember empty regions and reuse them as needed.
- ResourceMark rm;
+ 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;
+ }
- GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
+ // Sliding compaction. Walk all regions in the slice, and compact them.
+ // Remember empty regions and reuse them as needed.
+ ResourceMark rm;
- ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
+ GrowableArray<ShenandoahHeapRegion*> empty_regions((int)_heap->num_regions());
- 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);
- }
+ if (_heap->mode()->is_generational()) {
+ ShenandoahPrepareForGenerationalCompactionObjectClosure cl(_preserved_marks->get(worker_id),
+ empty_regions, from_region, worker_id);
+ prepare_for_compaction(cl, empty_regions, it, from_region);
+ } else {
+ ShenandoahPrepareForCompactionObjectClosure cl(_preserved_marks->get(worker_id), empty_regions, from_region);
+ prepare_for_compaction(cl, empty_regions, it, from_region);
+ }
+ }
- // 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();
+ template<typename ClosureType>
+ void ShenandoahPrepareForCompactionTask::prepare_for_compaction(ClosureType& cl,
+ GrowableArray<ShenandoahHeapRegion*>& empty_regions,
+ ShenandoahHeapRegionSetIterator& it,
+ ShenandoahHeapRegion* 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);
}
- cl.finish_region();
- // 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());
+ // 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());
+ }
+ }
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();
void heap_region_do(ShenandoahHeapRegion* r) {
if (r->is_trash()) {
r->recycle();
}
if (r->is_cset()) {
+ // Leave affiliation 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_affiliated()) {
+ // Ignore free regions
+ // TODO: change iterators so they do not process FREE regions.
+ return;
+ }
+
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());
assert(is_distributed || !is_candidate, "All candidates are distributed: " SIZE_FORMAT, rid);
}
#endif
}
+ // TODO:
+ // Consider compacting old-gen objects toward the high end of memory and young-gen objects towards the low-end
+ // of memory. As currently implemented, all regions are compacted toward the low-end of memory. This creates more
+ // fragmentation of the heap, because old-gen regions get scattered among low-address regions such that it becomes
+ // more difficult to find contiguous regions for humongous objects.
void ShenandoahFullGC::phase2_calculate_target_addresses(ShenandoahHeapRegionSet** worker_slices) {
GCTraceTime(Info, gc, phases) time("Phase 2: Compute new object addresses", _gc_timer);
ShenandoahGCPhase calculate_address_phase(ShenandoahPhaseTimings::full_gc_calculate_addresses);
ShenandoahHeap* heap = ShenandoahHeap::heap();
{
ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_calculate_addresses_regular);
distribute_slices(worker_slices);
+ // TODO: This is ResourceMark is missing upstream.
+ ResourceMark rm;
ShenandoahPrepareForCompactionTask task(_preserved_marks, worker_slices);
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 (_heap->mode()->is_generational()) {
+ ShenandoahGenerationalFullGC::maybe_coalesce_and_fill_region(r);
+ }
r = _regions.next();
}
}
};
};
class ShenandoahPostCompactClosure : public ShenandoahHeapRegionClosure {
private:
ShenandoahHeap* const _heap;
- size_t _live;
+ bool _is_generational;
+ size_t _young_regions, _young_usage, _young_humongous_waste;
+ size_t _old_regions, _old_usage, _old_humongous_waste;
public:
- ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()), _live(0) {
+ ShenandoahPostCompactClosure() : _heap(ShenandoahHeap::heap()),
+ _is_generational(_heap->mode()->is_generational()),
+ _young_regions(0),
+ _young_usage(0),
+ _young_humongous_waste(0),
+ _old_regions(0),
+ _old_usage(0),
+ _old_humongous_waste(0)
+ {
_heap->free_set()->clear();
}
void heap_region_do(ShenandoahHeapRegion* r) {
assert (!r->is_cset(), "cset regions should have been demoted already");
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();
if (ZapUnusedHeapArea) {
SpaceMangler::mangle_region(MemRegion(r->top(), r->end()));
}
}
// Recycle all trash regions
if (r->is_trash()) {
live = 0;
r->recycle();
+ } else {
+ if (r->is_old()) {
+ ShenandoahGenerationalFullGC::account_for_region(r, _old_regions, _old_usage, _old_humongous_waste);
+ } else if (r->is_young()) {
+ ShenandoahGenerationalFullGC::account_for_region(r, _young_regions, _young_usage, _young_humongous_waste);
+ }
}
-
r->set_live_data(live);
r->reset_alloc_metadata();
- _live += live;
}
- size_t get_live() {
- return _live;
+ void update_generation_usage() {
+ if (_is_generational) {
+ _heap->old_generation()->establish_usage(_old_regions, _old_usage, _old_humongous_waste);
+ _heap->young_generation()->establish_usage(_young_regions, _young_usage, _young_humongous_waste);
+ } else {
+ assert(_old_regions == 0, "Old regions only expected in generational mode");
+ assert(_old_usage == 0, "Old usage only expected in generational mode");
+ assert(_old_humongous_waste == 0, "Old humongous waste only expected in generational mode");
+ }
+
+ // In generational mode, global usage should be the sum of young and old. This is also true
+ // for non-generational modes except that there are no old regions.
+ _heap->global_generation()->establish_usage(_old_regions + _young_regions,
+ _old_usage + _young_usage,
+ _old_humongous_waste + _young_humongous_waste);
}
};
void ShenandoahFullGC::compact_humongous_objects() {
// Compact humongous regions, based on their fwdptr objects.
//
// This code is serial, because doing the in-slice parallel sliding is tricky. In most cases,
// humongous regions are already compacted, and do not require further moves, which alleviates
- // sliding costs. We may consider doing this in parallel in future.
+ // sliding costs. We may consider doing this in parallel in the future.
ShenandoahHeap* heap = ShenandoahHeap::heap();
for (size_t c = heap->num_regions(); c > 0; c--) {
ShenandoahHeapRegion* r = heap->get_region(c - 1);
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());
+ 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(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();
{
+ ShenandoahAffiliation 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();
+ r->make_humongous_start_bypass(original_affiliation);
} else {
- r->make_humongous_cont_bypass();
+ 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)) {
// Compact humongous objects after regular object moves
{
ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_humong);
compact_humongous_objects();
}
+ }
+
+ void ShenandoahFullGC::phase5_epilog() {
+ GCTraceTime(Info, gc, phases) time("Phase 5: Full GC epilog", _gc_timer);
+ ShenandoahHeap* heap = ShenandoahHeap::heap();
// Reset complete bitmap. We're about to reset the complete-top-at-mark-start pointer
// and must ensure the bitmap is in sync.
{
ShenandoahGCPhase phase(ShenandoahPhaseTimings::full_gc_copy_objects_reset_complete);
}
// 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());
+ post_compact.update_generation_usage();
+
+ if (heap->mode()->is_generational()) {
+ ShenandoahGenerationalFullGC::balance_generations_after_gc(heap);
+ }
heap->collection_set()->clear();
- heap->free_set()->rebuild();
+ size_t young_cset_regions, old_cset_regions;
+ size_t first_old, last_old, num_old;
+ heap->free_set()->prepare_to_rebuild(young_cset_regions, old_cset_regions, first_old, last_old, num_old);
+
+ // We also do not expand old generation size following Full GC because we have scrambled age populations and
+ // no longer have objects separated by age into distinct regions.
+
+ // TODO: Do we need to fix FullGC so that it maintains aged segregation of objects into distinct regions?
+ // A partial solution would be to remember how many objects are of tenure age following Full GC, but
+ // this is probably suboptimal, because most of these objects will not reside in a region that will be
+ // selected for the next evacuation phase.
+
+
+ if (heap->mode()->is_generational()) {
+ ShenandoahGenerationalFullGC::compute_balances();
+ }
+
+ heap->free_set()->finish_rebuild(young_cset_regions, old_cset_regions, num_old);
+
+ heap->clear_cancelled_gc(true /* clear oom handler */);
}
- heap->clear_cancelled_gc();
+ _preserved_marks->restore(heap->workers());
+ _preserved_marks->reclaim();
+
+ // We defer generation resizing actions until after cset regions have been recycled. We do this even following an
+ // abbreviated cycle.
+ if (heap->mode()->is_generational()) {
+ ShenandoahGenerationalFullGC::balance_generations_after_rebuilding_free_set();
+ ShenandoahGenerationalFullGC::rebuild_remembered_set(heap);
+ }
}
< prev index next >