Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_implementation/parNew/parCardTableModRefBS.cpp
38920 views
/*1* Copyright (c) 2007, 2014, 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 "memory/allocation.inline.hpp"26#include "memory/cardTableModRefBS.hpp"27#include "memory/cardTableRS.hpp"28#include "memory/sharedHeap.hpp"29#include "memory/space.inline.hpp"30#include "memory/universe.hpp"31#include "oops/oop.inline.hpp"32#include "runtime/java.hpp"33#include "runtime/mutexLocker.hpp"34#include "runtime/orderAccess.inline.hpp"35#include "runtime/virtualspace.hpp"36#include "runtime/vmThread.hpp"3738PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC3940void CardTableModRefBS::non_clean_card_iterate_parallel_work(Space* sp, MemRegion mr,41OopsInGenClosure* cl,42CardTableRS* ct,43int n_threads) {44assert(n_threads > 0, "Error: expected n_threads > 0");45assert((n_threads == 1 && ParallelGCThreads == 0) ||46n_threads <= (int)ParallelGCThreads,47"# worker threads != # requested!");48assert(!Thread::current()->is_VM_thread() || (n_threads == 1), "There is only 1 VM thread");49assert(UseDynamicNumberOfGCThreads ||50!FLAG_IS_DEFAULT(ParallelGCThreads) ||51n_threads == (int)ParallelGCThreads,52"# worker threads != # requested!");53// Make sure the LNC array is valid for the space.54jbyte** lowest_non_clean;55uintptr_t lowest_non_clean_base_chunk_index;56size_t lowest_non_clean_chunk_size;57get_LNC_array_for_space(sp, lowest_non_clean,58lowest_non_clean_base_chunk_index,59lowest_non_clean_chunk_size);6061uint n_strides = n_threads * ParGCStridesPerThread;62SequentialSubTasksDone* pst = sp->par_seq_tasks();63// Sets the condition for completion of the subtask (how many threads64// need to finish in order to be done).65pst->set_n_threads(n_threads);66pst->set_n_tasks(n_strides);6768uint stride = 0;69while (!pst->is_task_claimed(/* reference */ stride)) {70process_stride(sp, mr, stride, n_strides, cl, ct,71lowest_non_clean,72lowest_non_clean_base_chunk_index,73lowest_non_clean_chunk_size);74}75if (pst->all_tasks_completed()) {76// Clear lowest_non_clean array for next time.77intptr_t first_chunk_index = addr_to_chunk_index(mr.start());78uintptr_t last_chunk_index = addr_to_chunk_index(mr.last());79for (uintptr_t ch = first_chunk_index; ch <= last_chunk_index; ch++) {80intptr_t ind = ch - lowest_non_clean_base_chunk_index;81assert(0 <= ind && ind < (intptr_t)lowest_non_clean_chunk_size,82"Bounds error");83lowest_non_clean[ind] = NULL;84}85}86}8788void89CardTableModRefBS::90process_stride(Space* sp,91MemRegion used,92jint stride, int n_strides,93OopsInGenClosure* cl,94CardTableRS* ct,95jbyte** lowest_non_clean,96uintptr_t lowest_non_clean_base_chunk_index,97size_t lowest_non_clean_chunk_size) {98// We go from higher to lower addresses here; it wouldn't help that much99// because of the strided parallelism pattern used here.100101// Find the first card address of the first chunk in the stride that is102// at least "bottom" of the used region.103jbyte* start_card = byte_for(used.start());104jbyte* end_card = byte_after(used.last());105uintptr_t start_chunk = addr_to_chunk_index(used.start());106uintptr_t start_chunk_stride_num = start_chunk % n_strides;107jbyte* chunk_card_start;108109if ((uintptr_t)stride >= start_chunk_stride_num) {110chunk_card_start = (jbyte*)(start_card +111(stride - start_chunk_stride_num) *112ParGCCardsPerStrideChunk);113} else {114// Go ahead to the next chunk group boundary, then to the requested stride.115chunk_card_start = (jbyte*)(start_card +116(n_strides - start_chunk_stride_num + stride) *117ParGCCardsPerStrideChunk);118}119120while (chunk_card_start < end_card) {121// Even though we go from lower to higher addresses below, the122// strided parallelism can interleave the actual processing of the123// dirty pages in various ways. For a specific chunk within this124// stride, we take care to avoid double scanning or missing a card125// by suitably initializing the "min_done" field in process_chunk_boundaries()126// below, together with the dirty region extension accomplished in127// DirtyCardToOopClosure::do_MemRegion().128jbyte* chunk_card_end = chunk_card_start + ParGCCardsPerStrideChunk;129// Invariant: chunk_mr should be fully contained within the "used" region.130MemRegion chunk_mr = MemRegion(addr_for(chunk_card_start),131chunk_card_end >= end_card ?132used.end() : addr_for(chunk_card_end));133assert(chunk_mr.word_size() > 0, "[chunk_card_start > used_end)");134assert(used.contains(chunk_mr), "chunk_mr should be subset of used");135136DirtyCardToOopClosure* dcto_cl = sp->new_dcto_cl(cl, precision(),137cl->gen_boundary());138ClearNoncleanCardWrapper clear_cl(dcto_cl, ct);139140141// Process the chunk.142process_chunk_boundaries(sp,143dcto_cl,144chunk_mr,145used,146lowest_non_clean,147lowest_non_clean_base_chunk_index,148lowest_non_clean_chunk_size);149150// We want the LNC array updates above in process_chunk_boundaries151// to be visible before any of the card table value changes as a152// result of the dirty card iteration below.153OrderAccess::storestore();154155// We do not call the non_clean_card_iterate_serial() version because156// we want to clear the cards: clear_cl here does the work of finding157// contiguous dirty ranges of cards to process and clear.158clear_cl.do_MemRegion(chunk_mr);159160// Find the next chunk of the stride.161chunk_card_start += ParGCCardsPerStrideChunk * n_strides;162}163}164165166// If you want a talkative process_chunk_boundaries,167// then #define NOISY(x) x168#ifdef NOISY169#error "Encountered a global preprocessor flag, NOISY, which might clash with local definition to follow"170#else171#define NOISY(x)172#endif173174void175CardTableModRefBS::176process_chunk_boundaries(Space* sp,177DirtyCardToOopClosure* dcto_cl,178MemRegion chunk_mr,179MemRegion used,180jbyte** lowest_non_clean,181uintptr_t lowest_non_clean_base_chunk_index,182size_t lowest_non_clean_chunk_size)183{184// We must worry about non-array objects that cross chunk boundaries,185// because such objects are both precisely and imprecisely marked:186// .. if the head of such an object is dirty, the entire object187// needs to be scanned, under the interpretation that this188// was an imprecise mark189// .. if the head of such an object is not dirty, we can assume190// precise marking and it's efficient to scan just the dirty191// cards.192// In either case, each scanned reference must be scanned precisely193// once so as to avoid cloning of a young referent. For efficiency,194// our closures depend on this property and do not protect against195// double scans.196197uintptr_t cur_chunk_index = addr_to_chunk_index(chunk_mr.start());198cur_chunk_index = cur_chunk_index - lowest_non_clean_base_chunk_index;199200NOISY(tty->print_cr("===========================================================================");)201NOISY(tty->print_cr(" process_chunk_boundary: Called with [" PTR_FORMAT "," PTR_FORMAT ")",202chunk_mr.start(), chunk_mr.end());)203204// First, set "our" lowest_non_clean entry, which would be205// used by the thread scanning an adjoining left chunk with206// a non-array object straddling the mutual boundary.207// Find the object that spans our boundary, if one exists.208// first_block is the block possibly straddling our left boundary.209HeapWord* first_block = sp->block_start(chunk_mr.start());210assert((chunk_mr.start() != used.start()) || (first_block == chunk_mr.start()),211"First chunk should always have a co-initial block");212// Does the block straddle the chunk's left boundary, and is it213// a non-array object?214if (first_block < chunk_mr.start() // first block straddles left bdry215&& sp->block_is_obj(first_block) // first block is an object216&& !(oop(first_block)->is_objArray() // first block is not an array (arrays are precisely dirtied)217|| oop(first_block)->is_typeArray())) {218// Find our least non-clean card, so that a left neighbour219// does not scan an object straddling the mutual boundary220// too far to the right, and attempt to scan a portion of221// that object twice.222jbyte* first_dirty_card = NULL;223jbyte* last_card_of_first_obj =224byte_for(first_block + sp->block_size(first_block) - 1);225jbyte* first_card_of_cur_chunk = byte_for(chunk_mr.start());226jbyte* last_card_of_cur_chunk = byte_for(chunk_mr.last());227jbyte* last_card_to_check =228(jbyte*) MIN2((intptr_t) last_card_of_cur_chunk,229(intptr_t) last_card_of_first_obj);230// Note that this does not need to go beyond our last card231// if our first object completely straddles this chunk.232for (jbyte* cur = first_card_of_cur_chunk;233cur <= last_card_to_check; cur++) {234jbyte val = *cur;235if (card_will_be_scanned(val)) {236first_dirty_card = cur; break;237} else {238assert(!card_may_have_been_dirty(val), "Error");239}240}241if (first_dirty_card != NULL) {242NOISY(tty->print_cr(" LNC: Found a dirty card at " PTR_FORMAT " in current chunk",243first_dirty_card);)244assert(0 <= cur_chunk_index && cur_chunk_index < lowest_non_clean_chunk_size,245"Bounds error.");246assert(lowest_non_clean[cur_chunk_index] == NULL,247"Write exactly once : value should be stable hereafter for this round");248lowest_non_clean[cur_chunk_index] = first_dirty_card;249} NOISY(else {250tty->print_cr(" LNC: Found no dirty card in current chunk; leaving LNC entry NULL");251// In the future, we could have this thread look for a non-NULL value to copy from its252// right neighbour (up to the end of the first object).253if (last_card_of_cur_chunk < last_card_of_first_obj) {254tty->print_cr(" LNC: BEWARE!!! first obj straddles past right end of chunk:\n"255" might be efficient to get value from right neighbour?");256}257})258} else {259// In this case we can help our neighbour by just asking them260// to stop at our first card (even though it may not be dirty).261NOISY(tty->print_cr(" LNC: first block is not a non-array object; setting LNC to first card of current chunk");)262assert(lowest_non_clean[cur_chunk_index] == NULL, "Write once : value should be stable hereafter");263jbyte* first_card_of_cur_chunk = byte_for(chunk_mr.start());264lowest_non_clean[cur_chunk_index] = first_card_of_cur_chunk;265}266NOISY(tty->print_cr(" process_chunk_boundary: lowest_non_clean[" INTPTR_FORMAT "] = " PTR_FORMAT267" which corresponds to the heap address " PTR_FORMAT,268cur_chunk_index, lowest_non_clean[cur_chunk_index],269(lowest_non_clean[cur_chunk_index] != NULL)270? addr_for(lowest_non_clean[cur_chunk_index])271: NULL);)272NOISY(tty->print_cr("---------------------------------------------------------------------------");)273274// Next, set our own max_to_do, which will strictly/exclusively bound275// the highest address that we will scan past the right end of our chunk.276HeapWord* max_to_do = NULL;277if (chunk_mr.end() < used.end()) {278// This is not the last chunk in the used region.279// What is our last block? We check the first block of280// the next (right) chunk rather than strictly check our last block281// because it's potentially more efficient to do so.282HeapWord* const last_block = sp->block_start(chunk_mr.end());283assert(last_block <= chunk_mr.end(), "In case this property changes.");284if ((last_block == chunk_mr.end()) // our last block does not straddle boundary285|| !sp->block_is_obj(last_block) // last_block isn't an object286|| oop(last_block)->is_objArray() // last_block is an array (precisely marked)287|| oop(last_block)->is_typeArray()) {288max_to_do = chunk_mr.end();289NOISY(tty->print_cr(" process_chunk_boundary: Last block on this card is not a non-array object;\n"290" max_to_do left at " PTR_FORMAT, max_to_do);)291} else {292assert(last_block < chunk_mr.end(), "Tautology");293// It is a non-array object that straddles the right boundary of this chunk.294// last_obj_card is the card corresponding to the start of the last object295// in the chunk. Note that the last object may not start in296// the chunk.297jbyte* const last_obj_card = byte_for(last_block);298const jbyte val = *last_obj_card;299if (!card_will_be_scanned(val)) {300assert(!card_may_have_been_dirty(val), "Error");301// The card containing the head is not dirty. Any marks on302// subsequent cards still in this chunk must have been made303// precisely; we can cap processing at the end of our chunk.304max_to_do = chunk_mr.end();305NOISY(tty->print_cr(" process_chunk_boundary: Head of last object on this card is not dirty;\n"306" max_to_do left at " PTR_FORMAT,307max_to_do);)308} else {309// The last object must be considered dirty, and extends onto the310// following chunk. Look for a dirty card in that chunk that will311// bound our processing.312jbyte* limit_card = NULL;313const size_t last_block_size = sp->block_size(last_block);314jbyte* const last_card_of_last_obj =315byte_for(last_block + last_block_size - 1);316jbyte* const first_card_of_next_chunk = byte_for(chunk_mr.end());317// This search potentially goes a long distance looking318// for the next card that will be scanned, terminating319// at the end of the last_block, if no earlier dirty card320// is found.321assert(byte_for(chunk_mr.end()) - byte_for(chunk_mr.start()) == ParGCCardsPerStrideChunk,322"last card of next chunk may be wrong");323for (jbyte* cur = first_card_of_next_chunk;324cur <= last_card_of_last_obj; cur++) {325const jbyte val = *cur;326if (card_will_be_scanned(val)) {327NOISY(tty->print_cr(" Found a non-clean card " PTR_FORMAT " with value 0x%x",328cur, (int)val);)329limit_card = cur; break;330} else {331assert(!card_may_have_been_dirty(val), "Error: card can't be skipped");332}333}334if (limit_card != NULL) {335max_to_do = addr_for(limit_card);336assert(limit_card != NULL && max_to_do != NULL, "Error");337NOISY(tty->print_cr(" process_chunk_boundary: Found a dirty card at " PTR_FORMAT338" max_to_do set at " PTR_FORMAT " which is before end of last block in chunk: "339PTR_FORMAT " + " PTR_FORMAT " = " PTR_FORMAT,340limit_card, max_to_do, last_block, last_block_size, (last_block+last_block_size));)341} else {342// The following is a pessimistic value, because it's possible343// that a dirty card on a subsequent chunk has been cleared by344// the time we get to look at it; we'll correct for that further below,345// using the LNC array which records the least non-clean card346// before cards were cleared in a particular chunk.347limit_card = last_card_of_last_obj;348max_to_do = last_block + last_block_size;349assert(limit_card != NULL && max_to_do != NULL, "Error");350NOISY(tty->print_cr(" process_chunk_boundary: Found no dirty card before end of last block in chunk\n"351" Setting limit_card to " PTR_FORMAT352" and max_to_do " PTR_FORMAT " + " PTR_FORMAT " = " PTR_FORMAT,353limit_card, last_block, last_block_size, max_to_do);)354}355assert(0 < cur_chunk_index+1 && cur_chunk_index+1 < lowest_non_clean_chunk_size,356"Bounds error.");357// It is possible that a dirty card for the last object may have been358// cleared before we had a chance to examine it. In that case, the value359// will have been logged in the LNC for that chunk.360// We need to examine as many chunks to the right as this object361// covers. However, we need to bound this checking to the largest362// entry in the LNC array: this is because the heap may expand363// after the LNC array has been created but before we reach this point,364// and the last block in our chunk may have been expanded to include365// the expansion delta (and possibly subsequently allocated from, so366// it wouldn't be sufficient to check whether that last block was367// or was not an object at this point).368uintptr_t last_chunk_index_to_check = addr_to_chunk_index(last_block + last_block_size - 1)369- lowest_non_clean_base_chunk_index;370const uintptr_t last_chunk_index = addr_to_chunk_index(used.last())371- lowest_non_clean_base_chunk_index;372if (last_chunk_index_to_check > last_chunk_index) {373assert(last_block + last_block_size > used.end(),374err_msg("Inconsistency detected: last_block [" PTR_FORMAT "," PTR_FORMAT "]"375" does not exceed used.end() = " PTR_FORMAT ","376" yet last_chunk_index_to_check " INTPTR_FORMAT377" exceeds last_chunk_index " INTPTR_FORMAT,378last_block, last_block + last_block_size,379used.end(),380last_chunk_index_to_check, last_chunk_index));381assert(sp->used_region().end() > used.end(),382err_msg("Expansion did not happen: "383"[" PTR_FORMAT "," PTR_FORMAT ") -> [" PTR_FORMAT "," PTR_FORMAT ")",384sp->used_region().start(), sp->used_region().end(), used.start(), used.end()));385NOISY(tty->print_cr(" process_chunk_boundary: heap expanded; explicitly bounding last_chunk");)386last_chunk_index_to_check = last_chunk_index;387}388for (uintptr_t lnc_index = cur_chunk_index + 1;389lnc_index <= last_chunk_index_to_check;390lnc_index++) {391jbyte* lnc_card = lowest_non_clean[lnc_index];392if (lnc_card != NULL) {393// we can stop at the first non-NULL entry we find394if (lnc_card <= limit_card) {395NOISY(tty->print_cr(" process_chunk_boundary: LNC card " PTR_FORMAT " is lower than limit_card " PTR_FORMAT,396" max_to_do will be lowered to " PTR_FORMAT " from " PTR_FORMAT,397lnc_card, limit_card, addr_for(lnc_card), max_to_do);)398limit_card = lnc_card;399max_to_do = addr_for(limit_card);400assert(limit_card != NULL && max_to_do != NULL, "Error");401}402// In any case, we break now403break;404} // else continue to look for a non-NULL entry if any405}406assert(limit_card != NULL && max_to_do != NULL, "Error");407}408assert(max_to_do != NULL, "OOPS 1 !");409}410assert(max_to_do != NULL, "OOPS 2!");411} else {412max_to_do = used.end();413NOISY(tty->print_cr(" process_chunk_boundary: Last chunk of this space;\n"414" max_to_do left at " PTR_FORMAT,415max_to_do);)416}417assert(max_to_do != NULL, "OOPS 3!");418// Now we can set the closure we're using so it doesn't to beyond419// max_to_do.420dcto_cl->set_min_done(max_to_do);421#ifndef PRODUCT422dcto_cl->set_last_bottom(max_to_do);423#endif424NOISY(tty->print_cr("===========================================================================\n");)425}426427#undef NOISY428429void430CardTableModRefBS::431get_LNC_array_for_space(Space* sp,432jbyte**& lowest_non_clean,433uintptr_t& lowest_non_clean_base_chunk_index,434size_t& lowest_non_clean_chunk_size) {435436int i = find_covering_region_containing(sp->bottom());437MemRegion covered = _covered[i];438size_t n_chunks = chunks_to_cover(covered);439440// Only the first thread to obtain the lock will resize the441// LNC array for the covered region. Any later expansion can't affect442// the used_at_save_marks region.443// (I observed a bug in which the first thread to execute this would444// resize, and then it would cause "expand_and_allocate" that would445// increase the number of chunks in the covered region. Then a second446// thread would come and execute this, see that the size didn't match,447// and free and allocate again. So the first thread would be using a448// freed "_lowest_non_clean" array.)449450// Do a dirty read here. If we pass the conditional then take the rare451// event lock and do the read again in case some other thread had already452// succeeded and done the resize.453int cur_collection = Universe::heap()->total_collections();454// Updated _last_LNC_resizing_collection[i] must not be visible before455// _lowest_non_clean and friends are visible. Therefore use acquire/release456// to guarantee this on non TSO architecures.457if (OrderAccess::load_acquire(&_last_LNC_resizing_collection[i]) != cur_collection) {458MutexLocker x(ParGCRareEvent_lock);459// This load_acquire is here for clarity only. The MutexLocker already fences.460if (OrderAccess::load_acquire(&_last_LNC_resizing_collection[i]) != cur_collection) {461if (_lowest_non_clean[i] == NULL ||462n_chunks != _lowest_non_clean_chunk_size[i]) {463464// Should we delete the old?465if (_lowest_non_clean[i] != NULL) {466assert(n_chunks != _lowest_non_clean_chunk_size[i],467"logical consequence");468FREE_C_HEAP_ARRAY(CardPtr, _lowest_non_clean[i], mtGC);469_lowest_non_clean[i] = NULL;470}471// Now allocate a new one if necessary.472if (_lowest_non_clean[i] == NULL) {473_lowest_non_clean[i] = NEW_C_HEAP_ARRAY(CardPtr, n_chunks, mtGC);474_lowest_non_clean_chunk_size[i] = n_chunks;475_lowest_non_clean_base_chunk_index[i] = addr_to_chunk_index(covered.start());476for (int j = 0; j < (int)n_chunks; j++)477_lowest_non_clean[i][j] = NULL;478}479}480// Make sure this gets visible only after _lowest_non_clean* was initialized481OrderAccess::release_store(&_last_LNC_resizing_collection[i], cur_collection);482}483}484// In any case, now do the initialization.485lowest_non_clean = _lowest_non_clean[i];486lowest_non_clean_base_chunk_index = _lowest_non_clean_base_chunk_index[i];487lowest_non_clean_chunk_size = _lowest_non_clean_chunk_size[i];488}489490491