src/share/vm/memory/blockOffsetTable.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6876
710a3c8b516e
child 9448
73d689add964
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "gc_interface/collectedHeap.inline.hpp"
aoqi@0 27 #include "memory/blockOffsetTable.inline.hpp"
aoqi@0 28 #include "memory/iterator.hpp"
aoqi@0 29 #include "memory/space.inline.hpp"
aoqi@0 30 #include "memory/universe.hpp"
aoqi@0 31 #include "oops/oop.inline.hpp"
aoqi@0 32 #include "runtime/java.hpp"
aoqi@0 33 #include "services/memTracker.hpp"
aoqi@0 34
aoqi@0 35 //////////////////////////////////////////////////////////////////////
aoqi@0 36 // BlockOffsetSharedArray
aoqi@0 37 //////////////////////////////////////////////////////////////////////
aoqi@0 38
aoqi@0 39 BlockOffsetSharedArray::BlockOffsetSharedArray(MemRegion reserved,
aoqi@0 40 size_t init_word_size):
aoqi@0 41 _reserved(reserved), _end(NULL)
aoqi@0 42 {
aoqi@0 43 size_t size = compute_size(reserved.word_size());
aoqi@0 44 ReservedSpace rs(size);
aoqi@0 45 if (!rs.is_reserved()) {
aoqi@0 46 vm_exit_during_initialization("Could not reserve enough space for heap offset array");
aoqi@0 47 }
aoqi@0 48
aoqi@0 49 MemTracker::record_virtual_memory_type((address)rs.base(), mtGC);
aoqi@0 50
aoqi@0 51 if (!_vs.initialize(rs, 0)) {
aoqi@0 52 vm_exit_during_initialization("Could not reserve enough space for heap offset array");
aoqi@0 53 }
aoqi@0 54 _offset_array = (u_char*)_vs.low_boundary();
aoqi@0 55 resize(init_word_size);
aoqi@0 56 if (TraceBlockOffsetTable) {
aoqi@0 57 gclog_or_tty->print_cr("BlockOffsetSharedArray::BlockOffsetSharedArray: ");
aoqi@0 58 gclog_or_tty->print_cr(" "
aoqi@0 59 " rs.base(): " INTPTR_FORMAT
aoqi@0 60 " rs.size(): " INTPTR_FORMAT
aoqi@0 61 " rs end(): " INTPTR_FORMAT,
aoqi@0 62 p2i(rs.base()), rs.size(), p2i(rs.base() + rs.size()));
aoqi@0 63 gclog_or_tty->print_cr(" "
aoqi@0 64 " _vs.low_boundary(): " INTPTR_FORMAT
aoqi@0 65 " _vs.high_boundary(): " INTPTR_FORMAT,
aoqi@0 66 p2i(_vs.low_boundary()),
aoqi@0 67 p2i(_vs.high_boundary()));
aoqi@0 68 }
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 void BlockOffsetSharedArray::resize(size_t new_word_size) {
aoqi@0 72 assert(new_word_size <= _reserved.word_size(), "Resize larger than reserved");
aoqi@0 73 size_t new_size = compute_size(new_word_size);
aoqi@0 74 size_t old_size = _vs.committed_size();
aoqi@0 75 size_t delta;
aoqi@0 76 char* high = _vs.high();
aoqi@0 77 _end = _reserved.start() + new_word_size;
aoqi@0 78 if (new_size > old_size) {
aoqi@0 79 delta = ReservedSpace::page_align_size_up(new_size - old_size);
aoqi@0 80 assert(delta > 0, "just checking");
aoqi@0 81 if (!_vs.expand_by(delta)) {
aoqi@0 82 // Do better than this for Merlin
aoqi@0 83 vm_exit_out_of_memory(delta, OOM_MMAP_ERROR, "offset table expansion");
aoqi@0 84 }
aoqi@0 85 assert(_vs.high() == high + delta, "invalid expansion");
aoqi@0 86 } else {
aoqi@0 87 delta = ReservedSpace::page_align_size_down(old_size - new_size);
aoqi@0 88 if (delta == 0) return;
aoqi@0 89 _vs.shrink_by(delta);
aoqi@0 90 assert(_vs.high() == high - delta, "invalid expansion");
aoqi@0 91 }
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 bool BlockOffsetSharedArray::is_card_boundary(HeapWord* p) const {
aoqi@0 95 assert(p >= _reserved.start(), "just checking");
aoqi@0 96 size_t delta = pointer_delta(p, _reserved.start());
aoqi@0 97 return (delta & right_n_bits(LogN_words)) == (size_t)NoBits;
aoqi@0 98 }
aoqi@0 99
aoqi@0 100
aoqi@0 101 //////////////////////////////////////////////////////////////////////
aoqi@0 102 // BlockOffsetArray
aoqi@0 103 //////////////////////////////////////////////////////////////////////
aoqi@0 104
aoqi@0 105 BlockOffsetArray::BlockOffsetArray(BlockOffsetSharedArray* array,
aoqi@0 106 MemRegion mr, bool init_to_zero_) :
aoqi@0 107 BlockOffsetTable(mr.start(), mr.end()),
aoqi@0 108 _array(array)
aoqi@0 109 {
aoqi@0 110 assert(_bottom <= _end, "arguments out of order");
aoqi@0 111 set_init_to_zero(init_to_zero_);
aoqi@0 112 if (!init_to_zero_) {
aoqi@0 113 // initialize cards to point back to mr.start()
aoqi@0 114 set_remainder_to_point_to_start(mr.start() + N_words, mr.end());
aoqi@0 115 _array->set_offset_array(0, 0); // set first card to 0
aoqi@0 116 }
aoqi@0 117 }
aoqi@0 118
aoqi@0 119
aoqi@0 120 // The arguments follow the normal convention of denoting
aoqi@0 121 // a right-open interval: [start, end)
aoqi@0 122 void
aoqi@0 123 BlockOffsetArray::
aoqi@0 124 set_remainder_to_point_to_start(HeapWord* start, HeapWord* end, bool reducing) {
aoqi@0 125
aoqi@0 126 check_reducing_assertion(reducing);
aoqi@0 127 if (start >= end) {
aoqi@0 128 // The start address is equal to the end address (or to
aoqi@0 129 // the right of the end address) so there are not cards
aoqi@0 130 // that need to be updated..
aoqi@0 131 return;
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 // Write the backskip value for each region.
aoqi@0 135 //
aoqi@0 136 // offset
aoqi@0 137 // card 2nd 3rd
aoqi@0 138 // | +- 1st | |
aoqi@0 139 // v v v v
aoqi@0 140 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-
aoqi@0 141 // |x|0|0|0|0|0|0|0|1|1|1|1|1|1| ... |1|1|1|1|2|2|2|2|2|2| ...
aoqi@0 142 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+-+-+-
aoqi@0 143 // 11 19 75
aoqi@0 144 // 12
aoqi@0 145 //
aoqi@0 146 // offset card is the card that points to the start of an object
aoqi@0 147 // x - offset value of offset card
aoqi@0 148 // 1st - start of first logarithmic region
aoqi@0 149 // 0 corresponds to logarithmic value N_words + 0 and 2**(3 * 0) = 1
aoqi@0 150 // 2nd - start of second logarithmic region
aoqi@0 151 // 1 corresponds to logarithmic value N_words + 1 and 2**(3 * 1) = 8
aoqi@0 152 // 3rd - start of third logarithmic region
aoqi@0 153 // 2 corresponds to logarithmic value N_words + 2 and 2**(3 * 2) = 64
aoqi@0 154 //
aoqi@0 155 // integer below the block offset entry is an example of
aoqi@0 156 // the index of the entry
aoqi@0 157 //
aoqi@0 158 // Given an address,
aoqi@0 159 // Find the index for the address
aoqi@0 160 // Find the block offset table entry
aoqi@0 161 // Convert the entry to a back slide
aoqi@0 162 // (e.g., with today's, offset = 0x81 =>
aoqi@0 163 // back slip = 2**(3*(0x81 - N_words)) = 2**3) = 8
aoqi@0 164 // Move back N (e.g., 8) entries and repeat with the
aoqi@0 165 // value of the new entry
aoqi@0 166 //
aoqi@0 167 size_t start_card = _array->index_for(start);
aoqi@0 168 size_t end_card = _array->index_for(end-1);
aoqi@0 169 assert(start ==_array->address_for_index(start_card), "Precondition");
aoqi@0 170 assert(end ==_array->address_for_index(end_card)+N_words, "Precondition");
aoqi@0 171 set_remainder_to_point_to_start_incl(start_card, end_card, reducing); // closed interval
aoqi@0 172 }
aoqi@0 173
aoqi@0 174
aoqi@0 175 // Unlike the normal convention in this code, the argument here denotes
aoqi@0 176 // a closed, inclusive interval: [start_card, end_card], cf set_remainder_to_point_to_start()
aoqi@0 177 // above.
aoqi@0 178 void
aoqi@0 179 BlockOffsetArray::set_remainder_to_point_to_start_incl(size_t start_card, size_t end_card, bool reducing) {
aoqi@0 180
aoqi@0 181 check_reducing_assertion(reducing);
aoqi@0 182 if (start_card > end_card) {
aoqi@0 183 return;
aoqi@0 184 }
aoqi@0 185 assert(start_card > _array->index_for(_bottom), "Cannot be first card");
aoqi@0 186 assert(_array->offset_array(start_card-1) <= N_words,
aoqi@0 187 "Offset card has an unexpected value");
aoqi@0 188 size_t start_card_for_region = start_card;
aoqi@0 189 u_char offset = max_jubyte;
aoqi@0 190 for (int i = 0; i < N_powers; i++) {
aoqi@0 191 // -1 so that the the card with the actual offset is counted. Another -1
aoqi@0 192 // so that the reach ends in this region and not at the start
aoqi@0 193 // of the next.
aoqi@0 194 size_t reach = start_card - 1 + (power_to_cards_back(i+1) - 1);
aoqi@0 195 offset = N_words + i;
aoqi@0 196 if (reach >= end_card) {
aoqi@0 197 _array->set_offset_array(start_card_for_region, end_card, offset, reducing);
aoqi@0 198 start_card_for_region = reach + 1;
aoqi@0 199 break;
aoqi@0 200 }
aoqi@0 201 _array->set_offset_array(start_card_for_region, reach, offset, reducing);
aoqi@0 202 start_card_for_region = reach + 1;
aoqi@0 203 }
aoqi@0 204 assert(start_card_for_region > end_card, "Sanity check");
aoqi@0 205 DEBUG_ONLY(check_all_cards(start_card, end_card);)
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 // The card-interval [start_card, end_card] is a closed interval; this
aoqi@0 209 // is an expensive check -- use with care and only under protection of
aoqi@0 210 // suitable flag.
aoqi@0 211 void BlockOffsetArray::check_all_cards(size_t start_card, size_t end_card) const {
aoqi@0 212
aoqi@0 213 if (end_card < start_card) {
aoqi@0 214 return;
aoqi@0 215 }
aoqi@0 216 guarantee(_array->offset_array(start_card) == N_words, "Wrong value in second card");
aoqi@0 217 u_char last_entry = N_words;
aoqi@0 218 for (size_t c = start_card + 1; c <= end_card; c++ /* yeah! */) {
aoqi@0 219 u_char entry = _array->offset_array(c);
aoqi@0 220 guarantee(entry >= last_entry, "Monotonicity");
aoqi@0 221 if (c - start_card > power_to_cards_back(1)) {
aoqi@0 222 guarantee(entry > N_words, "Should be in logarithmic region");
aoqi@0 223 }
aoqi@0 224 size_t backskip = entry_to_cards_back(entry);
aoqi@0 225 size_t landing_card = c - backskip;
aoqi@0 226 guarantee(landing_card >= (start_card - 1), "Inv");
aoqi@0 227 if (landing_card >= start_card) {
aoqi@0 228 guarantee(_array->offset_array(landing_card) <= entry, "Monotonicity");
aoqi@0 229 } else {
aoqi@0 230 guarantee(landing_card == (start_card - 1), "Tautology");
aoqi@0 231 // Note that N_words is the maximum offset value
aoqi@0 232 guarantee(_array->offset_array(landing_card) <= N_words, "Offset value");
aoqi@0 233 }
aoqi@0 234 last_entry = entry; // remember for monotonicity test
aoqi@0 235 }
aoqi@0 236 }
aoqi@0 237
aoqi@0 238
aoqi@0 239 void
aoqi@0 240 BlockOffsetArray::alloc_block(HeapWord* blk_start, HeapWord* blk_end) {
aoqi@0 241 assert(blk_start != NULL && blk_end > blk_start,
aoqi@0 242 "phantom block");
aoqi@0 243 single_block(blk_start, blk_end);
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 // Action_mark - update the BOT for the block [blk_start, blk_end).
aoqi@0 247 // Current typical use is for splitting a block.
aoqi@0 248 // Action_single - udpate the BOT for an allocation.
aoqi@0 249 // Action_verify - BOT verification.
aoqi@0 250 void
aoqi@0 251 BlockOffsetArray::do_block_internal(HeapWord* blk_start,
aoqi@0 252 HeapWord* blk_end,
aoqi@0 253 Action action, bool reducing) {
aoqi@0 254 assert(Universe::heap()->is_in_reserved(blk_start),
aoqi@0 255 "reference must be into the heap");
aoqi@0 256 assert(Universe::heap()->is_in_reserved(blk_end-1),
aoqi@0 257 "limit must be within the heap");
aoqi@0 258 // This is optimized to make the test fast, assuming we only rarely
aoqi@0 259 // cross boundaries.
aoqi@0 260 uintptr_t end_ui = (uintptr_t)(blk_end - 1);
aoqi@0 261 uintptr_t start_ui = (uintptr_t)blk_start;
aoqi@0 262 // Calculate the last card boundary preceding end of blk
aoqi@0 263 intptr_t boundary_before_end = (intptr_t)end_ui;
aoqi@0 264 clear_bits(boundary_before_end, right_n_bits(LogN));
aoqi@0 265 if (start_ui <= (uintptr_t)boundary_before_end) {
aoqi@0 266 // blk starts at or crosses a boundary
aoqi@0 267 // Calculate index of card on which blk begins
aoqi@0 268 size_t start_index = _array->index_for(blk_start);
aoqi@0 269 // Index of card on which blk ends
aoqi@0 270 size_t end_index = _array->index_for(blk_end - 1);
aoqi@0 271 // Start address of card on which blk begins
aoqi@0 272 HeapWord* boundary = _array->address_for_index(start_index);
aoqi@0 273 assert(boundary <= blk_start, "blk should start at or after boundary");
aoqi@0 274 if (blk_start != boundary) {
aoqi@0 275 // blk starts strictly after boundary
aoqi@0 276 // adjust card boundary and start_index forward to next card
aoqi@0 277 boundary += N_words;
aoqi@0 278 start_index++;
aoqi@0 279 }
aoqi@0 280 assert(start_index <= end_index, "monotonicity of index_for()");
aoqi@0 281 assert(boundary <= (HeapWord*)boundary_before_end, "tautology");
aoqi@0 282 switch (action) {
aoqi@0 283 case Action_mark: {
aoqi@0 284 if (init_to_zero()) {
aoqi@0 285 _array->set_offset_array(start_index, boundary, blk_start, reducing);
aoqi@0 286 break;
aoqi@0 287 } // Else fall through to the next case
aoqi@0 288 }
aoqi@0 289 case Action_single: {
aoqi@0 290 _array->set_offset_array(start_index, boundary, blk_start, reducing);
aoqi@0 291 // We have finished marking the "offset card". We need to now
aoqi@0 292 // mark the subsequent cards that this blk spans.
aoqi@0 293 if (start_index < end_index) {
aoqi@0 294 HeapWord* rem_st = _array->address_for_index(start_index) + N_words;
aoqi@0 295 HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
aoqi@0 296 set_remainder_to_point_to_start(rem_st, rem_end, reducing);
aoqi@0 297 }
aoqi@0 298 break;
aoqi@0 299 }
aoqi@0 300 case Action_check: {
aoqi@0 301 _array->check_offset_array(start_index, boundary, blk_start);
aoqi@0 302 // We have finished checking the "offset card". We need to now
aoqi@0 303 // check the subsequent cards that this blk spans.
aoqi@0 304 check_all_cards(start_index + 1, end_index);
aoqi@0 305 break;
aoqi@0 306 }
aoqi@0 307 default:
aoqi@0 308 ShouldNotReachHere();
aoqi@0 309 }
aoqi@0 310 }
aoqi@0 311 }
aoqi@0 312
aoqi@0 313 // The range [blk_start, blk_end) represents a single contiguous block
aoqi@0 314 // of storage; modify the block offset table to represent this
aoqi@0 315 // information; Right-open interval: [blk_start, blk_end)
aoqi@0 316 // NOTE: this method does _not_ adjust _unallocated_block.
aoqi@0 317 void
aoqi@0 318 BlockOffsetArray::single_block(HeapWord* blk_start,
aoqi@0 319 HeapWord* blk_end) {
aoqi@0 320 do_block_internal(blk_start, blk_end, Action_single);
aoqi@0 321 }
aoqi@0 322
aoqi@0 323 void BlockOffsetArray::verify() const {
aoqi@0 324 // For each entry in the block offset table, verify that
aoqi@0 325 // the entry correctly finds the start of an object at the
aoqi@0 326 // first address covered by the block or to the left of that
aoqi@0 327 // first address.
aoqi@0 328
aoqi@0 329 size_t next_index = 1;
aoqi@0 330 size_t last_index = last_active_index();
aoqi@0 331
aoqi@0 332 // Use for debugging. Initialize to NULL to distinguish the
aoqi@0 333 // first iteration through the while loop.
aoqi@0 334 HeapWord* last_p = NULL;
aoqi@0 335 HeapWord* last_start = NULL;
aoqi@0 336 oop last_o = NULL;
aoqi@0 337
aoqi@0 338 while (next_index <= last_index) {
aoqi@0 339 // Use an address past the start of the address for
aoqi@0 340 // the entry.
aoqi@0 341 HeapWord* p = _array->address_for_index(next_index) + 1;
aoqi@0 342 if (p >= _end) {
aoqi@0 343 // That's all of the allocated block table.
aoqi@0 344 return;
aoqi@0 345 }
aoqi@0 346 // block_start() asserts that start <= p.
aoqi@0 347 HeapWord* start = block_start(p);
aoqi@0 348 // First check if the start is an allocated block and only
aoqi@0 349 // then if it is a valid object.
aoqi@0 350 oop o = oop(start);
aoqi@0 351 assert(!Universe::is_fully_initialized() ||
aoqi@0 352 _sp->is_free_block(start) ||
aoqi@0 353 o->is_oop_or_null(), "Bad object was found");
aoqi@0 354 next_index++;
aoqi@0 355 last_p = p;
aoqi@0 356 last_start = start;
aoqi@0 357 last_o = o;
aoqi@0 358 }
aoqi@0 359 }
aoqi@0 360
aoqi@0 361 //////////////////////////////////////////////////////////////////////
aoqi@0 362 // BlockOffsetArrayNonContigSpace
aoqi@0 363 //////////////////////////////////////////////////////////////////////
aoqi@0 364
aoqi@0 365 // The block [blk_start, blk_end) has been allocated;
aoqi@0 366 // adjust the block offset table to represent this information;
aoqi@0 367 // NOTE: Clients of BlockOffsetArrayNonContigSpace: consider using
aoqi@0 368 // the somewhat more lightweight split_block() or
aoqi@0 369 // (when init_to_zero()) mark_block() wherever possible.
aoqi@0 370 // right-open interval: [blk_start, blk_end)
aoqi@0 371 void
aoqi@0 372 BlockOffsetArrayNonContigSpace::alloc_block(HeapWord* blk_start,
aoqi@0 373 HeapWord* blk_end) {
aoqi@0 374 assert(blk_start != NULL && blk_end > blk_start,
aoqi@0 375 "phantom block");
aoqi@0 376 single_block(blk_start, blk_end);
aoqi@0 377 allocated(blk_start, blk_end);
aoqi@0 378 }
aoqi@0 379
aoqi@0 380 // Adjust BOT to show that a previously whole block has been split
aoqi@0 381 // into two. We verify the BOT for the first part (prefix) and
aoqi@0 382 // update the BOT for the second part (suffix).
aoqi@0 383 // blk is the start of the block
aoqi@0 384 // blk_size is the size of the original block
aoqi@0 385 // left_blk_size is the size of the first part of the split
aoqi@0 386 void BlockOffsetArrayNonContigSpace::split_block(HeapWord* blk,
aoqi@0 387 size_t blk_size,
aoqi@0 388 size_t left_blk_size) {
aoqi@0 389 // Verify that the BOT shows [blk, blk + blk_size) to be one block.
aoqi@0 390 verify_single_block(blk, blk_size);
aoqi@0 391 // Update the BOT to indicate that [blk + left_blk_size, blk + blk_size)
aoqi@0 392 // is one single block.
aoqi@0 393 assert(blk_size > 0, "Should be positive");
aoqi@0 394 assert(left_blk_size > 0, "Should be positive");
aoqi@0 395 assert(left_blk_size < blk_size, "Not a split");
aoqi@0 396
aoqi@0 397 // Start addresses of prefix block and suffix block.
aoqi@0 398 HeapWord* pref_addr = blk;
aoqi@0 399 HeapWord* suff_addr = blk + left_blk_size;
aoqi@0 400 HeapWord* end_addr = blk + blk_size;
aoqi@0 401
aoqi@0 402 // Indices for starts of prefix block and suffix block.
aoqi@0 403 size_t pref_index = _array->index_for(pref_addr);
aoqi@0 404 if (_array->address_for_index(pref_index) != pref_addr) {
aoqi@0 405 // pref_addr does not begin pref_index
aoqi@0 406 pref_index++;
aoqi@0 407 }
aoqi@0 408
aoqi@0 409 size_t suff_index = _array->index_for(suff_addr);
aoqi@0 410 if (_array->address_for_index(suff_index) != suff_addr) {
aoqi@0 411 // suff_addr does not begin suff_index
aoqi@0 412 suff_index++;
aoqi@0 413 }
aoqi@0 414
aoqi@0 415 // Definition: A block B, denoted [B_start, B_end) __starts__
aoqi@0 416 // a card C, denoted [C_start, C_end), where C_start and C_end
aoqi@0 417 // are the heap addresses that card C covers, iff
aoqi@0 418 // B_start <= C_start < B_end.
aoqi@0 419 //
aoqi@0 420 // We say that a card C "is started by" a block B, iff
aoqi@0 421 // B "starts" C.
aoqi@0 422 //
aoqi@0 423 // Note that the cardinality of the set of cards {C}
aoqi@0 424 // started by a block B can be 0, 1, or more.
aoqi@0 425 //
aoqi@0 426 // Below, pref_index and suff_index are, respectively, the
aoqi@0 427 // first (least) card indices that the prefix and suffix of
aoqi@0 428 // the split start; end_index is one more than the index of
aoqi@0 429 // the last (greatest) card that blk starts.
aoqi@0 430 size_t end_index = _array->index_for(end_addr - 1) + 1;
aoqi@0 431
aoqi@0 432 // Calculate the # cards that the prefix and suffix affect.
aoqi@0 433 size_t num_pref_cards = suff_index - pref_index;
aoqi@0 434
aoqi@0 435 size_t num_suff_cards = end_index - suff_index;
aoqi@0 436 // Change the cards that need changing
aoqi@0 437 if (num_suff_cards > 0) {
aoqi@0 438 HeapWord* boundary = _array->address_for_index(suff_index);
aoqi@0 439 // Set the offset card for suffix block
aoqi@0 440 _array->set_offset_array(suff_index, boundary, suff_addr, true /* reducing */);
aoqi@0 441 // Change any further cards that need changing in the suffix
aoqi@0 442 if (num_pref_cards > 0) {
aoqi@0 443 if (num_pref_cards >= num_suff_cards) {
aoqi@0 444 // Unilaterally fix all of the suffix cards: closed card
aoqi@0 445 // index interval in args below.
aoqi@0 446 set_remainder_to_point_to_start_incl(suff_index + 1, end_index - 1, true /* reducing */);
aoqi@0 447 } else {
aoqi@0 448 // Unilaterally fix the first (num_pref_cards - 1) following
aoqi@0 449 // the "offset card" in the suffix block.
aoqi@0 450 set_remainder_to_point_to_start_incl(suff_index + 1,
aoqi@0 451 suff_index + num_pref_cards - 1, true /* reducing */);
aoqi@0 452 // Fix the appropriate cards in the remainder of the
aoqi@0 453 // suffix block -- these are the last num_pref_cards
aoqi@0 454 // cards in each power block of the "new" range plumbed
aoqi@0 455 // from suff_addr.
aoqi@0 456 bool more = true;
aoqi@0 457 uint i = 1;
aoqi@0 458 while (more && (i < N_powers)) {
aoqi@0 459 size_t back_by = power_to_cards_back(i);
aoqi@0 460 size_t right_index = suff_index + back_by - 1;
aoqi@0 461 size_t left_index = right_index - num_pref_cards + 1;
aoqi@0 462 if (right_index >= end_index - 1) { // last iteration
aoqi@0 463 right_index = end_index - 1;
aoqi@0 464 more = false;
aoqi@0 465 }
aoqi@0 466 if (back_by > num_pref_cards) {
aoqi@0 467 // Fill in the remainder of this "power block", if it
aoqi@0 468 // is non-null.
aoqi@0 469 if (left_index <= right_index) {
aoqi@0 470 _array->set_offset_array(left_index, right_index,
aoqi@0 471 N_words + i - 1, true /* reducing */);
aoqi@0 472 } else {
aoqi@0 473 more = false; // we are done
aoqi@0 474 }
aoqi@0 475 i++;
aoqi@0 476 break;
aoqi@0 477 }
aoqi@0 478 i++;
aoqi@0 479 }
aoqi@0 480 while (more && (i < N_powers)) {
aoqi@0 481 size_t back_by = power_to_cards_back(i);
aoqi@0 482 size_t right_index = suff_index + back_by - 1;
aoqi@0 483 size_t left_index = right_index - num_pref_cards + 1;
aoqi@0 484 if (right_index >= end_index - 1) { // last iteration
aoqi@0 485 right_index = end_index - 1;
aoqi@0 486 if (left_index > right_index) {
aoqi@0 487 break;
aoqi@0 488 }
aoqi@0 489 more = false;
aoqi@0 490 }
aoqi@0 491 assert(left_index <= right_index, "Error");
aoqi@0 492 _array->set_offset_array(left_index, right_index, N_words + i - 1, true /* reducing */);
aoqi@0 493 i++;
aoqi@0 494 }
aoqi@0 495 }
aoqi@0 496 } // else no more cards to fix in suffix
aoqi@0 497 } // else nothing needs to be done
aoqi@0 498 // Verify that we did the right thing
aoqi@0 499 verify_single_block(pref_addr, left_blk_size);
aoqi@0 500 verify_single_block(suff_addr, blk_size - left_blk_size);
aoqi@0 501 }
aoqi@0 502
aoqi@0 503
aoqi@0 504 // Mark the BOT such that if [blk_start, blk_end) straddles a card
aoqi@0 505 // boundary, the card following the first such boundary is marked
aoqi@0 506 // with the appropriate offset.
aoqi@0 507 // NOTE: this method does _not_ adjust _unallocated_block or
aoqi@0 508 // any cards subsequent to the first one.
aoqi@0 509 void
aoqi@0 510 BlockOffsetArrayNonContigSpace::mark_block(HeapWord* blk_start,
aoqi@0 511 HeapWord* blk_end, bool reducing) {
aoqi@0 512 do_block_internal(blk_start, blk_end, Action_mark, reducing);
aoqi@0 513 }
aoqi@0 514
aoqi@0 515 HeapWord* BlockOffsetArrayNonContigSpace::block_start_unsafe(
aoqi@0 516 const void* addr) const {
aoqi@0 517 assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
aoqi@0 518 assert(_bottom <= addr && addr < _end,
aoqi@0 519 "addr must be covered by this Array");
aoqi@0 520 // Must read this exactly once because it can be modified by parallel
aoqi@0 521 // allocation.
aoqi@0 522 HeapWord* ub = _unallocated_block;
aoqi@0 523 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
aoqi@0 524 assert(ub < _end, "tautology (see above)");
aoqi@0 525 return ub;
aoqi@0 526 }
aoqi@0 527
aoqi@0 528 // Otherwise, find the block start using the table.
aoqi@0 529 size_t index = _array->index_for(addr);
aoqi@0 530 HeapWord* q = _array->address_for_index(index);
aoqi@0 531
aoqi@0 532 uint offset = _array->offset_array(index); // Extend u_char to uint.
aoqi@0 533 while (offset >= N_words) {
aoqi@0 534 // The excess of the offset from N_words indicates a power of Base
aoqi@0 535 // to go back by.
aoqi@0 536 size_t n_cards_back = entry_to_cards_back(offset);
aoqi@0 537 q -= (N_words * n_cards_back);
aoqi@0 538 assert(q >= _sp->bottom(),
aoqi@0 539 err_msg("q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,
aoqi@0 540 p2i(q), p2i(_sp->bottom())));
aoqi@0 541 assert(q < _sp->end(),
aoqi@0 542 err_msg("q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,
aoqi@0 543 p2i(q), p2i(_sp->end())));
aoqi@0 544 index -= n_cards_back;
aoqi@0 545 offset = _array->offset_array(index);
aoqi@0 546 }
aoqi@0 547 assert(offset < N_words, "offset too large");
aoqi@0 548 index--;
aoqi@0 549 q -= offset;
aoqi@0 550 assert(q >= _sp->bottom(),
aoqi@0 551 err_msg("q = " PTR_FORMAT " crossed below bottom = " PTR_FORMAT,
aoqi@0 552 p2i(q), p2i(_sp->bottom())));
aoqi@0 553 assert(q < _sp->end(),
aoqi@0 554 err_msg("q = " PTR_FORMAT " crossed above end = " PTR_FORMAT,
aoqi@0 555 p2i(q), p2i(_sp->end())));
aoqi@0 556 HeapWord* n = q;
aoqi@0 557
aoqi@0 558 while (n <= addr) {
aoqi@0 559 debug_only(HeapWord* last = q); // for debugging
aoqi@0 560 q = n;
aoqi@0 561 n += _sp->block_size(n);
aoqi@0 562 assert(n > q,
aoqi@0 563 err_msg("Looping at n = " PTR_FORMAT " with last = " PTR_FORMAT","
aoqi@0 564 " while querying blk_start(" PTR_FORMAT ")"
aoqi@0 565 " on _sp = [" PTR_FORMAT "," PTR_FORMAT ")",
aoqi@0 566 p2i(n), p2i(last), p2i(addr), p2i(_sp->bottom()), p2i(_sp->end())));
aoqi@0 567 }
aoqi@0 568 assert(q <= addr,
aoqi@0 569 err_msg("wrong order for current (" INTPTR_FORMAT ")" " <= arg (" INTPTR_FORMAT ")",
aoqi@0 570 p2i(q), p2i(addr)));
aoqi@0 571 assert(addr <= n,
aoqi@0 572 err_msg("wrong order for arg (" INTPTR_FORMAT ") <= next (" INTPTR_FORMAT ")",
aoqi@0 573 p2i(addr), p2i(n)));
aoqi@0 574 return q;
aoqi@0 575 }
aoqi@0 576
aoqi@0 577 HeapWord* BlockOffsetArrayNonContigSpace::block_start_careful(
aoqi@0 578 const void* addr) const {
aoqi@0 579 assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
aoqi@0 580
aoqi@0 581 assert(_bottom <= addr && addr < _end,
aoqi@0 582 "addr must be covered by this Array");
aoqi@0 583 // Must read this exactly once because it can be modified by parallel
aoqi@0 584 // allocation.
aoqi@0 585 HeapWord* ub = _unallocated_block;
aoqi@0 586 if (BlockOffsetArrayUseUnallocatedBlock && addr >= ub) {
aoqi@0 587 assert(ub < _end, "tautology (see above)");
aoqi@0 588 return ub;
aoqi@0 589 }
aoqi@0 590
aoqi@0 591 // Otherwise, find the block start using the table, but taking
aoqi@0 592 // care (cf block_start_unsafe() above) not to parse any objects/blocks
aoqi@0 593 // on the cards themsleves.
aoqi@0 594 size_t index = _array->index_for(addr);
aoqi@0 595 assert(_array->address_for_index(index) == addr,
aoqi@0 596 "arg should be start of card");
aoqi@0 597
aoqi@0 598 HeapWord* q = (HeapWord*)addr;
aoqi@0 599 uint offset;
aoqi@0 600 do {
aoqi@0 601 offset = _array->offset_array(index);
aoqi@0 602 if (offset < N_words) {
aoqi@0 603 q -= offset;
aoqi@0 604 } else {
aoqi@0 605 size_t n_cards_back = entry_to_cards_back(offset);
aoqi@0 606 q -= (n_cards_back * N_words);
aoqi@0 607 index -= n_cards_back;
aoqi@0 608 }
aoqi@0 609 } while (offset >= N_words);
aoqi@0 610 assert(q <= addr, "block start should be to left of arg");
aoqi@0 611 return q;
aoqi@0 612 }
aoqi@0 613
aoqi@0 614 #ifndef PRODUCT
aoqi@0 615 // Verification & debugging - ensure that the offset table reflects the fact
aoqi@0 616 // that the block [blk_start, blk_end) or [blk, blk + size) is a
aoqi@0 617 // single block of storage. NOTE: can't const this because of
aoqi@0 618 // call to non-const do_block_internal() below.
aoqi@0 619 void BlockOffsetArrayNonContigSpace::verify_single_block(
aoqi@0 620 HeapWord* blk_start, HeapWord* blk_end) {
aoqi@0 621 if (VerifyBlockOffsetArray) {
aoqi@0 622 do_block_internal(blk_start, blk_end, Action_check);
aoqi@0 623 }
aoqi@0 624 }
aoqi@0 625
aoqi@0 626 void BlockOffsetArrayNonContigSpace::verify_single_block(
aoqi@0 627 HeapWord* blk, size_t size) {
aoqi@0 628 verify_single_block(blk, blk + size);
aoqi@0 629 }
aoqi@0 630
aoqi@0 631 // Verify that the given block is before _unallocated_block
aoqi@0 632 void BlockOffsetArrayNonContigSpace::verify_not_unallocated(
aoqi@0 633 HeapWord* blk_start, HeapWord* blk_end) const {
aoqi@0 634 if (BlockOffsetArrayUseUnallocatedBlock) {
aoqi@0 635 assert(blk_start < blk_end, "Block inconsistency?");
aoqi@0 636 assert(blk_end <= _unallocated_block, "_unallocated_block problem");
aoqi@0 637 }
aoqi@0 638 }
aoqi@0 639
aoqi@0 640 void BlockOffsetArrayNonContigSpace::verify_not_unallocated(
aoqi@0 641 HeapWord* blk, size_t size) const {
aoqi@0 642 verify_not_unallocated(blk, blk + size);
aoqi@0 643 }
aoqi@0 644 #endif // PRODUCT
aoqi@0 645
aoqi@0 646 size_t BlockOffsetArrayNonContigSpace::last_active_index() const {
aoqi@0 647 if (_unallocated_block == _bottom) {
aoqi@0 648 return 0;
aoqi@0 649 } else {
aoqi@0 650 return _array->index_for(_unallocated_block - 1);
aoqi@0 651 }
aoqi@0 652 }
aoqi@0 653
aoqi@0 654 //////////////////////////////////////////////////////////////////////
aoqi@0 655 // BlockOffsetArrayContigSpace
aoqi@0 656 //////////////////////////////////////////////////////////////////////
aoqi@0 657
aoqi@0 658 HeapWord* BlockOffsetArrayContigSpace::block_start_unsafe(const void* addr) const {
aoqi@0 659 assert(_array->offset_array(0) == 0, "objects can't cross covered areas");
aoqi@0 660
aoqi@0 661 // Otherwise, find the block start using the table.
aoqi@0 662 assert(_bottom <= addr && addr < _end,
aoqi@0 663 "addr must be covered by this Array");
aoqi@0 664 size_t index = _array->index_for(addr);
aoqi@0 665 // We must make sure that the offset table entry we use is valid. If
aoqi@0 666 // "addr" is past the end, start at the last known one and go forward.
aoqi@0 667 index = MIN2(index, _next_offset_index-1);
aoqi@0 668 HeapWord* q = _array->address_for_index(index);
aoqi@0 669
aoqi@0 670 uint offset = _array->offset_array(index); // Extend u_char to uint.
aoqi@0 671 while (offset > N_words) {
aoqi@0 672 // The excess of the offset from N_words indicates a power of Base
aoqi@0 673 // to go back by.
aoqi@0 674 size_t n_cards_back = entry_to_cards_back(offset);
aoqi@0 675 q -= (N_words * n_cards_back);
aoqi@0 676 assert(q >= _sp->bottom(), "Went below bottom!");
aoqi@0 677 index -= n_cards_back;
aoqi@0 678 offset = _array->offset_array(index);
aoqi@0 679 }
aoqi@0 680 while (offset == N_words) {
aoqi@0 681 assert(q >= _sp->bottom(), "Went below bottom!");
aoqi@0 682 q -= N_words;
aoqi@0 683 index--;
aoqi@0 684 offset = _array->offset_array(index);
aoqi@0 685 }
aoqi@0 686 assert(offset < N_words, "offset too large");
aoqi@0 687 q -= offset;
aoqi@0 688 HeapWord* n = q;
aoqi@0 689
aoqi@0 690 while (n <= addr) {
aoqi@0 691 debug_only(HeapWord* last = q); // for debugging
aoqi@0 692 q = n;
aoqi@0 693 n += _sp->block_size(n);
aoqi@0 694 }
aoqi@0 695 assert(q <= addr, "wrong order for current and arg");
aoqi@0 696 assert(addr <= n, "wrong order for arg and next");
aoqi@0 697 return q;
aoqi@0 698 }
aoqi@0 699
aoqi@0 700 //
aoqi@0 701 // _next_offset_threshold
aoqi@0 702 // | _next_offset_index
aoqi@0 703 // v v
aoqi@0 704 // +-------+-------+-------+-------+-------+
aoqi@0 705 // | i-1 | i | i+1 | i+2 | i+3 |
aoqi@0 706 // +-------+-------+-------+-------+-------+
aoqi@0 707 // ( ^ ]
aoqi@0 708 // block-start
aoqi@0 709 //
aoqi@0 710
aoqi@0 711 void BlockOffsetArrayContigSpace::alloc_block_work(HeapWord* blk_start,
aoqi@0 712 HeapWord* blk_end) {
aoqi@0 713 assert(blk_start != NULL && blk_end > blk_start,
aoqi@0 714 "phantom block");
aoqi@0 715 assert(blk_end > _next_offset_threshold,
aoqi@0 716 "should be past threshold");
aoqi@0 717 assert(blk_start <= _next_offset_threshold,
aoqi@0 718 "blk_start should be at or before threshold");
aoqi@0 719 assert(pointer_delta(_next_offset_threshold, blk_start) <= N_words,
aoqi@0 720 "offset should be <= BlockOffsetSharedArray::N");
aoqi@0 721 assert(Universe::heap()->is_in_reserved(blk_start),
aoqi@0 722 "reference must be into the heap");
aoqi@0 723 assert(Universe::heap()->is_in_reserved(blk_end-1),
aoqi@0 724 "limit must be within the heap");
aoqi@0 725 assert(_next_offset_threshold ==
aoqi@0 726 _array->_reserved.start() + _next_offset_index*N_words,
aoqi@0 727 "index must agree with threshold");
aoqi@0 728
aoqi@0 729 debug_only(size_t orig_next_offset_index = _next_offset_index;)
aoqi@0 730
aoqi@0 731 // Mark the card that holds the offset into the block. Note
aoqi@0 732 // that _next_offset_index and _next_offset_threshold are not
aoqi@0 733 // updated until the end of this method.
aoqi@0 734 _array->set_offset_array(_next_offset_index,
aoqi@0 735 _next_offset_threshold,
aoqi@0 736 blk_start);
aoqi@0 737
aoqi@0 738 // We need to now mark the subsequent cards that this blk spans.
aoqi@0 739
aoqi@0 740 // Index of card on which blk ends.
aoqi@0 741 size_t end_index = _array->index_for(blk_end - 1);
aoqi@0 742
aoqi@0 743 // Are there more cards left to be updated?
aoqi@0 744 if (_next_offset_index + 1 <= end_index) {
aoqi@0 745 HeapWord* rem_st = _array->address_for_index(_next_offset_index + 1);
aoqi@0 746 // Calculate rem_end this way because end_index
aoqi@0 747 // may be the last valid index in the covered region.
aoqi@0 748 HeapWord* rem_end = _array->address_for_index(end_index) + N_words;
aoqi@0 749 set_remainder_to_point_to_start(rem_st, rem_end);
aoqi@0 750 }
aoqi@0 751
aoqi@0 752 // _next_offset_index and _next_offset_threshold updated here.
aoqi@0 753 _next_offset_index = end_index + 1;
aoqi@0 754 // Calculate _next_offset_threshold this way because end_index
aoqi@0 755 // may be the last valid index in the covered region.
aoqi@0 756 _next_offset_threshold = _array->address_for_index(end_index) + N_words;
aoqi@0 757 assert(_next_offset_threshold >= blk_end, "Incorrect offset threshold");
aoqi@0 758
aoqi@0 759 #ifdef ASSERT
aoqi@0 760 // The offset can be 0 if the block starts on a boundary. That
aoqi@0 761 // is checked by an assertion above.
aoqi@0 762 size_t start_index = _array->index_for(blk_start);
aoqi@0 763 HeapWord* boundary = _array->address_for_index(start_index);
aoqi@0 764 assert((_array->offset_array(orig_next_offset_index) == 0 &&
aoqi@0 765 blk_start == boundary) ||
aoqi@0 766 (_array->offset_array(orig_next_offset_index) > 0 &&
aoqi@0 767 _array->offset_array(orig_next_offset_index) <= N_words),
aoqi@0 768 "offset array should have been set");
aoqi@0 769 for (size_t j = orig_next_offset_index + 1; j <= end_index; j++) {
aoqi@0 770 assert(_array->offset_array(j) > 0 &&
aoqi@0 771 _array->offset_array(j) <= (u_char) (N_words+N_powers-1),
aoqi@0 772 "offset array should have been set");
aoqi@0 773 }
aoqi@0 774 #endif
aoqi@0 775 }
aoqi@0 776
aoqi@0 777 HeapWord* BlockOffsetArrayContigSpace::initialize_threshold() {
aoqi@0 778 assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
aoqi@0 779 "just checking");
aoqi@0 780 _next_offset_index = _array->index_for(_bottom);
aoqi@0 781 _next_offset_index++;
aoqi@0 782 _next_offset_threshold =
aoqi@0 783 _array->address_for_index(_next_offset_index);
aoqi@0 784 return _next_offset_threshold;
aoqi@0 785 }
aoqi@0 786
aoqi@0 787 void BlockOffsetArrayContigSpace::zero_bottom_entry() {
aoqi@0 788 assert(!Universe::heap()->is_in_reserved(_array->_offset_array),
aoqi@0 789 "just checking");
aoqi@0 790 size_t bottom_index = _array->index_for(_bottom);
aoqi@0 791 _array->set_offset_array(bottom_index, 0);
aoqi@0 792 }
aoqi@0 793
aoqi@0 794 size_t BlockOffsetArrayContigSpace::last_active_index() const {
aoqi@0 795 size_t result = _next_offset_index - 1;
aoqi@0 796 return result >= 0 ? result : 0;
aoqi@0 797 }

mercurial