/* * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
// Collects information about the overall heap root scan progress during an evacuation. // // Scanning the remembered sets works by first merging all sources of cards to be // scanned (log buffers, hcc, remembered sets) into a single data structure to remove // duplicates and simplify work distribution. // // During the following card scanning we not only scan this combined set of cards, but // also remember that these were completely scanned. The following evacuation passes // do not scan these cards again, and so need to be preserved across increments. // // The representation for all the cards to scan is the card table: cards can have // one of three states during GC: // - clean: these cards will not be scanned in this pass // - dirty: these cards will be scanned in this pass // - scanned: these cards have already been scanned in a previous pass // // After all evacuation is done, we reset the card table to clean. // // Work distribution occurs on "chunk" basis, i.e. contiguous ranges of cards. As an // additional optimization, during card merging we remember which regions and which // chunks actually contain cards to be scanned. Threads iterate only across these // regions, and only compete for chunks containing any cards. // // Within these chunks, a worker scans the card table on "blocks" of cards, i.e. // contiguous ranges of dirty cards to be scanned. These blocks are converted to actual // memory ranges and then passed on to actual scanning. class G1RemSetScanState : public CHeapObj<mtGC> { class G1DirtyRegions;
size_t _max_reserved_regions;
// Has this region that is part of the regions in the collection set been processed yet. typedefbool G1RemsetIterState;
// Card table iteration claim for each heap region, from 0 (completely unscanned) // to (>=) HeapRegion::CardsPerRegion (completely scanned).
uint volatile* _card_table_scan_state;
uint _scan_chunks_per_region; // Number of chunks per region.
uint8_t _log_scan_chunks_per_region; // Log of number of chunks per region. bool* _region_scan_chunks;
size_t _num_total_scan_chunks; // Total number of elements in _region_scan_chunks.
uint8_t _scan_chunks_shift; // For conversion between card index and chunk index. public:
uint scan_chunk_size_in_cards() const { return (uint)1 << _scan_chunks_shift; }
// Returns whether the chunk corresponding to the given region/card in region contain a // dirty card, i.e. actually needs scanning. bool chunk_needs_scan(uint const region_idx, uint const card_in_region) const {
size_t const idx = ((size_t)region_idx << _log_scan_chunks_per_region) + (card_in_region >> _scan_chunks_shift);
assert(idx < _num_total_scan_chunks, "Index " SIZE_FORMAT " out of bounds " SIZE_FORMAT,
idx, _num_total_scan_chunks); return _region_scan_chunks[idx];
}
private: // The complete set of regions which card table needs to be cleared at the end // of GC because we scribbled over these card tables. // // Regions may be added for two reasons: // - they were part of the collection set: they may contain g1_young_card_val // or regular card marks that we never scan so we must always clear their card // table // - or in case g1 does an optional evacuation pass, g1 marks the cards in there // as g1_scanned_card_val. If G1 only did an initial evacuation pass, the // scanning already cleared these cards. In that case they are not in this set // at the end of the collection.
G1DirtyRegions* _all_dirty_regions; // The set of regions which card table needs to be scanned for new dirty cards // in the current evacuation pass.
G1DirtyRegions* _next_dirty_regions;
// Set of (unique) regions that can be added to concurrently. class G1DirtyRegions : public CHeapObj<mtGC> {
uint* _buffer;
uint _cur_idx;
size_t _max_reserved_regions;
// Creates the union of this and the other G1DirtyRegions. void merge(const G1DirtyRegions* other) { for (uint i = 0; i < other->size(); i++) {
uint region = other->at(i); if (!_contains[region]) {
_buffer[_cur_idx++] = region;
_contains[region] = true;
}
}
}
};
// For each region, contains the maximum top() value to be used during this garbage // collection. Subsumes common checks like filtering out everything but old and // humongous regions outside the collection set. // This is valid because we are not interested in scanning stray remembered set // entries from free or archive regions.
HeapWord** _scan_top;
class G1ClearCardTableTask : public G1AbstractSubTask {
G1CollectedHeap* _g1h;
G1DirtyRegions* _regions;
uint _chunk_length;
if (num_regions == 0) { // There is no card table clean work, only some cleanup of memory. return AlmostNoWork;
} return ((double)align_up((size_t)num_regions << HeapRegion::LogCardsPerRegion, chunk_size()) / chunk_size());
}
void prepare() { // Reset the claim and clear scan top for all regions, including // regions currently not available or free. Since regions might // become used during the collection these values must be valid // for those regions as well. for (size_t i = 0; i < _max_reserved_regions; i++) {
reset_region_claim((uint)i);
clear_scan_top((uint)i);
}
_all_dirty_regions = new G1DirtyRegions(_max_reserved_regions);
_next_dirty_regions = new G1DirtyRegions(_max_reserved_regions);
}
void prepare_for_merge_heap_roots() {
assert(_next_dirty_regions->size() == 0, "next dirty regions must be empty");
for (size_t i = 0; i < _max_reserved_regions; i++) {
_card_table_scan_state[i] = 0;
}
// Returns whether the given region contains cards we need to scan. The remembered // set and other sources may contain cards that // - are in uncommitted regions // - are located in the collection set // - are located in free regions // as we do not clean up remembered sets before merging heap roots. bool contains_cards_to_process(uint const region_idx) const {
HeapRegion* hr = G1CollectedHeap::heap()->region_at_or_null(region_idx); return (hr != NULL && !hr->in_collection_set() && hr->is_old_or_humongous_or_archive());
}
size_t num_visited_cards() const {
size_t result = 0; for (uint i = 0; i < _num_total_scan_chunks; i++) { if (_region_scan_chunks[i]) {
result++;
}
} return result * (HeapRegion::CardsPerRegion / _scan_chunks_per_region);
}
void set_chunk_range_dirty(size_t const region_card_idx, size_t const card_length) {
size_t chunk_idx = region_card_idx >> _scan_chunks_shift; // Make sure that all chunks that contain the range are marked. Calculate the // chunk of the last card that is actually marked.
size_t const end_chunk = (region_card_idx + card_length - 1) >> _scan_chunks_shift; for (; chunk_idx <= end_chunk; chunk_idx++) {
_region_scan_chunks[chunk_idx] = true;
}
}
void set_chunk_dirty(size_t const card_idx) {
assert((card_idx >> _scan_chunks_shift) < _num_total_scan_chunks, "Trying to access index " SIZE_FORMAT " out of bounds " SIZE_FORMAT,
card_idx >> _scan_chunks_shift, _num_total_scan_chunks);
size_t const chunk_idx = card_idx >> _scan_chunks_shift;
_region_scan_chunks[chunk_idx] = true;
}
do { bool result = cl->do_heap_region(g1h->region_at(_next_dirty_regions->at(cur)));
guarantee(!result, "Not allowed to ask for early termination.");
cur++; if (cur == _next_dirty_regions->size()) {
cur = 0;
}
} while (cur != start_pos);
}
// Attempt to claim the given region in the collection set for iteration. Returns true // if this call caused the transition from Unclaimed to Claimed. inlinebool claim_collection_set_region(uint region) {
assert(region < _max_reserved_regions, "Tried to access invalid region %u", region); if (_collection_set_iter_state[region]) { returnfalse;
} return !Atomic::cmpxchg(&_collection_set_iter_state[region], false, true);
}
bool has_cards_to_scan(uint region) {
assert(region < _max_reserved_regions, "Tried to access invalid region %u", region); return _card_table_scan_state[region] < HeapRegion::CardsPerRegion;
}
uint claim_cards_to_scan(uint region, uint increment) {
assert(region < _max_reserved_regions, "Tried to access invalid region %u", region); return Atomic::fetch_and_add(&_card_table_scan_state[region], increment);
}
void add_dirty_region(uint const region) { #ifdef ASSERT
HeapRegion* hr = G1CollectedHeap::heap()->region_at(region);
assert(!hr->in_collection_set() && hr->is_old_or_humongous_or_archive(), "Region %u is not suitable for scanning, is %sin collection set or %s",
hr->hrm_index(), hr->in_collection_set() ? "" : "not ", hr->get_short_type_str()); #endif
_next_dirty_regions->add_dirty_region(region);
}
void add_all_dirty_region(uint region) { #ifdef ASSERT
HeapRegion* hr = G1CollectedHeap::heap()->region_at(region);
assert(hr->in_collection_set(), "Only add collection set regions to all dirty regions directly but %u is %s",
hr->hrm_index(), hr->get_short_type_str()); #endif
_all_dirty_regions->add_dirty_region(region);
}
// Helper class to scan and detect ranges of cards that need to be scanned on the // card table. class G1CardTableScanner : public StackObj { public: typedef CardTable::CardValue CardValue;
size_t find_next_dirty() { while (!cur_addr_aligned()) { if (cur_card_is_dirty()) { return get_and_advance_pos();
}
_cur_addr++;
}
assert(cur_addr_aligned(), "Current address should be aligned now."); while (_cur_addr != _end_addr) { if (cur_word_of_cards_contains_any_dirty_card()) { for (size_t i = 0; i < sizeof(size_t); i++) { if (cur_card_is_dirty()) { return get_and_advance_pos();
}
_cur_addr++;
}
assert(false, "Should not reach here given we detected a dirty card in the word.");
}
_cur_addr += sizeof(size_t);
} return get_and_advance_pos();
}
size_t find_next_non_dirty() {
assert(_cur_addr <= _end_addr, "Not allowed to search for marks after area.");
while (!cur_addr_aligned()) { if (!cur_card_is_dirty()) { return get_and_advance_pos();
}
_cur_addr++;
}
assert(cur_addr_aligned(), "Current address should be aligned now."); while (_cur_addr != _end_addr) { if (!cur_word_of_cards_all_dirty_cards()) { for (size_t i = 0; i < sizeof(size_t); i++) { if (!cur_card_is_dirty()) { return get_and_advance_pos();
}
_cur_addr++;
}
assert(false, "Should not reach here given we detected a non-dirty card in the word.");
}
_cur_addr += sizeof(size_t);
} return get_and_advance_pos();
}
};
// Helper class to claim dirty chunks within the card table. class G1CardTableChunkClaimer {
G1RemSetScanState* _scan_state;
uint _region_idx;
uint _cur_claim;
public:
G1CardTableChunkClaimer(G1RemSetScanState* scan_state, uint region_idx) :
_scan_state(scan_state),
_region_idx(region_idx),
_cur_claim(0) {
guarantee(size() <= HeapRegion::CardsPerRegion, "Should not claim more space than possible.");
}
bool has_next() { while (true) {
_cur_claim = _scan_state->claim_cards_to_scan(_region_idx, size()); if (_cur_claim >= HeapRegion::CardsPerRegion) { returnfalse;
} if (_scan_state->chunk_needs_scan(_region_idx, _cur_claim)) { returntrue;
}
}
}
// Scans a heap region for dirty cards. class G1ScanHRForRegionClosure : public HeapRegionClosure {
G1CollectedHeap* _g1h;
G1CardTable* _ct;
G1BlockOffsetTable* _bot;
// The address to which this thread already scanned (walked the heap) up to during // card scanning (exclusive).
HeapWord* _scanned_to;
G1CardTable::CardValue _scanned_card_value;
HeapWord* const scanned_to = card_region->oops_on_memregion_seq_iterate_careful<true>(mr, &card_cl);
assert(scanned_to != NULL, "Should be able to scan range");
assert(scanned_to >= mr.end(), "Scanned to " PTR_FORMAT " less than range " PTR_FORMAT, p2i(scanned_to), p2i(mr.end()));
// Set the current scan "finger" to NULL for every heap region to scan. Since // the claim value is monotonically increasing, the check to not scan below this // will filter out objects spanning chunks within the region too then, as opposed // to resetting this value for every claim.
_scanned_to = NULL;
bool do_heap_region(HeapRegion* r) {
assert(!r->in_collection_set() && r->is_old_or_humongous_or_archive(), "Should only be called on old gen non-collection set regions but region %u is not.",
r->hrm_index());
uint const region_idx = r->hrm_index();
// Heap region closure to be applied to all regions in the current collection set // increment to fix up non-card related roots. class G1ScanCollectionSetRegionClosure : public HeapRegionClosure {
G1ParScanThreadState* _pss;
G1RemSetScanState* _scan_state;
// The individual references for the optional remembered set are per-worker, so we // always need to scan them. if (r->has_index_in_opt_cset()) {
EventGCPhaseParallel event;
G1EvacPhaseWithTrimTimeTracker timer(_pss, _rem_set_opt_root_scan_time, _rem_set_opt_trim_partially_time);
scan_opt_rem_set_roots(r);
if (_scan_state->claim_collection_set_region(region_idx)) {
EventGCPhaseParallel event;
G1EvacPhaseWithTrimTimeTracker timer(_pss, _code_root_scan_time, _code_trim_partially_time); // Scan the code root list attached to the current region
r->code_roots_do(_pss->closures()->weak_codeblobs());
// At this time we record some metrics only for the evacuations after the initial one. if (scan_phase == G1GCPhaseTimes::OptScanHR) {
p->record_or_add_thread_work_item(scan_phase, worker_id, cl.opt_roots_scanned(), G1GCPhaseTimes::ScanHRFoundRoots);
p->record_or_add_thread_work_item(scan_phase, worker_id, cl.opt_refs_scanned(), G1GCPhaseTimes::ScanHRScannedOptRefs);
p->record_or_add_thread_work_item(scan_phase, worker_id, cl.opt_refs_memory_used(), G1GCPhaseTimes::ScanHRUsedMemory);
}
}
#ifdef ASSERT void G1RemSet::assert_scan_top_is_null(uint hrm_index) {
assert(_scan_state->scan_top(hrm_index) == NULL, "scan_top of region %u is unexpectedly " PTR_FORMAT,
hrm_index, p2i(_scan_state->scan_top(hrm_index)));
} #endif
// Only update non-collection set old regions, others must have already been set // to NULL (don't scan) in the initialization. if (r->in_collection_set()) {
assert_scan_top_is_null(hrm_index);
} elseif (r->is_old_or_humongous_or_archive()) {
_scan_state->set_scan_top(hrm_index, r->top());
} else {
assert_scan_top_is_null(hrm_index);
assert(r->is_free(), "Region %u should be free region but is %s", hrm_index, r->get_type_str());
}
}
// Small ring buffer used for prefetching cards for write from the card // table during GC. template <class T> class G1MergeHeapRootsPrefetchCache { public: staticconst uint CacheSize = G1MergeHeapRootsPrefetchCacheSize;
static_assert(is_power_of_2(CacheSize), "Cache size must be power of 2");
private:
T* _cache[CacheSize];
uint _cur_cache_idx;
NONCOPYABLE(G1MergeHeapRootsPrefetchCache);
protected: // Initial content of all elements in the cache. It's value should be // "neutral", i.e. no work done on it when processing it.
G1CardTable::CardValue _dummy_card;
// Visitor for remembered sets. Several methods of it are called by a region's // card set iterator to drop card set remembered set entries onto the card. // table. This is in addition to being the HeapRegionClosure to iterate over // all region's remembered sets. // // We add a small prefetching cache in front of the actual work as dropping // onto the card table is basically random memory access. This improves // performance of this operation significantly. class G1MergeCardSetClosure : public HeapRegionClosure { friendclass G1MergeCardSetCache;
G1RemSetScanState* _scan_state;
G1CardTable* _ct;
G1MergeCardSetStats _stats;
// Cached card table index of the currently processed region to avoid constant // recalculation as our remembered set containers are per region.
size_t _region_base_idx;
class G1MergeCardSetCache : public G1MergeHeapRootsPrefetchCache<G1CardTable::CardValue> {
G1MergeCardSetClosure* const _merge_card_cl;
public:
G1MergeCardSetCache(G1MergeCardSetClosure* const merge_card_cl) : // Initially set dummy card value to Dirty to avoid any actual mark work if we // try to process it.
G1MergeHeapRootsPrefetchCache<G1CardTable::CardValue>(G1CardTable::dirty_card_val()),
_merge_card_cl(merge_card_cl) { }
void flush() { for (uint i = 0; i < CacheSize; i++) {
_merge_card_cl->mark_card(push(&_dummy_card));
}
}
} _merge_card_set_cache;
// Returns whether the region contains cards we need to scan. If so, remember that // region in the current set of dirty regions. bool remember_if_interesting(uint const region_idx) { if (!_scan_state->contains_cards_to_process(region_idx)) { returnfalse;
}
_scan_state->add_dirty_region(region_idx); returntrue;
}
// Helper to merge the cards in the card set for the given region onto the card // table. // // Called directly for humongous starts regions because we should not add // humongous eager reclaim candidates to the "all" list of regions to // clear the card table by default as we do not know yet whether this region // will be reclaimed (and reused). // If the humongous region contains dirty cards, g1 will scan them // because dumping the remembered set entries onto the card table will add // the humongous region to the "dirty" region list to scan. Then scanning // either clears the card during scan (if there is only an initial evacuation // pass) or the "dirty" list will be merged with the "all" list later otherwise. // (And there is no problem either way if the region does not contain dirty // cards). void merge_card_set_for_region(HeapRegion* r) {
assert(r->in_collection_set() || r->is_starts_humongous(), "must be");
HeapRegionRemSet* rem_set = r->rem_set(); if (!rem_set->is_empty()) {
rem_set->iterate_for_merge(*this);
}
}
// Closure to make sure that the marking bitmap is clear for any old region in // the collection set. // This is needed to be able to use the bitmap for evacuation failure handling. class G1ClearBitmapClosure : public HeapRegionClosure {
G1CollectedHeap* _g1h;
void assert_bitmap_clear(HeapRegion* hr, const G1CMBitMap* bitmap) {
assert(bitmap->get_next_marked_addr(hr->bottom(), hr->end()) == hr->end(), "Bitmap should have no mark for region %u (%s)", hr->hrm_index(), hr->get_short_type_str());
}
bool should_clear_region(HeapRegion* hr) const { // The bitmap for young regions must obviously be clear as we never mark through them; // old regions are only in the collection set after the concurrent cycle completed, // so their bitmaps must also be clear except when the pause occurs during the // Concurrent Cleanup for Next Mark phase. Only at that point the region's bitmap may // contain marks while being in the collection set at the same time. // // There is one exception: shutdown might have aborted the Concurrent Cleanup for Next // Mark phase midway, which might have also left stale marks in old generation regions. // There might actually have been scheduled multiple collections, but at that point we do // not care that much about performance and just do the work multiple times if needed. return (_g1h->collector_state()->clearing_bitmap() ||
_g1h->concurrent_mark_is_terminating()) &&
hr->is_old();
}
bool do_heap_region(HeapRegion* hr) {
assert(_g1h->is_in_cset(hr), "Should only be used iterating the collection set");
// Evacuation failure uses the bitmap to record evacuation failed objects, // so the bitmap for the regions in the collection set must be cleared if not already. if (should_clear_region(hr)) {
_g1h->clear_bitmap_for_region(hr);
hr->reset_top_at_mark_start();
} else {
assert_bitmap_clear(hr, _g1h->concurrent_mark()->mark_bitmap());
} returnfalse;
}
};
// Helper to allow two closure to be applied when // iterating through the collection set. class G1CombinedClosure : public HeapRegionClosure {
HeapRegionClosure* _closure1;
HeapRegionClosure* _closure2; public:
G1CombinedClosure(HeapRegionClosure* cl1, HeapRegionClosure* cl2) :
_closure1(cl1),
_closure2(cl2) { }
// Visitor for the remembered sets of humongous candidate regions to merge their // remembered set into the card table. class G1FlushHumongousCandidateRemSets : public HeapRegionIndexClosure {
G1MergeCardSetClosure _cl;
if (!g1h->region_attr(region_index).is_humongous_candidate()) { returnfalse;
}
HeapRegion* r = g1h->region_at(region_index); if (r->rem_set()->is_empty()) { returnfalse;
}
guarantee(r->rem_set()->occupancy_less_or_equal_than(G1EagerReclaimRemSetThreshold), "Found a not-small remembered set here. This is inconsistent with previous assumptions.");
_cl.merge_card_set_for_region(r);
// We should only clear the card based remembered set here as we will not // implicitly rebuild anything else during eager reclaim. Note that at the moment // (and probably never) we do not enter this path if there are other kind of // remembered sets for this region.
r->rem_set()->clear_locked(true/* only_cardset */); // Clear_locked() above sets the state to Empty. However we want to continue // collecting remembered set entries for humongous regions that were not // reclaimed.
r->rem_set()->set_state_complete(); #ifdef ASSERT
G1HeapRegionAttr region_attr = g1h->region_attr(region_index);
assert(region_attr.remset_is_tracked(), "must be"); #endif
assert(r->rem_set()->is_empty(), "At this point any humongous candidate remembered set must be empty.");
void do_card_ptr(CardValue* card_ptr, uint worker_id) { // The only time we care about recording cards that // contain references that point into the collection set // is during RSet updating within an evacuation pause. // In this case worker_id should be the id of a GC worker thread.
assert(SafepointSynchronize::is_at_safepoint(), "not during an evacuation pause");
// The second clause must come after - the log buffers might contain cards to uncommitted // regions. // This code may count duplicate entries in the log buffers (even if rare) multiple // times. if (_scan_state->contains_cards_to_process(region_idx)) {
process_card(card_ptr);
} else { // We may have had dirty cards in the (initial) collection set (or the // young regions which are always in the initial collection set). We do // not fix their cards here: we already added these regions to the set of // regions to clear the card table at the end during the prepare() phase.
_cards_skipped++;
}
}
// Now apply the closure to all remaining log entries. if (_initial_evacuation) {
assert(merge_remset_phase == G1GCPhaseTimes::MergeRS, "Wrong merge phase");
G1GCParPhaseTimesTracker x(p, G1GCPhaseTimes::MergeLB, worker_id);
CardValue* card_ptr = *card_ptr_addr; // Find the start address represented by the card.
HeapWord* start = _ct->addr_for(card_ptr); // And find the region containing it.
HeapRegion* r = _g1h->heap_region_containing_or_null(start);
// If this is a (stale) card into an uncommitted region, exit. if (r == NULL) { returnfalse;
}
check_card_ptr(card_ptr, _ct);
// If the card is no longer dirty, nothing to do. // We cannot load the card value before the "r == NULL" check, because G1 // could uncommit parts of the card table covering uncommitted regions. if (*card_ptr != G1CardTable::dirty_card_val()) { returnfalse;
}
// This check is needed for some uncommon cases where we should // ignore the card. // // The region could be young. Cards for young regions are // distinctly marked (set to g1_young_gen), so the post-barrier will // filter them out. However, that marking is performed // concurrently. A write to a young object could occur before the // card has been marked young, slipping past the filter. // // The card could be stale, because the region has been freed since // the card was recorded. In this case the region type could be // anything. If (still) free or (reallocated) young, just ignore // it. If (reallocated) old or humongous, the later card trimming // and additional checks in iteration may detect staleness. At // worst, we end up processing a stale card unnecessarily. // // In the normal (non-stale) case, the synchronization between the // enqueueing of the card and processing it here will have ensured // we see the up-to-date region type here. if (!r->is_old_or_humongous_or_archive()) { returnfalse;
}
// The result from the hot card cache insert call is either: // * pointer to the current card // (implying that the current card is not 'hot'), // * null // (meaning we had inserted the card ptr into the "hot" card cache, // which had some headroom), // * a pointer to a "hot" card that was evicted from the "hot" cache. //
if (G1HotCardCache::use_cache()) { const CardValue* orig_card_ptr = card_ptr;
card_ptr = _hot_card_cache->insert(card_ptr); if (card_ptr == NULL) { // There was no eviction. Nothing to do. returnfalse;
} elseif (card_ptr != orig_card_ptr) { // Original card was inserted and an old card was evicted.
start = _ct->addr_for(card_ptr);
r = _g1h->heap_region_containing_or_null(start);
// Check whether the region formerly in the cache should be // ignored, as discussed earlier for the original card. The // region could have been freed (or even uncommitted) while // in the cache. if (r == nullptr || !r->is_old_or_humongous_or_archive()) { returnfalse;
}
*card_ptr_addr = card_ptr;
} // Else we still have the original card.
}
// Trim the region designated by the card to what's been allocated // in the region. The card could be stale, or the card could cover // (part of) an object at the end of the allocated space and extend // beyond the end of allocation.
// Non-humongous objects are either allocated in the old regions during GC, // or mapped in archive regions during startup. So if region is old or // archive then top is stable. // Humongous object allocation sets top last; if top has not yet been set, // this is a stale card and we'll end up with an empty intersection. // If this is not a stale card, the synchronization between the // enqueuing of the card and processing it here will have ensured // we see the up-to-date top here.
HeapWord* scan_limit = r->top();
if (scan_limit <= start) { // If the trimmed region is empty, the card must be stale. returnfalse;
}
// Okay to clean and process the card now. There are still some // stale card cases that may be detected by iteration and dealt with // as iteration failure.
*const_cast<volatile CardValue*>(card_ptr) = G1CardTable::clean_card_val();
// Construct the MemRegion representing the card.
HeapWord* start = _ct->addr_for(card_ptr); // And find the region containing it.
HeapRegion* r = _g1h->heap_region_containing(start); // This reload of the top is safe even though it happens after the full // fence, because top is stable for old, archive and unfiltered humongous // regions, so it must return the same value as the previous load when // cleaning the card. Also cleaning the card and refinement of the card // cannot span across safepoint, so we don't need to worry about top being // changed during safepoint.
HeapWord* scan_limit = r->top();
assert(scan_limit > start, "sanity");
// Don't use addr_for(card_ptr + 1) which can ask for // a card beyond the heap.
HeapWord* end = start + G1CardTable::card_size_in_words();
MemRegion dirty_region(start, MIN2(scan_limit, end));
assert(!dirty_region.is_empty(), "sanity");
// If unable to process the card then we encountered an unparsable // part of the heap (e.g. a partially allocated object, so only // temporarily a problem) while processing a stale card. Despite // the card being stale, we can't simply ignore it, because we've // already marked the card cleaned, so taken responsibility for // ensuring the card gets scanned. // // However, the card might have gotten re-dirtied and re-enqueued // while we worked. (In fact, it's pretty likely.) if (*card_ptr == G1CardTable::dirty_card_val()) { return;
}
enqueue_for_reprocessing(card_ptr);
}
// Re-dirty and re-enqueue the card to retry refinement later. // This is used to deal with a rare race condition in concurrent refinement. void G1RemSet::enqueue_for_reprocessing(CardValue* card_ptr) { // We can't use the thread-local queue, because that might be the queue // that is being processed by us; we could be a Java thread conscripted to // perform refinement on our queue's current buffer. This situation only // arises from rare race condition, so it's not worth any significant // development effort or clever lock-free queue implementation. Instead // we use brute force, allocating and enqueuing an entire buffer for just // this card. Since buffers are processed in FIFO order and we try to // keep some in the queue, it is likely that the racing state will have // resolved by the time this card comes up for reprocessing.
*card_ptr = G1CardTable::dirty_card_val();
G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set(); void** buffer = dcqs.allocate_buffer();
size_t index = dcqs.buffer_size() - 1;
buffer[index] = card_ptr;
dcqs.enqueue_completed_buffer(BufferNode::make_node_from_buffer(buffer, index));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.