1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)cardTableRS.cpp      1.45 07/05/25 12:54:50 JVM"
   3 #endif
   4 /*
   5  * Copyright 2001-2008 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 # include "incls/_precompiled.incl"
  29 # include "incls/_cardTableRS.cpp.incl"
  30 
  31 CardTableRS::CardTableRS(MemRegion whole_heap,
  32                          int max_covered_regions) :
  33   GenRemSet(),
  34   _cur_youngergen_card_val(youngergenP1_card),
  35   _regions_to_iterate(max_covered_regions - 1)
  36 {
  37 #ifndef SERIALGC
  38   if (UseG1GC) {
  39     if (G1RSBarrierUseQueue) {
  40       _ct_bs = new G1SATBCardTableLoggingModRefBS(whole_heap,
  41                                                   max_covered_regions);
  42     } else {
  43       _ct_bs = new G1SATBCardTableModRefBS(whole_heap, max_covered_regions);
  44     }
  45   } else {
  46     _ct_bs = new CardTableModRefBSForCTRS(whole_heap, max_covered_regions);
  47   }
  48 #else
  49   _ct_bs = new CardTableModRefBSForCTRS(whole_heap, max_covered_regions);
  50 #endif
  51   set_bs(_ct_bs);
  52   _last_cur_val_in_gen = new jbyte[GenCollectedHeap::max_gens + 1];
  53   if (_last_cur_val_in_gen == NULL) {
  54     vm_exit_during_initialization("Could not last_cur_val_in_gen array.");
  55   }
  56   for (int i = 0; i < GenCollectedHeap::max_gens + 1; i++) {
  57     _last_cur_val_in_gen[i] = clean_card_val();
  58   }
  59   _ct_bs->set_CTRS(this);
  60 }
  61 
  62 void CardTableRS::resize_covered_region(MemRegion new_region) {
  63   _ct_bs->resize_covered_region(new_region);
  64 }
  65 
  66 jbyte CardTableRS::find_unused_youngergenP_card_value() {
  67   for (jbyte v = youngergenP1_card;
  68        v < cur_youngergen_and_prev_nonclean_card;
  69        v++) {
  70     bool seen = false;
  71     for (int g = 0; g < _regions_to_iterate; g++) {
  72       if (_last_cur_val_in_gen[g] == v) {
  73         seen = true;
  74         break;
  75       }
  76     }
  77     if (!seen) return v;
  78   }
  79   ShouldNotReachHere();
  80   return 0;
  81 }
  82 
  83 void CardTableRS::prepare_for_younger_refs_iterate(bool parallel) {
  84   // Parallel or sequential, we must always set the prev to equal the
  85   // last one written.
  86   if (parallel) {
  87     // Find a parallel value to be used next.
  88     jbyte next_val = find_unused_youngergenP_card_value();
  89     set_cur_youngergen_card_val(next_val);
  90 
  91   } else {
  92     // In an sequential traversal we will always write youngergen, so that
  93     // the inline barrier is  correct.
  94     set_cur_youngergen_card_val(youngergen_card);
  95   }
  96 }
  97 
  98 void CardTableRS::younger_refs_iterate(Generation* g,
  99                                        OopsInGenClosure* blk) {
 100   _last_cur_val_in_gen[g->level()+1] = cur_youngergen_card_val();
 101   g->younger_refs_iterate(blk);
 102 }
 103 
 104 class ClearNoncleanCardWrapper: public MemRegionClosure {
 105   MemRegionClosure* _dirty_card_closure;
 106   CardTableRS* _ct;
 107   bool _is_par;
 108 private:
 109   // Clears the given card, return true if the corresponding card should be 
 110   // processed.
 111   bool clear_card(jbyte* entry) {
 112     if (_is_par) {
 113       while (true) {
 114         // In the parallel case, we may have to do this several times.
 115         jbyte entry_val = *entry;
 116         assert(entry_val != CardTableRS::clean_card_val(),
 117                "We shouldn't be looking at clean cards, and this should "
 118                "be the only place they get cleaned.");
 119         if (CardTableRS::card_is_dirty_wrt_gen_iter(entry_val)
 120             || _ct->is_prev_youngergen_card_val(entry_val)) {
 121           jbyte res =
 122             Atomic::cmpxchg(CardTableRS::clean_card_val(), entry, entry_val);
 123           if (res == entry_val) {
 124             break;
 125           } else {
 126             assert(res == CardTableRS::cur_youngergen_and_prev_nonclean_card,
 127                    "The CAS above should only fail if another thread did "
 128                    "a GC write barrier.");
 129           }
 130         } else if (entry_val ==
 131                    CardTableRS::cur_youngergen_and_prev_nonclean_card) {
 132           // Parallelism shouldn't matter in this case.  Only the thread
 133           // assigned to scan the card should change this value.
 134           *entry = _ct->cur_youngergen_card_val();
 135           break;
 136         } else {
 137           assert(entry_val == _ct->cur_youngergen_card_val(),
 138                  "Should be the only possibility.");
 139           // In this case, the card was clean before, and become
 140           // cur_youngergen only because of processing of a promoted object.
 141           // We don't have to look at the card.
 142           return false;
 143         }
 144       }
 145       return true;
 146     } else {
 147       jbyte entry_val = *entry;
 148       assert(entry_val != CardTableRS::clean_card_val(),
 149              "We shouldn't be looking at clean cards, and this should "
 150              "be the only place they get cleaned.");
 151       assert(entry_val != CardTableRS::cur_youngergen_and_prev_nonclean_card,
 152              "This should be possible in the sequential case.");
 153       *entry = CardTableRS::clean_card_val();
 154       return true;
 155     }
 156   }
 157 
 158 public:
 159   ClearNoncleanCardWrapper(MemRegionClosure* dirty_card_closure,
 160                            CardTableRS* ct) :
 161     _dirty_card_closure(dirty_card_closure), _ct(ct) {
 162     _is_par = (SharedHeap::heap()->n_par_threads() > 0);
 163   }
 164   void do_MemRegion(MemRegion mr) {
 165     // We start at the high end of "mr", walking backwards
 166     // while accumulating a contiguous dirty range of cards in
 167     // [start_of_non_clean, end_of_non_clean) which we then
 168     // process en masse.
 169     HeapWord* end_of_non_clean = mr.end();
 170     HeapWord* start_of_non_clean = end_of_non_clean;
 171     jbyte*       entry = _ct->byte_for(mr.last());
 172     const jbyte* first_entry = _ct->byte_for(mr.start());
 173     while (entry >= first_entry) {
 174       HeapWord* cur = _ct->addr_for(entry);
 175       if (!clear_card(entry)) {
 176         // We hit a clean card; process any non-empty
 177         // dirty range accumulated so far.
 178         if (start_of_non_clean < end_of_non_clean) {
 179           MemRegion mr2(start_of_non_clean, end_of_non_clean);
 180           _dirty_card_closure->do_MemRegion(mr2);
 181         }
 182         // Reset the dirty window while continuing to
 183         // look for the next dirty window to process.
 184         end_of_non_clean = cur;
 185         start_of_non_clean = end_of_non_clean;
 186       }
 187       // Open the left end of the window one card to the left.
 188       start_of_non_clean = cur;
 189       // Note that "entry" leads "start_of_non_clean" in
 190       // its leftward excursion after this point
 191       // in the loop and, when we hit the left end of "mr",
 192       // will point off of the left end of the card-table
 193       // for "mr".
 194       entry--;
 195     }
 196     // If the first card of "mr" was dirty, we will have
 197     // been left with a dirty window, co-initial with "mr",
 198     // which we now process.
 199     if (start_of_non_clean < end_of_non_clean) {
 200       MemRegion mr2(start_of_non_clean, end_of_non_clean);
 201       _dirty_card_closure->do_MemRegion(mr2);
 202     }
 203   }
 204 };
 205 // clean (by dirty->clean before) ==> cur_younger_gen
 206 // dirty                          ==> cur_youngergen_and_prev_nonclean_card
 207 // precleaned                     ==> cur_youngergen_and_prev_nonclean_card
 208 // prev-younger-gen               ==> cur_youngergen_and_prev_nonclean_card
 209 // cur-younger-gen                ==> cur_younger_gen
 210 // cur_youngergen_and_prev_nonclean_card ==> no change.
 211 void CardTableRS::write_ref_field_gc_par(void* field, oop new_val) {
 212   jbyte* entry = ct_bs()->byte_for(field);
 213   do {
 214     jbyte entry_val = *entry;
 215     // We put this first because it's probably the most common case.
 216     if (entry_val == clean_card_val()) {
 217       // No threat of contention with cleaning threads.
 218       *entry = cur_youngergen_card_val();
 219       return;
 220     } else if (card_is_dirty_wrt_gen_iter(entry_val)
 221                || is_prev_youngergen_card_val(entry_val)) {
 222       // Mark it as both cur and prev youngergen; card cleaning thread will
 223       // eventually remove the previous stuff.
 224       jbyte new_val = cur_youngergen_and_prev_nonclean_card;
 225       jbyte res = Atomic::cmpxchg(new_val, entry, entry_val);
 226       // Did the CAS succeed?
 227       if (res == entry_val) return;
 228       // Otherwise, retry, to see the new value.
 229       continue;
 230     } else {
 231       assert(entry_val == cur_youngergen_and_prev_nonclean_card
 232              || entry_val == cur_youngergen_card_val(),
 233              "should be only possibilities.");
 234       return;
 235     }
 236   } while (true);
 237 }
 238 
 239 void CardTableRS::younger_refs_in_space_iterate(Space* sp,
 240                                                 OopsInGenClosure* cl) {
 241   DirtyCardToOopClosure* dcto_cl = sp->new_dcto_cl(cl, _ct_bs->precision(),
 242                                                    cl->gen_boundary());
 243   ClearNoncleanCardWrapper clear_cl(dcto_cl, this);
 244 
 245   _ct_bs->non_clean_card_iterate(sp, sp->used_region_at_save_marks(),
 246                                 dcto_cl, &clear_cl, false);
 247 }
 248 
 249 void CardTableRS::clear_into_younger(Generation* gen, bool clear_perm) {
 250   GenCollectedHeap* gch = GenCollectedHeap::heap();
 251   // Generations younger than gen have been evacuated. We can clear
 252   // card table entries for gen (we know that it has no pointers
 253   // to younger gens) and for those below. The card tables for
 254   // the youngest gen need never be cleared, and those for perm gen
 255   // will be cleared based on the parameter clear_perm.
 256   // There's a bit of subtlety in the clear() and invalidate()
 257   // methods that we exploit here and in invalidate_or_clear()
 258   // below to avoid missing cards at the fringes. If clear() or
 259   // invalidate() are changed in the future, this code should
 260   // be revisited. 20040107.ysr
 261   Generation* g = gen;
 262   for(Generation* prev_gen = gch->prev_gen(g);
 263       prev_gen != NULL;
 264       g = prev_gen, prev_gen = gch->prev_gen(g)) {
 265     MemRegion to_be_cleared_mr = g->prev_used_region();
 266     clear(to_be_cleared_mr);
 267   }
 268   // Clear perm gen cards if asked to do so.
 269   if (clear_perm) {
 270     MemRegion to_be_cleared_mr = gch->perm_gen()->prev_used_region();
 271     clear(to_be_cleared_mr);
 272   }
 273 }
 274 
 275 void CardTableRS::invalidate_or_clear(Generation* gen, bool younger,
 276                                       bool perm) {
 277   GenCollectedHeap* gch = GenCollectedHeap::heap();
 278   // For each generation gen (and younger and/or perm)
 279   // invalidate the cards for the currently occupied part
 280   // of that generation and clear the cards for the
 281   // unoccupied part of the generation (if any, making use
 282   // of that generation's prev_used_region to determine that
 283   // region). No need to do anything for the youngest
 284   // generation. Also see note#20040107.ysr above.
 285   Generation* g = gen;
 286   for(Generation* prev_gen = gch->prev_gen(g); prev_gen != NULL;
 287       g = prev_gen, prev_gen = gch->prev_gen(g))  {
 288     MemRegion used_mr = g->used_region();
 289     MemRegion to_be_cleared_mr = g->prev_used_region().minus(used_mr);
 290     if (!to_be_cleared_mr.is_empty()) {
 291       clear(to_be_cleared_mr);
 292     }
 293     invalidate(used_mr);
 294     if (!younger) break;
 295   }
 296   // Clear perm gen cards if asked to do so.
 297   if (perm) {
 298     g = gch->perm_gen();
 299     MemRegion used_mr = g->used_region();
 300     MemRegion to_be_cleared_mr = g->prev_used_region().minus(used_mr);
 301     if (!to_be_cleared_mr.is_empty()) {
 302       clear(to_be_cleared_mr);
 303     }
 304     invalidate(used_mr);
 305   }
 306 }
 307 
 308 
 309 class VerifyCleanCardClosure: public OopClosure {
 310 private:
 311   HeapWord* _boundary;
 312   HeapWord* _begin;
 313   HeapWord* _end;
 314 protected:
 315   template <class T> void do_oop_work(T* p) {
 316     HeapWord* jp = (HeapWord*)p;
 317     if (jp >= _begin && jp < _end) {
 318       oop obj = oopDesc::load_decode_heap_oop(p);
 319       guarantee(obj == NULL ||
 320                 (HeapWord*)p < _boundary ||
 321                 (HeapWord*)obj >= _boundary,
 322                 "pointer on clean card crosses boundary");
 323     }
 324   }
 325 public:
 326   VerifyCleanCardClosure(HeapWord* b, HeapWord* begin, HeapWord* end) :
 327     _boundary(b), _begin(begin), _end(end) {}
 328   virtual void do_oop(oop* p)       { VerifyCleanCardClosure::do_oop_work(p); }
 329   virtual void do_oop(narrowOop* p) { VerifyCleanCardClosure::do_oop_work(p); }
 330 };
 331 
 332 class VerifyCTSpaceClosure: public SpaceClosure {
 333 private:
 334   CardTableRS* _ct;
 335   HeapWord* _boundary;
 336 public:
 337   VerifyCTSpaceClosure(CardTableRS* ct, HeapWord* boundary) :
 338     _ct(ct), _boundary(boundary) {}
 339   virtual void do_space(Space* s) { _ct->verify_space(s, _boundary); }
 340 };
 341 
 342 class VerifyCTGenClosure: public GenCollectedHeap::GenClosure {
 343   CardTableRS* _ct;
 344 public:
 345   VerifyCTGenClosure(CardTableRS* ct) : _ct(ct) {}
 346   void do_generation(Generation* gen) {
 347     // Skip the youngest generation.
 348     if (gen->level() == 0) return;
 349     // Normally, we're interested in pointers to younger generations.
 350     VerifyCTSpaceClosure blk(_ct, gen->reserved().start());
 351     gen->space_iterate(&blk, true);
 352   }
 353 };
 354 
 355 void CardTableRS::verify_space(Space* s, HeapWord* gen_boundary) {
 356   // We don't need to do young-gen spaces.
 357   if (s->end() <= gen_boundary) return;
 358   MemRegion used = s->used_region();
 359 
 360   jbyte* cur_entry = byte_for(used.start());
 361   jbyte* limit = byte_after(used.last());
 362   while (cur_entry < limit) {
 363     if (*cur_entry == CardTableModRefBS::clean_card) {
 364       jbyte* first_dirty = cur_entry+1;
 365       while (first_dirty < limit &&
 366              *first_dirty == CardTableModRefBS::clean_card) {
 367         first_dirty++;
 368       }
 369       // If the first object is a regular object, and it has a
 370       // young-to-old field, that would mark the previous card.
 371       HeapWord* boundary = addr_for(cur_entry);
 372       HeapWord* end = (first_dirty >= limit) ? used.end() : addr_for(first_dirty);
 373       HeapWord* boundary_block = s->block_start(boundary);
 374       HeapWord* begin = boundary;             // Until proven otherwise.
 375       HeapWord* start_block = boundary_block; // Until proven otherwise.
 376       if (boundary_block < boundary) {
 377         if (s->block_is_obj(boundary_block) && s->obj_is_alive(boundary_block)) {
 378           oop boundary_obj = oop(boundary_block);
 379           if (!boundary_obj->is_objArray() &&
 380               !boundary_obj->is_typeArray()) {
 381             guarantee(cur_entry > byte_for(used.start()),
 382                       "else boundary would be boundary_block");
 383             if (*byte_for(boundary_block) != CardTableModRefBS::clean_card) {
 384               begin = boundary_block + s->block_size(boundary_block);
 385               start_block = begin;
 386             }
 387           }
 388         }
 389       }
 390       // Now traverse objects until end.
 391       HeapWord* cur = start_block;
 392       VerifyCleanCardClosure verify_blk(gen_boundary, begin, end);
 393       while (cur < end) {
 394         if (s->block_is_obj(cur) && s->obj_is_alive(cur)) {
 395           oop(cur)->oop_iterate(&verify_blk);
 396         }
 397         cur += s->block_size(cur);
 398       }
 399       cur_entry = first_dirty;
 400     } else {
 401       // We'd normally expect that cur_youngergen_and_prev_nonclean_card
 402       // is a transient value, that cannot be in the card table
 403       // except during GC, and thus assert that:
 404       // guarantee(*cur_entry != cur_youngergen_and_prev_nonclean_card,
 405       //        "Illegal CT value");
 406       // That however, need not hold, as will become clear in the
 407       // following...
 408 
 409       // We'd normally expect that if we are in the parallel case,
 410       // we can't have left a prev value (which would be different
 411       // from the current value) in the card table, and so we'd like to
 412       // assert that:
 413       // guarantee(cur_youngergen_card_val() == youngergen_card
 414       //           || !is_prev_youngergen_card_val(*cur_entry),
 415       //           "Illegal CT value");
 416       // That, however, may not hold occasionally, because of
 417       // CMS or MSC in the old gen. To wit, consider the
 418       // following two simple illustrative scenarios:
 419       // (a) CMS: Consider the case where a large object L
 420       //     spanning several cards is allocated in the old
 421       //     gen, and has a young gen reference stored in it, dirtying
 422       //     some interior cards. A young collection scans the card,
 423       //     finds a young ref and installs a youngergenP_n value.
 424       //     L then goes dead. Now a CMS collection starts,
 425       //     finds L dead and sweeps it up. Assume that L is
 426       //     abutting _unallocated_blk, so _unallocated_blk is
 427       //     adjusted down to (below) L. Assume further that
 428       //     no young collection intervenes during this CMS cycle.
 429       //     The next young gen cycle will not get to look at this
 430       //     youngergenP_n card since it lies in the unoccupied
 431       //     part of the space.
 432       //     Some young collections later the blocks on this
 433       //     card can be re-allocated either due to direct allocation
 434       //     or due to absorbing promotions. At this time, the
 435       //     before-gc verification will fail the above assert.
 436       // (b) MSC: In this case, an object L with a young reference
 437       //     is on a card that (therefore) holds a youngergen_n value.
 438       //     Suppose also that L lies towards the end of the used
 439       //     the used space before GC. An MSC collection
 440       //     occurs that compacts to such an extent that this
 441       //     card is no longer in the occupied part of the space.
 442       //     Since current code in MSC does not always clear cards
 443       //     in the unused part of old gen, this stale youngergen_n
 444       //     value is left behind and can later be covered by
 445       //     an object when promotion or direct allocation
 446       //     re-allocates that part of the heap.
 447       //
 448       // Fortunately, the presence of such stale card values is
 449       // "only" a minor annoyance in that subsequent young collections
 450       // might needlessly scan such cards, but would still never corrupt
 451       // the heap as a result. However, it's likely not to be a significant
 452       // performance inhibitor in practice. For instance,
 453       // some recent measurements with unoccupied cards eagerly cleared
 454       // out to maintain this invariant, showed next to no
 455       // change in young collection times; of course one can construct
 456       // degenerate examples where the cost can be significant.)
 457       // Note, in particular, that if the "stale" card is modified
 458       // after re-allocation, it would be dirty, not "stale". Thus,
 459       // we can never have a younger ref in such a card and it is
 460       // safe not to scan that card in any collection. [As we see
 461       // below, we do some unnecessary scanning
 462       // in some cases in the current parallel scanning algorithm.]
 463       //
 464       // The main point below is that the parallel card scanning code
 465       // deals correctly with these stale card values. There are two main
 466       // cases to consider where we have a stale "younger gen" value and a
 467       // "derivative" case to consider, where we have a stale
 468       // "cur_younger_gen_and_prev_non_clean" value, as will become
 469       // apparent in the case analysis below.
 470       // o Case 1. If the stale value corresponds to a younger_gen_n 
 471       //   value other than the cur_younger_gen value then the code
 472       //   treats this as being tantamount to a prev_younger_gen
 473       //   card. This means that the card may be unnecessarily scanned.
 474       //   There are two sub-cases to consider:
 475       //   o Case 1a. Let us say that the card is in the occupied part
 476       //     of the generation at the time the collection begins. In
 477       //     that case the card will be either cleared when it is scanned
 478       //     for young pointers, or will be set to cur_younger_gen as a
 479       //     result of promotion. (We have elided the normal case where
 480       //     the scanning thread and the promoting thread interleave
 481       //     possibly resulting in a transient
 482       //     cur_younger_gen_and_prev_non_clean value before settling
 483       //     to cur_younger_gen. [End Case 1a.]
 484       //   o Case 1b. Consider now the case when the card is in the unoccupied
 485       //     part of the space which becomes occupied because of promotions
 486       //     into it during the current young GC. In this case the card
 487       //     will never be scanned for young references. The current
 488       //     code will set the card value to either
 489       //     cur_younger_gen_and_prev_non_clean or leave
 490       //     it with its stale value -- because the promotions didn't
 491       //     result in any younger refs on that card. Of these two
 492       //     cases, the latter will be covered in Case 1a during
 493       //     a subsequent scan. To deal with the former case, we need
 494       //     to further consider how we deal with a stale value of
 495       //     cur_younger_gen_and_prev_non_clean in our case analysis
 496       //     below. This we do in Case 3 below. [End Case 1b]
 497       //   [End Case 1]
 498       // o Case 2. If the stale value corresponds to cur_younger_gen being
 499       //   a value not necessarily written by a current promotion, the
 500       //   card will not be scanned by the younger refs scanning code.
 501       //   (This is OK since as we argued above such cards cannot contain
 502       //   any younger refs.) The result is that this value will be
 503       //   treated as a prev_younger_gen value in a subsequent collection,
 504       //   which is addressed in Case 1 above. [End Case 2]
 505       // o Case 3. We here consider the "derivative" case from Case 1b. above
 506       //   because of which we may find a stale
 507       //   cur_younger_gen_and_prev_non_clean card value in the table.
 508       //   Once again, as in Case 1, we consider two subcases, depending
 509       //   on whether the card lies in the occupied or unoccupied part
 510       //   of the space at the start of the young collection.
 511       //   o Case 3a. Let us say the card is in the occupied part of
 512       //     the old gen at the start of the young collection. In that
 513       //     case, the card will be scanned by the younger refs scanning
 514       //     code which will set it to cur_younger_gen. In a subsequent
 515       //     scan, the card will be considered again and get its final
 516       //     correct value. [End Case 3a]
 517       //   o Case 3b. Now consider the case where the card is in the
 518       //     unoccupied part of the old gen, and is occupied as a result
 519       //     of promotions during thus young gc. In that case,
 520       //     the card will not be scanned for younger refs. The presence
 521       //     of newly promoted objects on the card will then result in
 522       //     its keeping the value cur_younger_gen_and_prev_non_clean
 523       //     value, which we have dealt with in Case 3 here. [End Case 3b]
 524       //   [End Case 3]
 525       //
 526       // (Please refer to the code in the helper class
 527       // ClearNonCleanCardWrapper and in CardTableModRefBS for details.)
 528       //
 529       // The informal arguments above can be tightened into a formal
 530       // correctness proof and it behooves us to write up such a proof,
 531       // or to use model checking to prove that there are no lingering
 532       // concerns.
 533       //
 534       // Clearly because of Case 3b one cannot bound the time for
 535       // which a card will retain what we have called a "stale" value.
 536       // However, one can obtain a Loose upper bound on the redundant
 537       // work as a result of such stale values. Note first that any
 538       // time a stale card lies in the occupied part of the space at
 539       // the start of the collection, it is scanned by younger refs
 540       // code and we can define a rank function on card values that
 541       // declines when this is so. Note also that when a card does not
 542       // lie in the occupied part of the space at the beginning of a
 543       // young collection, its rank can either decline or stay unchanged.
 544       // In this case, no extra work is done in terms of redundant
 545       // younger refs scanning of that card.
 546       // Then, the case analysis above reveals that, in the worst case,
 547       // any such stale card will be scanned unnecessarily at most twice.
 548       //
 549       // It is nonethelss advisable to try and get rid of some of this
 550       // redundant work in a subsequent (low priority) re-design of
 551       // the card-scanning code, if only to simplify the underlying
 552       // state machine analysis/proof. ysr 1/28/2002. XXX
 553       cur_entry++;
 554     }
 555   }
 556 }
 557 
 558 void CardTableRS::verify() {
 559   // At present, we only know how to verify the card table RS for
 560   // generational heaps.
 561   VerifyCTGenClosure blk(this);
 562   CollectedHeap* ch = Universe::heap();
 563   // We will do the perm-gen portion of the card table, too.
 564   Generation* pg = SharedHeap::heap()->perm_gen();
 565   HeapWord* pg_boundary = pg->reserved().start();
 566 
 567   if (ch->kind() == CollectedHeap::GenCollectedHeap) {
 568     GenCollectedHeap::heap()->generation_iterate(&blk, false);
 569     _ct_bs->verify();
 570 
 571     // If the old gen collections also collect perm, then we are only
 572     // interested in perm-to-young pointers, not perm-to-old pointers.
 573     GenCollectedHeap* gch = GenCollectedHeap::heap();
 574     CollectorPolicy* cp = gch->collector_policy();
 575     if (cp->is_mark_sweep_policy() || cp->is_concurrent_mark_sweep_policy()) {
 576       pg_boundary = gch->get_gen(1)->reserved().start();
 577     }
 578   }
 579   VerifyCTSpaceClosure perm_space_blk(this, pg_boundary);
 580   SharedHeap::heap()->perm_gen()->space_iterate(&perm_space_blk, true);
 581 }
 582 
 583 
 584 void CardTableRS::verify_aligned_region_empty(MemRegion mr) {
 585   if (!mr.is_empty()) {
 586     jbyte* cur_entry = byte_for(mr.start());
 587     jbyte* limit = byte_after(mr.last());
 588     // The region mr may not start on a card boundary so
 589     // the first card may reflect a write to the space
 590     // just prior to mr.
 591     if (!is_aligned(mr.start())) {
 592       cur_entry++;
 593     }
 594     for (;cur_entry < limit; cur_entry++) {
 595       guarantee(*cur_entry == CardTableModRefBS::clean_card,
 596                 "Unexpected dirty card found");
 597     }
 598   }
 599 }