1 #ifdef USE_PRAGMA_IDENT_SRC
   2 #pragma ident "@(#)cardTableExtension.cpp       1.35 07/09/25 16:47:41 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/_cardTableExtension.cpp.incl"
  30 
  31 // Checks an individual oop for missing precise marks. Mark
  32 // may be either dirty or newgen.
  33 class CheckForUnmarkedOops : public OopClosure {
  34  private:
  35   PSYoungGen*         _young_gen;
  36   CardTableExtension* _card_table;
  37   HeapWord*           _unmarked_addr;
  38   jbyte*              _unmarked_card;
  39 
  40  protected:
  41   template <class T> void do_oop_work(T* p) {
  42     oop obj = oopDesc::load_decode_heap_oop_not_null(p);
  43     if (_young_gen->is_in_reserved(obj) &&
  44         !_card_table->addr_is_marked_imprecise(p)) {
  45       // Don't overwrite the first missing card mark
  46       if (_unmarked_addr == NULL) {
  47         _unmarked_addr = (HeapWord*)p;
  48         _unmarked_card = _card_table->byte_for(p);
  49       }
  50     }
  51   }
  52 
  53  public:
  54   CheckForUnmarkedOops(PSYoungGen* young_gen, CardTableExtension* card_table) :
  55     _young_gen(young_gen), _card_table(card_table), _unmarked_addr(NULL) { }
  56 
  57   virtual void do_oop(oop* p)       { CheckForUnmarkedOops::do_oop_work(p); }
  58   virtual void do_oop(narrowOop* p) { CheckForUnmarkedOops::do_oop_work(p); }
  59 
  60   bool has_unmarked_oop() {
  61     return _unmarked_addr != NULL;
  62   }
  63 };
  64 
  65 // Checks all objects for the existance of some type of mark,
  66 // precise or imprecise, dirty or newgen.
  67 class CheckForUnmarkedObjects : public ObjectClosure {
  68  private:
  69   PSYoungGen*         _young_gen;
  70   CardTableExtension* _card_table;
  71 
  72  public:
  73   CheckForUnmarkedObjects() {
  74     ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
  75     assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
  76     
  77     _young_gen = heap->young_gen();
  78     _card_table = (CardTableExtension*)heap->barrier_set();
  79     // No point in asserting barrier set type here. Need to make CardTableExtension
  80     // a unique barrier set type.
  81   }
  82 
  83   // Card marks are not precise. The current system can leave us with
  84   // a mismash of precise marks and begining of object marks. This means
  85   // we test for missing precise marks first. If any are found, we don't
  86   // fail unless the object head is also unmarked.
  87   virtual void do_object(oop obj) {
  88     CheckForUnmarkedOops object_check(_young_gen, _card_table);
  89     obj->oop_iterate(&object_check);
  90     if (object_check.has_unmarked_oop()) {
  91       assert(_card_table->addr_is_marked_imprecise(obj), "Found unmarked young_gen object");
  92     }
  93   }
  94 };
  95 
  96 // Checks for precise marking of oops as newgen.
  97 class CheckForPreciseMarks : public OopClosure {
  98  private:
  99   PSYoungGen*         _young_gen;
 100   CardTableExtension* _card_table;
 101 
 102  protected:
 103   template <class T> void do_oop_work(T* p) {
 104     oop obj = oopDesc::load_decode_heap_oop_not_null(p);
 105     if (_young_gen->is_in_reserved(obj)) {
 106       assert(_card_table->addr_is_marked_precise(p), "Found unmarked precise oop");
 107       _card_table->set_card_newgen(p);
 108     }
 109   }
 110 
 111  public:
 112   CheckForPreciseMarks( PSYoungGen* young_gen, CardTableExtension* card_table ) :
 113     _young_gen(young_gen), _card_table(card_table) { }
 114 
 115   virtual void do_oop(oop* p)       { CheckForPreciseMarks::do_oop_work(p); }
 116   virtual void do_oop(narrowOop* p) { CheckForPreciseMarks::do_oop_work(p); }
 117 };
 118 
 119 // We get passed the space_top value to prevent us from traversing into
 120 // the old_gen promotion labs, which cannot be safely parsed.
 121 void CardTableExtension::scavenge_contents(ObjectStartArray* start_array,
 122                                            MutableSpace* sp,
 123                                            HeapWord* space_top,
 124                                            PSPromotionManager* pm) 
 125 {
 126   assert(start_array != NULL && sp != NULL && pm != NULL, "Sanity");
 127   assert(start_array->covered_region().contains(sp->used_region()),
 128          "ObjectStartArray does not cover space");
 129   bool depth_first = pm->depth_first();
 130 
 131   if (sp->not_empty()) {
 132     oop* sp_top = (oop*)space_top;
 133     oop* prev_top = NULL;
 134     jbyte* current_card = byte_for(sp->bottom());
 135     jbyte* end_card     = byte_for(sp_top - 1);    // sp_top is exclusive
 136     // scan card marking array
 137     while (current_card <= end_card) {
 138       jbyte value = *current_card;
 139       // skip clean cards
 140       if (card_is_clean(value)) {
 141         current_card++;
 142       } else {
 143         // we found a non-clean card
 144         jbyte* first_nonclean_card = current_card++;
 145         oop* bottom = (oop*)addr_for(first_nonclean_card);
 146         // find object starting on card
 147         oop* bottom_obj = (oop*)start_array->object_start((HeapWord*)bottom);
 148         // bottom_obj = (oop*)start_array->object_start((HeapWord*)bottom);
 149         assert(bottom_obj <= bottom, "just checking");
 150         // make sure we don't scan oops we already looked at
 151         if (bottom < prev_top) bottom = prev_top;
 152         // figure out when to stop scanning
 153         jbyte* first_clean_card;
 154         oop* top;
 155         bool restart_scanning;
 156         do {
 157           restart_scanning = false;
 158           // find a clean card
 159           while (current_card <= end_card) {
 160             value = *current_card;
 161             if (card_is_clean(value)) break;
 162             current_card++;
 163           }
 164           // check if we reached the end, if so we are done
 165           if (current_card >= end_card) {
 166             first_clean_card = end_card + 1;
 167             current_card++;
 168             top = sp_top;
 169           } else {
 170             // we have a clean card, find object starting on that card
 171             first_clean_card = current_card++;
 172             top = (oop*)addr_for(first_clean_card);
 173             oop* top_obj = (oop*)start_array->object_start((HeapWord*)top);
 174             // top_obj = (oop*)start_array->object_start((HeapWord*)top);
 175             assert(top_obj <= top, "just checking");
 176             if (oop(top_obj)->is_objArray() || oop(top_obj)->is_typeArray()) {
 177               // an arrayOop is starting on the clean card - since we do exact store
 178               // checks for objArrays we are done
 179             } else {
 180               // otherwise, it is possible that the object starting on the clean card
 181               // spans the entire card, and that the store happened on a later card.
 182               // figure out where the object ends
 183               top = top_obj + oop(top_obj)->size();
 184               jbyte* top_card = CardTableModRefBS::byte_for(top - 1);   // top is exclusive
 185               if (top_card > first_clean_card) {
 186                 // object ends a different card
 187                 current_card = top_card + 1;
 188                 if (card_is_clean(*top_card)) {
 189                   // the ending card is clean, we are done
 190                   first_clean_card = top_card;
 191                 } else {
 192                   // the ending card is not clean, continue scanning at start of do-while
 193                   restart_scanning = true;
 194                 }
 195               } else {
 196                 // object ends on the clean card, we are done.
 197                 assert(first_clean_card == top_card, "just checking");
 198               }
 199             }
 200           }
 201         } while (restart_scanning);
 202         // we know which cards to scan, now clear them
 203         while (first_nonclean_card < first_clean_card) {
 204           *first_nonclean_card++ = clean_card;
 205         }
 206         // scan oops in objects
 207         // hoisted the if (depth_first) check out of the loop
 208         if (depth_first){ 
 209           do {
 210             oop(bottom_obj)->push_contents(pm);
 211             bottom_obj += oop(bottom_obj)->size();
 212             assert(bottom_obj <= sp_top, "just checking");
 213           } while (bottom_obj < top);
 214           pm->drain_stacks_cond_depth();
 215         } else {
 216           do {
 217             oop(bottom_obj)->copy_contents(pm);
 218             bottom_obj += oop(bottom_obj)->size();
 219             assert(bottom_obj <= sp_top, "just checking");
 220           } while (bottom_obj < top);
 221         }
 222         // remember top oop* scanned
 223         prev_top = top;
 224       }
 225     }
 226   }
 227 }
 228 
 229 void CardTableExtension::scavenge_contents_parallel(ObjectStartArray* start_array,
 230                                                     MutableSpace* sp,
 231                                                     HeapWord* space_top,
 232                                                     PSPromotionManager* pm,
 233                                                     uint stripe_number) {
 234   int ssize = 128; // Naked constant!  Work unit = 64k.
 235   int dirty_card_count = 0;
 236   bool depth_first = pm->depth_first();
 237 
 238   oop* sp_top = (oop*)space_top;
 239   jbyte* start_card = byte_for(sp->bottom());
 240   jbyte* end_card   = byte_for(sp_top - 1) + 1;
 241   oop* last_scanned = NULL; // Prevent scanning objects more than once
 242   for (jbyte* slice = start_card; slice < end_card; slice += ssize*ParallelGCThreads) {
 243     jbyte* worker_start_card = slice + stripe_number * ssize;
 244     if (worker_start_card >= end_card)
 245       return; // We're done.
 246 
 247     jbyte* worker_end_card = worker_start_card + ssize;
 248     if (worker_end_card > end_card)
 249       worker_end_card = end_card;
 250 
 251     // We do not want to scan objects more than once. In order to accomplish
 252     // this, we assert that any object with an object head inside our 'slice'
 253     // belongs to us. We may need to extend the range of scanned cards if the
 254     // last object continues into the next 'slice'.
 255     //
 256     // Note! ending cards are exclusive!
 257     HeapWord* slice_start = addr_for(worker_start_card);
 258     HeapWord* slice_end = MIN2((HeapWord*) sp_top, addr_for(worker_end_card));
 259 
 260     // If there are not objects starting within the chunk, skip it.
 261     if (!start_array->object_starts_in_range(slice_start, slice_end)) {
 262       continue;
 263     }
 264     // Update our begining addr
 265     HeapWord* first_object = start_array->object_start(slice_start);
 266     debug_only(oop* first_object_within_slice = (oop*) first_object;)
 267     if (first_object < slice_start) {
 268       last_scanned = (oop*)(first_object + oop(first_object)->size());
 269       debug_only(first_object_within_slice = last_scanned;)
 270       worker_start_card = byte_for(last_scanned);
 271     }
 272 
 273     // Update the ending addr
 274     if (slice_end < (HeapWord*)sp_top) {
 275       // The subtraction is important! An object may start precisely at slice_end.
 276       HeapWord* last_object = start_array->object_start(slice_end - 1);
 277       slice_end = last_object + oop(last_object)->size();
 278       // worker_end_card is exclusive, so bump it one past the end of last_object's
 279       // covered span.
 280       worker_end_card = byte_for(slice_end) + 1;
 281 
 282       if (worker_end_card > end_card)
 283         worker_end_card = end_card;
 284     }
 285 
 286     assert(slice_end <= (HeapWord*)sp_top, "Last object in slice crosses space boundary");
 287     assert(is_valid_card_address(worker_start_card), "Invalid worker start card");
 288     assert(is_valid_card_address(worker_end_card), "Invalid worker end card");
 289     // Note that worker_start_card >= worker_end_card is legal, and happens when
 290     // an object spans an entire slice.
 291     assert(worker_start_card <= end_card, "worker start card beyond end card");
 292     assert(worker_end_card <= end_card, "worker end card beyond end card");
 293 
 294     jbyte* current_card = worker_start_card;
 295     while (current_card < worker_end_card) {
 296       // Find an unclean card.
 297       while (current_card < worker_end_card && card_is_clean(*current_card)) {
 298         current_card++;
 299       }
 300       jbyte* first_unclean_card = current_card;
 301 
 302       // Find the end of a run of contiguous unclean cards
 303       while (current_card < worker_end_card && !card_is_clean(*current_card)) {
 304         while (current_card < worker_end_card && !card_is_clean(*current_card)) {
 305           current_card++;
 306         }
 307 
 308         if (current_card < worker_end_card) {
 309           // Some objects may be large enough to span several cards. If such
 310           // an object has more than one dirty card, separated by a clean card,
 311           // we will attempt to scan it twice. The test against "last_scanned"
 312           // prevents the redundant object scan, but it does not prevent newly
 313           // marked cards from being cleaned.
 314           HeapWord* last_object_in_dirty_region = start_array->object_start(addr_for(current_card)-1);
 315           size_t size_of_last_object = oop(last_object_in_dirty_region)->size();
 316           HeapWord* end_of_last_object = last_object_in_dirty_region + size_of_last_object;
 317           jbyte* ending_card_of_last_object = byte_for(end_of_last_object);
 318           assert(ending_card_of_last_object <= worker_end_card, "ending_card_of_last_object is greater than worker_end_card");
 319           if (ending_card_of_last_object > current_card) {
 320             // This means the object spans the next complete card.
 321             // We need to bump the current_card to ending_card_of_last_object
 322             current_card = ending_card_of_last_object;
 323           }
 324         }
 325       }
 326       jbyte* following_clean_card = current_card;
 327 
 328       if (first_unclean_card < worker_end_card) {
 329         oop* p = (oop*) start_array->object_start(addr_for(first_unclean_card));
 330         assert((HeapWord*)p <= addr_for(first_unclean_card), "checking");
 331         // "p" should always be >= "last_scanned" because newly GC dirtied
 332         // cards are no longer scanned again (see comment at end
 333         // of loop on the increment of "current_card").  Test that
 334         // hypothesis before removing this code.
 335         // If this code is removed, deal with the first time through
 336         // the loop when the last_scanned is the object starting in
 337         // the previous slice.
 338         assert((p >= last_scanned) || 
 339                (last_scanned == first_object_within_slice),
 340                "Should no longer be possible");
 341         if (p < last_scanned) {   
 342           // Avoid scanning more than once; this can happen because
 343           // newgen cards set by GC may a different set than the
 344           // originally dirty set
 345           p = last_scanned;
 346         }
 347         oop* to = (oop*)addr_for(following_clean_card);
 348 
 349         // Test slice_end first!
 350         if ((HeapWord*)to > slice_end) {
 351           to = (oop*)slice_end;
 352         } else if (to > sp_top) {
 353           to = sp_top;
 354         } 
 355 
 356         // we know which cards to scan, now clear them
 357         if (first_unclean_card <= worker_start_card+1)
 358           first_unclean_card = worker_start_card+1;
 359         if (following_clean_card >= worker_end_card-1)
 360           following_clean_card = worker_end_card-1;
 361 
 362         while (first_unclean_card < following_clean_card) {
 363           *first_unclean_card++ = clean_card;
 364         }
 365 
 366         const int interval = PrefetchScanIntervalInBytes;
 367         // scan all objects in the range
 368         if (interval != 0) {
 369           // hoisted the if (depth_first) check out of the loop
 370           if (depth_first) {
 371             while (p < to) {
 372               Prefetch::write(p, interval);
 373               oop m = oop(p);
 374               assert(m->is_oop_or_null(), "check for header");
 375               m->push_contents(pm); 
 376               p += m->size();
 377             }
 378             pm->drain_stacks_cond_depth();
 379           } else {
 380             while (p < to) {
 381               Prefetch::write(p, interval);
 382               oop m = oop(p);
 383               assert(m->is_oop_or_null(), "check for header");
 384               m->copy_contents(pm); 
 385               p += m->size();
 386             }
 387           }
 388         } else {
 389           // hoisted the if (depth_first) check out of the loop
 390           if (depth_first) {
 391             while (p < to) {
 392               oop m = oop(p);
 393               assert(m->is_oop_or_null(), "check for header");
 394               m->push_contents(pm);
 395               p += m->size();
 396             }
 397             pm->drain_stacks_cond_depth();
 398           } else {
 399             while (p < to) {
 400               oop m = oop(p);
 401               assert(m->is_oop_or_null(), "check for header");
 402               m->copy_contents(pm);
 403               p += m->size();
 404             }
 405           }
 406         }
 407         last_scanned = p;
 408       }
 409       // "current_card" is still the "following_clean_card" or
 410       // the current_card is >= the worker_end_card so the
 411       // loop will not execute again.
 412       assert((current_card == following_clean_card) ||
 413              (current_card >= worker_end_card),
 414         "current_card should only be incremented if it still equals "
 415         "following_clean_card");
 416       // Increment current_card so that it is not processed again.
 417       // It may now be dirty because a old-to-young pointer was
 418       // found on it an updated.  If it is now dirty, it cannot be
 419       // be safely cleaned in the next iteration.
 420       current_card++;
 421     }
 422   }
 423 }
 424 
 425 // This should be called before a scavenge.
 426 void CardTableExtension::verify_all_young_refs_imprecise() {
 427   CheckForUnmarkedObjects check;
 428   
 429   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 430   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 431 
 432   PSOldGen* old_gen = heap->old_gen();
 433   PSPermGen* perm_gen = heap->perm_gen();
 434 
 435   old_gen->object_iterate(&check);
 436   perm_gen->object_iterate(&check);
 437 }
 438 
 439 // This should be called immediately after a scavenge, before mutators resume.
 440 void CardTableExtension::verify_all_young_refs_precise() {
 441   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 442   assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity");
 443 
 444   PSOldGen* old_gen = heap->old_gen();
 445   PSPermGen* perm_gen = heap->perm_gen();
 446 
 447   CheckForPreciseMarks check(heap->young_gen(), (CardTableExtension*)heap->barrier_set());
 448 
 449   old_gen->oop_iterate(&check);
 450   perm_gen->oop_iterate(&check);
 451 
 452   verify_all_young_refs_precise_helper(old_gen->object_space()->used_region());
 453   verify_all_young_refs_precise_helper(perm_gen->object_space()->used_region());
 454 }
 455 
 456 void CardTableExtension::verify_all_young_refs_precise_helper(MemRegion mr) {
 457   CardTableExtension* card_table = (CardTableExtension*)Universe::heap()->barrier_set();
 458   // FIX ME ASSERT HERE
 459 
 460   jbyte* bot = card_table->byte_for(mr.start());
 461   jbyte* top = card_table->byte_for(mr.end());
 462   while(bot <= top) {
 463     assert(*bot == clean_card || *bot == verify_card, "Found unwanted or unknown card mark");
 464     if (*bot == verify_card)
 465       *bot = youngergen_card;
 466     bot++;
 467   }
 468 }
 469 
 470 bool CardTableExtension::addr_is_marked_imprecise(void *addr) {
 471   jbyte* p = byte_for(addr);
 472   jbyte val = *p;
 473 
 474   if (card_is_dirty(val))
 475     return true;
 476 
 477   if (card_is_newgen(val))
 478     return true;
 479 
 480   if (card_is_clean(val))
 481     return false;
 482 
 483   assert(false, "Found unhandled card mark type");
 484 
 485   return false;
 486 }
 487 
 488 // Also includes verify_card
 489 bool CardTableExtension::addr_is_marked_precise(void *addr) {
 490   jbyte* p = byte_for(addr);
 491   jbyte val = *p;
 492 
 493   if (card_is_newgen(val))
 494     return true;
 495 
 496   if (card_is_verify(val))
 497     return true;
 498 
 499   if (card_is_clean(val))
 500     return false;
 501 
 502   if (card_is_dirty(val))
 503     return false;
 504 
 505   assert(false, "Found unhandled card mark type");
 506 
 507   return false;
 508 }
 509 
 510 // Assumes that only the base or the end changes.  This allows indentification
 511 // of the region that is being resized.  The 
 512 // CardTableModRefBS::resize_covered_region() is used for the normal case
 513 // where the covered regions are growing or shrinking at the high end.
 514 // The method resize_covered_region_by_end() is analogous to 
 515 // CardTableModRefBS::resize_covered_region() but
 516 // for regions that grow or shrink at the low end.
 517 void CardTableExtension::resize_covered_region(MemRegion new_region) {
 518 
 519   for (int i = 0; i < _cur_covered_regions; i++) {
 520     if (_covered[i].start() == new_region.start()) {
 521       // Found a covered region with the same start as the
 522       // new region.  The region is growing or shrinking
 523       // from the start of the region.
 524       resize_covered_region_by_start(new_region);
 525       return;
 526     }
 527     if (_covered[i].start() > new_region.start()) {
 528       break;
 529     }
 530   }
 531 
 532   int changed_region = -1;
 533   for (int j = 0; j < _cur_covered_regions; j++) {
 534     if (_covered[j].end() == new_region.end()) {
 535       changed_region = j;
 536       // This is a case where the covered region is growing or shrinking
 537       // at the start of the region.
 538       assert(changed_region != -1, "Don't expect to add a covered region");
 539       assert(_covered[changed_region].byte_size() != new_region.byte_size(), 
 540         "The sizes should be different here");
 541       resize_covered_region_by_end(changed_region, new_region);
 542       return;
 543     }
 544   }
 545   // This should only be a new covered region (where no existing
 546   // covered region matches at the start or the end).
 547   assert(_cur_covered_regions < _max_covered_regions, 
 548     "An existing region should have been found");
 549   resize_covered_region_by_start(new_region);
 550 }
 551 
 552 void CardTableExtension::resize_covered_region_by_start(MemRegion new_region) {
 553   CardTableModRefBS::resize_covered_region(new_region);
 554   debug_only(verify_guard();)
 555 }
 556 
 557 void CardTableExtension::resize_covered_region_by_end(int changed_region,
 558                                                       MemRegion new_region) {
 559   assert(SafepointSynchronize::is_at_safepoint(), 
 560     "Only expect an expansion at the low end at a GC");
 561   debug_only(verify_guard();)
 562 #ifdef ASSERT
 563   for (int k = 0; k < _cur_covered_regions; k++) {
 564     if (_covered[k].end() == new_region.end()) {
 565       assert(changed_region == k, "Changed region is incorrect");
 566       break;
 567     }
 568   }
 569 #endif
 570 
 571   // Commit new or uncommit old pages, if necessary.
 572   resize_commit_uncommit(changed_region, new_region);
 573 
 574   // Update card table entries
 575   resize_update_card_table_entries(changed_region, new_region);
 576 
 577   // Set the new start of the committed region
 578   resize_update_committed_table(changed_region, new_region);
 579 
 580   // Update the covered region
 581   resize_update_covered_table(changed_region, new_region);
 582 
 583   if (TraceCardTableModRefBS) {
 584     int ind = changed_region;
 585     gclog_or_tty->print_cr("CardTableModRefBS::resize_covered_region: ");
 586     gclog_or_tty->print_cr("  "
 587                   "  _covered[%d].start(): " INTPTR_FORMAT
 588                   "  _covered[%d].last(): " INTPTR_FORMAT,
 589                   ind, _covered[ind].start(),
 590                   ind, _covered[ind].last());
 591     gclog_or_tty->print_cr("  "
 592                   "  _committed[%d].start(): " INTPTR_FORMAT
 593                   "  _committed[%d].last(): " INTPTR_FORMAT,
 594                   ind, _committed[ind].start(),
 595                   ind, _committed[ind].last());
 596     gclog_or_tty->print_cr("  "
 597                   "  byte_for(start): " INTPTR_FORMAT
 598                   "  byte_for(last): " INTPTR_FORMAT,
 599                   byte_for(_covered[ind].start()),
 600                   byte_for(_covered[ind].last()));
 601     gclog_or_tty->print_cr("  "
 602                   "  addr_for(start): " INTPTR_FORMAT
 603                   "  addr_for(last): " INTPTR_FORMAT,
 604                   addr_for((jbyte*) _committed[ind].start()),
 605                   addr_for((jbyte*) _committed[ind].last()));
 606   }
 607   debug_only(verify_guard();)
 608 }
 609 
 610 void CardTableExtension::resize_commit_uncommit(int changed_region,
 611                                                 MemRegion new_region) {
 612   // Commit new or uncommit old pages, if necessary.
 613   MemRegion cur_committed = _committed[changed_region];
 614   assert(_covered[changed_region].end() == new_region.end(), 
 615     "The ends of the regions are expected to match");
 616   // Extend the start of this _committed region to
 617   // to cover the start of any previous _committed region.
 618   // This forms overlapping regions, but never interior regions.
 619   HeapWord* min_prev_start = lowest_prev_committed_start(changed_region);
 620   if (min_prev_start < cur_committed.start()) {
 621     // Only really need to set start of "cur_committed" to
 622     // the new start (min_prev_start) but assertion checking code
 623     // below use cur_committed.end() so make it correct.
 624     MemRegion new_committed = 
 625         MemRegion(min_prev_start, cur_committed.end());
 626     cur_committed = new_committed;
 627   }
 628 #ifdef ASSERT
 629   ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap();
 630   assert(cur_committed.start() == 
 631     (HeapWord*) align_size_up((uintptr_t) cur_committed.start(), 
 632                               os::vm_page_size()),
 633     "Starts should have proper alignment");
 634 #endif
 635 
 636   jbyte* new_start = byte_for(new_region.start());
 637   // Round down because this is for the start address
 638   HeapWord* new_start_aligned =
 639     (HeapWord*)align_size_down((uintptr_t)new_start, os::vm_page_size());
 640   // The guard page is always committed and should not be committed over.
 641   // This method is used in cases where the generation is growing toward
 642   // lower addresses but the guard region is still at the end of the
 643   // card table.  That still makes sense when looking for writes
 644   // off the end of the card table.
 645   if (new_start_aligned < cur_committed.start()) {
 646     // Expand the committed region
 647     //
 648     // Case A
 649     //                                          |+ guard +|
 650     //                          |+ cur committed +++++++++|
 651     //                  |+ new committed +++++++++++++++++|
 652     //
 653     // Case B
 654     //                                          |+ guard +|
 655     //                        |+ cur committed +|
 656     //                  |+ new committed +++++++|
 657     //
 658     // These are not expected because the calculation of the
 659     // cur committed region and the new committed region
 660     // share the same end for the covered region.
 661     // Case C
 662     //                                          |+ guard +|
 663     //                        |+ cur committed +|
 664     //                  |+ new committed +++++++++++++++++|
 665     // Case D
 666     //                                          |+ guard +|
 667     //                        |+ cur committed +++++++++++|
 668     //                  |+ new committed +++++++|
 669 
 670     HeapWord* new_end_for_commit = 
 671       MIN2(cur_committed.end(), _guard_region.start());
 672     if(new_start_aligned < new_end_for_commit) {
 673       MemRegion new_committed =
 674         MemRegion(new_start_aligned, new_end_for_commit);
 675       if (!os::commit_memory((char*)new_committed.start(),
 676                              new_committed.byte_size())) {
 677         vm_exit_out_of_memory(new_committed.byte_size(),
 678                               "card table expansion");
 679       }
 680     }
 681   } else if (new_start_aligned > cur_committed.start()) {
 682     // Shrink the committed region
 683     MemRegion uncommit_region = committed_unique_to_self(changed_region, 
 684       MemRegion(cur_committed.start(), new_start_aligned));
 685     if (!uncommit_region.is_empty()) {
 686       if (!os::uncommit_memory((char*)uncommit_region.start(),
 687                                uncommit_region.byte_size())) {
 688         vm_exit_out_of_memory(uncommit_region.byte_size(),
 689           "card table contraction");
 690       }
 691     }
 692   }
 693   assert(_committed[changed_region].end() == cur_committed.end(),
 694     "end should not change");
 695 }
 696 
 697 void CardTableExtension::resize_update_committed_table(int changed_region,
 698                                                        MemRegion new_region) {
 699 
 700   jbyte* new_start = byte_for(new_region.start());
 701   // Set the new start of the committed region
 702   HeapWord* new_start_aligned =
 703     (HeapWord*)align_size_down((uintptr_t)new_start, 
 704                              os::vm_page_size());
 705   MemRegion new_committed = MemRegion(new_start_aligned, 
 706     _committed[changed_region].end());
 707   _committed[changed_region] = new_committed;
 708   _committed[changed_region].set_start(new_start_aligned);
 709 }
 710 
 711 void CardTableExtension::resize_update_card_table_entries(int changed_region,
 712                                                           MemRegion new_region) {
 713   debug_only(verify_guard();)
 714   MemRegion original_covered = _covered[changed_region];
 715   // Initialize the card entries.  Only consider the
 716   // region covered by the card table (_whole_heap)
 717   jbyte* entry;
 718   if (new_region.start() < _whole_heap.start()) {
 719     entry = byte_for(_whole_heap.start());
 720   } else {
 721     entry = byte_for(new_region.start());
 722   }
 723   jbyte* end = byte_for(original_covered.start());
 724   // If _whole_heap starts at the original covered regions start,
 725   // this loop will not execute.
 726   while (entry < end) { *entry++ = clean_card; }
 727 }
 728 
 729 void CardTableExtension::resize_update_covered_table(int changed_region,
 730                                                      MemRegion new_region) {
 731   // Update the covered region
 732   _covered[changed_region].set_start(new_region.start());
 733   _covered[changed_region].set_word_size(new_region.word_size());
 734 
 735   // reorder regions.  There should only be at most 1 out
 736   // of order.
 737   for (int i = _cur_covered_regions-1 ; i > 0; i--) {
 738     if (_covered[i].start() < _covered[i-1].start()) {
 739         MemRegion covered_mr = _covered[i-1];
 740         _covered[i-1] = _covered[i];
 741         _covered[i] = covered_mr;
 742         MemRegion committed_mr = _committed[i-1];
 743       _committed[i-1] = _committed[i];
 744       _committed[i] = committed_mr;
 745       break;
 746     }
 747   }
 748 #ifdef ASSERT
 749   for (int m = 0; m < _cur_covered_regions-1; m++) {
 750     assert(_covered[m].start() <= _covered[m+1].start(), 
 751       "Covered regions out of order");
 752     assert(_committed[m].start() <= _committed[m+1].start(), 
 753       "Committed regions out of order");
 754   }
 755 #endif
 756 }
 757 
 758 // Returns the start of any committed region that is lower than
 759 // the target committed region (index ind) and that intersects the
 760 // target region.  If none, return start of target region.
 761 //
 762 //      -------------
 763 //      |           |
 764 //      -------------
 765 //              ------------
 766 //              | target   |
 767 //              ------------
 768 //                               -------------
 769 //                               |           |
 770 //                               -------------
 771 //      ^ returns this
 772 //
 773 //      -------------
 774 //      |           |
 775 //      -------------
 776 //                      ------------
 777 //                      | target   |
 778 //                      ------------
 779 //                               -------------
 780 //                               |           |
 781 //                               -------------
 782 //                      ^ returns this
 783 
 784 HeapWord* CardTableExtension::lowest_prev_committed_start(int ind) const {
 785   assert(_cur_covered_regions >= 0, "Expecting at least on region");
 786   HeapWord* min_start = _committed[ind].start();
 787   for (int j = 0; j < ind; j++) {
 788     HeapWord* this_start = _committed[j].start();
 789     if ((this_start < min_start) && 
 790         !(_committed[j].intersection(_committed[ind])).is_empty()) {
 791        min_start = this_start;
 792     }
 793   }
 794   return min_start;
 795 }
 796