Path: blob/master/src/hotspot/share/gc/serial/genMarkSweep.cpp
41149 views
/*1* Copyright (c) 2001, 2021, Oracle and/or its affiliates. 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"25#include "classfile/classLoaderDataGraph.hpp"26#include "classfile/javaClasses.hpp"27#include "classfile/stringTable.hpp"28#include "classfile/symbolTable.hpp"29#include "classfile/systemDictionary.hpp"30#include "classfile/vmSymbols.hpp"31#include "code/codeCache.hpp"32#include "code/icBuffer.hpp"33#include "compiler/oopMap.hpp"34#include "gc/serial/genMarkSweep.hpp"35#include "gc/serial/serialGcRefProcProxyTask.hpp"36#include "gc/shared/collectedHeap.inline.hpp"37#include "gc/shared/gcHeapSummary.hpp"38#include "gc/shared/gcTimer.hpp"39#include "gc/shared/gcTrace.hpp"40#include "gc/shared/gcTraceTime.inline.hpp"41#include "gc/shared/genCollectedHeap.hpp"42#include "gc/shared/generation.hpp"43#include "gc/shared/genOopClosures.inline.hpp"44#include "gc/shared/modRefBarrierSet.hpp"45#include "gc/shared/referencePolicy.hpp"46#include "gc/shared/referenceProcessorPhaseTimes.hpp"47#include "gc/shared/space.hpp"48#include "gc/shared/strongRootsScope.hpp"49#include "gc/shared/weakProcessor.hpp"50#include "memory/universe.hpp"51#include "oops/instanceRefKlass.hpp"52#include "oops/oop.inline.hpp"53#include "prims/jvmtiExport.hpp"54#include "runtime/handles.inline.hpp"55#include "runtime/synchronizer.hpp"56#include "runtime/thread.inline.hpp"57#include "runtime/vmThread.hpp"58#include "utilities/copy.hpp"59#include "utilities/events.hpp"60#include "utilities/stack.inline.hpp"61#if INCLUDE_JVMCI62#include "jvmci/jvmci.hpp"63#endif6465void GenMarkSweep::invoke_at_safepoint(ReferenceProcessor* rp, bool clear_all_softrefs) {66assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");6768GenCollectedHeap* gch = GenCollectedHeap::heap();69#ifdef ASSERT70if (gch->soft_ref_policy()->should_clear_all_soft_refs()) {71assert(clear_all_softrefs, "Policy should have been checked earlier");72}73#endif7475// hook up weak ref data so it can be used during Mark-Sweep76assert(ref_processor() == NULL, "no stomping");77assert(rp != NULL, "should be non-NULL");78set_ref_processor(rp);79rp->setup_policy(clear_all_softrefs);8081gch->trace_heap_before_gc(_gc_tracer);8283// Increment the invocation count84_total_invocations++;8586// Capture used regions for each generation that will be87// subject to collection, so that card table adjustments can88// be made intelligently (see clear / invalidate further below).89gch->save_used_regions();9091allocate_stacks();9293mark_sweep_phase1(clear_all_softrefs);9495mark_sweep_phase2();9697// Don't add any more derived pointers during phase398#if COMPILER2_OR_JVMCI99assert(DerivedPointerTable::is_active(), "Sanity");100DerivedPointerTable::set_active(false);101#endif102103mark_sweep_phase3();104105mark_sweep_phase4();106107restore_marks();108109// Set saved marks for allocation profiler (and other things? -- dld)110// (Should this be in general part?)111gch->save_marks();112113deallocate_stacks();114115// If compaction completely evacuated the young generation then we116// can clear the card table. Otherwise, we must invalidate117// it (consider all cards dirty). In the future, we might consider doing118// compaction within generations only, and doing card-table sliding.119CardTableRS* rs = gch->rem_set();120Generation* old_gen = gch->old_gen();121122// Clear/invalidate below make use of the "prev_used_regions" saved earlier.123if (gch->young_gen()->used() == 0) {124// We've evacuated the young generation.125rs->clear_into_younger(old_gen);126} else {127// Invalidate the cards corresponding to the currently used128// region and clear those corresponding to the evacuated region.129rs->invalidate_or_clear(old_gen);130}131132gch->prune_scavengable_nmethods();133134// refs processing: clean slate135set_ref_processor(NULL);136137// Update heap occupancy information which is used as138// input to soft ref clearing policy at the next gc.139Universe::heap()->update_capacity_and_used_at_gc();140141// Signal that we have completed a visit to all live objects.142Universe::heap()->record_whole_heap_examined_timestamp();143144gch->trace_heap_after_gc(_gc_tracer);145}146147void GenMarkSweep::allocate_stacks() {148GenCollectedHeap* gch = GenCollectedHeap::heap();149// Scratch request on behalf of old generation; will do no allocation.150ScratchBlock* scratch = gch->gather_scratch(gch->old_gen(), 0);151152// $$$ To cut a corner, we'll only use the first scratch block, and then153// revert to malloc.154if (scratch != NULL) {155_preserved_count_max =156scratch->num_words * HeapWordSize / sizeof(PreservedMark);157} else {158_preserved_count_max = 0;159}160161_preserved_marks = (PreservedMark*)scratch;162_preserved_count = 0;163}164165166void GenMarkSweep::deallocate_stacks() {167GenCollectedHeap* gch = GenCollectedHeap::heap();168gch->release_scratch();169170_preserved_mark_stack.clear(true);171_preserved_oop_stack.clear(true);172_marking_stack.clear();173_objarray_stack.clear(true);174}175176void GenMarkSweep::mark_sweep_phase1(bool clear_all_softrefs) {177// Recursively traverse all live objects and mark them178GCTraceTime(Info, gc, phases) tm("Phase 1: Mark live objects", _gc_timer);179180GenCollectedHeap* gch = GenCollectedHeap::heap();181182// Need new claim bits before marking starts.183ClassLoaderDataGraph::clear_claimed_marks();184185{186StrongRootsScope srs(0);187188gch->full_process_roots(false, // not the adjust phase189GenCollectedHeap::SO_None,190ClassUnloading, // only strong roots if ClassUnloading191// is enabled192&follow_root_closure,193&follow_cld_closure);194}195196// Process reference objects found during marking197{198GCTraceTime(Debug, gc, phases) tm_m("Reference Processing", gc_timer());199200ref_processor()->setup_policy(clear_all_softrefs);201ReferenceProcessorPhaseTimes pt(_gc_timer, ref_processor()->max_num_queues());202SerialGCRefProcProxyTask task(is_alive, keep_alive, follow_stack_closure);203const ReferenceProcessorStats& stats = ref_processor()->process_discovered_references(task, pt);204pt.print_all_references();205gc_tracer()->report_gc_reference_stats(stats);206}207208// This is the point where the entire marking should have completed.209assert(_marking_stack.is_empty(), "Marking should have completed");210211{212GCTraceTime(Debug, gc, phases) tm_m("Weak Processing", gc_timer());213WeakProcessor::weak_oops_do(&is_alive, &do_nothing_cl);214}215216{217GCTraceTime(Debug, gc, phases) tm_m("Class Unloading", gc_timer());218219// Unload classes and purge the SystemDictionary.220bool purged_class = SystemDictionary::do_unloading(gc_timer());221222// Unload nmethods.223CodeCache::do_unloading(&is_alive, purged_class);224225// Prune dead klasses from subklass/sibling/implementor lists.226Klass::clean_weak_klass_links(purged_class);227228// Clean JVMCI metadata handles.229JVMCI_ONLY(JVMCI::do_unloading(purged_class));230}231232gc_tracer()->report_object_count_after_gc(&is_alive);233}234235236void GenMarkSweep::mark_sweep_phase2() {237// Now all live objects are marked, compute the new object addresses.238239// It is imperative that we traverse perm_gen LAST. If dead space is240// allowed a range of dead object may get overwritten by a dead int241// array. If perm_gen is not traversed last a Klass* may get242// overwritten. This is fine since it is dead, but if the class has dead243// instances we have to skip them, and in order to find their size we244// need the Klass*!245//246// It is not required that we traverse spaces in the same order in247// phase2, phase3 and phase4, but the ValidateMarkSweep live oops248// tracking expects us to do so. See comment under phase4.249250GenCollectedHeap* gch = GenCollectedHeap::heap();251252GCTraceTime(Info, gc, phases) tm("Phase 2: Compute new object addresses", _gc_timer);253254gch->prepare_for_compaction();255}256257class GenAdjustPointersClosure: public GenCollectedHeap::GenClosure {258public:259void do_generation(Generation* gen) {260gen->adjust_pointers();261}262};263264void GenMarkSweep::mark_sweep_phase3() {265GenCollectedHeap* gch = GenCollectedHeap::heap();266267// Adjust the pointers to reflect the new locations268GCTraceTime(Info, gc, phases) tm("Phase 3: Adjust pointers", gc_timer());269270// Need new claim bits for the pointer adjustment tracing.271ClassLoaderDataGraph::clear_claimed_marks();272273{274StrongRootsScope srs(0);275276gch->full_process_roots(true, // this is the adjust phase277GenCollectedHeap::SO_AllCodeCache,278false, // all roots279&adjust_pointer_closure,280&adjust_cld_closure);281}282283gch->gen_process_weak_roots(&adjust_pointer_closure);284285adjust_marks();286GenAdjustPointersClosure blk;287gch->generation_iterate(&blk, true);288}289290class GenCompactClosure: public GenCollectedHeap::GenClosure {291public:292void do_generation(Generation* gen) {293gen->compact();294}295};296297void GenMarkSweep::mark_sweep_phase4() {298// All pointers are now adjusted, move objects accordingly299300// It is imperative that we traverse perm_gen first in phase4. All301// classes must be allocated earlier than their instances, and traversing302// perm_gen first makes sure that all Klass*s have moved to their new303// location before any instance does a dispatch through it's klass!304305// The ValidateMarkSweep live oops tracking expects us to traverse spaces306// in the same order in phase2, phase3 and phase4. We don't quite do that307// here (perm_gen first rather than last), so we tell the validate code308// to use a higher index (saved from phase2) when verifying perm_gen.309GenCollectedHeap* gch = GenCollectedHeap::heap();310311GCTraceTime(Info, gc, phases) tm("Phase 4: Move objects", _gc_timer);312313GenCompactClosure blk;314gch->generation_iterate(&blk, true);315}316317318