1 /* 2 * Copyright (c) 2001, 2018, 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/g1ConcurrentRefine.hpp" 29 #include "gc/g1/heapRegionManager.inline.hpp" 30 #include "gc/g1/heapRegionRemSet.hpp" 31 #include "gc/shared/space.inline.hpp" 32 #include "memory/allocation.hpp" 33 #include "memory/padded.inline.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "runtime/atomic.hpp" 36 #include "utilities/bitMap.inline.hpp" 37 #include "utilities/debug.hpp" 38 #include "utilities/formatBuffer.hpp" 39 #include "utilities/globalDefinitions.hpp" 40 #include "utilities/growableArray.hpp" 41 42 const char* HeapRegionRemSet::_state_strings[] = {"Untracked", "Updating", "Complete"}; 43 const char* HeapRegionRemSet::_short_state_strings[] = {"UNTRA", "UPDAT", "CMPLT"}; 44 45 class PerRegionTable: public CHeapObj<mtGC> { 46 friend class OtherRegionsTable; 47 friend class HeapRegionRemSetIterator; 48 49 HeapRegion* _hr; 50 CHeapBitMap _bm; 51 jint _occupied; 52 53 // next pointer for free/allocated 'all' list 54 PerRegionTable* _next; 55 56 // prev pointer for the allocated 'all' list 57 PerRegionTable* _prev; 58 59 // next pointer in collision list 60 PerRegionTable * _collision_list_next; 61 62 // Global free list of PRTs 63 static PerRegionTable* volatile _free_list; 64 65 protected: 66 // We need access in order to union things into the base table. 67 BitMap* bm() { return &_bm; } 68 69 PerRegionTable(HeapRegion* hr) : 70 _hr(hr), 71 _bm(HeapRegion::CardsPerRegion, mtGC), 72 _occupied(0), 73 _next(NULL), _prev(NULL), 74 _collision_list_next(NULL) 75 {} 76 77 void add_card_work(CardIdx_t from_card, bool par) { 78 if (!_bm.at(from_card)) { 79 if (par) { 80 if (_bm.par_at_put(from_card, 1)) { 81 Atomic::inc(&_occupied); 82 } 83 } else { 84 _bm.at_put(from_card, 1); 85 _occupied++; 86 } 87 } 88 } 89 90 void add_reference_work(OopOrNarrowOopStar from, bool par) { 91 // Must make this robust in case "from" is not in "_hr", because of 92 // concurrency. 93 94 HeapRegion* loc_hr = hr(); 95 // If the test below fails, then this table was reused concurrently 96 // with this operation. This is OK, since the old table was coarsened, 97 // and adding a bit to the new table is never incorrect. 98 if (loc_hr->is_in_reserved(from)) { 99 CardIdx_t from_card = OtherRegionsTable::card_within_region(from, loc_hr); 100 add_card_work(from_card, par); 101 } 102 } 103 104 public: 105 106 HeapRegion* hr() const { return OrderAccess::load_acquire(&_hr); } 107 108 jint occupied() const { 109 // Overkill, but if we ever need it... 110 // guarantee(_occupied == _bm.count_one_bits(), "Check"); 111 return _occupied; 112 } 113 114 void init(HeapRegion* hr, bool clear_links_to_all_list) { 115 if (clear_links_to_all_list) { 116 set_next(NULL); 117 set_prev(NULL); 118 } 119 _collision_list_next = NULL; 120 _occupied = 0; 121 _bm.clear(); 122 // Make sure that the bitmap clearing above has been finished before publishing 123 // this PRT to concurrent threads. 124 OrderAccess::release_store(&_hr, hr); 125 } 126 127 void add_reference(OopOrNarrowOopStar from) { 128 add_reference_work(from, /*parallel*/ true); 129 } 130 131 void seq_add_reference(OopOrNarrowOopStar from) { 132 add_reference_work(from, /*parallel*/ false); 133 } 134 135 void add_card(CardIdx_t from_card_index) { 136 add_card_work(from_card_index, /*parallel*/ true); 137 } 138 139 void seq_add_card(CardIdx_t from_card_index) { 140 add_card_work(from_card_index, /*parallel*/ false); 141 } 142 143 // (Destructively) union the bitmap of the current table into the given 144 // bitmap (which is assumed to be of the same size.) 145 void union_bitmap_into(BitMap* bm) { 146 bm->set_union(_bm); 147 } 148 149 // Mem size in bytes. 150 size_t mem_size() const { 151 return sizeof(PerRegionTable) + _bm.size_in_words() * HeapWordSize; 152 } 153 154 // Requires "from" to be in "hr()". 155 bool contains_reference(OopOrNarrowOopStar from) const { 156 assert(hr()->is_in_reserved(from), "Precondition."); 157 size_t card_ind = pointer_delta(from, hr()->bottom(), 158 G1CardTable::card_size); 159 return _bm.at(card_ind); 160 } 161 162 // Bulk-free the PRTs from prt to last, assumes that they are 163 // linked together using their _next field. 164 static void bulk_free(PerRegionTable* prt, PerRegionTable* last) { 165 while (true) { 166 PerRegionTable* fl = _free_list; 167 last->set_next(fl); 168 PerRegionTable* res = Atomic::cmpxchg(prt, &_free_list, fl); 169 if (res == fl) { 170 return; 171 } 172 } 173 ShouldNotReachHere(); 174 } 175 176 static void free(PerRegionTable* prt) { 177 bulk_free(prt, prt); 178 } 179 180 // Returns an initialized PerRegionTable instance. 181 static PerRegionTable* alloc(HeapRegion* hr) { 182 PerRegionTable* fl = _free_list; 183 while (fl != NULL) { 184 PerRegionTable* nxt = fl->next(); 185 PerRegionTable* res = Atomic::cmpxchg(nxt, &_free_list, fl); 186 if (res == fl) { 187 fl->init(hr, true); 188 return fl; 189 } else { 190 fl = _free_list; 191 } 192 } 193 assert(fl == NULL, "Loop condition."); 194 return new PerRegionTable(hr); 195 } 196 197 PerRegionTable* next() const { return _next; } 198 void set_next(PerRegionTable* next) { _next = next; } 199 PerRegionTable* prev() const { return _prev; } 200 void set_prev(PerRegionTable* prev) { _prev = prev; } 201 202 // Accessor and Modification routines for the pointer for the 203 // singly linked collision list that links the PRTs within the 204 // OtherRegionsTable::_fine_grain_regions hash table. 205 // 206 // It might be useful to also make the collision list doubly linked 207 // to avoid iteration over the collisions list during scrubbing/deletion. 208 // OTOH there might not be many collisions. 209 210 PerRegionTable* collision_list_next() const { 211 return _collision_list_next; 212 } 213 214 void set_collision_list_next(PerRegionTable* next) { 215 _collision_list_next = next; 216 } 217 218 PerRegionTable** collision_list_next_addr() { 219 return &_collision_list_next; 220 } 221 222 static size_t fl_mem_size() { 223 PerRegionTable* cur = _free_list; 224 size_t res = 0; 225 while (cur != NULL) { 226 res += cur->mem_size(); 227 cur = cur->next(); 228 } 229 return res; 230 } 231 232 static void test_fl_mem_size(); 233 }; 234 235 PerRegionTable* volatile PerRegionTable::_free_list = NULL; 236 237 size_t OtherRegionsTable::_max_fine_entries = 0; 238 size_t OtherRegionsTable::_mod_max_fine_entries_mask = 0; 239 size_t OtherRegionsTable::_fine_eviction_stride = 0; 240 size_t OtherRegionsTable::_fine_eviction_sample_size = 0; 241 242 OtherRegionsTable::OtherRegionsTable(HeapRegion* hr, Mutex* m) : 243 _g1h(G1CollectedHeap::heap()), 244 _m(m), 245 _hr(hr), 246 _coarse_map(G1CollectedHeap::heap()->max_regions(), mtGC), 247 _n_coarse_entries(0), 248 _fine_grain_regions(NULL), 249 _n_fine_entries(0), 250 _first_all_fine_prts(NULL), 251 _last_all_fine_prts(NULL), 252 _fine_eviction_start(0), 253 _sparse_table(hr) 254 { 255 typedef PerRegionTable* PerRegionTablePtr; 256 257 if (_max_fine_entries == 0) { 258 assert(_mod_max_fine_entries_mask == 0, "Both or none."); 259 size_t max_entries_log = (size_t)log2_long((jlong)G1RSetRegionEntries); 260 _max_fine_entries = (size_t)1 << max_entries_log; 261 _mod_max_fine_entries_mask = _max_fine_entries - 1; 262 263 assert(_fine_eviction_sample_size == 0 264 && _fine_eviction_stride == 0, "All init at same time."); 265 _fine_eviction_sample_size = MAX2((size_t)4, max_entries_log); 266 _fine_eviction_stride = _max_fine_entries / _fine_eviction_sample_size; 267 } 268 269 _fine_grain_regions = NEW_C_HEAP_ARRAY3(PerRegionTablePtr, _max_fine_entries, 270 mtGC, CURRENT_PC, AllocFailStrategy::RETURN_NULL); 271 272 if (_fine_grain_regions == NULL) { 273 vm_exit_out_of_memory(sizeof(void*)*_max_fine_entries, OOM_MALLOC_ERROR, 274 "Failed to allocate _fine_grain_entries."); 275 } 276 277 for (size_t i = 0; i < _max_fine_entries; i++) { 278 _fine_grain_regions[i] = NULL; 279 } 280 } 281 282 void OtherRegionsTable::link_to_all(PerRegionTable* prt) { 283 // We always append to the beginning of the list for convenience; 284 // the order of entries in this list does not matter. 285 if (_first_all_fine_prts != NULL) { 286 assert(_first_all_fine_prts->prev() == NULL, "invariant"); 287 _first_all_fine_prts->set_prev(prt); 288 prt->set_next(_first_all_fine_prts); 289 } else { 290 // this is the first element we insert. Adjust the "last" pointer 291 _last_all_fine_prts = prt; 292 assert(prt->next() == NULL, "just checking"); 293 } 294 // the new element is always the first element without a predecessor 295 prt->set_prev(NULL); 296 _first_all_fine_prts = prt; 297 298 assert(prt->prev() == NULL, "just checking"); 299 assert(_first_all_fine_prts == prt, "just checking"); 300 assert((_first_all_fine_prts == NULL && _last_all_fine_prts == NULL) || 301 (_first_all_fine_prts != NULL && _last_all_fine_prts != NULL), 302 "just checking"); 303 assert(_last_all_fine_prts == NULL || _last_all_fine_prts->next() == NULL, 304 "just checking"); 305 assert(_first_all_fine_prts == NULL || _first_all_fine_prts->prev() == NULL, 306 "just checking"); 307 } 308 309 void OtherRegionsTable::unlink_from_all(PerRegionTable* prt) { 310 if (prt->prev() != NULL) { 311 assert(_first_all_fine_prts != prt, "just checking"); 312 prt->prev()->set_next(prt->next()); 313 // removing the last element in the list? 314 if (_last_all_fine_prts == prt) { 315 _last_all_fine_prts = prt->prev(); 316 } 317 } else { 318 assert(_first_all_fine_prts == prt, "just checking"); 319 _first_all_fine_prts = prt->next(); 320 // list is empty now? 321 if (_first_all_fine_prts == NULL) { 322 _last_all_fine_prts = NULL; 323 } 324 } 325 326 if (prt->next() != NULL) { 327 prt->next()->set_prev(prt->prev()); 328 } 329 330 prt->set_next(NULL); 331 prt->set_prev(NULL); 332 333 assert((_first_all_fine_prts == NULL && _last_all_fine_prts == NULL) || 334 (_first_all_fine_prts != NULL && _last_all_fine_prts != NULL), 335 "just checking"); 336 assert(_last_all_fine_prts == NULL || _last_all_fine_prts->next() == NULL, 337 "just checking"); 338 assert(_first_all_fine_prts == NULL || _first_all_fine_prts->prev() == NULL, 339 "just checking"); 340 } 341 342 CardIdx_t OtherRegionsTable::card_within_region(OopOrNarrowOopStar within_region, HeapRegion* hr) { 343 assert(hr->is_in_reserved(within_region), 344 "HeapWord " PTR_FORMAT " is outside of region %u [" PTR_FORMAT ", " PTR_FORMAT ")", 345 p2i(within_region), hr->hrm_index(), p2i(hr->bottom()), p2i(hr->end())); 346 CardIdx_t result = (CardIdx_t)(pointer_delta((HeapWord*)within_region, hr->bottom()) >> (CardTable::card_shift - LogHeapWordSize)); 347 return result; 348 } 349 350 void OtherRegionsTable::add_reference(OopOrNarrowOopStar from, uint tid) { 351 uint cur_hrm_ind = _hr->hrm_index(); 352 353 uintptr_t from_card = uintptr_t(from) >> CardTable::card_shift; 354 355 if (G1FromCardCache::contains_or_replace(tid, cur_hrm_ind, from_card)) { 356 assert(contains_reference(from), "We just found " PTR_FORMAT " in the FromCardCache", p2i(from)); 357 return; 358 } 359 360 // Note that this may be a continued H region. 361 HeapRegion* from_hr = _g1h->heap_region_containing(from); 362 RegionIdx_t from_hrm_ind = (RegionIdx_t) from_hr->hrm_index(); 363 364 // If the region is already coarsened, return. 365 if (_coarse_map.at(from_hrm_ind)) { 366 assert(contains_reference(from), "We just found " PTR_FORMAT " in the Coarse table", p2i(from)); 367 return; 368 } 369 370 // Otherwise find a per-region table to add it to. 371 size_t ind = from_hrm_ind & _mod_max_fine_entries_mask; 372 PerRegionTable* prt = find_region_table(ind, from_hr); 373 if (prt == NULL) { 374 MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag); 375 // Confirm that it's really not there... 376 prt = find_region_table(ind, from_hr); 377 if (prt == NULL) { 378 379 CardIdx_t card_index = card_within_region(from, from_hr); 380 381 if (G1HRRSUseSparseTable && 382 _sparse_table.add_card(from_hrm_ind, card_index)) { 383 assert(contains_reference_locked(from), "We just added " PTR_FORMAT " to the Sparse table", p2i(from)); 384 return; 385 } 386 387 if (_n_fine_entries == _max_fine_entries) { 388 prt = delete_region_table(); 389 // There is no need to clear the links to the 'all' list here: 390 // prt will be reused immediately, i.e. remain in the 'all' list. 391 prt->init(from_hr, false /* clear_links_to_all_list */); 392 } else { 393 prt = PerRegionTable::alloc(from_hr); 394 link_to_all(prt); 395 } 396 397 PerRegionTable* first_prt = _fine_grain_regions[ind]; 398 prt->set_collision_list_next(first_prt); 399 // The assignment into _fine_grain_regions allows the prt to 400 // start being used concurrently. In addition to 401 // collision_list_next which must be visible (else concurrent 402 // parsing of the list, if any, may fail to see other entries), 403 // the content of the prt must be visible (else for instance 404 // some mark bits may not yet seem cleared or a 'later' update 405 // performed by a concurrent thread could be undone when the 406 // zeroing becomes visible). This requires store ordering. 407 OrderAccess::release_store(&_fine_grain_regions[ind], prt); 408 _n_fine_entries++; 409 410 if (G1HRRSUseSparseTable) { 411 // Transfer from sparse to fine-grain. 412 SparsePRTEntry *sprt_entry = _sparse_table.get_entry(from_hrm_ind); 413 assert(sprt_entry != NULL, "There should have been an entry"); 414 for (int i = 0; i < sprt_entry->num_valid_cards(); i++) { 415 CardIdx_t c = sprt_entry->card(i); 416 prt->add_card(c); 417 } 418 // Now we can delete the sparse entry. 419 bool res = _sparse_table.delete_entry(from_hrm_ind); 420 assert(res, "It should have been there."); 421 } 422 } 423 assert(prt != NULL && prt->hr() == from_hr, "consequence"); 424 } 425 // Note that we can't assert "prt->hr() == from_hr", because of the 426 // possibility of concurrent reuse. But see head comment of 427 // OtherRegionsTable for why this is OK. 428 assert(prt != NULL, "Inv"); 429 430 prt->add_reference(from); 431 assert(contains_reference(from), "We just added " PTR_FORMAT " to the PRT (%d)", p2i(from), prt->contains_reference(from)); 432 } 433 434 PerRegionTable* 435 OtherRegionsTable::find_region_table(size_t ind, HeapRegion* hr) const { 436 assert(ind < _max_fine_entries, "Preconditions."); 437 PerRegionTable* prt = _fine_grain_regions[ind]; 438 while (prt != NULL && prt->hr() != hr) { 439 prt = prt->collision_list_next(); 440 } 441 // Loop postcondition is the method postcondition. 442 return prt; 443 } 444 445 jint OtherRegionsTable::_n_coarsenings = 0; 446 447 PerRegionTable* OtherRegionsTable::delete_region_table() { 448 assert(_m->owned_by_self(), "Precondition"); 449 assert(_n_fine_entries == _max_fine_entries, "Precondition"); 450 PerRegionTable* max = NULL; 451 jint max_occ = 0; 452 PerRegionTable** max_prev = NULL; 453 size_t max_ind; 454 455 size_t i = _fine_eviction_start; 456 for (size_t k = 0; k < _fine_eviction_sample_size; k++) { 457 size_t ii = i; 458 // Make sure we get a non-NULL sample. 459 while (_fine_grain_regions[ii] == NULL) { 460 ii++; 461 if (ii == _max_fine_entries) ii = 0; 462 guarantee(ii != i, "We must find one."); 463 } 464 PerRegionTable** prev = &_fine_grain_regions[ii]; 465 PerRegionTable* cur = *prev; 466 while (cur != NULL) { 467 jint cur_occ = cur->occupied(); 468 if (max == NULL || cur_occ > max_occ) { 469 max = cur; 470 max_prev = prev; 471 max_ind = i; 472 max_occ = cur_occ; 473 } 474 prev = cur->collision_list_next_addr(); 475 cur = cur->collision_list_next(); 476 } 477 i = i + _fine_eviction_stride; 478 if (i >= _n_fine_entries) i = i - _n_fine_entries; 479 } 480 481 _fine_eviction_start++; 482 483 if (_fine_eviction_start >= _n_fine_entries) { 484 _fine_eviction_start -= _n_fine_entries; 485 } 486 487 guarantee(max != NULL, "Since _n_fine_entries > 0"); 488 guarantee(max_prev != NULL, "Since max != NULL."); 489 490 // Set the corresponding coarse bit. 491 size_t max_hrm_index = (size_t) max->hr()->hrm_index(); 492 if (!_coarse_map.at(max_hrm_index)) { 493 _coarse_map.at_put(max_hrm_index, true); 494 _n_coarse_entries++; 495 } 496 497 // Unsplice. 498 *max_prev = max->collision_list_next(); 499 Atomic::inc(&_n_coarsenings); 500 _n_fine_entries--; 501 return max; 502 } 503 504 bool OtherRegionsTable::occupancy_less_or_equal_than(size_t limit) const { 505 if (limit <= (size_t)G1RSetSparseRegionEntries) { 506 return occ_coarse() == 0 && _first_all_fine_prts == NULL && occ_sparse() <= limit; 507 } else { 508 // Current uses of this method may only use values less than G1RSetSparseRegionEntries 509 // for the limit. The solution, comparing against occupied() would be too slow 510 // at this time. 511 Unimplemented(); 512 return false; 513 } 514 } 515 516 bool OtherRegionsTable::is_empty() const { 517 return occ_sparse() == 0 && occ_coarse() == 0 && _first_all_fine_prts == NULL; 518 } 519 520 size_t OtherRegionsTable::occupied() const { 521 size_t sum = occ_fine(); 522 sum += occ_sparse(); 523 sum += occ_coarse(); 524 return sum; 525 } 526 527 size_t OtherRegionsTable::occ_fine() const { 528 size_t sum = 0; 529 530 size_t num = 0; 531 PerRegionTable * cur = _first_all_fine_prts; 532 while (cur != NULL) { 533 sum += cur->occupied(); 534 cur = cur->next(); 535 num++; 536 } 537 guarantee(num == _n_fine_entries, "just checking"); 538 return sum; 539 } 540 541 size_t OtherRegionsTable::occ_coarse() const { 542 return (_n_coarse_entries * HeapRegion::CardsPerRegion); 543 } 544 545 size_t OtherRegionsTable::occ_sparse() const { 546 return _sparse_table.occupied(); 547 } 548 549 size_t OtherRegionsTable::mem_size() const { 550 size_t sum = 0; 551 // all PRTs are of the same size so it is sufficient to query only one of them. 552 if (_first_all_fine_prts != NULL) { 553 assert(_last_all_fine_prts != NULL && 554 _first_all_fine_prts->mem_size() == _last_all_fine_prts->mem_size(), "check that mem_size() is constant"); 555 sum += _first_all_fine_prts->mem_size() * _n_fine_entries; 556 } 557 sum += (sizeof(PerRegionTable*) * _max_fine_entries); 558 sum += (_coarse_map.size_in_words() * HeapWordSize); 559 sum += (_sparse_table.mem_size()); 560 sum += sizeof(OtherRegionsTable) - sizeof(_sparse_table); // Avoid double counting above. 561 return sum; 562 } 563 564 size_t OtherRegionsTable::static_mem_size() { 565 return G1FromCardCache::static_mem_size(); 566 } 567 568 size_t OtherRegionsTable::fl_mem_size() { 569 return PerRegionTable::fl_mem_size(); 570 } 571 572 void OtherRegionsTable::clear_fcc() { 573 G1FromCardCache::clear(_hr->hrm_index()); 574 } 575 576 void OtherRegionsTable::clear() { 577 // if there are no entries, skip this step 578 if (_first_all_fine_prts != NULL) { 579 guarantee(_first_all_fine_prts != NULL && _last_all_fine_prts != NULL, "just checking"); 580 PerRegionTable::bulk_free(_first_all_fine_prts, _last_all_fine_prts); 581 memset(_fine_grain_regions, 0, _max_fine_entries * sizeof(_fine_grain_regions[0])); 582 } else { 583 guarantee(_first_all_fine_prts == NULL && _last_all_fine_prts == NULL, "just checking"); 584 } 585 586 _first_all_fine_prts = _last_all_fine_prts = NULL; 587 _sparse_table.clear(); 588 if (_n_coarse_entries > 0) { 589 _coarse_map.clear(); 590 } 591 _n_fine_entries = 0; 592 _n_coarse_entries = 0; 593 594 clear_fcc(); 595 } 596 597 bool OtherRegionsTable::contains_reference(OopOrNarrowOopStar from) const { 598 // Cast away const in this case. 599 MutexLockerEx x((Mutex*)_m, Mutex::_no_safepoint_check_flag); 600 return contains_reference_locked(from); 601 } 602 603 bool OtherRegionsTable::contains_reference_locked(OopOrNarrowOopStar from) const { 604 HeapRegion* hr = _g1h->heap_region_containing(from); 605 RegionIdx_t hr_ind = (RegionIdx_t) hr->hrm_index(); 606 // Is this region in the coarse map? 607 if (_coarse_map.at(hr_ind)) return true; 608 609 PerRegionTable* prt = find_region_table(hr_ind & _mod_max_fine_entries_mask, 610 hr); 611 if (prt != NULL) { 612 return prt->contains_reference(from); 613 614 } else { 615 CardIdx_t card_index = card_within_region(from, hr); 616 return _sparse_table.contains_card(hr_ind, card_index); 617 } 618 } 619 620 void 621 OtherRegionsTable::do_cleanup_work(HRRSCleanupTask* hrrs_cleanup_task) { 622 _sparse_table.do_cleanup_work(hrrs_cleanup_task); 623 } 624 625 HeapRegionRemSet::HeapRegionRemSet(G1BlockOffsetTable* bot, 626 HeapRegion* hr) 627 : _bot(bot), 628 _code_roots(), 629 _m(Mutex::leaf, FormatBuffer<128>("HeapRegionRemSet lock #%u", hr->hrm_index()), true, Monitor::_safepoint_check_never), 630 _other_regions(hr, &_m), 631 _state(Untracked) 632 { 633 } 634 635 void HeapRegionRemSet::setup_remset_size() { 636 // Setup sparse and fine-grain tables sizes. 637 // table_size = base * (log(region_size / 1M) + 1) 638 const int LOG_M = 20; 639 int region_size_log_mb = MAX2(HeapRegion::LogOfHRGrainBytes - LOG_M, 0); 640 if (FLAG_IS_DEFAULT(G1RSetSparseRegionEntries)) { 641 G1RSetSparseRegionEntries = G1RSetSparseRegionEntriesBase * (region_size_log_mb + 1); 642 } 643 if (FLAG_IS_DEFAULT(G1RSetRegionEntries)) { 644 G1RSetRegionEntries = G1RSetRegionEntriesBase * (region_size_log_mb + 1); 645 } 646 guarantee(G1RSetSparseRegionEntries > 0 && G1RSetRegionEntries > 0 , "Sanity"); 647 } 648 649 void HeapRegionRemSet::cleanup() { 650 SparsePRT::cleanup_all(); 651 } 652 653 void HeapRegionRemSet::clear(bool only_cardset) { 654 MutexLockerEx x(&_m, Mutex::_no_safepoint_check_flag); 655 clear_locked(only_cardset); 656 } 657 658 void HeapRegionRemSet::clear_locked(bool only_cardset) { 659 if (!only_cardset) { 660 _code_roots.clear(); 661 } 662 _other_regions.clear(); 663 set_state_empty(); 664 assert(occupied_locked() == 0, "Should be clear."); 665 } 666 667 // Code roots support 668 // 669 // The code root set is protected by two separate locking schemes 670 // When at safepoint the per-hrrs lock must be held during modifications 671 // except when doing a full gc. 672 // When not at safepoint the CodeCache_lock must be held during modifications. 673 // When concurrent readers access the contains() function 674 // (during the evacuation phase) no removals are allowed. 675 676 void HeapRegionRemSet::add_strong_code_root(nmethod* nm) { 677 assert(nm != NULL, "sanity"); 678 assert((!CodeCache_lock->owned_by_self() || SafepointSynchronize::is_at_safepoint()), 679 "should call add_strong_code_root_locked instead. CodeCache_lock->owned_by_self(): %s, is_at_safepoint(): %s", 680 BOOL_TO_STR(CodeCache_lock->owned_by_self()), BOOL_TO_STR(SafepointSynchronize::is_at_safepoint())); 681 // Optimistic unlocked contains-check 682 if (!_code_roots.contains(nm)) { 683 MutexLockerEx ml(&_m, Mutex::_no_safepoint_check_flag); 684 add_strong_code_root_locked(nm); 685 } 686 } 687 688 void HeapRegionRemSet::add_strong_code_root_locked(nmethod* nm) { 689 assert(nm != NULL, "sanity"); 690 assert((CodeCache_lock->owned_by_self() || 691 (SafepointSynchronize::is_at_safepoint() && 692 (_m.owned_by_self() || Thread::current()->is_VM_thread()))), 693 "not safely locked. CodeCache_lock->owned_by_self(): %s, is_at_safepoint(): %s, _m.owned_by_self(): %s, Thread::current()->is_VM_thread(): %s", 694 BOOL_TO_STR(CodeCache_lock->owned_by_self()), BOOL_TO_STR(SafepointSynchronize::is_at_safepoint()), 695 BOOL_TO_STR(_m.owned_by_self()), BOOL_TO_STR(Thread::current()->is_VM_thread())); 696 _code_roots.add(nm); 697 } 698 699 void HeapRegionRemSet::remove_strong_code_root(nmethod* nm) { 700 assert(nm != NULL, "sanity"); 701 assert_locked_or_safepoint(CodeCache_lock); 702 703 MutexLockerEx ml(CodeCache_lock->owned_by_self() ? NULL : &_m, Mutex::_no_safepoint_check_flag); 704 _code_roots.remove(nm); 705 706 // Check that there were no duplicates 707 guarantee(!_code_roots.contains(nm), "duplicate entry found"); 708 } 709 710 void HeapRegionRemSet::strong_code_roots_do(CodeBlobClosure* blk) const { 711 _code_roots.nmethods_do(blk); 712 } 713 714 void HeapRegionRemSet::clean_strong_code_roots(HeapRegion* hr) { 715 _code_roots.clean(hr); 716 } 717 718 size_t HeapRegionRemSet::strong_code_roots_mem_size() { 719 return _code_roots.mem_size(); 720 } 721 722 HeapRegionRemSetIterator:: HeapRegionRemSetIterator(HeapRegionRemSet* hrrs) : 723 _hrrs(hrrs), 724 _coarse_map(&hrrs->_other_regions._coarse_map), 725 _bot(hrrs->_bot), 726 _g1h(G1CollectedHeap::heap()), 727 _n_yielded_fine(0), 728 _n_yielded_coarse(0), 729 _n_yielded_sparse(0), 730 _is(Sparse), 731 _cur_region_card_offset(0), 732 // Set these values so that we increment to the first region. 733 _coarse_cur_region_index(-1), 734 _coarse_cur_region_cur_card(HeapRegion::CardsPerRegion-1), 735 _fine_cur_prt(NULL), 736 _cur_card_in_prt(HeapRegion::CardsPerRegion), 737 _sparse_iter(&hrrs->_other_regions._sparse_table) {} 738 739 bool HeapRegionRemSetIterator::coarse_has_next(size_t& card_index) { 740 if (_hrrs->_other_regions._n_coarse_entries == 0) return false; 741 // Go to the next card. 742 _coarse_cur_region_cur_card++; 743 // Was the last the last card in the current region? 744 if (_coarse_cur_region_cur_card == HeapRegion::CardsPerRegion) { 745 // Yes: find the next region. This may leave _coarse_cur_region_index 746 // Set to the last index, in which case there are no more coarse 747 // regions. 748 _coarse_cur_region_index = 749 (int) _coarse_map->get_next_one_offset(_coarse_cur_region_index + 1); 750 if ((size_t)_coarse_cur_region_index < _coarse_map->size()) { 751 _coarse_cur_region_cur_card = 0; 752 HeapWord* r_bot = 753 _g1h->region_at((uint) _coarse_cur_region_index)->bottom(); 754 _cur_region_card_offset = _bot->index_for_raw(r_bot); 755 } else { 756 return false; 757 } 758 } 759 // If we didn't return false above, then we can yield a card. 760 card_index = _cur_region_card_offset + _coarse_cur_region_cur_card; 761 return true; 762 } 763 764 bool HeapRegionRemSetIterator::fine_has_next(size_t& card_index) { 765 if (fine_has_next()) { 766 _cur_card_in_prt = 767 _fine_cur_prt->_bm.get_next_one_offset(_cur_card_in_prt + 1); 768 } 769 if (_cur_card_in_prt == HeapRegion::CardsPerRegion) { 770 // _fine_cur_prt may still be NULL in case if there are not PRTs at all for 771 // the remembered set. 772 if (_fine_cur_prt == NULL || _fine_cur_prt->next() == NULL) { 773 return false; 774 } 775 PerRegionTable* next_prt = _fine_cur_prt->next(); 776 switch_to_prt(next_prt); 777 _cur_card_in_prt = _fine_cur_prt->_bm.get_next_one_offset(_cur_card_in_prt + 1); 778 } 779 780 card_index = _cur_region_card_offset + _cur_card_in_prt; 781 guarantee(_cur_card_in_prt < HeapRegion::CardsPerRegion, 782 "Card index " SIZE_FORMAT " must be within the region", _cur_card_in_prt); 783 return true; 784 } 785 786 bool HeapRegionRemSetIterator::fine_has_next() { 787 return _cur_card_in_prt != HeapRegion::CardsPerRegion; 788 } 789 790 void HeapRegionRemSetIterator::switch_to_prt(PerRegionTable* prt) { 791 assert(prt != NULL, "Cannot switch to NULL prt"); 792 _fine_cur_prt = prt; 793 794 HeapWord* r_bot = _fine_cur_prt->hr()->bottom(); 795 _cur_region_card_offset = _bot->index_for_raw(r_bot); 796 797 // The bitmap scan for the PRT always scans from _cur_region_cur_card + 1. 798 // To avoid special-casing this start case, and not miss the first bitmap 799 // entry, initialize _cur_region_cur_card with -1 instead of 0. 800 _cur_card_in_prt = (size_t)-1; 801 } 802 803 bool HeapRegionRemSetIterator::has_next(size_t& card_index) { 804 switch (_is) { 805 case Sparse: { 806 if (_sparse_iter.has_next(card_index)) { 807 _n_yielded_sparse++; 808 return true; 809 } 810 // Otherwise, deliberate fall-through 811 _is = Fine; 812 PerRegionTable* initial_fine_prt = _hrrs->_other_regions._first_all_fine_prts; 813 if (initial_fine_prt != NULL) { 814 switch_to_prt(_hrrs->_other_regions._first_all_fine_prts); 815 } 816 } 817 case Fine: 818 if (fine_has_next(card_index)) { 819 _n_yielded_fine++; 820 return true; 821 } 822 // Otherwise, deliberate fall-through 823 _is = Coarse; 824 case Coarse: 825 if (coarse_has_next(card_index)) { 826 _n_yielded_coarse++; 827 return true; 828 } 829 // Otherwise... 830 break; 831 } 832 return false; 833 } 834 835 void HeapRegionRemSet::reset_for_cleanup_tasks() { 836 SparsePRT::reset_for_cleanup_tasks(); 837 } 838 839 void HeapRegionRemSet::do_cleanup_work(HRRSCleanupTask* hrrs_cleanup_task) { 840 _other_regions.do_cleanup_work(hrrs_cleanup_task); 841 } 842 843 void HeapRegionRemSet::finish_cleanup_task(HRRSCleanupTask* hrrs_cleanup_task) { 844 SparsePRT::finish_cleanup_task(hrrs_cleanup_task); 845 } 846 847 #ifndef PRODUCT 848 void HeapRegionRemSet::test() { 849 os::sleep(Thread::current(), (jlong)5000, false); 850 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 851 852 // Run with "-XX:G1LogRSetRegionEntries=2", so that 1 and 5 end up in same 853 // hash bucket. 854 HeapRegion* hr0 = g1h->region_at(0); 855 HeapRegion* hr1 = g1h->region_at(1); 856 HeapRegion* hr2 = g1h->region_at(5); 857 HeapRegion* hr3 = g1h->region_at(6); 858 HeapRegion* hr4 = g1h->region_at(7); 859 HeapRegion* hr5 = g1h->region_at(8); 860 861 HeapWord* hr1_start = hr1->bottom(); 862 HeapWord* hr1_mid = hr1_start + HeapRegion::GrainWords/2; 863 HeapWord* hr1_last = hr1->end() - 1; 864 865 HeapWord* hr2_start = hr2->bottom(); 866 HeapWord* hr2_mid = hr2_start + HeapRegion::GrainWords/2; 867 HeapWord* hr2_last = hr2->end() - 1; 868 869 HeapWord* hr3_start = hr3->bottom(); 870 HeapWord* hr3_mid = hr3_start + HeapRegion::GrainWords/2; 871 HeapWord* hr3_last = hr3->end() - 1; 872 873 HeapRegionRemSet* hrrs = hr0->rem_set(); 874 875 // Make three references from region 0x101... 876 hrrs->add_reference((OopOrNarrowOopStar)hr1_start); 877 hrrs->add_reference((OopOrNarrowOopStar)hr1_mid); 878 hrrs->add_reference((OopOrNarrowOopStar)hr1_last); 879 880 hrrs->add_reference((OopOrNarrowOopStar)hr2_start); 881 hrrs->add_reference((OopOrNarrowOopStar)hr2_mid); 882 hrrs->add_reference((OopOrNarrowOopStar)hr2_last); 883 884 hrrs->add_reference((OopOrNarrowOopStar)hr3_start); 885 hrrs->add_reference((OopOrNarrowOopStar)hr3_mid); 886 hrrs->add_reference((OopOrNarrowOopStar)hr3_last); 887 888 // Now cause a coarsening. 889 hrrs->add_reference((OopOrNarrowOopStar)hr4->bottom()); 890 hrrs->add_reference((OopOrNarrowOopStar)hr5->bottom()); 891 892 // Now, does iteration yield these three? 893 HeapRegionRemSetIterator iter(hrrs); 894 size_t sum = 0; 895 size_t card_index; 896 while (iter.has_next(card_index)) { 897 HeapWord* card_start = g1h->bot()->address_for_index(card_index); 898 tty->print_cr(" Card " PTR_FORMAT ".", p2i(card_start)); 899 sum++; 900 } 901 guarantee(sum == 11 - 3 + 2048, "Failure"); 902 guarantee(sum == hrrs->occupied(), "Failure"); 903 } 904 #endif