Path: blob/master/src/hotspot/share/gc/shared/cardTableRS.cpp
40957 views
/*1* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/classLoaderDataGraph.hpp"26#include "gc/shared/cardTableRS.hpp"27#include "gc/shared/genCollectedHeap.hpp"28#include "gc/shared/genOopClosures.hpp"29#include "gc/shared/generation.hpp"30#include "gc/shared/space.inline.hpp"31#include "memory/allocation.inline.hpp"32#include "memory/iterator.inline.hpp"33#include "oops/access.inline.hpp"34#include "oops/oop.inline.hpp"35#include "runtime/atomic.hpp"36#include "runtime/java.hpp"37#include "runtime/os.hpp"38#include "utilities/macros.hpp"3940inline bool ClearNoncleanCardWrapper::clear_card(CardValue* entry) {41assert(*entry == CardTableRS::dirty_card_val(), "Only look at dirty cards.");42*entry = CardTableRS::clean_card_val();43return true;44}4546ClearNoncleanCardWrapper::ClearNoncleanCardWrapper(47DirtyCardToOopClosure* dirty_card_closure, CardTableRS* ct) :48_dirty_card_closure(dirty_card_closure), _ct(ct) {49}5051bool ClearNoncleanCardWrapper::is_word_aligned(CardTable::CardValue* entry) {52return (((intptr_t)entry) & (BytesPerWord-1)) == 0;53}5455// The regions are visited in *decreasing* address order.56// This order aids with imprecise card marking, where a dirty57// card may cause scanning, and summarization marking, of objects58// that extend onto subsequent cards.59void ClearNoncleanCardWrapper::do_MemRegion(MemRegion mr) {60assert(mr.word_size() > 0, "Error");61assert(_ct->is_aligned(mr.start()), "mr.start() should be card aligned");62// mr.end() may not necessarily be card aligned.63CardValue* cur_entry = _ct->byte_for(mr.last());64const CardValue* limit = _ct->byte_for(mr.start());65HeapWord* end_of_non_clean = mr.end();66HeapWord* start_of_non_clean = end_of_non_clean;67while (cur_entry >= limit) {68HeapWord* cur_hw = _ct->addr_for(cur_entry);69if ((*cur_entry != CardTableRS::clean_card_val()) && clear_card(cur_entry)) {70// Continue the dirty range by opening the71// dirty window one card to the left.72start_of_non_clean = cur_hw;73} else {74// We hit a "clean" card; process any non-empty75// "dirty" range accumulated so far.76if (start_of_non_clean < end_of_non_clean) {77const MemRegion mrd(start_of_non_clean, end_of_non_clean);78_dirty_card_closure->do_MemRegion(mrd);79}8081// fast forward through potential continuous whole-word range of clean cards beginning at a word-boundary82if (is_word_aligned(cur_entry)) {83CardValue* cur_row = cur_entry - BytesPerWord;84while (cur_row >= limit && *((intptr_t*)cur_row) == CardTableRS::clean_card_row_val()) {85cur_row -= BytesPerWord;86}87cur_entry = cur_row + BytesPerWord;88cur_hw = _ct->addr_for(cur_entry);89}9091// Reset the dirty window, while continuing to look92// for the next dirty card that will start a93// new dirty window.94end_of_non_clean = cur_hw;95start_of_non_clean = cur_hw;96}97// Note that "cur_entry" leads "start_of_non_clean" in98// its leftward excursion after this point99// in the loop and, when we hit the left end of "mr",100// will point off of the left end of the card-table101// for "mr".102cur_entry--;103}104// If the first card of "mr" was dirty, we will have105// been left with a dirty window, co-initial with "mr",106// which we now process.107if (start_of_non_clean < end_of_non_clean) {108const MemRegion mrd(start_of_non_clean, end_of_non_clean);109_dirty_card_closure->do_MemRegion(mrd);110}111}112113void CardTableRS::younger_refs_in_space_iterate(Space* sp,114HeapWord* gen_boundary,115OopIterateClosure* cl) {116verify_used_region_at_save_marks(sp);117118const MemRegion urasm = sp->used_region_at_save_marks();119non_clean_card_iterate(sp, gen_boundary, urasm, cl, this);120}121122#ifdef ASSERT123void CardTableRS::verify_used_region_at_save_marks(Space* sp) const {124MemRegion ur = sp->used_region();125MemRegion urasm = sp->used_region_at_save_marks();126127assert(ur.contains(urasm),128"Did you forget to call save_marks()? "129"[" PTR_FORMAT ", " PTR_FORMAT ") is not contained in "130"[" PTR_FORMAT ", " PTR_FORMAT ")",131p2i(urasm.start()), p2i(urasm.end()), p2i(ur.start()), p2i(ur.end()));132}133#endif134135void CardTableRS::clear_into_younger(Generation* old_gen) {136assert(GenCollectedHeap::heap()->is_old_gen(old_gen),137"Should only be called for the old generation");138// The card tables for the youngest gen need never be cleared.139// There's a bit of subtlety in the clear() and invalidate()140// methods that we exploit here and in invalidate_or_clear()141// below to avoid missing cards at the fringes. If clear() or142// invalidate() are changed in the future, this code should143// be revisited. 20040107.ysr144clear(old_gen->prev_used_region());145}146147void CardTableRS::invalidate_or_clear(Generation* old_gen) {148assert(GenCollectedHeap::heap()->is_old_gen(old_gen),149"Should only be called for the old generation");150// Invalidate the cards for the currently occupied part of151// the old generation and clear the cards for the152// unoccupied part of the generation (if any, making use153// of that generation's prev_used_region to determine that154// region). No need to do anything for the youngest155// generation. Also see note#20040107.ysr above.156MemRegion used_mr = old_gen->used_region();157MemRegion to_be_cleared_mr = old_gen->prev_used_region().minus(used_mr);158if (!to_be_cleared_mr.is_empty()) {159clear(to_be_cleared_mr);160}161invalidate(used_mr);162}163164165class VerifyCleanCardClosure: public BasicOopIterateClosure {166private:167HeapWord* _boundary;168HeapWord* _begin;169HeapWord* _end;170protected:171template <class T> void do_oop_work(T* p) {172HeapWord* jp = (HeapWord*)p;173assert(jp >= _begin && jp < _end,174"Error: jp " PTR_FORMAT " should be within "175"[_begin, _end) = [" PTR_FORMAT "," PTR_FORMAT ")",176p2i(jp), p2i(_begin), p2i(_end));177oop obj = RawAccess<>::oop_load(p);178guarantee(obj == NULL || cast_from_oop<HeapWord*>(obj) >= _boundary,179"pointer " PTR_FORMAT " at " PTR_FORMAT " on "180"clean card crosses boundary" PTR_FORMAT,181p2i(obj), p2i(jp), p2i(_boundary));182}183184public:185VerifyCleanCardClosure(HeapWord* b, HeapWord* begin, HeapWord* end) :186_boundary(b), _begin(begin), _end(end) {187assert(b <= begin,188"Error: boundary " PTR_FORMAT " should be at or below begin " PTR_FORMAT,189p2i(b), p2i(begin));190assert(begin <= end,191"Error: begin " PTR_FORMAT " should be strictly below end " PTR_FORMAT,192p2i(begin), p2i(end));193}194195virtual void do_oop(oop* p) { VerifyCleanCardClosure::do_oop_work(p); }196virtual void do_oop(narrowOop* p) { VerifyCleanCardClosure::do_oop_work(p); }197};198199class VerifyCTSpaceClosure: public SpaceClosure {200private:201CardTableRS* _ct;202HeapWord* _boundary;203public:204VerifyCTSpaceClosure(CardTableRS* ct, HeapWord* boundary) :205_ct(ct), _boundary(boundary) {}206virtual void do_space(Space* s) { _ct->verify_space(s, _boundary); }207};208209class VerifyCTGenClosure: public GenCollectedHeap::GenClosure {210CardTableRS* _ct;211public:212VerifyCTGenClosure(CardTableRS* ct) : _ct(ct) {}213void do_generation(Generation* gen) {214// Skip the youngest generation.215if (GenCollectedHeap::heap()->is_young_gen(gen)) {216return;217}218// Normally, we're interested in pointers to younger generations.219VerifyCTSpaceClosure blk(_ct, gen->reserved().start());220gen->space_iterate(&blk, true);221}222};223224void CardTableRS::verify_space(Space* s, HeapWord* gen_boundary) {225// We don't need to do young-gen spaces.226if (s->end() <= gen_boundary) return;227MemRegion used = s->used_region();228229CardValue* cur_entry = byte_for(used.start());230CardValue* limit = byte_after(used.last());231while (cur_entry < limit) {232if (*cur_entry == clean_card_val()) {233CardValue* first_dirty = cur_entry+1;234while (first_dirty < limit &&235*first_dirty == clean_card_val()) {236first_dirty++;237}238// If the first object is a regular object, and it has a239// young-to-old field, that would mark the previous card.240HeapWord* boundary = addr_for(cur_entry);241HeapWord* end = (first_dirty >= limit) ? used.end() : addr_for(first_dirty);242HeapWord* boundary_block = s->block_start(boundary);243HeapWord* begin = boundary; // Until proven otherwise.244HeapWord* start_block = boundary_block; // Until proven otherwise.245if (boundary_block < boundary) {246if (s->block_is_obj(boundary_block) && s->obj_is_alive(boundary_block)) {247oop boundary_obj = cast_to_oop(boundary_block);248if (!boundary_obj->is_objArray() &&249!boundary_obj->is_typeArray()) {250guarantee(cur_entry > byte_for(used.start()),251"else boundary would be boundary_block");252if (*byte_for(boundary_block) != clean_card_val()) {253begin = boundary_block + s->block_size(boundary_block);254start_block = begin;255}256}257}258}259// Now traverse objects until end.260if (begin < end) {261MemRegion mr(begin, end);262VerifyCleanCardClosure verify_blk(gen_boundary, begin, end);263for (HeapWord* cur = start_block; cur < end; cur += s->block_size(cur)) {264if (s->block_is_obj(cur) && s->obj_is_alive(cur)) {265cast_to_oop(cur)->oop_iterate(&verify_blk, mr);266}267}268}269cur_entry = first_dirty;270} else {271// We'd normally expect that cur_youngergen_and_prev_nonclean_card272// is a transient value, that cannot be in the card table273// except during GC, and thus assert that:274// guarantee(*cur_entry != cur_youngergen_and_prev_nonclean_card,275// "Illegal CT value");276// That however, need not hold, as will become clear in the277// following...278279// We'd normally expect that if we are in the parallel case,280// we can't have left a prev value (which would be different281// from the current value) in the card table, and so we'd like to282// assert that:283// guarantee(cur_youngergen_card_val() == youngergen_card284// || !is_prev_youngergen_card_val(*cur_entry),285// "Illegal CT value");286// That, however, may not hold occasionally, because of287// CMS or MSC in the old gen. To wit, consider the288// following two simple illustrative scenarios:289// (a) CMS: Consider the case where a large object L290// spanning several cards is allocated in the old291// gen, and has a young gen reference stored in it, dirtying292// some interior cards. A young collection scans the card,293// finds a young ref and installs a youngergenP_n value.294// L then goes dead. Now a CMS collection starts,295// finds L dead and sweeps it up. Assume that L is296// abutting _unallocated_blk, so _unallocated_blk is297// adjusted down to (below) L. Assume further that298// no young collection intervenes during this CMS cycle.299// The next young gen cycle will not get to look at this300// youngergenP_n card since it lies in the unoccupied301// part of the space.302// Some young collections later the blocks on this303// card can be re-allocated either due to direct allocation304// or due to absorbing promotions. At this time, the305// before-gc verification will fail the above assert.306// (b) MSC: In this case, an object L with a young reference307// is on a card that (therefore) holds a youngergen_n value.308// Suppose also that L lies towards the end of the used309// the used space before GC. An MSC collection310// occurs that compacts to such an extent that this311// card is no longer in the occupied part of the space.312// Since current code in MSC does not always clear cards313// in the unused part of old gen, this stale youngergen_n314// value is left behind and can later be covered by315// an object when promotion or direct allocation316// re-allocates that part of the heap.317//318// Fortunately, the presence of such stale card values is319// "only" a minor annoyance in that subsequent young collections320// might needlessly scan such cards, but would still never corrupt321// the heap as a result. However, it's likely not to be a significant322// performance inhibitor in practice. For instance,323// some recent measurements with unoccupied cards eagerly cleared324// out to maintain this invariant, showed next to no325// change in young collection times; of course one can construct326// degenerate examples where the cost can be significant.)327// Note, in particular, that if the "stale" card is modified328// after re-allocation, it would be dirty, not "stale". Thus,329// we can never have a younger ref in such a card and it is330// safe not to scan that card in any collection. [As we see331// below, we do some unnecessary scanning332// in some cases in the current parallel scanning algorithm.]333//334// The main point below is that the parallel card scanning code335// deals correctly with these stale card values. There are two main336// cases to consider where we have a stale "young gen" value and a337// "derivative" case to consider, where we have a stale338// "cur_younger_gen_and_prev_non_clean" value, as will become339// apparent in the case analysis below.340// o Case 1. If the stale value corresponds to a younger_gen_n341// value other than the cur_younger_gen value then the code342// treats this as being tantamount to a prev_younger_gen343// card. This means that the card may be unnecessarily scanned.344// There are two sub-cases to consider:345// o Case 1a. Let us say that the card is in the occupied part346// of the generation at the time the collection begins. In347// that case the card will be either cleared when it is scanned348// for young pointers, or will be set to cur_younger_gen as a349// result of promotion. (We have elided the normal case where350// the scanning thread and the promoting thread interleave351// possibly resulting in a transient352// cur_younger_gen_and_prev_non_clean value before settling353// to cur_younger_gen. [End Case 1a.]354// o Case 1b. Consider now the case when the card is in the unoccupied355// part of the space which becomes occupied because of promotions356// into it during the current young GC. In this case the card357// will never be scanned for young references. The current358// code will set the card value to either359// cur_younger_gen_and_prev_non_clean or leave360// it with its stale value -- because the promotions didn't361// result in any younger refs on that card. Of these two362// cases, the latter will be covered in Case 1a during363// a subsequent scan. To deal with the former case, we need364// to further consider how we deal with a stale value of365// cur_younger_gen_and_prev_non_clean in our case analysis366// below. This we do in Case 3 below. [End Case 1b]367// [End Case 1]368// o Case 2. If the stale value corresponds to cur_younger_gen being369// a value not necessarily written by a current promotion, the370// card will not be scanned by the younger refs scanning code.371// (This is OK since as we argued above such cards cannot contain372// any younger refs.) The result is that this value will be373// treated as a prev_younger_gen value in a subsequent collection,374// which is addressed in Case 1 above. [End Case 2]375// o Case 3. We here consider the "derivative" case from Case 1b. above376// because of which we may find a stale377// cur_younger_gen_and_prev_non_clean card value in the table.378// Once again, as in Case 1, we consider two subcases, depending379// on whether the card lies in the occupied or unoccupied part380// of the space at the start of the young collection.381// o Case 3a. Let us say the card is in the occupied part of382// the old gen at the start of the young collection. In that383// case, the card will be scanned by the younger refs scanning384// code which will set it to cur_younger_gen. In a subsequent385// scan, the card will be considered again and get its final386// correct value. [End Case 3a]387// o Case 3b. Now consider the case where the card is in the388// unoccupied part of the old gen, and is occupied as a result389// of promotions during thus young gc. In that case,390// the card will not be scanned for younger refs. The presence391// of newly promoted objects on the card will then result in392// its keeping the value cur_younger_gen_and_prev_non_clean393// value, which we have dealt with in Case 3 here. [End Case 3b]394// [End Case 3]395//396// (Please refer to the code in the helper class397// ClearNonCleanCardWrapper and in CardTable for details.)398//399// The informal arguments above can be tightened into a formal400// correctness proof and it behooves us to write up such a proof,401// or to use model checking to prove that there are no lingering402// concerns.403//404// Clearly because of Case 3b one cannot bound the time for405// which a card will retain what we have called a "stale" value.406// However, one can obtain a Loose upper bound on the redundant407// work as a result of such stale values. Note first that any408// time a stale card lies in the occupied part of the space at409// the start of the collection, it is scanned by younger refs410// code and we can define a rank function on card values that411// declines when this is so. Note also that when a card does not412// lie in the occupied part of the space at the beginning of a413// young collection, its rank can either decline or stay unchanged.414// In this case, no extra work is done in terms of redundant415// younger refs scanning of that card.416// Then, the case analysis above reveals that, in the worst case,417// any such stale card will be scanned unnecessarily at most twice.418//419// It is nonetheless advisable to try and get rid of some of this420// redundant work in a subsequent (low priority) re-design of421// the card-scanning code, if only to simplify the underlying422// state machine analysis/proof. ysr 1/28/2002. XXX423cur_entry++;424}425}426}427428void CardTableRS::verify() {429// At present, we only know how to verify the card table RS for430// generational heaps.431VerifyCTGenClosure blk(this);432GenCollectedHeap::heap()->generation_iterate(&blk, false);433CardTable::verify();434}435436CardTableRS::CardTableRS(MemRegion whole_heap) :437CardTable(whole_heap) { }438439void CardTableRS::initialize() {440CardTable::initialize();441}442443void CardTableRS::non_clean_card_iterate(Space* sp,444HeapWord* gen_boundary,445MemRegion mr,446OopIterateClosure* cl,447CardTableRS* ct)448{449if (mr.is_empty()) {450return;451}452// clear_cl finds contiguous dirty ranges of cards to process and clear.453454DirtyCardToOopClosure* dcto_cl = sp->new_dcto_cl(cl, precision(), gen_boundary);455ClearNoncleanCardWrapper clear_cl(dcto_cl, ct);456457clear_cl.do_MemRegion(mr);458}459460bool CardTableRS::is_in_young(oop obj) const {461return GenCollectedHeap::heap()->is_in_young(obj);462}463464465