1 /* 2 * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 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 "oops/oop.inline.hpp" 31 #include "runtime/java.hpp" 32 #include "services/memTracker.hpp" 33 34 35 36 ////////////////////////////////////////////////////////////////////// 37 // G1BlockOffsetSharedArray 38 ////////////////////////////////////////////////////////////////////// 39 40 G1BlockOffsetSharedArray::G1BlockOffsetSharedArray(MemRegion heap, G1RegionToSpaceMapper* storage) : 41 _reserved(), _end(NULL), _listener(), _offset_array(NULL) { 42 43 _reserved = heap; 44 _end = NULL; 45 46 MemRegion bot_reserved = storage->reserved(); 47 48 _offset_array = (u_char*)bot_reserved.start(); 49 _end = _reserved.end(); 50 51 storage->set_mapping_changed_listener(&_listener); 52 53 if (TraceBlockOffsetTable) { 54 gclog_or_tty->print_cr("G1BlockOffsetSharedArray::G1BlockOffsetSharedArray: "); 55 gclog_or_tty->print_cr(" " 56 " rs.base(): " PTR_FORMAT 57 " rs.size(): " SIZE_FORMAT 58 " rs end(): " PTR_FORMAT, 59 p2i(bot_reserved.start()), bot_reserved.byte_size(), p2i(bot_reserved.end())); 60 } 61 } 62 63 bool G1BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const { 64 assert(p >= _reserved.start(), "just checking"); 65 size_t delta = pointer_delta(p, _reserved.start()); 66 return (delta & right_n_bits(LogN_words)) == (size_t)NoBits; 67 } 68 69 #ifdef ASSERT 70 void G1BlockOffsetSharedArray::check_index(size_t index, const char* msg) const { 71 assert((index) < (_reserved.word_size() >> LogN_words), 72 "%s - index: " SIZE_FORMAT ", _vs.committed_size: " SIZE_FORMAT, 73 msg, (index), (_reserved.word_size() >> LogN_words)); 74 assert(G1CollectedHeap::heap()->is_in_exact(address_for_index_raw(index)), 75 "Index " SIZE_FORMAT " corresponding to " PTR_FORMAT 76 " (%u) is not in committed area.", 77 (index), 78 p2i(address_for_index_raw(index)), 79 G1CollectedHeap::heap()->addr_to_region(address_for_index_raw(index))); 80 } 81 #endif // ASSERT 82 83 ////////////////////////////////////////////////////////////////////// 84 // G1BlockOffsetArray 85 ////////////////////////////////////////////////////////////////////// 86 87 G1BlockOffsetArray::G1BlockOffsetArray(G1BlockOffsetSharedArray* array, 88 MemRegion mr) : 89 G1BlockOffsetTable(mr.start(), mr.end()), 90 _unallocated_block(_bottom), 91 _array(array), _gsp(NULL) { 92 assert(_bottom <= _end, "arguments out of order"); 93 } 94 95 void G1BlockOffsetArray::set_space(G1OffsetTableContigSpace* sp) { 96 _gsp = sp; 97 } 98 99 // The arguments follow the normal convention of denoting 100 // a right-open interval: [start, end) 101 void 102 G1BlockOffsetArray:: set_remainder_to_point_to_start(HeapWord* start, HeapWord* end) { 103 104 if (start >= end) { 105 // The start address is equal to the end address (or to 106 // the right of the end address) so there are not cards 107 // that need to be updated.. 108 return; 109 } 110 111 // Write the backskip value for each region. 112 // 113 // offset 114 // card 2nd 3rd 115 // | +- 1st | | 116 // v v v v 117 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+- 118 // |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ... 119 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+- 120 // 11 19 75 121 // 12 122 // 123 // offset card is the card that points to the start of an object 124 // x - offset value of offset card 125 // 1st - start of first logarithmic region 126 // 0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1 127 // 2nd - start of second logarithmic region 128 // 1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8 129 // 3rd - start of third logarithmic region 130 // 2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64 131 // 132 // integer below the block offset entry is an example of 133 // the index of the entry 134 // 135 // Given an address, 136 // Find the index for the address 137 // Find the block offset table entry 138 // Convert the entry to a back slide 139 // (e.g., with today's, offset = 0x81 => 140 // back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8 141 // Move back N (e.g., 8) entries and repeat with the 142 // value of the new entry 143 // 144 size_t start_card = _array->index_for(start); 145 size_t end_card = _array->index_for(end-1); 146 assert(start ==_array->address_for_index(start_card), "Precondition"); 147 assert(end ==_array->address_for_index(end_card)+N_words, "Precondition"); 148 set_remainder_to_point_to_start_incl(start_card, end_card); // closed interval 149 } 150 151 // Unlike the normal convention in this code, the argument here denotes 152 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start() 153 // above. 154 void 155 G1BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card) { 156 if (start_card > end_card) { 157 return; 158 } 159 assert(start_card > _array->index_for(_bottom), "Cannot be first card"); 160 assert(_array->offset_array(start_card-1) <= N_words, 161 "Offset card has an unexpected value"); 162 size_t start_card_for_region = start_card; 163 u_char offset = max_jubyte; 164 for (int i = 0; i < BlockOffsetArray::N_powers; i++) { 165 // -1 so that the the card with the actual offset is counted. Another -1 166 // so that the reach ends in this region and not at the start 167 // of the next. 168 size_t reach = start_card - 1 + (BlockOffsetArray::power_to_cards_back(i+1) - 1); 169 offset = N_words + i; 170 if (reach >= end_card) { 171 _array->set_offset_array(start_card_for_region, end_card, offset); 172 start_card_for_region = reach + 1; 173 break; 174 } 175 _array->set_offset_array(start_card_for_region, reach, offset); 176 start_card_for_region = reach + 1; 177 } 178 assert(start_card_for_region > end_card, "Sanity check"); 179 DEBUG_ONLY(check_all_cards(start_card, end_card);) 180 } 181 182 // The card-interval [start_card, end_card] is a closed interval; this 183 // is an expensive check -- use with care and only under protection of 184 // suitable flag. 185 void G1BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const { 186 187 if (end_card < start_card) { 188 return; 189 } 190 guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card"); 191 for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) { 192 u_char entry = _array->offset_array(c); 193 if (c - start_card > BlockOffsetArray::power_to_cards_back(1)) { 194 guarantee(entry > N_words, 195 "Should be in logarithmic region - " 196 "entry: %u, " 197 "_array->offset_array(c): %u, " 198 "N_words: %u", 199 (uint)entry, (uint)_array->offset_array(c), (uint)N_words); 200 } 201 size_t backskip = BlockOffsetArray::entry_to_cards_back(entry); 202 size_t landing_card = c - backskip; 203 guarantee(landing_card >= (start_card - 1), "Inv"); 204 if (landing_card >= start_card) { 205 guarantee(_array->offset_array(landing_card) <= entry, 206 "Monotonicity - landing_card offset: %u, " 207 "entry: %u", 208 (uint)_array->offset_array(landing_card), (uint)entry); 209 } else { 210 guarantee(landing_card == start_card - 1, "Tautology"); 211 // Note that N_words is the maximum offset value 212 guarantee(_array->offset_array(landing_card) <= N_words, 213 "landing card offset: %u, " 214 "N_words: %u", 215 (uint)_array->offset_array(landing_card), (uint)N_words); 216 } 217 } 218 } 219 220 HeapWord* G1BlockOffsetArray::block_start_unsafe(const void* addr) { 221 assert(_bottom <= addr && addr < _end, 222 "addr must be covered by this Array"); 223 // Must read this exactly once because it can be modified by parallel 224 // allocation. 225 HeapWord* ub = _unallocated_block; 226 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) { 227 assert(ub < _end, "tautology (see above)"); 228 return ub; 229 } 230 // Otherwise, find the block start using the table. 231 HeapWord* q = block_at_or_preceding(addr, false, 0); 232 return forward_to_block_containing_addr(q, addr); 233 } 234 235 // This duplicates a little code from the above: unavoidable. 236 HeapWord* 237 G1BlockOffsetArray::block_start_unsafe_const(const void* addr) const { 238 assert(_bottom <= addr && addr < _end, 239 "addr must be covered by this Array"); 240 // Must read this exactly once because it can be modified by parallel 241 // allocation. 242 HeapWord* ub = _unallocated_block; 243 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) { 244 assert(ub < _end, "tautology (see above)"); 245 return ub; 246 } 247 // Otherwise, find the block start using the table. 248 HeapWord* q = block_at_or_preceding(addr, false, 0); 249 HeapWord* n = q + block_size(q); 250 return forward_to_block_containing_addr_const(q, n, addr); 251 } 252 253 254 HeapWord* 255 G1BlockOffsetArray::forward_to_block_containing_addr_slow(HeapWord* q, 256 HeapWord* n, 257 const void* addr) { 258 // We're not in the normal case. We need to handle an important subcase 259 // here: LAB allocation. An allocation previously recorded in the 260 // offset table was actually a lab allocation, and was divided into 261 // several objects subsequently. Fix this situation as we answer the 262 // query, by updating entries as we cross them. 263 264 // If the fist object's end q is at the card boundary. Start refining 265 // with the corresponding card (the value of the entry will be basically 266 // set to 0). If the object crosses the boundary -- start from the next card. 267 size_t n_index = _array->index_for(n); 268 size_t next_index = _array->index_for(n) + !_array->is_card_boundary(n); 269 // Calculate a consistent next boundary. If "n" is not at the boundary 270 // already, step to the boundary. 271 HeapWord* next_boundary = _array->address_for_index(n_index) + 272 (n_index == next_index ? 0 : N_words); 273 assert(next_boundary <= _array->_end, 274 "next_boundary is beyond the end of the covered region " 275 " next_boundary " PTR_FORMAT " _array->_end " PTR_FORMAT, 276 p2i(next_boundary), p2i(_array->_end)); 277 if (addr >= gsp()->top()) return gsp()->top(); 278 while (next_boundary < addr) { 279 while (n <= next_boundary) { 280 q = n; 281 oop obj = oop(q); 282 if (obj->klass_or_null() == NULL) return q; 283 n += block_size(q); 284 } 285 assert(q <= next_boundary && n > next_boundary, "Consequence of loop"); 286 // [q, n) is the block that crosses the boundary. 287 alloc_block_work2(&next_boundary, &next_index, q, n); 288 } 289 return forward_to_block_containing_addr_const(q, n, addr); 290 } 291 292 // Note that the committed size of the covered space may have changed, 293 // so the table size might also wish to change. 294 void G1BlockOffsetArray::resize(size_t new_word_size) { 295 HeapWord* new_end = _bottom + new_word_size; 296 _end = new_end; // update _end 297 } 298 299 // 300 // threshold_ 301 // | _index_ 302 // v v 303 // +-------+-------+-------+-------+-------+ 304 // | i-1 | i | i+1 | i+2 | i+3 | 305 // +-------+-------+-------+-------+-------+ 306 // ( ^ ] 307 // block-start 308 // 309 void G1BlockOffsetArray::alloc_block_work2(HeapWord** threshold_, size_t* index_, 310 HeapWord* blk_start, HeapWord* blk_end) { 311 // For efficiency, do copy-in/copy-out. 312 HeapWord* threshold = *threshold_; 313 size_t index = *index_; 314 315 assert(blk_start != NULL && blk_end > blk_start, 316 "phantom block"); 317 assert(blk_end > threshold, "should be past threshold"); 318 assert(blk_start <= threshold, "blk_start should be at or before threshold"); 319 assert(pointer_delta(threshold, blk_start) <= N_words, 320 "offset should be <= BlockOffsetSharedArray::N"); 321 assert(G1CollectedHeap::heap()->is_in_reserved(blk_start), 322 "reference must be into the heap"); 323 assert(G1CollectedHeap::heap()->is_in_reserved(blk_end-1), 324 "limit must be within the heap"); 325 assert(threshold == _array->_reserved.start() + index*N_words, 326 "index must agree with threshold"); 327 328 DEBUG_ONLY(size_t orig_index = index;) 329 330 // Mark the card that holds the offset into the block. Note 331 // that _next_offset_index and _next_offset_threshold are not 332 // updated until the end of this method. 333 _array->set_offset_array(index, threshold, blk_start); 334 335 // We need to now mark the subsequent cards that this blk spans. 336 337 // Index of card on which blk ends. 338 size_t end_index = _array->index_for(blk_end - 1); 339 340 // Are there more cards left to be updated? 341 if (index + 1 <= end_index) { 342 HeapWord* rem_st = _array->address_for_index(index + 1); 343 // Calculate rem_end this way because end_index 344 // may be the last valid index in the covered region. 345 HeapWord* rem_end = _array->address_for_index(end_index) + N_words; 346 set_remainder_to_point_to_start(rem_st, rem_end); 347 } 348 349 index = end_index + 1; 350 // Calculate threshold_ this way because end_index 351 // may be the last valid index in the covered region. 352 threshold = _array->address_for_index(end_index) + N_words; 353 assert(threshold >= blk_end, "Incorrect offset threshold"); 354 355 // index_ and threshold_ updated here. 356 *threshold_ = threshold; 357 *index_ = index; 358 359 #ifdef ASSERT 360 // The offset can be 0 if the block starts on a boundary. That 361 // is checked by an assertion above. 362 size_t start_index = _array->index_for(blk_start); 363 HeapWord* boundary = _array->address_for_index(start_index); 364 assert((_array->offset_array(orig_index) == 0 && blk_start == boundary) || 365 (_array->offset_array(orig_index) > 0 && _array->offset_array(orig_index) <= N_words), 366 "offset array should have been set - " 367 "orig_index offset: %u, " 368 "blk_start: " PTR_FORMAT ", " 369 "boundary: " PTR_FORMAT, 370 (uint)_array->offset_array(orig_index), 371 p2i(blk_start), p2i(boundary)); 372 for (size_t j = orig_index + 1; j <= end_index; j++) { 373 assert(_array->offset_array(j) > 0 && 374 _array->offset_array(j) <= 375 (u_char) (N_words+BlockOffsetArray::N_powers-1), 376 "offset array should have been set - " 377 "%u not > 0 OR %u not <= %u", 378 (uint) _array->offset_array(j), 379 (uint) _array->offset_array(j), 380 (uint) (N_words+BlockOffsetArray::N_powers-1)); 381 } 382 #endif 383 } 384 385 void G1BlockOffsetArray::verify() const { 386 assert(gsp()->bottom() < gsp()->top(), "Only non-empty regions should be verified."); 387 size_t start_card = _array->index_for(gsp()->bottom()); 388 size_t end_card = _array->index_for(gsp()->top() - 1); 389 390 for (size_t current_card = start_card; current_card < end_card; current_card++) { 391 u_char entry = _array->offset_array(current_card); 392 if (entry < N_words) { 393 // The entry should point to an object before the current card. Verify that 394 // it is possible to walk from that object in to the current card by just 395 // iterating over the objects following it. 396 HeapWord* card_address = _array->address_for_index(current_card); 397 HeapWord* obj_end = card_address - entry; 398 while (obj_end < card_address) { 399 HeapWord* obj = obj_end; 400 size_t obj_size = block_size(obj); 401 obj_end = obj + obj_size; 402 guarantee(obj_end > obj && obj_end <= gsp()->top(), 403 "Invalid object end. obj: " PTR_FORMAT " obj_size: " SIZE_FORMAT " obj_end: " PTR_FORMAT " top: " PTR_FORMAT, 404 p2i(obj), obj_size, p2i(obj_end), p2i(gsp()->top())); 405 } 406 } else { 407 // Because we refine the BOT based on which cards are dirty there is not much we can verify here. 408 // We need to make sure that we are going backwards and that we don't pass the start of the 409 // corresponding heap region. But that is about all we can verify. 410 size_t backskip = BlockOffsetArray::entry_to_cards_back(entry); 411 guarantee(backskip >= 1, "Must be going back at least one card."); 412 413 size_t max_backskip = current_card - start_card; 414 guarantee(backskip <= max_backskip, 415 "Going backwards beyond the start_card. start_card: " SIZE_FORMAT " current_card: " SIZE_FORMAT " backskip: " SIZE_FORMAT, 416 start_card, current_card, backskip); 417 418 HeapWord* backskip_address = _array->address_for_index(current_card - backskip); 419 guarantee(backskip_address >= gsp()->bottom(), 420 "Going backwards beyond bottom of the region: bottom: " PTR_FORMAT ", backskip_address: " PTR_FORMAT, 421 p2i(gsp()->bottom()), p2i(backskip_address)); 422 } 423 } 424 } 425 426 #ifndef PRODUCT 427 void 428 G1BlockOffsetArray::print_on(outputStream* out) { 429 size_t from_index = _array->index_for(_bottom); 430 size_t to_index = _array->index_for(_end); 431 out->print_cr(">> BOT for area [" PTR_FORMAT "," PTR_FORMAT ") " 432 "cards [" SIZE_FORMAT "," SIZE_FORMAT ")", 433 p2i(_bottom), p2i(_end), from_index, to_index); 434 for (size_t i = from_index; i < to_index; ++i) { 435 out->print_cr(" entry " SIZE_FORMAT_W(8) " | " PTR_FORMAT " : %3u", 436 i, p2i(_array->address_for_index(i)), 437 (uint) _array->offset_array(i)); 438 } 439 } 440 #endif // !PRODUCT 441 442 ////////////////////////////////////////////////////////////////////// 443 // G1BlockOffsetArrayContigSpace 444 ////////////////////////////////////////////////////////////////////// 445 446 HeapWord* 447 G1BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) { 448 assert(_bottom <= addr && addr < _end, 449 "addr must be covered by this Array"); 450 HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1); 451 return forward_to_block_containing_addr(q, addr); 452 } 453 454 HeapWord* 455 G1BlockOffsetArrayContigSpace:: 456 block_start_unsafe_const(const void* addr) const { 457 assert(_bottom <= addr && addr < _end, 458 "addr must be covered by this Array"); 459 HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1); 460 HeapWord* n = q + block_size(q); 461 return forward_to_block_containing_addr_const(q, n, addr); 462 } 463 464 G1BlockOffsetArrayContigSpace:: 465 G1BlockOffsetArrayContigSpace(G1BlockOffsetSharedArray* array, 466 MemRegion mr) : 467 G1BlockOffsetArray(array, mr) 468 { 469 _next_offset_threshold = NULL; 470 _next_offset_index = 0; 471 } 472 473 HeapWord* G1BlockOffsetArrayContigSpace::initialize_threshold_raw() { 474 assert(!G1CollectedHeap::heap()->is_in_reserved(_array->_offset_array), 475 "just checking"); 476 _next_offset_index = _array->index_for_raw(_bottom); 477 _next_offset_index++; 478 _next_offset_threshold = 479 _array->address_for_index_raw(_next_offset_index); 480 return _next_offset_threshold; 481 } 482 483 void G1BlockOffsetArrayContigSpace::zero_bottom_entry_raw() { 484 assert(!G1CollectedHeap::heap()->is_in_reserved(_array->_offset_array), 485 "just checking"); 486 size_t bottom_index = _array->index_for_raw(_bottom); 487 assert(_array->address_for_index_raw(bottom_index) == _bottom, 488 "Precondition of call"); 489 _array->set_offset_array_raw(bottom_index, 0); 490 } 491 492 HeapWord* G1BlockOffsetArrayContigSpace::initialize_threshold() { 493 assert(!G1CollectedHeap::heap()->is_in_reserved(_array->_offset_array), 494 "just checking"); 495 _next_offset_index = _array->index_for(_bottom); 496 _next_offset_index++; 497 _next_offset_threshold = 498 _array->address_for_index(_next_offset_index); 499 return _next_offset_threshold; 500 } 501 502 void G1BlockOffsetArrayContigSpace::set_for_starts_humongous(HeapWord* obj_top) { 503 // The first BOT entry should have offset 0. 504 reset_bot(); 505 alloc_block(_bottom, obj_top); 506 } 507 508 #ifndef PRODUCT 509 void G1BlockOffsetArrayContigSpace::print_on(outputStream* out) { 510 G1BlockOffsetArray::print_on(out); 511 out->print_cr(" next offset threshold: " PTR_FORMAT, p2i(_next_offset_threshold)); 512 out->print_cr(" next offset index: " SIZE_FORMAT, _next_offset_index); 513 } 514 #endif // !PRODUCT