Path: blob/master/src/hotspot/share/gc/shenandoah/shenandoahDegeneratedGC.cpp
66644 views
/*1* Copyright (c) 2021, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"2526#include "gc/shared/collectorCounters.hpp"27#include "gc/shenandoah/shenandoahCollectorPolicy.hpp"28#include "gc/shenandoah/shenandoahConcurrentMark.hpp"29#include "gc/shenandoah/shenandoahDegeneratedGC.hpp"30#include "gc/shenandoah/shenandoahFullGC.hpp"31#include "gc/shenandoah/shenandoahHeap.inline.hpp"32#include "gc/shenandoah/shenandoahMetrics.hpp"33#include "gc/shenandoah/shenandoahMonitoringSupport.hpp"34#include "gc/shenandoah/shenandoahOopClosures.inline.hpp"35#include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"36#include "gc/shenandoah/shenandoahSTWMark.hpp"37#include "gc/shenandoah/shenandoahUtils.hpp"38#include "gc/shenandoah/shenandoahVerifier.hpp"39#include "gc/shenandoah/shenandoahWorkerPolicy.hpp"40#include "gc/shenandoah/shenandoahVMOperations.hpp"41#include "runtime/vmThread.hpp"42#include "utilities/events.hpp"4344ShenandoahDegenGC::ShenandoahDegenGC(ShenandoahDegenPoint degen_point) :45ShenandoahGC(),46_degen_point(degen_point) {47}4849bool ShenandoahDegenGC::collect(GCCause::Cause cause) {50vmop_degenerated();51return true;52}5354void ShenandoahDegenGC::vmop_degenerated() {55TraceCollectorStats tcs(ShenandoahHeap::heap()->monitoring_support()->full_stw_collection_counters());56ShenandoahTimingsTracker timing(ShenandoahPhaseTimings::degen_gc_gross);57VM_ShenandoahDegeneratedGC degenerated_gc(this);58VMThread::execute(°enerated_gc);59}6061void ShenandoahDegenGC::entry_degenerated() {62const char* msg = degen_event_message(_degen_point);63ShenandoahPausePhase gc_phase(msg, ShenandoahPhaseTimings::degen_gc, true /* log_heap_usage */);64EventMark em("%s", msg);65ShenandoahHeap* const heap = ShenandoahHeap::heap();6667ShenandoahWorkerScope scope(heap->workers(),68ShenandoahWorkerPolicy::calc_workers_for_stw_degenerated(),69"stw degenerated gc");7071heap->set_degenerated_gc_in_progress(true);72op_degenerated();73heap->set_degenerated_gc_in_progress(false);74}7576void ShenandoahDegenGC::op_degenerated() {77ShenandoahHeap* const heap = ShenandoahHeap::heap();78// Degenerated GC is STW, but it can also fail. Current mechanics communicates79// GC failure via cancelled_concgc() flag. So, if we detect the failure after80// some phase, we have to upgrade the Degenerate GC to Full GC.81heap->clear_cancelled_gc();8283ShenandoahMetricsSnapshot metrics;84metrics.snap_before();8586switch (_degen_point) {87// The cases below form the Duff's-like device: it describes the actual GC cycle,88// but enters it at different points, depending on which concurrent phase had89// degenerated.9091case _degenerated_outside_cycle:92// We have degenerated from outside the cycle, which means something is bad with93// the heap, most probably heavy humongous fragmentation, or we are very low on free94// space. It makes little sense to wait for Full GC to reclaim as much as it can, when95// we can do the most aggressive degen cycle, which includes processing references and96// class unloading, unless those features are explicitly disabled.97//9899// Degenerated from concurrent root mark, reset the flag for STW mark100if (heap->is_concurrent_mark_in_progress()) {101ShenandoahConcurrentMark::cancel();102heap->set_concurrent_mark_in_progress(false);103}104105// Note that we can only do this for "outside-cycle" degens, otherwise we would risk106// changing the cycle parameters mid-cycle during concurrent -> degenerated handover.107heap->set_unload_classes(heap->heuristics()->can_unload_classes());108109op_reset();110111// STW mark112op_mark();113114case _degenerated_mark:115// No fallthrough. Continue mark, handed over from concurrent mark if116// concurrent mark has yet completed117if (_degen_point == ShenandoahDegenPoint::_degenerated_mark &&118heap->is_concurrent_mark_in_progress()) {119op_finish_mark();120}121assert(!heap->cancelled_gc(), "STW mark can not OOM");122123/* Degen select Collection Set. etc. */124op_prepare_evacuation();125126op_cleanup_early();127128case _degenerated_evac:129// If heuristics thinks we should do the cycle, this flag would be set,130// and we can do evacuation. Otherwise, it would be the shortcut cycle.131if (heap->is_evacuation_in_progress()) {132133// Degeneration under oom-evac protocol might have left some objects in134// collection set un-evacuated. Restart evacuation from the beginning to135// capture all objects. For all the objects that are already evacuated,136// it would be a simple check, which is supposed to be fast. This is also137// safe to do even without degeneration, as CSet iterator is at beginning138// in preparation for evacuation anyway.139//140// Before doing that, we need to make sure we never had any cset-pinned141// regions. This may happen if allocation failure happened when evacuating142// the about-to-be-pinned object, oom-evac protocol left the object in143// the collection set, and then the pin reached the cset region. If we continue144// the cycle here, we would trash the cset and alive objects in it. To avoid145// it, we fail degeneration right away and slide into Full GC to recover.146147{148heap->sync_pinned_region_status();149heap->collection_set()->clear_current_index();150151ShenandoahHeapRegion* r;152while ((r = heap->collection_set()->next()) != NULL) {153if (r->is_pinned()) {154heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc);155op_degenerated_fail();156return;157}158}159160heap->collection_set()->clear_current_index();161}162op_evacuate();163if (heap->cancelled_gc()) {164op_degenerated_fail();165return;166}167}168169// If heuristics thinks we should do the cycle, this flag would be set,170// and we need to do update-refs. Otherwise, it would be the shortcut cycle.171if (heap->has_forwarded_objects()) {172op_init_updaterefs();173assert(!heap->cancelled_gc(), "STW reference update can not OOM");174}175176case _degenerated_updaterefs:177if (heap->has_forwarded_objects()) {178op_updaterefs();179op_update_roots();180assert(!heap->cancelled_gc(), "STW reference update can not OOM");181}182183if (ClassUnloading) {184// Disarm nmethods that armed in concurrent cycle.185// In above case, update roots should disarm them186ShenandoahCodeRoots::disarm_nmethods();187}188189op_cleanup_complete();190break;191default:192ShouldNotReachHere();193}194195if (ShenandoahVerify) {196heap->verifier()->verify_after_degenerated();197}198199if (VerifyAfterGC) {200Universe::verify();201}202203metrics.snap_after();204205// Check for futility and fail. There is no reason to do several back-to-back Degenerated cycles,206// because that probably means the heap is overloaded and/or fragmented.207if (!metrics.is_good_progress()) {208heap->notify_gc_no_progress();209heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc);210op_degenerated_futile();211} else {212heap->notify_gc_progress();213}214}215216void ShenandoahDegenGC::op_reset() {217ShenandoahHeap::heap()->prepare_gc();218}219220void ShenandoahDegenGC::op_mark() {221assert(!ShenandoahHeap::heap()->is_concurrent_mark_in_progress(), "Should be reset");222ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_mark);223ShenandoahSTWMark mark(false /*full gc*/);224mark.clear();225mark.mark();226}227228void ShenandoahDegenGC::op_finish_mark() {229ShenandoahConcurrentMark mark;230mark.finish_mark();231}232233void ShenandoahDegenGC::op_prepare_evacuation() {234ShenandoahHeap* const heap = ShenandoahHeap::heap();235if (ShenandoahVerify) {236heap->verifier()->verify_roots_no_forwarded();237}238239// STW cleanup weak roots and unload classes240heap->parallel_cleaning(false /*full gc*/);241// Prepare regions and collection set242heap->prepare_regions_and_collection_set(false /*concurrent*/);243244// Retire the TLABs, which will force threads to reacquire their TLABs after the pause.245// This is needed for two reasons. Strong one: new allocations would be with new freeset,246// which would be outside the collection set, so no cset writes would happen there.247// Weaker one: new allocations would happen past update watermark, and so less work would248// be needed for reference updates (would update the large filler instead).249if (UseTLAB) {250ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_final_manage_labs);251heap->tlabs_retire(false);252}253254if (!heap->collection_set()->is_empty()) {255heap->set_evacuation_in_progress(true);256heap->set_has_forwarded_objects(true);257258if(ShenandoahVerify) {259heap->verifier()->verify_during_evacuation();260}261} else {262if (ShenandoahVerify) {263heap->verifier()->verify_after_concmark();264}265266if (VerifyAfterGC) {267Universe::verify();268}269}270}271272void ShenandoahDegenGC::op_cleanup_early() {273ShenandoahHeap::heap()->recycle_trash();274}275276void ShenandoahDegenGC::op_evacuate() {277ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_evac);278ShenandoahHeap::heap()->evacuate_collection_set(false /* concurrent*/);279}280281void ShenandoahDegenGC::op_init_updaterefs() {282// Evacuation has completed283ShenandoahHeap* const heap = ShenandoahHeap::heap();284heap->set_evacuation_in_progress(false);285heap->set_concurrent_weak_root_in_progress(false);286heap->set_concurrent_strong_root_in_progress(false);287288heap->prepare_update_heap_references(false /*concurrent*/);289heap->set_update_refs_in_progress(true);290}291292void ShenandoahDegenGC::op_updaterefs() {293ShenandoahHeap* const heap = ShenandoahHeap::heap();294ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_updaterefs);295// Handed over from concurrent update references phase296heap->update_heap_references(false /*concurrent*/);297298heap->set_update_refs_in_progress(false);299heap->set_has_forwarded_objects(false);300}301302void ShenandoahDegenGC::op_update_roots() {303ShenandoahHeap* const heap = ShenandoahHeap::heap();304305update_roots(false /*full_gc*/);306307heap->update_heap_region_states(false /*concurrent*/);308309if (ShenandoahVerify) {310heap->verifier()->verify_after_updaterefs();311}312313if (VerifyAfterGC) {314Universe::verify();315}316317heap->rebuild_free_set(false /*concurrent*/);318}319320void ShenandoahDegenGC::op_cleanup_complete() {321ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_cleanup_complete);322ShenandoahHeap::heap()->recycle_trash();323}324325void ShenandoahDegenGC::op_degenerated_fail() {326log_info(gc)("Cannot finish degeneration, upgrading to Full GC");327ShenandoahHeap::heap()->shenandoah_policy()->record_degenerated_upgrade_to_full();328329ShenandoahFullGC full_gc;330full_gc.op_full(GCCause::_shenandoah_upgrade_to_full_gc);331}332333void ShenandoahDegenGC::op_degenerated_futile() {334ShenandoahHeap::heap()->shenandoah_policy()->record_degenerated_upgrade_to_full();335ShenandoahFullGC full_gc;336full_gc.op_full(GCCause::_shenandoah_upgrade_to_full_gc);337}338339const char* ShenandoahDegenGC::degen_event_message(ShenandoahDegenPoint point) const {340switch (point) {341case _degenerated_unset:342return "Pause Degenerated GC (<UNSET>)";343case _degenerated_outside_cycle:344return "Pause Degenerated GC (Outside of Cycle)";345case _degenerated_mark:346return "Pause Degenerated GC (Mark)";347case _degenerated_evac:348return "Pause Degenerated GC (Evacuation)";349case _degenerated_updaterefs:350return "Pause Degenerated GC (Update Refs)";351default:352ShouldNotReachHere();353return "ERROR";354}355}356357358