cardTableRS.cpp revision 6
/*
* Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
# include "incls/_precompiled.incl"
# include "incls/_cardTableRS.cpp.incl"
int max_covered_regions) :
{
if (_last_cur_val_in_gen == NULL) {
vm_exit_during_initialization("Could not last_cur_val_in_gen array.");
}
_last_cur_val_in_gen[i] = clean_card_val();
}
}
}
for (jbyte v = youngergenP1_card;
v++) {
bool seen = false;
if (_last_cur_val_in_gen[g] == v) {
seen = true;
break;
}
}
if (!seen) return v;
}
return 0;
}
// Parallel or sequential, we must always set the prev to equal the
// last one written.
if (parallel) {
// Find a parallel value to be used next.
} else {
// In an sequential traversal we will always write youngergen, so that
// the inline barrier is correct.
}
}
OopsInGenClosure* blk) {
g->younger_refs_iterate(blk);
}
class ClearNoncleanCardWrapper: public MemRegionClosure {
bool _is_par;
private:
// Clears the given card, return true if the corresponding card should be
// processed.
if (_is_par) {
while (true) {
// In the parallel case, we may have to do this several times.
"We shouldn't be looking at clean cards, and this should "
"be the only place they get cleaned.");
break;
} else {
"The CAS above should only fail if another thread did "
"a GC write barrier.");
}
} else if (entry_val ==
// Parallelism shouldn't matter in this case. Only the thread
// assigned to scan the card should change this value.
break;
} else {
"Should be the only possibility.");
// In this case, the card was clean before, and become
// cur_youngergen only because of processing of a promoted object.
// We don't have to look at the card.
return false;
}
}
return true;
} else {
"We shouldn't be looking at clean cards, and this should "
"be the only place they get cleaned.");
"This should be possible in the sequential case.");
return true;
}
}
public:
CardTableRS* ct) :
}
// We start at the high end of "mr", walking backwards
// while accumulating a contiguous dirty range of cards in
// [start_of_non_clean, end_of_non_clean) which we then
// process en masse.
while (entry >= first_entry) {
if (!clear_card(entry)) {
// We hit a clean card; process any non-empty
// dirty range accumulated so far.
if (start_of_non_clean < end_of_non_clean) {
}
// Reset the dirty window while continuing to
// look for the next dirty window to process.
}
// Open the left end of the window one card to the left.
// Note that "entry" leads "start_of_non_clean" in
// its leftward excursion after this point
// in the loop and, when we hit the left end of "mr",
// will point off of the left end of the card-table
// for "mr".
entry--;
}
// If the first card of "mr" was dirty, we will have
// been left with a dirty window, co-initial with "mr",
// which we now process.
if (start_of_non_clean < end_of_non_clean) {
}
}
};
// clean (by dirty->clean before) ==> cur_younger_gen
// dirty ==> cur_youngergen_and_prev_nonclean_card
// precleaned ==> cur_youngergen_and_prev_nonclean_card
// prev-younger-gen ==> cur_youngergen_and_prev_nonclean_card
// cur-younger-gen ==> cur_younger_gen
// cur_youngergen_and_prev_nonclean_card ==> no change.
do {
// We put this first because it's probably the most common case.
if (entry_val == clean_card_val()) {
// No threat of contention with cleaning threads.
*entry = cur_youngergen_card_val();
return;
} else if (card_is_dirty_wrt_gen_iter(entry_val)
// Mark it as both cur and prev youngergen; card cleaning thread will
// eventually remove the previous stuff.
// Did the CAS succeed?
// Otherwise, retry, to see the new value.
continue;
} else {
|| entry_val == cur_youngergen_card_val(),
"should be only possibilities.");
return;
}
} while (true);
}
OopsInGenClosure* cl) {
cl->gen_boundary());
}
// Generations younger than gen have been evacuated. We can clear
// card table entries for gen (we know that it has no pointers
// to younger gens) and for those below. The card tables for
// the youngest gen need never be cleared, and those for perm gen
// will be cleared based on the parameter clear_perm.
// There's a bit of subtlety in the clear() and invalidate()
// methods that we exploit here and in invalidate_or_clear()
// below to avoid missing cards at the fringes. If clear() or
// invalidate() are changed in the future, this code should
// be revisited. 20040107.ysr
Generation* g = gen;
}
// Clear perm gen cards if asked to do so.
if (clear_perm) {
}
}
bool perm) {
// invalidate the cards for the currently occupied part
// of that generation and clear the cards for the
// unoccupied part of the generation (if any, making use
// of that generation's prev_used_region to determine that
// region). No need to do anything for the youngest
// generation. Also see note#20040107.ysr above.
Generation* g = gen;
if (!to_be_cleared_mr.is_empty()) {
}
if (!younger) break;
}
// Clear perm gen cards if asked to do so.
if (perm) {
if (!to_be_cleared_mr.is_empty()) {
}
}
}
class VerifyCleanCardClosure: public OopClosure {
public:
"pointer on clean card crosses boundary");
}
}
};
class VerifyCTSpaceClosure: public SpaceClosure {
public:
};
public:
// Skip the youngest generation.
// Normally, we're interested in pointers to younger generations.
}
};
// We don't need to do young-gen spaces.
if (s->end() <= gen_boundary) return;
while (first_dirty < limit &&
first_dirty++;
}
// If the first object is a regular object, and it has a
// young-to-old field, that would mark the previous card.
if (boundary_block < boundary) {
if (!boundary_obj->is_objArray() &&
!boundary_obj->is_typeArray()) {
"else boundary would be boundary_block");
start_block = begin;
}
}
}
}
// Now traverse objects until end.
}
}
} else {
// We'd normally expect that cur_youngergen_and_prev_nonclean_card
// is a transient value, that cannot be in the card table
// except during GC, and thus assert that:
// guarantee(*cur_entry != cur_youngergen_and_prev_nonclean_card,
// "Illegal CT value");
// That however, need not hold, as will become clear in the
// following...
// We'd normally expect that if we are in the parallel case,
// we can't have left a prev value (which would be different
// from the current value) in the card table, and so we'd like to
// assert that:
// guarantee(cur_youngergen_card_val() == youngergen_card
// || !is_prev_youngergen_card_val(*cur_entry),
// "Illegal CT value");
// That, however, may not hold occasionally, because of
// CMS or MSC in the old gen. To wit, consider the
// following two simple illustrative scenarios:
// (a) CMS: Consider the case where a large object L
// spanning several cards is allocated in the old
// gen, and has a young gen reference stored in it, dirtying
// some interior cards. A young collection scans the card,
// finds a young ref and installs a youngergenP_n value.
// L then goes dead. Now a CMS collection starts,
// finds L dead and sweeps it up. Assume that L is
// abutting _unallocated_blk, so _unallocated_blk is
// adjusted down to (below) L. Assume further that
// no young collection intervenes during this CMS cycle.
// The next young gen cycle will not get to look at this
// youngergenP_n card since it lies in the unoccupied
// part of the space.
// Some young collections later the blocks on this
// card can be re-allocated either due to direct allocation
// or due to absorbing promotions. At this time, the
// before-gc verification will fail the above assert.
// (b) MSC: In this case, an object L with a young reference
// is on a card that (therefore) holds a youngergen_n value.
// Suppose also that L lies towards the end of the used
// the used space before GC. An MSC collection
// occurs that compacts to such an extent that this
// card is no longer in the occupied part of the space.
// Since current code in MSC does not always clear cards
// in the unused part of old gen, this stale youngergen_n
// value is left behind and can later be covered by
// an object when promotion or direct allocation
// re-allocates that part of the heap.
//
// Fortunately, the presence of such stale card values is
// "only" a minor annoyance in that subsequent young collections
// might needlessly scan such cards, but would still never corrupt
// the heap as a result. However, it's likely not to be a significant
// performance inhibitor in practice. For instance,
// some recent measurements with unoccupied cards eagerly cleared
// out to maintain this invariant, showed next to no
// change in young collection times; of course one can construct
// degenerate examples where the cost can be significant.)
// Note, in particular, that if the "stale" card is modified
// after re-allocation, it would be dirty, not "stale". Thus,
// we can never have a younger ref in such a card and it is
// safe not to scan that card in any collection. [As we see
// below, we do some unnecessary scanning
// in some cases in the current parallel scanning algorithm.]
//
// The main point below is that the parallel card scanning code
// deals correctly with these stale card values. There are two main
// cases to consider where we have a stale "younger gen" value and a
// "derivative" case to consider, where we have a stale
// "cur_younger_gen_and_prev_non_clean" value, as will become
// apparent in the case analysis below.
// o Case 1. If the stale value corresponds to a younger_gen_n
// value other than the cur_younger_gen value then the code
// treats this as being tantamount to a prev_younger_gen
// card. This means that the card may be unnecessarily scanned.
// There are two sub-cases to consider:
// o Case 1a. Let us say that the card is in the occupied part
// of the generation at the time the collection begins. In
// that case the card will be either cleared when it is scanned
// for young pointers, or will be set to cur_younger_gen as a
// result of promotion. (We have elided the normal case where
// the scanning thread and the promoting thread interleave
// possibly resulting in a transient
// cur_younger_gen_and_prev_non_clean value before settling
// to cur_younger_gen. [End Case 1a.]
// o Case 1b. Consider now the case when the card is in the unoccupied
// part of the space which becomes occupied because of promotions
// into it during the current young GC. In this case the card
// will never be scanned for young references. The current
// code will set the card value to either
// cur_younger_gen_and_prev_non_clean or leave
// it with its stale value -- because the promotions didn't
// result in any younger refs on that card. Of these two
// cases, the latter will be covered in Case 1a during
// a subsequent scan. To deal with the former case, we need
// to further consider how we deal with a stale value of
// cur_younger_gen_and_prev_non_clean in our case analysis
// below. This we do in Case 3 below. [End Case 1b]
// [End Case 1]
// o Case 2. If the stale value corresponds to cur_younger_gen being
// a value not necessarily written by a current promotion, the
// card will not be scanned by the younger refs scanning code.
// (This is OK since as we argued above such cards cannot contain
// any younger refs.) The result is that this value will be
// treated as a prev_younger_gen value in a subsequent collection,
// which is addressed in Case 1 above. [End Case 2]
// o Case 3. We here consider the "derivative" case from Case 1b. above
// because of which we may find a stale
// cur_younger_gen_and_prev_non_clean card value in the table.
// Once again, as in Case 1, we consider two subcases, depending
// on whether the card lies in the occupied or unoccupied part
// of the space at the start of the young collection.
// o Case 3a. Let us say the card is in the occupied part of
// the old gen at the start of the young collection. In that
// case, the card will be scanned by the younger refs scanning
// code which will set it to cur_younger_gen. In a subsequent
// scan, the card will be considered again and get its final
// correct value. [End Case 3a]
// o Case 3b. Now consider the case where the card is in the
// unoccupied part of the old gen, and is occupied as a result
// of promotions during thus young gc. In that case,
// the card will not be scanned for younger refs. The presence
// of newly promoted objects on the card will then result in
// its keeping the value cur_younger_gen_and_prev_non_clean
// value, which we have dealt with in Case 3 here. [End Case 3b]
// [End Case 3]
//
// (Please refer to the code in the helper class
// ClearNonCleanCardWrapper and in CardTableModRefBS for details.)
//
// The informal arguments above can be tightened into a formal
// correctness proof and it behooves us to write up such a proof,
// or to use model checking to prove that there are no lingering
// concerns.
//
// Clearly because of Case 3b one cannot bound the time for
// which a card will retain what we have called a "stale" value.
// However, one can obtain a Loose upper bound on the redundant
// work as a result of such stale values. Note first that any
// time a stale card lies in the occupied part of the space at
// the start of the collection, it is scanned by younger refs
// code and we can define a rank function on card values that
// declines when this is so. Note also that when a card does not
// lie in the occupied part of the space at the beginning of a
// young collection, its rank can either decline or stay unchanged.
// In this case, no extra work is done in terms of redundant
// younger refs scanning of that card.
// Then, the case analysis above reveals that, in the worst case,
// any such stale card will be scanned unnecessarily at most twice.
//
// It is nonethelss advisable to try and get rid of some of this
// redundant work in a subsequent (low priority) re-design of
// the card-scanning code, if only to simplify the underlying
cur_entry++;
}
}
}
void CardTableRS::verify() {
// At present, we only know how to verify the card table RS for
// generational heaps.
VerifyCTGenClosure blk(this);
// We will do the perm-gen portion of the card table, too.
// If the old gen collections also collect perm, then we are only
// interested in perm-to-young pointers, not perm-to-old pointers.
}
}
}
// The region mr may not start on a card boundary so
// the first card may reflect a write to the space
// just prior to mr.
cur_entry++;
}
"Unexpected dirty card found");
}
}
}