Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.cpp
38920 views
/*1* Copyright (c) 2001, 2019, 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/classLoaderData.hpp"26#include "classfile/symbolTable.hpp"27#include "classfile/systemDictionary.hpp"28#include "code/codeCache.hpp"29#include "gc_implementation/concurrentMarkSweep/cmsAdaptiveSizePolicy.hpp"30#include "gc_implementation/concurrentMarkSweep/cmsCollectorPolicy.hpp"31#include "gc_implementation/concurrentMarkSweep/cmsGCAdaptivePolicyCounters.hpp"32#include "gc_implementation/concurrentMarkSweep/cmsOopClosures.inline.hpp"33#include "gc_implementation/concurrentMarkSweep/compactibleFreeListSpace.hpp"34#include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp"35#include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"36#include "gc_implementation/concurrentMarkSweep/vmCMSOperations.hpp"37#include "gc_implementation/parNew/parNewGeneration.hpp"38#include "gc_implementation/shared/collectorCounters.hpp"39#include "gc_implementation/shared/gcTimer.hpp"40#include "gc_implementation/shared/gcTrace.hpp"41#include "gc_implementation/shared/gcTraceTime.hpp"42#include "gc_implementation/shared/isGCActiveMark.hpp"43#include "gc_interface/collectedHeap.inline.hpp"44#include "memory/allocation.hpp"45#include "memory/cardTableRS.hpp"46#include "memory/collectorPolicy.hpp"47#include "memory/gcLocker.inline.hpp"48#include "memory/genCollectedHeap.hpp"49#include "memory/genMarkSweep.hpp"50#include "memory/genOopClosures.inline.hpp"51#include "memory/iterator.inline.hpp"52#include "memory/padded.hpp"53#include "memory/referencePolicy.hpp"54#include "memory/resourceArea.hpp"55#include "memory/tenuredGeneration.hpp"56#include "oops/oop.inline.hpp"57#include "prims/jvmtiExport.hpp"58#include "runtime/globals_extension.hpp"59#include "runtime/handles.inline.hpp"60#include "runtime/java.hpp"61#include "runtime/orderAccess.inline.hpp"62#include "runtime/vmThread.hpp"63#include "services/memoryService.hpp"64#include "services/runtimeService.hpp"6566PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC6768// statics69CMSCollector* ConcurrentMarkSweepGeneration::_collector = NULL;70bool CMSCollector::_full_gc_requested = false;71GCCause::Cause CMSCollector::_full_gc_cause = GCCause::_no_gc;7273//////////////////////////////////////////////////////////////////74// In support of CMS/VM thread synchronization75//////////////////////////////////////////////////////////////////76// We split use of the CGC_lock into 2 "levels".77// The low-level locking is of the usual CGC_lock monitor. We introduce78// a higher level "token" (hereafter "CMS token") built on top of the79// low level monitor (hereafter "CGC lock").80// The token-passing protocol gives priority to the VM thread. The81// CMS-lock doesn't provide any fairness guarantees, but clients82// should ensure that it is only held for very short, bounded83// durations.84//85// When either of the CMS thread or the VM thread is involved in86// collection operations during which it does not want the other87// thread to interfere, it obtains the CMS token.88//89// If either thread tries to get the token while the other has90// it, that thread waits. However, if the VM thread and CMS thread91// both want the token, then the VM thread gets priority while the92// CMS thread waits. This ensures, for instance, that the "concurrent"93// phases of the CMS thread's work do not block out the VM thread94// for long periods of time as the CMS thread continues to hog95// the token. (See bug 4616232).96//97// The baton-passing functions are, however, controlled by the98// flags _foregroundGCShouldWait and _foregroundGCIsActive,99// and here the low-level CMS lock, not the high level token,100// ensures mutual exclusion.101//102// Two important conditions that we have to satisfy:103// 1. if a thread does a low-level wait on the CMS lock, then it104// relinquishes the CMS token if it were holding that token105// when it acquired the low-level CMS lock.106// 2. any low-level notifications on the low-level lock107// should only be sent when a thread has relinquished the token.108//109// In the absence of either property, we'd have potential deadlock.110//111// We protect each of the CMS (concurrent and sequential) phases112// with the CMS _token_, not the CMS _lock_.113//114// The only code protected by CMS lock is the token acquisition code115// itself, see ConcurrentMarkSweepThread::[de]synchronize(), and the116// baton-passing code.117//118// Unfortunately, i couldn't come up with a good abstraction to factor and119// hide the naked CGC_lock manipulation in the baton-passing code120// further below. That's something we should try to do. Also, the proof121// of correctness of this 2-level locking scheme is far from obvious,122// and potentially quite slippery. We have an uneasy supsicion, for instance,123// that there may be a theoretical possibility of delay/starvation in the124// low-level lock/wait/notify scheme used for the baton-passing because of125// potential intereference with the priority scheme embodied in the126// CMS-token-passing protocol. See related comments at a CGC_lock->wait()127// invocation further below and marked with "XXX 20011219YSR".128// Indeed, as we note elsewhere, this may become yet more slippery129// in the presence of multiple CMS and/or multiple VM threads. XXX130131class CMSTokenSync: public StackObj {132private:133bool _is_cms_thread;134public:135CMSTokenSync(bool is_cms_thread):136_is_cms_thread(is_cms_thread) {137assert(is_cms_thread == Thread::current()->is_ConcurrentGC_thread(),138"Incorrect argument to constructor");139ConcurrentMarkSweepThread::synchronize(_is_cms_thread);140}141142~CMSTokenSync() {143assert(_is_cms_thread ?144ConcurrentMarkSweepThread::cms_thread_has_cms_token() :145ConcurrentMarkSweepThread::vm_thread_has_cms_token(),146"Incorrect state");147ConcurrentMarkSweepThread::desynchronize(_is_cms_thread);148}149};150151// Convenience class that does a CMSTokenSync, and then acquires152// upto three locks.153class CMSTokenSyncWithLocks: public CMSTokenSync {154private:155// Note: locks are acquired in textual declaration order156// and released in the opposite order157MutexLockerEx _locker1, _locker2, _locker3;158public:159CMSTokenSyncWithLocks(bool is_cms_thread, Mutex* mutex1,160Mutex* mutex2 = NULL, Mutex* mutex3 = NULL):161CMSTokenSync(is_cms_thread),162_locker1(mutex1, Mutex::_no_safepoint_check_flag),163_locker2(mutex2, Mutex::_no_safepoint_check_flag),164_locker3(mutex3, Mutex::_no_safepoint_check_flag)165{ }166};167168169// Wrapper class to temporarily disable icms during a foreground cms collection.170class ICMSDisabler: public StackObj {171public:172// The ctor disables icms and wakes up the thread so it notices the change;173// the dtor re-enables icms. Note that the CMSCollector methods will check174// CMSIncrementalMode.175ICMSDisabler() { CMSCollector::disable_icms(); CMSCollector::start_icms(); }176~ICMSDisabler() { CMSCollector::enable_icms(); }177};178179//////////////////////////////////////////////////////////////////180// Concurrent Mark-Sweep Generation /////////////////////////////181//////////////////////////////////////////////////////////////////182183NOT_PRODUCT(CompactibleFreeListSpace* debug_cms_space;)184185// This struct contains per-thread things necessary to support parallel186// young-gen collection.187class CMSParGCThreadState: public CHeapObj<mtGC> {188public:189CFLS_LAB lab;190PromotionInfo promo;191192// Constructor.193CMSParGCThreadState(CompactibleFreeListSpace* cfls) : lab(cfls) {194promo.setSpace(cfls);195}196};197198ConcurrentMarkSweepGeneration::ConcurrentMarkSweepGeneration(199ReservedSpace rs, size_t initial_byte_size, int level,200CardTableRS* ct, bool use_adaptive_freelists,201FreeBlockDictionary<FreeChunk>::DictionaryChoice dictionaryChoice) :202CardGeneration(rs, initial_byte_size, level, ct),203_dilatation_factor(((double)MinChunkSize)/((double)(CollectedHeap::min_fill_size()))),204_debug_collection_type(Concurrent_collection_type),205_did_compact(false)206{207HeapWord* bottom = (HeapWord*) _virtual_space.low();208HeapWord* end = (HeapWord*) _virtual_space.high();209210_direct_allocated_words = 0;211NOT_PRODUCT(212_numObjectsPromoted = 0;213_numWordsPromoted = 0;214_numObjectsAllocated = 0;215_numWordsAllocated = 0;216)217218_cmsSpace = new CompactibleFreeListSpace(_bts, MemRegion(bottom, end),219use_adaptive_freelists,220dictionaryChoice);221NOT_PRODUCT(debug_cms_space = _cmsSpace;)222if (_cmsSpace == NULL) {223vm_exit_during_initialization(224"CompactibleFreeListSpace allocation failure");225}226_cmsSpace->_gen = this;227228_gc_stats = new CMSGCStats();229230// Verify the assumption that FreeChunk::_prev and OopDesc::_klass231// offsets match. The ability to tell free chunks from objects232// depends on this property.233debug_only(234FreeChunk* junk = NULL;235assert(UseCompressedClassPointers ||236junk->prev_addr() == (void*)(oop(junk)->klass_addr()),237"Offset of FreeChunk::_prev within FreeChunk must match"238" that of OopDesc::_klass within OopDesc");239)240if (CollectedHeap::use_parallel_gc_threads()) {241typedef CMSParGCThreadState* CMSParGCThreadStatePtr;242_par_gc_thread_states =243NEW_C_HEAP_ARRAY(CMSParGCThreadStatePtr, ParallelGCThreads, mtGC);244if (_par_gc_thread_states == NULL) {245vm_exit_during_initialization("Could not allocate par gc structs");246}247for (uint i = 0; i < ParallelGCThreads; i++) {248_par_gc_thread_states[i] = new CMSParGCThreadState(cmsSpace());249if (_par_gc_thread_states[i] == NULL) {250vm_exit_during_initialization("Could not allocate par gc structs");251}252}253} else {254_par_gc_thread_states = NULL;255}256_incremental_collection_failed = false;257// The "dilatation_factor" is the expansion that can occur on258// account of the fact that the minimum object size in the CMS259// generation may be larger than that in, say, a contiguous young260// generation.261// Ideally, in the calculation below, we'd compute the dilatation262// factor as: MinChunkSize/(promoting_gen's min object size)263// Since we do not have such a general query interface for the264// promoting generation, we'll instead just use the mimimum265// object size (which today is a header's worth of space);266// note that all arithmetic is in units of HeapWords.267assert(MinChunkSize >= CollectedHeap::min_fill_size(), "just checking");268assert(_dilatation_factor >= 1.0, "from previous assert");269}270271272// The field "_initiating_occupancy" represents the occupancy percentage273// at which we trigger a new collection cycle. Unless explicitly specified274// via CMSInitiatingOccupancyFraction (argument "io" below), it275// is calculated by:276//277// Let "f" be MinHeapFreeRatio in278//279// _intiating_occupancy = 100-f +280// f * (CMSTriggerRatio/100)281// where CMSTriggerRatio is the argument "tr" below.282//283// That is, if we assume the heap is at its desired maximum occupancy at the284// end of a collection, we let CMSTriggerRatio of the (purported) free285// space be allocated before initiating a new collection cycle.286//287void ConcurrentMarkSweepGeneration::init_initiating_occupancy(intx io, uintx tr) {288assert(io <= 100 && tr <= 100, "Check the arguments");289if (io >= 0) {290_initiating_occupancy = (double)io / 100.0;291} else {292_initiating_occupancy = ((100 - MinHeapFreeRatio) +293(double)(tr * MinHeapFreeRatio) / 100.0)294/ 100.0;295}296}297298void ConcurrentMarkSweepGeneration::ref_processor_init() {299assert(collector() != NULL, "no collector");300collector()->ref_processor_init();301}302303void CMSCollector::ref_processor_init() {304if (_ref_processor == NULL) {305// Allocate and initialize a reference processor306_ref_processor =307new ReferenceProcessor(_span, // span308(ParallelGCThreads > 1) && ParallelRefProcEnabled, // mt processing309(int) ParallelGCThreads, // mt processing degree310_cmsGen->refs_discovery_is_mt(), // mt discovery311(int) MAX2(ConcGCThreads, ParallelGCThreads), // mt discovery degree312_cmsGen->refs_discovery_is_atomic(), // discovery is not atomic313&_is_alive_closure); // closure for liveness info314// Initialize the _ref_processor field of CMSGen315_cmsGen->set_ref_processor(_ref_processor);316317}318}319320CMSAdaptiveSizePolicy* CMSCollector::size_policy() {321GenCollectedHeap* gch = GenCollectedHeap::heap();322assert(gch->kind() == CollectedHeap::GenCollectedHeap,323"Wrong type of heap");324CMSAdaptiveSizePolicy* sp = (CMSAdaptiveSizePolicy*)325gch->gen_policy()->size_policy();326assert(sp->is_gc_cms_adaptive_size_policy(),327"Wrong type of size policy");328return sp;329}330331CMSGCAdaptivePolicyCounters* CMSCollector::gc_adaptive_policy_counters() {332CMSGCAdaptivePolicyCounters* results =333(CMSGCAdaptivePolicyCounters*) collector_policy()->counters();334assert(335results->kind() == GCPolicyCounters::CMSGCAdaptivePolicyCountersKind,336"Wrong gc policy counter kind");337return results;338}339340341void ConcurrentMarkSweepGeneration::initialize_performance_counters() {342343const char* gen_name = "old";344345// Generation Counters - generation 1, 1 subspace346_gen_counters = new GenerationCounters(gen_name, 1, 1, &_virtual_space);347348_space_counters = new GSpaceCounters(gen_name, 0,349_virtual_space.reserved_size(),350this, _gen_counters);351}352353CMSStats::CMSStats(ConcurrentMarkSweepGeneration* cms_gen, unsigned int alpha):354_cms_gen(cms_gen)355{356assert(alpha <= 100, "bad value");357_saved_alpha = alpha;358359// Initialize the alphas to the bootstrap value of 100.360_gc0_alpha = _cms_alpha = 100;361362_cms_begin_time.update();363_cms_end_time.update();364365_gc0_duration = 0.0;366_gc0_period = 0.0;367_gc0_promoted = 0;368369_cms_duration = 0.0;370_cms_period = 0.0;371_cms_allocated = 0;372373_cms_used_at_gc0_begin = 0;374_cms_used_at_gc0_end = 0;375_allow_duty_cycle_reduction = false;376_valid_bits = 0;377_icms_duty_cycle = CMSIncrementalDutyCycle;378}379380double CMSStats::cms_free_adjustment_factor(size_t free) const {381// TBD: CR 6909490382return 1.0;383}384385void CMSStats::adjust_cms_free_adjustment_factor(bool fail, size_t free) {386}387388// If promotion failure handling is on use389// the padded average size of the promotion for each390// young generation collection.391double CMSStats::time_until_cms_gen_full() const {392size_t cms_free = _cms_gen->cmsSpace()->free();393GenCollectedHeap* gch = GenCollectedHeap::heap();394size_t expected_promotion = MIN2(gch->get_gen(0)->capacity(),395(size_t) _cms_gen->gc_stats()->avg_promoted()->padded_average());396if (cms_free > expected_promotion) {397// Start a cms collection if there isn't enough space to promote398// for the next minor collection. Use the padded average as399// a safety factor.400cms_free -= expected_promotion;401402// Adjust by the safety factor.403double cms_free_dbl = (double)cms_free;404double cms_adjustment = (100.0 - CMSIncrementalSafetyFactor)/100.0;405// Apply a further correction factor which tries to adjust406// for recent occurance of concurrent mode failures.407cms_adjustment = cms_adjustment * cms_free_adjustment_factor(cms_free);408cms_free_dbl = cms_free_dbl * cms_adjustment;409410if (PrintGCDetails && Verbose) {411gclog_or_tty->print_cr("CMSStats::time_until_cms_gen_full: cms_free "412SIZE_FORMAT " expected_promotion " SIZE_FORMAT,413cms_free, expected_promotion);414gclog_or_tty->print_cr(" cms_free_dbl %f cms_consumption_rate %f",415cms_free_dbl, cms_consumption_rate() + 1.0);416}417// Add 1 in case the consumption rate goes to zero.418return cms_free_dbl / (cms_consumption_rate() + 1.0);419}420return 0.0;421}422423// Compare the duration of the cms collection to the424// time remaining before the cms generation is empty.425// Note that the time from the start of the cms collection426// to the start of the cms sweep (less than the total427// duration of the cms collection) can be used. This428// has been tried and some applications experienced429// promotion failures early in execution. This was430// possibly because the averages were not accurate431// enough at the beginning.432double CMSStats::time_until_cms_start() const {433// We add "gc0_period" to the "work" calculation434// below because this query is done (mostly) at the435// end of a scavenge, so we need to conservatively436// account for that much possible delay437// in the query so as to avoid concurrent mode failures438// due to starting the collection just a wee bit too439// late.440double work = cms_duration() + gc0_period();441double deadline = time_until_cms_gen_full();442// If a concurrent mode failure occurred recently, we want to be443// more conservative and halve our expected time_until_cms_gen_full()444if (work > deadline) {445if (Verbose && PrintGCDetails) {446gclog_or_tty->print(447" CMSCollector: collect because of anticipated promotion "448"before full %3.7f + %3.7f > %3.7f ", cms_duration(),449gc0_period(), time_until_cms_gen_full());450}451return 0.0;452}453return work - deadline;454}455456// Return a duty cycle based on old_duty_cycle and new_duty_cycle, limiting the457// amount of change to prevent wild oscillation.458unsigned int CMSStats::icms_damped_duty_cycle(unsigned int old_duty_cycle,459unsigned int new_duty_cycle) {460assert(old_duty_cycle <= 100, "bad input value");461assert(new_duty_cycle <= 100, "bad input value");462463// Note: use subtraction with caution since it may underflow (values are464// unsigned). Addition is safe since we're in the range 0-100.465unsigned int damped_duty_cycle = new_duty_cycle;466if (new_duty_cycle < old_duty_cycle) {467const unsigned int largest_delta = MAX2(old_duty_cycle / 4, 5U);468if (new_duty_cycle + largest_delta < old_duty_cycle) {469damped_duty_cycle = old_duty_cycle - largest_delta;470}471} else if (new_duty_cycle > old_duty_cycle) {472const unsigned int largest_delta = MAX2(old_duty_cycle / 4, 15U);473if (new_duty_cycle > old_duty_cycle + largest_delta) {474damped_duty_cycle = MIN2(old_duty_cycle + largest_delta, 100U);475}476}477assert(damped_duty_cycle <= 100, "invalid duty cycle computed");478479if (CMSTraceIncrementalPacing) {480gclog_or_tty->print(" [icms_damped_duty_cycle(%d,%d) = %d] ",481old_duty_cycle, new_duty_cycle, damped_duty_cycle);482}483return damped_duty_cycle;484}485486unsigned int CMSStats::icms_update_duty_cycle_impl() {487assert(CMSIncrementalPacing && valid(),488"should be handled in icms_update_duty_cycle()");489490double cms_time_so_far = cms_timer().seconds();491double scaled_duration = cms_duration_per_mb() * _cms_used_at_gc0_end / M;492double scaled_duration_remaining = fabsd(scaled_duration - cms_time_so_far);493494// Avoid division by 0.495double time_until_full = MAX2(time_until_cms_gen_full(), 0.01);496double duty_cycle_dbl = 100.0 * scaled_duration_remaining / time_until_full;497498unsigned int new_duty_cycle = MIN2((unsigned int)duty_cycle_dbl, 100U);499if (new_duty_cycle > _icms_duty_cycle) {500// Avoid very small duty cycles (1 or 2); 0 is allowed.501if (new_duty_cycle > 2) {502_icms_duty_cycle = icms_damped_duty_cycle(_icms_duty_cycle,503new_duty_cycle);504}505} else if (_allow_duty_cycle_reduction) {506// The duty cycle is reduced only once per cms cycle (see record_cms_end()).507new_duty_cycle = icms_damped_duty_cycle(_icms_duty_cycle, new_duty_cycle);508// Respect the minimum duty cycle.509unsigned int min_duty_cycle = (unsigned int)CMSIncrementalDutyCycleMin;510_icms_duty_cycle = MAX2(new_duty_cycle, min_duty_cycle);511}512513if (PrintGCDetails || CMSTraceIncrementalPacing) {514gclog_or_tty->print(" icms_dc=%d ", _icms_duty_cycle);515}516517_allow_duty_cycle_reduction = false;518return _icms_duty_cycle;519}520521#ifndef PRODUCT522void CMSStats::print_on(outputStream *st) const {523st->print(" gc0_alpha=%d,cms_alpha=%d", _gc0_alpha, _cms_alpha);524st->print(",gc0_dur=%g,gc0_per=%g,gc0_promo=" SIZE_FORMAT,525gc0_duration(), gc0_period(), gc0_promoted());526st->print(",cms_dur=%g,cms_dur_per_mb=%g,cms_per=%g,cms_alloc=" SIZE_FORMAT,527cms_duration(), cms_duration_per_mb(),528cms_period(), cms_allocated());529st->print(",cms_since_beg=%g,cms_since_end=%g",530cms_time_since_begin(), cms_time_since_end());531st->print(",cms_used_beg=" SIZE_FORMAT ",cms_used_end=" SIZE_FORMAT,532_cms_used_at_gc0_begin, _cms_used_at_gc0_end);533if (CMSIncrementalMode) {534st->print(",dc=%d", icms_duty_cycle());535}536537if (valid()) {538st->print(",promo_rate=%g,cms_alloc_rate=%g",539promotion_rate(), cms_allocation_rate());540st->print(",cms_consumption_rate=%g,time_until_full=%g",541cms_consumption_rate(), time_until_cms_gen_full());542}543st->print(" ");544}545#endif // #ifndef PRODUCT546547CMSCollector::CollectorState CMSCollector::_collectorState =548CMSCollector::Idling;549bool CMSCollector::_foregroundGCIsActive = false;550bool CMSCollector::_foregroundGCShouldWait = false;551552CMSCollector::CMSCollector(ConcurrentMarkSweepGeneration* cmsGen,553CardTableRS* ct,554ConcurrentMarkSweepPolicy* cp):555_cmsGen(cmsGen),556_ct(ct),557_ref_processor(NULL), // will be set later558_conc_workers(NULL), // may be set later559_abort_preclean(false),560_start_sampling(false),561_between_prologue_and_epilogue(false),562_markBitMap(0, Mutex::leaf + 1, "CMS_markBitMap_lock"),563_modUnionTable((CardTableModRefBS::card_shift - LogHeapWordSize),564-1 /* lock-free */, "No_lock" /* dummy */),565_modUnionClosure(&_modUnionTable),566_modUnionClosurePar(&_modUnionTable),567// Adjust my span to cover old (cms) gen568_span(cmsGen->reserved()),569// Construct the is_alive_closure with _span & markBitMap570_is_alive_closure(_span, &_markBitMap),571_restart_addr(NULL),572_overflow_list(NULL),573_stats(cmsGen),574_eden_chunk_lock(new Mutex(Mutex::leaf + 1, "CMS_eden_chunk_lock", true)),575_eden_chunk_array(NULL), // may be set in ctor body576_eden_chunk_capacity(0), // -- ditto --577_eden_chunk_index(0), // -- ditto --578_survivor_plab_array(NULL), // -- ditto --579_survivor_chunk_array(NULL), // -- ditto --580_survivor_chunk_capacity(0), // -- ditto --581_survivor_chunk_index(0), // -- ditto --582_ser_pmc_preclean_ovflw(0),583_ser_kac_preclean_ovflw(0),584_ser_pmc_remark_ovflw(0),585_par_pmc_remark_ovflw(0),586_ser_kac_ovflw(0),587_par_kac_ovflw(0),588#ifndef PRODUCT589_num_par_pushes(0),590#endif591_collection_count_start(0),592_verifying(false),593_icms_start_limit(NULL),594_icms_stop_limit(NULL),595_verification_mark_bm(0, Mutex::leaf + 1, "CMS_verification_mark_bm_lock"),596_completed_initialization(false),597_collector_policy(cp),598_should_unload_classes(CMSClassUnloadingEnabled),599_concurrent_cycles_since_last_unload(0),600_roots_scanning_options(GenCollectedHeap::SO_None),601_inter_sweep_estimate(CMS_SweepWeight, CMS_SweepPadding),602_intra_sweep_estimate(CMS_SweepWeight, CMS_SweepPadding),603_gc_tracer_cm(new (ResourceObj::C_HEAP, mtGC) CMSTracer()),604_gc_timer_cm(new (ResourceObj::C_HEAP, mtGC) ConcurrentGCTimer()),605_cms_start_registered(false)606{607if (ExplicitGCInvokesConcurrentAndUnloadsClasses) {608ExplicitGCInvokesConcurrent = true;609}610// Now expand the span and allocate the collection support structures611// (MUT, marking bit map etc.) to cover both generations subject to612// collection.613614// For use by dirty card to oop closures.615_cmsGen->cmsSpace()->set_collector(this);616617// Allocate MUT and marking bit map618{619MutexLockerEx x(_markBitMap.lock(), Mutex::_no_safepoint_check_flag);620if (!_markBitMap.allocate(_span)) {621warning("Failed to allocate CMS Bit Map");622return;623}624assert(_markBitMap.covers(_span), "_markBitMap inconsistency?");625}626{627_modUnionTable.allocate(_span);628assert(_modUnionTable.covers(_span), "_modUnionTable inconsistency?");629}630631if (!_markStack.allocate(MarkStackSize)) {632warning("Failed to allocate CMS Marking Stack");633return;634}635636// Support for multi-threaded concurrent phases637if (CMSConcurrentMTEnabled) {638if (FLAG_IS_DEFAULT(ConcGCThreads)) {639// just for now640FLAG_SET_DEFAULT(ConcGCThreads, (ParallelGCThreads + 3)/4);641}642if (ConcGCThreads > 1) {643_conc_workers = new YieldingFlexibleWorkGang("Parallel CMS Threads",644ConcGCThreads, true);645if (_conc_workers == NULL) {646warning("GC/CMS: _conc_workers allocation failure: "647"forcing -CMSConcurrentMTEnabled");648CMSConcurrentMTEnabled = false;649} else {650_conc_workers->initialize_workers();651}652} else {653CMSConcurrentMTEnabled = false;654}655}656if (!CMSConcurrentMTEnabled) {657ConcGCThreads = 0;658} else {659// Turn off CMSCleanOnEnter optimization temporarily for660// the MT case where it's not fixed yet; see 6178663.661CMSCleanOnEnter = false;662}663assert((_conc_workers != NULL) == (ConcGCThreads > 1),664"Inconsistency");665666// Parallel task queues; these are shared for the667// concurrent and stop-world phases of CMS, but668// are not shared with parallel scavenge (ParNew).669{670uint i;671uint num_queues = (uint) MAX2(ParallelGCThreads, ConcGCThreads);672673if ((CMSParallelRemarkEnabled || CMSConcurrentMTEnabled674|| ParallelRefProcEnabled)675&& num_queues > 0) {676_task_queues = new OopTaskQueueSet(num_queues);677if (_task_queues == NULL) {678warning("task_queues allocation failure.");679return;680}681_hash_seed = NEW_C_HEAP_ARRAY(int, num_queues, mtGC);682if (_hash_seed == NULL) {683warning("_hash_seed array allocation failure");684return;685}686687typedef Padded<OopTaskQueue> PaddedOopTaskQueue;688for (i = 0; i < num_queues; i++) {689PaddedOopTaskQueue *q = new PaddedOopTaskQueue();690if (q == NULL) {691warning("work_queue allocation failure.");692return;693}694_task_queues->register_queue(i, q);695}696for (i = 0; i < num_queues; i++) {697_task_queues->queue(i)->initialize();698_hash_seed[i] = 17; // copied from ParNew699}700}701}702703_cmsGen ->init_initiating_occupancy(CMSInitiatingOccupancyFraction, CMSTriggerRatio);704705// Clip CMSBootstrapOccupancy between 0 and 100.706_bootstrap_occupancy = ((double)CMSBootstrapOccupancy)/(double)100;707708_full_gcs_since_conc_gc = 0;709710// Now tell CMS generations the identity of their collector711ConcurrentMarkSweepGeneration::set_collector(this);712713// Create & start a CMS thread for this CMS collector714_cmsThread = ConcurrentMarkSweepThread::start(this);715assert(cmsThread() != NULL, "CMS Thread should have been created");716assert(cmsThread()->collector() == this,717"CMS Thread should refer to this gen");718assert(CGC_lock != NULL, "Where's the CGC_lock?");719720// Support for parallelizing young gen rescan721GenCollectedHeap* gch = GenCollectedHeap::heap();722_young_gen = gch->prev_gen(_cmsGen);723if (gch->supports_inline_contig_alloc()) {724_top_addr = gch->top_addr();725_end_addr = gch->end_addr();726assert(_young_gen != NULL, "no _young_gen");727_eden_chunk_index = 0;728_eden_chunk_capacity = (_young_gen->max_capacity()+CMSSamplingGrain)/CMSSamplingGrain;729_eden_chunk_array = NEW_C_HEAP_ARRAY(HeapWord*, _eden_chunk_capacity, mtGC);730if (_eden_chunk_array == NULL) {731_eden_chunk_capacity = 0;732warning("GC/CMS: _eden_chunk_array allocation failure");733}734}735assert(_eden_chunk_array != NULL || _eden_chunk_capacity == 0, "Error");736737// Support for parallelizing survivor space rescan738if ((CMSParallelRemarkEnabled && CMSParallelSurvivorRemarkEnabled) || CMSParallelInitialMarkEnabled) {739const size_t max_plab_samples =740((DefNewGeneration*)_young_gen)->max_survivor_size() / plab_sample_minimum_size();741742_survivor_plab_array = NEW_C_HEAP_ARRAY(ChunkArray, ParallelGCThreads, mtGC);743_survivor_chunk_array = NEW_C_HEAP_ARRAY(HeapWord*, 2*max_plab_samples, mtGC);744_cursor = NEW_C_HEAP_ARRAY(size_t, ParallelGCThreads, mtGC);745if (_survivor_plab_array == NULL || _survivor_chunk_array == NULL746|| _cursor == NULL) {747warning("Failed to allocate survivor plab/chunk array");748if (_survivor_plab_array != NULL) {749FREE_C_HEAP_ARRAY(ChunkArray, _survivor_plab_array, mtGC);750_survivor_plab_array = NULL;751}752if (_survivor_chunk_array != NULL) {753FREE_C_HEAP_ARRAY(HeapWord*, _survivor_chunk_array, mtGC);754_survivor_chunk_array = NULL;755}756if (_cursor != NULL) {757FREE_C_HEAP_ARRAY(size_t, _cursor, mtGC);758_cursor = NULL;759}760} else {761_survivor_chunk_capacity = 2*max_plab_samples;762for (uint i = 0; i < ParallelGCThreads; i++) {763HeapWord** vec = NEW_C_HEAP_ARRAY(HeapWord*, max_plab_samples, mtGC);764if (vec == NULL) {765warning("Failed to allocate survivor plab array");766for (int j = i; j > 0; j--) {767FREE_C_HEAP_ARRAY(HeapWord*, _survivor_plab_array[j-1].array(), mtGC);768}769FREE_C_HEAP_ARRAY(ChunkArray, _survivor_plab_array, mtGC);770FREE_C_HEAP_ARRAY(HeapWord*, _survivor_chunk_array, mtGC);771_survivor_plab_array = NULL;772_survivor_chunk_array = NULL;773_survivor_chunk_capacity = 0;774break;775} else {776ChunkArray* cur =777::new (&_survivor_plab_array[i]) ChunkArray(vec,778max_plab_samples);779assert(cur->end() == 0, "Should be 0");780assert(cur->array() == vec, "Should be vec");781assert(cur->capacity() == max_plab_samples, "Error");782}783}784}785}786assert( ( _survivor_plab_array != NULL787&& _survivor_chunk_array != NULL)788|| ( _survivor_chunk_capacity == 0789&& _survivor_chunk_index == 0),790"Error");791792NOT_PRODUCT(_overflow_counter = CMSMarkStackOverflowInterval;)793_gc_counters = new CollectorCounters("CMS", 1);794_completed_initialization = true;795_inter_sweep_timer.start(); // start of time796}797798size_t CMSCollector::plab_sample_minimum_size() {799// The default value of MinTLABSize is 2k, but there is800// no way to get the default value if the flag has been overridden.801return MAX2(ThreadLocalAllocBuffer::min_size() * HeapWordSize, 2 * K);802}803804const char* ConcurrentMarkSweepGeneration::name() const {805return "concurrent mark-sweep generation";806}807void ConcurrentMarkSweepGeneration::update_counters() {808if (UsePerfData) {809_space_counters->update_all();810_gen_counters->update_all();811}812}813814// this is an optimized version of update_counters(). it takes the815// used value as a parameter rather than computing it.816//817void ConcurrentMarkSweepGeneration::update_counters(size_t used) {818if (UsePerfData) {819_space_counters->update_used(used);820_space_counters->update_capacity();821_gen_counters->update_all();822}823}824825void ConcurrentMarkSweepGeneration::print() const {826Generation::print();827cmsSpace()->print();828}829830#ifndef PRODUCT831void ConcurrentMarkSweepGeneration::print_statistics() {832cmsSpace()->printFLCensus(0);833}834#endif835836void ConcurrentMarkSweepGeneration::printOccupancy(const char *s) {837GenCollectedHeap* gch = GenCollectedHeap::heap();838if (PrintGCDetails) {839if (Verbose) {840gclog_or_tty->print("[%d %s-%s: " SIZE_FORMAT "(" SIZE_FORMAT ")]",841level(), short_name(), s, used(), capacity());842} else {843gclog_or_tty->print("[%d %s-%s: " SIZE_FORMAT "K(" SIZE_FORMAT "K)]",844level(), short_name(), s, used() / K, capacity() / K);845}846}847if (Verbose) {848gclog_or_tty->print(" " SIZE_FORMAT "(" SIZE_FORMAT ")",849gch->used(), gch->capacity());850} else {851gclog_or_tty->print(" " SIZE_FORMAT "K(" SIZE_FORMAT "K)",852gch->used() / K, gch->capacity() / K);853}854}855856size_t857ConcurrentMarkSweepGeneration::contiguous_available() const {858// dld proposes an improvement in precision here. If the committed859// part of the space ends in a free block we should add that to860// uncommitted size in the calculation below. Will make this861// change later, staying with the approximation below for the862// time being. -- ysr.863return MAX2(_virtual_space.uncommitted_size(), unsafe_max_alloc_nogc());864}865866size_t867ConcurrentMarkSweepGeneration::unsafe_max_alloc_nogc() const {868return _cmsSpace->max_alloc_in_words() * HeapWordSize;869}870871size_t ConcurrentMarkSweepGeneration::used_stable() const {872return cmsSpace()->used_stable();873}874875size_t ConcurrentMarkSweepGeneration::max_available() const {876return free() + _virtual_space.uncommitted_size();877}878879bool ConcurrentMarkSweepGeneration::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const {880size_t available = max_available();881size_t av_promo = (size_t)gc_stats()->avg_promoted()->padded_average();882bool res = (available >= av_promo) || (available >= max_promotion_in_bytes);883if (Verbose && PrintGCDetails) {884gclog_or_tty->print_cr(885"CMS: promo attempt is%s safe: available(" SIZE_FORMAT ") %s av_promo(" SIZE_FORMAT "),"886"max_promo(" SIZE_FORMAT ")",887res? "":" not", available, res? ">=":"<",888av_promo, max_promotion_in_bytes);889}890return res;891}892893// At a promotion failure dump information on block layout in heap894// (cms old generation).895void ConcurrentMarkSweepGeneration::promotion_failure_occurred() {896if (CMSDumpAtPromotionFailure) {897cmsSpace()->dump_at_safepoint_with_locks(collector(), gclog_or_tty);898}899}900901CompactibleSpace*902ConcurrentMarkSweepGeneration::first_compaction_space() const {903return _cmsSpace;904}905906void ConcurrentMarkSweepGeneration::reset_after_compaction() {907// Clear the promotion information. These pointers can be adjusted908// along with all the other pointers into the heap but909// compaction is expected to be a rare event with910// a heap using cms so don't do it without seeing the need.911if (CollectedHeap::use_parallel_gc_threads()) {912for (uint i = 0; i < ParallelGCThreads; i++) {913_par_gc_thread_states[i]->promo.reset();914}915}916}917918void ConcurrentMarkSweepGeneration::space_iterate(SpaceClosure* blk, bool usedOnly) {919blk->do_space(_cmsSpace);920}921922void ConcurrentMarkSweepGeneration::compute_new_size() {923assert_locked_or_safepoint(Heap_lock);924925// If incremental collection failed, we just want to expand926// to the limit.927if (incremental_collection_failed()) {928clear_incremental_collection_failed();929grow_to_reserved();930return;931}932933// The heap has been compacted but not reset yet.934// Any metric such as free() or used() will be incorrect.935936CardGeneration::compute_new_size();937938// Reset again after a possible resizing939if (did_compact()) {940cmsSpace()->reset_after_compaction();941}942}943944void ConcurrentMarkSweepGeneration::compute_new_size_free_list() {945assert_locked_or_safepoint(Heap_lock);946947// If incremental collection failed, we just want to expand948// to the limit.949if (incremental_collection_failed()) {950clear_incremental_collection_failed();951grow_to_reserved();952return;953}954955double free_percentage = ((double) free()) / capacity();956double desired_free_percentage = (double) MinHeapFreeRatio / 100;957double maximum_free_percentage = (double) MaxHeapFreeRatio / 100;958959// compute expansion delta needed for reaching desired free percentage960if (free_percentage < desired_free_percentage) {961size_t desired_capacity = (size_t)(used() / ((double) 1 - desired_free_percentage));962assert(desired_capacity >= capacity(), "invalid expansion size");963size_t expand_bytes = MAX2(desired_capacity - capacity(), MinHeapDeltaBytes);964if (PrintGCDetails && Verbose) {965size_t desired_capacity = (size_t)(used() / ((double) 1 - desired_free_percentage));966gclog_or_tty->print_cr("\nFrom compute_new_size: ");967gclog_or_tty->print_cr(" Free fraction %f", free_percentage);968gclog_or_tty->print_cr(" Desired free fraction %f",969desired_free_percentage);970gclog_or_tty->print_cr(" Maximum free fraction %f",971maximum_free_percentage);972gclog_or_tty->print_cr(" Capactiy " SIZE_FORMAT, capacity()/1000);973gclog_or_tty->print_cr(" Desired capacity " SIZE_FORMAT,974desired_capacity/1000);975int prev_level = level() - 1;976if (prev_level >= 0) {977size_t prev_size = 0;978GenCollectedHeap* gch = GenCollectedHeap::heap();979Generation* prev_gen = gch->_gens[prev_level];980prev_size = prev_gen->capacity();981gclog_or_tty->print_cr(" Younger gen size " SIZE_FORMAT,982prev_size/1000);983}984gclog_or_tty->print_cr(" unsafe_max_alloc_nogc " SIZE_FORMAT,985unsafe_max_alloc_nogc()/1000);986gclog_or_tty->print_cr(" contiguous available " SIZE_FORMAT,987contiguous_available()/1000);988gclog_or_tty->print_cr(" Expand by " SIZE_FORMAT " (bytes)",989expand_bytes);990}991// safe if expansion fails992expand(expand_bytes, 0, CMSExpansionCause::_satisfy_free_ratio);993if (PrintGCDetails && Verbose) {994gclog_or_tty->print_cr(" Expanded free fraction %f",995((double) free()) / capacity());996}997} else {998size_t desired_capacity = (size_t)(used() / ((double) 1 - desired_free_percentage));999assert(desired_capacity <= capacity(), "invalid expansion size");1000size_t shrink_bytes = capacity() - desired_capacity;1001// Don't shrink unless the delta is greater than the minimum shrink we want1002if (shrink_bytes >= MinHeapDeltaBytes) {1003shrink_free_list_by(shrink_bytes);1004}1005}1006}10071008Mutex* ConcurrentMarkSweepGeneration::freelistLock() const {1009return cmsSpace()->freelistLock();1010}10111012HeapWord* ConcurrentMarkSweepGeneration::allocate(size_t size,1013bool tlab) {1014CMSSynchronousYieldRequest yr;1015MutexLockerEx x(freelistLock(),1016Mutex::_no_safepoint_check_flag);1017return have_lock_and_allocate(size, tlab);1018}10191020HeapWord* ConcurrentMarkSweepGeneration::have_lock_and_allocate(size_t size,1021bool tlab /* ignored */) {1022assert_lock_strong(freelistLock());1023size_t adjustedSize = CompactibleFreeListSpace::adjustObjectSize(size);1024HeapWord* res = cmsSpace()->allocate(adjustedSize);1025// Allocate the object live (grey) if the background collector has1026// started marking. This is necessary because the marker may1027// have passed this address and consequently this object will1028// not otherwise be greyed and would be incorrectly swept up.1029// Note that if this object contains references, the writing1030// of those references will dirty the card containing this object1031// allowing the object to be blackened (and its references scanned)1032// either during a preclean phase or at the final checkpoint.1033if (res != NULL) {1034// We may block here with an uninitialized object with1035// its mark-bit or P-bits not yet set. Such objects need1036// to be safely navigable by block_start().1037assert(oop(res)->klass_or_null() == NULL, "Object should be uninitialized here.");1038assert(!((FreeChunk*)res)->is_free(), "Error, block will look free but show wrong size");1039collector()->direct_allocated(res, adjustedSize);1040_direct_allocated_words += adjustedSize;1041// allocation counters1042NOT_PRODUCT(1043_numObjectsAllocated++;1044_numWordsAllocated += (int)adjustedSize;1045)1046}1047return res;1048}10491050// In the case of direct allocation by mutators in a generation that1051// is being concurrently collected, the object must be allocated1052// live (grey) if the background collector has started marking.1053// This is necessary because the marker may1054// have passed this address and consequently this object will1055// not otherwise be greyed and would be incorrectly swept up.1056// Note that if this object contains references, the writing1057// of those references will dirty the card containing this object1058// allowing the object to be blackened (and its references scanned)1059// either during a preclean phase or at the final checkpoint.1060void CMSCollector::direct_allocated(HeapWord* start, size_t size) {1061assert(_markBitMap.covers(start, size), "Out of bounds");1062if (_collectorState >= Marking) {1063MutexLockerEx y(_markBitMap.lock(),1064Mutex::_no_safepoint_check_flag);1065// [see comments preceding SweepClosure::do_blk() below for details]1066//1067// Can the P-bits be deleted now? JJJ1068//1069// 1. need to mark the object as live so it isn't collected1070// 2. need to mark the 2nd bit to indicate the object may be uninitialized1071// 3. need to mark the end of the object so marking, precleaning or sweeping1072// can skip over uninitialized or unparsable objects. An allocated1073// object is considered uninitialized for our purposes as long as1074// its klass word is NULL. All old gen objects are parsable1075// as soon as they are initialized.)1076_markBitMap.mark(start); // object is live1077_markBitMap.mark(start + 1); // object is potentially uninitialized?1078_markBitMap.mark(start + size - 1);1079// mark end of object1080}1081// check that oop looks uninitialized1082assert(oop(start)->klass_or_null() == NULL, "_klass should be NULL");1083}10841085void CMSCollector::promoted(bool par, HeapWord* start,1086bool is_obj_array, size_t obj_size) {1087assert(_markBitMap.covers(start), "Out of bounds");1088// See comment in direct_allocated() about when objects should1089// be allocated live.1090if (_collectorState >= Marking) {1091// we already hold the marking bit map lock, taken in1092// the prologue1093if (par) {1094_markBitMap.par_mark(start);1095} else {1096_markBitMap.mark(start);1097}1098// We don't need to mark the object as uninitialized (as1099// in direct_allocated above) because this is being done with the1100// world stopped and the object will be initialized by the1101// time the marking, precleaning or sweeping get to look at it.1102// But see the code for copying objects into the CMS generation,1103// where we need to ensure that concurrent readers of the1104// block offset table are able to safely navigate a block that1105// is in flux from being free to being allocated (and in1106// transition while being copied into) and subsequently1107// becoming a bona-fide object when the copy/promotion is complete.1108assert(SafepointSynchronize::is_at_safepoint(),1109"expect promotion only at safepoints");11101111if (_collectorState < Sweeping) {1112// Mark the appropriate cards in the modUnionTable, so that1113// this object gets scanned before the sweep. If this is1114// not done, CMS generation references in the object might1115// not get marked.1116// For the case of arrays, which are otherwise precisely1117// marked, we need to dirty the entire array, not just its head.1118if (is_obj_array) {1119// The [par_]mark_range() method expects mr.end() below to1120// be aligned to the granularity of a bit's representation1121// in the heap. In the case of the MUT below, that's a1122// card size.1123MemRegion mr(start,1124(HeapWord*)round_to((intptr_t)(start + obj_size),1125CardTableModRefBS::card_size /* bytes */));1126if (par) {1127_modUnionTable.par_mark_range(mr);1128} else {1129_modUnionTable.mark_range(mr);1130}1131} else { // not an obj array; we can just mark the head1132if (par) {1133_modUnionTable.par_mark(start);1134} else {1135_modUnionTable.mark(start);1136}1137}1138}1139}1140}11411142static inline size_t percent_of_space(Space* space, HeapWord* addr)1143{1144size_t delta = pointer_delta(addr, space->bottom());1145return (size_t)(delta * 100.0 / (space->capacity() / HeapWordSize));1146}11471148void CMSCollector::icms_update_allocation_limits()1149{1150Generation* gen0 = GenCollectedHeap::heap()->get_gen(0);1151EdenSpace* eden = gen0->as_DefNewGeneration()->eden();11521153const unsigned int duty_cycle = stats().icms_update_duty_cycle();1154if (CMSTraceIncrementalPacing) {1155stats().print();1156}11571158assert(duty_cycle <= 100, "invalid duty cycle");1159if (duty_cycle != 0) {1160// The duty_cycle is a percentage between 0 and 100; convert to words and1161// then compute the offset from the endpoints of the space.1162size_t free_words = eden->free() / HeapWordSize;1163double free_words_dbl = (double)free_words;1164size_t duty_cycle_words = (size_t)(free_words_dbl * duty_cycle / 100.0);1165size_t offset_words = (free_words - duty_cycle_words) / 2;11661167_icms_start_limit = eden->top() + offset_words;1168_icms_stop_limit = eden->end() - offset_words;11691170// The limits may be adjusted (shifted to the right) by1171// CMSIncrementalOffset, to allow the application more mutator time after a1172// young gen gc (when all mutators were stopped) and before CMS starts and1173// takes away one or more cpus.1174if (CMSIncrementalOffset != 0) {1175double adjustment_dbl = free_words_dbl * CMSIncrementalOffset / 100.0;1176size_t adjustment = (size_t)adjustment_dbl;1177HeapWord* tmp_stop = _icms_stop_limit + adjustment;1178if (tmp_stop > _icms_stop_limit && tmp_stop < eden->end()) {1179_icms_start_limit += adjustment;1180_icms_stop_limit = tmp_stop;1181}1182}1183}1184if (duty_cycle == 0 || (_icms_start_limit == _icms_stop_limit)) {1185_icms_start_limit = _icms_stop_limit = eden->end();1186}11871188// Install the new start limit.1189eden->set_soft_end(_icms_start_limit);11901191if (CMSTraceIncrementalMode) {1192gclog_or_tty->print(" icms alloc limits: "1193PTR_FORMAT "," PTR_FORMAT1194" (" SIZE_FORMAT "%%," SIZE_FORMAT "%%) ",1195p2i(_icms_start_limit), p2i(_icms_stop_limit),1196percent_of_space(eden, _icms_start_limit),1197percent_of_space(eden, _icms_stop_limit));1198if (Verbose) {1199gclog_or_tty->print("eden: ");1200eden->print_on(gclog_or_tty);1201}1202}1203}12041205// Any changes here should try to maintain the invariant1206// that if this method is called with _icms_start_limit1207// and _icms_stop_limit both NULL, then it should return NULL1208// and not notify the icms thread.1209HeapWord*1210CMSCollector::allocation_limit_reached(Space* space, HeapWord* top,1211size_t word_size)1212{1213// A start_limit equal to end() means the duty cycle is 0, so treat that as a1214// nop.1215if (CMSIncrementalMode && _icms_start_limit != space->end()) {1216if (top <= _icms_start_limit) {1217if (CMSTraceIncrementalMode) {1218space->print_on(gclog_or_tty);1219gclog_or_tty->stamp();1220gclog_or_tty->print_cr(" start limit top=" PTR_FORMAT1221", new limit=" PTR_FORMAT1222" (" SIZE_FORMAT "%%)",1223p2i(top), p2i(_icms_stop_limit),1224percent_of_space(space, _icms_stop_limit));1225}1226ConcurrentMarkSweepThread::start_icms();1227assert(top < _icms_stop_limit, "Tautology");1228if (word_size < pointer_delta(_icms_stop_limit, top)) {1229return _icms_stop_limit;1230}12311232// The allocation will cross both the _start and _stop limits, so do the1233// stop notification also and return end().1234if (CMSTraceIncrementalMode) {1235space->print_on(gclog_or_tty);1236gclog_or_tty->stamp();1237gclog_or_tty->print_cr(" +stop limit top=" PTR_FORMAT1238", new limit=" PTR_FORMAT1239" (" SIZE_FORMAT "%%)",1240p2i(top), p2i(space->end()),1241percent_of_space(space, space->end()));1242}1243ConcurrentMarkSweepThread::stop_icms();1244return space->end();1245}12461247if (top <= _icms_stop_limit) {1248if (CMSTraceIncrementalMode) {1249space->print_on(gclog_or_tty);1250gclog_or_tty->stamp();1251gclog_or_tty->print_cr(" stop limit top=" PTR_FORMAT1252", new limit=" PTR_FORMAT1253" (" SIZE_FORMAT "%%)",1254top, space->end(),1255percent_of_space(space, space->end()));1256}1257ConcurrentMarkSweepThread::stop_icms();1258return space->end();1259}12601261if (CMSTraceIncrementalMode) {1262space->print_on(gclog_or_tty);1263gclog_or_tty->stamp();1264gclog_or_tty->print_cr(" end limit top=" PTR_FORMAT1265", new limit=" PTR_FORMAT,1266top, NULL);1267}1268}12691270return NULL;1271}12721273oop ConcurrentMarkSweepGeneration::promote(oop obj, size_t obj_size) {1274assert(obj_size == (size_t)obj->size(), "bad obj_size passed in");1275// allocate, copy and if necessary update promoinfo --1276// delegate to underlying space.1277assert_lock_strong(freelistLock());12781279#ifndef PRODUCT1280if (Universe::heap()->promotion_should_fail()) {1281return NULL;1282}1283#endif // #ifndef PRODUCT12841285oop res = _cmsSpace->promote(obj, obj_size);1286if (res == NULL) {1287// expand and retry1288size_t s = _cmsSpace->expansionSpaceRequired(obj_size); // HeapWords1289expand(s*HeapWordSize, MinHeapDeltaBytes,1290CMSExpansionCause::_satisfy_promotion);1291// Since there's currently no next generation, we don't try to promote1292// into a more senior generation.1293assert(next_gen() == NULL, "assumption, based upon which no attempt "1294"is made to pass on a possibly failing "1295"promotion to next generation");1296res = _cmsSpace->promote(obj, obj_size);1297}1298if (res != NULL) {1299// See comment in allocate() about when objects should1300// be allocated live.1301assert(obj->is_oop(), "Will dereference klass pointer below");1302collector()->promoted(false, // Not parallel1303(HeapWord*)res, obj->is_objArray(), obj_size);1304// promotion counters1305NOT_PRODUCT(1306_numObjectsPromoted++;1307_numWordsPromoted +=1308(int)(CompactibleFreeListSpace::adjustObjectSize(obj->size()));1309)1310}1311return res;1312}131313141315HeapWord*1316ConcurrentMarkSweepGeneration::allocation_limit_reached(Space* space,1317HeapWord* top,1318size_t word_sz)1319{1320return collector()->allocation_limit_reached(space, top, word_sz);1321}13221323// IMPORTANT: Notes on object size recognition in CMS.1324// ---------------------------------------------------1325// A block of storage in the CMS generation is always in1326// one of three states. A free block (FREE), an allocated1327// object (OBJECT) whose size() method reports the correct size,1328// and an intermediate state (TRANSIENT) in which its size cannot1329// be accurately determined.1330// STATE IDENTIFICATION: (32 bit and 64 bit w/o COOPS)1331// -----------------------------------------------------1332// FREE: klass_word & 1 == 1; mark_word holds block size1333//1334// OBJECT: klass_word installed; klass_word != 0 && klass_word & 1 == 0;1335// obj->size() computes correct size1336//1337// TRANSIENT: klass_word == 0; size is indeterminate until we become an OBJECT1338//1339// STATE IDENTIFICATION: (64 bit+COOPS)1340// ------------------------------------1341// FREE: mark_word & CMS_FREE_BIT == 1; mark_word & ~CMS_FREE_BIT gives block_size1342//1343// OBJECT: klass_word installed; klass_word != 0;1344// obj->size() computes correct size1345//1346// TRANSIENT: klass_word == 0; size is indeterminate until we become an OBJECT1347//1348//1349// STATE TRANSITION DIAGRAM1350//1351// mut / parnew mut / parnew1352// FREE --------------------> TRANSIENT ---------------------> OBJECT --|1353// ^ |1354// |------------------------ DEAD <------------------------------------|1355// sweep mut1356//1357// While a block is in TRANSIENT state its size cannot be determined1358// so readers will either need to come back later or stall until1359// the size can be determined. Note that for the case of direct1360// allocation, P-bits, when available, may be used to determine the1361// size of an object that may not yet have been initialized.13621363// Things to support parallel young-gen collection.1364oop1365ConcurrentMarkSweepGeneration::par_promote(int thread_num,1366oop old, markOop m,1367size_t word_sz) {1368#ifndef PRODUCT1369if (Universe::heap()->promotion_should_fail()) {1370return NULL;1371}1372#endif // #ifndef PRODUCT13731374CMSParGCThreadState* ps = _par_gc_thread_states[thread_num];1375PromotionInfo* promoInfo = &ps->promo;1376// if we are tracking promotions, then first ensure space for1377// promotion (including spooling space for saving header if necessary).1378// then allocate and copy, then track promoted info if needed.1379// When tracking (see PromotionInfo::track()), the mark word may1380// be displaced and in this case restoration of the mark word1381// occurs in the (oop_since_save_marks_)iterate phase.1382if (promoInfo->tracking() && !promoInfo->ensure_spooling_space()) {1383// Out of space for allocating spooling buffers;1384// try expanding and allocating spooling buffers.1385if (!expand_and_ensure_spooling_space(promoInfo)) {1386return NULL;1387}1388}1389assert(promoInfo->has_spooling_space(), "Control point invariant");1390const size_t alloc_sz = CompactibleFreeListSpace::adjustObjectSize(word_sz);1391HeapWord* obj_ptr = ps->lab.alloc(alloc_sz);1392if (obj_ptr == NULL) {1393obj_ptr = expand_and_par_lab_allocate(ps, alloc_sz);1394if (obj_ptr == NULL) {1395return NULL;1396}1397}1398oop obj = oop(obj_ptr);1399OrderAccess::storestore();1400assert(obj->klass_or_null() == NULL, "Object should be uninitialized here.");1401assert(!((FreeChunk*)obj_ptr)->is_free(), "Error, block will look free but show wrong size");1402// IMPORTANT: See note on object initialization for CMS above.1403// Otherwise, copy the object. Here we must be careful to insert the1404// klass pointer last, since this marks the block as an allocated object.1405// Except with compressed oops it's the mark word.1406HeapWord* old_ptr = (HeapWord*)old;1407// Restore the mark word copied above.1408obj->set_mark(m);1409assert(obj->klass_or_null() == NULL, "Object should be uninitialized here.");1410assert(!((FreeChunk*)obj_ptr)->is_free(), "Error, block will look free but show wrong size");1411OrderAccess::storestore();14121413if (UseCompressedClassPointers) {1414// Copy gap missed by (aligned) header size calculation below1415obj->set_klass_gap(old->klass_gap());1416}1417if (word_sz > (size_t)oopDesc::header_size()) {1418Copy::aligned_disjoint_words(old_ptr + oopDesc::header_size(),1419obj_ptr + oopDesc::header_size(),1420word_sz - oopDesc::header_size());1421}14221423// Now we can track the promoted object, if necessary. We take care1424// to delay the transition from uninitialized to full object1425// (i.e., insertion of klass pointer) until after, so that it1426// atomically becomes a promoted object.1427if (promoInfo->tracking()) {1428promoInfo->track((PromotedObject*)obj, old->klass());1429}1430assert(obj->klass_or_null() == NULL, "Object should be uninitialized here.");1431assert(!((FreeChunk*)obj_ptr)->is_free(), "Error, block will look free but show wrong size");1432assert(old->is_oop(), "Will use and dereference old klass ptr below");14331434// Finally, install the klass pointer (this should be volatile).1435OrderAccess::storestore();1436obj->set_klass(old->klass());1437// We should now be able to calculate the right size for this object1438assert(obj->is_oop() && obj->size() == (int)word_sz, "Error, incorrect size computed for promoted object");14391440collector()->promoted(true, // parallel1441obj_ptr, old->is_objArray(), word_sz);14421443NOT_PRODUCT(1444Atomic::inc_ptr(&_numObjectsPromoted);1445Atomic::add_ptr(alloc_sz, &_numWordsPromoted);1446)14471448return obj;1449}14501451void1452ConcurrentMarkSweepGeneration::1453par_promote_alloc_undo(int thread_num,1454HeapWord* obj, size_t word_sz) {1455// CMS does not support promotion undo.1456ShouldNotReachHere();1457}14581459void1460ConcurrentMarkSweepGeneration::1461par_promote_alloc_done(int thread_num) {1462CMSParGCThreadState* ps = _par_gc_thread_states[thread_num];1463ps->lab.retire(thread_num);1464}14651466void1467ConcurrentMarkSweepGeneration::1468par_oop_since_save_marks_iterate_done(int thread_num) {1469CMSParGCThreadState* ps = _par_gc_thread_states[thread_num];1470ParScanWithoutBarrierClosure* dummy_cl = NULL;1471ps->promo.promoted_oops_iterate_nv(dummy_cl);1472}14731474bool ConcurrentMarkSweepGeneration::should_collect(bool full,1475size_t size,1476bool tlab)1477{1478// We allow a STW collection only if a full1479// collection was requested.1480return full || should_allocate(size, tlab); // FIX ME !!!1481// This and promotion failure handling are connected at the1482// hip and should be fixed by untying them.1483}14841485bool CMSCollector::shouldConcurrentCollect() {1486if (_full_gc_requested) {1487if (Verbose && PrintGCDetails) {1488gclog_or_tty->print_cr("CMSCollector: collect because of explicit "1489" gc request (or gc_locker)");1490}1491return true;1492}14931494// For debugging purposes, change the type of collection.1495// If the rotation is not on the concurrent collection1496// type, don't start a concurrent collection.1497NOT_PRODUCT(1498if (RotateCMSCollectionTypes &&1499(_cmsGen->debug_collection_type() !=1500ConcurrentMarkSweepGeneration::Concurrent_collection_type)) {1501assert(_cmsGen->debug_collection_type() !=1502ConcurrentMarkSweepGeneration::Unknown_collection_type,1503"Bad cms collection type");1504return false;1505}1506)15071508FreelistLocker x(this);1509// ------------------------------------------------------------------1510// Print out lots of information which affects the initiation of1511// a collection.1512if (PrintCMSInitiationStatistics && stats().valid()) {1513gclog_or_tty->print("CMSCollector shouldConcurrentCollect: ");1514gclog_or_tty->stamp();1515gclog_or_tty->cr();1516stats().print_on(gclog_or_tty);1517gclog_or_tty->print_cr("time_until_cms_gen_full %3.7f",1518stats().time_until_cms_gen_full());1519gclog_or_tty->print_cr("free=" SIZE_FORMAT, _cmsGen->free());1520gclog_or_tty->print_cr("contiguous_available=" SIZE_FORMAT,1521_cmsGen->contiguous_available());1522gclog_or_tty->print_cr("promotion_rate=%g", stats().promotion_rate());1523gclog_or_tty->print_cr("cms_allocation_rate=%g", stats().cms_allocation_rate());1524gclog_or_tty->print_cr("occupancy=%3.7f", _cmsGen->occupancy());1525gclog_or_tty->print_cr("initiatingOccupancy=%3.7f", _cmsGen->initiating_occupancy());1526gclog_or_tty->print_cr("cms_time_since_begin=%3.7f", stats().cms_time_since_begin());1527gclog_or_tty->print_cr("cms_time_since_end=%3.7f", stats().cms_time_since_end());1528gclog_or_tty->print_cr("metadata initialized %d",1529MetaspaceGC::should_concurrent_collect());1530}1531// ------------------------------------------------------------------15321533// If the estimated time to complete a cms collection (cms_duration())1534// is less than the estimated time remaining until the cms generation1535// is full, start a collection.1536if (!UseCMSInitiatingOccupancyOnly) {1537if (stats().valid()) {1538if (stats().time_until_cms_start() == 0.0) {1539return true;1540}1541} else {1542// We want to conservatively collect somewhat early in order1543// to try and "bootstrap" our CMS/promotion statistics;1544// this branch will not fire after the first successful CMS1545// collection because the stats should then be valid.1546if (_cmsGen->occupancy() >= _bootstrap_occupancy) {1547if (Verbose && PrintGCDetails) {1548gclog_or_tty->print_cr(1549" CMSCollector: collect for bootstrapping statistics:"1550" occupancy = %f, boot occupancy = %f", _cmsGen->occupancy(),1551_bootstrap_occupancy);1552}1553return true;1554}1555}1556}15571558// Otherwise, we start a collection cycle if1559// old gen want a collection cycle started. Each may use1560// an appropriate criterion for making this decision.1561// XXX We need to make sure that the gen expansion1562// criterion dovetails well with this. XXX NEED TO FIX THIS1563if (_cmsGen->should_concurrent_collect()) {1564if (Verbose && PrintGCDetails) {1565gclog_or_tty->print_cr("CMS old gen initiated");1566}1567return true;1568}15691570// We start a collection if we believe an incremental collection may fail;1571// this is not likely to be productive in practice because it's probably too1572// late anyway.1573GenCollectedHeap* gch = GenCollectedHeap::heap();1574assert(gch->collector_policy()->is_two_generation_policy(),1575"You may want to check the correctness of the following");1576if (gch->incremental_collection_will_fail(true /* consult_young */)) {1577if (Verbose && PrintGCDetails) {1578gclog_or_tty->print("CMSCollector: collect because incremental collection will fail ");1579}1580return true;1581}15821583if (MetaspaceGC::should_concurrent_collect()) {1584if (Verbose && PrintGCDetails) {1585gclog_or_tty->print("CMSCollector: collect for metadata allocation ");1586}1587return true;1588}15891590// CMSTriggerInterval starts a CMS cycle if enough time has passed.1591if (CMSTriggerInterval >= 0) {1592if (CMSTriggerInterval == 0) {1593// Trigger always1594return true;1595}15961597// Check the CMS time since begin (we do not check the stats validity1598// as we want to be able to trigger the first CMS cycle as well)1599if (stats().cms_time_since_begin() >= (CMSTriggerInterval / ((double) MILLIUNITS))) {1600if (Verbose && PrintGCDetails) {1601if (stats().valid()) {1602gclog_or_tty->print_cr("CMSCollector: collect because of trigger interval (time since last begin %3.7f secs)",1603stats().cms_time_since_begin());1604} else {1605gclog_or_tty->print_cr("CMSCollector: collect because of trigger interval (first collection)");1606}1607}1608return true;1609}1610}16111612return false;1613}16141615void CMSCollector::set_did_compact(bool v) { _cmsGen->set_did_compact(v); }16161617// Clear _expansion_cause fields of constituent generations1618void CMSCollector::clear_expansion_cause() {1619_cmsGen->clear_expansion_cause();1620}16211622// We should be conservative in starting a collection cycle. To1623// start too eagerly runs the risk of collecting too often in the1624// extreme. To collect too rarely falls back on full collections,1625// which works, even if not optimum in terms of concurrent work.1626// As a work around for too eagerly collecting, use the flag1627// UseCMSInitiatingOccupancyOnly. This also has the advantage of1628// giving the user an easily understandable way of controlling the1629// collections.1630// We want to start a new collection cycle if any of the following1631// conditions hold:1632// . our current occupancy exceeds the configured initiating occupancy1633// for this generation, or1634// . we recently needed to expand this space and have not, since that1635// expansion, done a collection of this generation, or1636// . the underlying space believes that it may be a good idea to initiate1637// a concurrent collection (this may be based on criteria such as the1638// following: the space uses linear allocation and linear allocation is1639// going to fail, or there is believed to be excessive fragmentation in1640// the generation, etc... or ...1641// [.(currently done by CMSCollector::shouldConcurrentCollect() only for1642// the case of the old generation; see CR 6543076):1643// we may be approaching a point at which allocation requests may fail because1644// we will be out of sufficient free space given allocation rate estimates.]1645bool ConcurrentMarkSweepGeneration::should_concurrent_collect() const {16461647assert_lock_strong(freelistLock());1648if (occupancy() > initiating_occupancy()) {1649if (PrintGCDetails && Verbose) {1650gclog_or_tty->print(" %s: collect because of occupancy %f / %f ",1651short_name(), occupancy(), initiating_occupancy());1652}1653return true;1654}1655if (UseCMSInitiatingOccupancyOnly) {1656return false;1657}1658if (expansion_cause() == CMSExpansionCause::_satisfy_allocation) {1659if (PrintGCDetails && Verbose) {1660gclog_or_tty->print(" %s: collect because expanded for allocation ",1661short_name());1662}1663return true;1664}1665if (_cmsSpace->should_concurrent_collect()) {1666if (PrintGCDetails && Verbose) {1667gclog_or_tty->print(" %s: collect because cmsSpace says so ",1668short_name());1669}1670return true;1671}1672return false;1673}16741675void ConcurrentMarkSweepGeneration::collect(bool full,1676bool clear_all_soft_refs,1677size_t size,1678bool tlab)1679{1680collector()->collect(full, clear_all_soft_refs, size, tlab);1681}16821683void CMSCollector::collect(bool full,1684bool clear_all_soft_refs,1685size_t size,1686bool tlab)1687{1688if (!UseCMSCollectionPassing && _collectorState > Idling) {1689// For debugging purposes skip the collection if the state1690// is not currently idle1691if (TraceCMSState) {1692gclog_or_tty->print_cr("Thread " INTPTR_FORMAT " skipped full:%d CMS state %d",1693Thread::current(), full, _collectorState);1694}1695return;1696}16971698// The following "if" branch is present for defensive reasons.1699// In the current uses of this interface, it can be replaced with:1700// assert(!GC_locker.is_active(), "Can't be called otherwise");1701// But I am not placing that assert here to allow future1702// generality in invoking this interface.1703if (GC_locker::is_active()) {1704// A consistency test for GC_locker1705assert(GC_locker::needs_gc(), "Should have been set already");1706// Skip this foreground collection, instead1707// expanding the heap if necessary.1708// Need the free list locks for the call to free() in compute_new_size()1709compute_new_size();1710return;1711}1712acquire_control_and_collect(full, clear_all_soft_refs);1713_full_gcs_since_conc_gc++;1714}17151716void CMSCollector::request_full_gc(unsigned int full_gc_count, GCCause::Cause cause) {1717GenCollectedHeap* gch = GenCollectedHeap::heap();1718unsigned int gc_count = gch->total_full_collections();1719if (gc_count == full_gc_count) {1720MutexLockerEx y(CGC_lock, Mutex::_no_safepoint_check_flag);1721_full_gc_requested = true;1722_full_gc_cause = cause;1723CGC_lock->notify(); // nudge CMS thread1724} else {1725assert(gc_count > full_gc_count, "Error: causal loop");1726}1727}17281729bool CMSCollector::is_external_interruption() {1730GCCause::Cause cause = GenCollectedHeap::heap()->gc_cause();1731return GCCause::is_user_requested_gc(cause) ||1732GCCause::is_serviceability_requested_gc(cause);1733}17341735void CMSCollector::report_concurrent_mode_interruption() {1736if (is_external_interruption()) {1737if (PrintGCDetails) {1738gclog_or_tty->print(" (concurrent mode interrupted)");1739}1740} else {1741if (PrintGCDetails) {1742gclog_or_tty->print(" (concurrent mode failure)");1743}1744_gc_tracer_cm->report_concurrent_mode_failure();1745}1746}174717481749// The foreground and background collectors need to coordinate in order1750// to make sure that they do not mutually interfere with CMS collections.1751// When a background collection is active,1752// the foreground collector may need to take over (preempt) and1753// synchronously complete an ongoing collection. Depending on the1754// frequency of the background collections and the heap usage1755// of the application, this preemption can be seldom or frequent.1756// There are only certain1757// points in the background collection that the "collection-baton"1758// can be passed to the foreground collector.1759//1760// The foreground collector will wait for the baton before1761// starting any part of the collection. The foreground collector1762// will only wait at one location.1763//1764// The background collector will yield the baton before starting a new1765// phase of the collection (e.g., before initial marking, marking from roots,1766// precleaning, final re-mark, sweep etc.) This is normally done at the head1767// of the loop which switches the phases. The background collector does some1768// of the phases (initial mark, final re-mark) with the world stopped.1769// Because of locking involved in stopping the world,1770// the foreground collector should not block waiting for the background1771// collector when it is doing a stop-the-world phase. The background1772// collector will yield the baton at an additional point just before1773// it enters a stop-the-world phase. Once the world is stopped, the1774// background collector checks the phase of the collection. If the1775// phase has not changed, it proceeds with the collection. If the1776// phase has changed, it skips that phase of the collection. See1777// the comments on the use of the Heap_lock in collect_in_background().1778//1779// Variable used in baton passing.1780// _foregroundGCIsActive - Set to true by the foreground collector when1781// it wants the baton. The foreground clears it when it has finished1782// the collection.1783// _foregroundGCShouldWait - Set to true by the background collector1784// when it is running. The foreground collector waits while1785// _foregroundGCShouldWait is true.1786// CGC_lock - monitor used to protect access to the above variables1787// and to notify the foreground and background collectors.1788// _collectorState - current state of the CMS collection.1789//1790// The foreground collector1791// acquires the CGC_lock1792// sets _foregroundGCIsActive1793// waits on the CGC_lock for _foregroundGCShouldWait to be false1794// various locks acquired in preparation for the collection1795// are released so as not to block the background collector1796// that is in the midst of a collection1797// proceeds with the collection1798// clears _foregroundGCIsActive1799// returns1800//1801// The background collector in a loop iterating on the phases of the1802// collection1803// acquires the CGC_lock1804// sets _foregroundGCShouldWait1805// if _foregroundGCIsActive is set1806// clears _foregroundGCShouldWait, notifies _CGC_lock1807// waits on _CGC_lock for _foregroundGCIsActive to become false1808// and exits the loop.1809// otherwise1810// proceed with that phase of the collection1811// if the phase is a stop-the-world phase,1812// yield the baton once more just before enqueueing1813// the stop-world CMS operation (executed by the VM thread).1814// returns after all phases of the collection are done1815//18161817void CMSCollector::acquire_control_and_collect(bool full,1818bool clear_all_soft_refs) {1819assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");1820assert(!Thread::current()->is_ConcurrentGC_thread(),1821"shouldn't try to acquire control from self!");18221823// Start the protocol for acquiring control of the1824// collection from the background collector (aka CMS thread).1825assert(ConcurrentMarkSweepThread::vm_thread_has_cms_token(),1826"VM thread should have CMS token");1827// Remember the possibly interrupted state of an ongoing1828// concurrent collection1829CollectorState first_state = _collectorState;18301831// Signal to a possibly ongoing concurrent collection that1832// we want to do a foreground collection.1833_foregroundGCIsActive = true;18341835// Disable incremental mode during a foreground collection.1836ICMSDisabler icms_disabler;18371838// release locks and wait for a notify from the background collector1839// releasing the locks in only necessary for phases which1840// do yields to improve the granularity of the collection.1841assert_lock_strong(bitMapLock());1842// We need to lock the Free list lock for the space that we are1843// currently collecting.1844assert(haveFreelistLocks(), "Must be holding free list locks");1845bitMapLock()->unlock();1846releaseFreelistLocks();1847{1848MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);1849if (_foregroundGCShouldWait) {1850// We are going to be waiting for action for the CMS thread;1851// it had better not be gone (for instance at shutdown)!1852assert(ConcurrentMarkSweepThread::cmst() != NULL,1853"CMS thread must be running");1854// Wait here until the background collector gives us the go-ahead1855ConcurrentMarkSweepThread::clear_CMS_flag(1856ConcurrentMarkSweepThread::CMS_vm_has_token); // release token1857// Get a possibly blocked CMS thread going:1858// Note that we set _foregroundGCIsActive true above,1859// without protection of the CGC_lock.1860CGC_lock->notify();1861assert(!ConcurrentMarkSweepThread::vm_thread_wants_cms_token(),1862"Possible deadlock");1863while (_foregroundGCShouldWait) {1864// wait for notification1865CGC_lock->wait(Mutex::_no_safepoint_check_flag);1866// Possibility of delay/starvation here, since CMS token does1867// not know to give priority to VM thread? Actually, i think1868// there wouldn't be any delay/starvation, but the proof of1869// that "fact" (?) appears non-trivial. XXX 20011219YSR1870}1871ConcurrentMarkSweepThread::set_CMS_flag(1872ConcurrentMarkSweepThread::CMS_vm_has_token);1873}1874}1875// The CMS_token is already held. Get back the other locks.1876assert(ConcurrentMarkSweepThread::vm_thread_has_cms_token(),1877"VM thread should have CMS token");1878getFreelistLocks();1879bitMapLock()->lock_without_safepoint_check();1880if (TraceCMSState) {1881gclog_or_tty->print_cr("CMS foreground collector has asked for control "1882INTPTR_FORMAT " with first state %d", Thread::current(), first_state);1883gclog_or_tty->print_cr(" gets control with state %d", _collectorState);1884}18851886// Check if we need to do a compaction, or if not, whether1887// we need to start the mark-sweep from scratch.1888bool should_compact = false;1889bool should_start_over = false;1890decide_foreground_collection_type(clear_all_soft_refs,1891&should_compact, &should_start_over);18921893NOT_PRODUCT(1894if (RotateCMSCollectionTypes) {1895if (_cmsGen->debug_collection_type() ==1896ConcurrentMarkSweepGeneration::MSC_foreground_collection_type) {1897should_compact = true;1898} else if (_cmsGen->debug_collection_type() ==1899ConcurrentMarkSweepGeneration::MS_foreground_collection_type) {1900should_compact = false;1901}1902}1903)19041905if (first_state > Idling) {1906report_concurrent_mode_interruption();1907}19081909set_did_compact(should_compact);1910if (should_compact) {1911// If the collection is being acquired from the background1912// collector, there may be references on the discovered1913// references lists that have NULL referents (being those1914// that were concurrently cleared by a mutator) or1915// that are no longer active (having been enqueued concurrently1916// by the mutator).1917// Scrub the list of those references because Mark-Sweep-Compact1918// code assumes referents are not NULL and that all discovered1919// Reference objects are active.1920ref_processor()->clean_up_discovered_references();19211922if (first_state > Idling) {1923save_heap_summary();1924}19251926do_compaction_work(clear_all_soft_refs);19271928// Has the GC time limit been exceeded?1929DefNewGeneration* young_gen = _young_gen->as_DefNewGeneration();1930size_t max_eden_size = young_gen->max_eden_size();1931GenCollectedHeap* gch = GenCollectedHeap::heap();1932GCCause::Cause gc_cause = gch->gc_cause();1933size_policy()->check_gc_overhead_limit(_young_gen->used(),1934young_gen->eden()->used(),1935_cmsGen->max_capacity(),1936max_eden_size,1937full,1938gc_cause,1939gch->collector_policy());1940} else {1941do_mark_sweep_work(clear_all_soft_refs, first_state,1942should_start_over);1943}1944// Reset the expansion cause, now that we just completed1945// a collection cycle.1946clear_expansion_cause();1947_foregroundGCIsActive = false;1948return;1949}19501951// Resize the tenured generation1952// after obtaining the free list locks for the1953// two generations.1954void CMSCollector::compute_new_size() {1955assert_locked_or_safepoint(Heap_lock);1956FreelistLocker z(this);1957MetaspaceGC::compute_new_size();1958_cmsGen->compute_new_size_free_list();1959// recalculate CMS used space after CMS collection1960_cmsGen->cmsSpace()->recalculate_used_stable();1961}19621963// A work method used by foreground collection to determine1964// what type of collection (compacting or not, continuing or fresh)1965// it should do.1966// NOTE: the intent is to make UseCMSCompactAtFullCollection1967// and CMSCompactWhenClearAllSoftRefs the default in the future1968// and do away with the flags after a suitable period.1969void CMSCollector::decide_foreground_collection_type(1970bool clear_all_soft_refs, bool* should_compact,1971bool* should_start_over) {1972// Normally, we'll compact only if the UseCMSCompactAtFullCollection1973// flag is set, and we have either requested a System.gc() or1974// the number of full gc's since the last concurrent cycle1975// has exceeded the threshold set by CMSFullGCsBeforeCompaction,1976// or if an incremental collection has failed1977GenCollectedHeap* gch = GenCollectedHeap::heap();1978assert(gch->collector_policy()->is_two_generation_policy(),1979"You may want to check the correctness of the following");1980// Inform cms gen if this was due to partial collection failing.1981// The CMS gen may use this fact to determine its expansion policy.1982if (gch->incremental_collection_will_fail(false /* don't consult_young */)) {1983assert(!_cmsGen->incremental_collection_failed(),1984"Should have been noticed, reacted to and cleared");1985_cmsGen->set_incremental_collection_failed();1986}1987*should_compact =1988UseCMSCompactAtFullCollection &&1989((_full_gcs_since_conc_gc >= CMSFullGCsBeforeCompaction) ||1990GCCause::is_user_requested_gc(gch->gc_cause()) ||1991gch->incremental_collection_will_fail(true /* consult_young */));1992*should_start_over = false;1993if (clear_all_soft_refs && !*should_compact) {1994// We are about to do a last ditch collection attempt1995// so it would normally make sense to do a compaction1996// to reclaim as much space as possible.1997if (CMSCompactWhenClearAllSoftRefs) {1998// Default: The rationale is that in this case either1999// we are past the final marking phase, in which case2000// we'd have to start over, or so little has been done2001// that there's little point in saving that work. Compaction2002// appears to be the sensible choice in either case.2003*should_compact = true;2004} else {2005// We have been asked to clear all soft refs, but not to2006// compact. Make sure that we aren't past the final checkpoint2007// phase, for that is where we process soft refs. If we are already2008// past that phase, we'll need to redo the refs discovery phase and2009// if necessary clear soft refs that weren't previously2010// cleared. We do so by remembering the phase in which2011// we came in, and if we are past the refs processing2012// phase, we'll choose to just redo the mark-sweep2013// collection from scratch.2014if (_collectorState > FinalMarking) {2015// We are past the refs processing phase;2016// start over and do a fresh synchronous CMS cycle2017_collectorState = Resetting; // skip to reset to start new cycle2018reset(false /* == !asynch */);2019*should_start_over = true;2020} // else we can continue a possibly ongoing current cycle2021}2022}2023}20242025// A work method used by the foreground collector to do2026// a mark-sweep-compact.2027void CMSCollector::do_compaction_work(bool clear_all_soft_refs) {2028GenCollectedHeap* gch = GenCollectedHeap::heap();20292030STWGCTimer* gc_timer = GenMarkSweep::gc_timer();2031gc_timer->register_gc_start();20322033SerialOldTracer* gc_tracer = GenMarkSweep::gc_tracer();2034gc_tracer->report_gc_start(gch->gc_cause(), gc_timer->gc_start());20352036GCTraceTime t("CMS:MSC ", PrintGCDetails && Verbose, true, NULL, gc_tracer->gc_id());2037if (PrintGC && Verbose && !(GCCause::is_user_requested_gc(gch->gc_cause()))) {2038gclog_or_tty->print_cr("Compact ConcurrentMarkSweepGeneration after %d "2039"collections passed to foreground collector", _full_gcs_since_conc_gc);2040}20412042// Sample collection interval time and reset for collection pause.2043if (UseAdaptiveSizePolicy) {2044size_policy()->msc_collection_begin();2045}20462047// Temporarily widen the span of the weak reference processing to2048// the entire heap.2049MemRegion new_span(GenCollectedHeap::heap()->reserved_region());2050ReferenceProcessorSpanMutator rp_mut_span(ref_processor(), new_span);2051// Temporarily, clear the "is_alive_non_header" field of the2052// reference processor.2053ReferenceProcessorIsAliveMutator rp_mut_closure(ref_processor(), NULL);2054// Temporarily make reference _processing_ single threaded (non-MT).2055ReferenceProcessorMTProcMutator rp_mut_mt_processing(ref_processor(), false);2056// Temporarily make refs discovery atomic2057ReferenceProcessorAtomicMutator rp_mut_atomic(ref_processor(), true);2058// Temporarily make reference _discovery_ single threaded (non-MT)2059ReferenceProcessorMTDiscoveryMutator rp_mut_discovery(ref_processor(), false);20602061ref_processor()->set_enqueuing_is_done(false);2062ref_processor()->enable_discovery(false /*verify_disabled*/, false /*check_no_refs*/);2063ref_processor()->setup_policy(clear_all_soft_refs);2064// If an asynchronous collection finishes, the _modUnionTable is2065// all clear. If we are assuming the collection from an asynchronous2066// collection, clear the _modUnionTable.2067assert(_collectorState != Idling || _modUnionTable.isAllClear(),2068"_modUnionTable should be clear if the baton was not passed");2069_modUnionTable.clear_all();2070assert(_collectorState != Idling || _ct->klass_rem_set()->mod_union_is_clear(),2071"mod union for klasses should be clear if the baton was passed");2072_ct->klass_rem_set()->clear_mod_union();20732074// We must adjust the allocation statistics being maintained2075// in the free list space. We do so by reading and clearing2076// the sweep timer and updating the block flux rate estimates below.2077assert(!_intra_sweep_timer.is_active(), "_intra_sweep_timer should be inactive");2078if (_inter_sweep_timer.is_active()) {2079_inter_sweep_timer.stop();2080// Note that we do not use this sample to update the _inter_sweep_estimate.2081_cmsGen->cmsSpace()->beginSweepFLCensus((float)(_inter_sweep_timer.seconds()),2082_inter_sweep_estimate.padded_average(),2083_intra_sweep_estimate.padded_average());2084}20852086GenMarkSweep::invoke_at_safepoint(_cmsGen->level(),2087ref_processor(), clear_all_soft_refs);2088#ifdef ASSERT2089CompactibleFreeListSpace* cms_space = _cmsGen->cmsSpace();2090size_t free_size = cms_space->free();2091assert(free_size ==2092pointer_delta(cms_space->end(), cms_space->compaction_top())2093* HeapWordSize,2094"All the free space should be compacted into one chunk at top");2095assert(cms_space->dictionary()->total_chunk_size(2096debug_only(cms_space->freelistLock())) == 0 ||2097cms_space->totalSizeInIndexedFreeLists() == 0,2098"All the free space should be in a single chunk");2099size_t num = cms_space->totalCount();2100assert((free_size == 0 && num == 0) ||2101(free_size > 0 && (num == 1 || num == 2)),2102"There should be at most 2 free chunks after compaction");2103#endif // ASSERT2104_collectorState = Resetting;2105assert(_restart_addr == NULL,2106"Should have been NULL'd before baton was passed");2107reset(false /* == !asynch */);2108_cmsGen->reset_after_compaction();2109_concurrent_cycles_since_last_unload = 0;21102111// Clear any data recorded in the PLAB chunk arrays.2112if (_survivor_plab_array != NULL) {2113reset_survivor_plab_arrays();2114}21152116// Adjust the per-size allocation stats for the next epoch.2117_cmsGen->cmsSpace()->endSweepFLCensus(sweep_count() /* fake */);2118// Restart the "inter sweep timer" for the next epoch.2119_inter_sweep_timer.reset();2120_inter_sweep_timer.start();21212122// Sample collection pause time and reset for collection interval.2123if (UseAdaptiveSizePolicy) {2124size_policy()->msc_collection_end(gch->gc_cause());2125}21262127gc_timer->register_gc_end();21282129gc_tracer->report_gc_end(gc_timer->gc_end(), gc_timer->time_partitions());21302131// For a mark-sweep-compact, compute_new_size() will be called2132// in the heap's do_collection() method.2133}21342135// A work method used by the foreground collector to do2136// a mark-sweep, after taking over from a possibly on-going2137// concurrent mark-sweep collection.2138void CMSCollector::do_mark_sweep_work(bool clear_all_soft_refs,2139CollectorState first_state, bool should_start_over) {2140if (PrintGC && Verbose) {2141gclog_or_tty->print_cr("Pass concurrent collection to foreground "2142"collector with count %d",2143_full_gcs_since_conc_gc);2144}2145switch (_collectorState) {2146case Idling:2147if (first_state == Idling || should_start_over) {2148// The background GC was not active, or should2149// restarted from scratch; start the cycle.2150_collectorState = InitialMarking;2151}2152// If first_state was not Idling, then a background GC2153// was in progress and has now finished. No need to do it2154// again. Leave the state as Idling.2155break;2156case Precleaning:2157// In the foreground case don't do the precleaning since2158// it is not done concurrently and there is extra work2159// required.2160_collectorState = FinalMarking;2161}2162collect_in_foreground(clear_all_soft_refs, GenCollectedHeap::heap()->gc_cause());21632164// For a mark-sweep, compute_new_size() will be called2165// in the heap's do_collection() method.2166}216721682169void CMSCollector::print_eden_and_survivor_chunk_arrays() {2170DefNewGeneration* dng = _young_gen->as_DefNewGeneration();2171EdenSpace* eden_space = dng->eden();2172ContiguousSpace* from_space = dng->from();2173ContiguousSpace* to_space = dng->to();2174// Eden2175if (_eden_chunk_array != NULL) {2176gclog_or_tty->print_cr("eden " PTR_FORMAT "-" PTR_FORMAT "-" PTR_FORMAT "(" SIZE_FORMAT ")",2177eden_space->bottom(), eden_space->top(),2178eden_space->end(), eden_space->capacity());2179gclog_or_tty->print_cr("_eden_chunk_index=" SIZE_FORMAT ", "2180"_eden_chunk_capacity=" SIZE_FORMAT,2181_eden_chunk_index, _eden_chunk_capacity);2182for (size_t i = 0; i < _eden_chunk_index; i++) {2183gclog_or_tty->print_cr("_eden_chunk_array[" SIZE_FORMAT "]=" PTR_FORMAT,2184i, _eden_chunk_array[i]);2185}2186}2187// Survivor2188if (_survivor_chunk_array != NULL) {2189gclog_or_tty->print_cr("survivor " PTR_FORMAT "-" PTR_FORMAT "-" PTR_FORMAT "(" SIZE_FORMAT ")",2190from_space->bottom(), from_space->top(),2191from_space->end(), from_space->capacity());2192gclog_or_tty->print_cr("_survivor_chunk_index=" SIZE_FORMAT ", "2193"_survivor_chunk_capacity=" SIZE_FORMAT,2194_survivor_chunk_index, _survivor_chunk_capacity);2195for (size_t i = 0; i < _survivor_chunk_index; i++) {2196gclog_or_tty->print_cr("_survivor_chunk_array[" SIZE_FORMAT "]=" PTR_FORMAT,2197i, _survivor_chunk_array[i]);2198}2199}2200}22012202void CMSCollector::getFreelistLocks() const {2203// Get locks for all free lists in all generations that this2204// collector is responsible for2205_cmsGen->freelistLock()->lock_without_safepoint_check();2206}22072208void CMSCollector::releaseFreelistLocks() const {2209// Release locks for all free lists in all generations that this2210// collector is responsible for2211_cmsGen->freelistLock()->unlock();2212}22132214bool CMSCollector::haveFreelistLocks() const {2215// Check locks for all free lists in all generations that this2216// collector is responsible for2217assert_lock_strong(_cmsGen->freelistLock());2218PRODUCT_ONLY(ShouldNotReachHere());2219return true;2220}22212222// A utility class that is used by the CMS collector to2223// temporarily "release" the foreground collector from its2224// usual obligation to wait for the background collector to2225// complete an ongoing phase before proceeding.2226class ReleaseForegroundGC: public StackObj {2227private:2228CMSCollector* _c;2229public:2230ReleaseForegroundGC(CMSCollector* c) : _c(c) {2231assert(_c->_foregroundGCShouldWait, "Else should not need to call");2232MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);2233// allow a potentially blocked foreground collector to proceed2234_c->_foregroundGCShouldWait = false;2235if (_c->_foregroundGCIsActive) {2236CGC_lock->notify();2237}2238assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(),2239"Possible deadlock");2240}22412242~ReleaseForegroundGC() {2243assert(!_c->_foregroundGCShouldWait, "Usage protocol violation?");2244MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);2245_c->_foregroundGCShouldWait = true;2246}2247};22482249// There are separate collect_in_background and collect_in_foreground because of2250// the different locking requirements of the background collector and the2251// foreground collector. There was originally an attempt to share2252// one "collect" method between the background collector and the foreground2253// collector but the if-then-else required made it cleaner to have2254// separate methods.2255void CMSCollector::collect_in_background(bool clear_all_soft_refs, GCCause::Cause cause) {2256assert(Thread::current()->is_ConcurrentGC_thread(),2257"A CMS asynchronous collection is only allowed on a CMS thread.");22582259GenCollectedHeap* gch = GenCollectedHeap::heap();2260{2261bool safepoint_check = Mutex::_no_safepoint_check_flag;2262MutexLockerEx hl(Heap_lock, safepoint_check);2263FreelistLocker fll(this);2264MutexLockerEx x(CGC_lock, safepoint_check);2265if (_foregroundGCIsActive || !UseAsyncConcMarkSweepGC) {2266// The foreground collector is active or we're2267// not using asynchronous collections. Skip this2268// background collection.2269assert(!_foregroundGCShouldWait, "Should be clear");2270return;2271} else {2272assert(_collectorState == Idling, "Should be idling before start.");2273_collectorState = InitialMarking;2274register_gc_start(cause);2275// Reset the expansion cause, now that we are about to begin2276// a new cycle.2277clear_expansion_cause();22782279// Clear the MetaspaceGC flag since a concurrent collection2280// is starting but also clear it after the collection.2281MetaspaceGC::set_should_concurrent_collect(false);2282}2283// Decide if we want to enable class unloading as part of the2284// ensuing concurrent GC cycle.2285update_should_unload_classes();2286_full_gc_requested = false; // acks all outstanding full gc requests2287_full_gc_cause = GCCause::_no_gc;2288// Signal that we are about to start a collection2289gch->increment_total_full_collections(); // ... starting a collection cycle2290_collection_count_start = gch->total_full_collections();2291}22922293// Used for PrintGC2294size_t prev_used = 0;2295if (PrintGC && Verbose) {2296prev_used = _cmsGen->used(); // XXXPERM2297}22982299// The change of the collection state is normally done at this level;2300// the exceptions are phases that are executed while the world is2301// stopped. For those phases the change of state is done while the2302// world is stopped. For baton passing purposes this allows the2303// background collector to finish the phase and change state atomically.2304// The foreground collector cannot wait on a phase that is done2305// while the world is stopped because the foreground collector already2306// has the world stopped and would deadlock.2307while (_collectorState != Idling) {2308if (TraceCMSState) {2309gclog_or_tty->print_cr("Thread " INTPTR_FORMAT " in CMS state %d",2310Thread::current(), _collectorState);2311}2312// The foreground collector2313// holds the Heap_lock throughout its collection.2314// holds the CMS token (but not the lock)2315// except while it is waiting for the background collector to yield.2316//2317// The foreground collector should be blocked (not for long)2318// if the background collector is about to start a phase2319// executed with world stopped. If the background2320// collector has already started such a phase, the2321// foreground collector is blocked waiting for the2322// Heap_lock. The stop-world phases (InitialMarking and FinalMarking)2323// are executed in the VM thread.2324//2325// The locking order is2326// PendingListLock (PLL) -- if applicable (FinalMarking)2327// Heap_lock (both this & PLL locked in VM_CMS_Operation::prologue())2328// CMS token (claimed in2329// stop_world_and_do() -->2330// safepoint_synchronize() -->2331// CMSThread::synchronize())23322333{2334// Check if the FG collector wants us to yield.2335CMSTokenSync x(true); // is cms thread2336if (waitForForegroundGC()) {2337// We yielded to a foreground GC, nothing more to be2338// done this round.2339assert(_foregroundGCShouldWait == false, "We set it to false in "2340"waitForForegroundGC()");2341if (TraceCMSState) {2342gclog_or_tty->print_cr("CMS Thread " INTPTR_FORMAT2343" exiting collection CMS state %d",2344Thread::current(), _collectorState);2345}2346return;2347} else {2348// The background collector can run but check to see if the2349// foreground collector has done a collection while the2350// background collector was waiting to get the CGC_lock2351// above. If yes, break so that _foregroundGCShouldWait2352// is cleared before returning.2353if (_collectorState == Idling) {2354break;2355}2356}2357}23582359assert(_foregroundGCShouldWait, "Foreground collector, if active, "2360"should be waiting");23612362switch (_collectorState) {2363case InitialMarking:2364{2365ReleaseForegroundGC x(this);2366stats().record_cms_begin();2367VM_CMS_Initial_Mark initial_mark_op(this);2368VMThread::execute(&initial_mark_op);2369}2370// The collector state may be any legal state at this point2371// since the background collector may have yielded to the2372// foreground collector.2373break;2374case Marking:2375// initial marking in checkpointRootsInitialWork has been completed2376if (markFromRoots(true)) { // we were successful2377assert(_collectorState == Precleaning, "Collector state should "2378"have changed");2379} else {2380assert(_foregroundGCIsActive, "Internal state inconsistency");2381}2382break;2383case Precleaning:2384if (UseAdaptiveSizePolicy) {2385size_policy()->concurrent_precleaning_begin();2386}2387// marking from roots in markFromRoots has been completed2388preclean();2389if (UseAdaptiveSizePolicy) {2390size_policy()->concurrent_precleaning_end();2391}2392assert(_collectorState == AbortablePreclean ||2393_collectorState == FinalMarking,2394"Collector state should have changed");2395break;2396case AbortablePreclean:2397if (UseAdaptiveSizePolicy) {2398size_policy()->concurrent_phases_resume();2399}2400abortable_preclean();2401if (UseAdaptiveSizePolicy) {2402size_policy()->concurrent_precleaning_end();2403}2404assert(_collectorState == FinalMarking, "Collector state should "2405"have changed");2406break;2407case FinalMarking:2408{2409ReleaseForegroundGC x(this);24102411VM_CMS_Final_Remark final_remark_op(this);2412VMThread::execute(&final_remark_op);2413}2414assert(_foregroundGCShouldWait, "block post-condition");2415break;2416case Sweeping:2417if (UseAdaptiveSizePolicy) {2418size_policy()->concurrent_sweeping_begin();2419}2420// final marking in checkpointRootsFinal has been completed2421sweep(true);2422assert(_collectorState == Resizing, "Collector state change "2423"to Resizing must be done under the free_list_lock");2424_full_gcs_since_conc_gc = 0;24252426// Stop the timers for adaptive size policy for the concurrent phases2427if (UseAdaptiveSizePolicy) {2428size_policy()->concurrent_sweeping_end();2429size_policy()->concurrent_phases_end(gch->gc_cause(),2430gch->prev_gen(_cmsGen)->capacity(),2431_cmsGen->free());2432}24332434case Resizing: {2435// Sweeping has been completed...2436// At this point the background collection has completed.2437// Don't move the call to compute_new_size() down2438// into code that might be executed if the background2439// collection was preempted.2440{2441ReleaseForegroundGC x(this); // unblock FG collection2442MutexLockerEx y(Heap_lock, Mutex::_no_safepoint_check_flag);2443CMSTokenSync z(true); // not strictly needed.2444if (_collectorState == Resizing) {2445compute_new_size();2446save_heap_summary();2447_collectorState = Resetting;2448} else {2449assert(_collectorState == Idling, "The state should only change"2450" because the foreground collector has finished the collection");2451}2452}2453break;2454}2455case Resetting:2456// CMS heap resizing has been completed2457reset(true);2458assert(_collectorState == Idling, "Collector state should "2459"have changed");24602461MetaspaceGC::set_should_concurrent_collect(false);24622463stats().record_cms_end();2464// Don't move the concurrent_phases_end() and compute_new_size()2465// calls to here because a preempted background collection2466// has it's state set to "Resetting".2467break;2468case Idling:2469default:2470ShouldNotReachHere();2471break;2472}2473if (TraceCMSState) {2474gclog_or_tty->print_cr(" Thread " INTPTR_FORMAT " done - next CMS state %d",2475Thread::current(), _collectorState);2476}2477assert(_foregroundGCShouldWait, "block post-condition");2478}24792480// Should this be in gc_epilogue?2481collector_policy()->counters()->update_counters();24822483{2484// Clear _foregroundGCShouldWait and, in the event that the2485// foreground collector is waiting, notify it, before2486// returning.2487MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);2488_foregroundGCShouldWait = false;2489if (_foregroundGCIsActive) {2490CGC_lock->notify();2491}2492assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(),2493"Possible deadlock");2494}2495if (TraceCMSState) {2496gclog_or_tty->print_cr("CMS Thread " INTPTR_FORMAT2497" exiting collection CMS state %d",2498Thread::current(), _collectorState);2499}2500if (PrintGC && Verbose) {2501_cmsGen->print_heap_change(prev_used);2502}2503}25042505void CMSCollector::register_foreground_gc_start(GCCause::Cause cause) {2506if (!_cms_start_registered) {2507register_gc_start(cause);2508}2509}25102511void CMSCollector::register_gc_start(GCCause::Cause cause) {2512_cms_start_registered = true;2513_gc_timer_cm->register_gc_start();2514_gc_tracer_cm->report_gc_start(cause, _gc_timer_cm->gc_start());2515}25162517void CMSCollector::register_gc_end() {2518if (_cms_start_registered) {2519report_heap_summary(GCWhen::AfterGC);25202521_gc_timer_cm->register_gc_end();2522_gc_tracer_cm->report_gc_end(_gc_timer_cm->gc_end(), _gc_timer_cm->time_partitions());2523_cms_start_registered = false;2524}2525}25262527void CMSCollector::save_heap_summary() {2528GenCollectedHeap* gch = GenCollectedHeap::heap();2529_last_heap_summary = gch->create_heap_summary();2530_last_metaspace_summary = gch->create_metaspace_summary();2531}25322533void CMSCollector::report_heap_summary(GCWhen::Type when) {2534_gc_tracer_cm->report_gc_heap_summary(when, _last_heap_summary);2535_gc_tracer_cm->report_metaspace_summary(when, _last_metaspace_summary);2536}25372538void CMSCollector::collect_in_foreground(bool clear_all_soft_refs, GCCause::Cause cause) {2539assert(_foregroundGCIsActive && !_foregroundGCShouldWait,2540"Foreground collector should be waiting, not executing");2541assert(Thread::current()->is_VM_thread(), "A foreground collection"2542"may only be done by the VM Thread with the world stopped");2543assert(ConcurrentMarkSweepThread::vm_thread_has_cms_token(),2544"VM thread should have CMS token");25452546// The gc id is created in register_foreground_gc_start if this collection is synchronous2547const GCId gc_id = _collectorState == InitialMarking ? GCId::peek() : _gc_tracer_cm->gc_id();2548NOT_PRODUCT(GCTraceTime t("CMS:MS (foreground) ", PrintGCDetails && Verbose,2549true, NULL, gc_id);)2550if (UseAdaptiveSizePolicy) {2551size_policy()->ms_collection_begin();2552}2553COMPILER2_PRESENT(DerivedPointerTableDeactivate dpt_deact);25542555HandleMark hm; // Discard invalid handles created during verification25562557if (VerifyBeforeGC &&2558GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {2559Universe::verify();2560}25612562// Snapshot the soft reference policy to be used in this collection cycle.2563ref_processor()->setup_policy(clear_all_soft_refs);25642565// Decide if class unloading should be done2566update_should_unload_classes();25672568bool init_mark_was_synchronous = false; // until proven otherwise2569while (_collectorState != Idling) {2570if (TraceCMSState) {2571gclog_or_tty->print_cr("Thread " INTPTR_FORMAT " in CMS state %d",2572Thread::current(), _collectorState);2573}2574switch (_collectorState) {2575case InitialMarking:2576register_foreground_gc_start(cause);2577init_mark_was_synchronous = true; // fact to be exploited in re-mark2578checkpointRootsInitial(false);2579assert(_collectorState == Marking, "Collector state should have changed"2580" within checkpointRootsInitial()");2581break;2582case Marking:2583// initial marking in checkpointRootsInitialWork has been completed2584if (VerifyDuringGC &&2585GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {2586Universe::verify("Verify before initial mark: ");2587}2588{2589bool res = markFromRoots(false);2590assert(res && _collectorState == FinalMarking, "Collector state should "2591"have changed");2592break;2593}2594case FinalMarking:2595if (VerifyDuringGC &&2596GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {2597Universe::verify("Verify before re-mark: ");2598}2599checkpointRootsFinal(false, clear_all_soft_refs,2600init_mark_was_synchronous);2601assert(_collectorState == Sweeping, "Collector state should not "2602"have changed within checkpointRootsFinal()");2603break;2604case Sweeping:2605// final marking in checkpointRootsFinal has been completed2606if (VerifyDuringGC &&2607GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {2608Universe::verify("Verify before sweep: ");2609}2610sweep(false);2611assert(_collectorState == Resizing, "Incorrect state");2612break;2613case Resizing: {2614// Sweeping has been completed; the actual resize in this case2615// is done separately; nothing to be done in this state.2616_collectorState = Resetting;2617break;2618}2619case Resetting:2620// The heap has been resized.2621if (VerifyDuringGC &&2622GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {2623Universe::verify("Verify before reset: ");2624}2625save_heap_summary();2626reset(false);2627assert(_collectorState == Idling, "Collector state should "2628"have changed");2629break;2630case Precleaning:2631case AbortablePreclean:2632// Elide the preclean phase2633_collectorState = FinalMarking;2634break;2635default:2636ShouldNotReachHere();2637}2638if (TraceCMSState) {2639gclog_or_tty->print_cr(" Thread " INTPTR_FORMAT " done - next CMS state %d",2640Thread::current(), _collectorState);2641}2642}26432644if (UseAdaptiveSizePolicy) {2645GenCollectedHeap* gch = GenCollectedHeap::heap();2646size_policy()->ms_collection_end(gch->gc_cause());2647}26482649if (VerifyAfterGC &&2650GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {2651Universe::verify();2652}2653if (TraceCMSState) {2654gclog_or_tty->print_cr("CMS Thread " INTPTR_FORMAT2655" exiting collection CMS state %d",2656Thread::current(), _collectorState);2657}2658}26592660bool CMSCollector::waitForForegroundGC() {2661bool res = false;2662assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),2663"CMS thread should have CMS token");2664// Block the foreground collector until the2665// background collectors decides whether to2666// yield.2667MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);2668_foregroundGCShouldWait = true;2669if (_foregroundGCIsActive) {2670// The background collector yields to the2671// foreground collector and returns a value2672// indicating that it has yielded. The foreground2673// collector can proceed.2674res = true;2675_foregroundGCShouldWait = false;2676ConcurrentMarkSweepThread::clear_CMS_flag(2677ConcurrentMarkSweepThread::CMS_cms_has_token);2678ConcurrentMarkSweepThread::set_CMS_flag(2679ConcurrentMarkSweepThread::CMS_cms_wants_token);2680// Get a possibly blocked foreground thread going2681CGC_lock->notify();2682if (TraceCMSState) {2683gclog_or_tty->print_cr("CMS Thread " INTPTR_FORMAT " waiting at CMS state %d",2684Thread::current(), _collectorState);2685}2686while (_foregroundGCIsActive) {2687CGC_lock->wait(Mutex::_no_safepoint_check_flag);2688}2689ConcurrentMarkSweepThread::set_CMS_flag(2690ConcurrentMarkSweepThread::CMS_cms_has_token);2691ConcurrentMarkSweepThread::clear_CMS_flag(2692ConcurrentMarkSweepThread::CMS_cms_wants_token);2693}2694if (TraceCMSState) {2695gclog_or_tty->print_cr("CMS Thread " INTPTR_FORMAT " continuing at CMS state %d",2696Thread::current(), _collectorState);2697}2698return res;2699}27002701// Because of the need to lock the free lists and other structures in2702// the collector, common to all the generations that the collector is2703// collecting, we need the gc_prologues of individual CMS generations2704// delegate to their collector. It may have been simpler had the2705// current infrastructure allowed one to call a prologue on a2706// collector. In the absence of that we have the generation's2707// prologue delegate to the collector, which delegates back2708// some "local" work to a worker method in the individual generations2709// that it's responsible for collecting, while itself doing any2710// work common to all generations it's responsible for. A similar2711// comment applies to the gc_epilogue()'s.2712// The role of the varaible _between_prologue_and_epilogue is to2713// enforce the invocation protocol.2714void CMSCollector::gc_prologue(bool full) {2715// Call gc_prologue_work() for the CMSGen2716// we are responsible for.27172718// The following locking discipline assumes that we are only called2719// when the world is stopped.2720assert(SafepointSynchronize::is_at_safepoint(), "world is stopped assumption");27212722// The CMSCollector prologue must call the gc_prologues for the2723// "generations" that it's responsible2724// for.27252726assert( Thread::current()->is_VM_thread()2727|| ( CMSScavengeBeforeRemark2728&& Thread::current()->is_ConcurrentGC_thread()),2729"Incorrect thread type for prologue execution");27302731if (_between_prologue_and_epilogue) {2732// We have already been invoked; this is a gc_prologue delegation2733// from yet another CMS generation that we are responsible for, just2734// ignore it since all relevant work has already been done.2735return;2736}27372738// set a bit saying prologue has been called; cleared in epilogue2739_between_prologue_and_epilogue = true;2740// Claim locks for common data structures, then call gc_prologue_work()2741// for each CMSGen.27422743getFreelistLocks(); // gets free list locks on constituent spaces2744bitMapLock()->lock_without_safepoint_check();27452746// Should call gc_prologue_work() for all cms gens we are responsible for2747bool duringMarking = _collectorState >= Marking2748&& _collectorState < Sweeping;27492750// The young collections clear the modified oops state, which tells if2751// there are any modified oops in the class. The remark phase also needs2752// that information. Tell the young collection to save the union of all2753// modified klasses.2754if (duringMarking) {2755_ct->klass_rem_set()->set_accumulate_modified_oops(true);2756}27572758bool registerClosure = duringMarking;27592760ModUnionClosure* muc = CollectedHeap::use_parallel_gc_threads() ?2761&_modUnionClosurePar2762: &_modUnionClosure;2763_cmsGen->gc_prologue_work(full, registerClosure, muc);27642765if (!full) {2766stats().record_gc0_begin();2767}2768}27692770void ConcurrentMarkSweepGeneration::gc_prologue(bool full) {27712772_capacity_at_prologue = capacity();2773_used_at_prologue = used();2774_cmsSpace->recalculate_used_stable();27752776// Delegate to CMScollector which knows how to coordinate between2777// this and any other CMS generations that it is responsible for2778// collecting.2779collector()->gc_prologue(full);2780}27812782// This is a "private" interface for use by this generation's CMSCollector.2783// Not to be called directly by any other entity (for instance,2784// GenCollectedHeap, which calls the "public" gc_prologue method above).2785void ConcurrentMarkSweepGeneration::gc_prologue_work(bool full,2786bool registerClosure, ModUnionClosure* modUnionClosure) {2787assert(!incremental_collection_failed(), "Shouldn't be set yet");2788assert(cmsSpace()->preconsumptionDirtyCardClosure() == NULL,2789"Should be NULL");2790if (registerClosure) {2791cmsSpace()->setPreconsumptionDirtyCardClosure(modUnionClosure);2792}2793cmsSpace()->gc_prologue();2794// Clear stat counters2795NOT_PRODUCT(2796assert(_numObjectsPromoted == 0, "check");2797assert(_numWordsPromoted == 0, "check");2798if (Verbose && PrintGC) {2799gclog_or_tty->print("Allocated " SIZE_FORMAT " objects, "2800SIZE_FORMAT " bytes concurrently",2801_numObjectsAllocated, _numWordsAllocated*sizeof(HeapWord));2802}2803_numObjectsAllocated = 0;2804_numWordsAllocated = 0;2805)2806}28072808void CMSCollector::gc_epilogue(bool full) {2809// The following locking discipline assumes that we are only called2810// when the world is stopped.2811assert(SafepointSynchronize::is_at_safepoint(),2812"world is stopped assumption");28132814// Currently the CMS epilogue (see CompactibleFreeListSpace) merely checks2815// if linear allocation blocks need to be appropriately marked to allow the2816// the blocks to be parsable. We also check here whether we need to nudge the2817// CMS collector thread to start a new cycle (if it's not already active).2818assert( Thread::current()->is_VM_thread()2819|| ( CMSScavengeBeforeRemark2820&& Thread::current()->is_ConcurrentGC_thread()),2821"Incorrect thread type for epilogue execution");28222823if (!_between_prologue_and_epilogue) {2824// We have already been invoked; this is a gc_epilogue delegation2825// from yet another CMS generation that we are responsible for, just2826// ignore it since all relevant work has already been done.2827return;2828}2829assert(haveFreelistLocks(), "must have freelist locks");2830assert_lock_strong(bitMapLock());28312832_ct->klass_rem_set()->set_accumulate_modified_oops(false);28332834_cmsGen->gc_epilogue_work(full);28352836if (_collectorState == AbortablePreclean || _collectorState == Precleaning) {2837// in case sampling was not already enabled, enable it2838_start_sampling = true;2839}2840// reset _eden_chunk_array so sampling starts afresh2841_eden_chunk_index = 0;28422843size_t cms_used = _cmsGen->cmsSpace()->used();2844_cmsGen->cmsSpace()->recalculate_used_stable();28452846// update performance counters - this uses a special version of2847// update_counters() that allows the utilization to be passed as a2848// parameter, avoiding multiple calls to used().2849//2850_cmsGen->update_counters(cms_used);28512852if (CMSIncrementalMode) {2853icms_update_allocation_limits();2854}28552856bitMapLock()->unlock();2857releaseFreelistLocks();28582859if (!CleanChunkPoolAsync) {2860Chunk::clean_chunk_pool();2861}28622863set_did_compact(false);2864_between_prologue_and_epilogue = false; // ready for next cycle2865}28662867void ConcurrentMarkSweepGeneration::gc_epilogue(bool full) {2868collector()->gc_epilogue(full);28692870// Also reset promotion tracking in par gc thread states.2871if (CollectedHeap::use_parallel_gc_threads()) {2872for (uint i = 0; i < ParallelGCThreads; i++) {2873_par_gc_thread_states[i]->promo.stopTrackingPromotions(i);2874}2875}2876}28772878void ConcurrentMarkSweepGeneration::gc_epilogue_work(bool full) {2879assert(!incremental_collection_failed(), "Should have been cleared");2880cmsSpace()->setPreconsumptionDirtyCardClosure(NULL);2881cmsSpace()->gc_epilogue();2882// Print stat counters2883NOT_PRODUCT(2884assert(_numObjectsAllocated == 0, "check");2885assert(_numWordsAllocated == 0, "check");2886if (Verbose && PrintGC) {2887gclog_or_tty->print("Promoted " SIZE_FORMAT " objects, "2888SIZE_FORMAT " bytes",2889_numObjectsPromoted, _numWordsPromoted*sizeof(HeapWord));2890}2891_numObjectsPromoted = 0;2892_numWordsPromoted = 0;2893)28942895if (PrintGC && Verbose) {2896// Call down the chain in contiguous_available needs the freelistLock2897// so print this out before releasing the freeListLock.2898gclog_or_tty->print(" Contiguous available " SIZE_FORMAT " bytes ",2899contiguous_available());2900}2901}29022903#ifndef PRODUCT2904bool CMSCollector::have_cms_token() {2905Thread* thr = Thread::current();2906if (thr->is_VM_thread()) {2907return ConcurrentMarkSweepThread::vm_thread_has_cms_token();2908} else if (thr->is_ConcurrentGC_thread()) {2909return ConcurrentMarkSweepThread::cms_thread_has_cms_token();2910} else if (thr->is_GC_task_thread()) {2911return ConcurrentMarkSweepThread::vm_thread_has_cms_token() &&2912ParGCRareEvent_lock->owned_by_self();2913}2914return false;2915}2916#endif29172918// Check reachability of the given heap address in CMS generation,2919// treating all other generations as roots.2920bool CMSCollector::is_cms_reachable(HeapWord* addr) {2921// We could "guarantee" below, rather than assert, but i'll2922// leave these as "asserts" so that an adventurous debugger2923// could try this in the product build provided some subset of2924// the conditions were met, provided they were intersted in the2925// results and knew that the computation below wouldn't interfere2926// with other concurrent computations mutating the structures2927// being read or written.2928assert(SafepointSynchronize::is_at_safepoint(),2929"Else mutations in object graph will make answer suspect");2930assert(have_cms_token(), "Should hold cms token");2931assert(haveFreelistLocks(), "must hold free list locks");2932assert_lock_strong(bitMapLock());29332934// Clear the marking bit map array before starting, but, just2935// for kicks, first report if the given address is already marked2936gclog_or_tty->print_cr("Start: Address 0x%x is%s marked", addr,2937_markBitMap.isMarked(addr) ? "" : " not");29382939if (verify_after_remark()) {2940MutexLockerEx x(verification_mark_bm()->lock(), Mutex::_no_safepoint_check_flag);2941bool result = verification_mark_bm()->isMarked(addr);2942gclog_or_tty->print_cr("TransitiveMark: Address 0x%x %s marked", addr,2943result ? "IS" : "is NOT");2944return result;2945} else {2946gclog_or_tty->print_cr("Could not compute result");2947return false;2948}2949}295029512952void2953CMSCollector::print_on_error(outputStream* st) {2954CMSCollector* collector = ConcurrentMarkSweepGeneration::_collector;2955if (collector != NULL) {2956CMSBitMap* bitmap = &collector->_markBitMap;2957st->print_cr("Marking Bits: (CMSBitMap*) " PTR_FORMAT, bitmap);2958bitmap->print_on_error(st, " Bits: ");29592960st->cr();29612962CMSBitMap* mut_bitmap = &collector->_modUnionTable;2963st->print_cr("Mod Union Table: (CMSBitMap*) " PTR_FORMAT, mut_bitmap);2964mut_bitmap->print_on_error(st, " Bits: ");2965}2966}29672968////////////////////////////////////////////////////////2969// CMS Verification Support2970////////////////////////////////////////////////////////2971// Following the remark phase, the following invariant2972// should hold -- each object in the CMS heap which is2973// marked in markBitMap() should be marked in the verification_mark_bm().29742975class VerifyMarkedClosure: public BitMapClosure {2976CMSBitMap* _marks;2977bool _failed;29782979public:2980VerifyMarkedClosure(CMSBitMap* bm): _marks(bm), _failed(false) {}29812982bool do_bit(size_t offset) {2983HeapWord* addr = _marks->offsetToHeapWord(offset);2984if (!_marks->isMarked(addr)) {2985oop(addr)->print_on(gclog_or_tty);2986gclog_or_tty->print_cr(" (" INTPTR_FORMAT " should have been marked)", addr);2987_failed = true;2988}2989return true;2990}29912992bool failed() { return _failed; }2993};29942995bool CMSCollector::verify_after_remark(bool silent) {2996if (!silent) gclog_or_tty->print(" [Verifying CMS Marking... ");2997MutexLockerEx ml(verification_mark_bm()->lock(), Mutex::_no_safepoint_check_flag);2998static bool init = false;29993000assert(SafepointSynchronize::is_at_safepoint(),3001"Else mutations in object graph will make answer suspect");3002assert(have_cms_token(),3003"Else there may be mutual interference in use of "3004" verification data structures");3005assert(_collectorState > Marking && _collectorState <= Sweeping,3006"Else marking info checked here may be obsolete");3007assert(haveFreelistLocks(), "must hold free list locks");3008assert_lock_strong(bitMapLock());300930103011// Allocate marking bit map if not already allocated3012if (!init) { // first time3013if (!verification_mark_bm()->allocate(_span)) {3014return false;3015}3016init = true;3017}30183019assert(verification_mark_stack()->isEmpty(), "Should be empty");30203021// Turn off refs discovery -- so we will be tracing through refs.3022// This is as intended, because by this time3023// GC must already have cleared any refs that need to be cleared,3024// and traced those that need to be marked; moreover,3025// the marking done here is not going to intefere in any3026// way with the marking information used by GC.3027NoRefDiscovery no_discovery(ref_processor());30283029COMPILER2_PRESENT(DerivedPointerTableDeactivate dpt_deact;)30303031// Clear any marks from a previous round3032verification_mark_bm()->clear_all();3033assert(verification_mark_stack()->isEmpty(), "markStack should be empty");3034verify_work_stacks_empty();30353036GenCollectedHeap* gch = GenCollectedHeap::heap();3037gch->ensure_parsability(false); // fill TLABs, but no need to retire them3038// Update the saved marks which may affect the root scans.3039gch->save_marks();30403041if (CMSRemarkVerifyVariant == 1) {3042// In this first variant of verification, we complete3043// all marking, then check if the new marks-verctor is3044// a subset of the CMS marks-vector.3045verify_after_remark_work_1();3046} else if (CMSRemarkVerifyVariant == 2) {3047// In this second variant of verification, we flag an error3048// (i.e. an object reachable in the new marks-vector not reachable3049// in the CMS marks-vector) immediately, also indicating the3050// identify of an object (A) that references the unmarked object (B) --3051// presumably, a mutation to A failed to be picked up by preclean/remark?3052verify_after_remark_work_2();3053} else {3054warning("Unrecognized value %d for CMSRemarkVerifyVariant",3055CMSRemarkVerifyVariant);3056}3057if (!silent) gclog_or_tty->print(" done] ");3058return true;3059}30603061void CMSCollector::verify_after_remark_work_1() {3062ResourceMark rm;3063HandleMark hm;3064GenCollectedHeap* gch = GenCollectedHeap::heap();30653066// Get a clear set of claim bits for the roots processing to work with.3067ClassLoaderDataGraph::clear_claimed_marks();30683069// Mark from roots one level into CMS3070MarkRefsIntoClosure notOlder(_span, verification_mark_bm());3071gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.30723073gch->gen_process_roots(_cmsGen->level(),3074true, // younger gens are roots3075true, // activate StrongRootsScope3076GenCollectedHeap::ScanningOption(roots_scanning_options()),3077should_unload_classes(),3078¬Older,3079NULL,3080NULL); // SSS: Provide correct closure30813082// Now mark from the roots3083MarkFromRootsClosure markFromRootsClosure(this, _span,3084verification_mark_bm(), verification_mark_stack(),3085false /* don't yield */, true /* verifying */);3086assert(_restart_addr == NULL, "Expected pre-condition");3087verification_mark_bm()->iterate(&markFromRootsClosure);3088while (_restart_addr != NULL) {3089// Deal with stack overflow: by restarting at the indicated3090// address.3091HeapWord* ra = _restart_addr;3092markFromRootsClosure.reset(ra);3093_restart_addr = NULL;3094verification_mark_bm()->iterate(&markFromRootsClosure, ra, _span.end());3095}3096assert(verification_mark_stack()->isEmpty(), "Should have been drained");3097verify_work_stacks_empty();30983099// Marking completed -- now verify that each bit marked in3100// verification_mark_bm() is also marked in markBitMap(); flag all3101// errors by printing corresponding objects.3102VerifyMarkedClosure vcl(markBitMap());3103verification_mark_bm()->iterate(&vcl);3104if (vcl.failed()) {3105gclog_or_tty->print("Verification failed");3106Universe::heap()->print_on(gclog_or_tty);3107fatal("CMS: failed marking verification after remark");3108}3109}31103111class VerifyKlassOopsKlassClosure : public KlassClosure {3112class VerifyKlassOopsClosure : public OopClosure {3113CMSBitMap* _bitmap;3114public:3115VerifyKlassOopsClosure(CMSBitMap* bitmap) : _bitmap(bitmap) { }3116void do_oop(oop* p) { guarantee(*p == NULL || _bitmap->isMarked((HeapWord*) *p), "Should be marked"); }3117void do_oop(narrowOop* p) { ShouldNotReachHere(); }3118} _oop_closure;3119public:3120VerifyKlassOopsKlassClosure(CMSBitMap* bitmap) : _oop_closure(bitmap) {}3121void do_klass(Klass* k) {3122k->oops_do(&_oop_closure);3123}3124};31253126void CMSCollector::verify_after_remark_work_2() {3127ResourceMark rm;3128HandleMark hm;3129GenCollectedHeap* gch = GenCollectedHeap::heap();31303131// Get a clear set of claim bits for the roots processing to work with.3132ClassLoaderDataGraph::clear_claimed_marks();31333134// Mark from roots one level into CMS3135MarkRefsIntoVerifyClosure notOlder(_span, verification_mark_bm(),3136markBitMap());3137CLDToOopClosure cld_closure(¬Older, true);31383139gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.31403141gch->gen_process_roots(_cmsGen->level(),3142true, // younger gens are roots3143true, // activate StrongRootsScope3144GenCollectedHeap::ScanningOption(roots_scanning_options()),3145should_unload_classes(),3146¬Older,3147NULL,3148&cld_closure);31493150// Now mark from the roots3151MarkFromRootsVerifyClosure markFromRootsClosure(this, _span,3152verification_mark_bm(), markBitMap(), verification_mark_stack());3153assert(_restart_addr == NULL, "Expected pre-condition");3154verification_mark_bm()->iterate(&markFromRootsClosure);3155while (_restart_addr != NULL) {3156// Deal with stack overflow: by restarting at the indicated3157// address.3158HeapWord* ra = _restart_addr;3159markFromRootsClosure.reset(ra);3160_restart_addr = NULL;3161verification_mark_bm()->iterate(&markFromRootsClosure, ra, _span.end());3162}3163assert(verification_mark_stack()->isEmpty(), "Should have been drained");3164verify_work_stacks_empty();31653166VerifyKlassOopsKlassClosure verify_klass_oops(verification_mark_bm());3167ClassLoaderDataGraph::classes_do(&verify_klass_oops);31683169// Marking completed -- now verify that each bit marked in3170// verification_mark_bm() is also marked in markBitMap(); flag all3171// errors by printing corresponding objects.3172VerifyMarkedClosure vcl(markBitMap());3173verification_mark_bm()->iterate(&vcl);3174assert(!vcl.failed(), "Else verification above should not have succeeded");3175}31763177void ConcurrentMarkSweepGeneration::save_marks() {3178// delegate to CMS space3179cmsSpace()->save_marks();3180for (uint i = 0; i < ParallelGCThreads; i++) {3181_par_gc_thread_states[i]->promo.startTrackingPromotions();3182}3183}31843185bool ConcurrentMarkSweepGeneration::no_allocs_since_save_marks() {3186return cmsSpace()->no_allocs_since_save_marks();3187}31883189#define CMS_SINCE_SAVE_MARKS_DEFN(OopClosureType, nv_suffix) \3190\3191void ConcurrentMarkSweepGeneration:: \3192oop_since_save_marks_iterate##nv_suffix(OopClosureType* cl) { \3193cl->set_generation(this); \3194cmsSpace()->oop_since_save_marks_iterate##nv_suffix(cl); \3195cl->reset_generation(); \3196save_marks(); \3197}31983199ALL_SINCE_SAVE_MARKS_CLOSURES(CMS_SINCE_SAVE_MARKS_DEFN)32003201void3202ConcurrentMarkSweepGeneration::younger_refs_iterate(OopsInGenClosure* cl) {3203cl->set_generation(this);3204younger_refs_in_space_iterate(_cmsSpace, cl);3205cl->reset_generation();3206}32073208void3209ConcurrentMarkSweepGeneration::oop_iterate(ExtendedOopClosure* cl) {3210if (freelistLock()->owned_by_self()) {3211Generation::oop_iterate(cl);3212} else {3213MutexLockerEx x(freelistLock(), Mutex::_no_safepoint_check_flag);3214Generation::oop_iterate(cl);3215}3216}32173218void3219ConcurrentMarkSweepGeneration::object_iterate(ObjectClosure* cl) {3220if (freelistLock()->owned_by_self()) {3221Generation::object_iterate(cl);3222} else {3223MutexLockerEx x(freelistLock(), Mutex::_no_safepoint_check_flag);3224Generation::object_iterate(cl);3225}3226}32273228void3229ConcurrentMarkSweepGeneration::safe_object_iterate(ObjectClosure* cl) {3230if (freelistLock()->owned_by_self()) {3231Generation::safe_object_iterate(cl);3232} else {3233MutexLockerEx x(freelistLock(), Mutex::_no_safepoint_check_flag);3234Generation::safe_object_iterate(cl);3235}3236}32373238void3239ConcurrentMarkSweepGeneration::post_compact() {3240}32413242void3243ConcurrentMarkSweepGeneration::prepare_for_verify() {3244// Fix the linear allocation blocks to look like free blocks.32453246// Locks are normally acquired/released in gc_prologue/gc_epilogue, but those3247// are not called when the heap is verified during universe initialization and3248// at vm shutdown.3249if (freelistLock()->owned_by_self()) {3250cmsSpace()->prepare_for_verify();3251} else {3252MutexLockerEx fll(freelistLock(), Mutex::_no_safepoint_check_flag);3253cmsSpace()->prepare_for_verify();3254}3255}32563257void3258ConcurrentMarkSweepGeneration::verify() {3259// Locks are normally acquired/released in gc_prologue/gc_epilogue, but those3260// are not called when the heap is verified during universe initialization and3261// at vm shutdown.3262if (freelistLock()->owned_by_self()) {3263cmsSpace()->verify();3264} else {3265MutexLockerEx fll(freelistLock(), Mutex::_no_safepoint_check_flag);3266cmsSpace()->verify();3267}3268}32693270void CMSCollector::verify() {3271_cmsGen->verify();3272}32733274#ifndef PRODUCT3275bool CMSCollector::overflow_list_is_empty() const {3276assert(_num_par_pushes >= 0, "Inconsistency");3277if (_overflow_list == NULL) {3278assert(_num_par_pushes == 0, "Inconsistency");3279}3280return _overflow_list == NULL;3281}32823283// The methods verify_work_stacks_empty() and verify_overflow_empty()3284// merely consolidate assertion checks that appear to occur together frequently.3285void CMSCollector::verify_work_stacks_empty() const {3286assert(_markStack.isEmpty(), "Marking stack should be empty");3287assert(overflow_list_is_empty(), "Overflow list should be empty");3288}32893290void CMSCollector::verify_overflow_empty() const {3291assert(overflow_list_is_empty(), "Overflow list should be empty");3292assert(no_preserved_marks(), "No preserved marks");3293}3294#endif // PRODUCT32953296// Decide if we want to enable class unloading as part of the3297// ensuing concurrent GC cycle. We will collect and3298// unload classes if it's the case that:3299// (1) an explicit gc request has been made and the flag3300// ExplicitGCInvokesConcurrentAndUnloadsClasses is set, OR3301// (2) (a) class unloading is enabled at the command line, and3302// (b) old gen is getting really full3303// NOTE: Provided there is no change in the state of the heap between3304// calls to this method, it should have idempotent results. Moreover,3305// its results should be monotonically increasing (i.e. going from 0 to 1,3306// but not 1 to 0) between successive calls between which the heap was3307// not collected. For the implementation below, it must thus rely on3308// the property that concurrent_cycles_since_last_unload()3309// will not decrease unless a collection cycle happened and that3310// _cmsGen->is_too_full() are3311// themselves also monotonic in that sense. See check_monotonicity()3312// below.3313void CMSCollector::update_should_unload_classes() {3314_should_unload_classes = false;3315// Condition 1 above3316if (_full_gc_requested && ExplicitGCInvokesConcurrentAndUnloadsClasses) {3317_should_unload_classes = true;3318} else if (CMSClassUnloadingEnabled) { // Condition 2.a above3319// Disjuncts 2.b.(i,ii,iii) above3320_should_unload_classes = (concurrent_cycles_since_last_unload() >=3321CMSClassUnloadingMaxInterval)3322|| _cmsGen->is_too_full();3323}3324}33253326bool ConcurrentMarkSweepGeneration::is_too_full() const {3327bool res = should_concurrent_collect();3328res = res && (occupancy() > (double)CMSIsTooFullPercentage/100.0);3329return res;3330}33313332void CMSCollector::setup_cms_unloading_and_verification_state() {3333const bool should_verify = VerifyBeforeGC || VerifyAfterGC || VerifyDuringGC3334|| VerifyBeforeExit;3335const int rso = GenCollectedHeap::SO_AllCodeCache;33363337// We set the proper root for this CMS cycle here.3338if (should_unload_classes()) { // Should unload classes this cycle3339remove_root_scanning_option(rso); // Shrink the root set appropriately3340set_verifying(should_verify); // Set verification state for this cycle3341return; // Nothing else needs to be done at this time3342}33433344// Not unloading classes this cycle3345assert(!should_unload_classes(), "Inconsitency!");33463347// If we are not unloading classes then add SO_AllCodeCache to root3348// scanning options.3349add_root_scanning_option(rso);33503351if ((!verifying() || unloaded_classes_last_cycle()) && should_verify) {3352set_verifying(true);3353} else if (verifying() && !should_verify) {3354// We were verifying, but some verification flags got disabled.3355set_verifying(false);3356// Exclude symbols, strings and code cache elements from root scanning to3357// reduce IM and RM pauses.3358remove_root_scanning_option(rso);3359}3360}336133623363#ifndef PRODUCT3364HeapWord* CMSCollector::block_start(const void* p) const {3365const HeapWord* addr = (HeapWord*)p;3366if (_span.contains(p)) {3367if (_cmsGen->cmsSpace()->is_in_reserved(addr)) {3368return _cmsGen->cmsSpace()->block_start(p);3369}3370}3371return NULL;3372}3373#endif33743375HeapWord*3376ConcurrentMarkSweepGeneration::expand_and_allocate(size_t word_size,3377bool tlab,3378bool parallel) {3379CMSSynchronousYieldRequest yr;3380assert(!tlab, "Can't deal with TLAB allocation");3381MutexLockerEx x(freelistLock(), Mutex::_no_safepoint_check_flag);3382expand(word_size*HeapWordSize, MinHeapDeltaBytes,3383CMSExpansionCause::_satisfy_allocation);3384if (GCExpandToAllocateDelayMillis > 0) {3385os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);3386}3387return have_lock_and_allocate(word_size, tlab);3388}33893390// YSR: All of this generation expansion/shrinking stuff is an exact copy of3391// OneContigSpaceCardGeneration, which makes me wonder if we should move this3392// to CardGeneration and share it...3393bool ConcurrentMarkSweepGeneration::expand(size_t bytes, size_t expand_bytes) {3394return CardGeneration::expand(bytes, expand_bytes);3395}33963397void ConcurrentMarkSweepGeneration::expand(size_t bytes, size_t expand_bytes,3398CMSExpansionCause::Cause cause)3399{34003401bool success = expand(bytes, expand_bytes);34023403// remember why we expanded; this information is used3404// by shouldConcurrentCollect() when making decisions on whether to start3405// a new CMS cycle.3406if (success) {3407set_expansion_cause(cause);3408if (PrintGCDetails && Verbose) {3409gclog_or_tty->print_cr("Expanded CMS gen for %s",3410CMSExpansionCause::to_string(cause));3411}3412}3413}34143415HeapWord* ConcurrentMarkSweepGeneration::expand_and_par_lab_allocate(CMSParGCThreadState* ps, size_t word_sz) {3416HeapWord* res = NULL;3417MutexLocker x(ParGCRareEvent_lock);3418while (true) {3419// Expansion by some other thread might make alloc OK now:3420res = ps->lab.alloc(word_sz);3421if (res != NULL) return res;3422// If there's not enough expansion space available, give up.3423if (_virtual_space.uncommitted_size() < (word_sz * HeapWordSize)) {3424return NULL;3425}3426// Otherwise, we try expansion.3427expand(word_sz*HeapWordSize, MinHeapDeltaBytes,3428CMSExpansionCause::_allocate_par_lab);3429// Now go around the loop and try alloc again;3430// A competing par_promote might beat us to the expansion space,3431// so we may go around the loop again if promotion fails agaion.3432if (GCExpandToAllocateDelayMillis > 0) {3433os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);3434}3435}3436}343734383439bool ConcurrentMarkSweepGeneration::expand_and_ensure_spooling_space(3440PromotionInfo* promo) {3441MutexLocker x(ParGCRareEvent_lock);3442size_t refill_size_bytes = promo->refillSize() * HeapWordSize;3443while (true) {3444// Expansion by some other thread might make alloc OK now:3445if (promo->ensure_spooling_space()) {3446assert(promo->has_spooling_space(),3447"Post-condition of successful ensure_spooling_space()");3448return true;3449}3450// If there's not enough expansion space available, give up.3451if (_virtual_space.uncommitted_size() < refill_size_bytes) {3452return false;3453}3454// Otherwise, we try expansion.3455expand(refill_size_bytes, MinHeapDeltaBytes,3456CMSExpansionCause::_allocate_par_spooling_space);3457// Now go around the loop and try alloc again;3458// A competing allocation might beat us to the expansion space,3459// so we may go around the loop again if allocation fails again.3460if (GCExpandToAllocateDelayMillis > 0) {3461os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);3462}3463}3464}346534663467void ConcurrentMarkSweepGeneration::shrink_by(size_t bytes) {3468assert_locked_or_safepoint(ExpandHeap_lock);3469// Shrink committed space3470_virtual_space.shrink_by(bytes);3471// Shrink space; this also shrinks the space's BOT3472_cmsSpace->set_end((HeapWord*) _virtual_space.high());3473size_t new_word_size = heap_word_size(_cmsSpace->capacity());3474// Shrink the shared block offset array3475_bts->resize(new_word_size);3476MemRegion mr(_cmsSpace->bottom(), new_word_size);3477// Shrink the card table3478Universe::heap()->barrier_set()->resize_covered_region(mr);34793480if (Verbose && PrintGC) {3481size_t new_mem_size = _virtual_space.committed_size();3482size_t old_mem_size = new_mem_size + bytes;3483gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K to " SIZE_FORMAT "K",3484name(), old_mem_size/K, new_mem_size/K);3485}3486}34873488void ConcurrentMarkSweepGeneration::shrink(size_t bytes) {3489assert_locked_or_safepoint(Heap_lock);3490size_t size = ReservedSpace::page_align_size_down(bytes);3491// Only shrink if a compaction was done so that all the free space3492// in the generation is in a contiguous block at the end.3493if (size > 0 && did_compact()) {3494shrink_by(size);3495}3496}34973498bool ConcurrentMarkSweepGeneration::grow_by(size_t bytes) {3499assert_locked_or_safepoint(Heap_lock);3500bool result = _virtual_space.expand_by(bytes);3501if (result) {3502size_t new_word_size =3503heap_word_size(_virtual_space.committed_size());3504MemRegion mr(_cmsSpace->bottom(), new_word_size);3505_bts->resize(new_word_size); // resize the block offset shared array3506Universe::heap()->barrier_set()->resize_covered_region(mr);3507// Hmmmm... why doesn't CFLS::set_end verify locking?3508// This is quite ugly; FIX ME XXX3509_cmsSpace->assert_locked(freelistLock());3510_cmsSpace->set_end((HeapWord*)_virtual_space.high());35113512// update the space and generation capacity counters3513if (UsePerfData) {3514_space_counters->update_capacity();3515_gen_counters->update_all();3516}35173518if (Verbose && PrintGC) {3519size_t new_mem_size = _virtual_space.committed_size();3520size_t old_mem_size = new_mem_size - bytes;3521gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by " SIZE_FORMAT "K to " SIZE_FORMAT "K",3522name(), old_mem_size/K, bytes/K, new_mem_size/K);3523}3524}3525return result;3526}35273528bool ConcurrentMarkSweepGeneration::grow_to_reserved() {3529assert_locked_or_safepoint(Heap_lock);3530bool success = true;3531const size_t remaining_bytes = _virtual_space.uncommitted_size();3532if (remaining_bytes > 0) {3533success = grow_by(remaining_bytes);3534DEBUG_ONLY(if (!success) warning("grow to reserved failed");)3535}3536return success;3537}35383539void ConcurrentMarkSweepGeneration::shrink_free_list_by(size_t bytes) {3540assert_locked_or_safepoint(Heap_lock);3541assert_lock_strong(freelistLock());3542if (PrintGCDetails && Verbose) {3543warning("Shrinking of CMS not yet implemented");3544}3545return;3546}354735483549// Simple ctor/dtor wrapper for accounting & timer chores around concurrent3550// phases.3551class CMSPhaseAccounting: public StackObj {3552public:3553CMSPhaseAccounting(CMSCollector *collector,3554const char *phase,3555const GCId gc_id,3556bool print_cr = true);3557~CMSPhaseAccounting();35583559private:3560CMSCollector *_collector;3561const char *_phase;3562elapsedTimer _wallclock;3563bool _print_cr;3564const GCId _gc_id;35653566public:3567// Not MT-safe; so do not pass around these StackObj's3568// where they may be accessed by other threads.3569jlong wallclock_millis() {3570assert(_wallclock.is_active(), "Wall clock should not stop");3571_wallclock.stop(); // to record time3572jlong ret = _wallclock.milliseconds();3573_wallclock.start(); // restart3574return ret;3575}3576};35773578CMSPhaseAccounting::CMSPhaseAccounting(CMSCollector *collector,3579const char *phase,3580const GCId gc_id,3581bool print_cr) :3582_collector(collector), _phase(phase), _print_cr(print_cr), _gc_id(gc_id) {35833584if (PrintCMSStatistics != 0) {3585_collector->resetYields();3586}3587if (PrintGCDetails) {3588gclog_or_tty->gclog_stamp(_gc_id);3589gclog_or_tty->print_cr("[%s-concurrent-%s-start]",3590_collector->cmsGen()->short_name(), _phase);3591}3592_collector->resetTimer();3593_wallclock.start();3594_collector->startTimer();3595}35963597CMSPhaseAccounting::~CMSPhaseAccounting() {3598assert(_wallclock.is_active(), "Wall clock should not have stopped");3599_collector->stopTimer();3600_wallclock.stop();3601if (PrintGCDetails) {3602gclog_or_tty->gclog_stamp(_gc_id);3603gclog_or_tty->print("[%s-concurrent-%s: %3.3f/%3.3f secs]",3604_collector->cmsGen()->short_name(),3605_phase, _collector->timerValue(), _wallclock.seconds());3606if (_print_cr) {3607gclog_or_tty->cr();3608}3609if (PrintCMSStatistics != 0) {3610gclog_or_tty->print_cr(" (CMS-concurrent-%s yielded %d times)", _phase,3611_collector->yields());3612}3613}3614}36153616// CMS work36173618// The common parts of CMSParInitialMarkTask and CMSParRemarkTask.3619class CMSParMarkTask : public AbstractGangTask {3620protected:3621CMSCollector* _collector;3622int _n_workers;3623CMSParMarkTask(const char* name, CMSCollector* collector, int n_workers) :3624AbstractGangTask(name),3625_collector(collector),3626_n_workers(n_workers) {}3627// Work method in support of parallel rescan ... of young gen spaces3628void do_young_space_rescan(uint worker_id, OopsInGenClosure* cl,3629ContiguousSpace* space,3630HeapWord** chunk_array, size_t chunk_top);3631void work_on_young_gen_roots(uint worker_id, OopsInGenClosure* cl);3632};36333634// Parallel initial mark task3635class CMSParInitialMarkTask: public CMSParMarkTask {3636public:3637CMSParInitialMarkTask(CMSCollector* collector, int n_workers) :3638CMSParMarkTask("Scan roots and young gen for initial mark in parallel",3639collector, n_workers) {}3640void work(uint worker_id);3641};36423643// Checkpoint the roots into this generation from outside3644// this generation. [Note this initial checkpoint need only3645// be approximate -- we'll do a catch up phase subsequently.]3646void CMSCollector::checkpointRootsInitial(bool asynch) {3647assert(_collectorState == InitialMarking, "Wrong collector state");3648check_correct_thread_executing();3649TraceCMSMemoryManagerStats tms(_collectorState,GenCollectedHeap::heap()->gc_cause());36503651save_heap_summary();3652report_heap_summary(GCWhen::BeforeGC);36533654ReferenceProcessor* rp = ref_processor();3655SpecializationStats::clear();3656assert(_restart_addr == NULL, "Control point invariant");3657if (asynch) {3658// acquire locks for subsequent manipulations3659MutexLockerEx x(bitMapLock(),3660Mutex::_no_safepoint_check_flag);3661checkpointRootsInitialWork(asynch);3662// enable ("weak") refs discovery3663rp->enable_discovery(true /*verify_disabled*/, true /*check_no_refs*/);3664_collectorState = Marking;3665} else {3666// (Weak) Refs discovery: this is controlled from genCollectedHeap::do_collection3667// which recognizes if we are a CMS generation, and doesn't try to turn on3668// discovery; verify that they aren't meddling.3669assert(!rp->discovery_is_atomic(),3670"incorrect setting of discovery predicate");3671assert(!rp->discovery_enabled(), "genCollectedHeap shouldn't control "3672"ref discovery for this generation kind");3673// already have locks3674checkpointRootsInitialWork(asynch);3675// now enable ("weak") refs discovery3676rp->enable_discovery(true /*verify_disabled*/, false /*verify_no_refs*/);3677_collectorState = Marking;3678}3679SpecializationStats::print();3680_cmsGen->cmsSpace()->recalculate_used_stable();3681}36823683void CMSCollector::checkpointRootsInitialWork(bool asynch) {3684assert(SafepointSynchronize::is_at_safepoint(), "world should be stopped");3685assert(_collectorState == InitialMarking, "just checking");36863687// If there has not been a GC[n-1] since last GC[n] cycle completed,3688// precede our marking with a collection of all3689// younger generations to keep floating garbage to a minimum.3690// XXX: we won't do this for now -- it's an optimization to be done later.36913692// already have locks3693assert_lock_strong(bitMapLock());3694assert(_markBitMap.isAllClear(), "was reset at end of previous cycle");36953696// Setup the verification and class unloading state for this3697// CMS collection cycle.3698setup_cms_unloading_and_verification_state();36993700NOT_PRODUCT(GCTraceTime t("\ncheckpointRootsInitialWork",3701PrintGCDetails && Verbose, true, _gc_timer_cm, _gc_tracer_cm->gc_id());)3702if (UseAdaptiveSizePolicy) {3703size_policy()->checkpoint_roots_initial_begin();3704}37053706// Reset all the PLAB chunk arrays if necessary.3707if (_survivor_plab_array != NULL && !CMSPLABRecordAlways) {3708reset_survivor_plab_arrays();3709}37103711ResourceMark rm;3712HandleMark hm;37133714MarkRefsIntoClosure notOlder(_span, &_markBitMap);3715GenCollectedHeap* gch = GenCollectedHeap::heap();37163717verify_work_stacks_empty();3718verify_overflow_empty();37193720gch->ensure_parsability(false); // fill TLABs, but no need to retire them3721// Update the saved marks which may affect the root scans.3722gch->save_marks();37233724// weak reference processing has not started yet.3725ref_processor()->set_enqueuing_is_done(false);37263727// Need to remember all newly created CLDs,3728// so that we can guarantee that the remark finds them.3729ClassLoaderDataGraph::remember_new_clds(true);37303731// Whenever a CLD is found, it will be claimed before proceeding to mark3732// the klasses. The claimed marks need to be cleared before marking starts.3733ClassLoaderDataGraph::clear_claimed_marks();37343735if (CMSPrintEdenSurvivorChunks) {3736print_eden_and_survivor_chunk_arrays();3737}37383739{3740COMPILER2_PRESENT(DerivedPointerTableDeactivate dpt_deact;)3741if (CMSParallelInitialMarkEnabled && CollectedHeap::use_parallel_gc_threads()) {3742// The parallel version.3743FlexibleWorkGang* workers = gch->workers();3744assert(workers != NULL, "Need parallel worker threads.");3745int n_workers = workers->active_workers();3746CMSParInitialMarkTask tsk(this, n_workers);3747gch->set_par_threads(n_workers);3748initialize_sequential_subtasks_for_young_gen_rescan(n_workers);3749if (n_workers > 1) {3750GenCollectedHeap::StrongRootsScope srs(gch);3751workers->run_task(&tsk);3752} else {3753GenCollectedHeap::StrongRootsScope srs(gch);3754tsk.work(0);3755}3756gch->set_par_threads(0);3757} else {3758// The serial version.3759CLDToOopClosure cld_closure(¬Older, true);3760gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.3761gch->gen_process_roots(_cmsGen->level(),3762true, // younger gens are roots3763true, // activate StrongRootsScope3764GenCollectedHeap::ScanningOption(roots_scanning_options()),3765should_unload_classes(),3766¬Older,3767NULL,3768&cld_closure);3769}3770}37713772// Clear mod-union table; it will be dirtied in the prologue of3773// CMS generation per each younger generation collection.37743775assert(_modUnionTable.isAllClear(),3776"Was cleared in most recent final checkpoint phase"3777" or no bits are set in the gc_prologue before the start of the next "3778"subsequent marking phase.");37793780assert(_ct->klass_rem_set()->mod_union_is_clear(), "Must be");37813782// Save the end of the used_region of the constituent generations3783// to be used to limit the extent of sweep in each generation.3784save_sweep_limits();3785if (UseAdaptiveSizePolicy) {3786size_policy()->checkpoint_roots_initial_end(gch->gc_cause());3787}3788verify_overflow_empty();3789}37903791bool CMSCollector::markFromRoots(bool asynch) {3792// we might be tempted to assert that:3793// assert(asynch == !SafepointSynchronize::is_at_safepoint(),3794// "inconsistent argument?");3795// However that wouldn't be right, because it's possible that3796// a safepoint is indeed in progress as a younger generation3797// stop-the-world GC happens even as we mark in this generation.3798assert(_collectorState == Marking, "inconsistent state?");3799check_correct_thread_executing();3800verify_overflow_empty();38013802bool res;3803if (asynch) {38043805// Start the timers for adaptive size policy for the concurrent phases3806// Do it here so that the foreground MS can use the concurrent3807// timer since a foreground MS might has the sweep done concurrently3808// or STW.3809if (UseAdaptiveSizePolicy) {3810size_policy()->concurrent_marking_begin();3811}38123813// Weak ref discovery note: We may be discovering weak3814// refs in this generation concurrent (but interleaved) with3815// weak ref discovery by a younger generation collector.38163817CMSTokenSyncWithLocks ts(true, bitMapLock());3818TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);3819CMSPhaseAccounting pa(this, "mark", _gc_tracer_cm->gc_id(), !PrintGCDetails);3820res = markFromRootsWork(asynch);3821if (res) {3822_collectorState = Precleaning;3823} else { // We failed and a foreground collection wants to take over3824assert(_foregroundGCIsActive, "internal state inconsistency");3825assert(_restart_addr == NULL, "foreground will restart from scratch");3826if (PrintGCDetails) {3827gclog_or_tty->print_cr("bailing out to foreground collection");3828}3829}3830if (UseAdaptiveSizePolicy) {3831size_policy()->concurrent_marking_end();3832}3833} else {3834assert(SafepointSynchronize::is_at_safepoint(),3835"inconsistent with asynch == false");3836if (UseAdaptiveSizePolicy) {3837size_policy()->ms_collection_marking_begin();3838}3839// already have locks3840res = markFromRootsWork(asynch);3841_collectorState = FinalMarking;3842if (UseAdaptiveSizePolicy) {3843GenCollectedHeap* gch = GenCollectedHeap::heap();3844size_policy()->ms_collection_marking_end(gch->gc_cause());3845}3846}3847verify_overflow_empty();3848return res;3849}38503851bool CMSCollector::markFromRootsWork(bool asynch) {3852// iterate over marked bits in bit map, doing a full scan and mark3853// from these roots using the following algorithm:3854// . if oop is to the right of the current scan pointer,3855// mark corresponding bit (we'll process it later)3856// . else (oop is to left of current scan pointer)3857// push oop on marking stack3858// . drain the marking stack38593860// Note that when we do a marking step we need to hold the3861// bit map lock -- recall that direct allocation (by mutators)3862// and promotion (by younger generation collectors) is also3863// marking the bit map. [the so-called allocate live policy.]3864// Because the implementation of bit map marking is not3865// robust wrt simultaneous marking of bits in the same word,3866// we need to make sure that there is no such interference3867// between concurrent such updates.38683869// already have locks3870assert_lock_strong(bitMapLock());38713872verify_work_stacks_empty();3873verify_overflow_empty();3874bool result = false;3875if (CMSConcurrentMTEnabled && ConcGCThreads > 0) {3876result = do_marking_mt(asynch);3877} else {3878result = do_marking_st(asynch);3879}3880return result;3881}38823883// Forward decl3884class CMSConcMarkingTask;38853886class CMSConcMarkingTerminator: public ParallelTaskTerminator {3887CMSCollector* _collector;3888CMSConcMarkingTask* _task;3889public:3890virtual void yield();38913892// "n_threads" is the number of threads to be terminated.3893// "queue_set" is a set of work queues of other threads.3894// "collector" is the CMS collector associated with this task terminator.3895// "yield" indicates whether we need the gang as a whole to yield.3896CMSConcMarkingTerminator(int n_threads, TaskQueueSetSuper* queue_set, CMSCollector* collector) :3897ParallelTaskTerminator(n_threads, queue_set),3898_collector(collector) { }38993900void set_task(CMSConcMarkingTask* task) {3901_task = task;3902}3903};39043905class CMSConcMarkingTerminatorTerminator: public TerminatorTerminator {3906CMSConcMarkingTask* _task;3907public:3908bool should_exit_termination();3909void set_task(CMSConcMarkingTask* task) {3910_task = task;3911}3912};39133914// MT Concurrent Marking Task3915class CMSConcMarkingTask: public YieldingFlexibleGangTask {3916CMSCollector* _collector;3917int _n_workers; // requested/desired # workers3918bool _asynch;3919bool _result;3920CompactibleFreeListSpace* _cms_space;3921char _pad_front[64]; // padding to ...3922HeapWord* _global_finger; // ... avoid sharing cache line3923char _pad_back[64];3924HeapWord* _restart_addr;39253926// Exposed here for yielding support3927Mutex* const _bit_map_lock;39283929// The per thread work queues, available here for stealing3930OopTaskQueueSet* _task_queues;39313932// Termination (and yielding) support3933CMSConcMarkingTerminator _term;3934CMSConcMarkingTerminatorTerminator _term_term;39353936public:3937CMSConcMarkingTask(CMSCollector* collector,3938CompactibleFreeListSpace* cms_space,3939bool asynch,3940YieldingFlexibleWorkGang* workers,3941OopTaskQueueSet* task_queues):3942YieldingFlexibleGangTask("Concurrent marking done multi-threaded"),3943_collector(collector),3944_cms_space(cms_space),3945_asynch(asynch), _n_workers(0), _result(true),3946_task_queues(task_queues),3947_term(_n_workers, task_queues, _collector),3948_bit_map_lock(collector->bitMapLock())3949{3950_requested_size = _n_workers;3951_term.set_task(this);3952_term_term.set_task(this);3953_restart_addr = _global_finger = _cms_space->bottom();3954}395539563957OopTaskQueueSet* task_queues() { return _task_queues; }39583959OopTaskQueue* work_queue(int i) { return task_queues()->queue(i); }39603961HeapWord** global_finger_addr() { return &_global_finger; }39623963CMSConcMarkingTerminator* terminator() { return &_term; }39643965virtual void set_for_termination(int active_workers) {3966terminator()->reset_for_reuse(active_workers);3967}39683969void work(uint worker_id);3970bool should_yield() {3971return ConcurrentMarkSweepThread::should_yield()3972&& !_collector->foregroundGCIsActive()3973&& _asynch;3974}39753976virtual void coordinator_yield(); // stuff done by coordinator3977bool result() { return _result; }39783979void reset(HeapWord* ra) {3980assert(_global_finger >= _cms_space->end(), "Postcondition of ::work(i)");3981_restart_addr = _global_finger = ra;3982_term.reset_for_reuse();3983}39843985static bool get_work_from_overflow_stack(CMSMarkStack* ovflw_stk,3986OopTaskQueue* work_q);39873988private:3989void do_scan_and_mark(int i, CompactibleFreeListSpace* sp);3990void do_work_steal(int i);3991void bump_global_finger(HeapWord* f);3992};39933994bool CMSConcMarkingTerminatorTerminator::should_exit_termination() {3995assert(_task != NULL, "Error");3996return _task->yielding();3997// Note that we do not need the disjunct || _task->should_yield() above3998// because we want terminating threads to yield only if the task3999// is already in the midst of yielding, which happens only after at least one4000// thread has yielded.4001}40024003void CMSConcMarkingTerminator::yield() {4004if (_task->should_yield()) {4005_task->yield();4006} else {4007ParallelTaskTerminator::yield();4008}4009}40104011////////////////////////////////////////////////////////////////4012// Concurrent Marking Algorithm Sketch4013////////////////////////////////////////////////////////////////4014// Until all tasks exhausted (both spaces):4015// -- claim next available chunk4016// -- bump global finger via CAS4017// -- find first object that starts in this chunk4018// and start scanning bitmap from that position4019// -- scan marked objects for oops4020// -- CAS-mark target, and if successful:4021// . if target oop is above global finger (volatile read)4022// nothing to do4023// . if target oop is in chunk and above local finger4024// then nothing to do4025// . else push on work-queue4026// -- Deal with possible overflow issues:4027// . local work-queue overflow causes stuff to be pushed on4028// global (common) overflow queue4029// . always first empty local work queue4030// . then get a batch of oops from global work queue if any4031// . then do work stealing4032// -- When all tasks claimed (both spaces)4033// and local work queue empty,4034// then in a loop do:4035// . check global overflow stack; steal a batch of oops and trace4036// . try to steal from other threads oif GOS is empty4037// . if neither is available, offer termination4038// -- Terminate and return result4039//4040void CMSConcMarkingTask::work(uint worker_id) {4041elapsedTimer _timer;4042ResourceMark rm;4043HandleMark hm;40444045DEBUG_ONLY(_collector->verify_overflow_empty();)40464047// Before we begin work, our work queue should be empty4048assert(work_queue(worker_id)->size() == 0, "Expected to be empty");4049// Scan the bitmap covering _cms_space, tracing through grey objects.4050_timer.start();4051do_scan_and_mark(worker_id, _cms_space);4052_timer.stop();4053if (PrintCMSStatistics != 0) {4054gclog_or_tty->print_cr("Finished cms space scanning in %dth thread: %3.3f sec",4055worker_id, _timer.seconds());4056// XXX: need xxx/xxx type of notation, two timers4057}40584059// ... do work stealing4060_timer.reset();4061_timer.start();4062do_work_steal(worker_id);4063_timer.stop();4064if (PrintCMSStatistics != 0) {4065gclog_or_tty->print_cr("Finished work stealing in %dth thread: %3.3f sec",4066worker_id, _timer.seconds());4067// XXX: need xxx/xxx type of notation, two timers4068}4069assert(_collector->_markStack.isEmpty(), "Should have been emptied");4070assert(work_queue(worker_id)->size() == 0, "Should have been emptied");4071// Note that under the current task protocol, the4072// following assertion is true even of the spaces4073// expanded since the completion of the concurrent4074// marking. XXX This will likely change under a strict4075// ABORT semantics.4076// After perm removal the comparison was changed to4077// greater than or equal to from strictly greater than.4078// Before perm removal the highest address sweep would4079// have been at the end of perm gen but now is at the4080// end of the tenured gen.4081assert(_global_finger >= _cms_space->end(),4082"All tasks have been completed");4083DEBUG_ONLY(_collector->verify_overflow_empty();)4084}40854086void CMSConcMarkingTask::bump_global_finger(HeapWord* f) {4087HeapWord* read = _global_finger;4088HeapWord* cur = read;4089while (f > read) {4090cur = read;4091read = (HeapWord*) Atomic::cmpxchg_ptr(f, &_global_finger, cur);4092if (cur == read) {4093// our cas succeeded4094assert(_global_finger >= f, "protocol consistency");4095break;4096}4097}4098}40994100// This is really inefficient, and should be redone by4101// using (not yet available) block-read and -write interfaces to the4102// stack and the work_queue. XXX FIX ME !!!4103bool CMSConcMarkingTask::get_work_from_overflow_stack(CMSMarkStack* ovflw_stk,4104OopTaskQueue* work_q) {4105// Fast lock-free check4106if (ovflw_stk->length() == 0) {4107return false;4108}4109assert(work_q->size() == 0, "Shouldn't steal");4110MutexLockerEx ml(ovflw_stk->par_lock(),4111Mutex::_no_safepoint_check_flag);4112// Grab up to 1/4 the size of the work queue4113size_t num = MIN2((size_t)(work_q->max_elems() - work_q->size())/4,4114(size_t)ParGCDesiredObjsFromOverflowList);4115num = MIN2(num, ovflw_stk->length());4116for (int i = (int) num; i > 0; i--) {4117oop cur = ovflw_stk->pop();4118assert(cur != NULL, "Counted wrong?");4119work_q->push(cur);4120}4121return num > 0;4122}41234124void CMSConcMarkingTask::do_scan_and_mark(int i, CompactibleFreeListSpace* sp) {4125SequentialSubTasksDone* pst = sp->conc_par_seq_tasks();4126int n_tasks = pst->n_tasks();4127// We allow that there may be no tasks to do here because4128// we are restarting after a stack overflow.4129assert(pst->valid() || n_tasks == 0, "Uninitialized use?");4130uint nth_task = 0;41314132HeapWord* aligned_start = sp->bottom();4133if (sp->used_region().contains(_restart_addr)) {4134// Align down to a card boundary for the start of 0th task4135// for this space.4136aligned_start =4137(HeapWord*)align_size_down((uintptr_t)_restart_addr,4138CardTableModRefBS::card_size);4139}41404141size_t chunk_size = sp->marking_task_size();4142while (!pst->is_task_claimed(/* reference */ nth_task)) {4143// Having claimed the nth task in this space,4144// compute the chunk that it corresponds to:4145MemRegion span = MemRegion(aligned_start + nth_task*chunk_size,4146aligned_start + (nth_task+1)*chunk_size);4147// Try and bump the global finger via a CAS;4148// note that we need to do the global finger bump4149// _before_ taking the intersection below, because4150// the task corresponding to that region will be4151// deemed done even if the used_region() expands4152// because of allocation -- as it almost certainly will4153// during start-up while the threads yield in the4154// closure below.4155HeapWord* finger = span.end();4156bump_global_finger(finger); // atomically4157// There are null tasks here corresponding to chunks4158// beyond the "top" address of the space.4159span = span.intersection(sp->used_region());4160if (!span.is_empty()) { // Non-null task4161HeapWord* prev_obj;4162assert(!span.contains(_restart_addr) || nth_task == 0,4163"Inconsistency");4164if (nth_task == 0) {4165// For the 0th task, we'll not need to compute a block_start.4166if (span.contains(_restart_addr)) {4167// In the case of a restart because of stack overflow,4168// we might additionally skip a chunk prefix.4169prev_obj = _restart_addr;4170} else {4171prev_obj = span.start();4172}4173} else {4174// We want to skip the first object because4175// the protocol is to scan any object in its entirety4176// that _starts_ in this span; a fortiori, any4177// object starting in an earlier span is scanned4178// as part of an earlier claimed task.4179// Below we use the "careful" version of block_start4180// so we do not try to navigate uninitialized objects.4181prev_obj = sp->block_start_careful(span.start());4182// Below we use a variant of block_size that uses the4183// Printezis bits to avoid waiting for allocated4184// objects to become initialized/parsable.4185while (prev_obj < span.start()) {4186size_t sz = sp->block_size_no_stall(prev_obj, _collector);4187if (sz > 0) {4188prev_obj += sz;4189} else {4190// In this case we may end up doing a bit of redundant4191// scanning, but that appears unavoidable, short of4192// locking the free list locks; see bug 6324141.4193break;4194}4195}4196}4197if (prev_obj < span.end()) {4198MemRegion my_span = MemRegion(prev_obj, span.end());4199// Do the marking work within a non-empty span --4200// the last argument to the constructor indicates whether the4201// iteration should be incremental with periodic yields.4202Par_MarkFromRootsClosure cl(this, _collector, my_span,4203&_collector->_markBitMap,4204work_queue(i),4205&_collector->_markStack,4206_asynch);4207_collector->_markBitMap.iterate(&cl, my_span.start(), my_span.end());4208} // else nothing to do for this task4209} // else nothing to do for this task4210}4211// We'd be tempted to assert here that since there are no4212// more tasks left to claim in this space, the global_finger4213// must exceed space->top() and a fortiori space->end(). However,4214// that would not quite be correct because the bumping of4215// global_finger occurs strictly after the claiming of a task,4216// so by the time we reach here the global finger may not yet4217// have been bumped up by the thread that claimed the last4218// task.4219pst->all_tasks_completed();4220}42214222class Par_ConcMarkingClosure: public MetadataAwareOopClosure {4223private:4224CMSCollector* _collector;4225CMSConcMarkingTask* _task;4226MemRegion _span;4227CMSBitMap* _bit_map;4228CMSMarkStack* _overflow_stack;4229OopTaskQueue* _work_queue;4230protected:4231DO_OOP_WORK_DEFN4232public:4233Par_ConcMarkingClosure(CMSCollector* collector, CMSConcMarkingTask* task, OopTaskQueue* work_queue,4234CMSBitMap* bit_map, CMSMarkStack* overflow_stack):4235MetadataAwareOopClosure(collector->ref_processor()),4236_collector(collector),4237_task(task),4238_span(collector->_span),4239_work_queue(work_queue),4240_bit_map(bit_map),4241_overflow_stack(overflow_stack)4242{ }4243virtual void do_oop(oop* p);4244virtual void do_oop(narrowOop* p);42454246void trim_queue(size_t max);4247void handle_stack_overflow(HeapWord* lost);4248void do_yield_check() {4249if (_task->should_yield()) {4250_task->yield();4251}4252}4253};42544255// Grey object scanning during work stealing phase --4256// the salient assumption here is that any references4257// that are in these stolen objects being scanned must4258// already have been initialized (else they would not have4259// been published), so we do not need to check for4260// uninitialized objects before pushing here.4261void Par_ConcMarkingClosure::do_oop(oop obj) {4262assert(obj->is_oop_or_null(true), "expected an oop or NULL");4263HeapWord* addr = (HeapWord*)obj;4264// Check if oop points into the CMS generation4265// and is not marked4266if (_span.contains(addr) && !_bit_map->isMarked(addr)) {4267// a white object ...4268// If we manage to "claim" the object, by being the4269// first thread to mark it, then we push it on our4270// marking stack4271if (_bit_map->par_mark(addr)) { // ... now grey4272// push on work queue (grey set)4273bool simulate_overflow = false;4274NOT_PRODUCT(4275if (CMSMarkStackOverflowALot &&4276_collector->simulate_overflow()) {4277// simulate a stack overflow4278simulate_overflow = true;4279}4280)4281if (simulate_overflow ||4282!(_work_queue->push(obj) || _overflow_stack->par_push(obj))) {4283// stack overflow4284if (PrintCMSStatistics != 0) {4285gclog_or_tty->print_cr("CMS marking stack overflow (benign) at "4286SIZE_FORMAT, _overflow_stack->capacity());4287}4288// We cannot assert that the overflow stack is full because4289// it may have been emptied since.4290assert(simulate_overflow ||4291_work_queue->size() == _work_queue->max_elems(),4292"Else push should have succeeded");4293handle_stack_overflow(addr);4294}4295} // Else, some other thread got there first4296do_yield_check();4297}4298}42994300void Par_ConcMarkingClosure::do_oop(oop* p) { Par_ConcMarkingClosure::do_oop_work(p); }4301void Par_ConcMarkingClosure::do_oop(narrowOop* p) { Par_ConcMarkingClosure::do_oop_work(p); }43024303void Par_ConcMarkingClosure::trim_queue(size_t max) {4304while (_work_queue->size() > max) {4305oop new_oop;4306if (_work_queue->pop_local(new_oop)) {4307assert(new_oop->is_oop(), "Should be an oop");4308assert(_bit_map->isMarked((HeapWord*)new_oop), "Grey object");4309assert(_span.contains((HeapWord*)new_oop), "Not in span");4310new_oop->oop_iterate(this); // do_oop() above4311do_yield_check();4312}4313}4314}43154316// Upon stack overflow, we discard (part of) the stack,4317// remembering the least address amongst those discarded4318// in CMSCollector's _restart_address.4319void Par_ConcMarkingClosure::handle_stack_overflow(HeapWord* lost) {4320// We need to do this under a mutex to prevent other4321// workers from interfering with the work done below.4322MutexLockerEx ml(_overflow_stack->par_lock(),4323Mutex::_no_safepoint_check_flag);4324// Remember the least grey address discarded4325HeapWord* ra = (HeapWord*)_overflow_stack->least_value(lost);4326_collector->lower_restart_addr(ra);4327_overflow_stack->reset(); // discard stack contents4328_overflow_stack->expand(); // expand the stack if possible4329}433043314332void CMSConcMarkingTask::do_work_steal(int i) {4333OopTaskQueue* work_q = work_queue(i);4334oop obj_to_scan;4335CMSBitMap* bm = &(_collector->_markBitMap);4336CMSMarkStack* ovflw = &(_collector->_markStack);4337int* seed = _collector->hash_seed(i);4338Par_ConcMarkingClosure cl(_collector, this, work_q, bm, ovflw);4339while (true) {4340cl.trim_queue(0);4341assert(work_q->size() == 0, "Should have been emptied above");4342if (get_work_from_overflow_stack(ovflw, work_q)) {4343// Can't assert below because the work obtained from the4344// overflow stack may already have been stolen from us.4345// assert(work_q->size() > 0, "Work from overflow stack");4346continue;4347} else if (task_queues()->steal(i, seed, /* reference */ obj_to_scan)) {4348assert(obj_to_scan->is_oop(), "Should be an oop");4349assert(bm->isMarked((HeapWord*)obj_to_scan), "Grey object");4350obj_to_scan->oop_iterate(&cl);4351} else if (terminator()->offer_termination(&_term_term)) {4352assert(work_q->size() == 0, "Impossible!");4353break;4354} else if (yielding() || should_yield()) {4355yield();4356}4357}4358}43594360// This is run by the CMS (coordinator) thread.4361void CMSConcMarkingTask::coordinator_yield() {4362assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),4363"CMS thread should hold CMS token");4364// First give up the locks, then yield, then re-lock4365// We should probably use a constructor/destructor idiom to4366// do this unlock/lock or modify the MutexUnlocker class to4367// serve our purpose. XXX4368assert_lock_strong(_bit_map_lock);4369_bit_map_lock->unlock();4370ConcurrentMarkSweepThread::desynchronize(true);4371ConcurrentMarkSweepThread::acknowledge_yield_request();4372_collector->stopTimer();4373if (PrintCMSStatistics != 0) {4374_collector->incrementYields();4375}4376_collector->icms_wait();43774378// It is possible for whichever thread initiated the yield request4379// not to get a chance to wake up and take the bitmap lock between4380// this thread releasing it and reacquiring it. So, while the4381// should_yield() flag is on, let's sleep for a bit to give the4382// other thread a chance to wake up. The limit imposed on the number4383// of iterations is defensive, to avoid any unforseen circumstances4384// putting us into an infinite loop. Since it's always been this4385// (coordinator_yield()) method that was observed to cause the4386// problem, we are using a parameter (CMSCoordinatorYieldSleepCount)4387// which is by default non-zero. For the other seven methods that4388// also perform the yield operation, as are using a different4389// parameter (CMSYieldSleepCount) which is by default zero. This way we4390// can enable the sleeping for those methods too, if necessary.4391// See 6442774.4392//4393// We really need to reconsider the synchronization between the GC4394// thread and the yield-requesting threads in the future and we4395// should really use wait/notify, which is the recommended4396// way of doing this type of interaction. Additionally, we should4397// consolidate the eight methods that do the yield operation and they4398// are almost identical into one for better maintenability and4399// readability. See 6445193.4400//4401// Tony 2006.06.294402for (unsigned i = 0; i < CMSCoordinatorYieldSleepCount &&4403ConcurrentMarkSweepThread::should_yield() &&4404!CMSCollector::foregroundGCIsActive(); ++i) {4405os::sleep(Thread::current(), 1, false);4406ConcurrentMarkSweepThread::acknowledge_yield_request();4407}44084409ConcurrentMarkSweepThread::synchronize(true);4410_bit_map_lock->lock_without_safepoint_check();4411_collector->startTimer();4412}44134414bool CMSCollector::do_marking_mt(bool asynch) {4415assert(ConcGCThreads > 0 && conc_workers() != NULL, "precondition");4416int num_workers = AdaptiveSizePolicy::calc_active_conc_workers(4417conc_workers()->total_workers(),4418conc_workers()->active_workers(),4419Threads::number_of_non_daemon_threads());4420conc_workers()->set_active_workers(num_workers);44214422CompactibleFreeListSpace* cms_space = _cmsGen->cmsSpace();44234424CMSConcMarkingTask tsk(this,4425cms_space,4426asynch,4427conc_workers(),4428task_queues());44294430// Since the actual number of workers we get may be different4431// from the number we requested above, do we need to do anything different4432// below? In particular, may be we need to subclass the SequantialSubTasksDone4433// class?? XXX4434cms_space ->initialize_sequential_subtasks_for_marking(num_workers);44354436// Refs discovery is already non-atomic.4437assert(!ref_processor()->discovery_is_atomic(), "Should be non-atomic");4438assert(ref_processor()->discovery_is_mt(), "Discovery should be MT");4439conc_workers()->start_task(&tsk);4440while (tsk.yielded()) {4441tsk.coordinator_yield();4442conc_workers()->continue_task(&tsk);4443}4444// If the task was aborted, _restart_addr will be non-NULL4445assert(tsk.completed() || _restart_addr != NULL, "Inconsistency");4446while (_restart_addr != NULL) {4447// XXX For now we do not make use of ABORTED state and have not4448// yet implemented the right abort semantics (even in the original4449// single-threaded CMS case). That needs some more investigation4450// and is deferred for now; see CR# TBF. 07252005YSR. XXX4451assert(!CMSAbortSemantics || tsk.aborted(), "Inconsistency");4452// If _restart_addr is non-NULL, a marking stack overflow4453// occurred; we need to do a fresh marking iteration from the4454// indicated restart address.4455if (_foregroundGCIsActive && asynch) {4456// We may be running into repeated stack overflows, having4457// reached the limit of the stack size, while making very4458// slow forward progress. It may be best to bail out and4459// let the foreground collector do its job.4460// Clear _restart_addr, so that foreground GC4461// works from scratch. This avoids the headache of4462// a "rescan" which would otherwise be needed because4463// of the dirty mod union table & card table.4464_restart_addr = NULL;4465return false;4466}4467// Adjust the task to restart from _restart_addr4468tsk.reset(_restart_addr);4469cms_space ->initialize_sequential_subtasks_for_marking(num_workers,4470_restart_addr);4471_restart_addr = NULL;4472// Get the workers going again4473conc_workers()->start_task(&tsk);4474while (tsk.yielded()) {4475tsk.coordinator_yield();4476conc_workers()->continue_task(&tsk);4477}4478}4479assert(tsk.completed(), "Inconsistency");4480assert(tsk.result() == true, "Inconsistency");4481return true;4482}44834484bool CMSCollector::do_marking_st(bool asynch) {4485ResourceMark rm;4486HandleMark hm;44874488// Temporarily make refs discovery single threaded (non-MT)4489ReferenceProcessorMTDiscoveryMutator rp_mut_discovery(ref_processor(), false);4490MarkFromRootsClosure markFromRootsClosure(this, _span, &_markBitMap,4491&_markStack, CMSYield && asynch);4492// the last argument to iterate indicates whether the iteration4493// should be incremental with periodic yields.4494_markBitMap.iterate(&markFromRootsClosure);4495// If _restart_addr is non-NULL, a marking stack overflow4496// occurred; we need to do a fresh iteration from the4497// indicated restart address.4498while (_restart_addr != NULL) {4499if (_foregroundGCIsActive && asynch) {4500// We may be running into repeated stack overflows, having4501// reached the limit of the stack size, while making very4502// slow forward progress. It may be best to bail out and4503// let the foreground collector do its job.4504// Clear _restart_addr, so that foreground GC4505// works from scratch. This avoids the headache of4506// a "rescan" which would otherwise be needed because4507// of the dirty mod union table & card table.4508_restart_addr = NULL;4509return false; // indicating failure to complete marking4510}4511// Deal with stack overflow:4512// we restart marking from _restart_addr4513HeapWord* ra = _restart_addr;4514markFromRootsClosure.reset(ra);4515_restart_addr = NULL;4516_markBitMap.iterate(&markFromRootsClosure, ra, _span.end());4517}4518return true;4519}45204521void CMSCollector::preclean() {4522check_correct_thread_executing();4523assert(Thread::current()->is_ConcurrentGC_thread(), "Wrong thread");4524verify_work_stacks_empty();4525verify_overflow_empty();4526_abort_preclean = false;4527if (CMSPrecleaningEnabled) {4528if (!CMSEdenChunksRecordAlways) {4529_eden_chunk_index = 0;4530}4531size_t used = get_eden_used();4532size_t capacity = get_eden_capacity();4533// Don't start sampling unless we will get sufficiently4534// many samples.4535if (used < (capacity/(CMSScheduleRemarkSamplingRatio * 100)4536* CMSScheduleRemarkEdenPenetration)) {4537_start_sampling = true;4538} else {4539_start_sampling = false;4540}4541TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);4542CMSPhaseAccounting pa(this, "preclean", _gc_tracer_cm->gc_id(), !PrintGCDetails);4543preclean_work(CMSPrecleanRefLists1, CMSPrecleanSurvivors1);4544}4545CMSTokenSync x(true); // is cms thread4546if (CMSPrecleaningEnabled) {4547sample_eden();4548_collectorState = AbortablePreclean;4549} else {4550_collectorState = FinalMarking;4551}4552verify_work_stacks_empty();4553verify_overflow_empty();4554}45554556// Try and schedule the remark such that young gen4557// occupancy is CMSScheduleRemarkEdenPenetration %.4558void CMSCollector::abortable_preclean() {4559check_correct_thread_executing();4560assert(CMSPrecleaningEnabled, "Inconsistent control state");4561assert(_collectorState == AbortablePreclean, "Inconsistent control state");45624563// If Eden's current occupancy is below this threshold,4564// immediately schedule the remark; else preclean4565// past the next scavenge in an effort to4566// schedule the pause as described avove. By choosing4567// CMSScheduleRemarkEdenSizeThreshold >= max eden size4568// we will never do an actual abortable preclean cycle.4569if (get_eden_used() > CMSScheduleRemarkEdenSizeThreshold) {4570TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);4571CMSPhaseAccounting pa(this, "abortable-preclean", _gc_tracer_cm->gc_id(), !PrintGCDetails);4572// We need more smarts in the abortable preclean4573// loop below to deal with cases where allocation4574// in young gen is very very slow, and our precleaning4575// is running a losing race against a horde of4576// mutators intent on flooding us with CMS updates4577// (dirty cards).4578// One, admittedly dumb, strategy is to give up4579// after a certain number of abortable precleaning loops4580// or after a certain maximum time. We want to make4581// this smarter in the next iteration.4582// XXX FIX ME!!! YSR4583size_t loops = 0, workdone = 0, cumworkdone = 0, waited = 0;4584while (!(should_abort_preclean() ||4585ConcurrentMarkSweepThread::should_terminate())) {4586workdone = preclean_work(CMSPrecleanRefLists2, CMSPrecleanSurvivors2);4587cumworkdone += workdone;4588loops++;4589// Voluntarily terminate abortable preclean phase if we have4590// been at it for too long.4591if ((CMSMaxAbortablePrecleanLoops != 0) &&4592loops >= CMSMaxAbortablePrecleanLoops) {4593if (PrintGCDetails) {4594gclog_or_tty->print(" CMS: abort preclean due to loops ");4595}4596break;4597}4598if (pa.wallclock_millis() > CMSMaxAbortablePrecleanTime) {4599if (PrintGCDetails) {4600gclog_or_tty->print(" CMS: abort preclean due to time ");4601}4602break;4603}4604// If we are doing little work each iteration, we should4605// take a short break.4606if (workdone < CMSAbortablePrecleanMinWorkPerIteration) {4607// Sleep for some time, waiting for work to accumulate4608stopTimer();4609cmsThread()->wait_on_cms_lock(CMSAbortablePrecleanWaitMillis);4610startTimer();4611waited++;4612}4613}4614if (PrintCMSStatistics > 0) {4615gclog_or_tty->print(" [%d iterations, %d waits, %d cards)] ",4616loops, waited, cumworkdone);4617}4618}4619CMSTokenSync x(true); // is cms thread4620if (_collectorState != Idling) {4621assert(_collectorState == AbortablePreclean,4622"Spontaneous state transition?");4623_collectorState = FinalMarking;4624} // Else, a foreground collection completed this CMS cycle.4625return;4626}46274628// Respond to an Eden sampling opportunity4629void CMSCollector::sample_eden() {4630// Make sure a young gc cannot sneak in between our4631// reading and recording of a sample.4632assert(Thread::current()->is_ConcurrentGC_thread(),4633"Only the cms thread may collect Eden samples");4634assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),4635"Should collect samples while holding CMS token");4636if (!_start_sampling) {4637return;4638}4639// When CMSEdenChunksRecordAlways is true, the eden chunk array4640// is populated by the young generation.4641if (_eden_chunk_array != NULL && !CMSEdenChunksRecordAlways) {4642if (_eden_chunk_index < _eden_chunk_capacity) {4643_eden_chunk_array[_eden_chunk_index] = *_top_addr; // take sample4644assert(_eden_chunk_array[_eden_chunk_index] <= *_end_addr,4645"Unexpected state of Eden");4646// We'd like to check that what we just sampled is an oop-start address;4647// however, we cannot do that here since the object may not yet have been4648// initialized. So we'll instead do the check when we _use_ this sample4649// later.4650if (_eden_chunk_index == 0 ||4651(pointer_delta(_eden_chunk_array[_eden_chunk_index],4652_eden_chunk_array[_eden_chunk_index-1])4653>= CMSSamplingGrain)) {4654_eden_chunk_index++; // commit sample4655}4656}4657}4658if ((_collectorState == AbortablePreclean) && !_abort_preclean) {4659size_t used = get_eden_used();4660size_t capacity = get_eden_capacity();4661assert(used <= capacity, "Unexpected state of Eden");4662if (used > (capacity/100 * CMSScheduleRemarkEdenPenetration)) {4663_abort_preclean = true;4664}4665}4666}466746684669size_t CMSCollector::preclean_work(bool clean_refs, bool clean_survivor) {4670assert(_collectorState == Precleaning ||4671_collectorState == AbortablePreclean, "incorrect state");4672ResourceMark rm;4673HandleMark hm;46744675// Precleaning is currently not MT but the reference processor4676// may be set for MT. Disable it temporarily here.4677ReferenceProcessor* rp = ref_processor();4678ReferenceProcessorMTDiscoveryMutator rp_mut_discovery(rp, false);46794680// Do one pass of scrubbing the discovered reference lists4681// to remove any reference objects with strongly-reachable4682// referents.4683if (clean_refs) {4684CMSPrecleanRefsYieldClosure yield_cl(this);4685assert(rp->span().equals(_span), "Spans should be equal");4686CMSKeepAliveClosure keep_alive(this, _span, &_markBitMap,4687&_markStack, true /* preclean */);4688CMSDrainMarkingStackClosure complete_trace(this,4689_span, &_markBitMap, &_markStack,4690&keep_alive, true /* preclean */);46914692// We don't want this step to interfere with a young4693// collection because we don't want to take CPU4694// or memory bandwidth away from the young GC threads4695// (which may be as many as there are CPUs).4696// Note that we don't need to protect ourselves from4697// interference with mutators because they can't4698// manipulate the discovered reference lists nor affect4699// the computed reachability of the referents, the4700// only properties manipulated by the precleaning4701// of these reference lists.4702stopTimer();4703CMSTokenSyncWithLocks x(true /* is cms thread */,4704bitMapLock());4705startTimer();4706sample_eden();47074708// The following will yield to allow foreground4709// collection to proceed promptly. XXX YSR:4710// The code in this method may need further4711// tweaking for better performance and some restructuring4712// for cleaner interfaces.4713GCTimer *gc_timer = NULL; // Currently not tracing concurrent phases4714rp->preclean_discovered_references(4715rp->is_alive_non_header(), &keep_alive, &complete_trace, &yield_cl,4716gc_timer, _gc_tracer_cm->gc_id());4717}47184719if (clean_survivor) { // preclean the active survivor space(s)4720assert(_young_gen->kind() == Generation::DefNew ||4721_young_gen->kind() == Generation::ParNew ||4722_young_gen->kind() == Generation::ASParNew,4723"incorrect type for cast");4724DefNewGeneration* dng = (DefNewGeneration*)_young_gen;4725PushAndMarkClosure pam_cl(this, _span, ref_processor(),4726&_markBitMap, &_modUnionTable,4727&_markStack, true /* precleaning phase */);4728stopTimer();4729CMSTokenSyncWithLocks ts(true /* is cms thread */,4730bitMapLock());4731startTimer();4732unsigned int before_count =4733GenCollectedHeap::heap()->total_collections();4734SurvivorSpacePrecleanClosure4735sss_cl(this, _span, &_markBitMap, &_markStack,4736&pam_cl, before_count, CMSYield);4737dng->from()->object_iterate_careful(&sss_cl);4738dng->to()->object_iterate_careful(&sss_cl);4739}4740MarkRefsIntoAndScanClosure4741mrias_cl(_span, ref_processor(), &_markBitMap, &_modUnionTable,4742&_markStack, this, CMSYield,4743true /* precleaning phase */);4744// CAUTION: The following closure has persistent state that may need to4745// be reset upon a decrease in the sequence of addresses it4746// processes.4747ScanMarkedObjectsAgainCarefullyClosure4748smoac_cl(this, _span,4749&_markBitMap, &_markStack, &mrias_cl, CMSYield);47504751// Preclean dirty cards in ModUnionTable and CardTable using4752// appropriate convergence criterion;4753// repeat CMSPrecleanIter times unless we find that4754// we are losing.4755assert(CMSPrecleanIter < 10, "CMSPrecleanIter is too large");4756assert(CMSPrecleanNumerator < CMSPrecleanDenominator,4757"Bad convergence multiplier");4758assert(CMSPrecleanThreshold >= 100,4759"Unreasonably low CMSPrecleanThreshold");47604761size_t numIter, cumNumCards, lastNumCards, curNumCards;4762for (numIter = 0, cumNumCards = lastNumCards = curNumCards = 0;4763numIter < CMSPrecleanIter;4764numIter++, lastNumCards = curNumCards, cumNumCards += curNumCards) {4765curNumCards = preclean_mod_union_table(_cmsGen, &smoac_cl);4766if (Verbose && PrintGCDetails) {4767gclog_or_tty->print(" (modUnionTable: %d cards)", curNumCards);4768}4769// Either there are very few dirty cards, so re-mark4770// pause will be small anyway, or our pre-cleaning isn't4771// that much faster than the rate at which cards are being4772// dirtied, so we might as well stop and re-mark since4773// precleaning won't improve our re-mark time by much.4774if (curNumCards <= CMSPrecleanThreshold ||4775(numIter > 0 &&4776(curNumCards * CMSPrecleanDenominator >4777lastNumCards * CMSPrecleanNumerator))) {4778numIter++;4779cumNumCards += curNumCards;4780break;4781}4782}47834784preclean_klasses(&mrias_cl, _cmsGen->freelistLock());47854786curNumCards = preclean_card_table(_cmsGen, &smoac_cl);4787cumNumCards += curNumCards;4788if (PrintGCDetails && PrintCMSStatistics != 0) {4789gclog_or_tty->print_cr(" (cardTable: %d cards, re-scanned %d cards, %d iterations)",4790curNumCards, cumNumCards, numIter);4791}4792return cumNumCards; // as a measure of useful work done4793}47944795// PRECLEANING NOTES:4796// Precleaning involves:4797// . reading the bits of the modUnionTable and clearing the set bits.4798// . For the cards corresponding to the set bits, we scan the4799// objects on those cards. This means we need the free_list_lock4800// so that we can safely iterate over the CMS space when scanning4801// for oops.4802// . When we scan the objects, we'll be both reading and setting4803// marks in the marking bit map, so we'll need the marking bit map.4804// . For protecting _collector_state transitions, we take the CGC_lock.4805// Note that any races in the reading of of card table entries by the4806// CMS thread on the one hand and the clearing of those entries by the4807// VM thread or the setting of those entries by the mutator threads on the4808// other are quite benign. However, for efficiency it makes sense to keep4809// the VM thread from racing with the CMS thread while the latter is4810// dirty card info to the modUnionTable. We therefore also use the4811// CGC_lock to protect the reading of the card table and the mod union4812// table by the CM thread.4813// . We run concurrently with mutator updates, so scanning4814// needs to be done carefully -- we should not try to scan4815// potentially uninitialized objects.4816//4817// Locking strategy: While holding the CGC_lock, we scan over and4818// reset a maximal dirty range of the mod union / card tables, then lock4819// the free_list_lock and bitmap lock to do a full marking, then4820// release these locks; and repeat the cycle. This allows for a4821// certain amount of fairness in the sharing of these locks between4822// the CMS collector on the one hand, and the VM thread and the4823// mutators on the other.48244825// NOTE: preclean_mod_union_table() and preclean_card_table()4826// further below are largely identical; if you need to modify4827// one of these methods, please check the other method too.48284829size_t CMSCollector::preclean_mod_union_table(4830ConcurrentMarkSweepGeneration* gen,4831ScanMarkedObjectsAgainCarefullyClosure* cl) {4832verify_work_stacks_empty();4833verify_overflow_empty();48344835// strategy: starting with the first card, accumulate contiguous4836// ranges of dirty cards; clear these cards, then scan the region4837// covered by these cards.48384839// Since all of the MUT is committed ahead, we can just use4840// that, in case the generations expand while we are precleaning.4841// It might also be fine to just use the committed part of the4842// generation, but we might potentially miss cards when the4843// generation is rapidly expanding while we are in the midst4844// of precleaning.4845HeapWord* startAddr = gen->reserved().start();4846HeapWord* endAddr = gen->reserved().end();48474848cl->setFreelistLock(gen->freelistLock()); // needed for yielding48494850size_t numDirtyCards, cumNumDirtyCards;4851HeapWord *nextAddr, *lastAddr;4852for (cumNumDirtyCards = numDirtyCards = 0,4853nextAddr = lastAddr = startAddr;4854nextAddr < endAddr;4855nextAddr = lastAddr, cumNumDirtyCards += numDirtyCards) {48564857ResourceMark rm;4858HandleMark hm;48594860MemRegion dirtyRegion;4861{4862stopTimer();4863// Potential yield point4864CMSTokenSync ts(true);4865startTimer();4866sample_eden();4867// Get dirty region starting at nextOffset (inclusive),4868// simultaneously clearing it.4869dirtyRegion =4870_modUnionTable.getAndClearMarkedRegion(nextAddr, endAddr);4871assert(dirtyRegion.start() >= nextAddr,4872"returned region inconsistent?");4873}4874// Remember where the next search should begin.4875// The returned region (if non-empty) is a right open interval,4876// so lastOffset is obtained from the right end of that4877// interval.4878lastAddr = dirtyRegion.end();4879// Should do something more transparent and less hacky XXX4880numDirtyCards =4881_modUnionTable.heapWordDiffToOffsetDiff(dirtyRegion.word_size());48824883// We'll scan the cards in the dirty region (with periodic4884// yields for foreground GC as needed).4885if (!dirtyRegion.is_empty()) {4886assert(numDirtyCards > 0, "consistency check");4887HeapWord* stop_point = NULL;4888stopTimer();4889// Potential yield point4890CMSTokenSyncWithLocks ts(true, gen->freelistLock(),4891bitMapLock());4892startTimer();4893{4894verify_work_stacks_empty();4895verify_overflow_empty();4896sample_eden();4897stop_point =4898gen->cmsSpace()->object_iterate_careful_m(dirtyRegion, cl);4899}4900if (stop_point != NULL) {4901// The careful iteration stopped early either because it found an4902// uninitialized object, or because we were in the midst of an4903// "abortable preclean", which should now be aborted. Redirty4904// the bits corresponding to the partially-scanned or unscanned4905// cards. We'll either restart at the next block boundary or4906// abort the preclean.4907assert((_collectorState == AbortablePreclean && should_abort_preclean()),4908"Should only be AbortablePreclean.");4909_modUnionTable.mark_range(MemRegion(stop_point, dirtyRegion.end()));4910if (should_abort_preclean()) {4911break; // out of preclean loop4912} else {4913// Compute the next address at which preclean should pick up;4914// might need bitMapLock in order to read P-bits.4915lastAddr = next_card_start_after_block(stop_point);4916}4917}4918} else {4919assert(lastAddr == endAddr, "consistency check");4920assert(numDirtyCards == 0, "consistency check");4921break;4922}4923}4924verify_work_stacks_empty();4925verify_overflow_empty();4926return cumNumDirtyCards;4927}49284929// NOTE: preclean_mod_union_table() above and preclean_card_table()4930// below are largely identical; if you need to modify4931// one of these methods, please check the other method too.49324933size_t CMSCollector::preclean_card_table(ConcurrentMarkSweepGeneration* gen,4934ScanMarkedObjectsAgainCarefullyClosure* cl) {4935// strategy: it's similar to precleamModUnionTable above, in that4936// we accumulate contiguous ranges of dirty cards, mark these cards4937// precleaned, then scan the region covered by these cards.4938HeapWord* endAddr = (HeapWord*)(gen->_virtual_space.high());4939HeapWord* startAddr = (HeapWord*)(gen->_virtual_space.low());49404941cl->setFreelistLock(gen->freelistLock()); // needed for yielding49424943size_t numDirtyCards, cumNumDirtyCards;4944HeapWord *lastAddr, *nextAddr;49454946for (cumNumDirtyCards = numDirtyCards = 0,4947nextAddr = lastAddr = startAddr;4948nextAddr < endAddr;4949nextAddr = lastAddr, cumNumDirtyCards += numDirtyCards) {49504951ResourceMark rm;4952HandleMark hm;49534954MemRegion dirtyRegion;4955{4956// See comments in "Precleaning notes" above on why we4957// do this locking. XXX Could the locking overheads be4958// too high when dirty cards are sparse? [I don't think so.]4959stopTimer();4960CMSTokenSync x(true); // is cms thread4961startTimer();4962sample_eden();4963// Get and clear dirty region from card table4964dirtyRegion = _ct->ct_bs()->dirty_card_range_after_reset(4965MemRegion(nextAddr, endAddr),4966true,4967CardTableModRefBS::precleaned_card_val());49684969assert(dirtyRegion.start() >= nextAddr,4970"returned region inconsistent?");4971}4972lastAddr = dirtyRegion.end();4973numDirtyCards =4974dirtyRegion.word_size()/CardTableModRefBS::card_size_in_words;49754976if (!dirtyRegion.is_empty()) {4977stopTimer();4978CMSTokenSyncWithLocks ts(true, gen->freelistLock(), bitMapLock());4979startTimer();4980sample_eden();4981verify_work_stacks_empty();4982verify_overflow_empty();4983HeapWord* stop_point =4984gen->cmsSpace()->object_iterate_careful_m(dirtyRegion, cl);4985if (stop_point != NULL) {4986assert((_collectorState == AbortablePreclean && should_abort_preclean()),4987"Should only be AbortablePreclean.");4988_ct->ct_bs()->invalidate(MemRegion(stop_point, dirtyRegion.end()));4989if (should_abort_preclean()) {4990break; // out of preclean loop4991} else {4992// Compute the next address at which preclean should pick up.4993lastAddr = next_card_start_after_block(stop_point);4994}4995}4996} else {4997break;4998}4999}5000verify_work_stacks_empty();5001verify_overflow_empty();5002return cumNumDirtyCards;5003}50045005class PrecleanKlassClosure : public KlassClosure {5006KlassToOopClosure _cm_klass_closure;5007public:5008PrecleanKlassClosure(OopClosure* oop_closure) : _cm_klass_closure(oop_closure) {}5009void do_klass(Klass* k) {5010if (k->has_accumulated_modified_oops()) {5011k->clear_accumulated_modified_oops();50125013_cm_klass_closure.do_klass(k);5014}5015}5016};50175018// The freelist lock is needed to prevent asserts, is it really needed?5019void CMSCollector::preclean_klasses(MarkRefsIntoAndScanClosure* cl, Mutex* freelistLock) {50205021cl->set_freelistLock(freelistLock);50225023CMSTokenSyncWithLocks ts(true, freelistLock, bitMapLock());50245025// SSS: Add equivalent to ScanMarkedObjectsAgainCarefullyClosure::do_yield_check and should_abort_preclean?5026// SSS: We should probably check if precleaning should be aborted, at suitable intervals?5027PrecleanKlassClosure preclean_klass_closure(cl);5028ClassLoaderDataGraph::classes_do(&preclean_klass_closure);50295030verify_work_stacks_empty();5031verify_overflow_empty();5032}50335034void CMSCollector::checkpointRootsFinal(bool asynch,5035bool clear_all_soft_refs, bool init_mark_was_synchronous) {5036assert(_collectorState == FinalMarking, "incorrect state transition?");5037check_correct_thread_executing();5038// world is stopped at this checkpoint5039assert(SafepointSynchronize::is_at_safepoint(),5040"world should be stopped");5041TraceCMSMemoryManagerStats tms(_collectorState,GenCollectedHeap::heap()->gc_cause());50425043verify_work_stacks_empty();5044verify_overflow_empty();50455046SpecializationStats::clear();5047if (PrintGCDetails) {5048gclog_or_tty->print("[YG occupancy: " SIZE_FORMAT " K (" SIZE_FORMAT " K)]",5049_young_gen->used() / K,5050_young_gen->capacity() / K);5051}5052if (asynch) {5053if (CMSScavengeBeforeRemark) {5054GenCollectedHeap* gch = GenCollectedHeap::heap();5055// Temporarily set flag to false, GCH->do_collection will5056// expect it to be false and set to true5057FlagSetting fl(gch->_is_gc_active, false);5058NOT_PRODUCT(GCTraceTime t("Scavenge-Before-Remark",5059PrintGCDetails && Verbose, true, _gc_timer_cm, _gc_tracer_cm->gc_id());)5060int level = _cmsGen->level() - 1;5061if (level >= 0) {5062gch->do_collection(true, // full (i.e. force, see below)5063false, // !clear_all_soft_refs50640, // size5065false, // is_tlab5066level // max_level5067);5068}5069}5070FreelistLocker x(this);5071MutexLockerEx y(bitMapLock(),5072Mutex::_no_safepoint_check_flag);5073assert(!init_mark_was_synchronous, "but that's impossible!");5074checkpointRootsFinalWork(asynch, clear_all_soft_refs, false);5075_cmsGen->cmsSpace()->recalculate_used_stable();5076} else {5077// already have all the locks5078checkpointRootsFinalWork(asynch, clear_all_soft_refs,5079init_mark_was_synchronous);5080_cmsGen->cmsSpace()->recalculate_used_stable();5081}5082verify_work_stacks_empty();5083verify_overflow_empty();5084SpecializationStats::print();5085}50865087void CMSCollector::checkpointRootsFinalWork(bool asynch,5088bool clear_all_soft_refs, bool init_mark_was_synchronous) {50895090NOT_PRODUCT(GCTraceTime tr("checkpointRootsFinalWork", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());)50915092assert(haveFreelistLocks(), "must have free list locks");5093assert_lock_strong(bitMapLock());50945095if (UseAdaptiveSizePolicy) {5096size_policy()->checkpoint_roots_final_begin();5097}50985099ResourceMark rm;5100HandleMark hm;51015102GenCollectedHeap* gch = GenCollectedHeap::heap();51035104if (should_unload_classes()) {5105CodeCache::gc_prologue();5106}5107assert(haveFreelistLocks(), "must have free list locks");5108assert_lock_strong(bitMapLock());51095110if (!init_mark_was_synchronous) {5111// We might assume that we need not fill TLAB's when5112// CMSScavengeBeforeRemark is set, because we may have just done5113// a scavenge which would have filled all TLAB's -- and besides5114// Eden would be empty. This however may not always be the case --5115// for instance although we asked for a scavenge, it may not have5116// happened because of a JNI critical section. We probably need5117// a policy for deciding whether we can in that case wait until5118// the critical section releases and then do the remark following5119// the scavenge, and skip it here. In the absence of that policy,5120// or of an indication of whether the scavenge did indeed occur,5121// we cannot rely on TLAB's having been filled and must do5122// so here just in case a scavenge did not happen.5123gch->ensure_parsability(false); // fill TLAB's, but no need to retire them5124// Update the saved marks which may affect the root scans.5125gch->save_marks();51265127if (CMSPrintEdenSurvivorChunks) {5128print_eden_and_survivor_chunk_arrays();5129}51305131{5132COMPILER2_PRESENT(DerivedPointerTableDeactivate dpt_deact;)51335134// Note on the role of the mod union table:5135// Since the marker in "markFromRoots" marks concurrently with5136// mutators, it is possible for some reachable objects not to have been5137// scanned. For instance, an only reference to an object A was5138// placed in object B after the marker scanned B. Unless B is rescanned,5139// A would be collected. Such updates to references in marked objects5140// are detected via the mod union table which is the set of all cards5141// dirtied since the first checkpoint in this GC cycle and prior to5142// the most recent young generation GC, minus those cleaned up by the5143// concurrent precleaning.5144if (CMSParallelRemarkEnabled && CollectedHeap::use_parallel_gc_threads()) {5145GCTraceTime t("Rescan (parallel) ", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());5146do_remark_parallel();5147} else {5148GCTraceTime t("Rescan (non-parallel) ", PrintGCDetails, false,5149_gc_timer_cm, _gc_tracer_cm->gc_id());5150do_remark_non_parallel();5151}5152}5153} else {5154assert(!asynch, "Can't have init_mark_was_synchronous in asynch mode");5155// The initial mark was stop-world, so there's no rescanning to5156// do; go straight on to the next step below.5157}5158verify_work_stacks_empty();5159verify_overflow_empty();51605161{5162NOT_PRODUCT(GCTraceTime ts("refProcessingWork", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());)5163refProcessingWork(asynch, clear_all_soft_refs);5164}5165verify_work_stacks_empty();5166verify_overflow_empty();51675168if (should_unload_classes()) {5169CodeCache::gc_epilogue();5170}5171JvmtiExport::gc_epilogue();51725173// If we encountered any (marking stack / work queue) overflow5174// events during the current CMS cycle, take appropriate5175// remedial measures, where possible, so as to try and avoid5176// recurrence of that condition.5177assert(_markStack.isEmpty(), "No grey objects");5178size_t ser_ovflw = _ser_pmc_remark_ovflw + _ser_pmc_preclean_ovflw +5179_ser_kac_ovflw + _ser_kac_preclean_ovflw;5180if (ser_ovflw > 0) {5181if (PrintCMSStatistics != 0) {5182gclog_or_tty->print_cr("Marking stack overflow (benign) "5183"(pmc_pc=" SIZE_FORMAT ", pmc_rm=" SIZE_FORMAT ", kac=" SIZE_FORMAT5184", kac_preclean=" SIZE_FORMAT ")",5185_ser_pmc_preclean_ovflw, _ser_pmc_remark_ovflw,5186_ser_kac_ovflw, _ser_kac_preclean_ovflw);5187}5188_markStack.expand();5189_ser_pmc_remark_ovflw = 0;5190_ser_pmc_preclean_ovflw = 0;5191_ser_kac_preclean_ovflw = 0;5192_ser_kac_ovflw = 0;5193}5194if (_par_pmc_remark_ovflw > 0 || _par_kac_ovflw > 0) {5195if (PrintCMSStatistics != 0) {5196gclog_or_tty->print_cr("Work queue overflow (benign) "5197"(pmc_rm=" SIZE_FORMAT ", kac=" SIZE_FORMAT ")",5198_par_pmc_remark_ovflw, _par_kac_ovflw);5199}5200_par_pmc_remark_ovflw = 0;5201_par_kac_ovflw = 0;5202}5203if (PrintCMSStatistics != 0) {5204if (_markStack._hit_limit > 0) {5205gclog_or_tty->print_cr(" (benign) Hit max stack size limit (" SIZE_FORMAT ")",5206_markStack._hit_limit);5207}5208if (_markStack._failed_double > 0) {5209gclog_or_tty->print_cr(" (benign) Failed stack doubling (" SIZE_FORMAT "),"5210" current capacity " SIZE_FORMAT,5211_markStack._failed_double,5212_markStack.capacity());5213}5214}5215_markStack._hit_limit = 0;5216_markStack._failed_double = 0;52175218if ((VerifyAfterGC || VerifyDuringGC) &&5219GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {5220verify_after_remark();5221}52225223_gc_tracer_cm->report_object_count_after_gc(&_is_alive_closure);52245225// Change under the freelistLocks.5226_collectorState = Sweeping;5227// Call isAllClear() under bitMapLock5228assert(_modUnionTable.isAllClear(),5229"Should be clear by end of the final marking");5230assert(_ct->klass_rem_set()->mod_union_is_clear(),5231"Should be clear by end of the final marking");5232if (UseAdaptiveSizePolicy) {5233size_policy()->checkpoint_roots_final_end(gch->gc_cause());5234}5235}52365237void CMSParInitialMarkTask::work(uint worker_id) {5238elapsedTimer _timer;5239ResourceMark rm;5240HandleMark hm;52415242// ---------- scan from roots --------------5243_timer.start();5244GenCollectedHeap* gch = GenCollectedHeap::heap();5245Par_MarkRefsIntoClosure par_mri_cl(_collector->_span, &(_collector->_markBitMap));52465247// ---------- young gen roots --------------5248{5249work_on_young_gen_roots(worker_id, &par_mri_cl);5250_timer.stop();5251if (PrintCMSStatistics != 0) {5252gclog_or_tty->print_cr(5253"Finished young gen initial mark scan work in %dth thread: %3.3f sec",5254worker_id, _timer.seconds());5255}5256}52575258// ---------- remaining roots --------------5259_timer.reset();5260_timer.start();52615262CLDToOopClosure cld_closure(&par_mri_cl, true);52635264gch->gen_process_roots(_collector->_cmsGen->level(),5265false, // yg was scanned above5266false, // this is parallel code5267GenCollectedHeap::ScanningOption(_collector->CMSCollector::roots_scanning_options()),5268_collector->should_unload_classes(),5269&par_mri_cl,5270NULL,5271&cld_closure);5272assert(_collector->should_unload_classes()5273|| (_collector->CMSCollector::roots_scanning_options() & GenCollectedHeap::SO_AllCodeCache),5274"if we didn't scan the code cache, we have to be ready to drop nmethods with expired weak oops");5275_timer.stop();5276if (PrintCMSStatistics != 0) {5277gclog_or_tty->print_cr(5278"Finished remaining root initial mark scan work in %dth thread: %3.3f sec",5279worker_id, _timer.seconds());5280}5281}52825283// Parallel remark task5284class CMSParRemarkTask: public CMSParMarkTask {5285CompactibleFreeListSpace* _cms_space;52865287// The per-thread work queues, available here for stealing.5288OopTaskQueueSet* _task_queues;5289ParallelTaskTerminator _term;52905291public:5292// A value of 0 passed to n_workers will cause the number of5293// workers to be taken from the active workers in the work gang.5294CMSParRemarkTask(CMSCollector* collector,5295CompactibleFreeListSpace* cms_space,5296int n_workers, FlexibleWorkGang* workers,5297OopTaskQueueSet* task_queues):5298CMSParMarkTask("Rescan roots and grey objects in parallel",5299collector, n_workers),5300_cms_space(cms_space),5301_task_queues(task_queues),5302_term(n_workers, task_queues) { }53035304OopTaskQueueSet* task_queues() { return _task_queues; }53055306OopTaskQueue* work_queue(int i) { return task_queues()->queue(i); }53075308ParallelTaskTerminator* terminator() { return &_term; }5309int n_workers() { return _n_workers; }53105311void work(uint worker_id);53125313private:5314// ... of dirty cards in old space5315void do_dirty_card_rescan_tasks(CompactibleFreeListSpace* sp, int i,5316Par_MarkRefsIntoAndScanClosure* cl);53175318// ... work stealing for the above5319void do_work_steal(int i, Par_MarkRefsIntoAndScanClosure* cl, int* seed);5320};53215322class RemarkKlassClosure : public KlassClosure {5323KlassToOopClosure _cm_klass_closure;5324public:5325RemarkKlassClosure(OopClosure* oop_closure) : _cm_klass_closure(oop_closure) {}5326void do_klass(Klass* k) {5327// Check if we have modified any oops in the Klass during the concurrent marking.5328if (k->has_accumulated_modified_oops()) {5329k->clear_accumulated_modified_oops();53305331// We could have transfered the current modified marks to the accumulated marks,5332// like we do with the Card Table to Mod Union Table. But it's not really necessary.5333} else if (k->has_modified_oops()) {5334// Don't clear anything, this info is needed by the next young collection.5335} else {5336// No modified oops in the Klass.5337return;5338}53395340// The klass has modified fields, need to scan the klass.5341_cm_klass_closure.do_klass(k);5342}5343};53445345void CMSParMarkTask::work_on_young_gen_roots(uint worker_id, OopsInGenClosure* cl) {5346DefNewGeneration* dng = _collector->_young_gen->as_DefNewGeneration();5347EdenSpace* eden_space = dng->eden();5348ContiguousSpace* from_space = dng->from();5349ContiguousSpace* to_space = dng->to();53505351HeapWord** eca = _collector->_eden_chunk_array;5352size_t ect = _collector->_eden_chunk_index;5353HeapWord** sca = _collector->_survivor_chunk_array;5354size_t sct = _collector->_survivor_chunk_index;53555356assert(ect <= _collector->_eden_chunk_capacity, "out of bounds");5357assert(sct <= _collector->_survivor_chunk_capacity, "out of bounds");53585359do_young_space_rescan(worker_id, cl, to_space, NULL, 0);5360do_young_space_rescan(worker_id, cl, from_space, sca, sct);5361do_young_space_rescan(worker_id, cl, eden_space, eca, ect);5362}53635364// work_queue(i) is passed to the closure5365// Par_MarkRefsIntoAndScanClosure. The "i" parameter5366// also is passed to do_dirty_card_rescan_tasks() and to5367// do_work_steal() to select the i-th task_queue.53685369void CMSParRemarkTask::work(uint worker_id) {5370elapsedTimer _timer;5371ResourceMark rm;5372HandleMark hm;53735374// ---------- rescan from roots --------------5375_timer.start();5376GenCollectedHeap* gch = GenCollectedHeap::heap();5377Par_MarkRefsIntoAndScanClosure par_mrias_cl(_collector,5378_collector->_span, _collector->ref_processor(),5379&(_collector->_markBitMap),5380work_queue(worker_id));53815382// Rescan young gen roots first since these are likely5383// coarsely partitioned and may, on that account, constitute5384// the critical path; thus, it's best to start off that5385// work first.5386// ---------- young gen roots --------------5387{5388work_on_young_gen_roots(worker_id, &par_mrias_cl);5389_timer.stop();5390if (PrintCMSStatistics != 0) {5391gclog_or_tty->print_cr(5392"Finished young gen rescan work in %dth thread: %3.3f sec",5393worker_id, _timer.seconds());5394}5395}53965397// ---------- remaining roots --------------5398_timer.reset();5399_timer.start();5400gch->gen_process_roots(_collector->_cmsGen->level(),5401false, // yg was scanned above5402false, // this is parallel code5403GenCollectedHeap::ScanningOption(_collector->CMSCollector::roots_scanning_options()),5404_collector->should_unload_classes(),5405&par_mrias_cl,5406NULL,5407NULL); // The dirty klasses will be handled below54085409assert(_collector->should_unload_classes()5410|| (_collector->CMSCollector::roots_scanning_options() & GenCollectedHeap::SO_AllCodeCache),5411"if we didn't scan the code cache, we have to be ready to drop nmethods with expired weak oops");5412_timer.stop();5413if (PrintCMSStatistics != 0) {5414gclog_or_tty->print_cr(5415"Finished remaining root rescan work in %dth thread: %3.3f sec",5416worker_id, _timer.seconds());5417}54185419// ---------- unhandled CLD scanning ----------5420if (worker_id == 0) { // Single threaded at the moment.5421_timer.reset();5422_timer.start();54235424// Scan all new class loader data objects and new dependencies that were5425// introduced during concurrent marking.5426ResourceMark rm;5427GrowableArray<ClassLoaderData*>* array = ClassLoaderDataGraph::new_clds();5428for (int i = 0; i < array->length(); i++) {5429par_mrias_cl.do_class_loader_data(array->at(i));5430}54315432// We don't need to keep track of new CLDs anymore.5433ClassLoaderDataGraph::remember_new_clds(false);54345435_timer.stop();5436if (PrintCMSStatistics != 0) {5437gclog_or_tty->print_cr(5438"Finished unhandled CLD scanning work in %dth thread: %3.3f sec",5439worker_id, _timer.seconds());5440}5441}54425443// ---------- dirty klass scanning ----------5444if (worker_id == 0) { // Single threaded at the moment.5445_timer.reset();5446_timer.start();54475448// Scan all classes that was dirtied during the concurrent marking phase.5449RemarkKlassClosure remark_klass_closure(&par_mrias_cl);5450ClassLoaderDataGraph::classes_do(&remark_klass_closure);54515452_timer.stop();5453if (PrintCMSStatistics != 0) {5454gclog_or_tty->print_cr(5455"Finished dirty klass scanning work in %dth thread: %3.3f sec",5456worker_id, _timer.seconds());5457}5458}54595460// We might have added oops to ClassLoaderData::_handles during the5461// concurrent marking phase. These oops point to newly allocated objects5462// that are guaranteed to be kept alive. Either by the direct allocation5463// code, or when the young collector processes the roots. Hence,5464// we don't have to revisit the _handles block during the remark phase.54655466// ---------- rescan dirty cards ------------5467_timer.reset();5468_timer.start();54695470// Do the rescan tasks for each of the two spaces5471// (cms_space) in turn.5472// "worker_id" is passed to select the task_queue for "worker_id"5473do_dirty_card_rescan_tasks(_cms_space, worker_id, &par_mrias_cl);5474_timer.stop();5475if (PrintCMSStatistics != 0) {5476gclog_or_tty->print_cr(5477"Finished dirty card rescan work in %dth thread: %3.3f sec",5478worker_id, _timer.seconds());5479}54805481// ---------- steal work from other threads ...5482// ---------- ... and drain overflow list.5483_timer.reset();5484_timer.start();5485do_work_steal(worker_id, &par_mrias_cl, _collector->hash_seed(worker_id));5486_timer.stop();5487if (PrintCMSStatistics != 0) {5488gclog_or_tty->print_cr(5489"Finished work stealing in %dth thread: %3.3f sec",5490worker_id, _timer.seconds());5491}5492}54935494// Note that parameter "i" is not used.5495void5496CMSParMarkTask::do_young_space_rescan(uint worker_id,5497OopsInGenClosure* cl, ContiguousSpace* space,5498HeapWord** chunk_array, size_t chunk_top) {5499// Until all tasks completed:5500// . claim an unclaimed task5501// . compute region boundaries corresponding to task claimed5502// using chunk_array5503// . par_oop_iterate(cl) over that region55045505ResourceMark rm;5506HandleMark hm;55075508SequentialSubTasksDone* pst = space->par_seq_tasks();55095510uint nth_task = 0;5511uint n_tasks = pst->n_tasks();55125513if (n_tasks > 0) {5514assert(pst->valid(), "Uninitialized use?");5515HeapWord *start, *end;5516while (!pst->is_task_claimed(/* reference */ nth_task)) {5517// We claimed task # nth_task; compute its boundaries.5518if (chunk_top == 0) { // no samples were taken5519assert(nth_task == 0 && n_tasks == 1, "Can have only 1 EdenSpace task");5520start = space->bottom();5521end = space->top();5522} else if (nth_task == 0) {5523start = space->bottom();5524end = chunk_array[nth_task];5525} else if (nth_task < (uint)chunk_top) {5526assert(nth_task >= 1, "Control point invariant");5527start = chunk_array[nth_task - 1];5528end = chunk_array[nth_task];5529} else {5530assert(nth_task == (uint)chunk_top, "Control point invariant");5531start = chunk_array[chunk_top - 1];5532end = space->top();5533}5534MemRegion mr(start, end);5535// Verify that mr is in space5536assert(mr.is_empty() || space->used_region().contains(mr),5537"Should be in space");5538// Verify that "start" is an object boundary5539assert(mr.is_empty() || oop(mr.start())->is_oop(),5540"Should be an oop");5541space->par_oop_iterate(mr, cl);5542}5543pst->all_tasks_completed();5544}5545}55465547void5548CMSParRemarkTask::do_dirty_card_rescan_tasks(5549CompactibleFreeListSpace* sp, int i,5550Par_MarkRefsIntoAndScanClosure* cl) {5551// Until all tasks completed:5552// . claim an unclaimed task5553// . compute region boundaries corresponding to task claimed5554// . transfer dirty bits ct->mut for that region5555// . apply rescanclosure to dirty mut bits for that region55565557ResourceMark rm;5558HandleMark hm;55595560OopTaskQueue* work_q = work_queue(i);5561ModUnionClosure modUnionClosure(&(_collector->_modUnionTable));5562// CAUTION! CAUTION! CAUTION! CAUTION! CAUTION! CAUTION! CAUTION!5563// CAUTION: This closure has state that persists across calls to5564// the work method dirty_range_iterate_clear() in that it has5565// imbedded in it a (subtype of) UpwardsObjectClosure. The5566// use of that state in the imbedded UpwardsObjectClosure instance5567// assumes that the cards are always iterated (even if in parallel5568// by several threads) in monotonically increasing order per each5569// thread. This is true of the implementation below which picks5570// card ranges (chunks) in monotonically increasing order globally5571// and, a-fortiori, in monotonically increasing order per thread5572// (the latter order being a subsequence of the former).5573// If the work code below is ever reorganized into a more chaotic5574// work-partitioning form than the current "sequential tasks"5575// paradigm, the use of that persistent state will have to be5576// revisited and modified appropriately. See also related5577// bug 4756801 work on which should examine this code to make5578// sure that the changes there do not run counter to the5579// assumptions made here and necessary for correctness and5580// efficiency. Note also that this code might yield inefficient5581// behaviour in the case of very large objects that span one or5582// more work chunks. Such objects would potentially be scanned5583// several times redundantly. Work on 4756801 should try and5584// address that performance anomaly if at all possible. XXX5585MemRegion full_span = _collector->_span;5586CMSBitMap* bm = &(_collector->_markBitMap); // shared5587MarkFromDirtyCardsClosure5588greyRescanClosure(_collector, full_span, // entire span of interest5589sp, bm, work_q, cl);55905591SequentialSubTasksDone* pst = sp->conc_par_seq_tasks();5592assert(pst->valid(), "Uninitialized use?");5593uint nth_task = 0;5594const int alignment = CardTableModRefBS::card_size * BitsPerWord;5595MemRegion span = sp->used_region();5596HeapWord* start_addr = span.start();5597HeapWord* end_addr = (HeapWord*)round_to((intptr_t)span.end(),5598alignment);5599const size_t chunk_size = sp->rescan_task_size(); // in HeapWord units5600assert((HeapWord*)round_to((intptr_t)start_addr, alignment) ==5601start_addr, "Check alignment");5602assert((size_t)round_to((intptr_t)chunk_size, alignment) ==5603chunk_size, "Check alignment");56045605while (!pst->is_task_claimed(/* reference */ nth_task)) {5606// Having claimed the nth_task, compute corresponding mem-region,5607// which is a-fortiori aligned correctly (i.e. at a MUT bopundary).5608// The alignment restriction ensures that we do not need any5609// synchronization with other gang-workers while setting or5610// clearing bits in thus chunk of the MUT.5611MemRegion this_span = MemRegion(start_addr + nth_task*chunk_size,5612start_addr + (nth_task+1)*chunk_size);5613// The last chunk's end might be way beyond end of the5614// used region. In that case pull back appropriately.5615if (this_span.end() > end_addr) {5616this_span.set_end(end_addr);5617assert(!this_span.is_empty(), "Program logic (calculation of n_tasks)");5618}5619// Iterate over the dirty cards covering this chunk, marking them5620// precleaned, and setting the corresponding bits in the mod union5621// table. Since we have been careful to partition at Card and MUT-word5622// boundaries no synchronization is needed between parallel threads.5623_collector->_ct->ct_bs()->dirty_card_iterate(this_span,5624&modUnionClosure);56255626// Having transferred these marks into the modUnionTable,5627// rescan the marked objects on the dirty cards in the modUnionTable.5628// Even if this is at a synchronous collection, the initial marking5629// may have been done during an asynchronous collection so there5630// may be dirty bits in the mod-union table.5631_collector->_modUnionTable.dirty_range_iterate_clear(5632this_span, &greyRescanClosure);5633_collector->_modUnionTable.verifyNoOneBitsInRange(5634this_span.start(),5635this_span.end());5636}5637pst->all_tasks_completed(); // declare that i am done5638}56395640// . see if we can share work_queues with ParNew? XXX5641void5642CMSParRemarkTask::do_work_steal(int i, Par_MarkRefsIntoAndScanClosure* cl,5643int* seed) {5644OopTaskQueue* work_q = work_queue(i);5645NOT_PRODUCT(int num_steals = 0;)5646oop obj_to_scan;5647CMSBitMap* bm = &(_collector->_markBitMap);56485649while (true) {5650// Completely finish any left over work from (an) earlier round(s)5651cl->trim_queue(0);5652size_t num_from_overflow_list = MIN2((size_t)(work_q->max_elems() - work_q->size())/4,5653(size_t)ParGCDesiredObjsFromOverflowList);5654// Now check if there's any work in the overflow list5655// Passing ParallelGCThreads as the third parameter, no_of_gc_threads,5656// only affects the number of attempts made to get work from the5657// overflow list and does not affect the number of workers. Just5658// pass ParallelGCThreads so this behavior is unchanged.5659if (_collector->par_take_from_overflow_list(num_from_overflow_list,5660work_q,5661ParallelGCThreads)) {5662// found something in global overflow list;5663// not yet ready to go stealing work from others.5664// We'd like to assert(work_q->size() != 0, ...)5665// because we just took work from the overflow list,5666// but of course we can't since all of that could have5667// been already stolen from us.5668// "He giveth and He taketh away."5669continue;5670}5671// Verify that we have no work before we resort to stealing5672assert(work_q->size() == 0, "Have work, shouldn't steal");5673// Try to steal from other queues that have work5674if (task_queues()->steal(i, seed, /* reference */ obj_to_scan)) {5675NOT_PRODUCT(num_steals++;)5676assert(obj_to_scan->is_oop(), "Oops, not an oop!");5677assert(bm->isMarked((HeapWord*)obj_to_scan), "Stole an unmarked oop?");5678// Do scanning work5679obj_to_scan->oop_iterate(cl);5680// Loop around, finish this work, and try to steal some more5681} else if (terminator()->offer_termination()) {5682break; // nirvana from the infinite cycle5683}5684}5685NOT_PRODUCT(5686if (PrintCMSStatistics != 0) {5687gclog_or_tty->print("\n\t(%d: stole %d oops)", i, num_steals);5688}5689)5690assert(work_q->size() == 0 && _collector->overflow_list_is_empty(),5691"Else our work is not yet done");5692}56935694// Record object boundaries in _eden_chunk_array by sampling the eden5695// top in the slow-path eden object allocation code path and record5696// the boundaries, if CMSEdenChunksRecordAlways is true. If5697// CMSEdenChunksRecordAlways is false, we use the other asynchronous5698// sampling in sample_eden() that activates during the part of the5699// preclean phase.5700void CMSCollector::sample_eden_chunk() {5701if (CMSEdenChunksRecordAlways && _eden_chunk_array != NULL) {5702if (_eden_chunk_lock->try_lock()) {5703// Record a sample. This is the critical section. The contents5704// of the _eden_chunk_array have to be non-decreasing in the5705// address order.5706_eden_chunk_array[_eden_chunk_index] = *_top_addr;5707assert(_eden_chunk_array[_eden_chunk_index] <= *_end_addr,5708"Unexpected state of Eden");5709if (_eden_chunk_index == 0 ||5710((_eden_chunk_array[_eden_chunk_index] > _eden_chunk_array[_eden_chunk_index-1]) &&5711(pointer_delta(_eden_chunk_array[_eden_chunk_index],5712_eden_chunk_array[_eden_chunk_index-1]) >= CMSSamplingGrain))) {5713_eden_chunk_index++; // commit sample5714}5715_eden_chunk_lock->unlock();5716}5717}5718}57195720// Return a thread-local PLAB recording array, as appropriate.5721void* CMSCollector::get_data_recorder(int thr_num) {5722if (_survivor_plab_array != NULL &&5723(CMSPLABRecordAlways ||5724(_collectorState > Marking && _collectorState < FinalMarking))) {5725assert(thr_num < (int)ParallelGCThreads, "thr_num is out of bounds");5726ChunkArray* ca = &_survivor_plab_array[thr_num];5727ca->reset(); // clear it so that fresh data is recorded5728return (void*) ca;5729} else {5730return NULL;5731}5732}57335734// Reset all the thread-local PLAB recording arrays5735void CMSCollector::reset_survivor_plab_arrays() {5736for (uint i = 0; i < ParallelGCThreads; i++) {5737_survivor_plab_array[i].reset();5738}5739}57405741// Merge the per-thread plab arrays into the global survivor chunk5742// array which will provide the partitioning of the survivor space5743// for CMS initial scan and rescan.5744void CMSCollector::merge_survivor_plab_arrays(ContiguousSpace* surv,5745int no_of_gc_threads) {5746assert(_survivor_plab_array != NULL, "Error");5747assert(_survivor_chunk_array != NULL, "Error");5748assert(_collectorState == FinalMarking ||5749(CMSParallelInitialMarkEnabled && _collectorState == InitialMarking), "Error");5750for (int j = 0; j < no_of_gc_threads; j++) {5751_cursor[j] = 0;5752}5753HeapWord* top = surv->top();5754size_t i;5755for (i = 0; i < _survivor_chunk_capacity; i++) { // all sca entries5756HeapWord* min_val = top; // Higher than any PLAB address5757uint min_tid = 0; // position of min_val this round5758for (int j = 0; j < no_of_gc_threads; j++) {5759ChunkArray* cur_sca = &_survivor_plab_array[j];5760if (_cursor[j] == cur_sca->end()) {5761continue;5762}5763assert(_cursor[j] < cur_sca->end(), "ctl pt invariant");5764HeapWord* cur_val = cur_sca->nth(_cursor[j]);5765assert(surv->used_region().contains(cur_val), "Out of bounds value");5766if (cur_val < min_val) {5767min_tid = j;5768min_val = cur_val;5769} else {5770assert(cur_val < top, "All recorded addresses should be less");5771}5772}5773// At this point min_val and min_tid are respectively5774// the least address in _survivor_plab_array[j]->nth(_cursor[j])5775// and the thread (j) that witnesses that address.5776// We record this address in the _survivor_chunk_array[i]5777// and increment _cursor[min_tid] prior to the next round i.5778if (min_val == top) {5779break;5780}5781_survivor_chunk_array[i] = min_val;5782_cursor[min_tid]++;5783}5784// We are all done; record the size of the _survivor_chunk_array5785_survivor_chunk_index = i; // exclusive: [0, i)5786if (PrintCMSStatistics > 0) {5787gclog_or_tty->print(" (Survivor:" SIZE_FORMAT "chunks) ", i);5788}5789// Verify that we used up all the recorded entries5790#ifdef ASSERT5791size_t total = 0;5792for (int j = 0; j < no_of_gc_threads; j++) {5793assert(_cursor[j] == _survivor_plab_array[j].end(), "Ctl pt invariant");5794total += _cursor[j];5795}5796assert(total == _survivor_chunk_index, "Ctl Pt Invariant");5797// Check that the merged array is in sorted order5798if (total > 0) {5799for (size_t i = 0; i < total - 1; i++) {5800if (PrintCMSStatistics > 0) {5801gclog_or_tty->print(" (chunk" SIZE_FORMAT ":" INTPTR_FORMAT ") ",5802i, _survivor_chunk_array[i]);5803}5804assert(_survivor_chunk_array[i] < _survivor_chunk_array[i+1],5805"Not sorted");5806}5807}5808#endif // ASSERT5809}58105811// Set up the space's par_seq_tasks structure for work claiming5812// for parallel initial scan and rescan of young gen.5813// See ParRescanTask where this is currently used.5814void5815CMSCollector::5816initialize_sequential_subtasks_for_young_gen_rescan(int n_threads) {5817assert(n_threads > 0, "Unexpected n_threads argument");5818DefNewGeneration* dng = (DefNewGeneration*)_young_gen;58195820// Eden space5821if (!dng->eden()->is_empty()) {5822SequentialSubTasksDone* pst = dng->eden()->par_seq_tasks();5823assert(!pst->valid(), "Clobbering existing data?");5824// Each valid entry in [0, _eden_chunk_index) represents a task.5825size_t n_tasks = _eden_chunk_index + 1;5826assert(n_tasks == 1 || _eden_chunk_array != NULL, "Error");5827// Sets the condition for completion of the subtask (how many threads5828// need to finish in order to be done).5829pst->set_n_threads(n_threads);5830pst->set_n_tasks((int)n_tasks);5831}58325833// Merge the survivor plab arrays into _survivor_chunk_array5834if (_survivor_plab_array != NULL) {5835merge_survivor_plab_arrays(dng->from(), n_threads);5836} else {5837assert(_survivor_chunk_index == 0, "Error");5838}58395840// To space5841{5842SequentialSubTasksDone* pst = dng->to()->par_seq_tasks();5843assert(!pst->valid(), "Clobbering existing data?");5844// Sets the condition for completion of the subtask (how many threads5845// need to finish in order to be done).5846pst->set_n_threads(n_threads);5847pst->set_n_tasks(1);5848assert(pst->valid(), "Error");5849}58505851// From space5852{5853SequentialSubTasksDone* pst = dng->from()->par_seq_tasks();5854assert(!pst->valid(), "Clobbering existing data?");5855size_t n_tasks = _survivor_chunk_index + 1;5856assert(n_tasks == 1 || _survivor_chunk_array != NULL, "Error");5857// Sets the condition for completion of the subtask (how many threads5858// need to finish in order to be done).5859pst->set_n_threads(n_threads);5860pst->set_n_tasks((int)n_tasks);5861assert(pst->valid(), "Error");5862}5863}58645865// Parallel version of remark5866void CMSCollector::do_remark_parallel() {5867GenCollectedHeap* gch = GenCollectedHeap::heap();5868FlexibleWorkGang* workers = gch->workers();5869assert(workers != NULL, "Need parallel worker threads.");5870// Choose to use the number of GC workers most recently set5871// into "active_workers". If active_workers is not set, set it5872// to ParallelGCThreads.5873int n_workers = workers->active_workers();5874if (n_workers == 0) {5875assert(n_workers > 0, "Should have been set during scavenge");5876n_workers = ParallelGCThreads;5877workers->set_active_workers(n_workers);5878}5879CompactibleFreeListSpace* cms_space = _cmsGen->cmsSpace();58805881CMSParRemarkTask tsk(this,5882cms_space,5883n_workers, workers, task_queues());58845885// Set up for parallel process_roots work.5886gch->set_par_threads(n_workers);5887// We won't be iterating over the cards in the card table updating5888// the younger_gen cards, so we shouldn't call the following else5889// the verification code as well as subsequent younger_refs_iterate5890// code would get confused. XXX5891// gch->rem_set()->prepare_for_younger_refs_iterate(true); // parallel58925893// The young gen rescan work will not be done as part of5894// process_roots (which currently doesn't know how to5895// parallelize such a scan), but rather will be broken up into5896// a set of parallel tasks (via the sampling that the [abortable]5897// preclean phase did of EdenSpace, plus the [two] tasks of5898// scanning the [two] survivor spaces. Further fine-grain5899// parallelization of the scanning of the survivor spaces5900// themselves, and of precleaning of the younger gen itself5901// is deferred to the future.5902initialize_sequential_subtasks_for_young_gen_rescan(n_workers);59035904// The dirty card rescan work is broken up into a "sequence"5905// of parallel tasks (per constituent space) that are dynamically5906// claimed by the parallel threads.5907cms_space->initialize_sequential_subtasks_for_rescan(n_workers);59085909// It turns out that even when we're using 1 thread, doing the work in a5910// separate thread causes wide variance in run times. We can't help this5911// in the multi-threaded case, but we special-case n=1 here to get5912// repeatable measurements of the 1-thread overhead of the parallel code.5913if (n_workers > 1) {5914// Make refs discovery MT-safe, if it isn't already: it may not5915// necessarily be so, since it's possible that we are doing5916// ST marking.5917ReferenceProcessorMTDiscoveryMutator mt(ref_processor(), true);5918GenCollectedHeap::StrongRootsScope srs(gch);5919workers->run_task(&tsk);5920} else {5921ReferenceProcessorMTDiscoveryMutator mt(ref_processor(), false);5922GenCollectedHeap::StrongRootsScope srs(gch);5923tsk.work(0);5924}59255926gch->set_par_threads(0); // 0 ==> non-parallel.5927// restore, single-threaded for now, any preserved marks5928// as a result of work_q overflow5929restore_preserved_marks_if_any();5930}59315932// Non-parallel version of remark5933void CMSCollector::do_remark_non_parallel() {5934ResourceMark rm;5935HandleMark hm;5936GenCollectedHeap* gch = GenCollectedHeap::heap();5937ReferenceProcessorMTDiscoveryMutator mt(ref_processor(), false);59385939MarkRefsIntoAndScanClosure5940mrias_cl(_span, ref_processor(), &_markBitMap, NULL /* not precleaning */,5941&_markStack, this,5942false /* should_yield */, false /* not precleaning */);5943MarkFromDirtyCardsClosure5944markFromDirtyCardsClosure(this, _span,5945NULL, // space is set further below5946&_markBitMap, &_markStack, &mrias_cl);5947{5948GCTraceTime t("grey object rescan", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());5949// Iterate over the dirty cards, setting the corresponding bits in the5950// mod union table.5951{5952ModUnionClosure modUnionClosure(&_modUnionTable);5953_ct->ct_bs()->dirty_card_iterate(5954_cmsGen->used_region(),5955&modUnionClosure);5956}5957// Having transferred these marks into the modUnionTable, we just need5958// to rescan the marked objects on the dirty cards in the modUnionTable.5959// The initial marking may have been done during an asynchronous5960// collection so there may be dirty bits in the mod-union table.5961const int alignment =5962CardTableModRefBS::card_size * BitsPerWord;5963{5964// ... First handle dirty cards in CMS gen5965markFromDirtyCardsClosure.set_space(_cmsGen->cmsSpace());5966MemRegion ur = _cmsGen->used_region();5967HeapWord* lb = ur.start();5968HeapWord* ub = (HeapWord*)round_to((intptr_t)ur.end(), alignment);5969MemRegion cms_span(lb, ub);5970_modUnionTable.dirty_range_iterate_clear(cms_span,5971&markFromDirtyCardsClosure);5972verify_work_stacks_empty();5973if (PrintCMSStatistics != 0) {5974gclog_or_tty->print(" (re-scanned " SIZE_FORMAT " dirty cards in cms gen) ",5975markFromDirtyCardsClosure.num_dirty_cards());5976}5977}5978}5979if (VerifyDuringGC &&5980GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {5981HandleMark hm; // Discard invalid handles created during verification5982Universe::verify();5983}5984{5985GCTraceTime t("root rescan", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());59865987verify_work_stacks_empty();59885989gch->rem_set()->prepare_for_younger_refs_iterate(false); // Not parallel.5990GenCollectedHeap::StrongRootsScope srs(gch);59915992gch->gen_process_roots(_cmsGen->level(),5993true, // younger gens as roots5994false, // use the local StrongRootsScope5995GenCollectedHeap::ScanningOption(roots_scanning_options()),5996should_unload_classes(),5997&mrias_cl,5998NULL,5999NULL); // The dirty klasses will be handled below60006001assert(should_unload_classes()6002|| (roots_scanning_options() & GenCollectedHeap::SO_AllCodeCache),6003"if we didn't scan the code cache, we have to be ready to drop nmethods with expired weak oops");6004}60056006{6007GCTraceTime t("visit unhandled CLDs", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());60086009verify_work_stacks_empty();60106011// Scan all class loader data objects that might have been introduced6012// during concurrent marking.6013ResourceMark rm;6014GrowableArray<ClassLoaderData*>* array = ClassLoaderDataGraph::new_clds();6015for (int i = 0; i < array->length(); i++) {6016mrias_cl.do_class_loader_data(array->at(i));6017}60186019// We don't need to keep track of new CLDs anymore.6020ClassLoaderDataGraph::remember_new_clds(false);60216022verify_work_stacks_empty();6023}60246025{6026GCTraceTime t("dirty klass scan", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());60276028verify_work_stacks_empty();60296030RemarkKlassClosure remark_klass_closure(&mrias_cl);6031ClassLoaderDataGraph::classes_do(&remark_klass_closure);60326033verify_work_stacks_empty();6034}60356036// We might have added oops to ClassLoaderData::_handles during the6037// concurrent marking phase. These oops point to newly allocated objects6038// that are guaranteed to be kept alive. Either by the direct allocation6039// code, or when the young collector processes the roots. Hence,6040// we don't have to revisit the _handles block during the remark phase.60416042verify_work_stacks_empty();6043// Restore evacuated mark words, if any, used for overflow list links6044if (!CMSOverflowEarlyRestoration) {6045restore_preserved_marks_if_any();6046}6047verify_overflow_empty();6048}60496050////////////////////////////////////////////////////////6051// Parallel Reference Processing Task Proxy Class6052////////////////////////////////////////////////////////6053class CMSRefProcTaskProxy: public AbstractGangTaskWOopQueues {6054typedef AbstractRefProcTaskExecutor::ProcessTask ProcessTask;6055CMSCollector* _collector;6056CMSBitMap* _mark_bit_map;6057const MemRegion _span;6058ProcessTask& _task;60596060public:6061CMSRefProcTaskProxy(ProcessTask& task,6062CMSCollector* collector,6063const MemRegion& span,6064CMSBitMap* mark_bit_map,6065AbstractWorkGang* workers,6066OopTaskQueueSet* task_queues):6067// XXX Should superclass AGTWOQ also know about AWG since it knows6068// about the task_queues used by the AWG? Then it could initialize6069// the terminator() object. See 6984287. The set_for_termination()6070// below is a temporary band-aid for the regression in 6984287.6071AbstractGangTaskWOopQueues("Process referents by policy in parallel",6072task_queues),6073_task(task),6074_collector(collector), _span(span), _mark_bit_map(mark_bit_map)6075{6076assert(_collector->_span.equals(_span) && !_span.is_empty(),6077"Inconsistency in _span");6078set_for_termination(workers->active_workers());6079}60806081OopTaskQueueSet* task_queues() { return queues(); }60826083OopTaskQueue* work_queue(int i) { return task_queues()->queue(i); }60846085void do_work_steal(int i,6086CMSParDrainMarkingStackClosure* drain,6087CMSParKeepAliveClosure* keep_alive,6088int* seed);60896090virtual void work(uint worker_id);6091};60926093void CMSRefProcTaskProxy::work(uint worker_id) {6094ResourceMark rm;6095HandleMark hm;6096assert(_collector->_span.equals(_span), "Inconsistency in _span");6097CMSParKeepAliveClosure par_keep_alive(_collector, _span,6098_mark_bit_map,6099work_queue(worker_id));6100CMSParDrainMarkingStackClosure par_drain_stack(_collector, _span,6101_mark_bit_map,6102work_queue(worker_id));6103CMSIsAliveClosure is_alive_closure(_span, _mark_bit_map);6104_task.work(worker_id, is_alive_closure, par_keep_alive, par_drain_stack);6105if (_task.marks_oops_alive()) {6106do_work_steal(worker_id, &par_drain_stack, &par_keep_alive,6107_collector->hash_seed(worker_id));6108}6109assert(work_queue(worker_id)->size() == 0, "work_queue should be empty");6110assert(_collector->_overflow_list == NULL, "non-empty _overflow_list");6111}61126113class CMSRefEnqueueTaskProxy: public AbstractGangTask {6114typedef AbstractRefProcTaskExecutor::EnqueueTask EnqueueTask;6115EnqueueTask& _task;61166117public:6118CMSRefEnqueueTaskProxy(EnqueueTask& task)6119: AbstractGangTask("Enqueue reference objects in parallel"),6120_task(task)6121{ }61226123virtual void work(uint worker_id)6124{6125_task.work(worker_id);6126}6127};61286129CMSParKeepAliveClosure::CMSParKeepAliveClosure(CMSCollector* collector,6130MemRegion span, CMSBitMap* bit_map, OopTaskQueue* work_queue):6131_span(span),6132_bit_map(bit_map),6133_work_queue(work_queue),6134_mark_and_push(collector, span, bit_map, work_queue),6135_low_water_mark(MIN2((uint)(work_queue->max_elems()/4),6136(uint)(CMSWorkQueueDrainThreshold * ParallelGCThreads)))6137{ }61386139// . see if we can share work_queues with ParNew? XXX6140void CMSRefProcTaskProxy::do_work_steal(int i,6141CMSParDrainMarkingStackClosure* drain,6142CMSParKeepAliveClosure* keep_alive,6143int* seed) {6144OopTaskQueue* work_q = work_queue(i);6145NOT_PRODUCT(int num_steals = 0;)6146oop obj_to_scan;61476148while (true) {6149// Completely finish any left over work from (an) earlier round(s)6150drain->trim_queue(0);6151size_t num_from_overflow_list = MIN2((size_t)(work_q->max_elems() - work_q->size())/4,6152(size_t)ParGCDesiredObjsFromOverflowList);6153// Now check if there's any work in the overflow list6154// Passing ParallelGCThreads as the third parameter, no_of_gc_threads,6155// only affects the number of attempts made to get work from the6156// overflow list and does not affect the number of workers. Just6157// pass ParallelGCThreads so this behavior is unchanged.6158if (_collector->par_take_from_overflow_list(num_from_overflow_list,6159work_q,6160ParallelGCThreads)) {6161// Found something in global overflow list;6162// not yet ready to go stealing work from others.6163// We'd like to assert(work_q->size() != 0, ...)6164// because we just took work from the overflow list,6165// but of course we can't, since all of that might have6166// been already stolen from us.6167continue;6168}6169// Verify that we have no work before we resort to stealing6170assert(work_q->size() == 0, "Have work, shouldn't steal");6171// Try to steal from other queues that have work6172if (task_queues()->steal(i, seed, /* reference */ obj_to_scan)) {6173NOT_PRODUCT(num_steals++;)6174assert(obj_to_scan->is_oop(), "Oops, not an oop!");6175assert(_mark_bit_map->isMarked((HeapWord*)obj_to_scan), "Stole an unmarked oop?");6176// Do scanning work6177obj_to_scan->oop_iterate(keep_alive);6178// Loop around, finish this work, and try to steal some more6179} else if (terminator()->offer_termination()) {6180break; // nirvana from the infinite cycle6181}6182}6183NOT_PRODUCT(6184if (PrintCMSStatistics != 0) {6185gclog_or_tty->print("\n\t(%d: stole %d oops)", i, num_steals);6186}6187)6188}61896190void CMSRefProcTaskExecutor::execute(ProcessTask& task)6191{6192GenCollectedHeap* gch = GenCollectedHeap::heap();6193FlexibleWorkGang* workers = gch->workers();6194assert(workers != NULL, "Need parallel worker threads.");6195CMSRefProcTaskProxy rp_task(task, &_collector,6196_collector.ref_processor()->span(),6197_collector.markBitMap(),6198workers, _collector.task_queues());6199workers->run_task(&rp_task);6200}62016202void CMSRefProcTaskExecutor::execute(EnqueueTask& task)6203{62046205GenCollectedHeap* gch = GenCollectedHeap::heap();6206FlexibleWorkGang* workers = gch->workers();6207assert(workers != NULL, "Need parallel worker threads.");6208CMSRefEnqueueTaskProxy enq_task(task);6209workers->run_task(&enq_task);6210}62116212void CMSCollector::refProcessingWork(bool asynch, bool clear_all_soft_refs) {62136214ResourceMark rm;6215HandleMark hm;62166217ReferenceProcessor* rp = ref_processor();6218assert(rp->span().equals(_span), "Spans should be equal");6219assert(!rp->enqueuing_is_done(), "Enqueuing should not be complete");6220// Process weak references.6221rp->setup_policy(clear_all_soft_refs);6222verify_work_stacks_empty();62236224CMSKeepAliveClosure cmsKeepAliveClosure(this, _span, &_markBitMap,6225&_markStack, false /* !preclean */);6226CMSDrainMarkingStackClosure cmsDrainMarkingStackClosure(this,6227_span, &_markBitMap, &_markStack,6228&cmsKeepAliveClosure, false /* !preclean */);6229{6230GCTraceTime t("weak refs processing", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());62316232ReferenceProcessorStats stats;6233if (rp->processing_is_mt()) {6234// Set the degree of MT here. If the discovery is done MT, there6235// may have been a different number of threads doing the discovery6236// and a different number of discovered lists may have Ref objects.6237// That is OK as long as the Reference lists are balanced (see6238// balance_all_queues() and balance_queues()).6239GenCollectedHeap* gch = GenCollectedHeap::heap();6240int active_workers = ParallelGCThreads;6241FlexibleWorkGang* workers = gch->workers();6242if (workers != NULL) {6243active_workers = workers->active_workers();6244// The expectation is that active_workers will have already6245// been set to a reasonable value. If it has not been set,6246// investigate.6247assert(active_workers > 0, "Should have been set during scavenge");6248}6249rp->set_active_mt_degree(active_workers);6250CMSRefProcTaskExecutor task_executor(*this);6251stats = rp->process_discovered_references(&_is_alive_closure,6252&cmsKeepAliveClosure,6253&cmsDrainMarkingStackClosure,6254&task_executor,6255_gc_timer_cm,6256_gc_tracer_cm->gc_id());6257} else {6258stats = rp->process_discovered_references(&_is_alive_closure,6259&cmsKeepAliveClosure,6260&cmsDrainMarkingStackClosure,6261NULL,6262_gc_timer_cm,6263_gc_tracer_cm->gc_id());6264}6265_gc_tracer_cm->report_gc_reference_stats(stats);62666267}62686269// This is the point where the entire marking should have completed.6270verify_work_stacks_empty();62716272if (should_unload_classes()) {6273{6274GCTraceTime t("class unloading", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());62756276// Unload classes and purge the SystemDictionary.6277bool purged_class = SystemDictionary::do_unloading(&_is_alive_closure);62786279// Unload nmethods.6280CodeCache::do_unloading(&_is_alive_closure, purged_class);62816282// Prune dead klasses from subklass/sibling/implementor lists.6283Klass::clean_weak_klass_links(&_is_alive_closure);6284}62856286{6287GCTraceTime t("scrub symbol table", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());6288// Clean up unreferenced symbols in symbol table.6289SymbolTable::unlink();6290}62916292{6293GCTraceTime t("scrub string table", PrintGCDetails, false, _gc_timer_cm, _gc_tracer_cm->gc_id());6294// Delete entries for dead interned strings.6295StringTable::unlink(&_is_alive_closure);6296}6297}629862996300// Restore any preserved marks as a result of mark stack or6301// work queue overflow6302restore_preserved_marks_if_any(); // done single-threaded for now63036304rp->set_enqueuing_is_done(true);6305if (rp->processing_is_mt()) {6306rp->balance_all_queues();6307CMSRefProcTaskExecutor task_executor(*this);6308rp->enqueue_discovered_references(&task_executor);6309} else {6310rp->enqueue_discovered_references(NULL);6311}6312rp->verify_no_references_recorded();6313assert(!rp->discovery_enabled(), "should have been disabled");6314}63156316#ifndef PRODUCT6317void CMSCollector::check_correct_thread_executing() {6318Thread* t = Thread::current();6319// Only the VM thread or the CMS thread should be here.6320assert(t->is_ConcurrentGC_thread() || t->is_VM_thread(),6321"Unexpected thread type");6322// If this is the vm thread, the foreground process6323// should not be waiting. Note that _foregroundGCIsActive is6324// true while the foreground collector is waiting.6325if (_foregroundGCShouldWait) {6326// We cannot be the VM thread6327assert(t->is_ConcurrentGC_thread(),6328"Should be CMS thread");6329} else {6330// We can be the CMS thread only if we are in a stop-world6331// phase of CMS collection.6332if (t->is_ConcurrentGC_thread()) {6333assert(_collectorState == InitialMarking ||6334_collectorState == FinalMarking,6335"Should be a stop-world phase");6336// The CMS thread should be holding the CMS_token.6337assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),6338"Potential interference with concurrently "6339"executing VM thread");6340}6341}6342}6343#endif63446345void CMSCollector::sweep(bool asynch) {6346assert(_collectorState == Sweeping, "just checking");6347check_correct_thread_executing();6348verify_work_stacks_empty();6349verify_overflow_empty();6350increment_sweep_count();6351TraceCMSMemoryManagerStats tms(_collectorState,GenCollectedHeap::heap()->gc_cause());63526353_inter_sweep_timer.stop();6354_inter_sweep_estimate.sample(_inter_sweep_timer.seconds());6355size_policy()->avg_cms_free_at_sweep()->sample(_cmsGen->free());63566357assert(!_intra_sweep_timer.is_active(), "Should not be active");6358_intra_sweep_timer.reset();6359_intra_sweep_timer.start();6360if (asynch) {6361TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);6362CMSPhaseAccounting pa(this, "sweep", _gc_tracer_cm->gc_id(), !PrintGCDetails);6363// First sweep the old gen6364{6365CMSTokenSyncWithLocks ts(true, _cmsGen->freelistLock(),6366bitMapLock());6367sweepWork(_cmsGen, asynch);6368}63696370// Update Universe::_heap_*_at_gc figures.6371// We need all the free list locks to make the abstract state6372// transition from Sweeping to Resetting. See detailed note6373// further below.6374{6375CMSTokenSyncWithLocks ts(true, _cmsGen->freelistLock());6376// Update heap occupancy information which is used as6377// input to soft ref clearing policy at the next gc.6378Universe::update_heap_info_at_gc();63796380// recalculate CMS used space after CMS collection6381_cmsGen->cmsSpace()->recalculate_used_stable();63826383_collectorState = Resizing;6384}6385} else {6386// already have needed locks6387sweepWork(_cmsGen, asynch);6388// Update heap occupancy information which is used as6389// input to soft ref clearing policy at the next gc.6390Universe::update_heap_info_at_gc();6391_collectorState = Resizing;6392}6393verify_work_stacks_empty();6394verify_overflow_empty();63956396if (should_unload_classes()) {6397// Delay purge to the beginning of the next safepoint. Metaspace::contains6398// requires that the virtual spaces are stable and not deleted.6399ClassLoaderDataGraph::set_should_purge(true);6400}64016402_intra_sweep_timer.stop();6403_intra_sweep_estimate.sample(_intra_sweep_timer.seconds());64046405_inter_sweep_timer.reset();6406_inter_sweep_timer.start();64076408// We need to use a monotonically non-deccreasing time in ms6409// or we will see time-warp warnings and os::javaTimeMillis()6410// does not guarantee monotonicity.6411jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;6412update_time_of_last_gc(now);64136414// NOTE on abstract state transitions:6415// Mutators allocate-live and/or mark the mod-union table dirty6416// based on the state of the collection. The former is done in6417// the interval [Marking, Sweeping] and the latter in the interval6418// [Marking, Sweeping). Thus the transitions into the Marking state6419// and out of the Sweeping state must be synchronously visible6420// globally to the mutators.6421// The transition into the Marking state happens with the world6422// stopped so the mutators will globally see it. Sweeping is6423// done asynchronously by the background collector so the transition6424// from the Sweeping state to the Resizing state must be done6425// under the freelistLock (as is the check for whether to6426// allocate-live and whether to dirty the mod-union table).6427assert(_collectorState == Resizing, "Change of collector state to"6428" Resizing must be done under the freelistLocks (plural)");64296430// Now that sweeping has been completed, we clear6431// the incremental_collection_failed flag,6432// thus inviting a younger gen collection to promote into6433// this generation. If such a promotion may still fail,6434// the flag will be set again when a young collection is6435// attempted.6436GenCollectedHeap* gch = GenCollectedHeap::heap();6437gch->clear_incremental_collection_failed(); // Worth retrying as fresh space may have been freed up6438gch->update_full_collections_completed(_collection_count_start);6439}64406441// FIX ME!!! Looks like this belongs in CFLSpace, with6442// CMSGen merely delegating to it.6443void ConcurrentMarkSweepGeneration::setNearLargestChunk() {6444double nearLargestPercent = FLSLargestBlockCoalesceProximity;6445HeapWord* minAddr = _cmsSpace->bottom();6446HeapWord* largestAddr =6447(HeapWord*) _cmsSpace->dictionary()->find_largest_dict();6448if (largestAddr == NULL) {6449// The dictionary appears to be empty. In this case6450// try to coalesce at the end of the heap.6451largestAddr = _cmsSpace->end();6452}6453size_t largestOffset = pointer_delta(largestAddr, minAddr);6454size_t nearLargestOffset =6455(size_t)((double)largestOffset * nearLargestPercent) - MinChunkSize;6456if (PrintFLSStatistics != 0) {6457gclog_or_tty->print_cr(6458"CMS: Large Block: " PTR_FORMAT ";"6459" Proximity: " PTR_FORMAT " -> " PTR_FORMAT,6460largestAddr,6461_cmsSpace->nearLargestChunk(), minAddr + nearLargestOffset);6462}6463_cmsSpace->set_nearLargestChunk(minAddr + nearLargestOffset);6464}64656466bool ConcurrentMarkSweepGeneration::isNearLargestChunk(HeapWord* addr) {6467return addr >= _cmsSpace->nearLargestChunk();6468}64696470FreeChunk* ConcurrentMarkSweepGeneration::find_chunk_at_end() {6471return _cmsSpace->find_chunk_at_end();6472}64736474void ConcurrentMarkSweepGeneration::update_gc_stats(int current_level,6475bool full) {6476// The next lower level has been collected. Gather any statistics6477// that are of interest at this point.6478if (!full && (current_level + 1) == level()) {6479// Gather statistics on the young generation collection.6480collector()->stats().record_gc0_end(used());6481}6482_cmsSpace->recalculate_used_stable();6483}64846485CMSAdaptiveSizePolicy* ConcurrentMarkSweepGeneration::size_policy() {6486GenCollectedHeap* gch = GenCollectedHeap::heap();6487assert(gch->kind() == CollectedHeap::GenCollectedHeap,6488"Wrong type of heap");6489CMSAdaptiveSizePolicy* sp = (CMSAdaptiveSizePolicy*)6490gch->gen_policy()->size_policy();6491assert(sp->is_gc_cms_adaptive_size_policy(),6492"Wrong type of size policy");6493return sp;6494}64956496void ConcurrentMarkSweepGeneration::rotate_debug_collection_type() {6497if (PrintGCDetails && Verbose) {6498gclog_or_tty->print("Rotate from %d ", _debug_collection_type);6499}6500_debug_collection_type = (CollectionTypes) (_debug_collection_type + 1);6501_debug_collection_type =6502(CollectionTypes) (_debug_collection_type % Unknown_collection_type);6503if (PrintGCDetails && Verbose) {6504gclog_or_tty->print_cr("to %d ", _debug_collection_type);6505}6506}65076508void CMSCollector::sweepWork(ConcurrentMarkSweepGeneration* gen,6509bool asynch) {6510// We iterate over the space(s) underlying this generation,6511// checking the mark bit map to see if the bits corresponding6512// to specific blocks are marked or not. Blocks that are6513// marked are live and are not swept up. All remaining blocks6514// are swept up, with coalescing on-the-fly as we sweep up6515// contiguous free and/or garbage blocks:6516// We need to ensure that the sweeper synchronizes with allocators6517// and stop-the-world collectors. In particular, the following6518// locks are used:6519// . CMS token: if this is held, a stop the world collection cannot occur6520// . freelistLock: if this is held no allocation can occur from this6521// generation by another thread6522// . bitMapLock: if this is held, no other thread can access or update6523//65246525// Note that we need to hold the freelistLock if we use6526// block iterate below; else the iterator might go awry if6527// a mutator (or promotion) causes block contents to change6528// (for instance if the allocator divvies up a block).6529// If we hold the free list lock, for all practical purposes6530// young generation GC's can't occur (they'll usually need to6531// promote), so we might as well prevent all young generation6532// GC's while we do a sweeping step. For the same reason, we might6533// as well take the bit map lock for the entire duration65346535// check that we hold the requisite locks6536assert(have_cms_token(), "Should hold cms token");6537assert( (asynch && ConcurrentMarkSweepThread::cms_thread_has_cms_token())6538|| (!asynch && ConcurrentMarkSweepThread::vm_thread_has_cms_token()),6539"Should possess CMS token to sweep");6540assert_lock_strong(gen->freelistLock());6541assert_lock_strong(bitMapLock());65426543assert(!_inter_sweep_timer.is_active(), "Was switched off in an outer context");6544assert(_intra_sweep_timer.is_active(), "Was switched on in an outer context");6545gen->cmsSpace()->beginSweepFLCensus((float)(_inter_sweep_timer.seconds()),6546_inter_sweep_estimate.padded_average(),6547_intra_sweep_estimate.padded_average());6548gen->setNearLargestChunk();65496550{6551SweepClosure sweepClosure(this, gen, &_markBitMap,6552CMSYield && asynch);6553gen->cmsSpace()->blk_iterate_careful(&sweepClosure);6554// We need to free-up/coalesce garbage/blocks from a6555// co-terminal free run. This is done in the SweepClosure6556// destructor; so, do not remove this scope, else the6557// end-of-sweep-census below will be off by a little bit.6558}6559gen->cmsSpace()->sweep_completed();6560gen->cmsSpace()->endSweepFLCensus(sweep_count());6561if (should_unload_classes()) { // unloaded classes this cycle,6562_concurrent_cycles_since_last_unload = 0; // ... reset count6563} else { // did not unload classes,6564_concurrent_cycles_since_last_unload++; // ... increment count6565}6566}65676568// Reset CMS data structures (for now just the marking bit map)6569// preparatory for the next cycle.6570void CMSCollector::reset(bool asynch) {6571GenCollectedHeap* gch = GenCollectedHeap::heap();6572CMSAdaptiveSizePolicy* sp = size_policy();6573AdaptiveSizePolicyOutput(sp, gch->total_collections());6574if (asynch) {6575CMSTokenSyncWithLocks ts(true, bitMapLock());65766577// If the state is not "Resetting", the foreground thread6578// has done a collection and the resetting.6579if (_collectorState != Resetting) {6580assert(_collectorState == Idling, "The state should only change"6581" because the foreground collector has finished the collection");6582return;6583}65846585// Clear the mark bitmap (no grey objects to start with)6586// for the next cycle.6587TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);6588CMSPhaseAccounting cmspa(this, "reset", _gc_tracer_cm->gc_id(), !PrintGCDetails);65896590HeapWord* curAddr = _markBitMap.startWord();6591while (curAddr < _markBitMap.endWord()) {6592size_t remaining = pointer_delta(_markBitMap.endWord(), curAddr);6593MemRegion chunk(curAddr, MIN2(CMSBitMapYieldQuantum, remaining));6594_markBitMap.clear_large_range(chunk);6595if (ConcurrentMarkSweepThread::should_yield() &&6596!foregroundGCIsActive() &&6597CMSYield) {6598assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),6599"CMS thread should hold CMS token");6600assert_lock_strong(bitMapLock());6601bitMapLock()->unlock();6602ConcurrentMarkSweepThread::desynchronize(true);6603ConcurrentMarkSweepThread::acknowledge_yield_request();6604stopTimer();6605if (PrintCMSStatistics != 0) {6606incrementYields();6607}6608icms_wait();66096610// See the comment in coordinator_yield()6611for (unsigned i = 0; i < CMSYieldSleepCount &&6612ConcurrentMarkSweepThread::should_yield() &&6613!CMSCollector::foregroundGCIsActive(); ++i) {6614os::sleep(Thread::current(), 1, false);6615ConcurrentMarkSweepThread::acknowledge_yield_request();6616}66176618ConcurrentMarkSweepThread::synchronize(true);6619bitMapLock()->lock_without_safepoint_check();6620startTimer();6621}6622curAddr = chunk.end();6623}6624// A successful mostly concurrent collection has been done.6625// Because only the full (i.e., concurrent mode failure) collections6626// are being measured for gc overhead limits, clean the "near" flag6627// and count.6628sp->reset_gc_overhead_limit_count();6629_collectorState = Idling;6630} else {6631// already have the lock6632assert(_collectorState == Resetting, "just checking");6633assert_lock_strong(bitMapLock());6634_markBitMap.clear_all();6635_collectorState = Idling;6636}66376638// Stop incremental mode after a cycle completes, so that any future cycles6639// are triggered by allocation.6640stop_icms();66416642NOT_PRODUCT(6643if (RotateCMSCollectionTypes) {6644_cmsGen->rotate_debug_collection_type();6645}6646)66476648register_gc_end();6649}66506651void CMSCollector::do_CMS_operation(CMS_op_type op, GCCause::Cause gc_cause) {6652TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);6653GCTraceTime t(GCCauseString("GC", gc_cause), PrintGC, !PrintGCDetails, NULL, _gc_tracer_cm->gc_id());6654TraceCollectorStats tcs(counters());66556656switch (op) {6657case CMS_op_checkpointRootsInitial: {6658SvcGCMarker sgcm(SvcGCMarker::OTHER);6659checkpointRootsInitial(true); // asynch6660if (PrintGC) {6661_cmsGen->printOccupancy("initial-mark");6662}6663break;6664}6665case CMS_op_checkpointRootsFinal: {6666SvcGCMarker sgcm(SvcGCMarker::OTHER);6667checkpointRootsFinal(true, // asynch6668false, // !clear_all_soft_refs6669false); // !init_mark_was_synchronous6670if (PrintGC) {6671_cmsGen->printOccupancy("remark");6672}6673break;6674}6675default:6676fatal("No such CMS_op");6677}6678}66796680#ifndef PRODUCT6681size_t const CMSCollector::skip_header_HeapWords() {6682return FreeChunk::header_size();6683}66846685// Try and collect here conditions that should hold when6686// CMS thread is exiting. The idea is that the foreground GC6687// thread should not be blocked if it wants to terminate6688// the CMS thread and yet continue to run the VM for a while6689// after that.6690void CMSCollector::verify_ok_to_terminate() const {6691assert(Thread::current()->is_ConcurrentGC_thread(),6692"should be called by CMS thread");6693assert(!_foregroundGCShouldWait, "should be false");6694// We could check here that all the various low-level locks6695// are not held by the CMS thread, but that is overkill; see6696// also CMSThread::verify_ok_to_terminate() where the CGC_lock6697// is checked.6698}6699#endif67006701size_t CMSCollector::block_size_using_printezis_bits(HeapWord* addr) const {6702assert(_markBitMap.isMarked(addr) && _markBitMap.isMarked(addr + 1),6703"missing Printezis mark?");6704HeapWord* nextOneAddr = _markBitMap.getNextMarkedWordAddress(addr + 2);6705size_t size = pointer_delta(nextOneAddr + 1, addr);6706assert(size == CompactibleFreeListSpace::adjustObjectSize(size),6707"alignment problem");6708assert(size >= 3, "Necessary for Printezis marks to work");6709return size;6710}67116712// A variant of the above (block_size_using_printezis_bits()) except6713// that we return 0 if the P-bits are not yet set.6714size_t CMSCollector::block_size_if_printezis_bits(HeapWord* addr) const {6715if (_markBitMap.isMarked(addr + 1)) {6716assert(_markBitMap.isMarked(addr), "P-bit can be set only for marked objects");6717HeapWord* nextOneAddr = _markBitMap.getNextMarkedWordAddress(addr + 2);6718size_t size = pointer_delta(nextOneAddr + 1, addr);6719assert(size == CompactibleFreeListSpace::adjustObjectSize(size),6720"alignment problem");6721assert(size >= 3, "Necessary for Printezis marks to work");6722return size;6723}6724return 0;6725}67266727HeapWord* CMSCollector::next_card_start_after_block(HeapWord* addr) const {6728size_t sz = 0;6729oop p = (oop)addr;6730if (p->klass_or_null_acquire() != NULL) {6731sz = CompactibleFreeListSpace::adjustObjectSize(p->size());6732} else {6733sz = block_size_using_printezis_bits(addr);6734}6735assert(sz > 0, "size must be nonzero");6736HeapWord* next_block = addr + sz;6737HeapWord* next_card = (HeapWord*)round_to((uintptr_t)next_block,6738CardTableModRefBS::card_size);6739assert(round_down((uintptr_t)addr, CardTableModRefBS::card_size) <6740round_down((uintptr_t)next_card, CardTableModRefBS::card_size),6741"must be different cards");6742return next_card;6743}674467456746// CMS Bit Map Wrapper /////////////////////////////////////////67476748// Construct a CMS bit map infrastructure, but don't create the6749// bit vector itself. That is done by a separate call CMSBitMap::allocate()6750// further below.6751CMSBitMap::CMSBitMap(int shifter, int mutex_rank, const char* mutex_name):6752_bm(),6753_shifter(shifter),6754_lock(mutex_rank >= 0 ? new Mutex(mutex_rank, mutex_name, true) : NULL)6755{6756_bmStartWord = 0;6757_bmWordSize = 0;6758}67596760bool CMSBitMap::allocate(MemRegion mr) {6761_bmStartWord = mr.start();6762_bmWordSize = mr.word_size();6763ReservedSpace brs(ReservedSpace::allocation_align_size_up(6764(_bmWordSize >> (_shifter + LogBitsPerByte)) + 1));6765if (!brs.is_reserved()) {6766warning("CMS bit map allocation failure");6767return false;6768}6769// For now we'll just commit all of the bit map up fromt.6770// Later on we'll try to be more parsimonious with swap.6771if (!_virtual_space.initialize(brs, brs.size())) {6772warning("CMS bit map backing store failure");6773return false;6774}6775assert(_virtual_space.committed_size() == brs.size(),6776"didn't reserve backing store for all of CMS bit map?");6777_bm.set_map((BitMap::bm_word_t*)_virtual_space.low());6778assert(_virtual_space.committed_size() << (_shifter + LogBitsPerByte) >=6779_bmWordSize, "inconsistency in bit map sizing");6780_bm.set_size(_bmWordSize >> _shifter);67816782// bm.clear(); // can we rely on getting zero'd memory? verify below6783assert(isAllClear(),6784"Expected zero'd memory from ReservedSpace constructor");6785assert(_bm.size() == heapWordDiffToOffsetDiff(sizeInWords()),6786"consistency check");6787return true;6788}67896790void CMSBitMap::dirty_range_iterate_clear(MemRegion mr, MemRegionClosure* cl) {6791HeapWord *next_addr, *end_addr, *last_addr;6792assert_locked();6793assert(covers(mr), "out-of-range error");6794// XXX assert that start and end are appropriately aligned6795for (next_addr = mr.start(), end_addr = mr.end();6796next_addr < end_addr; next_addr = last_addr) {6797MemRegion dirty_region = getAndClearMarkedRegion(next_addr, end_addr);6798last_addr = dirty_region.end();6799if (!dirty_region.is_empty()) {6800cl->do_MemRegion(dirty_region);6801} else {6802assert(last_addr == end_addr, "program logic");6803return;6804}6805}6806}68076808void CMSBitMap::print_on_error(outputStream* st, const char* prefix) const {6809_bm.print_on_error(st, prefix);6810}68116812#ifndef PRODUCT6813void CMSBitMap::assert_locked() const {6814CMSLockVerifier::assert_locked(lock());6815}68166817bool CMSBitMap::covers(MemRegion mr) const {6818// assert(_bm.map() == _virtual_space.low(), "map inconsistency");6819assert((size_t)_bm.size() == (_bmWordSize >> _shifter),6820"size inconsistency");6821return (mr.start() >= _bmStartWord) &&6822(mr.end() <= endWord());6823}68246825bool CMSBitMap::covers(HeapWord* start, size_t size) const {6826return (start >= _bmStartWord && (start + size) <= endWord());6827}68286829void CMSBitMap::verifyNoOneBitsInRange(HeapWord* left, HeapWord* right) {6830// verify that there are no 1 bits in the interval [left, right)6831FalseBitMapClosure falseBitMapClosure;6832iterate(&falseBitMapClosure, left, right);6833}68346835void CMSBitMap::region_invariant(MemRegion mr)6836{6837assert_locked();6838// mr = mr.intersection(MemRegion(_bmStartWord, _bmWordSize));6839assert(!mr.is_empty(), "unexpected empty region");6840assert(covers(mr), "mr should be covered by bit map");6841// convert address range into offset range6842size_t start_ofs = heapWordToOffset(mr.start());6843// Make sure that end() is appropriately aligned6844assert(mr.end() == (HeapWord*)round_to((intptr_t)mr.end(),6845((intptr_t) 1 << (_shifter+LogHeapWordSize))),6846"Misaligned mr.end()");6847size_t end_ofs = heapWordToOffset(mr.end());6848assert(end_ofs > start_ofs, "Should mark at least one bit");6849}68506851#endif68526853bool CMSMarkStack::allocate(size_t size) {6854// allocate a stack of the requisite depth6855ReservedSpace rs(ReservedSpace::allocation_align_size_up(6856size * sizeof(oop)));6857if (!rs.is_reserved()) {6858warning("CMSMarkStack allocation failure");6859return false;6860}6861if (!_virtual_space.initialize(rs, rs.size())) {6862warning("CMSMarkStack backing store failure");6863return false;6864}6865assert(_virtual_space.committed_size() == rs.size(),6866"didn't reserve backing store for all of CMS stack?");6867_base = (oop*)(_virtual_space.low());6868_index = 0;6869_capacity = size;6870NOT_PRODUCT(_max_depth = 0);6871return true;6872}68736874// XXX FIX ME !!! In the MT case we come in here holding a6875// leaf lock. For printing we need to take a further lock6876// which has lower rank. We need to recallibrate the two6877// lock-ranks involved in order to be able to rpint the6878// messages below. (Or defer the printing to the caller.6879// For now we take the expedient path of just disabling the6880// messages for the problematic case.)6881void CMSMarkStack::expand() {6882assert(_capacity <= MarkStackSizeMax, "stack bigger than permitted");6883if (_capacity == MarkStackSizeMax) {6884if (_hit_limit++ == 0 && !CMSConcurrentMTEnabled && PrintGCDetails) {6885// We print a warning message only once per CMS cycle.6886gclog_or_tty->print_cr(" (benign) Hit CMSMarkStack max size limit");6887}6888return;6889}6890// Double capacity if possible6891size_t new_capacity = MIN2(_capacity*2, MarkStackSizeMax);6892// Do not give up existing stack until we have managed to6893// get the double capacity that we desired.6894ReservedSpace rs(ReservedSpace::allocation_align_size_up(6895new_capacity * sizeof(oop)));6896if (rs.is_reserved()) {6897// Release the backing store associated with old stack6898_virtual_space.release();6899// Reinitialize virtual space for new stack6900if (!_virtual_space.initialize(rs, rs.size())) {6901fatal("Not enough swap for expanded marking stack");6902}6903_base = (oop*)(_virtual_space.low());6904_index = 0;6905_capacity = new_capacity;6906} else if (_failed_double++ == 0 && !CMSConcurrentMTEnabled && PrintGCDetails) {6907// Failed to double capacity, continue;6908// we print a detail message only once per CMS cycle.6909gclog_or_tty->print(" (benign) Failed to expand marking stack from " SIZE_FORMAT "K to "6910SIZE_FORMAT "K",6911_capacity / K, new_capacity / K);6912}6913}691469156916// Closures6917// XXX: there seems to be a lot of code duplication here;6918// should refactor and consolidate common code.69196920// This closure is used to mark refs into the CMS generation in6921// the CMS bit map. Called at the first checkpoint. This closure6922// assumes that we do not need to re-mark dirty cards; if the CMS6923// generation on which this is used is not an oldest6924// generation then this will lose younger_gen cards!69256926MarkRefsIntoClosure::MarkRefsIntoClosure(6927MemRegion span, CMSBitMap* bitMap):6928_span(span),6929_bitMap(bitMap)6930{6931assert(_ref_processor == NULL, "deliberately left NULL");6932assert(_bitMap->covers(_span), "_bitMap/_span mismatch");6933}69346935void MarkRefsIntoClosure::do_oop(oop obj) {6936// if p points into _span, then mark corresponding bit in _markBitMap6937assert(obj->is_oop(), "expected an oop");6938HeapWord* addr = (HeapWord*)obj;6939if (_span.contains(addr)) {6940// this should be made more efficient6941_bitMap->mark(addr);6942}6943}69446945void MarkRefsIntoClosure::do_oop(oop* p) { MarkRefsIntoClosure::do_oop_work(p); }6946void MarkRefsIntoClosure::do_oop(narrowOop* p) { MarkRefsIntoClosure::do_oop_work(p); }69476948Par_MarkRefsIntoClosure::Par_MarkRefsIntoClosure(6949MemRegion span, CMSBitMap* bitMap):6950_span(span),6951_bitMap(bitMap)6952{6953assert(_ref_processor == NULL, "deliberately left NULL");6954assert(_bitMap->covers(_span), "_bitMap/_span mismatch");6955}69566957void Par_MarkRefsIntoClosure::do_oop(oop obj) {6958// if p points into _span, then mark corresponding bit in _markBitMap6959assert(obj->is_oop(), "expected an oop");6960HeapWord* addr = (HeapWord*)obj;6961if (_span.contains(addr)) {6962// this should be made more efficient6963_bitMap->par_mark(addr);6964}6965}69666967void Par_MarkRefsIntoClosure::do_oop(oop* p) { Par_MarkRefsIntoClosure::do_oop_work(p); }6968void Par_MarkRefsIntoClosure::do_oop(narrowOop* p) { Par_MarkRefsIntoClosure::do_oop_work(p); }69696970// A variant of the above, used for CMS marking verification.6971MarkRefsIntoVerifyClosure::MarkRefsIntoVerifyClosure(6972MemRegion span, CMSBitMap* verification_bm, CMSBitMap* cms_bm):6973_span(span),6974_verification_bm(verification_bm),6975_cms_bm(cms_bm)6976{6977assert(_ref_processor == NULL, "deliberately left NULL");6978assert(_verification_bm->covers(_span), "_verification_bm/_span mismatch");6979}69806981void MarkRefsIntoVerifyClosure::do_oop(oop obj) {6982// if p points into _span, then mark corresponding bit in _markBitMap6983assert(obj->is_oop(), "expected an oop");6984HeapWord* addr = (HeapWord*)obj;6985if (_span.contains(addr)) {6986_verification_bm->mark(addr);6987if (!_cms_bm->isMarked(addr)) {6988oop(addr)->print();6989gclog_or_tty->print_cr(" (" INTPTR_FORMAT " should have been marked)", addr);6990fatal("... aborting");6991}6992}6993}69946995void MarkRefsIntoVerifyClosure::do_oop(oop* p) { MarkRefsIntoVerifyClosure::do_oop_work(p); }6996void MarkRefsIntoVerifyClosure::do_oop(narrowOop* p) { MarkRefsIntoVerifyClosure::do_oop_work(p); }69976998//////////////////////////////////////////////////6999// MarkRefsIntoAndScanClosure7000//////////////////////////////////////////////////70017002MarkRefsIntoAndScanClosure::MarkRefsIntoAndScanClosure(MemRegion span,7003ReferenceProcessor* rp,7004CMSBitMap* bit_map,7005CMSBitMap* mod_union_table,7006CMSMarkStack* mark_stack,7007CMSCollector* collector,7008bool should_yield,7009bool concurrent_precleaning):7010_collector(collector),7011_span(span),7012_bit_map(bit_map),7013_mark_stack(mark_stack),7014_pushAndMarkClosure(collector, span, rp, bit_map, mod_union_table,7015mark_stack, concurrent_precleaning),7016_yield(should_yield),7017_concurrent_precleaning(concurrent_precleaning),7018_freelistLock(NULL)7019{7020_ref_processor = rp;7021assert(_ref_processor != NULL, "_ref_processor shouldn't be NULL");7022}70237024// This closure is used to mark refs into the CMS generation at the7025// second (final) checkpoint, and to scan and transitively follow7026// the unmarked oops. It is also used during the concurrent precleaning7027// phase while scanning objects on dirty cards in the CMS generation.7028// The marks are made in the marking bit map and the marking stack is7029// used for keeping the (newly) grey objects during the scan.7030// The parallel version (Par_...) appears further below.7031void MarkRefsIntoAndScanClosure::do_oop(oop obj) {7032if (obj != NULL) {7033assert(obj->is_oop(), "expected an oop");7034HeapWord* addr = (HeapWord*)obj;7035assert(_mark_stack->isEmpty(), "pre-condition (eager drainage)");7036assert(_collector->overflow_list_is_empty(),7037"overflow list should be empty");7038if (_span.contains(addr) &&7039!_bit_map->isMarked(addr)) {7040// mark bit map (object is now grey)7041_bit_map->mark(addr);7042// push on marking stack (stack should be empty), and drain the7043// stack by applying this closure to the oops in the oops popped7044// from the stack (i.e. blacken the grey objects)7045bool res = _mark_stack->push(obj);7046assert(res, "Should have space to push on empty stack");7047do {7048oop new_oop = _mark_stack->pop();7049assert(new_oop != NULL && new_oop->is_oop(), "Expected an oop");7050assert(_bit_map->isMarked((HeapWord*)new_oop),7051"only grey objects on this stack");7052// iterate over the oops in this oop, marking and pushing7053// the ones in CMS heap (i.e. in _span).7054new_oop->oop_iterate(&_pushAndMarkClosure);7055// check if it's time to yield7056do_yield_check();7057} while (!_mark_stack->isEmpty() ||7058(!_concurrent_precleaning && take_from_overflow_list()));7059// if marking stack is empty, and we are not doing this7060// during precleaning, then check the overflow list7061}7062assert(_mark_stack->isEmpty(), "post-condition (eager drainage)");7063assert(_collector->overflow_list_is_empty(),7064"overflow list was drained above");7065// We could restore evacuated mark words, if any, used for7066// overflow list links here because the overflow list is7067// provably empty here. That would reduce the maximum7068// size requirements for preserved_{oop,mark}_stack.7069// But we'll just postpone it until we are all done7070// so we can just stream through.7071if (!_concurrent_precleaning && CMSOverflowEarlyRestoration) {7072_collector->restore_preserved_marks_if_any();7073assert(_collector->no_preserved_marks(), "No preserved marks");7074}7075assert(!CMSOverflowEarlyRestoration || _collector->no_preserved_marks(),7076"All preserved marks should have been restored above");7077}7078}70797080void MarkRefsIntoAndScanClosure::do_oop(oop* p) { MarkRefsIntoAndScanClosure::do_oop_work(p); }7081void MarkRefsIntoAndScanClosure::do_oop(narrowOop* p) { MarkRefsIntoAndScanClosure::do_oop_work(p); }70827083void MarkRefsIntoAndScanClosure::do_yield_work() {7084assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),7085"CMS thread should hold CMS token");7086assert_lock_strong(_freelistLock);7087assert_lock_strong(_bit_map->lock());7088// relinquish the free_list_lock and bitMaplock()7089_bit_map->lock()->unlock();7090_freelistLock->unlock();7091ConcurrentMarkSweepThread::desynchronize(true);7092ConcurrentMarkSweepThread::acknowledge_yield_request();7093_collector->stopTimer();7094GCPauseTimer p(_collector->size_policy()->concurrent_timer_ptr());7095if (PrintCMSStatistics != 0) {7096_collector->incrementYields();7097}7098_collector->icms_wait();70997100// See the comment in coordinator_yield()7101for (unsigned i = 0;7102i < CMSYieldSleepCount &&7103ConcurrentMarkSweepThread::should_yield() &&7104!CMSCollector::foregroundGCIsActive();7105++i) {7106os::sleep(Thread::current(), 1, false);7107ConcurrentMarkSweepThread::acknowledge_yield_request();7108}71097110ConcurrentMarkSweepThread::synchronize(true);7111_freelistLock->lock_without_safepoint_check();7112_bit_map->lock()->lock_without_safepoint_check();7113_collector->startTimer();7114}71157116///////////////////////////////////////////////////////////7117// Par_MarkRefsIntoAndScanClosure: a parallel version of7118// MarkRefsIntoAndScanClosure7119///////////////////////////////////////////////////////////7120Par_MarkRefsIntoAndScanClosure::Par_MarkRefsIntoAndScanClosure(7121CMSCollector* collector, MemRegion span, ReferenceProcessor* rp,7122CMSBitMap* bit_map, OopTaskQueue* work_queue):7123_span(span),7124_bit_map(bit_map),7125_work_queue(work_queue),7126_low_water_mark(MIN2((uint)(work_queue->max_elems()/4),7127(uint)(CMSWorkQueueDrainThreshold * ParallelGCThreads))),7128_par_pushAndMarkClosure(collector, span, rp, bit_map, work_queue)7129{7130_ref_processor = rp;7131assert(_ref_processor != NULL, "_ref_processor shouldn't be NULL");7132}71337134// This closure is used to mark refs into the CMS generation at the7135// second (final) checkpoint, and to scan and transitively follow7136// the unmarked oops. The marks are made in the marking bit map and7137// the work_queue is used for keeping the (newly) grey objects during7138// the scan phase whence they are also available for stealing by parallel7139// threads. Since the marking bit map is shared, updates are7140// synchronized (via CAS).7141void Par_MarkRefsIntoAndScanClosure::do_oop(oop obj) {7142if (obj != NULL) {7143// Ignore mark word because this could be an already marked oop7144// that may be chained at the end of the overflow list.7145assert(obj->is_oop(true), "expected an oop");7146HeapWord* addr = (HeapWord*)obj;7147if (_span.contains(addr) &&7148!_bit_map->isMarked(addr)) {7149// mark bit map (object will become grey):7150// It is possible for several threads to be7151// trying to "claim" this object concurrently;7152// the unique thread that succeeds in marking the7153// object first will do the subsequent push on7154// to the work queue (or overflow list).7155if (_bit_map->par_mark(addr)) {7156// push on work_queue (which may not be empty), and trim the7157// queue to an appropriate length by applying this closure to7158// the oops in the oops popped from the stack (i.e. blacken the7159// grey objects)7160bool res = _work_queue->push(obj);7161assert(res, "Low water mark should be less than capacity?");7162trim_queue(_low_water_mark);7163} // Else, another thread claimed the object7164}7165}7166}71677168void Par_MarkRefsIntoAndScanClosure::do_oop(oop* p) { Par_MarkRefsIntoAndScanClosure::do_oop_work(p); }7169void Par_MarkRefsIntoAndScanClosure::do_oop(narrowOop* p) { Par_MarkRefsIntoAndScanClosure::do_oop_work(p); }71707171// This closure is used to rescan the marked objects on the dirty cards7172// in the mod union table and the card table proper.7173size_t ScanMarkedObjectsAgainCarefullyClosure::do_object_careful_m(7174oop p, MemRegion mr) {71757176size_t size = 0;7177HeapWord* addr = (HeapWord*)p;7178DEBUG_ONLY(_collector->verify_work_stacks_empty();)7179assert(_span.contains(addr), "we are scanning the CMS generation");7180// check if it's time to yield7181if (do_yield_check()) {7182// We yielded for some foreground stop-world work,7183// and we have been asked to abort this ongoing preclean cycle.7184return 0;7185}7186if (_bitMap->isMarked(addr)) {7187// it's marked; is it potentially uninitialized?7188if (p->klass_or_null_acquire() != NULL) {7189// an initialized object; ignore mark word in verification below7190// since we are running concurrent with mutators7191assert(p->is_oop(true), "should be an oop");7192if (p->is_objArray()) {7193// objArrays are precisely marked; restrict scanning7194// to dirty cards only.7195size = CompactibleFreeListSpace::adjustObjectSize(7196p->oop_iterate(_scanningClosure, mr));7197} else {7198// A non-array may have been imprecisely marked; we need7199// to scan object in its entirety.7200size = CompactibleFreeListSpace::adjustObjectSize(7201p->oop_iterate(_scanningClosure));7202}7203#ifdef ASSERT7204size_t direct_size =7205CompactibleFreeListSpace::adjustObjectSize(p->size());7206assert(size == direct_size, "Inconsistency in size");7207assert(size >= 3, "Necessary for Printezis marks to work");7208if (!_bitMap->isMarked(addr+1)) {7209_bitMap->verifyNoOneBitsInRange(addr+2, addr+size);7210} else {7211_bitMap->verifyNoOneBitsInRange(addr+2, addr+size-1);7212assert(_bitMap->isMarked(addr+size-1),7213"inconsistent Printezis mark");7214}7215#endif // ASSERT7216} else {7217// an unitialized object7218assert(_bitMap->isMarked(addr+1), "missing Printezis mark?");7219HeapWord* nextOneAddr = _bitMap->getNextMarkedWordAddress(addr + 2);7220size = pointer_delta(nextOneAddr + 1, addr);7221assert(size == CompactibleFreeListSpace::adjustObjectSize(size),7222"alignment problem");7223// Note that pre-cleaning needn't redirty the card. OopDesc::set_klass()7224// will dirty the card when the klass pointer is installed in the7225// object (signalling the completion of initialization).7226}7227} else {7228// Either a not yet marked object or an uninitialized object7229if (p->klass_or_null_acquire() == NULL) {7230// An uninitialized object, skip to the next card, since7231// we may not be able to read its P-bits yet.7232assert(size == 0, "Initial value");7233} else {7234// An object not (yet) reached by marking: we merely need to7235// compute its size so as to go look at the next block.7236assert(p->is_oop(true), "should be an oop");7237size = CompactibleFreeListSpace::adjustObjectSize(p->size());7238}7239}7240DEBUG_ONLY(_collector->verify_work_stacks_empty();)7241return size;7242}72437244void ScanMarkedObjectsAgainCarefullyClosure::do_yield_work() {7245assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),7246"CMS thread should hold CMS token");7247assert_lock_strong(_freelistLock);7248assert_lock_strong(_bitMap->lock());7249// relinquish the free_list_lock and bitMaplock()7250_bitMap->lock()->unlock();7251_freelistLock->unlock();7252ConcurrentMarkSweepThread::desynchronize(true);7253ConcurrentMarkSweepThread::acknowledge_yield_request();7254_collector->stopTimer();7255GCPauseTimer p(_collector->size_policy()->concurrent_timer_ptr());7256if (PrintCMSStatistics != 0) {7257_collector->incrementYields();7258}7259_collector->icms_wait();72607261// See the comment in coordinator_yield()7262for (unsigned i = 0; i < CMSYieldSleepCount &&7263ConcurrentMarkSweepThread::should_yield() &&7264!CMSCollector::foregroundGCIsActive(); ++i) {7265os::sleep(Thread::current(), 1, false);7266ConcurrentMarkSweepThread::acknowledge_yield_request();7267}72687269ConcurrentMarkSweepThread::synchronize(true);7270_freelistLock->lock_without_safepoint_check();7271_bitMap->lock()->lock_without_safepoint_check();7272_collector->startTimer();7273}727472757276//////////////////////////////////////////////////////////////////7277// SurvivorSpacePrecleanClosure7278//////////////////////////////////////////////////////////////////7279// This (single-threaded) closure is used to preclean the oops in7280// the survivor spaces.7281size_t SurvivorSpacePrecleanClosure::do_object_careful(oop p) {72827283HeapWord* addr = (HeapWord*)p;7284DEBUG_ONLY(_collector->verify_work_stacks_empty();)7285assert(!_span.contains(addr), "we are scanning the survivor spaces");7286assert(p->klass_or_null() != NULL, "object should be initializd");7287// an initialized object; ignore mark word in verification below7288// since we are running concurrent with mutators7289assert(p->is_oop(true), "should be an oop");7290// Note that we do not yield while we iterate over7291// the interior oops of p, pushing the relevant ones7292// on our marking stack.7293size_t size = p->oop_iterate(_scanning_closure);7294do_yield_check();7295// Observe that below, we do not abandon the preclean7296// phase as soon as we should; rather we empty the7297// marking stack before returning. This is to satisfy7298// some existing assertions. In general, it may be a7299// good idea to abort immediately and complete the marking7300// from the grey objects at a later time.7301while (!_mark_stack->isEmpty()) {7302oop new_oop = _mark_stack->pop();7303assert(new_oop != NULL && new_oop->is_oop(), "Expected an oop");7304assert(_bit_map->isMarked((HeapWord*)new_oop),7305"only grey objects on this stack");7306// iterate over the oops in this oop, marking and pushing7307// the ones in CMS heap (i.e. in _span).7308new_oop->oop_iterate(_scanning_closure);7309// check if it's time to yield7310do_yield_check();7311}7312unsigned int after_count =7313GenCollectedHeap::heap()->total_collections();7314bool abort = (_before_count != after_count) ||7315_collector->should_abort_preclean();7316return abort ? 0 : size;7317}73187319void SurvivorSpacePrecleanClosure::do_yield_work() {7320assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),7321"CMS thread should hold CMS token");7322assert_lock_strong(_bit_map->lock());7323// Relinquish the bit map lock7324_bit_map->lock()->unlock();7325ConcurrentMarkSweepThread::desynchronize(true);7326ConcurrentMarkSweepThread::acknowledge_yield_request();7327_collector->stopTimer();7328GCPauseTimer p(_collector->size_policy()->concurrent_timer_ptr());7329if (PrintCMSStatistics != 0) {7330_collector->incrementYields();7331}7332_collector->icms_wait();73337334// See the comment in coordinator_yield()7335for (unsigned i = 0; i < CMSYieldSleepCount &&7336ConcurrentMarkSweepThread::should_yield() &&7337!CMSCollector::foregroundGCIsActive(); ++i) {7338os::sleep(Thread::current(), 1, false);7339ConcurrentMarkSweepThread::acknowledge_yield_request();7340}73417342ConcurrentMarkSweepThread::synchronize(true);7343_bit_map->lock()->lock_without_safepoint_check();7344_collector->startTimer();7345}73467347// This closure is used to rescan the marked objects on the dirty cards7348// in the mod union table and the card table proper. In the parallel7349// case, although the bitMap is shared, we do a single read so the7350// isMarked() query is "safe".7351bool ScanMarkedObjectsAgainClosure::do_object_bm(oop p, MemRegion mr) {7352// Ignore mark word because we are running concurrent with mutators7353assert(p->is_oop_or_null(true), "expected an oop or null");7354HeapWord* addr = (HeapWord*)p;7355assert(_span.contains(addr), "we are scanning the CMS generation");7356bool is_obj_array = false;7357#ifdef ASSERT7358if (!_parallel) {7359assert(_mark_stack->isEmpty(), "pre-condition (eager drainage)");7360assert(_collector->overflow_list_is_empty(),7361"overflow list should be empty");73627363}7364#endif // ASSERT7365if (_bit_map->isMarked(addr)) {7366// Obj arrays are precisely marked, non-arrays are not;7367// so we scan objArrays precisely and non-arrays in their7368// entirety.7369if (p->is_objArray()) {7370is_obj_array = true;7371if (_parallel) {7372p->oop_iterate(_par_scan_closure, mr);7373} else {7374p->oop_iterate(_scan_closure, mr);7375}7376} else {7377if (_parallel) {7378p->oop_iterate(_par_scan_closure);7379} else {7380p->oop_iterate(_scan_closure);7381}7382}7383}7384#ifdef ASSERT7385if (!_parallel) {7386assert(_mark_stack->isEmpty(), "post-condition (eager drainage)");7387assert(_collector->overflow_list_is_empty(),7388"overflow list should be empty");73897390}7391#endif // ASSERT7392return is_obj_array;7393}73947395MarkFromRootsClosure::MarkFromRootsClosure(CMSCollector* collector,7396MemRegion span,7397CMSBitMap* bitMap, CMSMarkStack* markStack,7398bool should_yield, bool verifying):7399_collector(collector),7400_span(span),7401_bitMap(bitMap),7402_mut(&collector->_modUnionTable),7403_markStack(markStack),7404_yield(should_yield),7405_skipBits(0)7406{7407assert(_markStack->isEmpty(), "stack should be empty");7408_finger = _bitMap->startWord();7409_threshold = _finger;7410assert(_collector->_restart_addr == NULL, "Sanity check");7411assert(_span.contains(_finger), "Out of bounds _finger?");7412DEBUG_ONLY(_verifying = verifying;)7413}74147415void MarkFromRootsClosure::reset(HeapWord* addr) {7416assert(_markStack->isEmpty(), "would cause duplicates on stack");7417assert(_span.contains(addr), "Out of bounds _finger?");7418_finger = addr;7419_threshold = (HeapWord*)round_to(7420(intptr_t)_finger, CardTableModRefBS::card_size);7421}74227423// Should revisit to see if this should be restructured for7424// greater efficiency.7425bool MarkFromRootsClosure::do_bit(size_t offset) {7426if (_skipBits > 0) {7427_skipBits--;7428return true;7429}7430// convert offset into a HeapWord*7431HeapWord* addr = _bitMap->startWord() + offset;7432assert(_bitMap->endWord() && addr < _bitMap->endWord(),7433"address out of range");7434assert(_bitMap->isMarked(addr), "tautology");7435if (_bitMap->isMarked(addr+1)) {7436// this is an allocated but not yet initialized object7437assert(_skipBits == 0, "tautology");7438_skipBits = 2; // skip next two marked bits ("Printezis-marks")7439oop p = oop(addr);7440if (p->klass_or_null_acquire() == NULL) {7441DEBUG_ONLY(if (!_verifying) {)7442// We re-dirty the cards on which this object lies and increase7443// the _threshold so that we'll come back to scan this object7444// during the preclean or remark phase. (CMSCleanOnEnter)7445if (CMSCleanOnEnter) {7446size_t sz = _collector->block_size_using_printezis_bits(addr);7447HeapWord* end_card_addr = (HeapWord*)round_to(7448(intptr_t)(addr+sz), CardTableModRefBS::card_size);7449MemRegion redirty_range = MemRegion(addr, end_card_addr);7450assert(!redirty_range.is_empty(), "Arithmetical tautology");7451// Bump _threshold to end_card_addr; note that7452// _threshold cannot possibly exceed end_card_addr, anyhow.7453// This prevents future clearing of the card as the scan proceeds7454// to the right.7455assert(_threshold <= end_card_addr,7456"Because we are just scanning into this object");7457if (_threshold < end_card_addr) {7458_threshold = end_card_addr;7459}7460if (p->klass_or_null_acquire() != NULL) {7461// Redirty the range of cards...7462_mut->mark_range(redirty_range);7463} // ...else the setting of klass will dirty the card anyway.7464}7465DEBUG_ONLY(})7466return true;7467}7468}7469scanOopsInOop(addr);7470return true;7471}74727473// We take a break if we've been at this for a while,7474// so as to avoid monopolizing the locks involved.7475void MarkFromRootsClosure::do_yield_work() {7476// First give up the locks, then yield, then re-lock7477// We should probably use a constructor/destructor idiom to7478// do this unlock/lock or modify the MutexUnlocker class to7479// serve our purpose. XXX7480assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),7481"CMS thread should hold CMS token");7482assert_lock_strong(_bitMap->lock());7483_bitMap->lock()->unlock();7484ConcurrentMarkSweepThread::desynchronize(true);7485ConcurrentMarkSweepThread::acknowledge_yield_request();7486_collector->stopTimer();7487GCPauseTimer p(_collector->size_policy()->concurrent_timer_ptr());7488if (PrintCMSStatistics != 0) {7489_collector->incrementYields();7490}7491_collector->icms_wait();74927493// See the comment in coordinator_yield()7494for (unsigned i = 0; i < CMSYieldSleepCount &&7495ConcurrentMarkSweepThread::should_yield() &&7496!CMSCollector::foregroundGCIsActive(); ++i) {7497os::sleep(Thread::current(), 1, false);7498ConcurrentMarkSweepThread::acknowledge_yield_request();7499}75007501ConcurrentMarkSweepThread::synchronize(true);7502_bitMap->lock()->lock_without_safepoint_check();7503_collector->startTimer();7504}75057506void MarkFromRootsClosure::scanOopsInOop(HeapWord* ptr) {7507assert(_bitMap->isMarked(ptr), "expected bit to be set");7508assert(_markStack->isEmpty(),7509"should drain stack to limit stack usage");7510// convert ptr to an oop preparatory to scanning7511oop obj = oop(ptr);7512// Ignore mark word in verification below, since we7513// may be running concurrent with mutators.7514assert(obj->is_oop(true), "should be an oop");7515assert(_finger <= ptr, "_finger runneth ahead");7516// advance the finger to right end of this object7517_finger = ptr + obj->size();7518assert(_finger > ptr, "we just incremented it above");7519// On large heaps, it may take us some time to get through7520// the marking phase (especially if running iCMS). During7521// this time it's possible that a lot of mutations have7522// accumulated in the card table and the mod union table --7523// these mutation records are redundant until we have7524// actually traced into the corresponding card.7525// Here, we check whether advancing the finger would make7526// us cross into a new card, and if so clear corresponding7527// cards in the MUT (preclean them in the card-table in the7528// future).75297530DEBUG_ONLY(if (!_verifying) {)7531// The clean-on-enter optimization is disabled by default,7532// until we fix 6178663.7533if (CMSCleanOnEnter && (_finger > _threshold)) {7534// [_threshold, _finger) represents the interval7535// of cards to be cleared in MUT (or precleaned in card table).7536// The set of cards to be cleared is all those that overlap7537// with the interval [_threshold, _finger); note that7538// _threshold is always kept card-aligned but _finger isn't7539// always card-aligned.7540HeapWord* old_threshold = _threshold;7541assert(old_threshold == (HeapWord*)round_to(7542(intptr_t)old_threshold, CardTableModRefBS::card_size),7543"_threshold should always be card-aligned");7544_threshold = (HeapWord*)round_to(7545(intptr_t)_finger, CardTableModRefBS::card_size);7546MemRegion mr(old_threshold, _threshold);7547assert(!mr.is_empty(), "Control point invariant");7548assert(_span.contains(mr), "Should clear within span");7549_mut->clear_range(mr);7550}7551DEBUG_ONLY(})7552// Note: the finger doesn't advance while we drain7553// the stack below.7554PushOrMarkClosure pushOrMarkClosure(_collector,7555_span, _bitMap, _markStack,7556_finger, this);7557bool res = _markStack->push(obj);7558assert(res, "Empty non-zero size stack should have space for single push");7559while (!_markStack->isEmpty()) {7560oop new_oop = _markStack->pop();7561// Skip verifying header mark word below because we are7562// running concurrent with mutators.7563assert(new_oop->is_oop(true), "Oops! expected to pop an oop");7564// now scan this oop's oops7565new_oop->oop_iterate(&pushOrMarkClosure);7566do_yield_check();7567}7568assert(_markStack->isEmpty(), "tautology, emphasizing post-condition");7569}75707571Par_MarkFromRootsClosure::Par_MarkFromRootsClosure(CMSConcMarkingTask* task,7572CMSCollector* collector, MemRegion span,7573CMSBitMap* bit_map,7574OopTaskQueue* work_queue,7575CMSMarkStack* overflow_stack,7576bool should_yield):7577_collector(collector),7578_whole_span(collector->_span),7579_span(span),7580_bit_map(bit_map),7581_mut(&collector->_modUnionTable),7582_work_queue(work_queue),7583_overflow_stack(overflow_stack),7584_yield(should_yield),7585_skip_bits(0),7586_task(task)7587{7588assert(_work_queue->size() == 0, "work_queue should be empty");7589_finger = span.start();7590_threshold = _finger; // XXX Defer clear-on-enter optimization for now7591assert(_span.contains(_finger), "Out of bounds _finger?");7592}75937594// Should revisit to see if this should be restructured for7595// greater efficiency.7596bool Par_MarkFromRootsClosure::do_bit(size_t offset) {7597if (_skip_bits > 0) {7598_skip_bits--;7599return true;7600}7601// convert offset into a HeapWord*7602HeapWord* addr = _bit_map->startWord() + offset;7603assert(_bit_map->endWord() && addr < _bit_map->endWord(),7604"address out of range");7605assert(_bit_map->isMarked(addr), "tautology");7606if (_bit_map->isMarked(addr+1)) {7607// this is an allocated object that might not yet be initialized7608assert(_skip_bits == 0, "tautology");7609_skip_bits = 2; // skip next two marked bits ("Printezis-marks")7610oop p = oop(addr);7611if (p->klass_or_null_acquire() == NULL) {7612// in the case of Clean-on-Enter optimization, redirty card7613// and avoid clearing card by increasing the threshold.7614return true;7615}7616}7617scan_oops_in_oop(addr);7618return true;7619}76207621void Par_MarkFromRootsClosure::scan_oops_in_oop(HeapWord* ptr) {7622assert(_bit_map->isMarked(ptr), "expected bit to be set");7623// Should we assert that our work queue is empty or7624// below some drain limit?7625assert(_work_queue->size() == 0,7626"should drain stack to limit stack usage");7627// convert ptr to an oop preparatory to scanning7628oop obj = oop(ptr);7629// Ignore mark word in verification below, since we7630// may be running concurrent with mutators.7631assert(obj->is_oop(true), "should be an oop");7632assert(_finger <= ptr, "_finger runneth ahead");7633// advance the finger to right end of this object7634_finger = ptr + obj->size();7635assert(_finger > ptr, "we just incremented it above");7636// On large heaps, it may take us some time to get through7637// the marking phase (especially if running iCMS). During7638// this time it's possible that a lot of mutations have7639// accumulated in the card table and the mod union table --7640// these mutation records are redundant until we have7641// actually traced into the corresponding card.7642// Here, we check whether advancing the finger would make7643// us cross into a new card, and if so clear corresponding7644// cards in the MUT (preclean them in the card-table in the7645// future).76467647// The clean-on-enter optimization is disabled by default,7648// until we fix 6178663.7649if (CMSCleanOnEnter && (_finger > _threshold)) {7650// [_threshold, _finger) represents the interval7651// of cards to be cleared in MUT (or precleaned in card table).7652// The set of cards to be cleared is all those that overlap7653// with the interval [_threshold, _finger); note that7654// _threshold is always kept card-aligned but _finger isn't7655// always card-aligned.7656HeapWord* old_threshold = _threshold;7657assert(old_threshold == (HeapWord*)round_to(7658(intptr_t)old_threshold, CardTableModRefBS::card_size),7659"_threshold should always be card-aligned");7660_threshold = (HeapWord*)round_to(7661(intptr_t)_finger, CardTableModRefBS::card_size);7662MemRegion mr(old_threshold, _threshold);7663assert(!mr.is_empty(), "Control point invariant");7664assert(_span.contains(mr), "Should clear within span"); // _whole_span ??7665_mut->clear_range(mr);7666}76677668// Note: the local finger doesn't advance while we drain7669// the stack below, but the global finger sure can and will.7670HeapWord** gfa = _task->global_finger_addr();7671Par_PushOrMarkClosure pushOrMarkClosure(_collector,7672_span, _bit_map,7673_work_queue,7674_overflow_stack,7675_finger,7676gfa, this);7677bool res = _work_queue->push(obj); // overflow could occur here7678assert(res, "Will hold once we use workqueues");7679while (true) {7680oop new_oop;7681if (!_work_queue->pop_local(new_oop)) {7682// We emptied our work_queue; check if there's stuff that can7683// be gotten from the overflow stack.7684if (CMSConcMarkingTask::get_work_from_overflow_stack(7685_overflow_stack, _work_queue)) {7686do_yield_check();7687continue;7688} else { // done7689break;7690}7691}7692// Skip verifying header mark word below because we are7693// running concurrent with mutators.7694assert(new_oop->is_oop(true), "Oops! expected to pop an oop");7695// now scan this oop's oops7696new_oop->oop_iterate(&pushOrMarkClosure);7697do_yield_check();7698}7699assert(_work_queue->size() == 0, "tautology, emphasizing post-condition");7700}77017702// Yield in response to a request from VM Thread or7703// from mutators.7704void Par_MarkFromRootsClosure::do_yield_work() {7705assert(_task != NULL, "sanity");7706_task->yield();7707}77087709// A variant of the above used for verifying CMS marking work.7710MarkFromRootsVerifyClosure::MarkFromRootsVerifyClosure(CMSCollector* collector,7711MemRegion span,7712CMSBitMap* verification_bm, CMSBitMap* cms_bm,7713CMSMarkStack* mark_stack):7714_collector(collector),7715_span(span),7716_verification_bm(verification_bm),7717_cms_bm(cms_bm),7718_mark_stack(mark_stack),7719_pam_verify_closure(collector, span, verification_bm, cms_bm,7720mark_stack)7721{7722assert(_mark_stack->isEmpty(), "stack should be empty");7723_finger = _verification_bm->startWord();7724assert(_collector->_restart_addr == NULL, "Sanity check");7725assert(_span.contains(_finger), "Out of bounds _finger?");7726}77277728void MarkFromRootsVerifyClosure::reset(HeapWord* addr) {7729assert(_mark_stack->isEmpty(), "would cause duplicates on stack");7730assert(_span.contains(addr), "Out of bounds _finger?");7731_finger = addr;7732}77337734// Should revisit to see if this should be restructured for7735// greater efficiency.7736bool MarkFromRootsVerifyClosure::do_bit(size_t offset) {7737// convert offset into a HeapWord*7738HeapWord* addr = _verification_bm->startWord() + offset;7739assert(_verification_bm->endWord() && addr < _verification_bm->endWord(),7740"address out of range");7741assert(_verification_bm->isMarked(addr), "tautology");7742assert(_cms_bm->isMarked(addr), "tautology");77437744assert(_mark_stack->isEmpty(),7745"should drain stack to limit stack usage");7746// convert addr to an oop preparatory to scanning7747oop obj = oop(addr);7748assert(obj->is_oop(), "should be an oop");7749assert(_finger <= addr, "_finger runneth ahead");7750// advance the finger to right end of this object7751_finger = addr + obj->size();7752assert(_finger > addr, "we just incremented it above");7753// Note: the finger doesn't advance while we drain7754// the stack below.7755bool res = _mark_stack->push(obj);7756assert(res, "Empty non-zero size stack should have space for single push");7757while (!_mark_stack->isEmpty()) {7758oop new_oop = _mark_stack->pop();7759assert(new_oop->is_oop(), "Oops! expected to pop an oop");7760// now scan this oop's oops7761new_oop->oop_iterate(&_pam_verify_closure);7762}7763assert(_mark_stack->isEmpty(), "tautology, emphasizing post-condition");7764return true;7765}77667767PushAndMarkVerifyClosure::PushAndMarkVerifyClosure(7768CMSCollector* collector, MemRegion span,7769CMSBitMap* verification_bm, CMSBitMap* cms_bm,7770CMSMarkStack* mark_stack):7771MetadataAwareOopClosure(collector->ref_processor()),7772_collector(collector),7773_span(span),7774_verification_bm(verification_bm),7775_cms_bm(cms_bm),7776_mark_stack(mark_stack)7777{ }77787779void PushAndMarkVerifyClosure::do_oop(oop* p) { PushAndMarkVerifyClosure::do_oop_work(p); }7780void PushAndMarkVerifyClosure::do_oop(narrowOop* p) { PushAndMarkVerifyClosure::do_oop_work(p); }77817782// Upon stack overflow, we discard (part of) the stack,7783// remembering the least address amongst those discarded7784// in CMSCollector's _restart_address.7785void PushAndMarkVerifyClosure::handle_stack_overflow(HeapWord* lost) {7786// Remember the least grey address discarded7787HeapWord* ra = (HeapWord*)_mark_stack->least_value(lost);7788_collector->lower_restart_addr(ra);7789_mark_stack->reset(); // discard stack contents7790_mark_stack->expand(); // expand the stack if possible7791}77927793void PushAndMarkVerifyClosure::do_oop(oop obj) {7794assert(obj->is_oop_or_null(), "expected an oop or NULL");7795HeapWord* addr = (HeapWord*)obj;7796if (_span.contains(addr) && !_verification_bm->isMarked(addr)) {7797// Oop lies in _span and isn't yet grey or black7798_verification_bm->mark(addr); // now grey7799if (!_cms_bm->isMarked(addr)) {7800oop(addr)->print();7801gclog_or_tty->print_cr(" (" INTPTR_FORMAT " should have been marked)",7802addr);7803fatal("... aborting");7804}78057806if (!_mark_stack->push(obj)) { // stack overflow7807if (PrintCMSStatistics != 0) {7808gclog_or_tty->print_cr("CMS marking stack overflow (benign) at "7809SIZE_FORMAT, _mark_stack->capacity());7810}7811assert(_mark_stack->isFull(), "Else push should have succeeded");7812handle_stack_overflow(addr);7813}7814// anything including and to the right of _finger7815// will be scanned as we iterate over the remainder of the7816// bit map7817}7818}78197820PushOrMarkClosure::PushOrMarkClosure(CMSCollector* collector,7821MemRegion span,7822CMSBitMap* bitMap, CMSMarkStack* markStack,7823HeapWord* finger, MarkFromRootsClosure* parent) :7824MetadataAwareOopClosure(collector->ref_processor()),7825_collector(collector),7826_span(span),7827_bitMap(bitMap),7828_markStack(markStack),7829_finger(finger),7830_parent(parent)7831{ }78327833Par_PushOrMarkClosure::Par_PushOrMarkClosure(CMSCollector* collector,7834MemRegion span,7835CMSBitMap* bit_map,7836OopTaskQueue* work_queue,7837CMSMarkStack* overflow_stack,7838HeapWord* finger,7839HeapWord** global_finger_addr,7840Par_MarkFromRootsClosure* parent) :7841MetadataAwareOopClosure(collector->ref_processor()),7842_collector(collector),7843_whole_span(collector->_span),7844_span(span),7845_bit_map(bit_map),7846_work_queue(work_queue),7847_overflow_stack(overflow_stack),7848_finger(finger),7849_global_finger_addr(global_finger_addr),7850_parent(parent)7851{ }78527853// Assumes thread-safe access by callers, who are7854// responsible for mutual exclusion.7855void CMSCollector::lower_restart_addr(HeapWord* low) {7856assert(_span.contains(low), "Out of bounds addr");7857if (_restart_addr == NULL) {7858_restart_addr = low;7859} else {7860_restart_addr = MIN2(_restart_addr, low);7861}7862}78637864// Upon stack overflow, we discard (part of) the stack,7865// remembering the least address amongst those discarded7866// in CMSCollector's _restart_address.7867void PushOrMarkClosure::handle_stack_overflow(HeapWord* lost) {7868// Remember the least grey address discarded7869HeapWord* ra = (HeapWord*)_markStack->least_value(lost);7870_collector->lower_restart_addr(ra);7871_markStack->reset(); // discard stack contents7872_markStack->expand(); // expand the stack if possible7873}78747875// Upon stack overflow, we discard (part of) the stack,7876// remembering the least address amongst those discarded7877// in CMSCollector's _restart_address.7878void Par_PushOrMarkClosure::handle_stack_overflow(HeapWord* lost) {7879// We need to do this under a mutex to prevent other7880// workers from interfering with the work done below.7881MutexLockerEx ml(_overflow_stack->par_lock(),7882Mutex::_no_safepoint_check_flag);7883// Remember the least grey address discarded7884HeapWord* ra = (HeapWord*)_overflow_stack->least_value(lost);7885_collector->lower_restart_addr(ra);7886_overflow_stack->reset(); // discard stack contents7887_overflow_stack->expand(); // expand the stack if possible7888}78897890void PushOrMarkClosure::do_oop(oop obj) {7891// Ignore mark word because we are running concurrent with mutators.7892assert(obj->is_oop_or_null(true), "expected an oop or NULL");7893HeapWord* addr = (HeapWord*)obj;7894if (_span.contains(addr) && !_bitMap->isMarked(addr)) {7895// Oop lies in _span and isn't yet grey or black7896_bitMap->mark(addr); // now grey7897if (addr < _finger) {7898// the bit map iteration has already either passed, or7899// sampled, this bit in the bit map; we'll need to7900// use the marking stack to scan this oop's oops.7901bool simulate_overflow = false;7902NOT_PRODUCT(7903if (CMSMarkStackOverflowALot &&7904_collector->simulate_overflow()) {7905// simulate a stack overflow7906simulate_overflow = true;7907}7908)7909if (simulate_overflow || !_markStack->push(obj)) { // stack overflow7910if (PrintCMSStatistics != 0) {7911gclog_or_tty->print_cr("CMS marking stack overflow (benign) at "7912SIZE_FORMAT, _markStack->capacity());7913}7914assert(simulate_overflow || _markStack->isFull(), "Else push should have succeeded");7915handle_stack_overflow(addr);7916}7917}7918// anything including and to the right of _finger7919// will be scanned as we iterate over the remainder of the7920// bit map7921do_yield_check();7922}7923}79247925void PushOrMarkClosure::do_oop(oop* p) { PushOrMarkClosure::do_oop_work(p); }7926void PushOrMarkClosure::do_oop(narrowOop* p) { PushOrMarkClosure::do_oop_work(p); }79277928void Par_PushOrMarkClosure::do_oop(oop obj) {7929// Ignore mark word because we are running concurrent with mutators.7930assert(obj->is_oop_or_null(true), "expected an oop or NULL");7931HeapWord* addr = (HeapWord*)obj;7932if (_whole_span.contains(addr) && !_bit_map->isMarked(addr)) {7933// Oop lies in _span and isn't yet grey or black7934// We read the global_finger (volatile read) strictly after marking oop7935bool res = _bit_map->par_mark(addr); // now grey7936volatile HeapWord** gfa = (volatile HeapWord**)_global_finger_addr;7937// Should we push this marked oop on our stack?7938// -- if someone else marked it, nothing to do7939// -- if target oop is above global finger nothing to do7940// -- if target oop is in chunk and above local finger7941// then nothing to do7942// -- else push on work queue7943if ( !res // someone else marked it, they will deal with it7944|| (addr >= *gfa) // will be scanned in a later task7945|| (_span.contains(addr) && addr >= _finger)) { // later in this chunk7946return;7947}7948// the bit map iteration has already either passed, or7949// sampled, this bit in the bit map; we'll need to7950// use the marking stack to scan this oop's oops.7951bool simulate_overflow = false;7952NOT_PRODUCT(7953if (CMSMarkStackOverflowALot &&7954_collector->simulate_overflow()) {7955// simulate a stack overflow7956simulate_overflow = true;7957}7958)7959if (simulate_overflow ||7960!(_work_queue->push(obj) || _overflow_stack->par_push(obj))) {7961// stack overflow7962if (PrintCMSStatistics != 0) {7963gclog_or_tty->print_cr("CMS marking stack overflow (benign) at "7964SIZE_FORMAT, _overflow_stack->capacity());7965}7966// We cannot assert that the overflow stack is full because7967// it may have been emptied since.7968assert(simulate_overflow ||7969_work_queue->size() == _work_queue->max_elems(),7970"Else push should have succeeded");7971handle_stack_overflow(addr);7972}7973do_yield_check();7974}7975}79767977void Par_PushOrMarkClosure::do_oop(oop* p) { Par_PushOrMarkClosure::do_oop_work(p); }7978void Par_PushOrMarkClosure::do_oop(narrowOop* p) { Par_PushOrMarkClosure::do_oop_work(p); }79797980PushAndMarkClosure::PushAndMarkClosure(CMSCollector* collector,7981MemRegion span,7982ReferenceProcessor* rp,7983CMSBitMap* bit_map,7984CMSBitMap* mod_union_table,7985CMSMarkStack* mark_stack,7986bool concurrent_precleaning):7987MetadataAwareOopClosure(rp),7988_collector(collector),7989_span(span),7990_bit_map(bit_map),7991_mod_union_table(mod_union_table),7992_mark_stack(mark_stack),7993_concurrent_precleaning(concurrent_precleaning)7994{7995assert(_ref_processor != NULL, "_ref_processor shouldn't be NULL");7996}79977998// Grey object rescan during pre-cleaning and second checkpoint phases --7999// the non-parallel version (the parallel version appears further below.)8000void PushAndMarkClosure::do_oop(oop obj) {8001// Ignore mark word verification. If during concurrent precleaning,8002// the object monitor may be locked. If during the checkpoint8003// phases, the object may already have been reached by a different8004// path and may be at the end of the global overflow list (so8005// the mark word may be NULL).8006assert(obj->is_oop_or_null(true /* ignore mark word */),8007"expected an oop or NULL");8008HeapWord* addr = (HeapWord*)obj;8009// Check if oop points into the CMS generation8010// and is not marked8011if (_span.contains(addr) && !_bit_map->isMarked(addr)) {8012// a white object ...8013_bit_map->mark(addr); // ... now grey8014// push on the marking stack (grey set)8015bool simulate_overflow = false;8016NOT_PRODUCT(8017if (CMSMarkStackOverflowALot &&8018_collector->simulate_overflow()) {8019// simulate a stack overflow8020simulate_overflow = true;8021}8022)8023if (simulate_overflow || !_mark_stack->push(obj)) {8024if (_concurrent_precleaning) {8025// During precleaning we can just dirty the appropriate card(s)8026// in the mod union table, thus ensuring that the object remains8027// in the grey set and continue. In the case of object arrays8028// we need to dirty all of the cards that the object spans,8029// since the rescan of object arrays will be limited to the8030// dirty cards.8031// Note that no one can be intefering with us in this action8032// of dirtying the mod union table, so no locking or atomics8033// are required.8034if (obj->is_objArray()) {8035size_t sz = obj->size();8036HeapWord* end_card_addr = (HeapWord*)round_to(8037(intptr_t)(addr+sz), CardTableModRefBS::card_size);8038MemRegion redirty_range = MemRegion(addr, end_card_addr);8039assert(!redirty_range.is_empty(), "Arithmetical tautology");8040_mod_union_table->mark_range(redirty_range);8041} else {8042_mod_union_table->mark(addr);8043}8044_collector->_ser_pmc_preclean_ovflw++;8045} else {8046// During the remark phase, we need to remember this oop8047// in the overflow list.8048_collector->push_on_overflow_list(obj);8049_collector->_ser_pmc_remark_ovflw++;8050}8051}8052}8053}80548055Par_PushAndMarkClosure::Par_PushAndMarkClosure(CMSCollector* collector,8056MemRegion span,8057ReferenceProcessor* rp,8058CMSBitMap* bit_map,8059OopTaskQueue* work_queue):8060MetadataAwareOopClosure(rp),8061_collector(collector),8062_span(span),8063_bit_map(bit_map),8064_work_queue(work_queue)8065{8066assert(_ref_processor != NULL, "_ref_processor shouldn't be NULL");8067}80688069void PushAndMarkClosure::do_oop(oop* p) { PushAndMarkClosure::do_oop_work(p); }8070void PushAndMarkClosure::do_oop(narrowOop* p) { PushAndMarkClosure::do_oop_work(p); }80718072// Grey object rescan during second checkpoint phase --8073// the parallel version.8074void Par_PushAndMarkClosure::do_oop(oop obj) {8075// In the assert below, we ignore the mark word because8076// this oop may point to an already visited object that is8077// on the overflow stack (in which case the mark word has8078// been hijacked for chaining into the overflow stack --8079// if this is the last object in the overflow stack then8080// its mark word will be NULL). Because this object may8081// have been subsequently popped off the global overflow8082// stack, and the mark word possibly restored to the prototypical8083// value, by the time we get to examined this failing assert in8084// the debugger, is_oop_or_null(false) may subsequently start8085// to hold.8086assert(obj->is_oop_or_null(true),8087"expected an oop or NULL");8088HeapWord* addr = (HeapWord*)obj;8089// Check if oop points into the CMS generation8090// and is not marked8091if (_span.contains(addr) && !_bit_map->isMarked(addr)) {8092// a white object ...8093// If we manage to "claim" the object, by being the8094// first thread to mark it, then we push it on our8095// marking stack8096if (_bit_map->par_mark(addr)) { // ... now grey8097// push on work queue (grey set)8098bool simulate_overflow = false;8099NOT_PRODUCT(8100if (CMSMarkStackOverflowALot &&8101_collector->par_simulate_overflow()) {8102// simulate a stack overflow8103simulate_overflow = true;8104}8105)8106if (simulate_overflow || !_work_queue->push(obj)) {8107_collector->par_push_on_overflow_list(obj);8108_collector->_par_pmc_remark_ovflw++; // imprecise OK: no need to CAS8109}8110} // Else, some other thread got there first8111}8112}81138114void Par_PushAndMarkClosure::do_oop(oop* p) { Par_PushAndMarkClosure::do_oop_work(p); }8115void Par_PushAndMarkClosure::do_oop(narrowOop* p) { Par_PushAndMarkClosure::do_oop_work(p); }81168117void CMSPrecleanRefsYieldClosure::do_yield_work() {8118Mutex* bml = _collector->bitMapLock();8119assert_lock_strong(bml);8120assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),8121"CMS thread should hold CMS token");81228123bml->unlock();8124ConcurrentMarkSweepThread::desynchronize(true);81258126ConcurrentMarkSweepThread::acknowledge_yield_request();81278128_collector->stopTimer();8129GCPauseTimer p(_collector->size_policy()->concurrent_timer_ptr());8130if (PrintCMSStatistics != 0) {8131_collector->incrementYields();8132}8133_collector->icms_wait();81348135// See the comment in coordinator_yield()8136for (unsigned i = 0; i < CMSYieldSleepCount &&8137ConcurrentMarkSweepThread::should_yield() &&8138!CMSCollector::foregroundGCIsActive(); ++i) {8139os::sleep(Thread::current(), 1, false);8140ConcurrentMarkSweepThread::acknowledge_yield_request();8141}81428143ConcurrentMarkSweepThread::synchronize(true);8144bml->lock();81458146_collector->startTimer();8147}81488149bool CMSPrecleanRefsYieldClosure::should_return() {8150if (ConcurrentMarkSweepThread::should_yield()) {8151do_yield_work();8152}8153return _collector->foregroundGCIsActive();8154}81558156void MarkFromDirtyCardsClosure::do_MemRegion(MemRegion mr) {8157assert(((size_t)mr.start())%CardTableModRefBS::card_size_in_words == 0,8158"mr should be aligned to start at a card boundary");8159// We'd like to assert:8160// assert(mr.word_size()%CardTableModRefBS::card_size_in_words == 0,8161// "mr should be a range of cards");8162// However, that would be too strong in one case -- the last8163// partition ends at _unallocated_block which, in general, can be8164// an arbitrary boundary, not necessarily card aligned.8165if (PrintCMSStatistics != 0) {8166_num_dirty_cards +=8167mr.word_size()/CardTableModRefBS::card_size_in_words;8168}8169_space->object_iterate_mem(mr, &_scan_cl);8170}81718172SweepClosure::SweepClosure(CMSCollector* collector,8173ConcurrentMarkSweepGeneration* g,8174CMSBitMap* bitMap, bool should_yield) :8175_collector(collector),8176_g(g),8177_sp(g->cmsSpace()),8178_limit(_sp->sweep_limit()),8179_freelistLock(_sp->freelistLock()),8180_bitMap(bitMap),8181_yield(should_yield),8182_inFreeRange(false), // No free range at beginning of sweep8183_freeRangeInFreeLists(false), // No free range at beginning of sweep8184_lastFreeRangeCoalesced(false),8185_freeFinger(g->used_region().start())8186{8187NOT_PRODUCT(8188_numObjectsFreed = 0;8189_numWordsFreed = 0;8190_numObjectsLive = 0;8191_numWordsLive = 0;8192_numObjectsAlreadyFree = 0;8193_numWordsAlreadyFree = 0;8194_last_fc = NULL;81958196_sp->initializeIndexedFreeListArrayReturnedBytes();8197_sp->dictionary()->initialize_dict_returned_bytes();8198)8199assert(_limit >= _sp->bottom() && _limit <= _sp->end(),8200"sweep _limit out of bounds");8201if (CMSTraceSweeper) {8202gclog_or_tty->print_cr("\n====================\nStarting new sweep with limit " PTR_FORMAT,8203_limit);8204}8205}82068207void SweepClosure::print_on(outputStream* st) const {8208tty->print_cr("_sp = [" PTR_FORMAT "," PTR_FORMAT ")",8209_sp->bottom(), _sp->end());8210tty->print_cr("_limit = " PTR_FORMAT, _limit);8211tty->print_cr("_freeFinger = " PTR_FORMAT, _freeFinger);8212NOT_PRODUCT(tty->print_cr("_last_fc = " PTR_FORMAT, _last_fc);)8213tty->print_cr("_inFreeRange = %d, _freeRangeInFreeLists = %d, _lastFreeRangeCoalesced = %d",8214_inFreeRange, _freeRangeInFreeLists, _lastFreeRangeCoalesced);8215}82168217#ifndef PRODUCT8218// Assertion checking only: no useful work in product mode --8219// however, if any of the flags below become product flags,8220// you may need to review this code to see if it needs to be8221// enabled in product mode.8222SweepClosure::~SweepClosure() {8223assert_lock_strong(_freelistLock);8224assert(_limit >= _sp->bottom() && _limit <= _sp->end(),8225"sweep _limit out of bounds");8226if (inFreeRange()) {8227warning("inFreeRange() should have been reset; dumping state of SweepClosure");8228print();8229ShouldNotReachHere();8230}8231if (Verbose && PrintGC) {8232gclog_or_tty->print("Collected " SIZE_FORMAT " objects, " SIZE_FORMAT " bytes",8233_numObjectsFreed, _numWordsFreed*sizeof(HeapWord));8234gclog_or_tty->print_cr("\nLive " SIZE_FORMAT " objects, "8235SIZE_FORMAT " bytes "8236"Already free " SIZE_FORMAT " objects, " SIZE_FORMAT " bytes",8237_numObjectsLive, _numWordsLive*sizeof(HeapWord),8238_numObjectsAlreadyFree, _numWordsAlreadyFree*sizeof(HeapWord));8239size_t totalBytes = (_numWordsFreed + _numWordsLive + _numWordsAlreadyFree)8240* sizeof(HeapWord);8241gclog_or_tty->print_cr("Total sweep: " SIZE_FORMAT " bytes", totalBytes);82428243if (PrintCMSStatistics && CMSVerifyReturnedBytes) {8244size_t indexListReturnedBytes = _sp->sumIndexedFreeListArrayReturnedBytes();8245size_t dict_returned_bytes = _sp->dictionary()->sum_dict_returned_bytes();8246size_t returned_bytes = indexListReturnedBytes + dict_returned_bytes;8247gclog_or_tty->print("Returned " SIZE_FORMAT " bytes", returned_bytes);8248gclog_or_tty->print(" Indexed List Returned " SIZE_FORMAT " bytes",8249indexListReturnedBytes);8250gclog_or_tty->print_cr(" Dictionary Returned " SIZE_FORMAT " bytes",8251dict_returned_bytes);8252}8253}8254if (CMSTraceSweeper) {8255gclog_or_tty->print_cr("end of sweep with _limit = " PTR_FORMAT "\n================",8256_limit);8257}8258}8259#endif // PRODUCT82608261void SweepClosure::initialize_free_range(HeapWord* freeFinger,8262bool freeRangeInFreeLists) {8263if (CMSTraceSweeper) {8264gclog_or_tty->print("---- Start free range at 0x%x with free block (%d)\n",8265freeFinger, freeRangeInFreeLists);8266}8267assert(!inFreeRange(), "Trampling existing free range");8268set_inFreeRange(true);8269set_lastFreeRangeCoalesced(false);82708271set_freeFinger(freeFinger);8272set_freeRangeInFreeLists(freeRangeInFreeLists);8273if (CMSTestInFreeList) {8274if (freeRangeInFreeLists) {8275FreeChunk* fc = (FreeChunk*) freeFinger;8276assert(fc->is_free(), "A chunk on the free list should be free.");8277assert(fc->size() > 0, "Free range should have a size");8278assert(_sp->verify_chunk_in_free_list(fc), "Chunk is not in free lists");8279}8280}8281}82828283// Note that the sweeper runs concurrently with mutators. Thus,8284// it is possible for direct allocation in this generation to happen8285// in the middle of the sweep. Note that the sweeper also coalesces8286// contiguous free blocks. Thus, unless the sweeper and the allocator8287// synchronize appropriately freshly allocated blocks may get swept up.8288// This is accomplished by the sweeper locking the free lists while8289// it is sweeping. Thus blocks that are determined to be free are8290// indeed free. There is however one additional complication:8291// blocks that have been allocated since the final checkpoint and8292// mark, will not have been marked and so would be treated as8293// unreachable and swept up. To prevent this, the allocator marks8294// the bit map when allocating during the sweep phase. This leads,8295// however, to a further complication -- objects may have been allocated8296// but not yet initialized -- in the sense that the header isn't yet8297// installed. The sweeper can not then determine the size of the block8298// in order to skip over it. To deal with this case, we use a technique8299// (due to Printezis) to encode such uninitialized block sizes in the8300// bit map. Since the bit map uses a bit per every HeapWord, but the8301// CMS generation has a minimum object size of 3 HeapWords, it follows8302// that "normal marks" won't be adjacent in the bit map (there will8303// always be at least two 0 bits between successive 1 bits). We make use8304// of these "unused" bits to represent uninitialized blocks -- the bit8305// corresponding to the start of the uninitialized object and the next8306// bit are both set. Finally, a 1 bit marks the end of the object that8307// started with the two consecutive 1 bits to indicate its potentially8308// uninitialized state.83098310size_t SweepClosure::do_blk_careful(HeapWord* addr) {8311FreeChunk* fc = (FreeChunk*)addr;8312size_t res;83138314// Check if we are done sweeping. Below we check "addr >= _limit" rather8315// than "addr == _limit" because although _limit was a block boundary when8316// we started the sweep, it may no longer be one because heap expansion8317// may have caused us to coalesce the block ending at the address _limit8318// with a newly expanded chunk (this happens when _limit was set to the8319// previous _end of the space), so we may have stepped past _limit:8320// see the following Zeno-like trail of CRs 6977970, 7008136, 7042740.8321if (addr >= _limit) { // we have swept up to or past the limit: finish up8322assert(_limit >= _sp->bottom() && _limit <= _sp->end(),8323"sweep _limit out of bounds");8324assert(addr < _sp->end(), "addr out of bounds");8325// Flush any free range we might be holding as a single8326// coalesced chunk to the appropriate free list.8327if (inFreeRange()) {8328assert(freeFinger() >= _sp->bottom() && freeFinger() < _limit,8329err_msg("freeFinger() " PTR_FORMAT " is out-of-bounds", freeFinger()));8330flush_cur_free_chunk(freeFinger(),8331pointer_delta(addr, freeFinger()));8332if (CMSTraceSweeper) {8333gclog_or_tty->print("Sweep: last chunk: ");8334gclog_or_tty->print("put_free_blk 0x%x (" SIZE_FORMAT ") "8335"[coalesced:" SIZE_FORMAT "]\n",8336freeFinger(), pointer_delta(addr, freeFinger()),8337lastFreeRangeCoalesced());8338}8339}83408341// help the iterator loop finish8342return pointer_delta(_sp->end(), addr);8343}83448345assert(addr < _limit, "sweep invariant");8346// check if we should yield8347do_yield_check(addr);8348if (fc->is_free()) {8349// Chunk that is already free8350res = fc->size();8351do_already_free_chunk(fc);8352debug_only(_sp->verifyFreeLists());8353// If we flush the chunk at hand in lookahead_and_flush()8354// and it's coalesced with a preceding chunk, then the8355// process of "mangling" the payload of the coalesced block8356// will cause erasure of the size information from the8357// (erstwhile) header of all the coalesced blocks but the8358// first, so the first disjunct in the assert will not hold8359// in that specific case (in which case the second disjunct8360// will hold).8361assert(res == fc->size() || ((HeapWord*)fc) + res >= _limit,8362"Otherwise the size info doesn't change at this step");8363NOT_PRODUCT(8364_numObjectsAlreadyFree++;8365_numWordsAlreadyFree += res;8366)8367NOT_PRODUCT(_last_fc = fc;)8368} else if (!_bitMap->isMarked(addr)) {8369// Chunk is fresh garbage8370res = do_garbage_chunk(fc);8371debug_only(_sp->verifyFreeLists());8372NOT_PRODUCT(8373_numObjectsFreed++;8374_numWordsFreed += res;8375)8376} else {8377// Chunk that is alive.8378res = do_live_chunk(fc);8379debug_only(_sp->verifyFreeLists());8380NOT_PRODUCT(8381_numObjectsLive++;8382_numWordsLive += res;8383)8384}8385return res;8386}83878388// For the smart allocation, record following8389// split deaths - a free chunk is removed from its free list because8390// it is being split into two or more chunks.8391// split birth - a free chunk is being added to its free list because8392// a larger free chunk has been split and resulted in this free chunk.8393// coal death - a free chunk is being removed from its free list because8394// it is being coalesced into a large free chunk.8395// coal birth - a free chunk is being added to its free list because8396// it was created when two or more free chunks where coalesced into8397// this free chunk.8398//8399// These statistics are used to determine the desired number of free8400// chunks of a given size. The desired number is chosen to be relative8401// to the end of a CMS sweep. The desired number at the end of a sweep8402// is the8403// count-at-end-of-previous-sweep (an amount that was enough)8404// - count-at-beginning-of-current-sweep (the excess)8405// + split-births (gains in this size during interval)8406// - split-deaths (demands on this size during interval)8407// where the interval is from the end of one sweep to the end of the8408// next.8409//8410// When sweeping the sweeper maintains an accumulated chunk which is8411// the chunk that is made up of chunks that have been coalesced. That8412// will be termed the left-hand chunk. A new chunk of garbage that8413// is being considered for coalescing will be referred to as the8414// right-hand chunk.8415//8416// When making a decision on whether to coalesce a right-hand chunk with8417// the current left-hand chunk, the current count vs. the desired count8418// of the left-hand chunk is considered. Also if the right-hand chunk8419// is near the large chunk at the end of the heap (see8420// ConcurrentMarkSweepGeneration::isNearLargestChunk()), then the8421// left-hand chunk is coalesced.8422//8423// When making a decision about whether to split a chunk, the desired count8424// vs. the current count of the candidate to be split is also considered.8425// If the candidate is underpopulated (currently fewer chunks than desired)8426// a chunk of an overpopulated (currently more chunks than desired) size may8427// be chosen. The "hint" associated with a free list, if non-null, points8428// to a free list which may be overpopulated.8429//84308431void SweepClosure::do_already_free_chunk(FreeChunk* fc) {8432const size_t size = fc->size();8433// Chunks that cannot be coalesced are not in the8434// free lists.8435if (CMSTestInFreeList && !fc->cantCoalesce()) {8436assert(_sp->verify_chunk_in_free_list(fc),8437"free chunk should be in free lists");8438}8439// a chunk that is already free, should not have been8440// marked in the bit map8441HeapWord* const addr = (HeapWord*) fc;8442assert(!_bitMap->isMarked(addr), "free chunk should be unmarked");8443// Verify that the bit map has no bits marked between8444// addr and purported end of this block.8445_bitMap->verifyNoOneBitsInRange(addr + 1, addr + size);84468447// Some chunks cannot be coalesced under any circumstances.8448// See the definition of cantCoalesce().8449if (!fc->cantCoalesce()) {8450// This chunk can potentially be coalesced.8451if (_sp->adaptive_freelists()) {8452// All the work is done in8453do_post_free_or_garbage_chunk(fc, size);8454} else { // Not adaptive free lists8455// this is a free chunk that can potentially be coalesced by the sweeper;8456if (!inFreeRange()) {8457// if the next chunk is a free block that can't be coalesced8458// it doesn't make sense to remove this chunk from the free lists8459FreeChunk* nextChunk = (FreeChunk*)(addr + size);8460assert((HeapWord*)nextChunk <= _sp->end(), "Chunk size out of bounds?");8461if ((HeapWord*)nextChunk < _sp->end() && // There is another free chunk to the right ...8462nextChunk->is_free() && // ... which is free...8463nextChunk->cantCoalesce()) { // ... but can't be coalesced8464// nothing to do8465} else {8466// Potentially the start of a new free range:8467// Don't eagerly remove it from the free lists.8468// No need to remove it if it will just be put8469// back again. (Also from a pragmatic point of view8470// if it is a free block in a region that is beyond8471// any allocated blocks, an assertion will fail)8472// Remember the start of a free run.8473initialize_free_range(addr, true);8474// end - can coalesce with next chunk8475}8476} else {8477// the midst of a free range, we are coalescing8478print_free_block_coalesced(fc);8479if (CMSTraceSweeper) {8480gclog_or_tty->print(" -- pick up free block 0x%x (%d)\n", fc, size);8481}8482// remove it from the free lists8483_sp->removeFreeChunkFromFreeLists(fc);8484set_lastFreeRangeCoalesced(true);8485// If the chunk is being coalesced and the current free range is8486// in the free lists, remove the current free range so that it8487// will be returned to the free lists in its entirety - all8488// the coalesced pieces included.8489if (freeRangeInFreeLists()) {8490FreeChunk* ffc = (FreeChunk*) freeFinger();8491assert(ffc->size() == pointer_delta(addr, freeFinger()),8492"Size of free range is inconsistent with chunk size.");8493if (CMSTestInFreeList) {8494assert(_sp->verify_chunk_in_free_list(ffc),8495"free range is not in free lists");8496}8497_sp->removeFreeChunkFromFreeLists(ffc);8498set_freeRangeInFreeLists(false);8499}8500}8501}8502// Note that if the chunk is not coalescable (the else arm8503// below), we unconditionally flush, without needing to do8504// a "lookahead," as we do below.8505if (inFreeRange()) lookahead_and_flush(fc, size);8506} else {8507// Code path common to both original and adaptive free lists.85088509// cant coalesce with previous block; this should be treated8510// as the end of a free run if any8511if (inFreeRange()) {8512// we kicked some butt; time to pick up the garbage8513assert(freeFinger() < addr, "freeFinger points too high");8514flush_cur_free_chunk(freeFinger(), pointer_delta(addr, freeFinger()));8515}8516// else, nothing to do, just continue8517}8518}85198520size_t SweepClosure::do_garbage_chunk(FreeChunk* fc) {8521// This is a chunk of garbage. It is not in any free list.8522// Add it to a free list or let it possibly be coalesced into8523// a larger chunk.8524HeapWord* const addr = (HeapWord*) fc;8525const size_t size = CompactibleFreeListSpace::adjustObjectSize(oop(addr)->size());85268527if (_sp->adaptive_freelists()) {8528// Verify that the bit map has no bits marked between8529// addr and purported end of just dead object.8530_bitMap->verifyNoOneBitsInRange(addr + 1, addr + size);85318532do_post_free_or_garbage_chunk(fc, size);8533} else {8534if (!inFreeRange()) {8535// start of a new free range8536assert(size > 0, "A free range should have a size");8537initialize_free_range(addr, false);8538} else {8539// this will be swept up when we hit the end of the8540// free range8541if (CMSTraceSweeper) {8542gclog_or_tty->print(" -- pick up garbage 0x%x (%d) \n", fc, size);8543}8544// If the chunk is being coalesced and the current free range is8545// in the free lists, remove the current free range so that it8546// will be returned to the free lists in its entirety - all8547// the coalesced pieces included.8548if (freeRangeInFreeLists()) {8549FreeChunk* ffc = (FreeChunk*)freeFinger();8550assert(ffc->size() == pointer_delta(addr, freeFinger()),8551"Size of free range is inconsistent with chunk size.");8552if (CMSTestInFreeList) {8553assert(_sp->verify_chunk_in_free_list(ffc),8554"free range is not in free lists");8555}8556_sp->removeFreeChunkFromFreeLists(ffc);8557set_freeRangeInFreeLists(false);8558}8559set_lastFreeRangeCoalesced(true);8560}8561// this will be swept up when we hit the end of the free range85628563// Verify that the bit map has no bits marked between8564// addr and purported end of just dead object.8565_bitMap->verifyNoOneBitsInRange(addr + 1, addr + size);8566}8567assert(_limit >= addr + size,8568"A freshly garbage chunk can't possibly straddle over _limit");8569if (inFreeRange()) lookahead_and_flush(fc, size);8570return size;8571}85728573size_t SweepClosure::do_live_chunk(FreeChunk* fc) {8574HeapWord* addr = (HeapWord*) fc;8575// The sweeper has just found a live object. Return any accumulated8576// left hand chunk to the free lists.8577if (inFreeRange()) {8578assert(freeFinger() < addr, "freeFinger points too high");8579flush_cur_free_chunk(freeFinger(), pointer_delta(addr, freeFinger()));8580}85818582// This object is live: we'd normally expect this to be8583// an oop, and like to assert the following:8584// assert(oop(addr)->is_oop(), "live block should be an oop");8585// However, as we commented above, this may be an object whose8586// header hasn't yet been initialized.8587size_t size;8588assert(_bitMap->isMarked(addr), "Tautology for this control point");8589if (_bitMap->isMarked(addr + 1)) {8590// Determine the size from the bit map, rather than trying to8591// compute it from the object header.8592HeapWord* nextOneAddr = _bitMap->getNextMarkedWordAddress(addr + 2);8593size = pointer_delta(nextOneAddr + 1, addr);8594assert(size == CompactibleFreeListSpace::adjustObjectSize(size),8595"alignment problem");85968597#ifdef ASSERT8598if (oop(addr)->klass_or_null_acquire() != NULL) {8599// Ignore mark word because we are running concurrent with mutators8600assert(oop(addr)->is_oop(true), "live block should be an oop");8601assert(size ==8602CompactibleFreeListSpace::adjustObjectSize(oop(addr)->size()),8603"P-mark and computed size do not agree");8604}8605#endif86068607} else {8608// This should be an initialized object that's alive.8609assert(oop(addr)->klass_or_null_acquire() != NULL,8610"Should be an initialized object");8611// Ignore mark word because we are running concurrent with mutators8612assert(oop(addr)->is_oop(true), "live block should be an oop");8613// Verify that the bit map has no bits marked between8614// addr and purported end of this block.8615size = CompactibleFreeListSpace::adjustObjectSize(oop(addr)->size());8616assert(size >= 3, "Necessary for Printezis marks to work");8617assert(!_bitMap->isMarked(addr+1), "Tautology for this control point");8618DEBUG_ONLY(_bitMap->verifyNoOneBitsInRange(addr+2, addr+size);)8619}8620return size;8621}86228623void SweepClosure::do_post_free_or_garbage_chunk(FreeChunk* fc,8624size_t chunkSize) {8625// do_post_free_or_garbage_chunk() should only be called in the case8626// of the adaptive free list allocator.8627const bool fcInFreeLists = fc->is_free();8628assert(_sp->adaptive_freelists(), "Should only be used in this case.");8629assert((HeapWord*)fc <= _limit, "sweep invariant");8630if (CMSTestInFreeList && fcInFreeLists) {8631assert(_sp->verify_chunk_in_free_list(fc), "free chunk is not in free lists");8632}86338634if (CMSTraceSweeper) {8635gclog_or_tty->print_cr(" -- pick up another chunk at 0x%x (%d)", fc, chunkSize);8636}86378638HeapWord* const fc_addr = (HeapWord*) fc;86398640bool coalesce = false;8641const size_t left = pointer_delta(fc_addr, freeFinger());8642const size_t right = chunkSize;8643switch (FLSCoalescePolicy) {8644// numeric value forms a coalition aggressiveness metric8645case 0: { // never coalesce8646coalesce = false;8647break;8648}8649case 1: { // coalesce if left & right chunks on overpopulated lists8650coalesce = _sp->coalOverPopulated(left) &&8651_sp->coalOverPopulated(right);8652break;8653}8654case 2: { // coalesce if left chunk on overpopulated list (default)8655coalesce = _sp->coalOverPopulated(left);8656break;8657}8658case 3: { // coalesce if left OR right chunk on overpopulated list8659coalesce = _sp->coalOverPopulated(left) ||8660_sp->coalOverPopulated(right);8661break;8662}8663case 4: { // always coalesce8664coalesce = true;8665break;8666}8667default:8668ShouldNotReachHere();8669}86708671// Should the current free range be coalesced?8672// If the chunk is in a free range and either we decided to coalesce above8673// or the chunk is near the large block at the end of the heap8674// (isNearLargestChunk() returns true), then coalesce this chunk.8675const bool doCoalesce = inFreeRange()8676&& (coalesce || _g->isNearLargestChunk(fc_addr));8677if (doCoalesce) {8678// Coalesce the current free range on the left with the new8679// chunk on the right. If either is on a free list,8680// it must be removed from the list and stashed in the closure.8681if (freeRangeInFreeLists()) {8682FreeChunk* const ffc = (FreeChunk*)freeFinger();8683assert(ffc->size() == pointer_delta(fc_addr, freeFinger()),8684"Size of free range is inconsistent with chunk size.");8685if (CMSTestInFreeList) {8686assert(_sp->verify_chunk_in_free_list(ffc),8687"Chunk is not in free lists");8688}8689_sp->coalDeath(ffc->size());8690_sp->removeFreeChunkFromFreeLists(ffc);8691set_freeRangeInFreeLists(false);8692}8693if (fcInFreeLists) {8694_sp->coalDeath(chunkSize);8695assert(fc->size() == chunkSize,8696"The chunk has the wrong size or is not in the free lists");8697_sp->removeFreeChunkFromFreeLists(fc);8698}8699set_lastFreeRangeCoalesced(true);8700print_free_block_coalesced(fc);8701} else { // not in a free range and/or should not coalesce8702// Return the current free range and start a new one.8703if (inFreeRange()) {8704// In a free range but cannot coalesce with the right hand chunk.8705// Put the current free range into the free lists.8706flush_cur_free_chunk(freeFinger(),8707pointer_delta(fc_addr, freeFinger()));8708}8709// Set up for new free range. Pass along whether the right hand8710// chunk is in the free lists.8711initialize_free_range((HeapWord*)fc, fcInFreeLists);8712}8713}87148715// Lookahead flush:8716// If we are tracking a free range, and this is the last chunk that8717// we'll look at because its end crosses past _limit, we'll preemptively8718// flush it along with any free range we may be holding on to. Note that8719// this can be the case only for an already free or freshly garbage8720// chunk. If this block is an object, it can never straddle8721// over _limit. The "straddling" occurs when _limit is set at8722// the previous end of the space when this cycle started, and8723// a subsequent heap expansion caused the previously co-terminal8724// free block to be coalesced with the newly expanded portion,8725// thus rendering _limit a non-block-boundary making it dangerous8726// for the sweeper to step over and examine.8727void SweepClosure::lookahead_and_flush(FreeChunk* fc, size_t chunk_size) {8728assert(inFreeRange(), "Should only be called if currently in a free range.");8729HeapWord* const eob = ((HeapWord*)fc) + chunk_size;8730assert(_sp->used_region().contains(eob - 1),8731err_msg("eob = " PTR_FORMAT " eob-1 = " PTR_FORMAT " _limit = " PTR_FORMAT8732" out of bounds wrt _sp = [" PTR_FORMAT "," PTR_FORMAT ")"8733" when examining fc = " PTR_FORMAT "(" SIZE_FORMAT ")",8734eob, eob-1, _limit, _sp->bottom(), _sp->end(), fc, chunk_size));8735if (eob >= _limit) {8736assert(eob == _limit || fc->is_free(), "Only a free chunk should allow us to cross over the limit");8737if (CMSTraceSweeper) {8738gclog_or_tty->print_cr("_limit " PTR_FORMAT " reached or crossed by block "8739"[" PTR_FORMAT "," PTR_FORMAT ") in space "8740"[" PTR_FORMAT "," PTR_FORMAT ")",8741_limit, fc, eob, _sp->bottom(), _sp->end());8742}8743// Return the storage we are tracking back into the free lists.8744if (CMSTraceSweeper) {8745gclog_or_tty->print_cr("Flushing ... ");8746}8747assert(freeFinger() < eob, "Error");8748flush_cur_free_chunk( freeFinger(), pointer_delta(eob, freeFinger()));8749}8750}87518752void SweepClosure::flush_cur_free_chunk(HeapWord* chunk, size_t size) {8753assert(inFreeRange(), "Should only be called if currently in a free range.");8754assert(size > 0,8755"A zero sized chunk cannot be added to the free lists.");8756if (!freeRangeInFreeLists()) {8757if (CMSTestInFreeList) {8758FreeChunk* fc = (FreeChunk*) chunk;8759fc->set_size(size);8760assert(!_sp->verify_chunk_in_free_list(fc),8761"chunk should not be in free lists yet");8762}8763if (CMSTraceSweeper) {8764gclog_or_tty->print_cr(" -- add free block 0x%x (%d) to free lists",8765chunk, size);8766}8767// A new free range is going to be starting. The current8768// free range has not been added to the free lists yet or8769// was removed so add it back.8770// If the current free range was coalesced, then the death8771// of the free range was recorded. Record a birth now.8772if (lastFreeRangeCoalesced()) {8773_sp->coalBirth(size);8774}8775_sp->addChunkAndRepairOffsetTable(chunk, size,8776lastFreeRangeCoalesced());8777} else if (CMSTraceSweeper) {8778gclog_or_tty->print_cr("Already in free list: nothing to flush");8779}8780set_inFreeRange(false);8781set_freeRangeInFreeLists(false);8782}87838784// We take a break if we've been at this for a while,8785// so as to avoid monopolizing the locks involved.8786void SweepClosure::do_yield_work(HeapWord* addr) {8787// Return current free chunk being used for coalescing (if any)8788// to the appropriate freelist. After yielding, the next8789// free block encountered will start a coalescing range of8790// free blocks. If the next free block is adjacent to the8791// chunk just flushed, they will need to wait for the next8792// sweep to be coalesced.8793if (inFreeRange()) {8794flush_cur_free_chunk(freeFinger(), pointer_delta(addr, freeFinger()));8795}87968797// First give up the locks, then yield, then re-lock.8798// We should probably use a constructor/destructor idiom to8799// do this unlock/lock or modify the MutexUnlocker class to8800// serve our purpose. XXX8801assert_lock_strong(_bitMap->lock());8802assert_lock_strong(_freelistLock);8803assert(ConcurrentMarkSweepThread::cms_thread_has_cms_token(),8804"CMS thread should hold CMS token");8805_bitMap->lock()->unlock();8806_freelistLock->unlock();8807ConcurrentMarkSweepThread::desynchronize(true);8808ConcurrentMarkSweepThread::acknowledge_yield_request();8809_collector->stopTimer();8810GCPauseTimer p(_collector->size_policy()->concurrent_timer_ptr());8811if (PrintCMSStatistics != 0) {8812_collector->incrementYields();8813}8814_collector->icms_wait();88158816// See the comment in coordinator_yield()8817for (unsigned i = 0; i < CMSYieldSleepCount &&8818ConcurrentMarkSweepThread::should_yield() &&8819!CMSCollector::foregroundGCIsActive(); ++i) {8820os::sleep(Thread::current(), 1, false);8821ConcurrentMarkSweepThread::acknowledge_yield_request();8822}88238824ConcurrentMarkSweepThread::synchronize(true);8825_freelistLock->lock();8826_bitMap->lock()->lock_without_safepoint_check();8827_collector->startTimer();8828}88298830#ifndef PRODUCT8831// This is actually very useful in a product build if it can8832// be called from the debugger. Compile it into the product8833// as needed.8834bool debug_verify_chunk_in_free_list(FreeChunk* fc) {8835return debug_cms_space->verify_chunk_in_free_list(fc);8836}8837#endif88388839void SweepClosure::print_free_block_coalesced(FreeChunk* fc) const {8840if (CMSTraceSweeper) {8841gclog_or_tty->print_cr("Sweep:coal_free_blk " PTR_FORMAT " (" SIZE_FORMAT ")",8842fc, fc->size());8843}8844}88458846// CMSIsAliveClosure8847bool CMSIsAliveClosure::do_object_b(oop obj) {8848HeapWord* addr = (HeapWord*)obj;8849return addr != NULL &&8850(!_span.contains(addr) || _bit_map->isMarked(addr));8851}885288538854CMSKeepAliveClosure::CMSKeepAliveClosure( CMSCollector* collector,8855MemRegion span,8856CMSBitMap* bit_map, CMSMarkStack* mark_stack,8857bool cpc):8858_collector(collector),8859_span(span),8860_bit_map(bit_map),8861_mark_stack(mark_stack),8862_concurrent_precleaning(cpc) {8863assert(!_span.is_empty(), "Empty span could spell trouble");8864}886588668867// CMSKeepAliveClosure: the serial version8868void CMSKeepAliveClosure::do_oop(oop obj) {8869HeapWord* addr = (HeapWord*)obj;8870if (_span.contains(addr) &&8871!_bit_map->isMarked(addr)) {8872_bit_map->mark(addr);8873bool simulate_overflow = false;8874NOT_PRODUCT(8875if (CMSMarkStackOverflowALot &&8876_collector->simulate_overflow()) {8877// simulate a stack overflow8878simulate_overflow = true;8879}8880)8881if (simulate_overflow || !_mark_stack->push(obj)) {8882if (_concurrent_precleaning) {8883// We dirty the overflown object and let the remark8884// phase deal with it.8885assert(_collector->overflow_list_is_empty(), "Error");8886// In the case of object arrays, we need to dirty all of8887// the cards that the object spans. No locking or atomics8888// are needed since no one else can be mutating the mod union8889// table.8890if (obj->is_objArray()) {8891size_t sz = obj->size();8892HeapWord* end_card_addr =8893(HeapWord*)round_to((intptr_t)(addr+sz), CardTableModRefBS::card_size);8894MemRegion redirty_range = MemRegion(addr, end_card_addr);8895assert(!redirty_range.is_empty(), "Arithmetical tautology");8896_collector->_modUnionTable.mark_range(redirty_range);8897} else {8898_collector->_modUnionTable.mark(addr);8899}8900_collector->_ser_kac_preclean_ovflw++;8901} else {8902_collector->push_on_overflow_list(obj);8903_collector->_ser_kac_ovflw++;8904}8905}8906}8907}89088909void CMSKeepAliveClosure::do_oop(oop* p) { CMSKeepAliveClosure::do_oop_work(p); }8910void CMSKeepAliveClosure::do_oop(narrowOop* p) { CMSKeepAliveClosure::do_oop_work(p); }89118912// CMSParKeepAliveClosure: a parallel version of the above.8913// The work queues are private to each closure (thread),8914// but (may be) available for stealing by other threads.8915void CMSParKeepAliveClosure::do_oop(oop obj) {8916HeapWord* addr = (HeapWord*)obj;8917if (_span.contains(addr) &&8918!_bit_map->isMarked(addr)) {8919// In general, during recursive tracing, several threads8920// may be concurrently getting here; the first one to8921// "tag" it, claims it.8922if (_bit_map->par_mark(addr)) {8923bool res = _work_queue->push(obj);8924assert(res, "Low water mark should be much less than capacity");8925// Do a recursive trim in the hope that this will keep8926// stack usage lower, but leave some oops for potential stealers8927trim_queue(_low_water_mark);8928} // Else, another thread got there first8929}8930}89318932void CMSParKeepAliveClosure::do_oop(oop* p) { CMSParKeepAliveClosure::do_oop_work(p); }8933void CMSParKeepAliveClosure::do_oop(narrowOop* p) { CMSParKeepAliveClosure::do_oop_work(p); }89348935void CMSParKeepAliveClosure::trim_queue(uint max) {8936while (_work_queue->size() > max) {8937oop new_oop;8938if (_work_queue->pop_local(new_oop)) {8939assert(new_oop != NULL && new_oop->is_oop(), "Expected an oop");8940assert(_bit_map->isMarked((HeapWord*)new_oop),8941"no white objects on this stack!");8942assert(_span.contains((HeapWord*)new_oop), "Out of bounds oop");8943// iterate over the oops in this oop, marking and pushing8944// the ones in CMS heap (i.e. in _span).8945new_oop->oop_iterate(&_mark_and_push);8946}8947}8948}89498950CMSInnerParMarkAndPushClosure::CMSInnerParMarkAndPushClosure(8951CMSCollector* collector,8952MemRegion span, CMSBitMap* bit_map,8953OopTaskQueue* work_queue):8954_collector(collector),8955_span(span),8956_bit_map(bit_map),8957_work_queue(work_queue) { }89588959void CMSInnerParMarkAndPushClosure::do_oop(oop obj) {8960HeapWord* addr = (HeapWord*)obj;8961if (_span.contains(addr) &&8962!_bit_map->isMarked(addr)) {8963if (_bit_map->par_mark(addr)) {8964bool simulate_overflow = false;8965NOT_PRODUCT(8966if (CMSMarkStackOverflowALot &&8967_collector->par_simulate_overflow()) {8968// simulate a stack overflow8969simulate_overflow = true;8970}8971)8972if (simulate_overflow || !_work_queue->push(obj)) {8973_collector->par_push_on_overflow_list(obj);8974_collector->_par_kac_ovflw++;8975}8976} // Else another thread got there already8977}8978}89798980void CMSInnerParMarkAndPushClosure::do_oop(oop* p) { CMSInnerParMarkAndPushClosure::do_oop_work(p); }8981void CMSInnerParMarkAndPushClosure::do_oop(narrowOop* p) { CMSInnerParMarkAndPushClosure::do_oop_work(p); }89828983//////////////////////////////////////////////////////////////////8984// CMSExpansionCause /////////////////////////////8985//////////////////////////////////////////////////////////////////8986const char* CMSExpansionCause::to_string(CMSExpansionCause::Cause cause) {8987switch (cause) {8988case _no_expansion:8989return "No expansion";8990case _satisfy_free_ratio:8991return "Free ratio";8992case _satisfy_promotion:8993return "Satisfy promotion";8994case _satisfy_allocation:8995return "allocation";8996case _allocate_par_lab:8997return "Par LAB";8998case _allocate_par_spooling_space:8999return "Par Spooling Space";9000case _adaptive_size_policy:9001return "Ergonomics";9002default:9003return "unknown";9004}9005}90069007void CMSDrainMarkingStackClosure::do_void() {9008// the max number to take from overflow list at a time9009const size_t num = _mark_stack->capacity()/4;9010assert(!_concurrent_precleaning || _collector->overflow_list_is_empty(),9011"Overflow list should be NULL during concurrent phases");9012while (!_mark_stack->isEmpty() ||9013// if stack is empty, check the overflow list9014_collector->take_from_overflow_list(num, _mark_stack)) {9015oop obj = _mark_stack->pop();9016HeapWord* addr = (HeapWord*)obj;9017assert(_span.contains(addr), "Should be within span");9018assert(_bit_map->isMarked(addr), "Should be marked");9019assert(obj->is_oop(), "Should be an oop");9020obj->oop_iterate(_keep_alive);9021}9022}90239024void CMSParDrainMarkingStackClosure::do_void() {9025// drain queue9026trim_queue(0);9027}90289029// Trim our work_queue so its length is below max at return9030void CMSParDrainMarkingStackClosure::trim_queue(uint max) {9031while (_work_queue->size() > max) {9032oop new_oop;9033if (_work_queue->pop_local(new_oop)) {9034assert(new_oop->is_oop(), "Expected an oop");9035assert(_bit_map->isMarked((HeapWord*)new_oop),9036"no white objects on this stack!");9037assert(_span.contains((HeapWord*)new_oop), "Out of bounds oop");9038// iterate over the oops in this oop, marking and pushing9039// the ones in CMS heap (i.e. in _span).9040new_oop->oop_iterate(&_mark_and_push);9041}9042}9043}90449045////////////////////////////////////////////////////////////////////9046// Support for Marking Stack Overflow list handling and related code9047////////////////////////////////////////////////////////////////////9048// Much of the following code is similar in shape and spirit to the9049// code used in ParNewGC. We should try and share that code9050// as much as possible in the future.90519052#ifndef PRODUCT9053// Debugging support for CMSStackOverflowALot90549055// It's OK to call this multi-threaded; the worst thing9056// that can happen is that we'll get a bunch of closely9057// spaced simulated oveflows, but that's OK, in fact9058// probably good as it would exercise the overflow code9059// under contention.9060bool CMSCollector::simulate_overflow() {9061if (_overflow_counter-- <= 0) { // just being defensive9062_overflow_counter = CMSMarkStackOverflowInterval;9063return true;9064} else {9065return false;9066}9067}90689069bool CMSCollector::par_simulate_overflow() {9070return simulate_overflow();9071}9072#endif90739074// Single-threaded9075bool CMSCollector::take_from_overflow_list(size_t num, CMSMarkStack* stack) {9076assert(stack->isEmpty(), "Expected precondition");9077assert(stack->capacity() > num, "Shouldn't bite more than can chew");9078size_t i = num;9079oop cur = _overflow_list;9080const markOop proto = markOopDesc::prototype();9081NOT_PRODUCT(ssize_t n = 0;)9082for (oop next; i > 0 && cur != NULL; cur = next, i--) {9083next = oop(cur->mark());9084cur->set_mark(proto); // until proven otherwise9085assert(cur->is_oop(), "Should be an oop");9086bool res = stack->push(cur);9087assert(res, "Bit off more than can chew?");9088NOT_PRODUCT(n++;)9089}9090_overflow_list = cur;9091#ifndef PRODUCT9092assert(_num_par_pushes >= n, "Too many pops?");9093_num_par_pushes -=n;9094#endif9095return !stack->isEmpty();9096}90979098#define BUSY (cast_to_oop<intptr_t>(0x1aff1aff))9099// (MT-safe) Get a prefix of at most "num" from the list.9100// The overflow list is chained through the mark word of9101// each object in the list. We fetch the entire list,9102// break off a prefix of the right size and return the9103// remainder. If other threads try to take objects from9104// the overflow list at that time, they will wait for9105// some time to see if data becomes available. If (and9106// only if) another thread places one or more object(s)9107// on the global list before we have returned the suffix9108// to the global list, we will walk down our local list9109// to find its end and append the global list to9110// our suffix before returning it. This suffix walk can9111// prove to be expensive (quadratic in the amount of traffic)9112// when there are many objects in the overflow list and9113// there is much producer-consumer contention on the list.9114// *NOTE*: The overflow list manipulation code here and9115// in ParNewGeneration:: are very similar in shape,9116// except that in the ParNew case we use the old (from/eden)9117// copy of the object to thread the list via its klass word.9118// Because of the common code, if you make any changes in9119// the code below, please check the ParNew version to see if9120// similar changes might be needed.9121// CR 6797058 has been filed to consolidate the common code.9122bool CMSCollector::par_take_from_overflow_list(size_t num,9123OopTaskQueue* work_q,9124int no_of_gc_threads) {9125assert(work_q->size() == 0, "First empty local work queue");9126assert(num < work_q->max_elems(), "Can't bite more than we can chew");9127if (_overflow_list == NULL) {9128return false;9129}9130// Grab the entire list; we'll put back a suffix9131oop prefix = cast_to_oop(Atomic::xchg_ptr(BUSY, &_overflow_list));9132Thread* tid = Thread::current();9133// Before "no_of_gc_threads" was introduced CMSOverflowSpinCount was9134// set to ParallelGCThreads.9135size_t CMSOverflowSpinCount = (size_t) no_of_gc_threads; // was ParallelGCThreads;9136size_t sleep_time_millis = MAX2((size_t)1, num/100);9137// If the list is busy, we spin for a short while,9138// sleeping between attempts to get the list.9139for (size_t spin = 0; prefix == BUSY && spin < CMSOverflowSpinCount; spin++) {9140os::sleep(tid, sleep_time_millis, false);9141if (_overflow_list == NULL) {9142// Nothing left to take9143return false;9144} else if (_overflow_list != BUSY) {9145// Try and grab the prefix9146prefix = cast_to_oop(Atomic::xchg_ptr(BUSY, &_overflow_list));9147}9148}9149// If the list was found to be empty, or we spun long9150// enough, we give up and return empty-handed. If we leave9151// the list in the BUSY state below, it must be the case that9152// some other thread holds the overflow list and will set it9153// to a non-BUSY state in the future.9154if (prefix == NULL || prefix == BUSY) {9155// Nothing to take or waited long enough9156if (prefix == NULL) {9157// Write back the NULL in case we overwrote it with BUSY above9158// and it is still the same value.9159(void) Atomic::cmpxchg_ptr(NULL, &_overflow_list, BUSY);9160}9161return false;9162}9163assert(prefix != NULL && prefix != BUSY, "Error");9164size_t i = num;9165oop cur = prefix;9166// Walk down the first "num" objects, unless we reach the end.9167for (; i > 1 && cur->mark() != NULL; cur = oop(cur->mark()), i--);9168if (cur->mark() == NULL) {9169// We have "num" or fewer elements in the list, so there9170// is nothing to return to the global list.9171// Write back the NULL in lieu of the BUSY we wrote9172// above, if it is still the same value.9173if (_overflow_list == BUSY) {9174(void) Atomic::cmpxchg_ptr(NULL, &_overflow_list, BUSY);9175}9176} else {9177// Chop off the suffix and rerturn it to the global list.9178assert(cur->mark() != BUSY, "Error");9179oop suffix_head = cur->mark(); // suffix will be put back on global list9180cur->set_mark(NULL); // break off suffix9181// It's possible that the list is still in the empty(busy) state9182// we left it in a short while ago; in that case we may be9183// able to place back the suffix without incurring the cost9184// of a walk down the list.9185oop observed_overflow_list = _overflow_list;9186oop cur_overflow_list = observed_overflow_list;9187bool attached = false;9188while (observed_overflow_list == BUSY || observed_overflow_list == NULL) {9189observed_overflow_list =9190(oop) Atomic::cmpxchg_ptr(suffix_head, &_overflow_list, cur_overflow_list);9191if (cur_overflow_list == observed_overflow_list) {9192attached = true;9193break;9194} else cur_overflow_list = observed_overflow_list;9195}9196if (!attached) {9197// Too bad, someone else sneaked in (at least) an element; we'll need9198// to do a splice. Find tail of suffix so we can prepend suffix to global9199// list.9200for (cur = suffix_head; cur->mark() != NULL; cur = (oop)(cur->mark()));9201oop suffix_tail = cur;9202assert(suffix_tail != NULL && suffix_tail->mark() == NULL,9203"Tautology");9204observed_overflow_list = _overflow_list;9205do {9206cur_overflow_list = observed_overflow_list;9207if (cur_overflow_list != BUSY) {9208// Do the splice ...9209suffix_tail->set_mark(markOop(cur_overflow_list));9210} else { // cur_overflow_list == BUSY9211suffix_tail->set_mark(NULL);9212}9213// ... and try to place spliced list back on overflow_list ...9214observed_overflow_list =9215(oop) Atomic::cmpxchg_ptr(suffix_head, &_overflow_list, cur_overflow_list);9216} while (cur_overflow_list != observed_overflow_list);9217// ... until we have succeeded in doing so.9218}9219}92209221// Push the prefix elements on work_q9222assert(prefix != NULL, "control point invariant");9223const markOop proto = markOopDesc::prototype();9224oop next;9225NOT_PRODUCT(ssize_t n = 0;)9226for (cur = prefix; cur != NULL; cur = next) {9227next = oop(cur->mark());9228cur->set_mark(proto); // until proven otherwise9229assert(cur->is_oop(), "Should be an oop");9230bool res = work_q->push(cur);9231assert(res, "Bit off more than we can chew?");9232NOT_PRODUCT(n++;)9233}9234#ifndef PRODUCT9235assert(_num_par_pushes >= n, "Too many pops?");9236Atomic::add_ptr(-(intptr_t)n, &_num_par_pushes);9237#endif9238return true;9239}92409241// Single-threaded9242void CMSCollector::push_on_overflow_list(oop p) {9243NOT_PRODUCT(_num_par_pushes++;)9244assert(p->is_oop(), "Not an oop");9245preserve_mark_if_necessary(p);9246p->set_mark((markOop)_overflow_list);9247_overflow_list = p;9248}92499250// Multi-threaded; use CAS to prepend to overflow list9251void CMSCollector::par_push_on_overflow_list(oop p) {9252NOT_PRODUCT(Atomic::inc_ptr(&_num_par_pushes);)9253assert(p->is_oop(), "Not an oop");9254par_preserve_mark_if_necessary(p);9255oop observed_overflow_list = _overflow_list;9256oop cur_overflow_list;9257do {9258cur_overflow_list = observed_overflow_list;9259if (cur_overflow_list != BUSY) {9260p->set_mark(markOop(cur_overflow_list));9261} else {9262p->set_mark(NULL);9263}9264observed_overflow_list =9265(oop) Atomic::cmpxchg_ptr(p, &_overflow_list, cur_overflow_list);9266} while (cur_overflow_list != observed_overflow_list);9267}9268#undef BUSY92699270// Single threaded9271// General Note on GrowableArray: pushes may silently fail9272// because we are (temporarily) out of C-heap for expanding9273// the stack. The problem is quite ubiquitous and affects9274// a lot of code in the JVM. The prudent thing for GrowableArray9275// to do (for now) is to exit with an error. However, that may9276// be too draconian in some cases because the caller may be9277// able to recover without much harm. For such cases, we9278// should probably introduce a "soft_push" method which returns9279// an indication of success or failure with the assumption that9280// the caller may be able to recover from a failure; code in9281// the VM can then be changed, incrementally, to deal with such9282// failures where possible, thus, incrementally hardening the VM9283// in such low resource situations.9284void CMSCollector::preserve_mark_work(oop p, markOop m) {9285_preserved_oop_stack.push(p);9286_preserved_mark_stack.push(m);9287assert(m == p->mark(), "Mark word changed");9288assert(_preserved_oop_stack.size() == _preserved_mark_stack.size(),9289"bijection");9290}92919292// Single threaded9293void CMSCollector::preserve_mark_if_necessary(oop p) {9294markOop m = p->mark();9295if (m->must_be_preserved(p)) {9296preserve_mark_work(p, m);9297}9298}92999300void CMSCollector::par_preserve_mark_if_necessary(oop p) {9301markOop m = p->mark();9302if (m->must_be_preserved(p)) {9303MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);9304// Even though we read the mark word without holding9305// the lock, we are assured that it will not change9306// because we "own" this oop, so no other thread can9307// be trying to push it on the overflow list; see9308// the assertion in preserve_mark_work() that checks9309// that m == p->mark().9310preserve_mark_work(p, m);9311}9312}93139314// We should be able to do this multi-threaded,9315// a chunk of stack being a task (this is9316// correct because each oop only ever appears9317// once in the overflow list. However, it's9318// not very easy to completely overlap this with9319// other operations, so will generally not be done9320// until all work's been completed. Because we9321// expect the preserved oop stack (set) to be small,9322// it's probably fine to do this single-threaded.9323// We can explore cleverer concurrent/overlapped/parallel9324// processing of preserved marks if we feel the9325// need for this in the future. Stack overflow should9326// be so rare in practice and, when it happens, its9327// effect on performance so great that this will9328// likely just be in the noise anyway.9329void CMSCollector::restore_preserved_marks_if_any() {9330assert(SafepointSynchronize::is_at_safepoint(),9331"world should be stopped");9332assert(Thread::current()->is_ConcurrentGC_thread() ||9333Thread::current()->is_VM_thread(),9334"should be single-threaded");9335assert(_preserved_oop_stack.size() == _preserved_mark_stack.size(),9336"bijection");93379338while (!_preserved_oop_stack.is_empty()) {9339oop p = _preserved_oop_stack.pop();9340assert(p->is_oop(), "Should be an oop");9341assert(_span.contains(p), "oop should be in _span");9342assert(p->mark() == markOopDesc::prototype(),9343"Set when taken from overflow list");9344markOop m = _preserved_mark_stack.pop();9345p->set_mark(m);9346}9347assert(_preserved_mark_stack.is_empty() && _preserved_oop_stack.is_empty(),9348"stacks were cleared above");9349}93509351#ifndef PRODUCT9352bool CMSCollector::no_preserved_marks() const {9353return _preserved_mark_stack.is_empty() && _preserved_oop_stack.is_empty();9354}9355#endif93569357CMSAdaptiveSizePolicy* ASConcurrentMarkSweepGeneration::cms_size_policy() const9358{9359GenCollectedHeap* gch = (GenCollectedHeap*) GenCollectedHeap::heap();9360CMSAdaptiveSizePolicy* size_policy =9361(CMSAdaptiveSizePolicy*) gch->gen_policy()->size_policy();9362assert(size_policy->is_gc_cms_adaptive_size_policy(),9363"Wrong type for size policy");9364return size_policy;9365}93669367void ASConcurrentMarkSweepGeneration::resize(size_t cur_promo_size,9368size_t desired_promo_size) {9369if (cur_promo_size < desired_promo_size) {9370size_t expand_bytes = desired_promo_size - cur_promo_size;9371if (PrintAdaptiveSizePolicy && Verbose) {9372gclog_or_tty->print_cr(" ASConcurrentMarkSweepGeneration::resize "9373"Expanding tenured generation by " SIZE_FORMAT " (bytes)",9374expand_bytes);9375}9376expand(expand_bytes,9377MinHeapDeltaBytes,9378CMSExpansionCause::_adaptive_size_policy);9379} else if (desired_promo_size < cur_promo_size) {9380size_t shrink_bytes = cur_promo_size - desired_promo_size;9381if (PrintAdaptiveSizePolicy && Verbose) {9382gclog_or_tty->print_cr(" ASConcurrentMarkSweepGeneration::resize "9383"Shrinking tenured generation by " SIZE_FORMAT " (bytes)",9384shrink_bytes);9385}9386shrink(shrink_bytes);9387}9388}93899390CMSGCAdaptivePolicyCounters* ASConcurrentMarkSweepGeneration::gc_adaptive_policy_counters() {9391GenCollectedHeap* gch = GenCollectedHeap::heap();9392CMSGCAdaptivePolicyCounters* counters =9393(CMSGCAdaptivePolicyCounters*) gch->collector_policy()->counters();9394assert(counters->kind() == GCPolicyCounters::CMSGCAdaptivePolicyCountersKind,9395"Wrong kind of counters");9396return counters;9397}939893999400void ASConcurrentMarkSweepGeneration::update_counters() {9401if (UsePerfData) {9402_space_counters->update_all();9403_gen_counters->update_all();9404CMSGCAdaptivePolicyCounters* counters = gc_adaptive_policy_counters();9405GenCollectedHeap* gch = GenCollectedHeap::heap();9406CMSGCStats* gc_stats_l = (CMSGCStats*) gc_stats();9407assert(gc_stats_l->kind() == GCStats::CMSGCStatsKind,9408"Wrong gc statistics type");9409counters->update_counters(gc_stats_l);9410}9411}94129413void ASConcurrentMarkSweepGeneration::update_counters(size_t used) {9414if (UsePerfData) {9415_space_counters->update_used(used);9416_space_counters->update_capacity();9417_gen_counters->update_all();94189419CMSGCAdaptivePolicyCounters* counters = gc_adaptive_policy_counters();9420GenCollectedHeap* gch = GenCollectedHeap::heap();9421CMSGCStats* gc_stats_l = (CMSGCStats*) gc_stats();9422assert(gc_stats_l->kind() == GCStats::CMSGCStatsKind,9423"Wrong gc statistics type");9424counters->update_counters(gc_stats_l);9425}9426}94279428void ASConcurrentMarkSweepGeneration::shrink_by(size_t desired_bytes) {9429assert_locked_or_safepoint(Heap_lock);9430assert_lock_strong(freelistLock());9431HeapWord* old_end = _cmsSpace->end();9432HeapWord* unallocated_start = _cmsSpace->unallocated_block();9433assert(old_end >= unallocated_start, "Miscalculation of unallocated_start");9434FreeChunk* chunk_at_end = find_chunk_at_end();9435if (chunk_at_end == NULL) {9436// No room to shrink9437if (PrintGCDetails && Verbose) {9438gclog_or_tty->print_cr("No room to shrink: old_end "9439PTR_FORMAT " unallocated_start " PTR_FORMAT9440" chunk_at_end " PTR_FORMAT,9441old_end, unallocated_start, chunk_at_end);9442}9443return;9444} else {94459446// Find the chunk at the end of the space and determine9447// how much it can be shrunk.9448size_t shrinkable_size_in_bytes = chunk_at_end->size();9449size_t aligned_shrinkable_size_in_bytes =9450align_size_down(shrinkable_size_in_bytes, os::vm_page_size());9451assert(unallocated_start <= (HeapWord*) chunk_at_end->end(),9452"Inconsistent chunk at end of space");9453size_t bytes = MIN2(desired_bytes, aligned_shrinkable_size_in_bytes);9454size_t word_size_before = heap_word_size(_virtual_space.committed_size());94559456// Shrink the underlying space9457_virtual_space.shrink_by(bytes);9458if (PrintGCDetails && Verbose) {9459gclog_or_tty->print_cr("ConcurrentMarkSweepGeneration::shrink_by:"9460" desired_bytes " SIZE_FORMAT9461" shrinkable_size_in_bytes " SIZE_FORMAT9462" aligned_shrinkable_size_in_bytes " SIZE_FORMAT9463" bytes " SIZE_FORMAT,9464desired_bytes, shrinkable_size_in_bytes,9465aligned_shrinkable_size_in_bytes, bytes);9466gclog_or_tty->print_cr(" old_end " SIZE_FORMAT9467" unallocated_start " SIZE_FORMAT,9468old_end, unallocated_start);9469}94709471// If the space did shrink (shrinking is not guaranteed),9472// shrink the chunk at the end by the appropriate amount.9473if (((HeapWord*)_virtual_space.high()) < old_end) {9474size_t new_word_size =9475heap_word_size(_virtual_space.committed_size());94769477// Have to remove the chunk from the dictionary because it is changing9478// size and might be someplace elsewhere in the dictionary.94799480// Get the chunk at end, shrink it, and put it9481// back.9482_cmsSpace->removeChunkFromDictionary(chunk_at_end);9483size_t word_size_change = word_size_before - new_word_size;9484size_t chunk_at_end_old_size = chunk_at_end->size();9485assert(chunk_at_end_old_size >= word_size_change,9486"Shrink is too large");9487chunk_at_end->set_size(chunk_at_end_old_size -9488word_size_change);9489_cmsSpace->freed((HeapWord*) chunk_at_end->end(),9490word_size_change);94919492_cmsSpace->returnChunkToDictionary(chunk_at_end);94939494MemRegion mr(_cmsSpace->bottom(), new_word_size);9495_bts->resize(new_word_size); // resize the block offset shared array9496Universe::heap()->barrier_set()->resize_covered_region(mr);9497_cmsSpace->assert_locked();9498_cmsSpace->set_end((HeapWord*)_virtual_space.high());94999500NOT_PRODUCT(_cmsSpace->dictionary()->verify());95019502// update the space and generation capacity counters9503if (UsePerfData) {9504_space_counters->update_capacity();9505_gen_counters->update_all();9506}95079508if (Verbose && PrintGCDetails) {9509size_t new_mem_size = _virtual_space.committed_size();9510size_t old_mem_size = new_mem_size + bytes;9511gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K by " SIZE_FORMAT "K to " SIZE_FORMAT "K",9512name(), old_mem_size/K, bytes/K, new_mem_size/K);9513}9514}95159516assert(_cmsSpace->unallocated_block() <= _cmsSpace->end(),9517"Inconsistency at end of space");9518assert(chunk_at_end->end() == (uintptr_t*) _cmsSpace->end(),9519"Shrinking is inconsistent");9520return;9521}9522}9523// Transfer some number of overflown objects to usual marking9524// stack. Return true if some objects were transferred.9525bool MarkRefsIntoAndScanClosure::take_from_overflow_list() {9526size_t num = MIN2((size_t)(_mark_stack->capacity() - _mark_stack->length())/4,9527(size_t)ParGCDesiredObjsFromOverflowList);95289529bool res = _collector->take_from_overflow_list(num, _mark_stack);9530assert(_collector->overflow_list_is_empty() || res,9531"If list is not empty, we should have taken something");9532assert(!res || !_mark_stack->isEmpty(),9533"If we took something, it should now be on our stack");9534return res;9535}95369537size_t MarkDeadObjectsClosure::do_blk(HeapWord* addr) {9538size_t res = _sp->block_size_no_stall(addr, _collector);9539if (_sp->block_is_obj(addr)) {9540if (_live_bit_map->isMarked(addr)) {9541// It can't have been dead in a previous cycle9542guarantee(!_dead_bit_map->isMarked(addr), "No resurrection!");9543} else {9544_dead_bit_map->mark(addr); // mark the dead object9545}9546}9547// Could be 0, if the block size could not be computed without stalling.9548return res;9549}95509551TraceCMSMemoryManagerStats::TraceCMSMemoryManagerStats(CMSCollector::CollectorState phase, GCCause::Cause cause): TraceMemoryManagerStats() {95529553switch (phase) {9554case CMSCollector::InitialMarking:9555initialize(true /* fullGC */ ,9556cause /* cause of the GC */,9557true /* allMemoryPoolsAffected */,9558true /* recordGCBeginTime */,9559true /* recordPreGCUsage */,9560false /* recordPeakUsage */,9561false /* recordPostGCusage */,9562true /* recordAccumulatedGCTime */,9563false /* recordGCEndTime */,9564false /* countCollection */ );9565break;95669567case CMSCollector::FinalMarking:9568initialize(true /* fullGC */ ,9569cause /* cause of the GC */,9570true /* allMemoryPoolsAffected */,9571false /* recordGCBeginTime */,9572false /* recordPreGCUsage */,9573false /* recordPeakUsage */,9574false /* recordPostGCusage */,9575true /* recordAccumulatedGCTime */,9576false /* recordGCEndTime */,9577false /* countCollection */ );9578break;95799580case CMSCollector::Sweeping:9581initialize(true /* fullGC */ ,9582cause /* cause of the GC */,9583true /* allMemoryPoolsAffected */,9584false /* recordGCBeginTime */,9585false /* recordPreGCUsage */,9586true /* recordPeakUsage */,9587true /* recordPostGCusage */,9588false /* recordAccumulatedGCTime */,9589true /* recordGCEndTime */,9590true /* countCollection */ );9591break;95929593default:9594ShouldNotReachHere();9595}9596}959795989599