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

Mon, 07 Jul 2014 10:12:40 +0200

author
stefank
date
Mon, 07 Jul 2014 10:12:40 +0200
changeset 6992
2c6ef90f030a
parent 6987
9441d22e429a
child 7050
6701abbc4441
permissions
-rw-r--r--

8049421: G1 Class Unloading after completing a concurrent mark cycle
Reviewed-by: tschatzl, ehelin, brutisso, coleenp, roland, iveresov
Contributed-by: stefan.karlsson@oracle.com, mikael.gerdin@oracle.com

ysr@777 1 /*
drchase@6680 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
ysr@777 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ysr@777 4 *
ysr@777 5 * This code is free software; you can redistribute it and/or modify it
ysr@777 6 * under the terms of the GNU General Public License version 2 only, as
ysr@777 7 * published by the Free Software Foundation.
ysr@777 8 *
ysr@777 9 * This code is distributed in the hope that it will be useful, but WITHOUT
ysr@777 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ysr@777 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ysr@777 12 * version 2 for more details (a copy is included in the LICENSE file that
ysr@777 13 * accompanied this code).
ysr@777 14 *
ysr@777 15 * You should have received a copy of the GNU General Public License version
ysr@777 16 * 2 along with this work; if not, write to the Free Software Foundation,
ysr@777 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ysr@777 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
ysr@777 22 *
ysr@777 23 */
ysr@777 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "gc_implementation/g1/g1BlockOffsetTable.inline.hpp"
mgerdin@6987 27 #include "gc_implementation/g1/heapRegion.hpp"
stefank@2314 28 #include "memory/space.hpp"
stefank@2314 29 #include "oops/oop.inline.hpp"
stefank@2314 30 #include "runtime/java.hpp"
zgu@3900 31 #include "services/memTracker.hpp"
ysr@777 32
drchase@6680 33 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
drchase@6680 34
ysr@777 35 //////////////////////////////////////////////////////////////////////
ysr@777 36 // G1BlockOffsetSharedArray
ysr@777 37 //////////////////////////////////////////////////////////////////////
ysr@777 38
ysr@777 39 G1BlockOffsetSharedArray::G1BlockOffsetSharedArray(MemRegion reserved,
ysr@777 40 size_t init_word_size) :
ysr@777 41 _reserved(reserved), _end(NULL)
ysr@777 42 {
ysr@777 43 size_t size = compute_size(reserved.word_size());
ysr@777 44 ReservedSpace rs(ReservedSpace::allocation_align_size_up(size));
ysr@777 45 if (!rs.is_reserved()) {
ysr@777 46 vm_exit_during_initialization("Could not reserve enough space for heap offset array");
ysr@777 47 }
ysr@777 48 if (!_vs.initialize(rs, 0)) {
ysr@777 49 vm_exit_during_initialization("Could not reserve enough space for heap offset array");
ysr@777 50 }
zgu@3900 51
zgu@3900 52 MemTracker::record_virtual_memory_type((address)rs.base(), mtGC);
zgu@3900 53
ysr@777 54 _offset_array = (u_char*)_vs.low_boundary();
ysr@777 55 resize(init_word_size);
ysr@777 56 if (TraceBlockOffsetTable) {
ysr@777 57 gclog_or_tty->print_cr("G1BlockOffsetSharedArray::G1BlockOffsetSharedArray: ");
ysr@777 58 gclog_or_tty->print_cr(" "
ysr@777 59 " rs.base(): " INTPTR_FORMAT
ysr@777 60 " rs.size(): " INTPTR_FORMAT
ysr@777 61 " rs end(): " INTPTR_FORMAT,
ysr@777 62 rs.base(), rs.size(), rs.base() + rs.size());
ysr@777 63 gclog_or_tty->print_cr(" "
ysr@777 64 " _vs.low_boundary(): " INTPTR_FORMAT
ysr@777 65 " _vs.high_boundary(): " INTPTR_FORMAT,
ysr@777 66 _vs.low_boundary(),
ysr@777 67 _vs.high_boundary());
ysr@777 68 }
ysr@777 69 }
ysr@777 70
ysr@777 71 void G1BlockOffsetSharedArray::resize(size_t new_word_size) {
ysr@777 72 assert(new_word_size <= _reserved.word_size(), "Resize larger than reserved");
ysr@777 73 size_t new_size = compute_size(new_word_size);
ysr@777 74 size_t old_size = _vs.committed_size();
ysr@777 75 size_t delta;
ysr@777 76 char* high = _vs.high();
ysr@777 77 _end = _reserved.start() + new_word_size;
ysr@777 78 if (new_size > old_size) {
ysr@777 79 delta = ReservedSpace::page_align_size_up(new_size - old_size);
ysr@777 80 assert(delta > 0, "just checking");
ysr@777 81 if (!_vs.expand_by(delta)) {
ysr@777 82 // Do better than this for Merlin
ccheung@4993 83 vm_exit_out_of_memory(delta, OOM_MMAP_ERROR, "offset table expansion");
ysr@777 84 }
ysr@777 85 assert(_vs.high() == high + delta, "invalid expansion");
ysr@777 86 // Initialization of the contents is left to the
ysr@777 87 // G1BlockOffsetArray that uses it.
ysr@777 88 } else {
ysr@777 89 delta = ReservedSpace::page_align_size_down(old_size - new_size);
ysr@777 90 if (delta == 0) return;
ysr@777 91 _vs.shrink_by(delta);
ysr@777 92 assert(_vs.high() == high - delta, "invalid expansion");
ysr@777 93 }
ysr@777 94 }
ysr@777 95
ysr@777 96 bool G1BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {
ysr@777 97 assert(p >= _reserved.start(), "just checking");
ysr@777 98 size_t delta = pointer_delta(p, _reserved.start());
ysr@777 99 return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;
ysr@777 100 }
ysr@777 101
mgerdin@6987 102 void G1BlockOffsetSharedArray::set_offset_array(HeapWord* left, HeapWord* right, u_char offset) {
mgerdin@6987 103 check_index(index_for(right - 1), "right address out of range");
mgerdin@6987 104 assert(left < right, "Heap addresses out of order");
mgerdin@6987 105 size_t num_cards = pointer_delta(right, left) >> LogN_words;
mgerdin@6987 106 if (UseMemSetInBOT) {
mgerdin@6987 107 memset(&_offset_array[index_for(left)], offset, num_cards);
mgerdin@6987 108 } else {
mgerdin@6987 109 size_t i = index_for(left);
mgerdin@6987 110 const size_t end = i + num_cards;
mgerdin@6987 111 for (; i < end; i++) {
mgerdin@6987 112 _offset_array[i] = offset;
mgerdin@6987 113 }
mgerdin@6987 114 }
mgerdin@6987 115 }
ysr@777 116
ysr@777 117 //////////////////////////////////////////////////////////////////////
ysr@777 118 // G1BlockOffsetArray
ysr@777 119 //////////////////////////////////////////////////////////////////////
ysr@777 120
ysr@777 121 G1BlockOffsetArray::G1BlockOffsetArray(G1BlockOffsetSharedArray* array,
ysr@777 122 MemRegion mr, bool init_to_zero) :
ysr@777 123 G1BlockOffsetTable(mr.start(), mr.end()),
ysr@777 124 _unallocated_block(_bottom),
mgerdin@6987 125 _array(array), _gsp(NULL),
ysr@777 126 _init_to_zero(init_to_zero) {
ysr@777 127 assert(_bottom <= _end, "arguments out of order");
ysr@777 128 if (!_init_to_zero) {
ysr@777 129 // initialize cards to point back to mr.start()
ysr@777 130 set_remainder_to_point_to_start(mr.start() + N_words, mr.end());
ysr@777 131 _array->set_offset_array(0, 0); // set first card to 0
ysr@777 132 }
ysr@777 133 }
ysr@777 134
mgerdin@6987 135 void G1BlockOffsetArray::set_space(G1OffsetTableContigSpace* sp) {
mgerdin@6987 136 _gsp = sp;
ysr@777 137 }
ysr@777 138
ysr@777 139 // The arguments follow the normal convention of denoting
ysr@777 140 // a right-open interval: [start, end)
ysr@777 141 void
ysr@777 142 G1BlockOffsetArray:: set_remainder_to_point_to_start(HeapWord* start, HeapWord* end) {
ysr@777 143
ysr@777 144 if (start >= end) {
ysr@777 145 // The start address is equal to the end address (or to
ysr@777 146 // the right of the end address) so there are not cards
ysr@777 147 // that need to be updated..
ysr@777 148 return;
ysr@777 149 }
ysr@777 150
ysr@777 151 // Write the backskip value for each region.
ysr@777 152 //
ysr@777 153 // offset
ysr@777 154 // card 2nd 3rd
ysr@777 155 // | +- 1st | |
ysr@777 156 // v v v v
ysr@777 157 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-
ysr@777 158 // |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ...
ysr@777 159 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-
ysr@777 160 // 11 19 75
ysr@777 161 // 12
ysr@777 162 //
ysr@777 163 // offset card is the card that points to the start of an object
ysr@777 164 // x - offset value of offset card
ysr@777 165 // 1st - start of first logarithmic region
ysr@777 166 // 0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1
ysr@777 167 // 2nd - start of second logarithmic region
ysr@777 168 // 1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8
ysr@777 169 // 3rd - start of third logarithmic region
ysr@777 170 // 2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64
ysr@777 171 //
ysr@777 172 // integer below the block offset entry is an example of
ysr@777 173 // the index of the entry
ysr@777 174 //
ysr@777 175 // Given an address,
ysr@777 176 // Find the index for the address
ysr@777 177 // Find the block offset table entry
ysr@777 178 // Convert the entry to a back slide
ysr@777 179 // (e.g., with today's, offset = 0x81 =>
ysr@777 180 // back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8
ysr@777 181 // Move back N (e.g., 8) entries and repeat with the
ysr@777 182 // value of the new entry
ysr@777 183 //
ysr@777 184 size_t start_card = _array->index_for(start);
ysr@777 185 size_t end_card = _array->index_for(end-1);
ysr@777 186 assert(start ==_array->address_for_index(start_card), "Precondition");
ysr@777 187 assert(end ==_array->address_for_index(end_card)+N_words, "Precondition");
ysr@777 188 set_remainder_to_point_to_start_incl(start_card, end_card); // closed interval
ysr@777 189 }
ysr@777 190
ysr@777 191 // Unlike the normal convention in this code, the argument here denotes
ysr@777 192 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()
ysr@777 193 // above.
ysr@777 194 void
ysr@777 195 G1BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card) {
ysr@777 196 if (start_card > end_card) {
ysr@777 197 return;
ysr@777 198 }
ysr@777 199 assert(start_card > _array->index_for(_bottom), "Cannot be first card");
ysr@777 200 assert(_array->offset_array(start_card-1) <= N_words,
tonyp@2241 201 "Offset card has an unexpected value");
ysr@777 202 size_t start_card_for_region = start_card;
ysr@777 203 u_char offset = max_jubyte;
ysr@777 204 for (int i = 0; i < BlockOffsetArray::N_powers; i++) {
ysr@777 205 // -1 so that the the card with the actual offset is counted. Another -1
ysr@777 206 // so that the reach ends in this region and not at the start
ysr@777 207 // of the next.
ysr@777 208 size_t reach = start_card - 1 + (BlockOffsetArray::power_to_cards_back(i+1) - 1);
ysr@777 209 offset = N_words + i;
ysr@777 210 if (reach >= end_card) {
ysr@777 211 _array->set_offset_array(start_card_for_region, end_card, offset);
ysr@777 212 start_card_for_region = reach + 1;
ysr@777 213 break;
ysr@777 214 }
ysr@777 215 _array->set_offset_array(start_card_for_region, reach, offset);
ysr@777 216 start_card_for_region = reach + 1;
ysr@777 217 }
ysr@777 218 assert(start_card_for_region > end_card, "Sanity check");
ysr@777 219 DEBUG_ONLY(check_all_cards(start_card, end_card);)
ysr@777 220 }
ysr@777 221
ysr@777 222 // The block [blk_start, blk_end) has been allocated;
ysr@777 223 // adjust the block offset table to represent this information;
ysr@777 224 // right-open interval: [blk_start, blk_end)
ysr@777 225 void
ysr@777 226 G1BlockOffsetArray::alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
ysr@777 227 mark_block(blk_start, blk_end);
ysr@777 228 allocated(blk_start, blk_end);
ysr@777 229 }
ysr@777 230
ysr@777 231 // Adjust BOT to show that a previously whole block has been split
ysr@777 232 // into two.
ysr@777 233 void G1BlockOffsetArray::split_block(HeapWord* blk, size_t blk_size,
ysr@777 234 size_t left_blk_size) {
ysr@777 235 // Verify that the BOT shows [blk, blk + blk_size) to be one block.
ysr@777 236 verify_single_block(blk, blk_size);
ysr@777 237 // Update the BOT to indicate that [blk + left_blk_size, blk + blk_size)
ysr@777 238 // is one single block.
ysr@777 239 mark_block(blk + left_blk_size, blk + blk_size);
ysr@777 240 }
ysr@777 241
ysr@777 242
ysr@777 243 // Action_mark - update the BOT for the block [blk_start, blk_end).
ysr@777 244 // Current typical use is for splitting a block.
tonyp@2453 245 // Action_single - update the BOT for an allocation.
ysr@777 246 // Action_verify - BOT verification.
ysr@777 247 void G1BlockOffsetArray::do_block_internal(HeapWord* blk_start,
ysr@777 248 HeapWord* blk_end,
ysr@777 249 Action action) {
ysr@777 250 assert(Universe::heap()->is_in_reserved(blk_start),
ysr@777 251 "reference must be into the heap");
ysr@777 252 assert(Universe::heap()->is_in_reserved(blk_end-1),
ysr@777 253 "limit must be within the heap");
ysr@777 254 // This is optimized to make the test fast, assuming we only rarely
ysr@777 255 // cross boundaries.
ysr@777 256 uintptr_t end_ui = (uintptr_t)(blk_end - 1);
ysr@777 257 uintptr_t start_ui = (uintptr_t)blk_start;
ysr@777 258 // Calculate the last card boundary preceding end of blk
ysr@777 259 intptr_t boundary_before_end = (intptr_t)end_ui;
ysr@777 260 clear_bits(boundary_before_end, right_n_bits(LogN));
ysr@777 261 if (start_ui <= (uintptr_t)boundary_before_end) {
ysr@777 262 // blk starts at or crosses a boundary
ysr@777 263 // Calculate index of card on which blk begins
ysr@777 264 size_t start_index = _array->index_for(blk_start);
ysr@777 265 // Index of card on which blk ends
ysr@777 266 size_t end_index = _array->index_for(blk_end - 1);
ysr@777 267 // Start address of card on which blk begins
ysr@777 268 HeapWord* boundary = _array->address_for_index(start_index);
ysr@777 269 assert(boundary <= blk_start, "blk should start at or after boundary");
ysr@777 270 if (blk_start != boundary) {
ysr@777 271 // blk starts strictly after boundary
ysr@777 272 // adjust card boundary and start_index forward to next card
ysr@777 273 boundary += N_words;
ysr@777 274 start_index++;
ysr@777 275 }
ysr@777 276 assert(start_index <= end_index, "monotonicity of index_for()");
ysr@777 277 assert(boundary <= (HeapWord*)boundary_before_end, "tautology");
ysr@777 278 switch (action) {
ysr@777 279 case Action_mark: {
ysr@777 280 if (init_to_zero()) {
ysr@777 281 _array->set_offset_array(start_index, boundary, blk_start);
ysr@777 282 break;
ysr@777 283 } // Else fall through to the next case
ysr@777 284 }
ysr@777 285 case Action_single: {
ysr@777 286 _array->set_offset_array(start_index, boundary, blk_start);
ysr@777 287 // We have finished marking the "offset card". We need to now
ysr@777 288 // mark the subsequent cards that this blk spans.
ysr@777 289 if (start_index < end_index) {
ysr@777 290 HeapWord* rem_st = _array->address_for_index(start_index) + N_words;
ysr@777 291 HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
ysr@777 292 set_remainder_to_point_to_start(rem_st, rem_end);
ysr@777 293 }
ysr@777 294 break;
ysr@777 295 }
ysr@777 296 case Action_check: {
ysr@777 297 _array->check_offset_array(start_index, boundary, blk_start);
ysr@777 298 // We have finished checking the "offset card". We need to now
ysr@777 299 // check the subsequent cards that this blk spans.
ysr@777 300 check_all_cards(start_index + 1, end_index);
ysr@777 301 break;
ysr@777 302 }
ysr@777 303 default:
ysr@777 304 ShouldNotReachHere();
ysr@777 305 }
ysr@777 306 }
ysr@777 307 }
ysr@777 308
ysr@777 309 // The card-interval [start_card, end_card] is a closed interval; this
ysr@777 310 // is an expensive check -- use with care and only under protection of
ysr@777 311 // suitable flag.
ysr@777 312 void G1BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {
ysr@777 313
ysr@777 314 if (end_card < start_card) {
ysr@777 315 return;
ysr@777 316 }
ysr@777 317 guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card");
ysr@777 318 for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {
ysr@777 319 u_char entry = _array->offset_array(c);
ysr@777 320 if (c - start_card > BlockOffsetArray::power_to_cards_back(1)) {
johnc@4300 321 guarantee(entry > N_words,
johnc@4300 322 err_msg("Should be in logarithmic region - "
johnc@4300 323 "entry: " UINT32_FORMAT ", "
johnc@4300 324 "_array->offset_array(c): " UINT32_FORMAT ", "
johnc@4300 325 "N_words: " UINT32_FORMAT,
johnc@4300 326 entry, _array->offset_array(c), N_words));
ysr@777 327 }
ysr@777 328 size_t backskip = BlockOffsetArray::entry_to_cards_back(entry);
ysr@777 329 size_t landing_card = c - backskip;
ysr@777 330 guarantee(landing_card >= (start_card - 1), "Inv");
ysr@777 331 if (landing_card >= start_card) {
johnc@4300 332 guarantee(_array->offset_array(landing_card) <= entry,
johnc@4300 333 err_msg("Monotonicity - landing_card offset: " UINT32_FORMAT ", "
johnc@4300 334 "entry: " UINT32_FORMAT,
johnc@4300 335 _array->offset_array(landing_card), entry));
ysr@777 336 } else {
ysr@777 337 guarantee(landing_card == start_card - 1, "Tautology");
johnc@4300 338 // Note that N_words is the maximum offset value
johnc@4300 339 guarantee(_array->offset_array(landing_card) <= N_words,
johnc@4300 340 err_msg("landing card offset: " UINT32_FORMAT ", "
johnc@4300 341 "N_words: " UINT32_FORMAT,
johnc@4300 342 _array->offset_array(landing_card), N_words));
ysr@777 343 }
ysr@777 344 }
ysr@777 345 }
ysr@777 346
ysr@777 347 // The range [blk_start, blk_end) represents a single contiguous block
ysr@777 348 // of storage; modify the block offset table to represent this
ysr@777 349 // information; Right-open interval: [blk_start, blk_end)
ysr@777 350 // NOTE: this method does _not_ adjust _unallocated_block.
ysr@777 351 void
ysr@777 352 G1BlockOffsetArray::single_block(HeapWord* blk_start, HeapWord* blk_end) {
ysr@777 353 do_block_internal(blk_start, blk_end, Action_single);
ysr@777 354 }
ysr@777 355
ysr@777 356 // Mark the BOT such that if [blk_start, blk_end) straddles a card
ysr@777 357 // boundary, the card following the first such boundary is marked
ysr@777 358 // with the appropriate offset.
ysr@777 359 // NOTE: this method does _not_ adjust _unallocated_block or
ysr@777 360 // any cards subsequent to the first one.
ysr@777 361 void
ysr@777 362 G1BlockOffsetArray::mark_block(HeapWord* blk_start, HeapWord* blk_end) {
ysr@777 363 do_block_internal(blk_start, blk_end, Action_mark);
ysr@777 364 }
ysr@777 365
ysr@777 366 HeapWord* G1BlockOffsetArray::block_start_unsafe(const void* addr) {
ysr@777 367 assert(_bottom <= addr && addr < _end,
ysr@777 368 "addr must be covered by this Array");
ysr@777 369 // Must read this exactly once because it can be modified by parallel
ysr@777 370 // allocation.
ysr@777 371 HeapWord* ub = _unallocated_block;
ysr@777 372 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
ysr@777 373 assert(ub < _end, "tautology (see above)");
ysr@777 374 return ub;
ysr@777 375 }
ysr@777 376 // Otherwise, find the block start using the table.
ysr@777 377 HeapWord* q = block_at_or_preceding(addr, false, 0);
ysr@777 378 return forward_to_block_containing_addr(q, addr);
ysr@777 379 }
ysr@777 380
ysr@777 381 // This duplicates a little code from the above: unavoidable.
ysr@777 382 HeapWord*
ysr@777 383 G1BlockOffsetArray::block_start_unsafe_const(const void* addr) const {
ysr@777 384 assert(_bottom <= addr && addr < _end,
ysr@777 385 "addr must be covered by this Array");
ysr@777 386 // Must read this exactly once because it can be modified by parallel
ysr@777 387 // allocation.
ysr@777 388 HeapWord* ub = _unallocated_block;
ysr@777 389 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
ysr@777 390 assert(ub < _end, "tautology (see above)");
ysr@777 391 return ub;
ysr@777 392 }
ysr@777 393 // Otherwise, find the block start using the table.
ysr@777 394 HeapWord* q = block_at_or_preceding(addr, false, 0);
mgerdin@6987 395 HeapWord* n = q + block_size(q);
ysr@777 396 return forward_to_block_containing_addr_const(q, n, addr);
ysr@777 397 }
ysr@777 398
ysr@777 399
ysr@777 400 HeapWord*
ysr@777 401 G1BlockOffsetArray::forward_to_block_containing_addr_slow(HeapWord* q,
ysr@777 402 HeapWord* n,
ysr@777 403 const void* addr) {
ysr@777 404 // We're not in the normal case. We need to handle an important subcase
ysr@777 405 // here: LAB allocation. An allocation previously recorded in the
ysr@777 406 // offset table was actually a lab allocation, and was divided into
ysr@777 407 // several objects subsequently. Fix this situation as we answer the
ysr@777 408 // query, by updating entries as we cross them.
iveresov@787 409
iveresov@787 410 // If the fist object's end q is at the card boundary. Start refining
iveresov@787 411 // with the corresponding card (the value of the entry will be basically
iveresov@787 412 // set to 0). If the object crosses the boundary -- start from the next card.
coleenp@4037 413 size_t n_index = _array->index_for(n);
iveresov@787 414 size_t next_index = _array->index_for(n) + !_array->is_card_boundary(n);
coleenp@4037 415 // Calculate a consistent next boundary. If "n" is not at the boundary
coleenp@4037 416 // already, step to the boundary.
coleenp@4037 417 HeapWord* next_boundary = _array->address_for_index(n_index) +
coleenp@4037 418 (n_index == next_index ? 0 : N_words);
coleenp@4037 419 assert(next_boundary <= _array->_end,
coleenp@4037 420 err_msg("next_boundary is beyond the end of the covered region "
coleenp@4037 421 " next_boundary " PTR_FORMAT " _array->_end " PTR_FORMAT,
coleenp@4037 422 next_boundary, _array->_end));
mgerdin@6987 423 if (addr >= gsp()->top()) return gsp()->top();
mgerdin@6987 424 while (next_boundary < addr) {
mgerdin@6987 425 while (n <= next_boundary) {
mgerdin@6987 426 q = n;
mgerdin@6987 427 oop obj = oop(q);
mgerdin@6987 428 if (obj->klass_or_null() == NULL) return q;
stefank@6992 429 n += block_size(q);
ysr@777 430 }
mgerdin@6987 431 assert(q <= next_boundary && n > next_boundary, "Consequence of loop");
mgerdin@6987 432 // [q, n) is the block that crosses the boundary.
mgerdin@6987 433 alloc_block_work2(&next_boundary, &next_index, q, n);
ysr@777 434 }
ysr@777 435 return forward_to_block_containing_addr_const(q, n, addr);
ysr@777 436 }
ysr@777 437
ysr@777 438 HeapWord* G1BlockOffsetArray::block_start_careful(const void* addr) const {
ysr@777 439 assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
ysr@777 440
ysr@777 441 assert(_bottom <= addr && addr < _end,
ysr@777 442 "addr must be covered by this Array");
ysr@777 443 // Must read this exactly once because it can be modified by parallel
ysr@777 444 // allocation.
ysr@777 445 HeapWord* ub = _unallocated_block;
ysr@777 446 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
ysr@777 447 assert(ub < _end, "tautology (see above)");
ysr@777 448 return ub;
ysr@777 449 }
ysr@777 450
ysr@777 451 // Otherwise, find the block start using the table, but taking
ysr@777 452 // care (cf block_start_unsafe() above) not to parse any objects/blocks
ysr@777 453 // on the cards themsleves.
ysr@777 454 size_t index = _array->index_for(addr);
ysr@777 455 assert(_array->address_for_index(index) == addr,
ysr@777 456 "arg should be start of card");
ysr@777 457
ysr@777 458 HeapWord* q = (HeapWord*)addr;
ysr@777 459 uint offset;
ysr@777 460 do {
ysr@777 461 offset = _array->offset_array(index--);
ysr@777 462 q -= offset;
ysr@777 463 } while (offset == N_words);
ysr@777 464 assert(q <= addr, "block start should be to left of arg");
ysr@777 465 return q;
ysr@777 466 }
ysr@777 467
ysr@777 468 // Note that the committed size of the covered space may have changed,
ysr@777 469 // so the table size might also wish to change.
ysr@777 470 void G1BlockOffsetArray::resize(size_t new_word_size) {
ysr@777 471 HeapWord* new_end = _bottom + new_word_size;
ysr@777 472 if (_end < new_end && !init_to_zero()) {
ysr@777 473 // verify that the old and new boundaries are also card boundaries
ysr@777 474 assert(_array->is_card_boundary(_end),
ysr@777 475 "_end not a card boundary");
ysr@777 476 assert(_array->is_card_boundary(new_end),
ysr@777 477 "new _end would not be a card boundary");
ysr@777 478 // set all the newly added cards
ysr@777 479 _array->set_offset_array(_end, new_end, N_words);
ysr@777 480 }
ysr@777 481 _end = new_end; // update _end
ysr@777 482 }
ysr@777 483
ysr@777 484 void G1BlockOffsetArray::set_region(MemRegion mr) {
ysr@777 485 _bottom = mr.start();
ysr@777 486 _end = mr.end();
ysr@777 487 }
ysr@777 488
ysr@777 489 //
ysr@777 490 // threshold_
ysr@777 491 // | _index_
ysr@777 492 // v v
ysr@777 493 // +-------+-------+-------+-------+-------+
ysr@777 494 // | i-1 | i | i+1 | i+2 | i+3 |
ysr@777 495 // +-------+-------+-------+-------+-------+
ysr@777 496 // ( ^ ]
ysr@777 497 // block-start
ysr@777 498 //
ysr@777 499 void G1BlockOffsetArray::alloc_block_work2(HeapWord** threshold_, size_t* index_,
ysr@777 500 HeapWord* blk_start, HeapWord* blk_end) {
ysr@777 501 // For efficiency, do copy-in/copy-out.
ysr@777 502 HeapWord* threshold = *threshold_;
ysr@777 503 size_t index = *index_;
ysr@777 504
ysr@777 505 assert(blk_start != NULL && blk_end > blk_start,
ysr@777 506 "phantom block");
ysr@777 507 assert(blk_end > threshold, "should be past threshold");
jcoomes@1844 508 assert(blk_start <= threshold, "blk_start should be at or before threshold");
ysr@777 509 assert(pointer_delta(threshold, blk_start) <= N_words,
ysr@777 510 "offset should be <= BlockOffsetSharedArray::N");
ysr@777 511 assert(Universe::heap()->is_in_reserved(blk_start),
ysr@777 512 "reference must be into the heap");
ysr@777 513 assert(Universe::heap()->is_in_reserved(blk_end-1),
ysr@777 514 "limit must be within the heap");
ysr@777 515 assert(threshold == _array->_reserved.start() + index*N_words,
ysr@777 516 "index must agree with threshold");
ysr@777 517
ysr@777 518 DEBUG_ONLY(size_t orig_index = index;)
ysr@777 519
ysr@777 520 // Mark the card that holds the offset into the block. Note
ysr@777 521 // that _next_offset_index and _next_offset_threshold are not
ysr@777 522 // updated until the end of this method.
ysr@777 523 _array->set_offset_array(index, threshold, blk_start);
ysr@777 524
ysr@777 525 // We need to now mark the subsequent cards that this blk spans.
ysr@777 526
ysr@777 527 // Index of card on which blk ends.
ysr@777 528 size_t end_index = _array->index_for(blk_end - 1);
ysr@777 529
ysr@777 530 // Are there more cards left to be updated?
ysr@777 531 if (index + 1 <= end_index) {
ysr@777 532 HeapWord* rem_st = _array->address_for_index(index + 1);
ysr@777 533 // Calculate rem_end this way because end_index
ysr@777 534 // may be the last valid index in the covered region.
ysr@777 535 HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
ysr@777 536 set_remainder_to_point_to_start(rem_st, rem_end);
ysr@777 537 }
ysr@777 538
ysr@777 539 index = end_index + 1;
ysr@777 540 // Calculate threshold_ this way because end_index
ysr@777 541 // may be the last valid index in the covered region.
ysr@777 542 threshold = _array->address_for_index(end_index) + N_words;
ysr@777 543 assert(threshold >= blk_end, "Incorrect offset threshold");
ysr@777 544
ysr@777 545 // index_ and threshold_ updated here.
ysr@777 546 *threshold_ = threshold;
ysr@777 547 *index_ = index;
ysr@777 548
ysr@777 549 #ifdef ASSERT
ysr@777 550 // The offset can be 0 if the block starts on a boundary. That
ysr@777 551 // is checked by an assertion above.
ysr@777 552 size_t start_index = _array->index_for(blk_start);
johnc@4300 553 HeapWord* boundary = _array->address_for_index(start_index);
ysr@777 554 assert((_array->offset_array(orig_index) == 0 &&
ysr@777 555 blk_start == boundary) ||
ysr@777 556 (_array->offset_array(orig_index) > 0 &&
ysr@777 557 _array->offset_array(orig_index) <= N_words),
johnc@4300 558 err_msg("offset array should have been set - "
johnc@4300 559 "orig_index offset: " UINT32_FORMAT ", "
johnc@4300 560 "blk_start: " PTR_FORMAT ", "
johnc@4300 561 "boundary: " PTR_FORMAT,
johnc@4300 562 _array->offset_array(orig_index),
johnc@4300 563 blk_start, boundary));
ysr@777 564 for (size_t j = orig_index + 1; j <= end_index; j++) {
ysr@777 565 assert(_array->offset_array(j) > 0 &&
ysr@777 566 _array->offset_array(j) <=
ysr@777 567 (u_char) (N_words+BlockOffsetArray::N_powers-1),
johnc@4300 568 err_msg("offset array should have been set - "
johnc@4300 569 UINT32_FORMAT " not > 0 OR "
johnc@4300 570 UINT32_FORMAT " not <= " UINT32_FORMAT,
johnc@4300 571 _array->offset_array(j),
johnc@4300 572 _array->offset_array(j),
johnc@4300 573 (u_char) (N_words+BlockOffsetArray::N_powers-1)));
ysr@777 574 }
ysr@777 575 #endif
ysr@777 576 }
ysr@777 577
tonyp@2453 578 bool
tonyp@2453 579 G1BlockOffsetArray::verify_for_object(HeapWord* obj_start,
tonyp@2453 580 size_t word_size) const {
tonyp@2453 581 size_t first_card = _array->index_for(obj_start);
tonyp@2453 582 size_t last_card = _array->index_for(obj_start + word_size - 1);
tonyp@2453 583 if (!_array->is_card_boundary(obj_start)) {
tonyp@2453 584 // If the object is not on a card boundary the BOT entry of the
tonyp@2453 585 // first card should point to another object so we should not
tonyp@2453 586 // check that one.
tonyp@2453 587 first_card += 1;
tonyp@2453 588 }
tonyp@2453 589 for (size_t card = first_card; card <= last_card; card += 1) {
tonyp@2453 590 HeapWord* card_addr = _array->address_for_index(card);
tonyp@2453 591 HeapWord* block_start = block_start_const(card_addr);
tonyp@2453 592 if (block_start != obj_start) {
tonyp@2453 593 gclog_or_tty->print_cr("block start: "PTR_FORMAT" is incorrect - "
tonyp@2453 594 "card index: "SIZE_FORMAT" "
tonyp@2453 595 "card addr: "PTR_FORMAT" BOT entry: %u "
tonyp@2453 596 "obj: "PTR_FORMAT" word size: "SIZE_FORMAT" "
tonyp@2453 597 "cards: ["SIZE_FORMAT","SIZE_FORMAT"]",
tonyp@2453 598 block_start, card, card_addr,
tonyp@2453 599 _array->offset_array(card),
tonyp@2453 600 obj_start, word_size, first_card, last_card);
tonyp@2453 601 return false;
tonyp@2453 602 }
tonyp@2453 603 }
tonyp@2453 604 return true;
tonyp@2453 605 }
tonyp@2453 606
tonyp@2453 607 #ifndef PRODUCT
tonyp@2241 608 void
tonyp@2453 609 G1BlockOffsetArray::print_on(outputStream* out) {
tonyp@2453 610 size_t from_index = _array->index_for(_bottom);
tonyp@2453 611 size_t to_index = _array->index_for(_end);
tonyp@2453 612 out->print_cr(">> BOT for area ["PTR_FORMAT","PTR_FORMAT") "
tonyp@2453 613 "cards ["SIZE_FORMAT","SIZE_FORMAT")",
tonyp@2453 614 _bottom, _end, from_index, to_index);
tonyp@2453 615 for (size_t i = from_index; i < to_index; ++i) {
tonyp@2453 616 out->print_cr(" entry "SIZE_FORMAT_W(8)" | "PTR_FORMAT" : %3u",
tonyp@2453 617 i, _array->address_for_index(i),
tonyp@2453 618 (uint) _array->offset_array(i));
tonyp@2453 619 }
tonyp@2241 620 }
tonyp@2453 621 #endif // !PRODUCT
tonyp@2241 622
ysr@777 623 //////////////////////////////////////////////////////////////////////
ysr@777 624 // G1BlockOffsetArrayContigSpace
ysr@777 625 //////////////////////////////////////////////////////////////////////
ysr@777 626
ysr@777 627 HeapWord*
ysr@777 628 G1BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) {
ysr@777 629 assert(_bottom <= addr && addr < _end,
ysr@777 630 "addr must be covered by this Array");
ysr@777 631 HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);
ysr@777 632 return forward_to_block_containing_addr(q, addr);
ysr@777 633 }
ysr@777 634
ysr@777 635 HeapWord*
ysr@777 636 G1BlockOffsetArrayContigSpace::
ysr@777 637 block_start_unsafe_const(const void* addr) const {
ysr@777 638 assert(_bottom <= addr && addr < _end,
ysr@777 639 "addr must be covered by this Array");
ysr@777 640 HeapWord* q = block_at_or_preceding(addr, true, _next_offset_index-1);
mgerdin@6987 641 HeapWord* n = q + block_size(q);
ysr@777 642 return forward_to_block_containing_addr_const(q, n, addr);
ysr@777 643 }
ysr@777 644
ysr@777 645 G1BlockOffsetArrayContigSpace::
ysr@777 646 G1BlockOffsetArrayContigSpace(G1BlockOffsetSharedArray* array,
ysr@777 647 MemRegion mr) :
ysr@777 648 G1BlockOffsetArray(array, mr, true)
ysr@777 649 {
ysr@777 650 _next_offset_threshold = NULL;
ysr@777 651 _next_offset_index = 0;
ysr@777 652 }
ysr@777 653
ysr@777 654 HeapWord* G1BlockOffsetArrayContigSpace::initialize_threshold() {
ysr@777 655 assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
ysr@777 656 "just checking");
ysr@777 657 _next_offset_index = _array->index_for(_bottom);
ysr@777 658 _next_offset_index++;
ysr@777 659 _next_offset_threshold =
ysr@777 660 _array->address_for_index(_next_offset_index);
ysr@777 661 return _next_offset_threshold;
ysr@777 662 }
ysr@777 663
ysr@777 664 void G1BlockOffsetArrayContigSpace::zero_bottom_entry() {
ysr@777 665 assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
ysr@777 666 "just checking");
ysr@777 667 size_t bottom_index = _array->index_for(_bottom);
ysr@777 668 assert(_array->address_for_index(bottom_index) == _bottom,
ysr@777 669 "Precondition of call");
ysr@777 670 _array->set_offset_array(bottom_index, 0);
ysr@777 671 }
tonyp@2241 672
tonyp@2241 673 void
tonyp@2453 674 G1BlockOffsetArrayContigSpace::set_for_starts_humongous(HeapWord* new_top) {
tonyp@2453 675 assert(new_top <= _end, "_end should have already been updated");
tonyp@2241 676
tonyp@2453 677 // The first BOT entry should have offset 0.
tonyp@2453 678 zero_bottom_entry();
tonyp@2453 679 initialize_threshold();
tonyp@2453 680 alloc_block(_bottom, new_top);
tonyp@2453 681 }
tonyp@2453 682
tonyp@2453 683 #ifndef PRODUCT
tonyp@2453 684 void
tonyp@2453 685 G1BlockOffsetArrayContigSpace::print_on(outputStream* out) {
tonyp@2453 686 G1BlockOffsetArray::print_on(out);
tonyp@2453 687 out->print_cr(" next offset threshold: "PTR_FORMAT, _next_offset_threshold);
tonyp@2453 688 out->print_cr(" next offset index: "SIZE_FORMAT, _next_offset_index);
tonyp@2241 689 }
tonyp@2453 690 #endif // !PRODUCT

mercurial