Path: blob/master/src/hotspot/share/gc/shenandoah/shenandoahDegeneratedGC.cpp
40961 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 mark116if (_degen_point == ShenandoahDegenPoint::_degenerated_mark) {117op_finish_mark();118}119assert(!heap->cancelled_gc(), "STW mark can not OOM");120121/* Degen select Collection Set. etc. */122op_prepare_evacuation();123124op_cleanup_early();125126case _degenerated_evac:127// If heuristics thinks we should do the cycle, this flag would be set,128// and we can do evacuation. Otherwise, it would be the shortcut cycle.129if (heap->is_evacuation_in_progress()) {130131// Degeneration under oom-evac protocol might have left some objects in132// collection set un-evacuated. Restart evacuation from the beginning to133// capture all objects. For all the objects that are already evacuated,134// it would be a simple check, which is supposed to be fast. This is also135// safe to do even without degeneration, as CSet iterator is at beginning136// in preparation for evacuation anyway.137//138// Before doing that, we need to make sure we never had any cset-pinned139// regions. This may happen if allocation failure happened when evacuating140// the about-to-be-pinned object, oom-evac protocol left the object in141// the collection set, and then the pin reached the cset region. If we continue142// the cycle here, we would trash the cset and alive objects in it. To avoid143// it, we fail degeneration right away and slide into Full GC to recover.144145{146heap->sync_pinned_region_status();147heap->collection_set()->clear_current_index();148149ShenandoahHeapRegion* r;150while ((r = heap->collection_set()->next()) != NULL) {151if (r->is_pinned()) {152heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc);153op_degenerated_fail();154return;155}156}157158heap->collection_set()->clear_current_index();159}160op_evacuate();161if (heap->cancelled_gc()) {162op_degenerated_fail();163return;164}165}166167// If heuristics thinks we should do the cycle, this flag would be set,168// and we need to do update-refs. Otherwise, it would be the shortcut cycle.169if (heap->has_forwarded_objects()) {170op_init_updaterefs();171assert(!heap->cancelled_gc(), "STW reference update can not OOM");172}173174case _degenerated_updaterefs:175if (heap->has_forwarded_objects()) {176op_updaterefs();177op_update_roots();178assert(!heap->cancelled_gc(), "STW reference update can not OOM");179}180181if (ClassUnloading) {182// Disarm nmethods that armed in concurrent cycle.183// In above case, update roots should disarm them184ShenandoahCodeRoots::disarm_nmethods();185}186187op_cleanup_complete();188break;189default:190ShouldNotReachHere();191}192193if (ShenandoahVerify) {194heap->verifier()->verify_after_degenerated();195}196197if (VerifyAfterGC) {198Universe::verify();199}200201metrics.snap_after();202203// Check for futility and fail. There is no reason to do several back-to-back Degenerated cycles,204// because that probably means the heap is overloaded and/or fragmented.205if (!metrics.is_good_progress()) {206heap->notify_gc_no_progress();207heap->cancel_gc(GCCause::_shenandoah_upgrade_to_full_gc);208op_degenerated_futile();209} else {210heap->notify_gc_progress();211}212}213214void ShenandoahDegenGC::op_reset() {215ShenandoahHeap::heap()->prepare_gc();216}217218void ShenandoahDegenGC::op_mark() {219assert(!ShenandoahHeap::heap()->is_concurrent_mark_in_progress(), "Should be reset");220ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_mark);221ShenandoahSTWMark mark(false /*full gc*/);222mark.clear();223mark.mark();224}225226void ShenandoahDegenGC::op_finish_mark() {227ShenandoahConcurrentMark mark;228mark.finish_mark();229}230231void ShenandoahDegenGC::op_prepare_evacuation() {232ShenandoahHeap* const heap = ShenandoahHeap::heap();233if (ShenandoahVerify) {234heap->verifier()->verify_roots_no_forwarded();235}236237// STW cleanup weak roots and unload classes238heap->parallel_cleaning(false /*full gc*/);239// Prepare regions and collection set240heap->prepare_regions_and_collection_set(false /*concurrent*/);241242// Retire the TLABs, which will force threads to reacquire their TLABs after the pause.243// This is needed for two reasons. Strong one: new allocations would be with new freeset,244// which would be outside the collection set, so no cset writes would happen there.245// Weaker one: new allocations would happen past update watermark, and so less work would246// be needed for reference updates (would update the large filler instead).247if (UseTLAB) {248ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_final_manage_labs);249heap->tlabs_retire(false);250}251252if (!heap->collection_set()->is_empty()) {253heap->set_evacuation_in_progress(true);254heap->set_has_forwarded_objects(true);255256if(ShenandoahVerify) {257heap->verifier()->verify_during_evacuation();258}259} else {260if (ShenandoahVerify) {261heap->verifier()->verify_after_concmark();262}263264if (VerifyAfterGC) {265Universe::verify();266}267}268}269270void ShenandoahDegenGC::op_cleanup_early() {271ShenandoahHeap::heap()->recycle_trash();272}273274void ShenandoahDegenGC::op_evacuate() {275ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_stw_evac);276ShenandoahHeap::heap()->evacuate_collection_set(false /* concurrent*/);277}278279void ShenandoahDegenGC::op_init_updaterefs() {280// Evacuation has completed281ShenandoahHeap* const heap = ShenandoahHeap::heap();282heap->set_evacuation_in_progress(false);283heap->set_concurrent_weak_root_in_progress(false);284heap->set_concurrent_strong_root_in_progress(false);285286heap->prepare_update_heap_references(false /*concurrent*/);287heap->set_update_refs_in_progress(true);288}289290void ShenandoahDegenGC::op_updaterefs() {291ShenandoahHeap* const heap = ShenandoahHeap::heap();292ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_updaterefs);293// Handed over from concurrent update references phase294heap->update_heap_references(false /*concurrent*/);295296heap->set_update_refs_in_progress(false);297heap->set_has_forwarded_objects(false);298}299300void ShenandoahDegenGC::op_update_roots() {301ShenandoahHeap* const heap = ShenandoahHeap::heap();302303update_roots(false /*full_gc*/);304305heap->update_heap_region_states(false /*concurrent*/);306307if (ShenandoahVerify) {308heap->verifier()->verify_after_updaterefs();309}310311if (VerifyAfterGC) {312Universe::verify();313}314315heap->rebuild_free_set(false /*concurrent*/);316}317318void ShenandoahDegenGC::op_cleanup_complete() {319ShenandoahGCPhase phase(ShenandoahPhaseTimings::degen_gc_cleanup_complete);320ShenandoahHeap::heap()->recycle_trash();321}322323void ShenandoahDegenGC::op_degenerated_fail() {324log_info(gc)("Cannot finish degeneration, upgrading to Full GC");325ShenandoahHeap::heap()->shenandoah_policy()->record_degenerated_upgrade_to_full();326327ShenandoahFullGC full_gc;328full_gc.op_full(GCCause::_shenandoah_upgrade_to_full_gc);329}330331void ShenandoahDegenGC::op_degenerated_futile() {332ShenandoahHeap::heap()->shenandoah_policy()->record_degenerated_upgrade_to_full();333ShenandoahFullGC full_gc;334full_gc.op_full(GCCause::_shenandoah_upgrade_to_full_gc);335}336337const char* ShenandoahDegenGC::degen_event_message(ShenandoahDegenPoint point) const {338switch (point) {339case _degenerated_unset:340return "Pause Degenerated GC (<UNSET>)";341case _degenerated_outside_cycle:342return "Pause Degenerated GC (Outside of Cycle)";343case _degenerated_mark:344return "Pause Degenerated GC (Mark)";345case _degenerated_evac:346return "Pause Degenerated GC (Evacuation)";347case _degenerated_updaterefs:348return "Pause Degenerated GC (Update Refs)";349default:350ShouldNotReachHere();351return "ERROR";352}353}354355356