0N/A/*
3610N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "memory/cardTableRS.hpp"
1879N/A#include "memory/genCollectedHeap.hpp"
1879N/A#include "memory/generation.hpp"
1879N/A#include "memory/space.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/java.hpp"
1879N/A#include "runtime/os.hpp"
1879N/A#ifndef SERIALGC
1879N/A#include "gc_implementation/g1/concurrentMark.hpp"
1879N/A#include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"
1879N/A#endif
0N/A
0N/ACardTableRS::CardTableRS(MemRegion whole_heap,
0N/A int max_covered_regions) :
342N/A GenRemSet(),
342N/A _cur_youngergen_card_val(youngergenP1_card),
342N/A _regions_to_iterate(max_covered_regions - 1)
0N/A{
342N/A#ifndef SERIALGC
342N/A if (UseG1GC) {
342N/A _ct_bs = new G1SATBCardTableLoggingModRefBS(whole_heap,
342N/A max_covered_regions);
342N/A } else {
342N/A _ct_bs = new CardTableModRefBSForCTRS(whole_heap, max_covered_regions);
342N/A }
342N/A#else
342N/A _ct_bs = new CardTableModRefBSForCTRS(whole_heap, max_covered_regions);
342N/A#endif
342N/A set_bs(_ct_bs);
0N/A _last_cur_val_in_gen = new jbyte[GenCollectedHeap::max_gens + 1];
0N/A if (_last_cur_val_in_gen == NULL) {
0N/A vm_exit_during_initialization("Could not last_cur_val_in_gen array.");
0N/A }
0N/A for (int i = 0; i < GenCollectedHeap::max_gens + 1; i++) {
0N/A _last_cur_val_in_gen[i] = clean_card_val();
0N/A }
342N/A _ct_bs->set_CTRS(this);
0N/A}
0N/A
0N/Avoid CardTableRS::resize_covered_region(MemRegion new_region) {
342N/A _ct_bs->resize_covered_region(new_region);
0N/A}
0N/A
0N/Ajbyte CardTableRS::find_unused_youngergenP_card_value() {
0N/A for (jbyte v = youngergenP1_card;
0N/A v < cur_youngergen_and_prev_nonclean_card;
0N/A v++) {
0N/A bool seen = false;
342N/A for (int g = 0; g < _regions_to_iterate; g++) {
0N/A if (_last_cur_val_in_gen[g] == v) {
0N/A seen = true;
0N/A break;
0N/A }
0N/A }
0N/A if (!seen) return v;
0N/A }
0N/A ShouldNotReachHere();
0N/A return 0;
0N/A}
0N/A
0N/Avoid CardTableRS::prepare_for_younger_refs_iterate(bool parallel) {
0N/A // Parallel or sequential, we must always set the prev to equal the
0N/A // last one written.
0N/A if (parallel) {
0N/A // Find a parallel value to be used next.
0N/A jbyte next_val = find_unused_youngergenP_card_value();
0N/A set_cur_youngergen_card_val(next_val);
0N/A
0N/A } else {
0N/A // In an sequential traversal we will always write youngergen, so that
0N/A // the inline barrier is correct.
0N/A set_cur_youngergen_card_val(youngergen_card);
0N/A }
0N/A}
0N/A
0N/Avoid CardTableRS::younger_refs_iterate(Generation* g,
0N/A OopsInGenClosure* blk) {
0N/A _last_cur_val_in_gen[g->level()+1] = cur_youngergen_card_val();
0N/A g->younger_refs_iterate(blk);
0N/A}
0N/A
2384N/Ainline bool ClearNoncleanCardWrapper::clear_card(jbyte* entry) {
2384N/A if (_is_par) {
2384N/A return clear_card_parallel(entry);
2384N/A } else {
2384N/A return clear_card_serial(entry);
2384N/A }
2384N/A}
2384N/A
2384N/Ainline bool ClearNoncleanCardWrapper::clear_card_parallel(jbyte* entry) {
2384N/A while (true) {
2384N/A // In the parallel case, we may have to do this several times.
2384N/A jbyte entry_val = *entry;
2384N/A assert(entry_val != CardTableRS::clean_card_val(),
2384N/A "We shouldn't be looking at clean cards, and this should "
2384N/A "be the only place they get cleaned.");
2384N/A if (CardTableRS::card_is_dirty_wrt_gen_iter(entry_val)
2384N/A || _ct->is_prev_youngergen_card_val(entry_val)) {
2384N/A jbyte res =
2384N/A Atomic::cmpxchg(CardTableRS::clean_card_val(), entry, entry_val);
2384N/A if (res == entry_val) {
2384N/A break;
2384N/A } else {
2384N/A assert(res == CardTableRS::cur_youngergen_and_prev_nonclean_card,
2384N/A "The CAS above should only fail if another thread did "
2384N/A "a GC write barrier.");
0N/A }
2384N/A } else if (entry_val ==
2384N/A CardTableRS::cur_youngergen_and_prev_nonclean_card) {
2384N/A // Parallelism shouldn't matter in this case. Only the thread
2384N/A // assigned to scan the card should change this value.
2384N/A *entry = _ct->cur_youngergen_card_val();
2384N/A break;
0N/A } else {
2384N/A assert(entry_val == _ct->cur_youngergen_card_val(),
2384N/A "Should be the only possibility.");
2384N/A // In this case, the card was clean before, and become
2384N/A // cur_youngergen only because of processing of a promoted object.
2384N/A // We don't have to look at the card.
2384N/A return false;
0N/A }
0N/A }
2384N/A return true;
2384N/A}
0N/A
2384N/A
2384N/Ainline bool ClearNoncleanCardWrapper::clear_card_serial(jbyte* entry) {
2384N/A jbyte entry_val = *entry;
2384N/A assert(entry_val != CardTableRS::clean_card_val(),
2384N/A "We shouldn't be looking at clean cards, and this should "
2384N/A "be the only place they get cleaned.");
2384N/A assert(entry_val != CardTableRS::cur_youngergen_and_prev_nonclean_card,
2384N/A "This should be possible in the sequential case.");
2384N/A *entry = CardTableRS::clean_card_val();
2384N/A return true;
2384N/A}
2384N/A
2384N/AClearNoncleanCardWrapper::ClearNoncleanCardWrapper(
2454N/A DirtyCardToOopClosure* dirty_card_closure, CardTableRS* ct) :
0N/A _dirty_card_closure(dirty_card_closure), _ct(ct) {
2941N/A // Cannot yet substitute active_workers for n_par_threads
2941N/A // in the case where parallelism is being turned off by
2941N/A // setting n_par_threads to 0.
0N/A _is_par = (SharedHeap::heap()->n_par_threads() > 0);
2941N/A assert(!_is_par ||
2941N/A (SharedHeap::heap()->n_par_threads() ==
2941N/A SharedHeap::heap()->workers()->active_workers()), "Mismatch");
2384N/A}
2384N/A
3610N/Abool ClearNoncleanCardWrapper::is_word_aligned(jbyte* entry) {
3610N/A return (((intptr_t)entry) & (BytesPerWord-1)) == 0;
3610N/A}
3610N/A
2384N/Avoid ClearNoncleanCardWrapper::do_MemRegion(MemRegion mr) {
2384N/A assert(mr.word_size() > 0, "Error");
2384N/A assert(_ct->is_aligned(mr.start()), "mr.start() should be card aligned");
2384N/A // mr.end() may not necessarily be card aligned.
2384N/A jbyte* cur_entry = _ct->byte_for(mr.last());
2384N/A const jbyte* limit = _ct->byte_for(mr.start());
2384N/A HeapWord* end_of_non_clean = mr.end();
2384N/A HeapWord* start_of_non_clean = end_of_non_clean;
2384N/A while (cur_entry >= limit) {
2384N/A HeapWord* cur_hw = _ct->addr_for(cur_entry);
2384N/A if ((*cur_entry != CardTableRS::clean_card_val()) && clear_card(cur_entry)) {
2384N/A // Continue the dirty range by opening the
2384N/A // dirty window one card to the left.
2384N/A start_of_non_clean = cur_hw;
2384N/A } else {
2384N/A // We hit a "clean" card; process any non-empty
2384N/A // "dirty" range accumulated so far.
2384N/A if (start_of_non_clean < end_of_non_clean) {
2384N/A const MemRegion mrd(start_of_non_clean, end_of_non_clean);
2384N/A _dirty_card_closure->do_MemRegion(mrd);
2384N/A }
3610N/A
3610N/A // fast forward through potential continuous whole-word range of clean cards beginning at a word-boundary
3610N/A if (is_word_aligned(cur_entry)) {
3610N/A jbyte* cur_row = cur_entry - BytesPerWord;
3610N/A while (cur_row >= limit && *((intptr_t*)cur_row) == CardTableRS::clean_card_row()) {
3610N/A cur_row -= BytesPerWord;
3610N/A }
3610N/A cur_entry = cur_row + BytesPerWord;
3610N/A cur_hw = _ct->addr_for(cur_entry);
3610N/A }
3610N/A
2384N/A // Reset the dirty window, while continuing to look
2384N/A // for the next dirty card that will start a
2384N/A // new dirty window.
2384N/A end_of_non_clean = cur_hw;
2384N/A start_of_non_clean = cur_hw;
2384N/A }
2384N/A // Note that "cur_entry" leads "start_of_non_clean" in
2384N/A // its leftward excursion after this point
2384N/A // in the loop and, when we hit the left end of "mr",
2384N/A // will point off of the left end of the card-table
2384N/A // for "mr".
2384N/A cur_entry--;
0N/A }
2384N/A // If the first card of "mr" was dirty, we will have
2384N/A // been left with a dirty window, co-initial with "mr",
2384N/A // which we now process.
2384N/A if (start_of_non_clean < end_of_non_clean) {
2384N/A const MemRegion mrd(start_of_non_clean, end_of_non_clean);
2384N/A _dirty_card_closure->do_MemRegion(mrd);
0N/A }
2384N/A}
2384N/A
0N/A// clean (by dirty->clean before) ==> cur_younger_gen
0N/A// dirty ==> cur_youngergen_and_prev_nonclean_card
0N/A// precleaned ==> cur_youngergen_and_prev_nonclean_card
0N/A// prev-younger-gen ==> cur_youngergen_and_prev_nonclean_card
0N/A// cur-younger-gen ==> cur_younger_gen
0N/A// cur_youngergen_and_prev_nonclean_card ==> no change.
113N/Avoid CardTableRS::write_ref_field_gc_par(void* field, oop new_val) {
0N/A jbyte* entry = ct_bs()->byte_for(field);
0N/A do {
0N/A jbyte entry_val = *entry;
0N/A // We put this first because it's probably the most common case.
0N/A if (entry_val == clean_card_val()) {
0N/A // No threat of contention with cleaning threads.
0N/A *entry = cur_youngergen_card_val();
0N/A return;
0N/A } else if (card_is_dirty_wrt_gen_iter(entry_val)
0N/A || is_prev_youngergen_card_val(entry_val)) {
0N/A // Mark it as both cur and prev youngergen; card cleaning thread will
0N/A // eventually remove the previous stuff.
0N/A jbyte new_val = cur_youngergen_and_prev_nonclean_card;
0N/A jbyte res = Atomic::cmpxchg(new_val, entry, entry_val);
0N/A // Did the CAS succeed?
0N/A if (res == entry_val) return;
0N/A // Otherwise, retry, to see the new value.
0N/A continue;
0N/A } else {
0N/A assert(entry_val == cur_youngergen_and_prev_nonclean_card
0N/A || entry_val == cur_youngergen_card_val(),
0N/A "should be only possibilities.");
0N/A return;
0N/A }
0N/A } while (true);
0N/A}
0N/A
0N/Avoid CardTableRS::younger_refs_in_space_iterate(Space* sp,
0N/A OopsInGenClosure* cl) {
2390N/A const MemRegion urasm = sp->used_region_at_save_marks();
2390N/A#ifdef ASSERT
2390N/A // Convert the assertion check to a warning if we are running
2390N/A // CMS+ParNew until related bug is fixed.
2390N/A MemRegion ur = sp->used_region();
2390N/A assert(ur.contains(urasm) || (UseConcMarkSweepGC && UseParNewGC),
2390N/A err_msg("Did you forget to call save_marks()? "
2390N/A "[" PTR_FORMAT ", " PTR_FORMAT ") is not contained in "
2390N/A "[" PTR_FORMAT ", " PTR_FORMAT ")",
2390N/A urasm.start(), urasm.end(), ur.start(), ur.end()));
2390N/A // In the case of CMS+ParNew, issue a warning
2390N/A if (!ur.contains(urasm)) {
2390N/A assert(UseConcMarkSweepGC && UseParNewGC, "Tautology: see assert above");
2390N/A warning("CMS+ParNew: Did you forget to call save_marks()? "
2390N/A "[" PTR_FORMAT ", " PTR_FORMAT ") is not contained in "
2390N/A "[" PTR_FORMAT ", " PTR_FORMAT ")",
2390N/A urasm.start(), urasm.end(), ur.start(), ur.end());
2390N/A MemRegion ur2 = sp->used_region();
2390N/A MemRegion urasm2 = sp->used_region_at_save_marks();
2390N/A if (!ur.equals(ur2)) {
2390N/A warning("CMS+ParNew: Flickering used_region()!!");
2390N/A }
2390N/A if (!urasm.equals(urasm2)) {
2390N/A warning("CMS+ParNew: Flickering used_region_at_save_marks()!!");
2390N/A }
2454N/A ShouldNotReachHere();
2390N/A }
2390N/A#endif
2454N/A _ct_bs->non_clean_card_iterate_possibly_parallel(sp, urasm, cl, this);
0N/A}
0N/A
0N/Avoid CardTableRS::clear_into_younger(Generation* gen, bool clear_perm) {
0N/A GenCollectedHeap* gch = GenCollectedHeap::heap();
0N/A // Generations younger than gen have been evacuated. We can clear
0N/A // card table entries for gen (we know that it has no pointers
0N/A // to younger gens) and for those below. The card tables for
0N/A // the youngest gen need never be cleared, and those for perm gen
0N/A // will be cleared based on the parameter clear_perm.
0N/A // There's a bit of subtlety in the clear() and invalidate()
0N/A // methods that we exploit here and in invalidate_or_clear()
0N/A // below to avoid missing cards at the fringes. If clear() or
0N/A // invalidate() are changed in the future, this code should
0N/A // be revisited. 20040107.ysr
0N/A Generation* g = gen;
0N/A for(Generation* prev_gen = gch->prev_gen(g);
0N/A prev_gen != NULL;
0N/A g = prev_gen, prev_gen = gch->prev_gen(g)) {
0N/A MemRegion to_be_cleared_mr = g->prev_used_region();
0N/A clear(to_be_cleared_mr);
0N/A }
0N/A // Clear perm gen cards if asked to do so.
0N/A if (clear_perm) {
0N/A MemRegion to_be_cleared_mr = gch->perm_gen()->prev_used_region();
0N/A clear(to_be_cleared_mr);
0N/A }
0N/A}
0N/A
0N/Avoid CardTableRS::invalidate_or_clear(Generation* gen, bool younger,
0N/A bool perm) {
0N/A GenCollectedHeap* gch = GenCollectedHeap::heap();
0N/A // For each generation gen (and younger and/or perm)
0N/A // invalidate the cards for the currently occupied part
0N/A // of that generation and clear the cards for the
0N/A // unoccupied part of the generation (if any, making use
0N/A // of that generation's prev_used_region to determine that
0N/A // region). No need to do anything for the youngest
0N/A // generation. Also see note#20040107.ysr above.
0N/A Generation* g = gen;
0N/A for(Generation* prev_gen = gch->prev_gen(g); prev_gen != NULL;
0N/A g = prev_gen, prev_gen = gch->prev_gen(g)) {
0N/A MemRegion used_mr = g->used_region();
0N/A MemRegion to_be_cleared_mr = g->prev_used_region().minus(used_mr);
0N/A if (!to_be_cleared_mr.is_empty()) {
0N/A clear(to_be_cleared_mr);
0N/A }
0N/A invalidate(used_mr);
0N/A if (!younger) break;
0N/A }
0N/A // Clear perm gen cards if asked to do so.
0N/A if (perm) {
0N/A g = gch->perm_gen();
0N/A MemRegion used_mr = g->used_region();
0N/A MemRegion to_be_cleared_mr = g->prev_used_region().minus(used_mr);
0N/A if (!to_be_cleared_mr.is_empty()) {
0N/A clear(to_be_cleared_mr);
0N/A }
0N/A invalidate(used_mr);
0N/A }
0N/A}
0N/A
0N/A
0N/Aclass VerifyCleanCardClosure: public OopClosure {
113N/Aprivate:
113N/A HeapWord* _boundary;
113N/A HeapWord* _begin;
113N/A HeapWord* _end;
113N/Aprotected:
113N/A template <class T> void do_oop_work(T* p) {
0N/A HeapWord* jp = (HeapWord*)p;
2275N/A assert(jp >= _begin && jp < _end,
2275N/A err_msg("Error: jp " PTR_FORMAT " should be within "
2275N/A "[_begin, _end) = [" PTR_FORMAT "," PTR_FORMAT ")",
4348N/A jp, _begin, _end));
2275N/A oop obj = oopDesc::load_decode_heap_oop(p);
2275N/A guarantee(obj == NULL || (HeapWord*)obj >= _boundary,
2275N/A err_msg("pointer " PTR_FORMAT " at " PTR_FORMAT " on "
2275N/A "clean card crosses boundary" PTR_FORMAT,
2275N/A (HeapWord*)obj, jp, _boundary));
0N/A }
2275N/A
113N/Apublic:
113N/A VerifyCleanCardClosure(HeapWord* b, HeapWord* begin, HeapWord* end) :
2275N/A _boundary(b), _begin(begin), _end(end) {
2275N/A assert(b <= begin,
2275N/A err_msg("Error: boundary " PTR_FORMAT " should be at or below begin " PTR_FORMAT,
2275N/A b, begin));
2275N/A assert(begin <= end,
2275N/A err_msg("Error: begin " PTR_FORMAT " should be strictly below end " PTR_FORMAT,
2275N/A begin, end));
2275N/A }
2275N/A
113N/A virtual void do_oop(oop* p) { VerifyCleanCardClosure::do_oop_work(p); }
113N/A virtual void do_oop(narrowOop* p) { VerifyCleanCardClosure::do_oop_work(p); }
0N/A};
0N/A
0N/Aclass VerifyCTSpaceClosure: public SpaceClosure {
113N/Aprivate:
0N/A CardTableRS* _ct;
0N/A HeapWord* _boundary;
0N/Apublic:
0N/A VerifyCTSpaceClosure(CardTableRS* ct, HeapWord* boundary) :
0N/A _ct(ct), _boundary(boundary) {}
113N/A virtual void do_space(Space* s) { _ct->verify_space(s, _boundary); }
0N/A};
0N/A
0N/Aclass VerifyCTGenClosure: public GenCollectedHeap::GenClosure {
0N/A CardTableRS* _ct;
0N/Apublic:
0N/A VerifyCTGenClosure(CardTableRS* ct) : _ct(ct) {}
0N/A void do_generation(Generation* gen) {
0N/A // Skip the youngest generation.
0N/A if (gen->level() == 0) return;
0N/A // Normally, we're interested in pointers to younger generations.
0N/A VerifyCTSpaceClosure blk(_ct, gen->reserved().start());
0N/A gen->space_iterate(&blk, true);
0N/A }
0N/A};
0N/A
0N/Avoid CardTableRS::verify_space(Space* s, HeapWord* gen_boundary) {
0N/A // We don't need to do young-gen spaces.
0N/A if (s->end() <= gen_boundary) return;
0N/A MemRegion used = s->used_region();
0N/A
0N/A jbyte* cur_entry = byte_for(used.start());
0N/A jbyte* limit = byte_after(used.last());
0N/A while (cur_entry < limit) {
0N/A if (*cur_entry == CardTableModRefBS::clean_card) {
0N/A jbyte* first_dirty = cur_entry+1;
0N/A while (first_dirty < limit &&
0N/A *first_dirty == CardTableModRefBS::clean_card) {
0N/A first_dirty++;
0N/A }
0N/A // If the first object is a regular object, and it has a
0N/A // young-to-old field, that would mark the previous card.
0N/A HeapWord* boundary = addr_for(cur_entry);
0N/A HeapWord* end = (first_dirty >= limit) ? used.end() : addr_for(first_dirty);
0N/A HeapWord* boundary_block = s->block_start(boundary);
0N/A HeapWord* begin = boundary; // Until proven otherwise.
0N/A HeapWord* start_block = boundary_block; // Until proven otherwise.
0N/A if (boundary_block < boundary) {
0N/A if (s->block_is_obj(boundary_block) && s->obj_is_alive(boundary_block)) {
0N/A oop boundary_obj = oop(boundary_block);
0N/A if (!boundary_obj->is_objArray() &&
0N/A !boundary_obj->is_typeArray()) {
0N/A guarantee(cur_entry > byte_for(used.start()),
0N/A "else boundary would be boundary_block");
0N/A if (*byte_for(boundary_block) != CardTableModRefBS::clean_card) {
0N/A begin = boundary_block + s->block_size(boundary_block);
0N/A start_block = begin;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A // Now traverse objects until end.
2275N/A if (begin < end) {
2275N/A MemRegion mr(begin, end);
2275N/A VerifyCleanCardClosure verify_blk(gen_boundary, begin, end);
2275N/A for (HeapWord* cur = start_block; cur < end; cur += s->block_size(cur)) {
2275N/A if (s->block_is_obj(cur) && s->obj_is_alive(cur)) {
2275N/A oop(cur)->oop_iterate(&verify_blk, mr);
2275N/A }
0N/A }
0N/A }
0N/A cur_entry = first_dirty;
0N/A } else {
0N/A // We'd normally expect that cur_youngergen_and_prev_nonclean_card
0N/A // is a transient value, that cannot be in the card table
0N/A // except during GC, and thus assert that:
0N/A // guarantee(*cur_entry != cur_youngergen_and_prev_nonclean_card,
0N/A // "Illegal CT value");
0N/A // That however, need not hold, as will become clear in the
0N/A // following...
0N/A
0N/A // We'd normally expect that if we are in the parallel case,
0N/A // we can't have left a prev value (which would be different
0N/A // from the current value) in the card table, and so we'd like to
0N/A // assert that:
0N/A // guarantee(cur_youngergen_card_val() == youngergen_card
0N/A // || !is_prev_youngergen_card_val(*cur_entry),
0N/A // "Illegal CT value");
0N/A // That, however, may not hold occasionally, because of
0N/A // CMS or MSC in the old gen. To wit, consider the
0N/A // following two simple illustrative scenarios:
0N/A // (a) CMS: Consider the case where a large object L
0N/A // spanning several cards is allocated in the old
0N/A // gen, and has a young gen reference stored in it, dirtying
0N/A // some interior cards. A young collection scans the card,
0N/A // finds a young ref and installs a youngergenP_n value.
0N/A // L then goes dead. Now a CMS collection starts,
0N/A // finds L dead and sweeps it up. Assume that L is
0N/A // abutting _unallocated_blk, so _unallocated_blk is
0N/A // adjusted down to (below) L. Assume further that
0N/A // no young collection intervenes during this CMS cycle.
0N/A // The next young gen cycle will not get to look at this
0N/A // youngergenP_n card since it lies in the unoccupied
0N/A // part of the space.
0N/A // Some young collections later the blocks on this
0N/A // card can be re-allocated either due to direct allocation
0N/A // or due to absorbing promotions. At this time, the
0N/A // before-gc verification will fail the above assert.
0N/A // (b) MSC: In this case, an object L with a young reference
0N/A // is on a card that (therefore) holds a youngergen_n value.
0N/A // Suppose also that L lies towards the end of the used
0N/A // the used space before GC. An MSC collection
0N/A // occurs that compacts to such an extent that this
0N/A // card is no longer in the occupied part of the space.
0N/A // Since current code in MSC does not always clear cards
0N/A // in the unused part of old gen, this stale youngergen_n
0N/A // value is left behind and can later be covered by
0N/A // an object when promotion or direct allocation
0N/A // re-allocates that part of the heap.
0N/A //
0N/A // Fortunately, the presence of such stale card values is
0N/A // "only" a minor annoyance in that subsequent young collections
0N/A // might needlessly scan such cards, but would still never corrupt
0N/A // the heap as a result. However, it's likely not to be a significant
0N/A // performance inhibitor in practice. For instance,
0N/A // some recent measurements with unoccupied cards eagerly cleared
0N/A // out to maintain this invariant, showed next to no
0N/A // change in young collection times; of course one can construct
0N/A // degenerate examples where the cost can be significant.)
0N/A // Note, in particular, that if the "stale" card is modified
0N/A // after re-allocation, it would be dirty, not "stale". Thus,
0N/A // we can never have a younger ref in such a card and it is
0N/A // safe not to scan that card in any collection. [As we see
0N/A // below, we do some unnecessary scanning
0N/A // in some cases in the current parallel scanning algorithm.]
0N/A //
0N/A // The main point below is that the parallel card scanning code
0N/A // deals correctly with these stale card values. There are two main
0N/A // cases to consider where we have a stale "younger gen" value and a
0N/A // "derivative" case to consider, where we have a stale
0N/A // "cur_younger_gen_and_prev_non_clean" value, as will become
0N/A // apparent in the case analysis below.
0N/A // o Case 1. If the stale value corresponds to a younger_gen_n
0N/A // value other than the cur_younger_gen value then the code
0N/A // treats this as being tantamount to a prev_younger_gen
0N/A // card. This means that the card may be unnecessarily scanned.
0N/A // There are two sub-cases to consider:
0N/A // o Case 1a. Let us say that the card is in the occupied part
0N/A // of the generation at the time the collection begins. In
0N/A // that case the card will be either cleared when it is scanned
0N/A // for young pointers, or will be set to cur_younger_gen as a
0N/A // result of promotion. (We have elided the normal case where
0N/A // the scanning thread and the promoting thread interleave
0N/A // possibly resulting in a transient
0N/A // cur_younger_gen_and_prev_non_clean value before settling
0N/A // to cur_younger_gen. [End Case 1a.]
0N/A // o Case 1b. Consider now the case when the card is in the unoccupied
0N/A // part of the space which becomes occupied because of promotions
0N/A // into it during the current young GC. In this case the card
0N/A // will never be scanned for young references. The current
0N/A // code will set the card value to either
0N/A // cur_younger_gen_and_prev_non_clean or leave
0N/A // it with its stale value -- because the promotions didn't
0N/A // result in any younger refs on that card. Of these two
0N/A // cases, the latter will be covered in Case 1a during
0N/A // a subsequent scan. To deal with the former case, we need
0N/A // to further consider how we deal with a stale value of
0N/A // cur_younger_gen_and_prev_non_clean in our case analysis
0N/A // below. This we do in Case 3 below. [End Case 1b]
0N/A // [End Case 1]
0N/A // o Case 2. If the stale value corresponds to cur_younger_gen being
0N/A // a value not necessarily written by a current promotion, the
0N/A // card will not be scanned by the younger refs scanning code.
0N/A // (This is OK since as we argued above such cards cannot contain
0N/A // any younger refs.) The result is that this value will be
0N/A // treated as a prev_younger_gen value in a subsequent collection,
0N/A // which is addressed in Case 1 above. [End Case 2]
0N/A // o Case 3. We here consider the "derivative" case from Case 1b. above
0N/A // because of which we may find a stale
0N/A // cur_younger_gen_and_prev_non_clean card value in the table.
0N/A // Once again, as in Case 1, we consider two subcases, depending
0N/A // on whether the card lies in the occupied or unoccupied part
0N/A // of the space at the start of the young collection.
0N/A // o Case 3a. Let us say the card is in the occupied part of
0N/A // the old gen at the start of the young collection. In that
0N/A // case, the card will be scanned by the younger refs scanning
0N/A // code which will set it to cur_younger_gen. In a subsequent
0N/A // scan, the card will be considered again and get its final
0N/A // correct value. [End Case 3a]
0N/A // o Case 3b. Now consider the case where the card is in the
0N/A // unoccupied part of the old gen, and is occupied as a result
0N/A // of promotions during thus young gc. In that case,
0N/A // the card will not be scanned for younger refs. The presence
0N/A // of newly promoted objects on the card will then result in
0N/A // its keeping the value cur_younger_gen_and_prev_non_clean
0N/A // value, which we have dealt with in Case 3 here. [End Case 3b]
0N/A // [End Case 3]
0N/A //
0N/A // (Please refer to the code in the helper class
0N/A // ClearNonCleanCardWrapper and in CardTableModRefBS for details.)
0N/A //
0N/A // The informal arguments above can be tightened into a formal
0N/A // correctness proof and it behooves us to write up such a proof,
0N/A // or to use model checking to prove that there are no lingering
0N/A // concerns.
0N/A //
0N/A // Clearly because of Case 3b one cannot bound the time for
0N/A // which a card will retain what we have called a "stale" value.
0N/A // However, one can obtain a Loose upper bound on the redundant
0N/A // work as a result of such stale values. Note first that any
0N/A // time a stale card lies in the occupied part of the space at
0N/A // the start of the collection, it is scanned by younger refs
0N/A // code and we can define a rank function on card values that
0N/A // declines when this is so. Note also that when a card does not
0N/A // lie in the occupied part of the space at the beginning of a
0N/A // young collection, its rank can either decline or stay unchanged.
0N/A // In this case, no extra work is done in terms of redundant
0N/A // younger refs scanning of that card.
0N/A // Then, the case analysis above reveals that, in the worst case,
0N/A // any such stale card will be scanned unnecessarily at most twice.
0N/A //
0N/A // It is nonethelss advisable to try and get rid of some of this
0N/A // redundant work in a subsequent (low priority) re-design of
0N/A // the card-scanning code, if only to simplify the underlying
0N/A // state machine analysis/proof. ysr 1/28/2002. XXX
0N/A cur_entry++;
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid CardTableRS::verify() {
0N/A // At present, we only know how to verify the card table RS for
0N/A // generational heaps.
0N/A VerifyCTGenClosure blk(this);
0N/A CollectedHeap* ch = Universe::heap();
0N/A // We will do the perm-gen portion of the card table, too.
0N/A Generation* pg = SharedHeap::heap()->perm_gen();
0N/A HeapWord* pg_boundary = pg->reserved().start();
0N/A
0N/A if (ch->kind() == CollectedHeap::GenCollectedHeap) {
0N/A GenCollectedHeap::heap()->generation_iterate(&blk, false);
342N/A _ct_bs->verify();
0N/A
0N/A // If the old gen collections also collect perm, then we are only
0N/A // interested in perm-to-young pointers, not perm-to-old pointers.
0N/A GenCollectedHeap* gch = GenCollectedHeap::heap();
0N/A CollectorPolicy* cp = gch->collector_policy();
0N/A if (cp->is_mark_sweep_policy() || cp->is_concurrent_mark_sweep_policy()) {
0N/A pg_boundary = gch->get_gen(1)->reserved().start();
0N/A }
0N/A }
0N/A VerifyCTSpaceClosure perm_space_blk(this, pg_boundary);
0N/A SharedHeap::heap()->perm_gen()->space_iterate(&perm_space_blk, true);
0N/A}
0N/A
0N/A
6N/Avoid CardTableRS::verify_aligned_region_empty(MemRegion mr) {
0N/A if (!mr.is_empty()) {
0N/A jbyte* cur_entry = byte_for(mr.start());
0N/A jbyte* limit = byte_after(mr.last());
6N/A // The region mr may not start on a card boundary so
6N/A // the first card may reflect a write to the space
6N/A // just prior to mr.
6N/A if (!is_aligned(mr.start())) {
6N/A cur_entry++;
6N/A }
0N/A for (;cur_entry < limit; cur_entry++) {
0N/A guarantee(*cur_entry == CardTableModRefBS::clean_card,
0N/A "Unexpected dirty card found");
0N/A }
0N/A }
0N/A}