< prev index next >

src/share/vm/gc/g1/g1BlockOffsetTable.cpp

Print this page




  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/g1/g1BlockOffsetTable.inline.hpp"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc/g1/heapRegion.hpp"
  29 #include "gc/shared/space.hpp"
  30 #include "logging/log.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "runtime/java.hpp"
  33 #include "services/memTracker.hpp"
  34 
  35 
  36 
  37 //////////////////////////////////////////////////////////////////////
  38 // G1BlockOffsetSharedArray
  39 //////////////////////////////////////////////////////////////////////
  40 
  41 G1BlockOffsetSharedArray::G1BlockOffsetSharedArray(MemRegion heap, G1RegionToSpaceMapper* storage) :
  42   _reserved(), _end(NULL), _listener(), _offset_array(NULL) {
  43 
  44   _reserved = heap;
  45   _end = NULL;
  46 
  47   MemRegion bot_reserved = storage->reserved();
  48 
  49   _offset_array = (u_char*)bot_reserved.start();
  50   _end = _reserved.end();
  51 
  52   storage->set_mapping_changed_listener(&_listener);
  53 
  54   log_trace(gc, bot)("G1BlockOffsetSharedArray::G1BlockOffsetSharedArray: ");
  55   log_trace(gc, bot)("    rs.base(): " PTR_FORMAT "  rs.size(): " SIZE_FORMAT "  rs end(): " PTR_FORMAT,
  56                      p2i(bot_reserved.start()), bot_reserved.byte_size(), p2i(bot_reserved.end()));
  57 }
  58 
  59 bool G1BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {
  60   assert(p >= _reserved.start(), "just checking");
  61   size_t delta = pointer_delta(p, _reserved.start());
  62   return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;
  63 }
  64 
  65 #ifdef ASSERT
  66 void G1BlockOffsetSharedArray::check_index(size_t index, const char* msg) const {
  67   assert((index) < (_reserved.word_size() >> LogN_words),
  68          "%s - index: " SIZE_FORMAT ", _vs.committed_size: " SIZE_FORMAT,
  69          msg, (index), (_reserved.word_size() >> LogN_words));
  70   assert(G1CollectedHeap::heap()->is_in_exact(address_for_index_raw(index)),
  71          "Index " SIZE_FORMAT " corresponding to " PTR_FORMAT
  72          " (%u) is not in committed area.",
  73          (index),
  74          p2i(address_for_index_raw(index)),
  75          G1CollectedHeap::heap()->addr_to_region(address_for_index_raw(index)));
  76 }
  77 #endif // ASSERT
  78 
  79 //////////////////////////////////////////////////////////////////////
  80 // G1BlockOffsetArray
  81 //////////////////////////////////////////////////////////////////////
  82 
  83 G1BlockOffsetArray::G1BlockOffsetArray(G1BlockOffsetSharedArray* array,
  84                                        MemRegion mr) :
  85   G1BlockOffsetTable(mr.start(), mr.end()),
  86   _unallocated_block(_bottom),
  87   _array(array), _gsp(NULL) {
  88   assert(_bottom <= _end, "arguments out of order");
  89 }
  90 
  91 void G1BlockOffsetArray::set_space(G1OffsetTableContigSpace* sp) {
  92   _gsp = sp;
  93 }
  94 
  95 // The arguments follow the normal convention of denoting
  96 // a right-open interval: [start, end)
  97 void
  98 G1BlockOffsetArray:: set_remainder_to_point_to_start(HeapWord* start, HeapWord* end) {
  99 
 100   if (start >= end) {
 101     // The start address is equal to the end address (or to
 102     // the right of the end address) so there are not cards
 103     // that need to be updated..
 104     return;
 105   }
 106 
 107   // Write the backskip value for each region.
 108   //
 109   //    offset
 110   //    card             2nd                       3rd
 111   //     | +- 1st        |                         |
 112   //     v v             v                         v
 113   //    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+-+-+-
 114   //    |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ...
 115   //    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+-+-+-
 116   //    11              19                        75
 117   //      12
 118   //


 120   //      x - offset value of offset card
 121   //    1st - start of first logarithmic region
 122   //      0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1
 123   //    2nd - start of second logarithmic region
 124   //      1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8
 125   //    3rd - start of third logarithmic region
 126   //      2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64
 127   //
 128   //    integer below the block offset entry is an example of
 129   //    the index of the entry
 130   //
 131   //    Given an address,
 132   //      Find the index for the address
 133   //      Find the block offset table entry
 134   //      Convert the entry to a back slide
 135   //        (e.g., with today's, offset = 0x81 =>
 136   //          back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8
 137   //      Move back N (e.g., 8) entries and repeat with the
 138   //        value of the new entry
 139   //
 140   size_t start_card = _array->index_for(start);
 141   size_t end_card = _array->index_for(end-1);
 142   assert(start ==_array->address_for_index(start_card), "Precondition");
 143   assert(end ==_array->address_for_index(end_card)+N_words, "Precondition");
 144   set_remainder_to_point_to_start_incl(start_card, end_card); // closed interval
 145 }
 146 
 147 // Unlike the normal convention in this code, the argument here denotes
 148 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()
 149 // above.
 150 void
 151 G1BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card) {
 152   if (start_card > end_card) {
 153     return;
 154   }
 155   assert(start_card > _array->index_for(_bottom), "Cannot be first card");
 156   assert(_array->offset_array(start_card-1) <= N_words,
 157          "Offset card has an unexpected value");
 158   size_t start_card_for_region = start_card;
 159   u_char offset = max_jubyte;
 160   for (int i = 0; i < BlockOffsetArray::N_powers; i++) {
 161     // -1 so that the the card with the actual offset is counted.  Another -1
 162     // so that the reach ends in this region and not at the start
 163     // of the next.
 164     size_t reach = start_card - 1 + (BlockOffsetArray::power_to_cards_back(i+1) - 1);
 165     offset = N_words + i;
 166     if (reach >= end_card) {
 167       _array->set_offset_array(start_card_for_region, end_card, offset);
 168       start_card_for_region = reach + 1;
 169       break;
 170     }
 171     _array->set_offset_array(start_card_for_region, reach, offset);
 172     start_card_for_region = reach + 1;
 173   }
 174   assert(start_card_for_region > end_card, "Sanity check");
 175   DEBUG_ONLY(check_all_cards(start_card, end_card);)
 176 }
 177 
 178 // The card-interval [start_card, end_card] is a closed interval; this
 179 // is an expensive check -- use with care and only under protection of
 180 // suitable flag.
 181 void G1BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {
 182 
 183   if (end_card < start_card) {
 184     return;
 185   }
 186   guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card");
 187   for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {
 188     u_char entry = _array->offset_array(c);
 189     if (c - start_card > BlockOffsetArray::power_to_cards_back(1)) {
 190       guarantee(entry > N_words,
 191                 "Should be in logarithmic region - "
 192                 "entry: %u, "
 193                 "_array->offset_array(c): %u, "
 194                 "N_words: %u",
 195                 (uint)entry, (uint)_array->offset_array(c), (uint)N_words);
 196     }
 197     size_t backskip = BlockOffsetArray::entry_to_cards_back(entry);
 198     size_t landing_card = c - backskip;
 199     guarantee(landing_card >= (start_card - 1), "Inv");
 200     if (landing_card >= start_card) {
 201       guarantee(_array->offset_array(landing_card) <= entry,
 202                 "Monotonicity - landing_card offset: %u, "
 203                 "entry: %u",
 204                 (uint)_array->offset_array(landing_card), (uint)entry);
 205     } else {
 206       guarantee(landing_card == start_card - 1, "Tautology");
 207       // Note that N_words is the maximum offset value
 208       guarantee(_array->offset_array(landing_card) <= N_words,
 209                 "landing card offset: %u, "
 210                 "N_words: %u",
 211                 (uint)_array->offset_array(landing_card), (uint)N_words);
 212     }
 213   }
 214 }
 215 
 216 HeapWord* G1BlockOffsetArray::block_start_unsafe(const void* addr) {
 217   assert(_bottom <= addr && addr < _end,
 218          "addr must be covered by this Array");
 219   // Must read this exactly once because it can be modified by parallel
 220   // allocation.
 221   HeapWord* ub = _unallocated_block;
 222   if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
 223     assert(ub < _end, "tautology (see above)");
 224     return ub;
 225   }
 226   // Otherwise, find the block start using the table.
 227   HeapWord* q = block_at_or_preceding(addr, false, 0);
 228   return forward_to_block_containing_addr(q, addr);
 229 }
 230 
 231 // This duplicates a little code from the above: unavoidable.
 232 HeapWord*
 233 G1BlockOffsetArray::block_start_unsafe_const(const void* addr) const {
 234   assert(_bottom <= addr && addr < _end,
 235          "addr must be covered by this Array");
 236   // Must read this exactly once because it can be modified by parallel
 237   // allocation.
 238   HeapWord* ub = _unallocated_block;
 239   if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
 240     assert(ub < _end, "tautology (see above)");
 241     return ub;
 242   }
 243   // Otherwise, find the block start using the table.
 244   HeapWord* q = block_at_or_preceding(addr, false, 0);
 245   HeapWord* n = q + block_size(q);
 246   return forward_to_block_containing_addr_const(q, n, addr);
 247 }
 248 
 249 
 250 HeapWord*
 251 G1BlockOffsetArray::forward_to_block_containing_addr_slow(HeapWord* q,
 252                                                           HeapWord* n,
 253                                                           const void* addr) {
 254   // We're not in the normal case.  We need to handle an important subcase
 255   // here: LAB allocation.  An allocation previously recorded in the
 256   // offset table was actually a lab allocation, and was divided into
 257   // several objects subsequently.  Fix this situation as we answer the
 258   // query, by updating entries as we cross them.
 259 
 260   // If the fist object's end q is at the card boundary. Start refining
 261   // with the corresponding card (the value of the entry will be basically
 262   // set to 0). If the object crosses the boundary -- start from the next card.
 263   size_t n_index = _array->index_for(n);
 264   size_t next_index = _array->index_for(n) + !_array->is_card_boundary(n);
 265   // Calculate a consistent next boundary.  If "n" is not at the boundary
 266   // already, step to the boundary.
 267   HeapWord* next_boundary = _array->address_for_index(n_index) +
 268                             (n_index == next_index ? 0 : N_words);
 269   assert(next_boundary <= _array->_end,
 270          "next_boundary is beyond the end of the covered region "
 271          " next_boundary " PTR_FORMAT " _array->_end " PTR_FORMAT,
 272          p2i(next_boundary), p2i(_array->_end));
 273   if (addr >= gsp()->top()) return gsp()->top();
 274   while (next_boundary < addr) {
 275     while (n <= next_boundary) {
 276       q = n;
 277       oop obj = oop(q);
 278       if (obj->klass_or_null() == NULL) return q;
 279       n += block_size(q);
 280     }
 281     assert(q <= next_boundary && n > next_boundary, "Consequence of loop");
 282     // [q, n) is the block that crosses the boundary.
 283     alloc_block_work2(&next_boundary, &next_index, q, n);
 284   }
 285   return forward_to_block_containing_addr_const(q, n, addr);
 286 }
 287 
 288 // Note that the committed size of the covered space may have changed,
 289 // so the table size might also wish to change.
 290 void G1BlockOffsetArray::resize(size_t new_word_size) {
 291   HeapWord* new_end = _bottom + new_word_size;
 292   _end = new_end;  // update _end
 293 }
 294 
 295 //
 296 //              threshold_
 297 //              |   _index_
 298 //              v   v
 299 //      +-------+-------+-------+-------+-------+
 300 //      | i-1   |   i   | i+1   | i+2   | i+3   |
 301 //      +-------+-------+-------+-------+-------+
 302 //       ( ^    ]
 303 //         block-start
 304 //
 305 void G1BlockOffsetArray::alloc_block_work2(HeapWord** threshold_, size_t* index_,
 306                                            HeapWord* blk_start, HeapWord* blk_end) {
 307   // For efficiency, do copy-in/copy-out.
 308   HeapWord* threshold = *threshold_;
 309   size_t    index = *index_;
 310 
 311   assert(blk_start != NULL && blk_end > blk_start,
 312          "phantom block");
 313   assert(blk_end > threshold, "should be past threshold");
 314   assert(blk_start <= threshold, "blk_start should be at or before threshold");
 315   assert(pointer_delta(threshold, blk_start) <= N_words,
 316          "offset should be <= BlockOffsetSharedArray::N");
 317   assert(G1CollectedHeap::heap()->is_in_reserved(blk_start),
 318          "reference must be into the heap");
 319   assert(G1CollectedHeap::heap()->is_in_reserved(blk_end-1),
 320          "limit must be within the heap");
 321   assert(threshold == _array->_reserved.start() + index*N_words,
 322          "index must agree with threshold");
 323 
 324   DEBUG_ONLY(size_t orig_index = index;)
 325 
 326   // Mark the card that holds the offset into the block.  Note
 327   // that _next_offset_index and _next_offset_threshold are not
 328   // updated until the end of this method.
 329   _array->set_offset_array(index, threshold, blk_start);
 330 
 331   // We need to now mark the subsequent cards that this blk spans.
 332 
 333   // Index of card on which blk ends.
 334   size_t end_index   = _array->index_for(blk_end - 1);
 335 
 336   // Are there more cards left to be updated?
 337   if (index + 1 <= end_index) {
 338     HeapWord* rem_st  = _array->address_for_index(index + 1);
 339     // Calculate rem_end this way because end_index
 340     // may be the last valid index in the covered region.
 341     HeapWord* rem_end = _array->address_for_index(end_index) +  N_words;
 342     set_remainder_to_point_to_start(rem_st, rem_end);
 343   }
 344 
 345   index = end_index + 1;
 346   // Calculate threshold_ this way because end_index
 347   // may be the last valid index in the covered region.
 348   threshold = _array->address_for_index(end_index) + N_words;
 349   assert(threshold >= blk_end, "Incorrect offset threshold");
 350 
 351   // index_ and threshold_ updated here.
 352   *threshold_ = threshold;
 353   *index_ = index;
 354 
 355 #ifdef ASSERT
 356   // The offset can be 0 if the block starts on a boundary.  That
 357   // is checked by an assertion above.
 358   size_t start_index = _array->index_for(blk_start);
 359   HeapWord* boundary = _array->address_for_index(start_index);
 360   assert((_array->offset_array(orig_index) == 0 && blk_start == boundary) ||
 361          (_array->offset_array(orig_index) > 0 && _array->offset_array(orig_index) <= N_words),
 362          "offset array should have been set - "
 363          "orig_index offset: %u, "
 364          "blk_start: " PTR_FORMAT ", "
 365          "boundary: " PTR_FORMAT,
 366          (uint)_array->offset_array(orig_index),
 367          p2i(blk_start), p2i(boundary));
 368   for (size_t j = orig_index + 1; j <= end_index; j++) {
 369     assert(_array->offset_array(j) > 0 &&
 370            _array->offset_array(j) <=
 371              (u_char) (N_words+BlockOffsetArray::N_powers-1),
 372            "offset array should have been set - "
 373            "%u not > 0 OR %u not <= %u",
 374            (uint) _array->offset_array(j),
 375            (uint) _array->offset_array(j),
 376            (uint) (N_words+BlockOffsetArray::N_powers-1));
 377   }
 378 #endif
 379 }
 380 
 381 void G1BlockOffsetArray::verify() const {
 382   assert(gsp()->bottom() < gsp()->top(), "Only non-empty regions should be verified.");
 383   size_t start_card = _array->index_for(gsp()->bottom());
 384   size_t end_card = _array->index_for(gsp()->top() - 1);
 385 
 386   for (size_t current_card = start_card; current_card < end_card; current_card++) {
 387     u_char entry = _array->offset_array(current_card);
 388     if (entry < N_words) {
 389       // The entry should point to an object before the current card. Verify that
 390       // it is possible to walk from that object in to the current card by just
 391       // iterating over the objects following it.
 392       HeapWord* card_address = _array->address_for_index(current_card);
 393       HeapWord* obj_end = card_address - entry;
 394       while (obj_end < card_address) {
 395         HeapWord* obj = obj_end;
 396         size_t obj_size = block_size(obj);
 397         obj_end = obj + obj_size;
 398         guarantee(obj_end > obj && obj_end <= gsp()->top(),
 399                   "Invalid object end. obj: " PTR_FORMAT " obj_size: " SIZE_FORMAT " obj_end: " PTR_FORMAT " top: " PTR_FORMAT,
 400                   p2i(obj), obj_size, p2i(obj_end), p2i(gsp()->top()));
 401       }
 402     } else {
 403       // Because we refine the BOT based on which cards are dirty there is not much we can verify here.
 404       // We need to make sure that we are going backwards and that we don't pass the start of the
 405       // corresponding heap region. But that is about all we can verify.
 406       size_t backskip = BlockOffsetArray::entry_to_cards_back(entry);
 407       guarantee(backskip >= 1, "Must be going back at least one card.");
 408 
 409       size_t max_backskip = current_card - start_card;
 410       guarantee(backskip <= max_backskip,
 411                 "Going backwards beyond the start_card. start_card: " SIZE_FORMAT " current_card: " SIZE_FORMAT " backskip: " SIZE_FORMAT,
 412                 start_card, current_card, backskip);
 413 
 414       HeapWord* backskip_address = _array->address_for_index(current_card - backskip);
 415       guarantee(backskip_address >= gsp()->bottom(),
 416                 "Going backwards beyond bottom of the region: bottom: " PTR_FORMAT ", backskip_address: " PTR_FORMAT,
 417                 p2i(gsp()->bottom()), p2i(backskip_address));
 418     }
 419   }
 420 }
 421 
 422 #ifndef PRODUCT
 423 void
 424 G1BlockOffsetArray::print_on(outputStream* out) {
 425   size_t from_index = _array->index_for(_bottom);
 426   size_t to_index = _array->index_for(_end);
 427   out->print_cr(">> BOT for area [" PTR_FORMAT "," PTR_FORMAT ") "
 428                 "cards [" SIZE_FORMAT "," SIZE_FORMAT ")",
 429                 p2i(_bottom), p2i(_end), from_index, to_index);
 430   for (size_t i = from_index; i < to_index; ++i) {
 431     out->print_cr("  entry " SIZE_FORMAT_W(8) " | " PTR_FORMAT " : %3u",
 432                   i, p2i(_array->address_for_index(i)),
 433                   (uint) _array->offset_array(i));
 434   }


 435 }
 436 #endif // !PRODUCT
 437 
 438 //////////////////////////////////////////////////////////////////////
 439 // G1BlockOffsetArrayContigSpace
 440 //////////////////////////////////////////////////////////////////////
 441 
 442 HeapWord*
 443 G1BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) {
 444   assert(_bottom <= addr && addr < _end,
 445          "addr must be covered by this Array");
 446   HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);
 447   return forward_to_block_containing_addr(q, addr);
 448 }
 449 
 450 HeapWord*
 451 G1BlockOffsetArrayContigSpace::
 452 block_start_unsafe_const(const void* addr) const {
 453   assert(_bottom <= addr && addr < _end,
 454          "addr must be covered by this Array");
 455   HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);
 456   HeapWord* n = q + block_size(q);
 457   return forward_to_block_containing_addr_const(q, n, addr);
 458 }
 459 
 460 G1BlockOffsetArrayContigSpace::
 461 G1BlockOffsetArrayContigSpace(G1BlockOffsetSharedArray* array,
 462                               MemRegion mr) :
 463   G1BlockOffsetArray(array, mr)
 464 {
 465   _next_offset_threshold = NULL;
 466   _next_offset_index = 0;
 467 }
 468 
 469 HeapWord* G1BlockOffsetArrayContigSpace::initialize_threshold_raw() {
 470   assert(!G1CollectedHeap::heap()->is_in_reserved(_array->_offset_array),
 471          "just checking");
 472   _next_offset_index = _array->index_for_raw(_bottom);
 473   _next_offset_index++;
 474   _next_offset_threshold =
 475     _array->address_for_index_raw(_next_offset_index);
 476   return _next_offset_threshold;
 477 }
 478 
 479 void G1BlockOffsetArrayContigSpace::zero_bottom_entry_raw() {
 480   assert(!G1CollectedHeap::heap()->is_in_reserved(_array->_offset_array),
 481          "just checking");
 482   size_t bottom_index = _array->index_for_raw(_bottom);
 483   assert(_array->address_for_index_raw(bottom_index) == _bottom,
 484          "Precondition of call");
 485   _array->set_offset_array_raw(bottom_index, 0);
 486 }
 487 
 488 HeapWord* G1BlockOffsetArrayContigSpace::initialize_threshold() {
 489   assert(!G1CollectedHeap::heap()->is_in_reserved(_array->_offset_array),
 490          "just checking");
 491   _next_offset_index = _array->index_for(_bottom);
 492   _next_offset_index++;
 493   _next_offset_threshold =
 494     _array->address_for_index(_next_offset_index);
 495   return _next_offset_threshold;
 496 }
 497 
 498 void G1BlockOffsetArrayContigSpace::set_for_starts_humongous(HeapWord* obj_top, size_t fill_size) {
 499   // The first BOT entry should have offset 0.
 500   reset_bot();
 501   alloc_block(_bottom, obj_top);
 502   if (fill_size > 0) {
 503     alloc_block(obj_top, fill_size);
 504   }
 505 }
 506 
 507 #ifndef PRODUCT
 508 void G1BlockOffsetArrayContigSpace::print_on(outputStream* out) {
 509   G1BlockOffsetArray::print_on(out);
 510   out->print_cr("  next offset threshold: " PTR_FORMAT, p2i(_next_offset_threshold));
 511   out->print_cr("  next offset index:     " SIZE_FORMAT, _next_offset_index);
 512 }
 513 #endif // !PRODUCT


  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/g1/g1BlockOffsetTable.inline.hpp"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc/g1/heapRegion.hpp"
  29 #include "gc/shared/space.hpp"
  30 #include "logging/log.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "runtime/java.hpp"
  33 #include "services/memTracker.hpp"
  34 
  35 
  36 
  37 //////////////////////////////////////////////////////////////////////
  38 // G1BlockOffsetTable
  39 //////////////////////////////////////////////////////////////////////
  40 
  41 G1BlockOffsetTable::G1BlockOffsetTable(MemRegion heap, G1RegionToSpaceMapper* storage) :
  42   _reserved(heap), _offset_array(NULL) {



  43 
  44   MemRegion bot_reserved = storage->reserved();
  45 
  46   _offset_array = (u_char*)bot_reserved.start();



  47 
  48   log_trace(gc, bot)("G1BlockOffsetTable::G1BlockOffsetTable: ");
  49   log_trace(gc, bot)("    rs.base(): " PTR_FORMAT "  rs.size(): " SIZE_FORMAT "  rs end(): " PTR_FORMAT,
  50                      p2i(bot_reserved.start()), bot_reserved.byte_size(), p2i(bot_reserved.end()));
  51 }
  52 
  53 bool G1BlockOffsetTable::is_card_boundary(HeapWord* p) const {
  54   assert(p >= _reserved.start(), "just checking");
  55   size_t delta = pointer_delta(p, _reserved.start());
  56   return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;
  57 }
  58 
  59 #ifdef ASSERT
  60 void G1BlockOffsetTable::check_index(size_t index, const char* msg) const {
  61   assert((index) < (_reserved.word_size() >> LogN_words),
  62          "%s - index: " SIZE_FORMAT ", _vs.committed_size: " SIZE_FORMAT,
  63          msg, (index), (_reserved.word_size() >> LogN_words));
  64   assert(G1CollectedHeap::heap()->is_in_exact(address_for_index_raw(index)),
  65          "Index " SIZE_FORMAT " corresponding to " PTR_FORMAT
  66          " (%u) is not in committed area.",
  67          (index),
  68          p2i(address_for_index_raw(index)),
  69          G1CollectedHeap::heap()->addr_to_region(address_for_index_raw(index)));
  70 }
  71 #endif // ASSERT
  72 
  73 //////////////////////////////////////////////////////////////////////
  74 // G1BlockOffsetTablePart
  75 //////////////////////////////////////////////////////////////////////
  76 
  77 G1BlockOffsetTablePart::G1BlockOffsetTablePart(G1BlockOffsetTable* array, G1ContiguousSpace* gsp) :
  78   _bot(array),
  79   _space(gsp),
  80   _next_offset_threshold(NULL),
  81   _next_offset_index(0)
  82 { }





  83 
  84 // The arguments follow the normal convention of denoting
  85 // a right-open interval: [start, end)
  86 void G1BlockOffsetTablePart:: set_remainder_to_point_to_start(HeapWord* start, HeapWord* end) {

  87 
  88   if (start >= end) {
  89     // The start address is equal to the end address (or to
  90     // the right of the end address) so there are not cards
  91     // that need to be updated..
  92     return;
  93   }
  94 
  95   // Write the backskip value for each region.
  96   //
  97   //    offset
  98   //    card             2nd                       3rd
  99   //     | +- 1st        |                         |
 100   //     v v             v                         v
 101   //    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+-+-+-
 102   //    |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ...
 103   //    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+     +-+-+-+-+-+-+-+-+-+-+-
 104   //    11              19                        75
 105   //      12
 106   //


 108   //      x - offset value of offset card
 109   //    1st - start of first logarithmic region
 110   //      0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1
 111   //    2nd - start of second logarithmic region
 112   //      1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8
 113   //    3rd - start of third logarithmic region
 114   //      2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64
 115   //
 116   //    integer below the block offset entry is an example of
 117   //    the index of the entry
 118   //
 119   //    Given an address,
 120   //      Find the index for the address
 121   //      Find the block offset table entry
 122   //      Convert the entry to a back slide
 123   //        (e.g., with today's, offset = 0x81 =>
 124   //          back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8
 125   //      Move back N (e.g., 8) entries and repeat with the
 126   //        value of the new entry
 127   //
 128   size_t start_card = _bot->index_for(start);
 129   size_t end_card = _bot->index_for(end-1);
 130   assert(start ==_bot->address_for_index(start_card), "Precondition");
 131   assert(end ==_bot->address_for_index(end_card)+N_words, "Precondition");
 132   set_remainder_to_point_to_start_incl(start_card, end_card); // closed interval
 133 }
 134 
 135 // Unlike the normal convention in this code, the argument here denotes
 136 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()
 137 // above.
 138 void G1BlockOffsetTablePart::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card) {

 139   if (start_card > end_card) {
 140     return;
 141   }
 142   assert(start_card > _bot->index_for(_space->bottom()), "Cannot be first card");
 143   assert(_bot->offset_array(start_card-1) <= N_words,
 144          "Offset card has an unexpected value");
 145   size_t start_card_for_region = start_card;
 146   u_char offset = max_jubyte;
 147   for (int i = 0; i < BlockOffsetArray::N_powers; i++) {
 148     // -1 so that the the card with the actual offset is counted.  Another -1
 149     // so that the reach ends in this region and not at the start
 150     // of the next.
 151     size_t reach = start_card - 1 + (BlockOffsetArray::power_to_cards_back(i+1) - 1);
 152     offset = N_words + i;
 153     if (reach >= end_card) {
 154       _bot->set_offset_array(start_card_for_region, end_card, offset);
 155       start_card_for_region = reach + 1;
 156       break;
 157     }
 158     _bot->set_offset_array(start_card_for_region, reach, offset);
 159     start_card_for_region = reach + 1;
 160   }
 161   assert(start_card_for_region > end_card, "Sanity check");
 162   DEBUG_ONLY(check_all_cards(start_card, end_card);)
 163 }
 164 
 165 // The card-interval [start_card, end_card] is a closed interval; this
 166 // is an expensive check -- use with care and only under protection of
 167 // suitable flag.
 168 void G1BlockOffsetTablePart::check_all_cards(size_t start_card, size_t end_card) const {
 169 
 170   if (end_card < start_card) {
 171     return;
 172   }
 173   guarantee(_bot->offset_array(start_card) == N_words, "Wrong value in second card");
 174   for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {
 175     u_char entry = _bot->offset_array(c);
 176     if (c - start_card > BlockOffsetArray::power_to_cards_back(1)) {
 177       guarantee(entry > N_words,
 178                 "Should be in logarithmic region - "
 179                 "entry: %u, "
 180                 "_array->offset_array(c): %u, "
 181                 "N_words: %u",
 182                 (uint)entry, (uint)_bot->offset_array(c), (uint)N_words);
 183     }
 184     size_t backskip = BlockOffsetArray::entry_to_cards_back(entry);
 185     size_t landing_card = c - backskip;
 186     guarantee(landing_card >= (start_card - 1), "Inv");
 187     if (landing_card >= start_card) {
 188       guarantee(_bot->offset_array(landing_card) <= entry,
 189                 "Monotonicity - landing_card offset: %u, "
 190                 "entry: %u",
 191                 (uint)_bot->offset_array(landing_card), (uint)entry);
 192     } else {
 193       guarantee(landing_card == start_card - 1, "Tautology");
 194       // Note that N_words is the maximum offset value
 195       guarantee(_bot->offset_array(landing_card) <= N_words,
 196                 "landing card offset: %u, "
 197                 "N_words: %u",
 198                 (uint)_bot->offset_array(landing_card), (uint)N_words);
 199     }
 200   }
 201 }
 202 
 203 HeapWord* G1BlockOffsetTablePart::forward_to_block_containing_addr_slow(HeapWord* q,



































 204                                                                         HeapWord* n,
 205                                                                         const void* addr) {
 206   // We're not in the normal case.  We need to handle an important subcase
 207   // here: LAB allocation.  An allocation previously recorded in the
 208   // offset table was actually a lab allocation, and was divided into
 209   // several objects subsequently.  Fix this situation as we answer the
 210   // query, by updating entries as we cross them.
 211 
 212   // If the fist object's end q is at the card boundary. Start refining
 213   // with the corresponding card (the value of the entry will be basically
 214   // set to 0). If the object crosses the boundary -- start from the next card.
 215   size_t n_index = _bot->index_for(n);
 216   size_t next_index = _bot->index_for(n) + !_bot->is_card_boundary(n);
 217   // Calculate a consistent next boundary.  If "n" is not at the boundary
 218   // already, step to the boundary.
 219   HeapWord* next_boundary = _bot->address_for_index(n_index) +
 220                             (n_index == next_index ? 0 : N_words);
 221   assert(next_boundary <= _bot->_reserved.end(),
 222          "next_boundary is beyond the end of the covered region "
 223          " next_boundary " PTR_FORMAT " _array->_end " PTR_FORMAT,
 224          p2i(next_boundary), p2i(_bot->_reserved.end()));
 225   if (addr >= _space->top()) return _space->top();
 226   while (next_boundary < addr) {
 227     while (n <= next_boundary) {
 228       q = n;
 229       oop obj = oop(q);
 230       if (obj->klass_or_null() == NULL) return q;
 231       n += block_size(q);
 232     }
 233     assert(q <= next_boundary && n > next_boundary, "Consequence of loop");
 234     // [q, n) is the block that crosses the boundary.
 235     alloc_block_work(&next_boundary, &next_index, q, n);
 236   }
 237   return forward_to_block_containing_addr_const(q, n, addr);
 238 }
 239 







 240 //
 241 //              threshold_
 242 //              |   _index_
 243 //              v   v
 244 //      +-------+-------+-------+-------+-------+
 245 //      | i-1   |   i   | i+1   | i+2   | i+3   |
 246 //      +-------+-------+-------+-------+-------+
 247 //       ( ^    ]
 248 //         block-start
 249 //
 250 void G1BlockOffsetTablePart::alloc_block_work(HeapWord** threshold_, size_t* index_,
 251                                               HeapWord* blk_start, HeapWord* blk_end) {
 252   // For efficiency, do copy-in/copy-out.
 253   HeapWord* threshold = *threshold_;
 254   size_t    index = *index_;
 255 
 256   assert(blk_start != NULL && blk_end > blk_start,
 257          "phantom block");
 258   assert(blk_end > threshold, "should be past threshold");
 259   assert(blk_start <= threshold, "blk_start should be at or before threshold");
 260   assert(pointer_delta(threshold, blk_start) <= N_words,
 261          "offset should be <= BlockOffsetSharedArray::N");
 262   assert(G1CollectedHeap::heap()->is_in_reserved(blk_start),
 263          "reference must be into the heap");
 264   assert(G1CollectedHeap::heap()->is_in_reserved(blk_end-1),
 265          "limit must be within the heap");
 266   assert(threshold == _bot->_reserved.start() + index*N_words,
 267          "index must agree with threshold");
 268 
 269   DEBUG_ONLY(size_t orig_index = index;)
 270 
 271   // Mark the card that holds the offset into the block.  Note
 272   // that _next_offset_index and _next_offset_threshold are not
 273   // updated until the end of this method.
 274   _bot->set_offset_array(index, threshold, blk_start);
 275 
 276   // We need to now mark the subsequent cards that this blk spans.
 277 
 278   // Index of card on which blk ends.
 279   size_t end_index   = _bot->index_for(blk_end - 1);
 280 
 281   // Are there more cards left to be updated?
 282   if (index + 1 <= end_index) {
 283     HeapWord* rem_st  = _bot->address_for_index(index + 1);
 284     // Calculate rem_end this way because end_index
 285     // may be the last valid index in the covered region.
 286     HeapWord* rem_end = _bot->address_for_index(end_index) +  N_words;
 287     set_remainder_to_point_to_start(rem_st, rem_end);
 288   }
 289 
 290   index = end_index + 1;
 291   // Calculate threshold_ this way because end_index
 292   // may be the last valid index in the covered region.
 293   threshold = _bot->address_for_index(end_index) + N_words;
 294   assert(threshold >= blk_end, "Incorrect offset threshold");
 295 
 296   // index_ and threshold_ updated here.
 297   *threshold_ = threshold;
 298   *index_ = index;
 299 
 300 #ifdef ASSERT
 301   // The offset can be 0 if the block starts on a boundary.  That
 302   // is checked by an assertion above.
 303   size_t start_index = _bot->index_for(blk_start);
 304   HeapWord* boundary = _bot->address_for_index(start_index);
 305   assert((_bot->offset_array(orig_index) == 0 && blk_start == boundary) ||
 306          (_bot->offset_array(orig_index) > 0 && _bot->offset_array(orig_index) <= N_words),
 307          "offset array should have been set - "
 308          "orig_index offset: %u, "
 309          "blk_start: " PTR_FORMAT ", "
 310          "boundary: " PTR_FORMAT,
 311          (uint)_bot->offset_array(orig_index),
 312          p2i(blk_start), p2i(boundary));
 313   for (size_t j = orig_index + 1; j <= end_index; j++) {
 314     assert(_bot->offset_array(j) > 0 &&
 315            _bot->offset_array(j) <=
 316              (u_char) (N_words+BlockOffsetArray::N_powers-1),
 317            "offset array should have been set - "
 318            "%u not > 0 OR %u not <= %u",
 319            (uint) _bot->offset_array(j),
 320            (uint) _bot->offset_array(j),
 321            (uint) (N_words+BlockOffsetArray::N_powers-1));
 322   }
 323 #endif
 324 }
 325 
 326 void G1BlockOffsetTablePart::verify() const {
 327   assert(_space->bottom() < _space->top(), "Only non-empty regions should be verified.");
 328   size_t start_card = _bot->index_for(_space->bottom());
 329   size_t end_card = _bot->index_for(_space->top() - 1);
 330 
 331   for (size_t current_card = start_card; current_card < end_card; current_card++) {
 332     u_char entry = _bot->offset_array(current_card);
 333     if (entry < N_words) {
 334       // The entry should point to an object before the current card. Verify that
 335       // it is possible to walk from that object in to the current card by just
 336       // iterating over the objects following it.
 337       HeapWord* card_address = _bot->address_for_index(current_card);
 338       HeapWord* obj_end = card_address - entry;
 339       while (obj_end < card_address) {
 340         HeapWord* obj = obj_end;
 341         size_t obj_size = block_size(obj);
 342         obj_end = obj + obj_size;
 343         guarantee(obj_end > obj && obj_end <= _space->top(),
 344                   "Invalid object end. obj: " PTR_FORMAT " obj_size: " SIZE_FORMAT " obj_end: " PTR_FORMAT " top: " PTR_FORMAT,
 345                   p2i(obj), obj_size, p2i(obj_end), p2i(_space->top()));
 346       }
 347     } else {
 348       // Because we refine the BOT based on which cards are dirty there is not much we can verify here.
 349       // We need to make sure that we are going backwards and that we don't pass the start of the
 350       // corresponding heap region. But that is about all we can verify.
 351       size_t backskip = BlockOffsetArray::entry_to_cards_back(entry);
 352       guarantee(backskip >= 1, "Must be going back at least one card.");
 353 
 354       size_t max_backskip = current_card - start_card;
 355       guarantee(backskip <= max_backskip,
 356                 "Going backwards beyond the start_card. start_card: " SIZE_FORMAT " current_card: " SIZE_FORMAT " backskip: " SIZE_FORMAT,
 357                 start_card, current_card, backskip);
 358 
 359       HeapWord* backskip_address = _bot->address_for_index(current_card - backskip);
 360       guarantee(backskip_address >= _space->bottom(),
 361                 "Going backwards beyond bottom of the region: bottom: " PTR_FORMAT ", backskip_address: " PTR_FORMAT,
 362                 p2i(_space->bottom()), p2i(backskip_address));
 363     }
 364   }
 365 }
 366 
 367 #ifndef PRODUCT
 368 void
 369 G1BlockOffsetTablePart::print_on(outputStream* out) {
 370   size_t from_index = _bot->index_for(_space->bottom());
 371   size_t to_index = _bot->index_for(_space->end());
 372   out->print_cr(">> BOT for area [" PTR_FORMAT "," PTR_FORMAT ") "
 373                 "cards [" SIZE_FORMAT "," SIZE_FORMAT ")",
 374                 p2i(_space->bottom()), p2i(_space->end()), from_index, to_index);
 375   for (size_t i = from_index; i < to_index; ++i) {
 376     out->print_cr("  entry " SIZE_FORMAT_W(8) " | " PTR_FORMAT " : %3u",
 377                   i, p2i(_bot->address_for_index(i)),
 378                   (uint) _bot->offset_array(i));
 379   }
 380   out->print_cr("  next offset threshold: " PTR_FORMAT, p2i(_next_offset_threshold));
 381   out->print_cr("  next offset index:     " SIZE_FORMAT, _next_offset_index);
 382 }
 383 #endif // !PRODUCT
 384 
 385 HeapWord* G1BlockOffsetTablePart::initialize_threshold_raw() {
 386   assert(!G1CollectedHeap::heap()->is_in_reserved(_bot->_offset_array),































 387          "just checking");
 388   _next_offset_index = _bot->index_for_raw(_space->bottom());
 389   _next_offset_index++;
 390   _next_offset_threshold =
 391     _bot->address_for_index_raw(_next_offset_index);
 392   return _next_offset_threshold;
 393 }
 394 
 395 void G1BlockOffsetTablePart::zero_bottom_entry_raw() {
 396   assert(!G1CollectedHeap::heap()->is_in_reserved(_bot->_offset_array),
 397          "just checking");
 398   size_t bottom_index = _bot->index_for_raw(_space->bottom());
 399   assert(_bot->address_for_index_raw(bottom_index) == _space->bottom(),
 400          "Precondition of call");
 401   _bot->set_offset_array_raw(bottom_index, 0);
 402 }
 403 
 404 HeapWord* G1BlockOffsetTablePart::initialize_threshold() {
 405   assert(!G1CollectedHeap::heap()->is_in_reserved(_bot->_offset_array),
 406          "just checking");
 407   _next_offset_index = _bot->index_for(_space->bottom());
 408   _next_offset_index++;
 409   _next_offset_threshold =
 410     _bot->address_for_index(_next_offset_index);
 411   return _next_offset_threshold;
 412 }
 413 
 414 void G1BlockOffsetTablePart::set_for_starts_humongous(HeapWord* obj_top, size_t fill_size) {
 415   // The first BOT entry should have offset 0.
 416   reset_bot();
 417   alloc_block(_space->bottom(), obj_top);
 418   if (fill_size > 0) {
 419     alloc_block(obj_top, fill_size);
 420   }
 421 }








< prev index next >