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