Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/g1/concurrentMark.cpp
38920 views
/*1* Copyright (c) 2001, 2016, 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/metadataOnStackMark.hpp"26#include "classfile/symbolTable.hpp"27#include "code/codeCache.hpp"28#include "gc_implementation/g1/concurrentMark.inline.hpp"29#include "gc_implementation/g1/concurrentMarkThread.inline.hpp"30#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"31#include "gc_implementation/g1/g1CollectorPolicy.hpp"32#include "gc_implementation/g1/g1ErgoVerbose.hpp"33#include "gc_implementation/g1/g1Log.hpp"34#include "gc_implementation/g1/g1OopClosures.inline.hpp"35#include "gc_implementation/g1/g1RemSet.hpp"36#include "gc_implementation/g1/heapRegion.inline.hpp"37#include "gc_implementation/g1/heapRegionManager.inline.hpp"38#include "gc_implementation/g1/heapRegionRemSet.hpp"39#include "gc_implementation/g1/heapRegionSet.inline.hpp"40#include "gc_implementation/shared/vmGCOperations.hpp"41#include "gc_implementation/shared/gcTimer.hpp"42#include "gc_implementation/shared/gcTrace.hpp"43#include "gc_implementation/shared/gcTraceTime.hpp"44#include "memory/allocation.hpp"45#include "memory/genOopClosures.inline.hpp"46#include "memory/referencePolicy.hpp"47#include "memory/resourceArea.hpp"48#include "oops/oop.inline.hpp"49#include "runtime/handles.inline.hpp"50#include "runtime/java.hpp"51#include "runtime/prefetch.inline.hpp"52#include "services/memTracker.hpp"5354// Concurrent marking bit map wrapper5556CMBitMapRO::CMBitMapRO(int shifter) :57_bm(),58_shifter(shifter) {59_bmStartWord = 0;60_bmWordSize = 0;61}6263HeapWord* CMBitMapRO::getNextMarkedWordAddress(const HeapWord* addr,64const HeapWord* limit) const {65// First we must round addr *up* to a possible object boundary.66addr = (HeapWord*)align_size_up((intptr_t)addr,67HeapWordSize << _shifter);68size_t addrOffset = heapWordToOffset(addr);69if (limit == NULL) {70limit = _bmStartWord + _bmWordSize;71}72size_t limitOffset = heapWordToOffset(limit);73size_t nextOffset = _bm.get_next_one_offset(addrOffset, limitOffset);74HeapWord* nextAddr = offsetToHeapWord(nextOffset);75assert(nextAddr >= addr, "get_next_one postcondition");76assert(nextAddr == limit || isMarked(nextAddr),77"get_next_one postcondition");78return nextAddr;79}8081HeapWord* CMBitMapRO::getNextUnmarkedWordAddress(const HeapWord* addr,82const HeapWord* limit) const {83size_t addrOffset = heapWordToOffset(addr);84if (limit == NULL) {85limit = _bmStartWord + _bmWordSize;86}87size_t limitOffset = heapWordToOffset(limit);88size_t nextOffset = _bm.get_next_zero_offset(addrOffset, limitOffset);89HeapWord* nextAddr = offsetToHeapWord(nextOffset);90assert(nextAddr >= addr, "get_next_one postcondition");91assert(nextAddr == limit || !isMarked(nextAddr),92"get_next_one postcondition");93return nextAddr;94}9596int CMBitMapRO::heapWordDiffToOffsetDiff(size_t diff) const {97assert((diff & ((1 << _shifter) - 1)) == 0, "argument check");98return (int) (diff >> _shifter);99}100101#ifndef PRODUCT102bool CMBitMapRO::covers(MemRegion heap_rs) const {103// assert(_bm.map() == _virtual_space.low(), "map inconsistency");104assert(((size_t)_bm.size() * ((size_t)1 << _shifter)) == _bmWordSize,105"size inconsistency");106return _bmStartWord == (HeapWord*)(heap_rs.start()) &&107_bmWordSize == heap_rs.word_size();108}109#endif110111void CMBitMapRO::print_on_error(outputStream* st, const char* prefix) const {112_bm.print_on_error(st, prefix);113}114115size_t CMBitMap::compute_size(size_t heap_size) {116return ReservedSpace::allocation_align_size_up(heap_size / mark_distance());117}118119size_t CMBitMap::mark_distance() {120return MinObjAlignmentInBytes * BitsPerByte;121}122123void CMBitMap::initialize(MemRegion heap, G1RegionToSpaceMapper* storage) {124_bmStartWord = heap.start();125_bmWordSize = heap.word_size();126127_bm.set_map((BitMap::bm_word_t*) storage->reserved().start());128_bm.set_size(_bmWordSize >> _shifter);129130storage->set_mapping_changed_listener(&_listener);131}132133void CMBitMapMappingChangedListener::on_commit(uint start_region, size_t num_regions, bool zero_filled) {134if (zero_filled) {135return;136}137// We need to clear the bitmap on commit, removing any existing information.138MemRegion mr(G1CollectedHeap::heap()->bottom_addr_for_region(start_region), num_regions * HeapRegion::GrainWords);139_bm->clearRange(mr);140}141142// Closure used for clearing the given mark bitmap.143class ClearBitmapHRClosure : public HeapRegionClosure {144private:145ConcurrentMark* _cm;146CMBitMap* _bitmap;147bool _may_yield; // The closure may yield during iteration. If yielded, abort the iteration.148public:149ClearBitmapHRClosure(ConcurrentMark* cm, CMBitMap* bitmap, bool may_yield) : HeapRegionClosure(), _cm(cm), _bitmap(bitmap), _may_yield(may_yield) {150assert(!may_yield || cm != NULL, "CM must be non-NULL if this closure is expected to yield.");151}152153virtual bool doHeapRegion(HeapRegion* r) {154size_t const chunk_size_in_words = M / HeapWordSize;155156HeapWord* cur = r->bottom();157HeapWord* const end = r->end();158159while (cur < end) {160MemRegion mr(cur, MIN2(cur + chunk_size_in_words, end));161_bitmap->clearRange(mr);162163cur += chunk_size_in_words;164165// Abort iteration if after yielding the marking has been aborted.166if (_may_yield && _cm->do_yield_check() && _cm->has_aborted()) {167return true;168}169// Repeat the asserts from before the start of the closure. We will do them170// as asserts here to minimize their overhead on the product. However, we171// will have them as guarantees at the beginning / end of the bitmap172// clearing to get some checking in the product.173assert(!_may_yield || _cm->cmThread()->during_cycle(), "invariant");174assert(!_may_yield || !G1CollectedHeap::heap()->mark_in_progress(), "invariant");175}176177return false;178}179};180181void CMBitMap::clearAll() {182ClearBitmapHRClosure cl(NULL, this, false /* may_yield */);183G1CollectedHeap::heap()->heap_region_iterate(&cl);184guarantee(cl.complete(), "Must have completed iteration.");185return;186}187188void CMBitMap::markRange(MemRegion mr) {189mr.intersection(MemRegion(_bmStartWord, _bmWordSize));190assert(!mr.is_empty(), "unexpected empty region");191assert((offsetToHeapWord(heapWordToOffset(mr.end())) ==192((HeapWord *) mr.end())),193"markRange memory region end is not card aligned");194// convert address range into offset range195_bm.at_put_range(heapWordToOffset(mr.start()),196heapWordToOffset(mr.end()), true);197}198199void CMBitMap::clearRange(MemRegion mr) {200mr.intersection(MemRegion(_bmStartWord, _bmWordSize));201assert(!mr.is_empty(), "unexpected empty region");202// convert address range into offset range203_bm.at_put_range(heapWordToOffset(mr.start()),204heapWordToOffset(mr.end()), false);205}206207MemRegion CMBitMap::getAndClearMarkedRegion(HeapWord* addr,208HeapWord* end_addr) {209HeapWord* start = getNextMarkedWordAddress(addr);210start = MIN2(start, end_addr);211HeapWord* end = getNextUnmarkedWordAddress(start);212end = MIN2(end, end_addr);213assert(start <= end, "Consistency check");214MemRegion mr(start, end);215if (!mr.is_empty()) {216clearRange(mr);217}218return mr;219}220221CMMarkStack::CMMarkStack(ConcurrentMark* cm) :222_base(NULL), _cm(cm)223#ifdef ASSERT224, _drain_in_progress(false)225, _drain_in_progress_yields(false)226#endif227{}228229bool CMMarkStack::allocate(size_t capacity) {230// allocate a stack of the requisite depth231ReservedSpace rs(ReservedSpace::allocation_align_size_up(capacity * sizeof(oop)));232if (!rs.is_reserved()) {233warning("ConcurrentMark MarkStack allocation failure");234return false;235}236MemTracker::record_virtual_memory_type((address)rs.base(), mtGC);237if (!_virtual_space.initialize(rs, rs.size())) {238warning("ConcurrentMark MarkStack backing store failure");239// Release the virtual memory reserved for the marking stack240rs.release();241return false;242}243assert(_virtual_space.committed_size() == rs.size(),244"Didn't reserve backing store for all of ConcurrentMark stack?");245_base = (oop*) _virtual_space.low();246setEmpty();247_capacity = (jint) capacity;248_saved_index = -1;249_should_expand = false;250NOT_PRODUCT(_max_depth = 0);251return true;252}253254void CMMarkStack::expand() {255// Called, during remark, if we've overflown the marking stack during marking.256assert(isEmpty(), "stack should been emptied while handling overflow");257assert(_capacity <= (jint) MarkStackSizeMax, "stack bigger than permitted");258// Clear expansion flag259_should_expand = false;260if (_capacity == (jint) MarkStackSizeMax) {261if (PrintGCDetails && Verbose) {262gclog_or_tty->print_cr(" (benign) Can't expand marking stack capacity, at max size limit");263}264return;265}266// Double capacity if possible267jint new_capacity = MIN2(_capacity*2, (jint) MarkStackSizeMax);268// Do not give up existing stack until we have managed to269// get the double capacity that we desired.270ReservedSpace rs(ReservedSpace::allocation_align_size_up(new_capacity *271sizeof(oop)));272if (rs.is_reserved()) {273// Release the backing store associated with old stack274_virtual_space.release();275// Reinitialize virtual space for new stack276if (!_virtual_space.initialize(rs, rs.size())) {277fatal("Not enough swap for expanded marking stack capacity");278}279_base = (oop*)(_virtual_space.low());280_index = 0;281_capacity = new_capacity;282} else {283if (PrintGCDetails && Verbose) {284// Failed to double capacity, continue;285gclog_or_tty->print(" (benign) Failed to expand marking stack capacity from "286SIZE_FORMAT "K to " SIZE_FORMAT "K",287_capacity / K, new_capacity / K);288}289}290}291292void CMMarkStack::set_should_expand() {293// If we're resetting the marking state because of an294// marking stack overflow, record that we should, if295// possible, expand the stack.296_should_expand = _cm->has_overflown();297}298299CMMarkStack::~CMMarkStack() {300if (_base != NULL) {301_base = NULL;302_virtual_space.release();303}304}305306void CMMarkStack::par_push(oop ptr) {307while (true) {308if (isFull()) {309_overflow = true;310return;311}312// Otherwise...313jint index = _index;314jint next_index = index+1;315jint res = Atomic::cmpxchg(next_index, &_index, index);316if (res == index) {317_base[index] = ptr;318// Note that we don't maintain this atomically. We could, but it319// doesn't seem necessary.320NOT_PRODUCT(_max_depth = MAX2(_max_depth, next_index));321return;322}323// Otherwise, we need to try again.324}325}326327void CMMarkStack::par_adjoin_arr(oop* ptr_arr, int n) {328while (true) {329if (isFull()) {330_overflow = true;331return;332}333// Otherwise...334jint index = _index;335jint next_index = index + n;336if (next_index > _capacity) {337_overflow = true;338return;339}340jint res = Atomic::cmpxchg(next_index, &_index, index);341if (res == index) {342for (int i = 0; i < n; i++) {343int ind = index + i;344assert(ind < _capacity, "By overflow test above.");345_base[ind] = ptr_arr[i];346}347NOT_PRODUCT(_max_depth = MAX2(_max_depth, next_index));348return;349}350// Otherwise, we need to try again.351}352}353354void CMMarkStack::par_push_arr(oop* ptr_arr, int n) {355MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);356jint start = _index;357jint next_index = start + n;358if (next_index > _capacity) {359_overflow = true;360return;361}362// Otherwise.363_index = next_index;364for (int i = 0; i < n; i++) {365int ind = start + i;366assert(ind < _capacity, "By overflow test above.");367_base[ind] = ptr_arr[i];368}369NOT_PRODUCT(_max_depth = MAX2(_max_depth, next_index));370}371372bool CMMarkStack::par_pop_arr(oop* ptr_arr, int max, int* n) {373MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);374jint index = _index;375if (index == 0) {376*n = 0;377return false;378} else {379int k = MIN2(max, index);380jint new_ind = index - k;381for (int j = 0; j < k; j++) {382ptr_arr[j] = _base[new_ind + j];383}384_index = new_ind;385*n = k;386return true;387}388}389390template<class OopClosureClass>391bool CMMarkStack::drain(OopClosureClass* cl, CMBitMap* bm, bool yield_after) {392assert(!_drain_in_progress || !_drain_in_progress_yields || yield_after393|| SafepointSynchronize::is_at_safepoint(),394"Drain recursion must be yield-safe.");395bool res = true;396debug_only(_drain_in_progress = true);397debug_only(_drain_in_progress_yields = yield_after);398while (!isEmpty()) {399oop newOop = pop();400assert(G1CollectedHeap::heap()->is_in_reserved(newOop), "Bad pop");401assert(newOop->is_oop(), "Expected an oop");402assert(bm == NULL || bm->isMarked((HeapWord*)newOop),403"only grey objects on this stack");404newOop->oop_iterate(cl);405if (yield_after && _cm->do_yield_check()) {406res = false;407break;408}409}410debug_only(_drain_in_progress = false);411return res;412}413414void CMMarkStack::note_start_of_gc() {415assert(_saved_index == -1,416"note_start_of_gc()/end_of_gc() bracketed incorrectly");417_saved_index = _index;418}419420void CMMarkStack::note_end_of_gc() {421// This is intentionally a guarantee, instead of an assert. If we422// accidentally add something to the mark stack during GC, it423// will be a correctness issue so it's better if we crash. we'll424// only check this once per GC anyway, so it won't be a performance425// issue in any way.426guarantee(_saved_index == _index,427err_msg("saved index: %d index: %d", _saved_index, _index));428_saved_index = -1;429}430431void CMMarkStack::oops_do(OopClosure* f) {432assert(_saved_index == _index,433err_msg("saved index: %d index: %d", _saved_index, _index));434for (int i = 0; i < _index; i += 1) {435f->do_oop(&_base[i]);436}437}438439CMRootRegions::CMRootRegions() :440_young_list(NULL), _cm(NULL), _scan_in_progress(false),441_should_abort(false), _next_survivor(NULL) { }442443void CMRootRegions::init(G1CollectedHeap* g1h, ConcurrentMark* cm) {444_young_list = g1h->young_list();445_cm = cm;446}447448void CMRootRegions::prepare_for_scan() {449assert(!scan_in_progress(), "pre-condition");450451// Currently, only survivors can be root regions.452assert(_next_survivor == NULL, "pre-condition");453_next_survivor = _young_list->first_survivor_region();454_scan_in_progress = (_next_survivor != NULL);455_should_abort = false;456}457458HeapRegion* CMRootRegions::claim_next() {459if (_should_abort) {460// If someone has set the should_abort flag, we return NULL to461// force the caller to bail out of their loop.462return NULL;463}464465// Currently, only survivors can be root regions.466HeapRegion* res = _next_survivor;467if (res != NULL) {468MutexLockerEx x(RootRegionScan_lock, Mutex::_no_safepoint_check_flag);469// Read it again in case it changed while we were waiting for the lock.470res = _next_survivor;471if (res != NULL) {472if (res == _young_list->last_survivor_region()) {473// We just claimed the last survivor so store NULL to indicate474// that we're done.475_next_survivor = NULL;476} else {477_next_survivor = res->get_next_young_region();478}479} else {480// Someone else claimed the last survivor while we were trying481// to take the lock so nothing else to do.482}483}484assert(res == NULL || res->is_survivor(), "post-condition");485486return res;487}488489void CMRootRegions::scan_finished() {490assert(scan_in_progress(), "pre-condition");491492// Currently, only survivors can be root regions.493if (!_should_abort) {494assert(_next_survivor == NULL, "we should have claimed all survivors");495}496_next_survivor = NULL;497498{499MutexLockerEx x(RootRegionScan_lock, Mutex::_no_safepoint_check_flag);500_scan_in_progress = false;501RootRegionScan_lock->notify_all();502}503}504505bool CMRootRegions::wait_until_scan_finished() {506if (!scan_in_progress()) return false;507508{509MutexLockerEx x(RootRegionScan_lock, Mutex::_no_safepoint_check_flag);510while (scan_in_progress()) {511RootRegionScan_lock->wait(Mutex::_no_safepoint_check_flag);512}513}514return true;515}516517#ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away518#pragma warning( disable:4355 ) // 'this' : used in base member initializer list519#endif // _MSC_VER520521uint ConcurrentMark::scale_parallel_threads(uint n_par_threads) {522return MAX2((n_par_threads + 2) / 4, 1U);523}524525ConcurrentMark::ConcurrentMark(G1CollectedHeap* g1h, G1RegionToSpaceMapper* prev_bitmap_storage, G1RegionToSpaceMapper* next_bitmap_storage) :526_g1h(g1h),527_markBitMap1(),528_markBitMap2(),529_parallel_marking_threads(0),530_max_parallel_marking_threads(0),531_sleep_factor(0.0),532_marking_task_overhead(1.0),533_cleanup_sleep_factor(0.0),534_cleanup_task_overhead(1.0),535_cleanup_list("Cleanup List"),536_region_bm((BitMap::idx_t)(g1h->max_regions()), false /* in_resource_area*/),537_card_bm((g1h->reserved_region().byte_size() + CardTableModRefBS::card_size - 1) >>538CardTableModRefBS::card_shift,539false /* in_resource_area*/),540541_prevMarkBitMap(&_markBitMap1),542_nextMarkBitMap(&_markBitMap2),543544_markStack(this),545// _finger set in set_non_marking_state546547_max_worker_id(MAX2((uint)ParallelGCThreads, 1U)),548// _active_tasks set in set_non_marking_state549// _tasks set inside the constructor550_task_queues(new CMTaskQueueSet((int) _max_worker_id)),551_terminator(ParallelTaskTerminator((int) _max_worker_id, _task_queues)),552553_has_overflown(false),554_concurrent(false),555_has_aborted(false),556_aborted_gc_id(GCId::undefined()),557_restart_for_overflow(false),558_concurrent_marking_in_progress(false),559560// _verbose_level set below561562_init_times(),563_remark_times(), _remark_mark_times(), _remark_weak_ref_times(),564_cleanup_times(),565_total_counting_time(0.0),566_total_rs_scrub_time(0.0),567568_parallel_workers(NULL),569570_count_card_bitmaps(NULL),571_count_marked_bytes(NULL),572_completed_initialization(false) {573CMVerboseLevel verbose_level = (CMVerboseLevel) G1MarkingVerboseLevel;574if (verbose_level < no_verbose) {575verbose_level = no_verbose;576}577if (verbose_level > high_verbose) {578verbose_level = high_verbose;579}580_verbose_level = verbose_level;581582if (verbose_low()) {583gclog_or_tty->print_cr("[global] init, heap start = " PTR_FORMAT", "584"heap end = " INTPTR_FORMAT, p2i(_heap_start), p2i(_heap_end));585}586587_markBitMap1.initialize(g1h->reserved_region(), prev_bitmap_storage);588_markBitMap2.initialize(g1h->reserved_region(), next_bitmap_storage);589590// Create & start a ConcurrentMark thread.591_cmThread = new ConcurrentMarkThread(this);592assert(cmThread() != NULL, "CM Thread should have been created");593assert(cmThread()->cm() != NULL, "CM Thread should refer to this cm");594if (_cmThread->osthread() == NULL) {595vm_shutdown_during_initialization("Could not create ConcurrentMarkThread");596}597598assert(CGC_lock != NULL, "Where's the CGC_lock?");599assert(_markBitMap1.covers(g1h->reserved_region()), "_markBitMap1 inconsistency");600assert(_markBitMap2.covers(g1h->reserved_region()), "_markBitMap2 inconsistency");601602SATBMarkQueueSet& satb_qs = JavaThread::satb_mark_queue_set();603satb_qs.set_buffer_size(G1SATBBufferSize);604605_root_regions.init(_g1h, this);606607if (ConcGCThreads > ParallelGCThreads) {608warning("Can't have more ConcGCThreads (" UINTX_FORMAT ") "609"than ParallelGCThreads (" UINTX_FORMAT ").",610ConcGCThreads, ParallelGCThreads);611return;612}613if (ParallelGCThreads == 0) {614// if we are not running with any parallel GC threads we will not615// spawn any marking threads either616_parallel_marking_threads = 0;617_max_parallel_marking_threads = 0;618_sleep_factor = 0.0;619_marking_task_overhead = 1.0;620} else {621if (!FLAG_IS_DEFAULT(ConcGCThreads) && ConcGCThreads > 0) {622// Note: ConcGCThreads has precedence over G1MarkingOverheadPercent623// if both are set624_sleep_factor = 0.0;625_marking_task_overhead = 1.0;626} else if (G1MarkingOverheadPercent > 0) {627// We will calculate the number of parallel marking threads based628// on a target overhead with respect to the soft real-time goal629double marking_overhead = (double) G1MarkingOverheadPercent / 100.0;630double overall_cm_overhead =631(double) MaxGCPauseMillis * marking_overhead /632(double) GCPauseIntervalMillis;633double cpu_ratio = 1.0 / os::initial_active_processor_count();634double marking_thread_num = ceil(overall_cm_overhead / cpu_ratio);635double marking_task_overhead =636overall_cm_overhead / marking_thread_num * os::initial_active_processor_count();637double sleep_factor =638(1.0 - marking_task_overhead) / marking_task_overhead;639640FLAG_SET_ERGO(uintx, ConcGCThreads, (uint) marking_thread_num);641_sleep_factor = sleep_factor;642_marking_task_overhead = marking_task_overhead;643} else {644// Calculate the number of parallel marking threads by scaling645// the number of parallel GC threads.646uint marking_thread_num = scale_parallel_threads((uint) ParallelGCThreads);647FLAG_SET_ERGO(uintx, ConcGCThreads, marking_thread_num);648_sleep_factor = 0.0;649_marking_task_overhead = 1.0;650}651652assert(ConcGCThreads > 0, "Should have been set");653_parallel_marking_threads = (uint) ConcGCThreads;654_max_parallel_marking_threads = _parallel_marking_threads;655656if (parallel_marking_threads() > 1) {657_cleanup_task_overhead = 1.0;658} else {659_cleanup_task_overhead = marking_task_overhead();660}661_cleanup_sleep_factor =662(1.0 - cleanup_task_overhead()) / cleanup_task_overhead();663664#if 0665gclog_or_tty->print_cr("Marking Threads %d", parallel_marking_threads());666gclog_or_tty->print_cr("CM Marking Task Overhead %1.4lf", marking_task_overhead());667gclog_or_tty->print_cr("CM Sleep Factor %1.4lf", sleep_factor());668gclog_or_tty->print_cr("CL Marking Task Overhead %1.4lf", cleanup_task_overhead());669gclog_or_tty->print_cr("CL Sleep Factor %1.4lf", cleanup_sleep_factor());670#endif671672guarantee(parallel_marking_threads() > 0, "peace of mind");673_parallel_workers = new FlexibleWorkGang("G1 Parallel Marking Threads",674_max_parallel_marking_threads, false, true);675if (_parallel_workers == NULL) {676vm_exit_during_initialization("Failed necessary allocation.");677} else {678_parallel_workers->initialize_workers();679}680}681682if (FLAG_IS_DEFAULT(MarkStackSize)) {683uintx mark_stack_size =684MIN2(MarkStackSizeMax,685MAX2(MarkStackSize, (uintx) (parallel_marking_threads() * TASKQUEUE_SIZE)));686// Verify that the calculated value for MarkStackSize is in range.687// It would be nice to use the private utility routine from Arguments.688if (!(mark_stack_size >= 1 && mark_stack_size <= MarkStackSizeMax)) {689warning("Invalid value calculated for MarkStackSize (" UINTX_FORMAT "): "690"must be between " UINTX_FORMAT " and " UINTX_FORMAT,691mark_stack_size, (uintx) 1, MarkStackSizeMax);692return;693}694FLAG_SET_ERGO(uintx, MarkStackSize, mark_stack_size);695} else {696// Verify MarkStackSize is in range.697if (FLAG_IS_CMDLINE(MarkStackSize)) {698if (FLAG_IS_DEFAULT(MarkStackSizeMax)) {699if (!(MarkStackSize >= 1 && MarkStackSize <= MarkStackSizeMax)) {700warning("Invalid value specified for MarkStackSize (" UINTX_FORMAT "): "701"must be between " UINTX_FORMAT " and " UINTX_FORMAT,702MarkStackSize, (uintx) 1, MarkStackSizeMax);703return;704}705} else if (FLAG_IS_CMDLINE(MarkStackSizeMax)) {706if (!(MarkStackSize >= 1 && MarkStackSize <= MarkStackSizeMax)) {707warning("Invalid value specified for MarkStackSize (" UINTX_FORMAT ")"708" or for MarkStackSizeMax (" UINTX_FORMAT ")",709MarkStackSize, MarkStackSizeMax);710return;711}712}713}714}715716if (!_markStack.allocate(MarkStackSize)) {717warning("Failed to allocate CM marking stack");718return;719}720721_tasks = NEW_C_HEAP_ARRAY(CMTask*, _max_worker_id, mtGC);722_accum_task_vtime = NEW_C_HEAP_ARRAY(double, _max_worker_id, mtGC);723724_count_card_bitmaps = NEW_C_HEAP_ARRAY(BitMap, _max_worker_id, mtGC);725_count_marked_bytes = NEW_C_HEAP_ARRAY(size_t*, _max_worker_id, mtGC);726727BitMap::idx_t card_bm_size = _card_bm.size();728729// so that the assertion in MarkingTaskQueue::task_queue doesn't fail730_active_tasks = _max_worker_id;731732size_t max_regions = (size_t) _g1h->max_regions();733for (uint i = 0; i < _max_worker_id; ++i) {734CMTaskQueue* task_queue = new CMTaskQueue();735task_queue->initialize();736_task_queues->register_queue(i, task_queue);737738_count_card_bitmaps[i] = BitMap(card_bm_size, false);739_count_marked_bytes[i] = NEW_C_HEAP_ARRAY(size_t, max_regions, mtGC);740741_tasks[i] = new CMTask(i, this,742_count_marked_bytes[i],743&_count_card_bitmaps[i],744task_queue, _task_queues);745746_accum_task_vtime[i] = 0.0;747}748749// Calculate the card number for the bottom of the heap. Used750// in biasing indexes into the accounting card bitmaps.751_heap_bottom_card_num =752intptr_t(uintptr_t(_g1h->reserved_region().start()) >>753CardTableModRefBS::card_shift);754755// Clear all the liveness counting data756clear_all_count_data();757758// so that the call below can read a sensible value759_heap_start = g1h->reserved_region().start();760set_non_marking_state();761_completed_initialization = true;762}763764void ConcurrentMark::reset() {765// Starting values for these two. This should be called in a STW766// phase.767MemRegion reserved = _g1h->g1_reserved();768_heap_start = reserved.start();769_heap_end = reserved.end();770771// Separated the asserts so that we know which one fires.772assert(_heap_start != NULL, "heap bounds should look ok");773assert(_heap_end != NULL, "heap bounds should look ok");774assert(_heap_start < _heap_end, "heap bounds should look ok");775776// Reset all the marking data structures and any necessary flags777reset_marking_state();778779if (verbose_low()) {780gclog_or_tty->print_cr("[global] resetting");781}782783// We do reset all of them, since different phases will use784// different number of active threads. So, it's easiest to have all785// of them ready.786for (uint i = 0; i < _max_worker_id; ++i) {787_tasks[i]->reset(_nextMarkBitMap);788}789790// we need this to make sure that the flag is on during the evac791// pause with initial mark piggy-backed792set_concurrent_marking_in_progress();793}794795796void ConcurrentMark::reset_marking_state(bool clear_overflow) {797_markStack.set_should_expand();798_markStack.setEmpty(); // Also clears the _markStack overflow flag799if (clear_overflow) {800clear_has_overflown();801} else {802assert(has_overflown(), "pre-condition");803}804_finger = _heap_start;805806for (uint i = 0; i < _max_worker_id; ++i) {807CMTaskQueue* queue = _task_queues->queue(i);808queue->set_empty();809}810}811812void ConcurrentMark::set_concurrency(uint active_tasks) {813assert(active_tasks <= _max_worker_id, "we should not have more");814815_active_tasks = active_tasks;816// Need to update the three data structures below according to the817// number of active threads for this phase.818_terminator = ParallelTaskTerminator((int) active_tasks, _task_queues);819_first_overflow_barrier_sync.set_n_workers((int) active_tasks);820_second_overflow_barrier_sync.set_n_workers((int) active_tasks);821}822823void ConcurrentMark::set_concurrency_and_phase(uint active_tasks, bool concurrent) {824set_concurrency(active_tasks);825826_concurrent = concurrent;827// We propagate this to all tasks, not just the active ones.828for (uint i = 0; i < _max_worker_id; ++i)829_tasks[i]->set_concurrent(concurrent);830831if (concurrent) {832set_concurrent_marking_in_progress();833} else {834// We currently assume that the concurrent flag has been set to835// false before we start remark. At this point we should also be836// in a STW phase.837assert(!concurrent_marking_in_progress(), "invariant");838assert(out_of_regions(),839err_msg("only way to get here: _finger: " PTR_FORMAT ", _heap_end: " PTR_FORMAT,840p2i(_finger), p2i(_heap_end)));841}842}843844void ConcurrentMark::set_non_marking_state() {845// We set the global marking state to some default values when we're846// not doing marking.847reset_marking_state();848_active_tasks = 0;849clear_concurrent_marking_in_progress();850}851852ConcurrentMark::~ConcurrentMark() {853// The ConcurrentMark instance is never freed.854ShouldNotReachHere();855}856857void ConcurrentMark::clearNextBitmap() {858G1CollectedHeap* g1h = G1CollectedHeap::heap();859860// Make sure that the concurrent mark thread looks to still be in861// the current cycle.862guarantee(cmThread()->during_cycle(), "invariant");863864// We are finishing up the current cycle by clearing the next865// marking bitmap and getting it ready for the next cycle. During866// this time no other cycle can start. So, let's make sure that this867// is the case.868guarantee(!g1h->mark_in_progress(), "invariant");869870ClearBitmapHRClosure cl(this, _nextMarkBitMap, true /* may_yield */);871g1h->heap_region_iterate(&cl);872873// Clear the liveness counting data. If the marking has been aborted, the abort()874// call already did that.875if (cl.complete()) {876clear_all_count_data();877}878879// Repeat the asserts from above.880guarantee(cmThread()->during_cycle(), "invariant");881guarantee(!g1h->mark_in_progress(), "invariant");882}883884class CheckBitmapClearHRClosure : public HeapRegionClosure {885CMBitMap* _bitmap;886bool _error;887public:888CheckBitmapClearHRClosure(CMBitMap* bitmap) : _bitmap(bitmap) {889}890891virtual bool doHeapRegion(HeapRegion* r) {892// This closure can be called concurrently to the mutator, so we must make sure893// that the result of the getNextMarkedWordAddress() call is compared to the894// value passed to it as limit to detect any found bits.895// We can use the region's orig_end() for the limit and the comparison value896// as it always contains the "real" end of the region that never changes and897// has no side effects.898// Due to the latter, there can also be no problem with the compiler generating899// reloads of the orig_end() call.900HeapWord* end = r->orig_end();901return _bitmap->getNextMarkedWordAddress(r->bottom(), end) != end;902}903};904905bool ConcurrentMark::nextMarkBitmapIsClear() {906CheckBitmapClearHRClosure cl(_nextMarkBitMap);907_g1h->heap_region_iterate(&cl);908return cl.complete();909}910911class NoteStartOfMarkHRClosure: public HeapRegionClosure {912public:913bool doHeapRegion(HeapRegion* r) {914if (!r->continuesHumongous()) {915r->note_start_of_marking();916}917return false;918}919};920921void ConcurrentMark::checkpointRootsInitialPre() {922G1CollectedHeap* g1h = G1CollectedHeap::heap();923G1CollectorPolicy* g1p = g1h->g1_policy();924925_has_aborted = false;926927#ifndef PRODUCT928if (G1PrintReachableAtInitialMark) {929print_reachable("at-cycle-start",930VerifyOption_G1UsePrevMarking, true /* all */);931}932#endif933934// Initialise marking structures. This has to be done in a STW phase.935reset();936937// For each region note start of marking.938NoteStartOfMarkHRClosure startcl;939g1h->heap_region_iterate(&startcl);940}941942943void ConcurrentMark::checkpointRootsInitialPost() {944G1CollectedHeap* g1h = G1CollectedHeap::heap();945946// If we force an overflow during remark, the remark operation will947// actually abort and we'll restart concurrent marking. If we always948// force an oveflow during remark we'll never actually complete the949// marking phase. So, we initilize this here, at the start of the950// cycle, so that at the remaining overflow number will decrease at951// every remark and we'll eventually not need to cause one.952force_overflow_stw()->init();953954// Start Concurrent Marking weak-reference discovery.955ReferenceProcessor* rp = g1h->ref_processor_cm();956// enable ("weak") refs discovery957rp->enable_discovery(true /*verify_disabled*/, true /*verify_no_refs*/);958rp->setup_policy(false); // snapshot the soft ref policy to be used in this cycle959960SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();961// This is the start of the marking cycle, we're expected all962// threads to have SATB queues with active set to false.963satb_mq_set.set_active_all_threads(true, /* new active value */964false /* expected_active */);965966_root_regions.prepare_for_scan();967968// update_g1_committed() will be called at the end of an evac pause969// when marking is on. So, it's also called at the end of the970// initial-mark pause to update the heap end, if the heap expands971// during it. No need to call it here.972}973974/*975* Notice that in the next two methods, we actually leave the STS976* during the barrier sync and join it immediately afterwards. If we977* do not do this, the following deadlock can occur: one thread could978* be in the barrier sync code, waiting for the other thread to also979* sync up, whereas another one could be trying to yield, while also980* waiting for the other threads to sync up too.981*982* Note, however, that this code is also used during remark and in983* this case we should not attempt to leave / enter the STS, otherwise984* we'll either hit an asseert (debug / fastdebug) or deadlock985* (product). So we should only leave / enter the STS if we are986* operating concurrently.987*988* Because the thread that does the sync barrier has left the STS, it989* is possible to be suspended for a Full GC or an evacuation pause990* could occur. This is actually safe, since the entering the sync991* barrier is one of the last things do_marking_step() does, and it992* doesn't manipulate any data structures afterwards.993*/994995void ConcurrentMark::enter_first_sync_barrier(uint worker_id) {996if (verbose_low()) {997gclog_or_tty->print_cr("[%u] entering first barrier", worker_id);998}9991000if (concurrent()) {1001SuspendibleThreadSet::leave();1002}10031004bool barrier_aborted = !_first_overflow_barrier_sync.enter();10051006if (concurrent()) {1007SuspendibleThreadSet::join();1008}1009// at this point everyone should have synced up and not be doing any1010// more work10111012if (verbose_low()) {1013if (barrier_aborted) {1014gclog_or_tty->print_cr("[%u] aborted first barrier", worker_id);1015} else {1016gclog_or_tty->print_cr("[%u] leaving first barrier", worker_id);1017}1018}10191020if (barrier_aborted) {1021// If the barrier aborted we ignore the overflow condition and1022// just abort the whole marking phase as quickly as possible.1023return;1024}10251026// If we're executing the concurrent phase of marking, reset the marking1027// state; otherwise the marking state is reset after reference processing,1028// during the remark pause.1029// If we reset here as a result of an overflow during the remark we will1030// see assertion failures from any subsequent set_concurrency_and_phase()1031// calls.1032if (concurrent()) {1033// let the task associated with with worker 0 do this1034if (worker_id == 0) {1035// task 0 is responsible for clearing the global data structures1036// We should be here because of an overflow. During STW we should1037// not clear the overflow flag since we rely on it being true when1038// we exit this method to abort the pause and restart concurent1039// marking.1040reset_marking_state(true /* clear_overflow */);1041force_overflow()->update();10421043if (G1Log::fine()) {1044gclog_or_tty->gclog_stamp(concurrent_gc_id());1045gclog_or_tty->print_cr("[GC concurrent-mark-reset-for-overflow]");1046}1047}1048}10491050// after this, each task should reset its own data structures then1051// then go into the second barrier1052}10531054void ConcurrentMark::enter_second_sync_barrier(uint worker_id) {1055if (verbose_low()) {1056gclog_or_tty->print_cr("[%u] entering second barrier", worker_id);1057}10581059if (concurrent()) {1060SuspendibleThreadSet::leave();1061}10621063bool barrier_aborted = !_second_overflow_barrier_sync.enter();10641065if (concurrent()) {1066SuspendibleThreadSet::join();1067}1068// at this point everything should be re-initialized and ready to go10691070if (verbose_low()) {1071if (barrier_aborted) {1072gclog_or_tty->print_cr("[%u] aborted second barrier", worker_id);1073} else {1074gclog_or_tty->print_cr("[%u] leaving second barrier", worker_id);1075}1076}1077}10781079#ifndef PRODUCT1080void ForceOverflowSettings::init() {1081_num_remaining = G1ConcMarkForceOverflow;1082_force = false;1083update();1084}10851086void ForceOverflowSettings::update() {1087if (_num_remaining > 0) {1088_num_remaining -= 1;1089_force = true;1090} else {1091_force = false;1092}1093}10941095bool ForceOverflowSettings::should_force() {1096if (_force) {1097_force = false;1098return true;1099} else {1100return false;1101}1102}1103#endif // !PRODUCT11041105class CMConcurrentMarkingTask: public AbstractGangTask {1106private:1107ConcurrentMark* _cm;1108ConcurrentMarkThread* _cmt;11091110public:1111void work(uint worker_id) {1112assert(Thread::current()->is_ConcurrentGC_thread(),1113"this should only be done by a conc GC thread");1114ResourceMark rm;11151116double start_vtime = os::elapsedVTime();11171118SuspendibleThreadSet::join();11191120assert(worker_id < _cm->active_tasks(), "invariant");1121CMTask* the_task = _cm->task(worker_id);1122the_task->record_start_time();1123if (!_cm->has_aborted()) {1124do {1125double start_vtime_sec = os::elapsedVTime();1126double mark_step_duration_ms = G1ConcMarkStepDurationMillis;11271128the_task->do_marking_step(mark_step_duration_ms,1129true /* do_termination */,1130false /* is_serial*/);11311132double end_vtime_sec = os::elapsedVTime();1133double elapsed_vtime_sec = end_vtime_sec - start_vtime_sec;1134_cm->clear_has_overflown();11351136_cm->do_yield_check(worker_id);11371138jlong sleep_time_ms;1139if (!_cm->has_aborted() && the_task->has_aborted()) {1140sleep_time_ms =1141(jlong) (elapsed_vtime_sec * _cm->sleep_factor() * 1000.0);1142SuspendibleThreadSet::leave();1143os::sleep(Thread::current(), sleep_time_ms, false);1144SuspendibleThreadSet::join();1145}1146} while (!_cm->has_aborted() && the_task->has_aborted());1147}1148the_task->record_end_time();1149guarantee(!the_task->has_aborted() || _cm->has_aborted(), "invariant");11501151SuspendibleThreadSet::leave();11521153double end_vtime = os::elapsedVTime();1154_cm->update_accum_task_vtime(worker_id, end_vtime - start_vtime);1155}11561157CMConcurrentMarkingTask(ConcurrentMark* cm,1158ConcurrentMarkThread* cmt) :1159AbstractGangTask("Concurrent Mark"), _cm(cm), _cmt(cmt) { }11601161~CMConcurrentMarkingTask() { }1162};11631164// Calculates the number of active workers for a concurrent1165// phase.1166uint ConcurrentMark::calc_parallel_marking_threads() {1167if (G1CollectedHeap::use_parallel_gc_threads()) {1168uint n_conc_workers = 0;1169if (!UseDynamicNumberOfGCThreads ||1170(!FLAG_IS_DEFAULT(ConcGCThreads) &&1171!ForceDynamicNumberOfGCThreads)) {1172n_conc_workers = max_parallel_marking_threads();1173} else {1174n_conc_workers =1175AdaptiveSizePolicy::calc_default_active_workers(1176max_parallel_marking_threads(),11771, /* Minimum workers */1178parallel_marking_threads(),1179Threads::number_of_non_daemon_threads());1180// Don't scale down "n_conc_workers" by scale_parallel_threads() because1181// that scaling has already gone into "_max_parallel_marking_threads".1182}1183assert(n_conc_workers > 0, "Always need at least 1");1184return n_conc_workers;1185}1186// If we are not running with any parallel GC threads we will not1187// have spawned any marking threads either. Hence the number of1188// concurrent workers should be 0.1189return 0;1190}11911192void ConcurrentMark::scanRootRegion(HeapRegion* hr, uint worker_id) {1193// Currently, only survivors can be root regions.1194assert(hr->next_top_at_mark_start() == hr->bottom(), "invariant");1195G1RootRegionScanClosure cl(_g1h, this, worker_id);11961197const uintx interval = PrefetchScanIntervalInBytes;1198HeapWord* curr = hr->bottom();1199const HeapWord* end = hr->top();1200while (curr < end) {1201Prefetch::read(curr, interval);1202oop obj = oop(curr);1203int size = obj->oop_iterate(&cl);1204assert(size == obj->size(), "sanity");1205curr += size;1206}1207}12081209class CMRootRegionScanTask : public AbstractGangTask {1210private:1211ConcurrentMark* _cm;12121213public:1214CMRootRegionScanTask(ConcurrentMark* cm) :1215AbstractGangTask("Root Region Scan"), _cm(cm) { }12161217void work(uint worker_id) {1218assert(Thread::current()->is_ConcurrentGC_thread(),1219"this should only be done by a conc GC thread");12201221CMRootRegions* root_regions = _cm->root_regions();1222HeapRegion* hr = root_regions->claim_next();1223while (hr != NULL) {1224_cm->scanRootRegion(hr, worker_id);1225hr = root_regions->claim_next();1226}1227}1228};12291230void ConcurrentMark::scanRootRegions() {1231// Start of concurrent marking.1232ClassLoaderDataGraph::clear_claimed_marks();12331234// scan_in_progress() will have been set to true only if there was1235// at least one root region to scan. So, if it's false, we1236// should not attempt to do any further work.1237if (root_regions()->scan_in_progress()) {1238_parallel_marking_threads = calc_parallel_marking_threads();1239assert(parallel_marking_threads() <= max_parallel_marking_threads(),1240"Maximum number of marking threads exceeded");1241uint active_workers = MAX2(1U, parallel_marking_threads());12421243CMRootRegionScanTask task(this);1244if (use_parallel_marking_threads()) {1245_parallel_workers->set_active_workers((int) active_workers);1246_parallel_workers->run_task(&task);1247} else {1248task.work(0);1249}12501251// It's possible that has_aborted() is true here without actually1252// aborting the survivor scan earlier. This is OK as it's1253// mainly used for sanity checking.1254root_regions()->scan_finished();1255}1256}12571258void ConcurrentMark::markFromRoots() {1259// we might be tempted to assert that:1260// assert(asynch == !SafepointSynchronize::is_at_safepoint(),1261// "inconsistent argument?");1262// However that wouldn't be right, because it's possible that1263// a safepoint is indeed in progress as a younger generation1264// stop-the-world GC happens even as we mark in this generation.12651266_restart_for_overflow = false;1267force_overflow_conc()->init();12681269// _g1h has _n_par_threads1270_parallel_marking_threads = calc_parallel_marking_threads();1271assert(parallel_marking_threads() <= max_parallel_marking_threads(),1272"Maximum number of marking threads exceeded");12731274uint active_workers = MAX2(1U, parallel_marking_threads());12751276// Parallel task terminator is set in "set_concurrency_and_phase()"1277set_concurrency_and_phase(active_workers, true /* concurrent */);12781279CMConcurrentMarkingTask markingTask(this, cmThread());1280if (use_parallel_marking_threads()) {1281_parallel_workers->set_active_workers((int)active_workers);1282// Don't set _n_par_threads because it affects MT in process_roots()1283// and the decisions on that MT processing is made elsewhere.1284assert(_parallel_workers->active_workers() > 0, "Should have been set");1285_parallel_workers->run_task(&markingTask);1286} else {1287markingTask.work(0);1288}1289print_stats();1290}12911292void ConcurrentMark::checkpointRootsFinal(bool clear_all_soft_refs) {1293// world is stopped at this checkpoint1294assert(SafepointSynchronize::is_at_safepoint(),1295"world should be stopped");12961297G1CollectedHeap* g1h = G1CollectedHeap::heap();12981299// If a full collection has happened, we shouldn't do this.1300if (has_aborted()) {1301g1h->set_marking_complete(); // So bitmap clearing isn't confused1302return;1303}13041305SvcGCMarker sgcm(SvcGCMarker::OTHER);13061307if (VerifyDuringGC) {1308HandleMark hm; // handle scope1309Universe::heap()->prepare_for_verify();1310Universe::verify(VerifyOption_G1UsePrevMarking,1311" VerifyDuringGC:(before)");1312}1313g1h->check_bitmaps("Remark Start");13141315G1CollectorPolicy* g1p = g1h->g1_policy();1316g1p->record_concurrent_mark_remark_start();13171318double start = os::elapsedTime();13191320checkpointRootsFinalWork();13211322double mark_work_end = os::elapsedTime();13231324weakRefsWork(clear_all_soft_refs);13251326if (has_overflown()) {1327// Oops. We overflowed. Restart concurrent marking.1328_restart_for_overflow = true;1329if (G1TraceMarkStackOverflow) {1330gclog_or_tty->print_cr("\nRemark led to restart for overflow.");1331}13321333// Verify the heap w.r.t. the previous marking bitmap.1334if (VerifyDuringGC) {1335HandleMark hm; // handle scope1336Universe::heap()->prepare_for_verify();1337Universe::verify(VerifyOption_G1UsePrevMarking,1338" VerifyDuringGC:(overflow)");1339}13401341// Clear the marking state because we will be restarting1342// marking due to overflowing the global mark stack.1343reset_marking_state();1344} else {1345// Aggregate the per-task counting data that we have accumulated1346// while marking.1347aggregate_count_data();13481349SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();1350// We're done with marking.1351// This is the end of the marking cycle, we're expected all1352// threads to have SATB queues with active set to true.1353satb_mq_set.set_active_all_threads(false, /* new active value */1354true /* expected_active */);13551356if (VerifyDuringGC) {1357HandleMark hm; // handle scope1358Universe::heap()->prepare_for_verify();1359Universe::verify(VerifyOption_G1UseNextMarking,1360" VerifyDuringGC:(after)");1361}1362g1h->check_bitmaps("Remark End");1363assert(!restart_for_overflow(), "sanity");1364// Completely reset the marking state since marking completed1365set_non_marking_state();1366}13671368// Expand the marking stack, if we have to and if we can.1369if (_markStack.should_expand()) {1370_markStack.expand();1371}13721373// Statistics1374double now = os::elapsedTime();1375_remark_mark_times.add((mark_work_end - start) * 1000.0);1376_remark_weak_ref_times.add((now - mark_work_end) * 1000.0);1377_remark_times.add((now - start) * 1000.0);13781379g1p->record_concurrent_mark_remark_end();13801381G1CMIsAliveClosure is_alive(g1h);1382g1h->gc_tracer_cm()->report_object_count_after_gc(&is_alive);1383}13841385// Base class of the closures that finalize and verify the1386// liveness counting data.1387class CMCountDataClosureBase: public HeapRegionClosure {1388protected:1389G1CollectedHeap* _g1h;1390ConcurrentMark* _cm;1391CardTableModRefBS* _ct_bs;13921393BitMap* _region_bm;1394BitMap* _card_bm;13951396// Takes a region that's not empty (i.e., it has at least one1397// live object in it and sets its corresponding bit on the region1398// bitmap to 1. If the region is "starts humongous" it will also set1399// to 1 the bits on the region bitmap that correspond to its1400// associated "continues humongous" regions.1401void set_bit_for_region(HeapRegion* hr) {1402assert(!hr->continuesHumongous(), "should have filtered those out");14031404BitMap::idx_t index = (BitMap::idx_t) hr->hrm_index();1405if (!hr->startsHumongous()) {1406// Normal (non-humongous) case: just set the bit.1407_region_bm->par_at_put(index, true);1408} else {1409// Starts humongous case: calculate how many regions are part of1410// this humongous region and then set the bit range.1411BitMap::idx_t end_index = (BitMap::idx_t) hr->last_hc_index();1412_region_bm->par_at_put_range(index, end_index, true);1413}1414}14151416public:1417CMCountDataClosureBase(G1CollectedHeap* g1h,1418BitMap* region_bm, BitMap* card_bm):1419_g1h(g1h), _cm(g1h->concurrent_mark()),1420_ct_bs((CardTableModRefBS*) (g1h->barrier_set())),1421_region_bm(region_bm), _card_bm(card_bm) { }1422};14231424// Closure that calculates the # live objects per region. Used1425// for verification purposes during the cleanup pause.1426class CalcLiveObjectsClosure: public CMCountDataClosureBase {1427CMBitMapRO* _bm;1428size_t _region_marked_bytes;14291430public:1431CalcLiveObjectsClosure(CMBitMapRO *bm, G1CollectedHeap* g1h,1432BitMap* region_bm, BitMap* card_bm) :1433CMCountDataClosureBase(g1h, region_bm, card_bm),1434_bm(bm), _region_marked_bytes(0) { }14351436bool doHeapRegion(HeapRegion* hr) {14371438if (hr->continuesHumongous()) {1439// We will ignore these here and process them when their1440// associated "starts humongous" region is processed (see1441// set_bit_for_heap_region()). Note that we cannot rely on their1442// associated "starts humongous" region to have their bit set to1443// 1 since, due to the region chunking in the parallel region1444// iteration, a "continues humongous" region might be visited1445// before its associated "starts humongous".1446return false;1447}14481449HeapWord* ntams = hr->next_top_at_mark_start();1450HeapWord* start = hr->bottom();14511452assert(start <= hr->end() && start <= ntams && ntams <= hr->end(),1453err_msg("Preconditions not met - "1454"start: " PTR_FORMAT ", ntams: " PTR_FORMAT ", end: " PTR_FORMAT,1455p2i(start), p2i(ntams), p2i(hr->end())));14561457// Find the first marked object at or after "start".1458start = _bm->getNextMarkedWordAddress(start, ntams);14591460size_t marked_bytes = 0;14611462while (start < ntams) {1463oop obj = oop(start);1464int obj_sz = obj->size();1465HeapWord* obj_end = start + obj_sz;14661467BitMap::idx_t start_idx = _cm->card_bitmap_index_for(start);1468BitMap::idx_t end_idx = _cm->card_bitmap_index_for(obj_end);14691470// Note: if we're looking at the last region in heap - obj_end1471// could be actually just beyond the end of the heap; end_idx1472// will then correspond to a (non-existent) card that is also1473// just beyond the heap.1474if (_g1h->is_in_g1_reserved(obj_end) && !_ct_bs->is_card_aligned(obj_end)) {1475// end of object is not card aligned - increment to cover1476// all the cards spanned by the object1477end_idx += 1;1478}14791480// Set the bits in the card BM for the cards spanned by this object.1481_cm->set_card_bitmap_range(_card_bm, start_idx, end_idx, true /* is_par */);14821483// Add the size of this object to the number of marked bytes.1484marked_bytes += (size_t)obj_sz * HeapWordSize;14851486// Find the next marked object after this one.1487start = _bm->getNextMarkedWordAddress(obj_end, ntams);1488}14891490// Mark the allocated-since-marking portion...1491HeapWord* top = hr->top();1492if (ntams < top) {1493BitMap::idx_t start_idx = _cm->card_bitmap_index_for(ntams);1494BitMap::idx_t end_idx = _cm->card_bitmap_index_for(top);14951496// Note: if we're looking at the last region in heap - top1497// could be actually just beyond the end of the heap; end_idx1498// will then correspond to a (non-existent) card that is also1499// just beyond the heap.1500if (_g1h->is_in_g1_reserved(top) && !_ct_bs->is_card_aligned(top)) {1501// end of object is not card aligned - increment to cover1502// all the cards spanned by the object1503end_idx += 1;1504}1505_cm->set_card_bitmap_range(_card_bm, start_idx, end_idx, true /* is_par */);15061507// This definitely means the region has live objects.1508set_bit_for_region(hr);1509}15101511// Update the live region bitmap.1512if (marked_bytes > 0) {1513set_bit_for_region(hr);1514}15151516// Set the marked bytes for the current region so that1517// it can be queried by a calling verificiation routine1518_region_marked_bytes = marked_bytes;15191520return false;1521}15221523size_t region_marked_bytes() const { return _region_marked_bytes; }1524};15251526// Heap region closure used for verifying the counting data1527// that was accumulated concurrently and aggregated during1528// the remark pause. This closure is applied to the heap1529// regions during the STW cleanup pause.15301531class VerifyLiveObjectDataHRClosure: public HeapRegionClosure {1532G1CollectedHeap* _g1h;1533ConcurrentMark* _cm;1534CalcLiveObjectsClosure _calc_cl;1535BitMap* _region_bm; // Region BM to be verified1536BitMap* _card_bm; // Card BM to be verified1537bool _verbose; // verbose output?15381539BitMap* _exp_region_bm; // Expected Region BM values1540BitMap* _exp_card_bm; // Expected card BM values15411542int _failures;15431544public:1545VerifyLiveObjectDataHRClosure(G1CollectedHeap* g1h,1546BitMap* region_bm,1547BitMap* card_bm,1548BitMap* exp_region_bm,1549BitMap* exp_card_bm,1550bool verbose) :1551_g1h(g1h), _cm(g1h->concurrent_mark()),1552_calc_cl(_cm->nextMarkBitMap(), g1h, exp_region_bm, exp_card_bm),1553_region_bm(region_bm), _card_bm(card_bm), _verbose(verbose),1554_exp_region_bm(exp_region_bm), _exp_card_bm(exp_card_bm),1555_failures(0) { }15561557int failures() const { return _failures; }15581559bool doHeapRegion(HeapRegion* hr) {1560if (hr->continuesHumongous()) {1561// We will ignore these here and process them when their1562// associated "starts humongous" region is processed (see1563// set_bit_for_heap_region()). Note that we cannot rely on their1564// associated "starts humongous" region to have their bit set to1565// 1 since, due to the region chunking in the parallel region1566// iteration, a "continues humongous" region might be visited1567// before its associated "starts humongous".1568return false;1569}15701571int failures = 0;15721573// Call the CalcLiveObjectsClosure to walk the marking bitmap for1574// this region and set the corresponding bits in the expected region1575// and card bitmaps.1576bool res = _calc_cl.doHeapRegion(hr);1577assert(res == false, "should be continuing");15781579MutexLockerEx x((_verbose ? ParGCRareEvent_lock : NULL),1580Mutex::_no_safepoint_check_flag);15811582// Verify the marked bytes for this region.1583size_t exp_marked_bytes = _calc_cl.region_marked_bytes();1584size_t act_marked_bytes = hr->next_marked_bytes();15851586// We're not OK if expected marked bytes > actual marked bytes. It means1587// we have missed accounting some objects during the actual marking.1588if (exp_marked_bytes > act_marked_bytes) {1589if (_verbose) {1590gclog_or_tty->print_cr("Region %u: marked bytes mismatch: "1591"expected: " SIZE_FORMAT ", actual: " SIZE_FORMAT,1592hr->hrm_index(), exp_marked_bytes, act_marked_bytes);1593}1594failures += 1;1595}15961597// Verify the bit, for this region, in the actual and expected1598// (which was just calculated) region bit maps.1599// We're not OK if the bit in the calculated expected region1600// bitmap is set and the bit in the actual region bitmap is not.1601BitMap::idx_t index = (BitMap::idx_t) hr->hrm_index();16021603bool expected = _exp_region_bm->at(index);1604bool actual = _region_bm->at(index);1605if (expected && !actual) {1606if (_verbose) {1607gclog_or_tty->print_cr("Region %u: region bitmap mismatch: "1608"expected: %s, actual: %s",1609hr->hrm_index(),1610BOOL_TO_STR(expected), BOOL_TO_STR(actual));1611}1612failures += 1;1613}16141615// Verify that the card bit maps for the cards spanned by the current1616// region match. We have an error if we have a set bit in the expected1617// bit map and the corresponding bit in the actual bitmap is not set.16181619BitMap::idx_t start_idx = _cm->card_bitmap_index_for(hr->bottom());1620BitMap::idx_t end_idx = _cm->card_bitmap_index_for(hr->top());16211622for (BitMap::idx_t i = start_idx; i < end_idx; i+=1) {1623expected = _exp_card_bm->at(i);1624actual = _card_bm->at(i);16251626if (expected && !actual) {1627if (_verbose) {1628gclog_or_tty->print_cr("Region %u: card bitmap mismatch at " SIZE_FORMAT ": "1629"expected: %s, actual: %s",1630hr->hrm_index(), i,1631BOOL_TO_STR(expected), BOOL_TO_STR(actual));1632}1633failures += 1;1634}1635}16361637if (failures > 0 && _verbose) {1638gclog_or_tty->print_cr("Region " HR_FORMAT ", ntams: " PTR_FORMAT ", "1639"marked_bytes: calc/actual " SIZE_FORMAT "/" SIZE_FORMAT,1640HR_FORMAT_PARAMS(hr), p2i(hr->next_top_at_mark_start()),1641_calc_cl.region_marked_bytes(), hr->next_marked_bytes());1642}16431644_failures += failures;16451646// We could stop iteration over the heap when we1647// find the first violating region by returning true.1648return false;1649}1650};16511652class G1ParVerifyFinalCountTask: public AbstractGangTask {1653protected:1654G1CollectedHeap* _g1h;1655ConcurrentMark* _cm;1656BitMap* _actual_region_bm;1657BitMap* _actual_card_bm;16581659uint _n_workers;16601661BitMap* _expected_region_bm;1662BitMap* _expected_card_bm;16631664int _failures;1665bool _verbose;16661667public:1668G1ParVerifyFinalCountTask(G1CollectedHeap* g1h,1669BitMap* region_bm, BitMap* card_bm,1670BitMap* expected_region_bm, BitMap* expected_card_bm)1671: AbstractGangTask("G1 verify final counting"),1672_g1h(g1h), _cm(_g1h->concurrent_mark()),1673_actual_region_bm(region_bm), _actual_card_bm(card_bm),1674_expected_region_bm(expected_region_bm), _expected_card_bm(expected_card_bm),1675_failures(0), _verbose(false),1676_n_workers(0) {1677assert(VerifyDuringGC, "don't call this otherwise");16781679// Use the value already set as the number of active threads1680// in the call to run_task().1681if (G1CollectedHeap::use_parallel_gc_threads()) {1682assert( _g1h->workers()->active_workers() > 0,1683"Should have been previously set");1684_n_workers = _g1h->workers()->active_workers();1685} else {1686_n_workers = 1;1687}16881689assert(_expected_card_bm->size() == _actual_card_bm->size(), "sanity");1690assert(_expected_region_bm->size() == _actual_region_bm->size(), "sanity");16911692_verbose = _cm->verbose_medium();1693}16941695void work(uint worker_id) {1696assert(worker_id < _n_workers, "invariant");16971698VerifyLiveObjectDataHRClosure verify_cl(_g1h,1699_actual_region_bm, _actual_card_bm,1700_expected_region_bm,1701_expected_card_bm,1702_verbose);17031704if (G1CollectedHeap::use_parallel_gc_threads()) {1705_g1h->heap_region_par_iterate_chunked(&verify_cl,1706worker_id,1707_n_workers,1708HeapRegion::VerifyCountClaimValue);1709} else {1710_g1h->heap_region_iterate(&verify_cl);1711}17121713Atomic::add(verify_cl.failures(), &_failures);1714}17151716int failures() const { return _failures; }1717};17181719// Closure that finalizes the liveness counting data.1720// Used during the cleanup pause.1721// Sets the bits corresponding to the interval [NTAMS, top]1722// (which contains the implicitly live objects) in the1723// card liveness bitmap. Also sets the bit for each region,1724// containing live data, in the region liveness bitmap.17251726class FinalCountDataUpdateClosure: public CMCountDataClosureBase {1727public:1728FinalCountDataUpdateClosure(G1CollectedHeap* g1h,1729BitMap* region_bm,1730BitMap* card_bm) :1731CMCountDataClosureBase(g1h, region_bm, card_bm) { }17321733bool doHeapRegion(HeapRegion* hr) {17341735if (hr->continuesHumongous()) {1736// We will ignore these here and process them when their1737// associated "starts humongous" region is processed (see1738// set_bit_for_heap_region()). Note that we cannot rely on their1739// associated "starts humongous" region to have their bit set to1740// 1 since, due to the region chunking in the parallel region1741// iteration, a "continues humongous" region might be visited1742// before its associated "starts humongous".1743return false;1744}17451746HeapWord* ntams = hr->next_top_at_mark_start();1747HeapWord* top = hr->top();17481749assert(hr->bottom() <= ntams && ntams <= hr->end(), "Preconditions.");17501751// Mark the allocated-since-marking portion...1752if (ntams < top) {1753// This definitely means the region has live objects.1754set_bit_for_region(hr);17551756// Now set the bits in the card bitmap for [ntams, top)1757BitMap::idx_t start_idx = _cm->card_bitmap_index_for(ntams);1758BitMap::idx_t end_idx = _cm->card_bitmap_index_for(top);17591760// Note: if we're looking at the last region in heap - top1761// could be actually just beyond the end of the heap; end_idx1762// will then correspond to a (non-existent) card that is also1763// just beyond the heap.1764if (_g1h->is_in_g1_reserved(top) && !_ct_bs->is_card_aligned(top)) {1765// end of object is not card aligned - increment to cover1766// all the cards spanned by the object1767end_idx += 1;1768}17691770assert(end_idx <= _card_bm->size(),1771err_msg("oob: end_idx= " SIZE_FORMAT ", bitmap size= " SIZE_FORMAT,1772end_idx, _card_bm->size()));1773assert(start_idx < _card_bm->size(),1774err_msg("oob: start_idx= " SIZE_FORMAT ", bitmap size= " SIZE_FORMAT,1775start_idx, _card_bm->size()));17761777_cm->set_card_bitmap_range(_card_bm, start_idx, end_idx, true /* is_par */);1778}17791780// Set the bit for the region if it contains live data1781if (hr->next_marked_bytes() > 0) {1782set_bit_for_region(hr);1783}17841785return false;1786}1787};17881789class G1ParFinalCountTask: public AbstractGangTask {1790protected:1791G1CollectedHeap* _g1h;1792ConcurrentMark* _cm;1793BitMap* _actual_region_bm;1794BitMap* _actual_card_bm;17951796uint _n_workers;17971798public:1799G1ParFinalCountTask(G1CollectedHeap* g1h, BitMap* region_bm, BitMap* card_bm)1800: AbstractGangTask("G1 final counting"),1801_g1h(g1h), _cm(_g1h->concurrent_mark()),1802_actual_region_bm(region_bm), _actual_card_bm(card_bm),1803_n_workers(0) {1804// Use the value already set as the number of active threads1805// in the call to run_task().1806if (G1CollectedHeap::use_parallel_gc_threads()) {1807assert( _g1h->workers()->active_workers() > 0,1808"Should have been previously set");1809_n_workers = _g1h->workers()->active_workers();1810} else {1811_n_workers = 1;1812}1813}18141815void work(uint worker_id) {1816assert(worker_id < _n_workers, "invariant");18171818FinalCountDataUpdateClosure final_update_cl(_g1h,1819_actual_region_bm,1820_actual_card_bm);18211822if (G1CollectedHeap::use_parallel_gc_threads()) {1823_g1h->heap_region_par_iterate_chunked(&final_update_cl,1824worker_id,1825_n_workers,1826HeapRegion::FinalCountClaimValue);1827} else {1828_g1h->heap_region_iterate(&final_update_cl);1829}1830}1831};18321833class G1ParNoteEndTask;18341835class G1NoteEndOfConcMarkClosure : public HeapRegionClosure {1836G1CollectedHeap* _g1;1837size_t _max_live_bytes;1838uint _regions_claimed;1839size_t _freed_bytes;1840FreeRegionList* _local_cleanup_list;1841HeapRegionSetCount _old_regions_removed;1842HeapRegionSetCount _humongous_regions_removed;1843HRRSCleanupTask* _hrrs_cleanup_task;1844double _claimed_region_time;1845double _max_region_time;18461847public:1848G1NoteEndOfConcMarkClosure(G1CollectedHeap* g1,1849FreeRegionList* local_cleanup_list,1850HRRSCleanupTask* hrrs_cleanup_task) :1851_g1(g1),1852_max_live_bytes(0), _regions_claimed(0),1853_freed_bytes(0),1854_claimed_region_time(0.0), _max_region_time(0.0),1855_local_cleanup_list(local_cleanup_list),1856_old_regions_removed(),1857_humongous_regions_removed(),1858_hrrs_cleanup_task(hrrs_cleanup_task) { }18591860size_t freed_bytes() { return _freed_bytes; }1861const HeapRegionSetCount& old_regions_removed() { return _old_regions_removed; }1862const HeapRegionSetCount& humongous_regions_removed() { return _humongous_regions_removed; }18631864bool doHeapRegion(HeapRegion *hr) {1865if (hr->continuesHumongous()) {1866return false;1867}1868// We use a claim value of zero here because all regions1869// were claimed with value 1 in the FinalCount task.1870_g1->reset_gc_time_stamps(hr);1871double start = os::elapsedTime();1872_regions_claimed++;1873hr->note_end_of_marking();1874_max_live_bytes += hr->max_live_bytes();18751876if (hr->used() > 0 && hr->max_live_bytes() == 0 && !hr->is_young()) {1877_freed_bytes += hr->used();1878hr->set_containing_set(NULL);1879if (hr->isHumongous()) {1880assert(hr->startsHumongous(), "we should only see starts humongous");1881_humongous_regions_removed.increment(1u, hr->capacity());1882_g1->free_humongous_region(hr, _local_cleanup_list, true);1883} else {1884_old_regions_removed.increment(1u, hr->capacity());1885_g1->free_region(hr, _local_cleanup_list, true);1886}1887} else {1888hr->rem_set()->do_cleanup_work(_hrrs_cleanup_task);1889}18901891double region_time = (os::elapsedTime() - start);1892_claimed_region_time += region_time;1893if (region_time > _max_region_time) {1894_max_region_time = region_time;1895}1896return false;1897}18981899size_t max_live_bytes() { return _max_live_bytes; }1900uint regions_claimed() { return _regions_claimed; }1901double claimed_region_time_sec() { return _claimed_region_time; }1902double max_region_time_sec() { return _max_region_time; }1903};19041905class G1ParNoteEndTask: public AbstractGangTask {1906friend class G1NoteEndOfConcMarkClosure;19071908protected:1909G1CollectedHeap* _g1h;1910size_t _max_live_bytes;1911size_t _freed_bytes;1912FreeRegionList* _cleanup_list;19131914public:1915G1ParNoteEndTask(G1CollectedHeap* g1h,1916FreeRegionList* cleanup_list) :1917AbstractGangTask("G1 note end"), _g1h(g1h),1918_max_live_bytes(0), _freed_bytes(0), _cleanup_list(cleanup_list) { }19191920void work(uint worker_id) {1921double start = os::elapsedTime();1922FreeRegionList local_cleanup_list("Local Cleanup List");1923HRRSCleanupTask hrrs_cleanup_task;1924G1NoteEndOfConcMarkClosure g1_note_end(_g1h, &local_cleanup_list,1925&hrrs_cleanup_task);1926if (G1CollectedHeap::use_parallel_gc_threads()) {1927_g1h->heap_region_par_iterate_chunked(&g1_note_end, worker_id,1928_g1h->workers()->active_workers(),1929HeapRegion::NoteEndClaimValue);1930} else {1931_g1h->heap_region_iterate(&g1_note_end);1932}1933assert(g1_note_end.complete(), "Shouldn't have yielded!");19341935// Now update the lists1936_g1h->remove_from_old_sets(g1_note_end.old_regions_removed(), g1_note_end.humongous_regions_removed());1937{1938MutexLockerEx x(ParGCRareEvent_lock, Mutex::_no_safepoint_check_flag);1939_g1h->decrement_summary_bytes(g1_note_end.freed_bytes());1940_max_live_bytes += g1_note_end.max_live_bytes();1941_freed_bytes += g1_note_end.freed_bytes();19421943// If we iterate over the global cleanup list at the end of1944// cleanup to do this printing we will not guarantee to only1945// generate output for the newly-reclaimed regions (the list1946// might not be empty at the beginning of cleanup; we might1947// still be working on its previous contents). So we do the1948// printing here, before we append the new regions to the global1949// cleanup list.19501951G1HRPrinter* hr_printer = _g1h->hr_printer();1952if (hr_printer->is_active()) {1953FreeRegionListIterator iter(&local_cleanup_list);1954while (iter.more_available()) {1955HeapRegion* hr = iter.get_next();1956hr_printer->cleanup(hr);1957}1958}19591960_cleanup_list->add_ordered(&local_cleanup_list);1961assert(local_cleanup_list.is_empty(), "post-condition");19621963HeapRegionRemSet::finish_cleanup_task(&hrrs_cleanup_task);1964}1965}1966size_t max_live_bytes() { return _max_live_bytes; }1967size_t freed_bytes() { return _freed_bytes; }1968};19691970class G1ParScrubRemSetTask: public AbstractGangTask {1971protected:1972G1RemSet* _g1rs;1973BitMap* _region_bm;1974BitMap* _card_bm;1975public:1976G1ParScrubRemSetTask(G1CollectedHeap* g1h,1977BitMap* region_bm, BitMap* card_bm) :1978AbstractGangTask("G1 ScrubRS"), _g1rs(g1h->g1_rem_set()),1979_region_bm(region_bm), _card_bm(card_bm) { }19801981void work(uint worker_id) {1982if (G1CollectedHeap::use_parallel_gc_threads()) {1983_g1rs->scrub_par(_region_bm, _card_bm, worker_id,1984HeapRegion::ScrubRemSetClaimValue);1985} else {1986_g1rs->scrub(_region_bm, _card_bm);1987}1988}19891990};19911992void ConcurrentMark::cleanup() {1993// world is stopped at this checkpoint1994assert(SafepointSynchronize::is_at_safepoint(),1995"world should be stopped");1996G1CollectedHeap* g1h = G1CollectedHeap::heap();19971998// If a full collection has happened, we shouldn't do this.1999if (has_aborted()) {2000g1h->set_marking_complete(); // So bitmap clearing isn't confused2001return;2002}20032004g1h->verify_region_sets_optional();20052006if (VerifyDuringGC) {2007HandleMark hm; // handle scope2008Universe::heap()->prepare_for_verify();2009Universe::verify(VerifyOption_G1UsePrevMarking,2010" VerifyDuringGC:(before)");2011}2012g1h->check_bitmaps("Cleanup Start");20132014G1CollectorPolicy* g1p = G1CollectedHeap::heap()->g1_policy();2015g1p->record_concurrent_mark_cleanup_start();20162017double start = os::elapsedTime();20182019HeapRegionRemSet::reset_for_cleanup_tasks();20202021uint n_workers;20222023// Do counting once more with the world stopped for good measure.2024G1ParFinalCountTask g1_par_count_task(g1h, &_region_bm, &_card_bm);20252026if (G1CollectedHeap::use_parallel_gc_threads()) {2027assert(g1h->check_heap_region_claim_values(HeapRegion::InitialClaimValue),2028"sanity check");20292030g1h->set_par_threads();2031n_workers = g1h->n_par_threads();2032assert(g1h->n_par_threads() == n_workers,2033"Should not have been reset");2034g1h->workers()->run_task(&g1_par_count_task);2035// Done with the parallel phase so reset to 0.2036g1h->set_par_threads(0);20372038assert(g1h->check_heap_region_claim_values(HeapRegion::FinalCountClaimValue),2039"sanity check");2040} else {2041n_workers = 1;2042g1_par_count_task.work(0);2043}20442045if (VerifyDuringGC) {2046// Verify that the counting data accumulated during marking matches2047// that calculated by walking the marking bitmap.20482049// Bitmaps to hold expected values2050BitMap expected_region_bm(_region_bm.size(), true);2051BitMap expected_card_bm(_card_bm.size(), true);20522053G1ParVerifyFinalCountTask g1_par_verify_task(g1h,2054&_region_bm,2055&_card_bm,2056&expected_region_bm,2057&expected_card_bm);20582059if (G1CollectedHeap::use_parallel_gc_threads()) {2060g1h->set_par_threads((int)n_workers);2061g1h->workers()->run_task(&g1_par_verify_task);2062// Done with the parallel phase so reset to 0.2063g1h->set_par_threads(0);20642065assert(g1h->check_heap_region_claim_values(HeapRegion::VerifyCountClaimValue),2066"sanity check");2067} else {2068g1_par_verify_task.work(0);2069}20702071guarantee(g1_par_verify_task.failures() == 0, "Unexpected accounting failures");2072}20732074size_t start_used_bytes = g1h->used();2075g1h->set_marking_complete();20762077double count_end = os::elapsedTime();2078double this_final_counting_time = (count_end - start);2079_total_counting_time += this_final_counting_time;20802081if (G1PrintRegionLivenessInfo) {2082G1PrintRegionLivenessInfoClosure cl(gclog_or_tty, "Post-Marking");2083_g1h->heap_region_iterate(&cl);2084}20852086// Install newly created mark bitMap as "prev".2087swapMarkBitMaps();20882089g1h->reset_gc_time_stamp();20902091// Note end of marking in all heap regions.2092G1ParNoteEndTask g1_par_note_end_task(g1h, &_cleanup_list);2093if (G1CollectedHeap::use_parallel_gc_threads()) {2094g1h->set_par_threads((int)n_workers);2095g1h->workers()->run_task(&g1_par_note_end_task);2096g1h->set_par_threads(0);20972098assert(g1h->check_heap_region_claim_values(HeapRegion::NoteEndClaimValue),2099"sanity check");2100} else {2101g1_par_note_end_task.work(0);2102}2103g1h->check_gc_time_stamps();21042105if (!cleanup_list_is_empty()) {2106// The cleanup list is not empty, so we'll have to process it2107// concurrently. Notify anyone else that might be wanting free2108// regions that there will be more free regions coming soon.2109g1h->set_free_regions_coming();2110}21112112// call below, since it affects the metric by which we sort the heap2113// regions.2114if (G1ScrubRemSets) {2115double rs_scrub_start = os::elapsedTime();2116G1ParScrubRemSetTask g1_par_scrub_rs_task(g1h, &_region_bm, &_card_bm);2117if (G1CollectedHeap::use_parallel_gc_threads()) {2118g1h->set_par_threads((int)n_workers);2119g1h->workers()->run_task(&g1_par_scrub_rs_task);2120g1h->set_par_threads(0);21212122assert(g1h->check_heap_region_claim_values(2123HeapRegion::ScrubRemSetClaimValue),2124"sanity check");2125} else {2126g1_par_scrub_rs_task.work(0);2127}21282129double rs_scrub_end = os::elapsedTime();2130double this_rs_scrub_time = (rs_scrub_end - rs_scrub_start);2131_total_rs_scrub_time += this_rs_scrub_time;2132}21332134// this will also free any regions totally full of garbage objects,2135// and sort the regions.2136g1h->g1_policy()->record_concurrent_mark_cleanup_end((int)n_workers);21372138// Statistics.2139double end = os::elapsedTime();2140_cleanup_times.add((end - start) * 1000.0);21412142if (G1Log::fine()) {2143g1h->print_size_transition(gclog_or_tty,2144start_used_bytes,2145g1h->used(),2146g1h->capacity());2147}21482149// Clean up will have freed any regions completely full of garbage.2150// Update the soft reference policy with the new heap occupancy.2151Universe::update_heap_info_at_gc();21522153if (VerifyDuringGC) {2154HandleMark hm; // handle scope2155Universe::heap()->prepare_for_verify();2156Universe::verify(VerifyOption_G1UsePrevMarking,2157" VerifyDuringGC:(after)");2158}2159g1h->check_bitmaps("Cleanup End");21602161g1h->verify_region_sets_optional();21622163// We need to make this be a "collection" so any collection pause that2164// races with it goes around and waits for completeCleanup to finish.2165g1h->increment_total_collections();21662167// Clean out dead classes and update Metaspace sizes.2168if (ClassUnloadingWithConcurrentMark) {2169ClassLoaderDataGraph::purge();2170}2171MetaspaceGC::compute_new_size();21722173// We reclaimed old regions so we should calculate the sizes to make2174// sure we update the old gen/space data.2175g1h->g1mm()->update_sizes();2176g1h->allocation_context_stats().update_after_mark();21772178g1h->trace_heap_after_concurrent_cycle();2179}21802181void ConcurrentMark::completeCleanup() {2182if (has_aborted()) return;21832184G1CollectedHeap* g1h = G1CollectedHeap::heap();21852186_cleanup_list.verify_optional();2187FreeRegionList tmp_free_list("Tmp Free List");21882189if (G1ConcRegionFreeingVerbose) {2190gclog_or_tty->print_cr("G1ConcRegionFreeing [complete cleanup] : "2191"cleanup list has %u entries",2192_cleanup_list.length());2193}21942195// No one else should be accessing the _cleanup_list at this point,2196// so it is not necessary to take any locks2197while (!_cleanup_list.is_empty()) {2198HeapRegion* hr = _cleanup_list.remove_region(true /* from_head */);2199assert(hr != NULL, "Got NULL from a non-empty list");2200hr->par_clear();2201tmp_free_list.add_ordered(hr);22022203// Instead of adding one region at a time to the secondary_free_list,2204// we accumulate them in the local list and move them a few at a2205// time. This also cuts down on the number of notify_all() calls2206// we do during this process. We'll also append the local list when2207// _cleanup_list is empty (which means we just removed the last2208// region from the _cleanup_list).2209if ((tmp_free_list.length() % G1SecondaryFreeListAppendLength == 0) ||2210_cleanup_list.is_empty()) {2211if (G1ConcRegionFreeingVerbose) {2212gclog_or_tty->print_cr("G1ConcRegionFreeing [complete cleanup] : "2213"appending %u entries to the secondary_free_list, "2214"cleanup list still has %u entries",2215tmp_free_list.length(),2216_cleanup_list.length());2217}22182219{2220MutexLockerEx x(SecondaryFreeList_lock, Mutex::_no_safepoint_check_flag);2221g1h->secondary_free_list_add(&tmp_free_list);2222SecondaryFreeList_lock->notify_all();2223}22242225if (G1StressConcRegionFreeing) {2226for (uintx i = 0; i < G1StressConcRegionFreeingDelayMillis; ++i) {2227os::sleep(Thread::current(), (jlong) 1, false);2228}2229}2230}2231}2232assert(tmp_free_list.is_empty(), "post-condition");2233}22342235// Supporting Object and Oop closures for reference discovery2236// and processing in during marking22372238bool G1CMIsAliveClosure::do_object_b(oop obj) {2239HeapWord* addr = (HeapWord*)obj;2240return addr != NULL &&2241(!_g1->is_in_g1_reserved(addr) || !_g1->is_obj_ill(obj));2242}22432244// 'Keep Alive' oop closure used by both serial parallel reference processing.2245// Uses the CMTask associated with a worker thread (for serial reference2246// processing the CMTask for worker 0 is used) to preserve (mark) and2247// trace referent objects.2248//2249// Using the CMTask and embedded local queues avoids having the worker2250// threads operating on the global mark stack. This reduces the risk2251// of overflowing the stack - which we would rather avoid at this late2252// state. Also using the tasks' local queues removes the potential2253// of the workers interfering with each other that could occur if2254// operating on the global stack.22552256class G1CMKeepAliveAndDrainClosure: public OopClosure {2257ConcurrentMark* _cm;2258CMTask* _task;2259int _ref_counter_limit;2260int _ref_counter;2261bool _is_serial;2262public:2263G1CMKeepAliveAndDrainClosure(ConcurrentMark* cm, CMTask* task, bool is_serial) :2264_cm(cm), _task(task), _is_serial(is_serial),2265_ref_counter_limit(G1RefProcDrainInterval) {2266assert(_ref_counter_limit > 0, "sanity");2267assert(!_is_serial || _task->worker_id() == 0, "only task 0 for serial code");2268_ref_counter = _ref_counter_limit;2269}22702271virtual void do_oop(narrowOop* p) { do_oop_work(p); }2272virtual void do_oop( oop* p) { do_oop_work(p); }22732274template <class T> void do_oop_work(T* p) {2275if (!_cm->has_overflown()) {2276oop obj = oopDesc::load_decode_heap_oop(p);2277if (_cm->verbose_high()) {2278gclog_or_tty->print_cr("\t[%u] we're looking at location "2279"*" PTR_FORMAT " = " PTR_FORMAT,2280_task->worker_id(), p2i(p), p2i((void*) obj));2281}22822283_task->deal_with_reference(obj);2284_ref_counter--;22852286if (_ref_counter == 0) {2287// We have dealt with _ref_counter_limit references, pushing them2288// and objects reachable from them on to the local stack (and2289// possibly the global stack). Call CMTask::do_marking_step() to2290// process these entries.2291//2292// We call CMTask::do_marking_step() in a loop, which we'll exit if2293// there's nothing more to do (i.e. we're done with the entries that2294// were pushed as a result of the CMTask::deal_with_reference() calls2295// above) or we overflow.2296//2297// Note: CMTask::do_marking_step() can set the CMTask::has_aborted()2298// flag while there may still be some work to do. (See the comment at2299// the beginning of CMTask::do_marking_step() for those conditions -2300// one of which is reaching the specified time target.) It is only2301// when CMTask::do_marking_step() returns without setting the2302// has_aborted() flag that the marking step has completed.2303do {2304double mark_step_duration_ms = G1ConcMarkStepDurationMillis;2305_task->do_marking_step(mark_step_duration_ms,2306false /* do_termination */,2307_is_serial);2308} while (_task->has_aborted() && !_cm->has_overflown());2309_ref_counter = _ref_counter_limit;2310}2311} else {2312if (_cm->verbose_high()) {2313gclog_or_tty->print_cr("\t[%u] CM Overflow", _task->worker_id());2314}2315}2316}2317};23182319// 'Drain' oop closure used by both serial and parallel reference processing.2320// Uses the CMTask associated with a given worker thread (for serial2321// reference processing the CMtask for worker 0 is used). Calls the2322// do_marking_step routine, with an unbelievably large timeout value,2323// to drain the marking data structures of the remaining entries2324// added by the 'keep alive' oop closure above.23252326class G1CMDrainMarkingStackClosure: public VoidClosure {2327ConcurrentMark* _cm;2328CMTask* _task;2329bool _is_serial;2330public:2331G1CMDrainMarkingStackClosure(ConcurrentMark* cm, CMTask* task, bool is_serial) :2332_cm(cm), _task(task), _is_serial(is_serial) {2333assert(!_is_serial || _task->worker_id() == 0, "only task 0 for serial code");2334}23352336void do_void() {2337do {2338if (_cm->verbose_high()) {2339gclog_or_tty->print_cr("\t[%u] Drain: Calling do_marking_step - serial: %s",2340_task->worker_id(), BOOL_TO_STR(_is_serial));2341}23422343// We call CMTask::do_marking_step() to completely drain the local2344// and global marking stacks of entries pushed by the 'keep alive'2345// oop closure (an instance of G1CMKeepAliveAndDrainClosure above).2346//2347// CMTask::do_marking_step() is called in a loop, which we'll exit2348// if there's nothing more to do (i.e. we'completely drained the2349// entries that were pushed as a a result of applying the 'keep alive'2350// closure to the entries on the discovered ref lists) or we overflow2351// the global marking stack.2352//2353// Note: CMTask::do_marking_step() can set the CMTask::has_aborted()2354// flag while there may still be some work to do. (See the comment at2355// the beginning of CMTask::do_marking_step() for those conditions -2356// one of which is reaching the specified time target.) It is only2357// when CMTask::do_marking_step() returns without setting the2358// has_aborted() flag that the marking step has completed.23592360_task->do_marking_step(1000000000.0 /* something very large */,2361true /* do_termination */,2362_is_serial);2363} while (_task->has_aborted() && !_cm->has_overflown());2364}2365};23662367// Implementation of AbstractRefProcTaskExecutor for parallel2368// reference processing at the end of G1 concurrent marking23692370class G1CMRefProcTaskExecutor: public AbstractRefProcTaskExecutor {2371private:2372G1CollectedHeap* _g1h;2373ConcurrentMark* _cm;2374WorkGang* _workers;2375int _active_workers;23762377public:2378G1CMRefProcTaskExecutor(G1CollectedHeap* g1h,2379ConcurrentMark* cm,2380WorkGang* workers,2381int n_workers) :2382_g1h(g1h), _cm(cm),2383_workers(workers), _active_workers(n_workers) { }23842385// Executes the given task using concurrent marking worker threads.2386virtual void execute(ProcessTask& task);2387virtual void execute(EnqueueTask& task);2388};23892390class G1CMRefProcTaskProxy: public AbstractGangTask {2391typedef AbstractRefProcTaskExecutor::ProcessTask ProcessTask;2392ProcessTask& _proc_task;2393G1CollectedHeap* _g1h;2394ConcurrentMark* _cm;23952396public:2397G1CMRefProcTaskProxy(ProcessTask& proc_task,2398G1CollectedHeap* g1h,2399ConcurrentMark* cm) :2400AbstractGangTask("Process reference objects in parallel"),2401_proc_task(proc_task), _g1h(g1h), _cm(cm) {2402ReferenceProcessor* rp = _g1h->ref_processor_cm();2403assert(rp->processing_is_mt(), "shouldn't be here otherwise");2404}24052406virtual void work(uint worker_id) {2407ResourceMark rm;2408HandleMark hm;2409CMTask* task = _cm->task(worker_id);2410G1CMIsAliveClosure g1_is_alive(_g1h);2411G1CMKeepAliveAndDrainClosure g1_par_keep_alive(_cm, task, false /* is_serial */);2412G1CMDrainMarkingStackClosure g1_par_drain(_cm, task, false /* is_serial */);24132414_proc_task.work(worker_id, g1_is_alive, g1_par_keep_alive, g1_par_drain);2415}2416};24172418void G1CMRefProcTaskExecutor::execute(ProcessTask& proc_task) {2419assert(_workers != NULL, "Need parallel worker threads.");2420assert(_g1h->ref_processor_cm()->processing_is_mt(), "processing is not MT");24212422G1CMRefProcTaskProxy proc_task_proxy(proc_task, _g1h, _cm);24232424// We need to reset the concurrency level before each2425// proxy task execution, so that the termination protocol2426// and overflow handling in CMTask::do_marking_step() knows2427// how many workers to wait for.2428_cm->set_concurrency(_active_workers);2429_g1h->set_par_threads(_active_workers);2430_workers->run_task(&proc_task_proxy);2431_g1h->set_par_threads(0);2432}24332434class G1CMRefEnqueueTaskProxy: public AbstractGangTask {2435typedef AbstractRefProcTaskExecutor::EnqueueTask EnqueueTask;2436EnqueueTask& _enq_task;24372438public:2439G1CMRefEnqueueTaskProxy(EnqueueTask& enq_task) :2440AbstractGangTask("Enqueue reference objects in parallel"),2441_enq_task(enq_task) { }24422443virtual void work(uint worker_id) {2444_enq_task.work(worker_id);2445}2446};24472448void G1CMRefProcTaskExecutor::execute(EnqueueTask& enq_task) {2449assert(_workers != NULL, "Need parallel worker threads.");2450assert(_g1h->ref_processor_cm()->processing_is_mt(), "processing is not MT");24512452G1CMRefEnqueueTaskProxy enq_task_proxy(enq_task);24532454// Not strictly necessary but...2455//2456// We need to reset the concurrency level before each2457// proxy task execution, so that the termination protocol2458// and overflow handling in CMTask::do_marking_step() knows2459// how many workers to wait for.2460_cm->set_concurrency(_active_workers);2461_g1h->set_par_threads(_active_workers);2462_workers->run_task(&enq_task_proxy);2463_g1h->set_par_threads(0);2464}24652466void ConcurrentMark::weakRefsWorkParallelPart(BoolObjectClosure* is_alive, bool purged_classes) {2467G1CollectedHeap::heap()->parallel_cleaning(is_alive, true, true, purged_classes);2468}24692470// Helper class to get rid of some boilerplate code.2471class G1RemarkGCTraceTime : public GCTraceTime {2472static bool doit_and_prepend(bool doit) {2473if (doit) {2474gclog_or_tty->put(' ');2475}2476return doit;2477}24782479public:2480G1RemarkGCTraceTime(const char* title, bool doit)2481: GCTraceTime(title, doit_and_prepend(doit), false, G1CollectedHeap::heap()->gc_timer_cm(),2482G1CollectedHeap::heap()->concurrent_mark()->concurrent_gc_id()) {2483}2484};24852486void ConcurrentMark::weakRefsWork(bool clear_all_soft_refs) {2487if (has_overflown()) {2488// Skip processing the discovered references if we have2489// overflown the global marking stack. Reference objects2490// only get discovered once so it is OK to not2491// de-populate the discovered reference lists. We could have,2492// but the only benefit would be that, when marking restarts,2493// less reference objects are discovered.2494return;2495}24962497ResourceMark rm;2498HandleMark hm;24992500G1CollectedHeap* g1h = G1CollectedHeap::heap();25012502// Is alive closure.2503G1CMIsAliveClosure g1_is_alive(g1h);25042505// Inner scope to exclude the cleaning of the string and symbol2506// tables from the displayed time.2507{2508if (G1Log::finer()) {2509gclog_or_tty->put(' ');2510}2511GCTraceTime t("GC ref-proc", G1Log::finer(), false, g1h->gc_timer_cm(), concurrent_gc_id());25122513ReferenceProcessor* rp = g1h->ref_processor_cm();25142515// See the comment in G1CollectedHeap::ref_processing_init()2516// about how reference processing currently works in G1.25172518// Set the soft reference policy2519rp->setup_policy(clear_all_soft_refs);2520assert(_markStack.isEmpty(), "mark stack should be empty");25212522// Instances of the 'Keep Alive' and 'Complete GC' closures used2523// in serial reference processing. Note these closures are also2524// used for serially processing (by the the current thread) the2525// JNI references during parallel reference processing.2526//2527// These closures do not need to synchronize with the worker2528// threads involved in parallel reference processing as these2529// instances are executed serially by the current thread (e.g.2530// reference processing is not multi-threaded and is thus2531// performed by the current thread instead of a gang worker).2532//2533// The gang tasks involved in parallel reference procssing create2534// their own instances of these closures, which do their own2535// synchronization among themselves.2536G1CMKeepAliveAndDrainClosure g1_keep_alive(this, task(0), true /* is_serial */);2537G1CMDrainMarkingStackClosure g1_drain_mark_stack(this, task(0), true /* is_serial */);25382539// We need at least one active thread. If reference processing2540// is not multi-threaded we use the current (VMThread) thread,2541// otherwise we use the work gang from the G1CollectedHeap and2542// we utilize all the worker threads we can.2543bool processing_is_mt = rp->processing_is_mt() && g1h->workers() != NULL;2544uint active_workers = (processing_is_mt ? g1h->workers()->active_workers() : 1U);2545active_workers = MAX2(MIN2(active_workers, _max_worker_id), 1U);25462547// Parallel processing task executor.2548G1CMRefProcTaskExecutor par_task_executor(g1h, this,2549g1h->workers(), active_workers);2550AbstractRefProcTaskExecutor* executor = (processing_is_mt ? &par_task_executor : NULL);25512552// Set the concurrency level. The phase was already set prior to2553// executing the remark task.2554set_concurrency(active_workers);25552556// Set the degree of MT processing here. If the discovery was done MT,2557// the number of threads involved during discovery could differ from2558// the number of active workers. This is OK as long as the discovered2559// Reference lists are balanced (see balance_all_queues() and balance_queues()).2560rp->set_active_mt_degree(active_workers);25612562// Process the weak references.2563const ReferenceProcessorStats& stats =2564rp->process_discovered_references(&g1_is_alive,2565&g1_keep_alive,2566&g1_drain_mark_stack,2567executor,2568g1h->gc_timer_cm(),2569concurrent_gc_id());2570g1h->gc_tracer_cm()->report_gc_reference_stats(stats);25712572// The do_oop work routines of the keep_alive and drain_marking_stack2573// oop closures will set the has_overflown flag if we overflow the2574// global marking stack.25752576assert(_markStack.overflow() || _markStack.isEmpty(),2577"mark stack should be empty (unless it overflowed)");25782579if (_markStack.overflow()) {2580// This should have been done already when we tried to push an2581// entry on to the global mark stack. But let's do it again.2582set_has_overflown();2583}25842585assert(rp->num_q() == active_workers, "why not");25862587rp->enqueue_discovered_references(executor);25882589rp->verify_no_references_recorded();2590assert(!rp->discovery_enabled(), "Post condition");2591}25922593if (has_overflown()) {2594// We can not trust g1_is_alive if the marking stack overflowed2595return;2596}25972598assert(_markStack.isEmpty(), "Marking should have completed");25992600// Unload Klasses, String, Symbols, Code Cache, etc.2601{2602G1RemarkGCTraceTime trace("Unloading", G1Log::finer());26032604if (ClassUnloadingWithConcurrentMark) {2605// Cleaning of klasses depends on correct information from MetadataMarkOnStack. The CodeCache::mark_on_stack2606// part is too slow to be done serially, so it is handled during the weakRefsWorkParallelPart phase.2607// Defer the cleaning until we have complete on_stack data.2608MetadataOnStackMark md_on_stack(false /* Don't visit the code cache at this point */);26092610bool purged_classes;26112612{2613G1RemarkGCTraceTime trace("System Dictionary Unloading", G1Log::finest());2614purged_classes = SystemDictionary::do_unloading(&g1_is_alive, false /* Defer klass cleaning */);2615}26162617{2618G1RemarkGCTraceTime trace("Parallel Unloading", G1Log::finest());2619weakRefsWorkParallelPart(&g1_is_alive, purged_classes);2620}26212622{2623G1RemarkGCTraceTime trace("Deallocate Metadata", G1Log::finest());2624ClassLoaderDataGraph::free_deallocate_lists();2625}2626}26272628if (G1StringDedup::is_enabled()) {2629G1RemarkGCTraceTime trace("String Deduplication Unlink", G1Log::finest());2630G1StringDedup::unlink(&g1_is_alive);2631}2632}2633}26342635void ConcurrentMark::swapMarkBitMaps() {2636CMBitMapRO* temp = _prevMarkBitMap;2637_prevMarkBitMap = (CMBitMapRO*)_nextMarkBitMap;2638_nextMarkBitMap = (CMBitMap*) temp;2639}26402641// Closure for marking entries in SATB buffers.2642class CMSATBBufferClosure : public SATBBufferClosure {2643private:2644CMTask* _task;2645G1CollectedHeap* _g1h;26462647// This is very similar to CMTask::deal_with_reference, but with2648// more relaxed requirements for the argument, so this must be more2649// circumspect about treating the argument as an object.2650void do_entry(void* entry) const {2651_task->increment_refs_reached();2652HeapRegion* hr = _g1h->heap_region_containing_raw(entry);2653if (entry < hr->next_top_at_mark_start()) {2654// Until we get here, we don't know whether entry refers to a valid2655// object; it could instead have been a stale reference.2656oop obj = static_cast<oop>(entry);2657assert(obj->is_oop(true /* ignore mark word */),2658err_msg("Invalid oop in SATB buffer: " PTR_FORMAT, p2i(obj)));2659_task->make_reference_grey(obj, hr);2660}2661}26622663public:2664CMSATBBufferClosure(CMTask* task, G1CollectedHeap* g1h)2665: _task(task), _g1h(g1h) { }26662667virtual void do_buffer(void** buffer, size_t size) {2668for (size_t i = 0; i < size; ++i) {2669do_entry(buffer[i]);2670}2671}2672};26732674class G1RemarkThreadsClosure : public ThreadClosure {2675CMSATBBufferClosure _cm_satb_cl;2676G1CMOopClosure _cm_cl;2677MarkingCodeBlobClosure _code_cl;2678int _thread_parity;2679bool _is_par;26802681public:2682G1RemarkThreadsClosure(G1CollectedHeap* g1h, CMTask* task, bool is_par) :2683_cm_satb_cl(task, g1h),2684_cm_cl(g1h, g1h->concurrent_mark(), task),2685_code_cl(&_cm_cl, !CodeBlobToOopClosure::FixRelocations),2686_thread_parity(SharedHeap::heap()->strong_roots_parity()), _is_par(is_par) {}26872688void do_thread(Thread* thread) {2689if (thread->is_Java_thread()) {2690if (thread->claim_oops_do(_is_par, _thread_parity)) {2691JavaThread* jt = (JavaThread*)thread;26922693// In theory it should not be neccessary to explicitly walk the nmethods to find roots for concurrent marking2694// however the liveness of oops reachable from nmethods have very complex lifecycles:2695// * Alive if on the stack of an executing method2696// * Weakly reachable otherwise2697// Some objects reachable from nmethods, such as the class loader (or klass_holder) of the receiver should be2698// live by the SATB invariant but other oops recorded in nmethods may behave differently.2699jt->nmethods_do(&_code_cl);27002701jt->satb_mark_queue().apply_closure_and_empty(&_cm_satb_cl);2702}2703} else if (thread->is_VM_thread()) {2704if (thread->claim_oops_do(_is_par, _thread_parity)) {2705JavaThread::satb_mark_queue_set().shared_satb_queue()->apply_closure_and_empty(&_cm_satb_cl);2706}2707}2708}2709};27102711class CMRemarkTask: public AbstractGangTask {2712private:2713ConcurrentMark* _cm;2714bool _is_serial;2715public:2716void work(uint worker_id) {2717// Since all available tasks are actually started, we should2718// only proceed if we're supposed to be actived.2719if (worker_id < _cm->active_tasks()) {2720CMTask* task = _cm->task(worker_id);2721task->record_start_time();2722{2723ResourceMark rm;2724HandleMark hm;27252726G1RemarkThreadsClosure threads_f(G1CollectedHeap::heap(), task, !_is_serial);2727Threads::threads_do(&threads_f);2728}27292730do {2731task->do_marking_step(1000000000.0 /* something very large */,2732true /* do_termination */,2733_is_serial);2734} while (task->has_aborted() && !_cm->has_overflown());2735// If we overflow, then we do not want to restart. We instead2736// want to abort remark and do concurrent marking again.2737task->record_end_time();2738}2739}27402741CMRemarkTask(ConcurrentMark* cm, int active_workers, bool is_serial) :2742AbstractGangTask("Par Remark"), _cm(cm), _is_serial(is_serial) {2743_cm->terminator()->reset_for_reuse(active_workers);2744}2745};27462747void ConcurrentMark::checkpointRootsFinalWork() {2748ResourceMark rm;2749HandleMark hm;2750G1CollectedHeap* g1h = G1CollectedHeap::heap();27512752G1RemarkGCTraceTime trace("Finalize Marking", G1Log::finer());27532754g1h->ensure_parsability(false);27552756if (G1CollectedHeap::use_parallel_gc_threads()) {2757G1CollectedHeap::StrongRootsScope srs(g1h);2758// this is remark, so we'll use up all active threads2759uint active_workers = g1h->workers()->active_workers();2760if (active_workers == 0) {2761assert(active_workers > 0, "Should have been set earlier");2762active_workers = (uint) ParallelGCThreads;2763g1h->workers()->set_active_workers(active_workers);2764}2765set_concurrency_and_phase(active_workers, false /* concurrent */);2766// Leave _parallel_marking_threads at it's2767// value originally calculated in the ConcurrentMark2768// constructor and pass values of the active workers2769// through the gang in the task.27702771CMRemarkTask remarkTask(this, active_workers, false /* is_serial */);2772// We will start all available threads, even if we decide that the2773// active_workers will be fewer. The extra ones will just bail out2774// immediately.2775g1h->set_par_threads(active_workers);2776g1h->workers()->run_task(&remarkTask);2777g1h->set_par_threads(0);2778} else {2779G1CollectedHeap::StrongRootsScope srs(g1h);2780uint active_workers = 1;2781set_concurrency_and_phase(active_workers, false /* concurrent */);27822783// Note - if there's no work gang then the VMThread will be2784// the thread to execute the remark - serially. We have2785// to pass true for the is_serial parameter so that2786// CMTask::do_marking_step() doesn't enter the sync2787// barriers in the event of an overflow. Doing so will2788// cause an assert that the current thread is not a2789// concurrent GC thread.2790CMRemarkTask remarkTask(this, active_workers, true /* is_serial*/);2791remarkTask.work(0);2792}2793SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();2794guarantee(has_overflown() ||2795satb_mq_set.completed_buffers_num() == 0,2796err_msg("Invariant: has_overflown = %s, num buffers = %d",2797BOOL_TO_STR(has_overflown()),2798satb_mq_set.completed_buffers_num()));27992800print_stats();2801}28022803#ifndef PRODUCT28042805class PrintReachableOopClosure: public OopClosure {2806private:2807G1CollectedHeap* _g1h;2808outputStream* _out;2809VerifyOption _vo;2810bool _all;28112812public:2813PrintReachableOopClosure(outputStream* out,2814VerifyOption vo,2815bool all) :2816_g1h(G1CollectedHeap::heap()),2817_out(out), _vo(vo), _all(all) { }28182819void do_oop(narrowOop* p) { do_oop_work(p); }2820void do_oop( oop* p) { do_oop_work(p); }28212822template <class T> void do_oop_work(T* p) {2823oop obj = oopDesc::load_decode_heap_oop(p);2824const char* str = NULL;2825const char* str2 = "";28262827if (obj == NULL) {2828str = "";2829} else if (!_g1h->is_in_g1_reserved(obj)) {2830str = " O";2831} else {2832HeapRegion* hr = _g1h->heap_region_containing(obj);2833bool over_tams = _g1h->allocated_since_marking(obj, hr, _vo);2834bool marked = _g1h->is_marked(obj, _vo);28352836if (over_tams) {2837str = " >";2838if (marked) {2839str2 = " AND MARKED";2840}2841} else if (marked) {2842str = " M";2843} else {2844str = " NOT";2845}2846}28472848_out->print_cr(" " PTR_FORMAT ": " PTR_FORMAT "%s%s",2849p2i(p), p2i((void*) obj), str, str2);2850}2851};28522853class PrintReachableObjectClosure : public ObjectClosure {2854private:2855G1CollectedHeap* _g1h;2856outputStream* _out;2857VerifyOption _vo;2858bool _all;2859HeapRegion* _hr;28602861public:2862PrintReachableObjectClosure(outputStream* out,2863VerifyOption vo,2864bool all,2865HeapRegion* hr) :2866_g1h(G1CollectedHeap::heap()),2867_out(out), _vo(vo), _all(all), _hr(hr) { }28682869void do_object(oop o) {2870bool over_tams = _g1h->allocated_since_marking(o, _hr, _vo);2871bool marked = _g1h->is_marked(o, _vo);2872bool print_it = _all || over_tams || marked;28732874if (print_it) {2875_out->print_cr(" " PTR_FORMAT "%s",2876p2i((void *)o), (over_tams) ? " >" : (marked) ? " M" : "");2877PrintReachableOopClosure oopCl(_out, _vo, _all);2878o->oop_iterate_no_header(&oopCl);2879}2880}2881};28822883class PrintReachableRegionClosure : public HeapRegionClosure {2884private:2885G1CollectedHeap* _g1h;2886outputStream* _out;2887VerifyOption _vo;2888bool _all;28892890public:2891bool doHeapRegion(HeapRegion* hr) {2892HeapWord* b = hr->bottom();2893HeapWord* e = hr->end();2894HeapWord* t = hr->top();2895HeapWord* p = _g1h->top_at_mark_start(hr, _vo);2896_out->print_cr("** [" PTR_FORMAT ", " PTR_FORMAT "] top: " PTR_FORMAT " "2897"TAMS: " PTR_FORMAT, p2i(b), p2i(e), p2i(t), p2i(p));2898_out->cr();28992900HeapWord* from = b;2901HeapWord* to = t;29022903if (to > from) {2904_out->print_cr("Objects in [" PTR_FORMAT ", " PTR_FORMAT "]", p2i(from), p2i(to));2905_out->cr();2906PrintReachableObjectClosure ocl(_out, _vo, _all, hr);2907hr->object_iterate_mem_careful(MemRegion(from, to), &ocl);2908_out->cr();2909}29102911return false;2912}29132914PrintReachableRegionClosure(outputStream* out,2915VerifyOption vo,2916bool all) :2917_g1h(G1CollectedHeap::heap()), _out(out), _vo(vo), _all(all) { }2918};29192920void ConcurrentMark::print_reachable(const char* str,2921VerifyOption vo,2922bool all) {2923gclog_or_tty->cr();2924gclog_or_tty->print_cr("== Doing heap dump... ");29252926if (G1PrintReachableBaseFile == NULL) {2927gclog_or_tty->print_cr(" #### error: no base file defined");2928return;2929}29302931if (strlen(G1PrintReachableBaseFile) + 1 + strlen(str) >2932(JVM_MAXPATHLEN - 1)) {2933gclog_or_tty->print_cr(" #### error: file name too long");2934return;2935}29362937char file_name[JVM_MAXPATHLEN];2938sprintf(file_name, "%s.%s", G1PrintReachableBaseFile, str);2939gclog_or_tty->print_cr(" dumping to file %s", file_name);29402941fileStream fout(file_name);2942if (!fout.is_open()) {2943gclog_or_tty->print_cr(" #### error: could not open file");2944return;2945}29462947outputStream* out = &fout;2948out->print_cr("-- USING %s", _g1h->top_at_mark_start_str(vo));2949out->cr();29502951out->print_cr("--- ITERATING OVER REGIONS");2952out->cr();2953PrintReachableRegionClosure rcl(out, vo, all);2954_g1h->heap_region_iterate(&rcl);2955out->cr();29562957gclog_or_tty->print_cr(" done");2958gclog_or_tty->flush();2959}29602961#endif // PRODUCT29622963void ConcurrentMark::clearRangePrevBitmap(MemRegion mr) {2964// Note we are overriding the read-only view of the prev map here, via2965// the cast.2966((CMBitMap*)_prevMarkBitMap)->clearRange(mr);2967}29682969void ConcurrentMark::clearRangeNextBitmap(MemRegion mr) {2970_nextMarkBitMap->clearRange(mr);2971}29722973HeapRegion*2974ConcurrentMark::claim_region(uint worker_id) {2975// "checkpoint" the finger2976HeapWord* finger = _finger;29772978// _heap_end will not change underneath our feet; it only changes at2979// yield points.2980while (finger < _heap_end) {2981assert(_g1h->is_in_g1_reserved(finger), "invariant");29822983// Note on how this code handles humongous regions. In the2984// normal case the finger will reach the start of a "starts2985// humongous" (SH) region. Its end will either be the end of the2986// last "continues humongous" (CH) region in the sequence, or the2987// standard end of the SH region (if the SH is the only region in2988// the sequence). That way claim_region() will skip over the CH2989// regions. However, there is a subtle race between a CM thread2990// executing this method and a mutator thread doing a humongous2991// object allocation. The two are not mutually exclusive as the CM2992// thread does not need to hold the Heap_lock when it gets2993// here. So there is a chance that claim_region() will come across2994// a free region that's in the progress of becoming a SH or a CH2995// region. In the former case, it will either2996// a) Miss the update to the region's end, in which case it will2997// visit every subsequent CH region, will find their bitmaps2998// empty, and do nothing, or2999// b) Will observe the update of the region's end (in which case3000// it will skip the subsequent CH regions).3001// If it comes across a region that suddenly becomes CH, the3002// scenario will be similar to b). So, the race between3003// claim_region() and a humongous object allocation might force us3004// to do a bit of unnecessary work (due to some unnecessary bitmap3005// iterations) but it should not introduce and correctness issues.3006HeapRegion* curr_region = _g1h->heap_region_containing_raw(finger);30073008// Above heap_region_containing_raw may return NULL as we always scan claim3009// until the end of the heap. In this case, just jump to the next region.3010HeapWord* end = curr_region != NULL ? curr_region->end() : finger + HeapRegion::GrainWords;30113012// Is the gap between reading the finger and doing the CAS too long?3013HeapWord* res = (HeapWord*) Atomic::cmpxchg_ptr(end, &_finger, finger);3014if (res == finger && curr_region != NULL) {3015// we succeeded3016HeapWord* bottom = curr_region->bottom();3017HeapWord* limit = curr_region->next_top_at_mark_start();30183019if (verbose_low()) {3020gclog_or_tty->print_cr("[%u] curr_region = " PTR_FORMAT " "3021"[" PTR_FORMAT ", " PTR_FORMAT "), "3022"limit = " PTR_FORMAT,3023worker_id, p2i(curr_region), p2i(bottom), p2i(end), p2i(limit));3024}30253026// notice that _finger == end cannot be guaranteed here since,3027// someone else might have moved the finger even further3028assert(_finger >= end, "the finger should have moved forward");30293030if (verbose_low()) {3031gclog_or_tty->print_cr("[%u] we were successful with region = "3032PTR_FORMAT, worker_id, p2i(curr_region));3033}30343035if (limit > bottom) {3036if (verbose_low()) {3037gclog_or_tty->print_cr("[%u] region " PTR_FORMAT " is not empty, "3038"returning it ", worker_id, p2i(curr_region));3039}3040return curr_region;3041} else {3042assert(limit == bottom,3043"the region limit should be at bottom");3044if (verbose_low()) {3045gclog_or_tty->print_cr("[%u] region " PTR_FORMAT " is empty, "3046"returning NULL", worker_id, p2i(curr_region));3047}3048// we return NULL and the caller should try calling3049// claim_region() again.3050return NULL;3051}3052} else {3053assert(_finger > finger, "the finger should have moved forward");3054if (verbose_low()) {3055if (curr_region == NULL) {3056gclog_or_tty->print_cr("[%u] found uncommitted region, moving finger, "3057"global finger = " PTR_FORMAT ", "3058"our finger = " PTR_FORMAT,3059worker_id, p2i(_finger), p2i(finger));3060} else {3061gclog_or_tty->print_cr("[%u] somebody else moved the finger, "3062"global finger = " PTR_FORMAT ", "3063"our finger = " PTR_FORMAT,3064worker_id, p2i(_finger), p2i(finger));3065}3066}30673068// read it again3069finger = _finger;3070}3071}30723073return NULL;3074}30753076#ifndef PRODUCT3077enum VerifyNoCSetOopsPhase {3078VerifyNoCSetOopsStack,3079VerifyNoCSetOopsQueues3080};30813082class VerifyNoCSetOopsClosure : public OopClosure, public ObjectClosure {3083private:3084G1CollectedHeap* _g1h;3085VerifyNoCSetOopsPhase _phase;3086int _info;30873088const char* phase_str() {3089switch (_phase) {3090case VerifyNoCSetOopsStack: return "Stack";3091case VerifyNoCSetOopsQueues: return "Queue";3092default: ShouldNotReachHere();3093}3094return NULL;3095}30963097void do_object_work(oop obj) {3098guarantee(G1CMObjArrayProcessor::is_array_slice(obj) || obj->is_oop(),3099err_msg("Non-oop " PTR_FORMAT ", phase: %s, info: %d",3100p2i((void*) obj), phase_str(), _info));3101guarantee(G1CMObjArrayProcessor::is_array_slice(obj) || !_g1h->obj_in_cs(obj),3102err_msg("obj: " PTR_FORMAT " in CSet, phase: %s, info: %d",3103p2i((void*) obj), phase_str(), _info));3104}31053106public:3107VerifyNoCSetOopsClosure() : _g1h(G1CollectedHeap::heap()) { }31083109void set_phase(VerifyNoCSetOopsPhase phase, int info = -1) {3110_phase = phase;3111_info = info;3112}31133114virtual void do_oop(oop* p) {3115oop obj = oopDesc::load_decode_heap_oop(p);3116do_object_work(obj);3117}31183119virtual void do_oop(narrowOop* p) {3120// We should not come across narrow oops while scanning marking3121// stacks3122ShouldNotReachHere();3123}31243125virtual void do_object(oop obj) {3126do_object_work(obj);3127}3128};31293130void ConcurrentMark::verify_no_cset_oops() {3131assert(SafepointSynchronize::is_at_safepoint(), "should be at a safepoint");3132if (!G1CollectedHeap::heap()->mark_in_progress()) {3133return;3134}31353136VerifyNoCSetOopsClosure cl;31373138// Verify entries on the global mark stack3139cl.set_phase(VerifyNoCSetOopsStack);3140_markStack.oops_do(&cl);31413142// Verify entries on the task queues3143for (uint i = 0; i < _max_worker_id; i += 1) {3144cl.set_phase(VerifyNoCSetOopsQueues, i);3145CMTaskQueue* queue = _task_queues->queue(i);3146queue->oops_do(&cl);3147}31483149// Verify the global finger3150HeapWord* global_finger = finger();3151if (global_finger != NULL && global_finger < _heap_end) {3152// The global finger always points to a heap region boundary. We3153// use heap_region_containing_raw() to get the containing region3154// given that the global finger could be pointing to a free region3155// which subsequently becomes continues humongous. If that3156// happens, heap_region_containing() will return the bottom of the3157// corresponding starts humongous region and the check below will3158// not hold any more.3159// Since we always iterate over all regions, we might get a NULL HeapRegion3160// here.3161HeapRegion* global_hr = _g1h->heap_region_containing_raw(global_finger);3162guarantee(global_hr == NULL || global_finger == global_hr->bottom(),3163err_msg("global finger: " PTR_FORMAT " region: " HR_FORMAT,3164p2i(global_finger), HR_FORMAT_PARAMS(global_hr)));3165}31663167// Verify the task fingers3168assert(parallel_marking_threads() <= _max_worker_id, "sanity");3169for (int i = 0; i < (int) parallel_marking_threads(); i += 1) {3170CMTask* task = _tasks[i];3171HeapWord* task_finger = task->finger();3172if (task_finger != NULL && task_finger < _heap_end) {3173// See above note on the global finger verification.3174HeapRegion* task_hr = _g1h->heap_region_containing_raw(task_finger);3175guarantee(task_hr == NULL || task_finger == task_hr->bottom() ||3176!task_hr->in_collection_set(),3177err_msg("task finger: " PTR_FORMAT " region: " HR_FORMAT,3178p2i(task_finger), HR_FORMAT_PARAMS(task_hr)));3179}3180}3181}3182#endif // PRODUCT31833184// Aggregate the counting data that was constructed concurrently3185// with marking.3186class AggregateCountDataHRClosure: public HeapRegionClosure {3187G1CollectedHeap* _g1h;3188ConcurrentMark* _cm;3189CardTableModRefBS* _ct_bs;3190BitMap* _cm_card_bm;3191uint _max_worker_id;31923193public:3194AggregateCountDataHRClosure(G1CollectedHeap* g1h,3195BitMap* cm_card_bm,3196uint max_worker_id) :3197_g1h(g1h), _cm(g1h->concurrent_mark()),3198_ct_bs((CardTableModRefBS*) (g1h->barrier_set())),3199_cm_card_bm(cm_card_bm), _max_worker_id(max_worker_id) { }32003201bool doHeapRegion(HeapRegion* hr) {3202if (hr->continuesHumongous()) {3203// We will ignore these here and process them when their3204// associated "starts humongous" region is processed.3205// Note that we cannot rely on their associated3206// "starts humongous" region to have their bit set to 13207// since, due to the region chunking in the parallel region3208// iteration, a "continues humongous" region might be visited3209// before its associated "starts humongous".3210return false;3211}32123213HeapWord* start = hr->bottom();3214HeapWord* limit = hr->next_top_at_mark_start();3215HeapWord* end = hr->end();32163217assert(start <= limit && limit <= hr->top() && hr->top() <= hr->end(),3218err_msg("Preconditions not met - "3219"start: " PTR_FORMAT ", limit: " PTR_FORMAT ", "3220"top: " PTR_FORMAT ", end: " PTR_FORMAT,3221p2i(start), p2i(limit), p2i(hr->top()), p2i(hr->end())));32223223assert(hr->next_marked_bytes() == 0, "Precondition");32243225if (start == limit) {3226// NTAMS of this region has not been set so nothing to do.3227return false;3228}32293230// 'start' should be in the heap.3231assert(_g1h->is_in_g1_reserved(start) && _ct_bs->is_card_aligned(start), "sanity");3232// 'end' *may* be just beyone the end of the heap (if hr is the last region)3233assert(!_g1h->is_in_g1_reserved(end) || _ct_bs->is_card_aligned(end), "sanity");32343235BitMap::idx_t start_idx = _cm->card_bitmap_index_for(start);3236BitMap::idx_t limit_idx = _cm->card_bitmap_index_for(limit);3237BitMap::idx_t end_idx = _cm->card_bitmap_index_for(end);32383239// If ntams is not card aligned then we bump card bitmap index3240// for limit so that we get the all the cards spanned by3241// the object ending at ntams.3242// Note: if this is the last region in the heap then ntams3243// could be actually just beyond the end of the the heap;3244// limit_idx will then correspond to a (non-existent) card3245// that is also outside the heap.3246if (_g1h->is_in_g1_reserved(limit) && !_ct_bs->is_card_aligned(limit)) {3247limit_idx += 1;3248}32493250assert(limit_idx <= end_idx, "or else use atomics");32513252// Aggregate the "stripe" in the count data associated with hr.3253uint hrm_index = hr->hrm_index();3254size_t marked_bytes = 0;32553256for (uint i = 0; i < _max_worker_id; i += 1) {3257size_t* marked_bytes_array = _cm->count_marked_bytes_array_for(i);3258BitMap* task_card_bm = _cm->count_card_bitmap_for(i);32593260// Fetch the marked_bytes in this region for task i and3261// add it to the running total for this region.3262marked_bytes += marked_bytes_array[hrm_index];32633264// Now union the bitmaps[0,max_worker_id)[start_idx..limit_idx)3265// into the global card bitmap.3266BitMap::idx_t scan_idx = task_card_bm->get_next_one_offset(start_idx, limit_idx);32673268while (scan_idx < limit_idx) {3269assert(task_card_bm->at(scan_idx) == true, "should be");3270_cm_card_bm->set_bit(scan_idx);3271assert(_cm_card_bm->at(scan_idx) == true, "should be");32723273// BitMap::get_next_one_offset() can handle the case when3274// its left_offset parameter is greater than its right_offset3275// parameter. It does, however, have an early exit if3276// left_offset == right_offset. So let's limit the value3277// passed in for left offset here.3278BitMap::idx_t next_idx = MIN2(scan_idx + 1, limit_idx);3279scan_idx = task_card_bm->get_next_one_offset(next_idx, limit_idx);3280}3281}32823283// Update the marked bytes for this region.3284hr->add_to_marked_bytes(marked_bytes);32853286// Next heap region3287return false;3288}3289};32903291class G1AggregateCountDataTask: public AbstractGangTask {3292protected:3293G1CollectedHeap* _g1h;3294ConcurrentMark* _cm;3295BitMap* _cm_card_bm;3296uint _max_worker_id;3297int _active_workers;32983299public:3300G1AggregateCountDataTask(G1CollectedHeap* g1h,3301ConcurrentMark* cm,3302BitMap* cm_card_bm,3303uint max_worker_id,3304int n_workers) :3305AbstractGangTask("Count Aggregation"),3306_g1h(g1h), _cm(cm), _cm_card_bm(cm_card_bm),3307_max_worker_id(max_worker_id),3308_active_workers(n_workers) { }33093310void work(uint worker_id) {3311AggregateCountDataHRClosure cl(_g1h, _cm_card_bm, _max_worker_id);33123313if (G1CollectedHeap::use_parallel_gc_threads()) {3314_g1h->heap_region_par_iterate_chunked(&cl, worker_id,3315_active_workers,3316HeapRegion::AggregateCountClaimValue);3317} else {3318_g1h->heap_region_iterate(&cl);3319}3320}3321};332233233324void ConcurrentMark::aggregate_count_data() {3325int n_workers = (G1CollectedHeap::use_parallel_gc_threads() ?3326_g1h->workers()->active_workers() :33271);33283329G1AggregateCountDataTask g1_par_agg_task(_g1h, this, &_card_bm,3330_max_worker_id, n_workers);33313332if (G1CollectedHeap::use_parallel_gc_threads()) {3333assert(_g1h->check_heap_region_claim_values(HeapRegion::InitialClaimValue),3334"sanity check");3335_g1h->set_par_threads(n_workers);3336_g1h->workers()->run_task(&g1_par_agg_task);3337_g1h->set_par_threads(0);33383339assert(_g1h->check_heap_region_claim_values(HeapRegion::AggregateCountClaimValue),3340"sanity check");3341_g1h->reset_heap_region_claim_values();3342} else {3343g1_par_agg_task.work(0);3344}3345}33463347// Clear the per-worker arrays used to store the per-region counting data3348void ConcurrentMark::clear_all_count_data() {3349// Clear the global card bitmap - it will be filled during3350// liveness count aggregation (during remark) and the3351// final counting task.3352_card_bm.clear();33533354// Clear the global region bitmap - it will be filled as part3355// of the final counting task.3356_region_bm.clear();33573358uint max_regions = _g1h->max_regions();3359assert(_max_worker_id > 0, "uninitialized");33603361for (uint i = 0; i < _max_worker_id; i += 1) {3362BitMap* task_card_bm = count_card_bitmap_for(i);3363size_t* marked_bytes_array = count_marked_bytes_array_for(i);33643365assert(task_card_bm->size() == _card_bm.size(), "size mismatch");3366assert(marked_bytes_array != NULL, "uninitialized");33673368memset(marked_bytes_array, 0, (size_t) max_regions * sizeof(size_t));3369task_card_bm->clear();3370}3371}33723373void ConcurrentMark::print_stats() {3374if (verbose_stats()) {3375gclog_or_tty->print_cr("---------------------------------------------------------------------");3376for (size_t i = 0; i < _active_tasks; ++i) {3377_tasks[i]->print_stats();3378gclog_or_tty->print_cr("---------------------------------------------------------------------");3379}3380}3381}33823383// abandon current marking iteration due to a Full GC3384void ConcurrentMark::abort() {3385// Clear all marks in the next bitmap for the next marking cycle. This will allow us to skip the next3386// concurrent bitmap clearing.3387_nextMarkBitMap->clearAll();33883389// Note we cannot clear the previous marking bitmap here3390// since VerifyDuringGC verifies the objects marked during3391// a full GC against the previous bitmap.33923393// Clear the liveness counting data3394clear_all_count_data();3395// Empty mark stack3396reset_marking_state();3397for (uint i = 0; i < _max_worker_id; ++i) {3398_tasks[i]->clear_region_fields();3399}3400_first_overflow_barrier_sync.abort();3401_second_overflow_barrier_sync.abort();3402const GCId& gc_id = _g1h->gc_tracer_cm()->gc_id();3403if (!gc_id.is_undefined()) {3404// We can do multiple full GCs before ConcurrentMarkThread::run() gets a chance3405// to detect that it was aborted. Only keep track of the first GC id that we aborted.3406_aborted_gc_id = gc_id;3407}3408_has_aborted = true;34093410SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();3411satb_mq_set.abandon_partial_marking();3412// This can be called either during or outside marking, we'll read3413// the expected_active value from the SATB queue set.3414satb_mq_set.set_active_all_threads(3415false, /* new active value */3416satb_mq_set.is_active() /* expected_active */);34173418_g1h->trace_heap_after_concurrent_cycle();3419_g1h->register_concurrent_cycle_end();3420}34213422const GCId& ConcurrentMark::concurrent_gc_id() {3423if (has_aborted()) {3424return _aborted_gc_id;3425}3426return _g1h->gc_tracer_cm()->gc_id();3427}34283429static void print_ms_time_info(const char* prefix, const char* name,3430NumberSeq& ns) {3431gclog_or_tty->print_cr("%s%5d %12s: total time = %8.2f s (avg = %8.2f ms).",3432prefix, ns.num(), name, ns.sum()/1000.0, ns.avg());3433if (ns.num() > 0) {3434gclog_or_tty->print_cr("%s [std. dev = %8.2f ms, max = %8.2f ms]",3435prefix, ns.sd(), ns.maximum());3436}3437}34383439void ConcurrentMark::print_summary_info() {3440gclog_or_tty->print_cr(" Concurrent marking:");3441print_ms_time_info(" ", "init marks", _init_times);3442print_ms_time_info(" ", "remarks", _remark_times);3443{3444print_ms_time_info(" ", "final marks", _remark_mark_times);3445print_ms_time_info(" ", "weak refs", _remark_weak_ref_times);34463447}3448print_ms_time_info(" ", "cleanups", _cleanup_times);3449gclog_or_tty->print_cr(" Final counting total time = %8.2f s (avg = %8.2f ms).",3450_total_counting_time,3451(_cleanup_times.num() > 0 ? _total_counting_time * 1000.0 /3452(double)_cleanup_times.num()3453: 0.0));3454if (G1ScrubRemSets) {3455gclog_or_tty->print_cr(" RS scrub total time = %8.2f s (avg = %8.2f ms).",3456_total_rs_scrub_time,3457(_cleanup_times.num() > 0 ? _total_rs_scrub_time * 1000.0 /3458(double)_cleanup_times.num()3459: 0.0));3460}3461gclog_or_tty->print_cr(" Total stop_world time = %8.2f s.",3462(_init_times.sum() + _remark_times.sum() +3463_cleanup_times.sum())/1000.0);3464gclog_or_tty->print_cr(" Total concurrent time = %8.2f s "3465"(%8.2f s marking).",3466cmThread()->vtime_accum(),3467cmThread()->vtime_mark_accum());3468}34693470void ConcurrentMark::print_worker_threads_on(outputStream* st) const {3471if (use_parallel_marking_threads()) {3472_parallel_workers->print_worker_threads_on(st);3473}3474}34753476void ConcurrentMark::print_on_error(outputStream* st) const {3477st->print_cr("Marking Bits (Prev, Next): (CMBitMap*) " PTR_FORMAT ", (CMBitMap*) " PTR_FORMAT,3478p2i(_prevMarkBitMap), p2i(_nextMarkBitMap));3479_prevMarkBitMap->print_on_error(st, " Prev Bits: ");3480_nextMarkBitMap->print_on_error(st, " Next Bits: ");3481}34823483// We take a break if someone is trying to stop the world.3484bool ConcurrentMark::do_yield_check(uint worker_id) {3485if (SuspendibleThreadSet::should_yield()) {3486if (worker_id == 0) {3487_g1h->g1_policy()->record_concurrent_pause();3488}3489SuspendibleThreadSet::yield();3490return true;3491} else {3492return false;3493}3494}34953496#ifndef PRODUCT3497// for debugging purposes3498void ConcurrentMark::print_finger() {3499gclog_or_tty->print_cr("heap [" PTR_FORMAT ", " PTR_FORMAT "), global finger = " PTR_FORMAT,3500p2i(_heap_start), p2i(_heap_end), p2i(_finger));3501for (uint i = 0; i < _max_worker_id; ++i) {3502gclog_or_tty->print(" %u: " PTR_FORMAT, i, p2i(_tasks[i]->finger()));3503}3504gclog_or_tty->cr();3505}3506#endif35073508template<bool scan>3509inline void CMTask::process_grey_object(oop obj) {3510assert(scan || obj->is_typeArray(), "Skipping scan of grey non-typeArray");35113512if (_cm->verbose_high()) {3513gclog_or_tty->print_cr("[%u] processing grey object " PTR_FORMAT,3514_worker_id, p2i((void*) obj));3515}35163517assert(G1CMObjArrayProcessor::is_array_slice(obj) || _nextMarkBitMap->isMarked((HeapWord*) obj),3518"Any stolen object should be a slice or marked");35193520if (scan) {3521if (G1CMObjArrayProcessor::is_array_slice(obj)) {3522_words_scanned += _objArray_processor.process_slice(obj);3523} else if (G1CMObjArrayProcessor::should_be_sliced(obj)) {3524_words_scanned += _objArray_processor.process_obj(obj);3525} else {3526size_t obj_size = obj->size();3527_words_scanned += obj_size;3528obj->oop_iterate(_cm_oop_closure);;3529}3530}3531statsOnly( ++_objs_scanned );3532check_limits();3533}35343535template void CMTask::process_grey_object<true>(oop);3536template void CMTask::process_grey_object<false>(oop);35373538// Closure for iteration over bitmaps3539class CMBitMapClosure : public BitMapClosure {3540private:3541// the bitmap that is being iterated over3542CMBitMap* _nextMarkBitMap;3543ConcurrentMark* _cm;3544CMTask* _task;35453546public:3547CMBitMapClosure(CMTask *task, ConcurrentMark* cm, CMBitMap* nextMarkBitMap) :3548_task(task), _cm(cm), _nextMarkBitMap(nextMarkBitMap) { }35493550bool do_bit(size_t offset) {3551HeapWord* addr = _nextMarkBitMap->offsetToHeapWord(offset);3552assert(_nextMarkBitMap->isMarked(addr), "invariant");3553assert( addr < _cm->finger(), "invariant");35543555statsOnly( _task->increase_objs_found_on_bitmap() );3556assert(addr >= _task->finger(), "invariant");35573558// We move that task's local finger along.3559_task->move_finger_to(addr);35603561_task->scan_object(oop(addr));3562// we only partially drain the local queue and global stack3563_task->drain_local_queue(true);3564_task->drain_global_stack(true);35653566// if the has_aborted flag has been raised, we need to bail out of3567// the iteration3568return !_task->has_aborted();3569}3570};35713572G1CMOopClosure::G1CMOopClosure(G1CollectedHeap* g1h,3573ConcurrentMark* cm,3574CMTask* task)3575: _g1h(g1h), _cm(cm), _task(task) {3576assert(_ref_processor == NULL, "should be initialized to NULL");35773578if (G1UseConcMarkReferenceProcessing) {3579_ref_processor = g1h->ref_processor_cm();3580assert(_ref_processor != NULL, "should not be NULL");3581}3582}35833584void CMTask::setup_for_region(HeapRegion* hr) {3585assert(hr != NULL,3586"claim_region() should have filtered out NULL regions");3587assert(!hr->continuesHumongous(),3588"claim_region() should have filtered out continues humongous regions");35893590if (_cm->verbose_low()) {3591gclog_or_tty->print_cr("[%u] setting up for region " PTR_FORMAT,3592_worker_id, p2i(hr));3593}35943595_curr_region = hr;3596_finger = hr->bottom();3597update_region_limit();3598}35993600void CMTask::update_region_limit() {3601HeapRegion* hr = _curr_region;3602HeapWord* bottom = hr->bottom();3603HeapWord* limit = hr->next_top_at_mark_start();36043605if (limit == bottom) {3606if (_cm->verbose_low()) {3607gclog_or_tty->print_cr("[%u] found an empty region "3608"[" PTR_FORMAT ", " PTR_FORMAT ")",3609_worker_id, p2i(bottom), p2i(limit));3610}3611// The region was collected underneath our feet.3612// We set the finger to bottom to ensure that the bitmap3613// iteration that will follow this will not do anything.3614// (this is not a condition that holds when we set the region up,3615// as the region is not supposed to be empty in the first place)3616_finger = bottom;3617} else if (limit >= _region_limit) {3618assert(limit >= _finger, "peace of mind");3619} else {3620assert(limit < _region_limit, "only way to get here");3621// This can happen under some pretty unusual circumstances. An3622// evacuation pause empties the region underneath our feet (NTAMS3623// at bottom). We then do some allocation in the region (NTAMS3624// stays at bottom), followed by the region being used as a GC3625// alloc region (NTAMS will move to top() and the objects3626// originally below it will be grayed). All objects now marked in3627// the region are explicitly grayed, if below the global finger,3628// and we do not need in fact to scan anything else. So, we simply3629// set _finger to be limit to ensure that the bitmap iteration3630// doesn't do anything.3631_finger = limit;3632}36333634_region_limit = limit;3635}36363637void CMTask::giveup_current_region() {3638assert(_curr_region != NULL, "invariant");3639if (_cm->verbose_low()) {3640gclog_or_tty->print_cr("[%u] giving up region " PTR_FORMAT,3641_worker_id, p2i(_curr_region));3642}3643clear_region_fields();3644}36453646void CMTask::clear_region_fields() {3647// Values for these three fields that indicate that we're not3648// holding on to a region.3649_curr_region = NULL;3650_finger = NULL;3651_region_limit = NULL;3652}36533654void CMTask::set_cm_oop_closure(G1CMOopClosure* cm_oop_closure) {3655if (cm_oop_closure == NULL) {3656assert(_cm_oop_closure != NULL, "invariant");3657} else {3658assert(_cm_oop_closure == NULL, "invariant");3659}3660_cm_oop_closure = cm_oop_closure;3661}36623663void CMTask::reset(CMBitMap* nextMarkBitMap) {3664guarantee(nextMarkBitMap != NULL, "invariant");36653666if (_cm->verbose_low()) {3667gclog_or_tty->print_cr("[%u] resetting", _worker_id);3668}36693670_nextMarkBitMap = nextMarkBitMap;3671clear_region_fields();36723673_calls = 0;3674_elapsed_time_ms = 0.0;3675_termination_time_ms = 0.0;3676_termination_start_time_ms = 0.0;36773678#if _MARKING_STATS_3679_local_pushes = 0;3680_local_pops = 0;3681_local_max_size = 0;3682_objs_scanned = 0;3683_global_pushes = 0;3684_global_pops = 0;3685_global_max_size = 0;3686_global_transfers_to = 0;3687_global_transfers_from = 0;3688_regions_claimed = 0;3689_objs_found_on_bitmap = 0;3690_satb_buffers_processed = 0;3691_steal_attempts = 0;3692_steals = 0;3693_aborted = 0;3694_aborted_overflow = 0;3695_aborted_cm_aborted = 0;3696_aborted_yield = 0;3697_aborted_timed_out = 0;3698_aborted_satb = 0;3699_aborted_termination = 0;3700#endif // _MARKING_STATS_3701}37023703bool CMTask::should_exit_termination() {3704regular_clock_call();3705// This is called when we are in the termination protocol. We should3706// quit if, for some reason, this task wants to abort or the global3707// stack is not empty (this means that we can get work from it).3708return !_cm->mark_stack_empty() || has_aborted();3709}37103711void CMTask::reached_limit() {3712assert(_words_scanned >= _words_scanned_limit ||3713_refs_reached >= _refs_reached_limit ,3714"shouldn't have been called otherwise");3715regular_clock_call();3716}37173718void CMTask::regular_clock_call() {3719if (has_aborted()) return;37203721// First, we need to recalculate the words scanned and refs reached3722// limits for the next clock call.3723recalculate_limits();37243725// During the regular clock call we do the following37263727// (1) If an overflow has been flagged, then we abort.3728if (_cm->has_overflown()) {3729set_has_aborted();3730return;3731}37323733// If we are not concurrent (i.e. we're doing remark) we don't need3734// to check anything else. The other steps are only needed during3735// the concurrent marking phase.3736if (!concurrent()) return;37373738// (2) If marking has been aborted for Full GC, then we also abort.3739if (_cm->has_aborted()) {3740set_has_aborted();3741statsOnly( ++_aborted_cm_aborted );3742return;3743}37443745double curr_time_ms = os::elapsedVTime() * 1000.0;37463747// (3) If marking stats are enabled, then we update the step history.3748#if _MARKING_STATS_3749if (_words_scanned >= _words_scanned_limit) {3750++_clock_due_to_scanning;3751}3752if (_refs_reached >= _refs_reached_limit) {3753++_clock_due_to_marking;3754}37553756double last_interval_ms = curr_time_ms - _interval_start_time_ms;3757_interval_start_time_ms = curr_time_ms;3758_all_clock_intervals_ms.add(last_interval_ms);37593760if (_cm->verbose_medium()) {3761gclog_or_tty->print_cr("[%u] regular clock, interval = %1.2lfms, "3762"scanned = " SIZE_FORMAT "%s, refs reached = " SIZE_FORMAT "%s",3763_worker_id, last_interval_ms,3764_words_scanned,3765(_words_scanned >= _words_scanned_limit) ? " (*)" : "",3766_refs_reached,3767(_refs_reached >= _refs_reached_limit) ? " (*)" : "");3768}3769#endif // _MARKING_STATS_37703771// (4) We check whether we should yield. If we have to, then we abort.3772if (SuspendibleThreadSet::should_yield()) {3773// We should yield. To do this we abort the task. The caller is3774// responsible for yielding.3775set_has_aborted();3776statsOnly( ++_aborted_yield );3777return;3778}37793780// (5) We check whether we've reached our time quota. If we have,3781// then we abort.3782double elapsed_time_ms = curr_time_ms - _start_time_ms;3783if (elapsed_time_ms > _time_target_ms) {3784set_has_aborted();3785_has_timed_out = true;3786statsOnly( ++_aborted_timed_out );3787return;3788}37893790// (6) Finally, we check whether there are enough completed STAB3791// buffers available for processing. If there are, we abort.3792SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();3793if (!_draining_satb_buffers && satb_mq_set.process_completed_buffers()) {3794if (_cm->verbose_low()) {3795gclog_or_tty->print_cr("[%u] aborting to deal with pending SATB buffers",3796_worker_id);3797}3798// we do need to process SATB buffers, we'll abort and restart3799// the marking task to do so3800set_has_aborted();3801statsOnly( ++_aborted_satb );3802return;3803}3804}38053806void CMTask::recalculate_limits() {3807_real_words_scanned_limit = _words_scanned + words_scanned_period;3808_words_scanned_limit = _real_words_scanned_limit;38093810_real_refs_reached_limit = _refs_reached + refs_reached_period;3811_refs_reached_limit = _real_refs_reached_limit;3812}38133814void CMTask::decrease_limits() {3815// This is called when we believe that we're going to do an infrequent3816// operation which will increase the per byte scanned cost (i.e. move3817// entries to/from the global stack). It basically tries to decrease the3818// scanning limit so that the clock is called earlier.38193820if (_cm->verbose_medium()) {3821gclog_or_tty->print_cr("[%u] decreasing limits", _worker_id);3822}38233824_words_scanned_limit = _real_words_scanned_limit -38253 * words_scanned_period / 4;3826_refs_reached_limit = _real_refs_reached_limit -38273 * refs_reached_period / 4;3828}38293830void CMTask::move_entries_to_global_stack() {3831// local array where we'll store the entries that will be popped3832// from the local queue3833oop buffer[global_stack_transfer_size];38343835int n = 0;3836oop obj;3837while (n < global_stack_transfer_size && _task_queue->pop_local(obj)) {3838buffer[n] = obj;3839++n;3840}38413842if (n > 0) {3843// we popped at least one entry from the local queue38443845statsOnly( ++_global_transfers_to; _local_pops += n );38463847if (!_cm->mark_stack_push(buffer, n)) {3848if (_cm->verbose_low()) {3849gclog_or_tty->print_cr("[%u] aborting due to global stack overflow",3850_worker_id);3851}3852set_has_aborted();3853} else {3854// the transfer was successful38553856if (_cm->verbose_medium()) {3857gclog_or_tty->print_cr("[%u] pushed %d entries to the global stack",3858_worker_id, n);3859}3860statsOnly( int tmp_size = _cm->mark_stack_size();3861if (tmp_size > _global_max_size) {3862_global_max_size = tmp_size;3863}3864_global_pushes += n );3865}3866}38673868// this operation was quite expensive, so decrease the limits3869decrease_limits();3870}38713872void CMTask::get_entries_from_global_stack() {3873// local array where we'll store the entries that will be popped3874// from the global stack.3875oop buffer[global_stack_transfer_size];3876int n;3877_cm->mark_stack_pop(buffer, global_stack_transfer_size, &n);3878assert(n <= global_stack_transfer_size,3879"we should not pop more than the given limit");3880if (n > 0) {3881// yes, we did actually pop at least one entry38823883statsOnly( ++_global_transfers_from; _global_pops += n );3884if (_cm->verbose_medium()) {3885gclog_or_tty->print_cr("[%u] popped %d entries from the global stack",3886_worker_id, n);3887}3888for (int i = 0; i < n; ++i) {3889assert(G1CMObjArrayProcessor::is_array_slice(buffer[i]) || buffer[i]->is_oop(),3890err_msg("Element " PTR_FORMAT " must be an array slice or oop", p2i(buffer[i])));3891bool success = _task_queue->push(buffer[i]);3892// We only call this when the local queue is empty or under a3893// given target limit. So, we do not expect this push to fail.3894assert(success, "invariant");3895}38963897statsOnly( int tmp_size = _task_queue->size();3898if (tmp_size > _local_max_size) {3899_local_max_size = tmp_size;3900}3901_local_pushes += n );3902}39033904// this operation was quite expensive, so decrease the limits3905decrease_limits();3906}39073908void CMTask::drain_local_queue(bool partially) {3909if (has_aborted()) {3910return;3911}39123913// Decide what the target size is, depending whether we're going to3914// drain it partially (so that other tasks can steal if they run out3915// of things to do) or totally (at the very end).3916size_t target_size;3917if (partially) {3918target_size = MIN2((size_t)_task_queue->max_elems()/3, GCDrainStackTargetSize);3919} else {3920target_size = 0;3921}39223923if (_task_queue->size() > target_size) {3924if (_cm->verbose_high()) {3925gclog_or_tty->print_cr("[%u] draining local queue, target size = " SIZE_FORMAT,3926_worker_id, target_size);3927}39283929oop obj;3930bool ret = _task_queue->pop_local(obj);3931while (ret) {3932statsOnly( ++_local_pops );39333934if (_cm->verbose_high()) {3935gclog_or_tty->print_cr("[%u] popped " PTR_FORMAT, _worker_id,3936p2i((void*) obj));3937}39383939scan_object(obj);39403941if (_task_queue->size() <= target_size || has_aborted()) {3942ret = false;3943} else {3944ret = _task_queue->pop_local(obj);3945}3946}39473948if (_cm->verbose_high()) {3949gclog_or_tty->print_cr("[%u] drained local queue, size = %d",3950_worker_id, _task_queue->size());3951}3952}3953}39543955void CMTask::drain_global_stack(bool partially) {3956if (has_aborted()) return;39573958// We have a policy to drain the local queue before we attempt to3959// drain the global stack.3960assert(partially || _task_queue->size() == 0, "invariant");39613962// Decide what the target size is, depending whether we're going to3963// drain it partially (so that other tasks can steal if they run out3964// of things to do) or totally (at the very end). Notice that,3965// because we move entries from the global stack in chunks or3966// because another task might be doing the same, we might in fact3967// drop below the target. But, this is not a problem.3968size_t target_size;3969if (partially) {3970target_size = _cm->partial_mark_stack_size_target();3971} else {3972target_size = 0;3973}39743975if (_cm->mark_stack_size() > target_size) {3976if (_cm->verbose_low()) {3977gclog_or_tty->print_cr("[%u] draining global_stack, target size " SIZE_FORMAT,3978_worker_id, target_size);3979}39803981while (!has_aborted() && _cm->mark_stack_size() > target_size) {3982get_entries_from_global_stack();3983drain_local_queue(partially);3984}39853986if (_cm->verbose_low()) {3987gclog_or_tty->print_cr("[%u] drained global stack, size = " SIZE_FORMAT,3988_worker_id, _cm->mark_stack_size());3989}3990}3991}39923993// SATB Queue has several assumptions on whether to call the par or3994// non-par versions of the methods. this is why some of the code is3995// replicated. We should really get rid of the single-threaded version3996// of the code to simplify things.3997void CMTask::drain_satb_buffers() {3998if (has_aborted()) return;39994000// We set this so that the regular clock knows that we're in the4001// middle of draining buffers and doesn't set the abort flag when it4002// notices that SATB buffers are available for draining. It'd be4003// very counter productive if it did that. :-)4004_draining_satb_buffers = true;40054006CMSATBBufferClosure satb_cl(this, _g1h);4007SATBMarkQueueSet& satb_mq_set = JavaThread::satb_mark_queue_set();40084009// This keeps claiming and applying the closure to completed buffers4010// until we run out of buffers or we need to abort.4011while (!has_aborted() &&4012satb_mq_set.apply_closure_to_completed_buffer(&satb_cl)) {4013if (_cm->verbose_medium()) {4014gclog_or_tty->print_cr("[%u] processed an SATB buffer", _worker_id);4015}4016statsOnly( ++_satb_buffers_processed );4017regular_clock_call();4018}40194020_draining_satb_buffers = false;40214022assert(has_aborted() ||4023concurrent() ||4024satb_mq_set.completed_buffers_num() == 0, "invariant");40254026// again, this was a potentially expensive operation, decrease the4027// limits to get the regular clock call early4028decrease_limits();4029}40304031void CMTask::print_stats() {4032gclog_or_tty->print_cr("Marking Stats, task = %u, calls = %d",4033_worker_id, _calls);4034gclog_or_tty->print_cr(" Elapsed time = %1.2lfms, Termination time = %1.2lfms",4035_elapsed_time_ms, _termination_time_ms);4036gclog_or_tty->print_cr(" Step Times (cum): num = %d, avg = %1.2lfms, sd = %1.2lfms",4037_step_times_ms.num(), _step_times_ms.avg(),4038_step_times_ms.sd());4039gclog_or_tty->print_cr(" max = %1.2lfms, total = %1.2lfms",4040_step_times_ms.maximum(), _step_times_ms.sum());40414042#if _MARKING_STATS_4043gclog_or_tty->print_cr(" Clock Intervals (cum): num = %d, avg = %1.2lfms, sd = %1.2lfms",4044_all_clock_intervals_ms.num(), _all_clock_intervals_ms.avg(),4045_all_clock_intervals_ms.sd());4046gclog_or_tty->print_cr(" max = %1.2lfms, total = %1.2lfms",4047_all_clock_intervals_ms.maximum(),4048_all_clock_intervals_ms.sum());4049gclog_or_tty->print_cr(" Clock Causes (cum): scanning = %d, marking = %d",4050_clock_due_to_scanning, _clock_due_to_marking);4051gclog_or_tty->print_cr(" Objects: scanned = %d, found on the bitmap = %d",4052_objs_scanned, _objs_found_on_bitmap);4053gclog_or_tty->print_cr(" Local Queue: pushes = %d, pops = %d, max size = %d",4054_local_pushes, _local_pops, _local_max_size);4055gclog_or_tty->print_cr(" Global Stack: pushes = %d, pops = %d, max size = %d",4056_global_pushes, _global_pops, _global_max_size);4057gclog_or_tty->print_cr(" transfers to = %d, transfers from = %d",4058_global_transfers_to,_global_transfers_from);4059gclog_or_tty->print_cr(" Regions: claimed = %d", _regions_claimed);4060gclog_or_tty->print_cr(" SATB buffers: processed = %d", _satb_buffers_processed);4061gclog_or_tty->print_cr(" Steals: attempts = %d, successes = %d",4062_steal_attempts, _steals);4063gclog_or_tty->print_cr(" Aborted: %d, due to", _aborted);4064gclog_or_tty->print_cr(" overflow: %d, global abort: %d, yield: %d",4065_aborted_overflow, _aborted_cm_aborted, _aborted_yield);4066gclog_or_tty->print_cr(" time out: %d, SATB: %d, termination: %d",4067_aborted_timed_out, _aborted_satb, _aborted_termination);4068#endif // _MARKING_STATS_4069}40704071/*****************************************************************************40724073The do_marking_step(time_target_ms, ...) method is the building4074block of the parallel marking framework. It can be called in parallel4075with other invocations of do_marking_step() on different tasks4076(but only one per task, obviously) and concurrently with the4077mutator threads, or during remark, hence it eliminates the need4078for two versions of the code. When called during remark, it will4079pick up from where the task left off during the concurrent marking4080phase. Interestingly, tasks are also claimable during evacuation4081pauses too, since do_marking_step() ensures that it aborts before4082it needs to yield.40834084The data structures that it uses to do marking work are the4085following:40864087(1) Marking Bitmap. If there are gray objects that appear only4088on the bitmap (this happens either when dealing with an overflow4089or when the initial marking phase has simply marked the roots4090and didn't push them on the stack), then tasks claim heap4091regions whose bitmap they then scan to find gray objects. A4092global finger indicates where the end of the last claimed region4093is. A local finger indicates how far into the region a task has4094scanned. The two fingers are used to determine how to gray an4095object (i.e. whether simply marking it is OK, as it will be4096visited by a task in the future, or whether it needs to be also4097pushed on a stack).40984099(2) Local Queue. The local queue of the task which is accessed4100reasonably efficiently by the task. Other tasks can steal from4101it when they run out of work. Throughout the marking phase, a4102task attempts to keep its local queue short but not totally4103empty, so that entries are available for stealing by other4104tasks. Only when there is no more work, a task will totally4105drain its local queue.41064107(3) Global Mark Stack. This handles local queue overflow. During4108marking only sets of entries are moved between it and the local4109queues, as access to it requires a mutex and more fine-grain4110interaction with it which might cause contention. If it4111overflows, then the marking phase should restart and iterate4112over the bitmap to identify gray objects. Throughout the marking4113phase, tasks attempt to keep the global mark stack at a small4114length but not totally empty, so that entries are available for4115popping by other tasks. Only when there is no more work, tasks4116will totally drain the global mark stack.41174118(4) SATB Buffer Queue. This is where completed SATB buffers are4119made available. Buffers are regularly removed from this queue4120and scanned for roots, so that the queue doesn't get too4121long. During remark, all completed buffers are processed, as4122well as the filled in parts of any uncompleted buffers.41234124The do_marking_step() method tries to abort when the time target4125has been reached. There are a few other cases when the4126do_marking_step() method also aborts:41274128(1) When the marking phase has been aborted (after a Full GC).41294130(2) When a global overflow (on the global stack) has been4131triggered. Before the task aborts, it will actually sync up with4132the other tasks to ensure that all the marking data structures4133(local queues, stacks, fingers etc.) are re-initialized so that4134when do_marking_step() completes, the marking phase can4135immediately restart.41364137(3) When enough completed SATB buffers are available. The4138do_marking_step() method only tries to drain SATB buffers right4139at the beginning. So, if enough buffers are available, the4140marking step aborts and the SATB buffers are processed at4141the beginning of the next invocation.41424143(4) To yield. when we have to yield then we abort and yield4144right at the end of do_marking_step(). This saves us from a lot4145of hassle as, by yielding we might allow a Full GC. If this4146happens then objects will be compacted underneath our feet, the4147heap might shrink, etc. We save checking for this by just4148aborting and doing the yield right at the end.41494150From the above it follows that the do_marking_step() method should4151be called in a loop (or, otherwise, regularly) until it completes.41524153If a marking step completes without its has_aborted() flag being4154true, it means it has completed the current marking phase (and4155also all other marking tasks have done so and have all synced up).41564157A method called regular_clock_call() is invoked "regularly" (in4158sub ms intervals) throughout marking. It is this clock method that4159checks all the abort conditions which were mentioned above and4160decides when the task should abort. A work-based scheme is used to4161trigger this clock method: when the number of object words the4162marking phase has scanned or the number of references the marking4163phase has visited reach a given limit. Additional invocations to4164the method clock have been planted in a few other strategic places4165too. The initial reason for the clock method was to avoid calling4166vtime too regularly, as it is quite expensive. So, once it was in4167place, it was natural to piggy-back all the other conditions on it4168too and not constantly check them throughout the code.41694170If do_termination is true then do_marking_step will enter its4171termination protocol.41724173The value of is_serial must be true when do_marking_step is being4174called serially (i.e. by the VMThread) and do_marking_step should4175skip any synchronization in the termination and overflow code.4176Examples include the serial remark code and the serial reference4177processing closures.41784179The value of is_serial must be false when do_marking_step is4180being called by any of the worker threads in a work gang.4181Examples include the concurrent marking code (CMMarkingTask),4182the MT remark code, and the MT reference processing closures.41834184*****************************************************************************/41854186void CMTask::do_marking_step(double time_target_ms,4187bool do_termination,4188bool is_serial) {4189assert(time_target_ms >= 1.0, "minimum granularity is 1ms");4190assert(concurrent() == _cm->concurrent(), "they should be the same");41914192G1CollectorPolicy* g1_policy = _g1h->g1_policy();4193assert(_task_queues != NULL, "invariant");4194assert(_task_queue != NULL, "invariant");4195assert(_task_queues->queue(_worker_id) == _task_queue, "invariant");41964197assert(!_claimed,4198"only one thread should claim this task at any one time");41994200// OK, this doesn't safeguard again all possible scenarios, as it is4201// possible for two threads to set the _claimed flag at the same4202// time. But it is only for debugging purposes anyway and it will4203// catch most problems.4204_claimed = true;42054206_start_time_ms = os::elapsedVTime() * 1000.0;4207statsOnly( _interval_start_time_ms = _start_time_ms );42084209// If do_stealing is true then do_marking_step will attempt to4210// steal work from the other CMTasks. It only makes sense to4211// enable stealing when the termination protocol is enabled4212// and do_marking_step() is not being called serially.4213bool do_stealing = do_termination && !is_serial;42144215double diff_prediction_ms =4216g1_policy->get_new_prediction(&_marking_step_diffs_ms);4217_time_target_ms = time_target_ms - diff_prediction_ms;42184219// set up the variables that are used in the work-based scheme to4220// call the regular clock method4221_words_scanned = 0;4222_refs_reached = 0;4223recalculate_limits();42244225// clear all flags4226clear_has_aborted();4227_has_timed_out = false;4228_draining_satb_buffers = false;42294230++_calls;42314232if (_cm->verbose_low()) {4233gclog_or_tty->print_cr("[%u] >>>>>>>>>> START, call = %d, "4234"target = %1.2lfms >>>>>>>>>>",4235_worker_id, _calls, _time_target_ms);4236}42374238// Set up the bitmap and oop closures. Anything that uses them is4239// eventually called from this method, so it is OK to allocate these4240// statically.4241CMBitMapClosure bitmap_closure(this, _cm, _nextMarkBitMap);4242G1CMOopClosure cm_oop_closure(_g1h, _cm, this);4243set_cm_oop_closure(&cm_oop_closure);42444245if (_cm->has_overflown()) {4246// This can happen if the mark stack overflows during a GC pause4247// and this task, after a yield point, restarts. We have to abort4248// as we need to get into the overflow protocol which happens4249// right at the end of this task.4250set_has_aborted();4251}42524253// First drain any available SATB buffers. After this, we will not4254// look at SATB buffers before the next invocation of this method.4255// If enough completed SATB buffers are queued up, the regular clock4256// will abort this task so that it restarts.4257drain_satb_buffers();4258// ...then partially drain the local queue and the global stack4259drain_local_queue(true);4260drain_global_stack(true);42614262do {4263if (!has_aborted() && _curr_region != NULL) {4264// This means that we're already holding on to a region.4265assert(_finger != NULL, "if region is not NULL, then the finger "4266"should not be NULL either");42674268// We might have restarted this task after an evacuation pause4269// which might have evacuated the region we're holding on to4270// underneath our feet. Let's read its limit again to make sure4271// that we do not iterate over a region of the heap that4272// contains garbage (update_region_limit() will also move4273// _finger to the start of the region if it is found empty).4274update_region_limit();4275// We will start from _finger not from the start of the region,4276// as we might be restarting this task after aborting half-way4277// through scanning this region. In this case, _finger points to4278// the address where we last found a marked object. If this is a4279// fresh region, _finger points to start().4280MemRegion mr = MemRegion(_finger, _region_limit);42814282if (_cm->verbose_low()) {4283gclog_or_tty->print_cr("[%u] we're scanning part "4284"[" PTR_FORMAT ", " PTR_FORMAT ") "4285"of region " HR_FORMAT,4286_worker_id, p2i(_finger), p2i(_region_limit),4287HR_FORMAT_PARAMS(_curr_region));4288}42894290assert(!_curr_region->isHumongous() || mr.start() == _curr_region->bottom(),4291"humongous regions should go around loop once only");42924293// Some special cases:4294// If the memory region is empty, we can just give up the region.4295// If the current region is humongous then we only need to check4296// the bitmap for the bit associated with the start of the object,4297// scan the object if it's live, and give up the region.4298// Otherwise, let's iterate over the bitmap of the part of the region4299// that is left.4300// If the iteration is successful, give up the region.4301if (mr.is_empty()) {4302giveup_current_region();4303regular_clock_call();4304} else if (_curr_region->isHumongous() && mr.start() == _curr_region->bottom()) {4305if (_nextMarkBitMap->isMarked(mr.start())) {4306// The object is marked - apply the closure4307BitMap::idx_t offset = _nextMarkBitMap->heapWordToOffset(mr.start());4308bitmap_closure.do_bit(offset);4309}4310// Even if this task aborted while scanning the humongous object4311// we can (and should) give up the current region.4312giveup_current_region();4313regular_clock_call();4314} else if (_nextMarkBitMap->iterate(&bitmap_closure, mr)) {4315giveup_current_region();4316regular_clock_call();4317} else {4318assert(has_aborted(), "currently the only way to do so");4319// The only way to abort the bitmap iteration is to return4320// false from the do_bit() method. However, inside the4321// do_bit() method we move the _finger to point to the4322// object currently being looked at. So, if we bail out, we4323// have definitely set _finger to something non-null.4324assert(_finger != NULL, "invariant");43254326// Region iteration was actually aborted. So now _finger4327// points to the address of the object we last scanned. If we4328// leave it there, when we restart this task, we will rescan4329// the object. It is easy to avoid this. We move the finger by4330// enough to point to the next possible object header (the4331// bitmap knows by how much we need to move it as it knows its4332// granularity).4333assert(_finger < _region_limit, "invariant");4334HeapWord* new_finger = _nextMarkBitMap->nextObject(_finger);4335// Check if bitmap iteration was aborted while scanning the last object4336if (new_finger >= _region_limit) {4337giveup_current_region();4338} else {4339move_finger_to(new_finger);4340}4341}4342}4343// At this point we have either completed iterating over the4344// region we were holding on to, or we have aborted.43454346// We then partially drain the local queue and the global stack.4347// (Do we really need this?)4348drain_local_queue(true);4349drain_global_stack(true);43504351// Read the note on the claim_region() method on why it might4352// return NULL with potentially more regions available for4353// claiming and why we have to check out_of_regions() to determine4354// whether we're done or not.4355while (!has_aborted() && _curr_region == NULL && !_cm->out_of_regions()) {4356// We are going to try to claim a new region. We should have4357// given up on the previous one.4358// Separated the asserts so that we know which one fires.4359assert(_curr_region == NULL, "invariant");4360assert(_finger == NULL, "invariant");4361assert(_region_limit == NULL, "invariant");4362if (_cm->verbose_low()) {4363gclog_or_tty->print_cr("[%u] trying to claim a new region", _worker_id);4364}4365HeapRegion* claimed_region = _cm->claim_region(_worker_id);4366if (claimed_region != NULL) {4367// Yes, we managed to claim one4368statsOnly( ++_regions_claimed );43694370if (_cm->verbose_low()) {4371gclog_or_tty->print_cr("[%u] we successfully claimed "4372"region " PTR_FORMAT,4373_worker_id, p2i(claimed_region));4374}43754376setup_for_region(claimed_region);4377assert(_curr_region == claimed_region, "invariant");4378}4379// It is important to call the regular clock here. It might take4380// a while to claim a region if, for example, we hit a large4381// block of empty regions. So we need to call the regular clock4382// method once round the loop to make sure it's called4383// frequently enough.4384regular_clock_call();4385}43864387if (!has_aborted() && _curr_region == NULL) {4388assert(_cm->out_of_regions(),4389"at this point we should be out of regions");4390}4391} while ( _curr_region != NULL && !has_aborted());43924393if (!has_aborted()) {4394// We cannot check whether the global stack is empty, since other4395// tasks might be pushing objects to it concurrently.4396assert(_cm->out_of_regions(),4397"at this point we should be out of regions");43984399if (_cm->verbose_low()) {4400gclog_or_tty->print_cr("[%u] all regions claimed", _worker_id);4401}44024403// Try to reduce the number of available SATB buffers so that4404// remark has less work to do.4405drain_satb_buffers();4406}44074408// Since we've done everything else, we can now totally drain the4409// local queue and global stack.4410drain_local_queue(false);4411drain_global_stack(false);44124413// Attempt at work stealing from other task's queues.4414if (do_stealing && !has_aborted()) {4415// We have not aborted. This means that we have finished all that4416// we could. Let's try to do some stealing...44174418// We cannot check whether the global stack is empty, since other4419// tasks might be pushing objects to it concurrently.4420assert(_cm->out_of_regions() && _task_queue->size() == 0,4421"only way to reach here");44224423if (_cm->verbose_low()) {4424gclog_or_tty->print_cr("[%u] starting to steal", _worker_id);4425}44264427while (!has_aborted()) {4428oop obj;4429statsOnly( ++_steal_attempts );44304431if (_cm->try_stealing(_worker_id, &_hash_seed, obj)) {4432if (_cm->verbose_medium()) {4433gclog_or_tty->print_cr("[%u] stolen " PTR_FORMAT " successfully",4434_worker_id, p2i((void*) obj));4435}44364437statsOnly( ++_steals );44384439scan_object(obj);44404441// And since we're towards the end, let's totally drain the4442// local queue and global stack.4443drain_local_queue(false);4444drain_global_stack(false);4445} else {4446break;4447}4448}4449}44504451// If we are about to wrap up and go into termination, check if we4452// should raise the overflow flag.4453if (do_termination && !has_aborted()) {4454if (_cm->force_overflow()->should_force()) {4455_cm->set_has_overflown();4456regular_clock_call();4457}4458}44594460// We still haven't aborted. Now, let's try to get into the4461// termination protocol.4462if (do_termination && !has_aborted()) {4463// We cannot check whether the global stack is empty, since other4464// tasks might be concurrently pushing objects on it.4465// Separated the asserts so that we know which one fires.4466assert(_cm->out_of_regions(), "only way to reach here");4467assert(_task_queue->size() == 0, "only way to reach here");44684469if (_cm->verbose_low()) {4470gclog_or_tty->print_cr("[%u] starting termination protocol", _worker_id);4471}44724473_termination_start_time_ms = os::elapsedVTime() * 1000.0;44744475// The CMTask class also extends the TerminatorTerminator class,4476// hence its should_exit_termination() method will also decide4477// whether to exit the termination protocol or not.4478bool finished = (is_serial ||4479_cm->terminator()->offer_termination(this));4480double termination_end_time_ms = os::elapsedVTime() * 1000.0;4481_termination_time_ms +=4482termination_end_time_ms - _termination_start_time_ms;44834484if (finished) {4485// We're all done.44864487if (_worker_id == 0) {4488// let's allow task 0 to do this4489if (concurrent()) {4490assert(_cm->concurrent_marking_in_progress(), "invariant");4491// we need to set this to false before the next4492// safepoint. This way we ensure that the marking phase4493// doesn't observe any more heap expansions.4494_cm->clear_concurrent_marking_in_progress();4495}4496}44974498// We can now guarantee that the global stack is empty, since4499// all other tasks have finished. We separated the guarantees so4500// that, if a condition is false, we can immediately find out4501// which one.4502guarantee(_cm->out_of_regions(), "only way to reach here");4503guarantee(_cm->mark_stack_empty(), "only way to reach here");4504guarantee(_task_queue->size() == 0, "only way to reach here");4505guarantee(!_cm->has_overflown(), "only way to reach here");4506guarantee(!_cm->mark_stack_overflow(), "only way to reach here");45074508if (_cm->verbose_low()) {4509gclog_or_tty->print_cr("[%u] all tasks terminated", _worker_id);4510}4511} else {4512// Apparently there's more work to do. Let's abort this task. It4513// will restart it and we can hopefully find more things to do.45144515if (_cm->verbose_low()) {4516gclog_or_tty->print_cr("[%u] apparently there is more work to do",4517_worker_id);4518}45194520set_has_aborted();4521statsOnly( ++_aborted_termination );4522}4523}45244525// Mainly for debugging purposes to make sure that a pointer to the4526// closure which was statically allocated in this frame doesn't4527// escape it by accident.4528set_cm_oop_closure(NULL);4529double end_time_ms = os::elapsedVTime() * 1000.0;4530double elapsed_time_ms = end_time_ms - _start_time_ms;4531// Update the step history.4532_step_times_ms.add(elapsed_time_ms);45334534if (has_aborted()) {4535// The task was aborted for some reason.45364537statsOnly( ++_aborted );45384539if (_has_timed_out) {4540double diff_ms = elapsed_time_ms - _time_target_ms;4541// Keep statistics of how well we did with respect to hitting4542// our target only if we actually timed out (if we aborted for4543// other reasons, then the results might get skewed).4544_marking_step_diffs_ms.add(diff_ms);4545}45464547if (_cm->has_overflown()) {4548// This is the interesting one. We aborted because a global4549// overflow was raised. This means we have to restart the4550// marking phase and start iterating over regions. However, in4551// order to do this we have to make sure that all tasks stop4552// what they are doing and re-initialise in a safe manner. We4553// will achieve this with the use of two barrier sync points.45544555if (_cm->verbose_low()) {4556gclog_or_tty->print_cr("[%u] detected overflow", _worker_id);4557}45584559if (!is_serial) {4560// We only need to enter the sync barrier if being called4561// from a parallel context4562_cm->enter_first_sync_barrier(_worker_id);45634564// When we exit this sync barrier we know that all tasks have4565// stopped doing marking work. So, it's now safe to4566// re-initialise our data structures. At the end of this method,4567// task 0 will clear the global data structures.4568}45694570statsOnly( ++_aborted_overflow );45714572// We clear the local state of this task...4573clear_region_fields();45744575if (!is_serial) {4576// ...and enter the second barrier.4577_cm->enter_second_sync_barrier(_worker_id);4578}4579// At this point, if we're during the concurrent phase of4580// marking, everything has been re-initialized and we're4581// ready to restart.4582}45834584if (_cm->verbose_low()) {4585gclog_or_tty->print_cr("[%u] <<<<<<<<<< ABORTING, target = %1.2lfms, "4586"elapsed = %1.2lfms <<<<<<<<<<",4587_worker_id, _time_target_ms, elapsed_time_ms);4588if (_cm->has_aborted()) {4589gclog_or_tty->print_cr("[%u] ========== MARKING ABORTED ==========",4590_worker_id);4591}4592}4593} else {4594if (_cm->verbose_low()) {4595gclog_or_tty->print_cr("[%u] <<<<<<<<<< FINISHED, target = %1.2lfms, "4596"elapsed = %1.2lfms <<<<<<<<<<",4597_worker_id, _time_target_ms, elapsed_time_ms);4598}4599}46004601_claimed = false;4602}46034604CMTask::CMTask(uint worker_id,4605ConcurrentMark* cm,4606size_t* marked_bytes,4607BitMap* card_bm,4608CMTaskQueue* task_queue,4609CMTaskQueueSet* task_queues)4610: _g1h(G1CollectedHeap::heap()),4611_worker_id(worker_id), _cm(cm),4612_objArray_processor(this),4613_claimed(false),4614_nextMarkBitMap(NULL), _hash_seed(17),4615_task_queue(task_queue),4616_task_queues(task_queues),4617_cm_oop_closure(NULL),4618_marked_bytes_array(marked_bytes),4619_card_bm(card_bm) {4620guarantee(task_queue != NULL, "invariant");4621guarantee(task_queues != NULL, "invariant");46224623statsOnly( _clock_due_to_scanning = 0;4624_clock_due_to_marking = 0 );46254626_marking_step_diffs_ms.add(0.5);4627}46284629// These are formatting macros that are used below to ensure4630// consistent formatting. The *_H_* versions are used to format the4631// header for a particular value and they should be kept consistent4632// with the corresponding macro. Also note that most of the macros add4633// the necessary white space (as a prefix) which makes them a bit4634// easier to compose.46354636// All the output lines are prefixed with this string to be able to4637// identify them easily in a large log file.4638#define G1PPRL_LINE_PREFIX "###"46394640#define G1PPRL_ADDR_BASE_FORMAT " " PTR_FORMAT "-" PTR_FORMAT4641#ifdef _LP644642#define G1PPRL_ADDR_BASE_H_FORMAT " %37s"4643#else // _LP644644#define G1PPRL_ADDR_BASE_H_FORMAT " %21s"4645#endif // _LP6446464647// For per-region info4648#define G1PPRL_TYPE_FORMAT " %-4s"4649#define G1PPRL_TYPE_H_FORMAT " %4s"4650#define G1PPRL_BYTE_FORMAT " " SIZE_FORMAT_W(9)4651#define G1PPRL_BYTE_H_FORMAT " %9s"4652#define G1PPRL_DOUBLE_FORMAT " %14.1f"4653#define G1PPRL_DOUBLE_H_FORMAT " %14s"46544655// For summary info4656#define G1PPRL_SUM_ADDR_FORMAT(tag) " " tag ":" G1PPRL_ADDR_BASE_FORMAT4657#define G1PPRL_SUM_BYTE_FORMAT(tag) " " tag ": " SIZE_FORMAT4658#define G1PPRL_SUM_MB_FORMAT(tag) " " tag ": %1.2f MB"4659#define G1PPRL_SUM_MB_PERC_FORMAT(tag) G1PPRL_SUM_MB_FORMAT(tag) " / %1.2f %%"46604661G1PrintRegionLivenessInfoClosure::4662G1PrintRegionLivenessInfoClosure(outputStream* out, const char* phase_name)4663: _out(out),4664_total_used_bytes(0), _total_capacity_bytes(0),4665_total_prev_live_bytes(0), _total_next_live_bytes(0),4666_hum_used_bytes(0), _hum_capacity_bytes(0),4667_hum_prev_live_bytes(0), _hum_next_live_bytes(0),4668_total_remset_bytes(0), _total_strong_code_roots_bytes(0) {4669G1CollectedHeap* g1h = G1CollectedHeap::heap();4670MemRegion g1_reserved = g1h->g1_reserved();4671double now = os::elapsedTime();46724673// Print the header of the output.4674_out->cr();4675_out->print_cr(G1PPRL_LINE_PREFIX" PHASE %s @ %1.3f", phase_name, now);4676_out->print_cr(G1PPRL_LINE_PREFIX" HEAP"4677G1PPRL_SUM_ADDR_FORMAT("reserved")4678G1PPRL_SUM_BYTE_FORMAT("region-size"),4679p2i(g1_reserved.start()), p2i(g1_reserved.end()),4680HeapRegion::GrainBytes);4681_out->print_cr(G1PPRL_LINE_PREFIX);4682_out->print_cr(G1PPRL_LINE_PREFIX4683G1PPRL_TYPE_H_FORMAT4684G1PPRL_ADDR_BASE_H_FORMAT4685G1PPRL_BYTE_H_FORMAT4686G1PPRL_BYTE_H_FORMAT4687G1PPRL_BYTE_H_FORMAT4688G1PPRL_DOUBLE_H_FORMAT4689G1PPRL_BYTE_H_FORMAT4690G1PPRL_BYTE_H_FORMAT,4691"type", "address-range",4692"used", "prev-live", "next-live", "gc-eff",4693"remset", "code-roots");4694_out->print_cr(G1PPRL_LINE_PREFIX4695G1PPRL_TYPE_H_FORMAT4696G1PPRL_ADDR_BASE_H_FORMAT4697G1PPRL_BYTE_H_FORMAT4698G1PPRL_BYTE_H_FORMAT4699G1PPRL_BYTE_H_FORMAT4700G1PPRL_DOUBLE_H_FORMAT4701G1PPRL_BYTE_H_FORMAT4702G1PPRL_BYTE_H_FORMAT,4703"", "",4704"(bytes)", "(bytes)", "(bytes)", "(bytes/ms)",4705"(bytes)", "(bytes)");4706}47074708// It takes as a parameter a reference to one of the _hum_* fields, it4709// deduces the corresponding value for a region in a humongous region4710// series (either the region size, or what's left if the _hum_* field4711// is < the region size), and updates the _hum_* field accordingly.4712size_t G1PrintRegionLivenessInfoClosure::get_hum_bytes(size_t* hum_bytes) {4713size_t bytes = 0;4714// The > 0 check is to deal with the prev and next live bytes which4715// could be 0.4716if (*hum_bytes > 0) {4717bytes = MIN2(HeapRegion::GrainBytes, *hum_bytes);4718*hum_bytes -= bytes;4719}4720return bytes;4721}47224723// It deduces the values for a region in a humongous region series4724// from the _hum_* fields and updates those accordingly. It assumes4725// that that _hum_* fields have already been set up from the "starts4726// humongous" region and we visit the regions in address order.4727void G1PrintRegionLivenessInfoClosure::get_hum_bytes(size_t* used_bytes,4728size_t* capacity_bytes,4729size_t* prev_live_bytes,4730size_t* next_live_bytes) {4731assert(_hum_used_bytes > 0 && _hum_capacity_bytes > 0, "pre-condition");4732*used_bytes = get_hum_bytes(&_hum_used_bytes);4733*capacity_bytes = get_hum_bytes(&_hum_capacity_bytes);4734*prev_live_bytes = get_hum_bytes(&_hum_prev_live_bytes);4735*next_live_bytes = get_hum_bytes(&_hum_next_live_bytes);4736}47374738bool G1PrintRegionLivenessInfoClosure::doHeapRegion(HeapRegion* r) {4739const char* type = r->get_type_str();4740HeapWord* bottom = r->bottom();4741HeapWord* end = r->end();4742size_t capacity_bytes = r->capacity();4743size_t used_bytes = r->used();4744size_t prev_live_bytes = r->live_bytes();4745size_t next_live_bytes = r->next_live_bytes();4746double gc_eff = r->gc_efficiency();4747size_t remset_bytes = r->rem_set()->mem_size();4748size_t strong_code_roots_bytes = r->rem_set()->strong_code_roots_mem_size();47494750if (r->startsHumongous()) {4751assert(_hum_used_bytes == 0 && _hum_capacity_bytes == 0 &&4752_hum_prev_live_bytes == 0 && _hum_next_live_bytes == 0,4753"they should have been zeroed after the last time we used them");4754// Set up the _hum_* fields.4755_hum_capacity_bytes = capacity_bytes;4756_hum_used_bytes = used_bytes;4757_hum_prev_live_bytes = prev_live_bytes;4758_hum_next_live_bytes = next_live_bytes;4759get_hum_bytes(&used_bytes, &capacity_bytes,4760&prev_live_bytes, &next_live_bytes);4761end = bottom + HeapRegion::GrainWords;4762} else if (r->continuesHumongous()) {4763get_hum_bytes(&used_bytes, &capacity_bytes,4764&prev_live_bytes, &next_live_bytes);4765assert(end == bottom + HeapRegion::GrainWords, "invariant");4766}47674768_total_used_bytes += used_bytes;4769_total_capacity_bytes += capacity_bytes;4770_total_prev_live_bytes += prev_live_bytes;4771_total_next_live_bytes += next_live_bytes;4772_total_remset_bytes += remset_bytes;4773_total_strong_code_roots_bytes += strong_code_roots_bytes;47744775// Print a line for this particular region.4776_out->print_cr(G1PPRL_LINE_PREFIX4777G1PPRL_TYPE_FORMAT4778G1PPRL_ADDR_BASE_FORMAT4779G1PPRL_BYTE_FORMAT4780G1PPRL_BYTE_FORMAT4781G1PPRL_BYTE_FORMAT4782G1PPRL_DOUBLE_FORMAT4783G1PPRL_BYTE_FORMAT4784G1PPRL_BYTE_FORMAT,4785type, p2i(bottom), p2i(end),4786used_bytes, prev_live_bytes, next_live_bytes, gc_eff,4787remset_bytes, strong_code_roots_bytes);47884789return false;4790}47914792G1PrintRegionLivenessInfoClosure::~G1PrintRegionLivenessInfoClosure() {4793// add static memory usages to remembered set sizes4794_total_remset_bytes += HeapRegionRemSet::fl_mem_size() + HeapRegionRemSet::static_mem_size();4795// Print the footer of the output.4796_out->print_cr(G1PPRL_LINE_PREFIX);4797_out->print_cr(G1PPRL_LINE_PREFIX4798" SUMMARY"4799G1PPRL_SUM_MB_FORMAT("capacity")4800G1PPRL_SUM_MB_PERC_FORMAT("used")4801G1PPRL_SUM_MB_PERC_FORMAT("prev-live")4802G1PPRL_SUM_MB_PERC_FORMAT("next-live")4803G1PPRL_SUM_MB_FORMAT("remset")4804G1PPRL_SUM_MB_FORMAT("code-roots"),4805bytes_to_mb(_total_capacity_bytes),4806bytes_to_mb(_total_used_bytes),4807perc(_total_used_bytes, _total_capacity_bytes),4808bytes_to_mb(_total_prev_live_bytes),4809perc(_total_prev_live_bytes, _total_capacity_bytes),4810bytes_to_mb(_total_next_live_bytes),4811perc(_total_next_live_bytes, _total_capacity_bytes),4812bytes_to_mb(_total_remset_bytes),4813bytes_to_mb(_total_strong_code_roots_bytes));4814_out->cr();4815}481648174818