1 /* 2 * Copyright (c) 2001, 2019, 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.inline.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 PerRegionTable* PerRegionTable::alloc(HeapRegion* hr) { 46 PerRegionTable* fl = _free_list; 47 while (fl != NULL) { 48 PerRegionTable* nxt = fl->next(); 49 PerRegionTable* res = Atomic::cmpxchg(&_free_list, fl, nxt); 50 if (res == fl) { 51 fl->init(hr, true); 52 return fl; 53 } else { 54 fl = _free_list; 55 } 56 } 57 assert(fl == NULL, "Loop condition."); 58 return new PerRegionTable(hr); 59 } 60 61 PerRegionTable* volatile PerRegionTable::_free_list = NULL; 62 63 size_t OtherRegionsTable::_max_fine_entries = 0; 64 size_t OtherRegionsTable::_mod_max_fine_entries_mask = 0; 65 size_t OtherRegionsTable::_fine_eviction_stride = 0; 66 size_t OtherRegionsTable::_fine_eviction_sample_size = 0; 67 68 OtherRegionsTable::OtherRegionsTable(Mutex* m) : 69 _g1h(G1CollectedHeap::heap()), 70 _m(m), 71 _coarse_map(G1CollectedHeap::heap()->max_regions(), mtGC), 72 _n_coarse_entries(0), 73 _fine_grain_regions(NULL), 74 _n_fine_entries(0), 75 _first_all_fine_prts(NULL), 76 _last_all_fine_prts(NULL), 77 _fine_eviction_start(0), 78 _sparse_table() 79 { 80 typedef PerRegionTable* PerRegionTablePtr; 81 82 if (_max_fine_entries == 0) { 83 assert(_mod_max_fine_entries_mask == 0, "Both or none."); 84 size_t max_entries_log = (size_t)log2_long((jlong)G1RSetRegionEntries); 85 _max_fine_entries = (size_t)1 << max_entries_log; 86 _mod_max_fine_entries_mask = _max_fine_entries - 1; 87 88 assert(_fine_eviction_sample_size == 0 89 && _fine_eviction_stride == 0, "All init at same time."); 90 _fine_eviction_sample_size = MAX2((size_t)4, max_entries_log); 91 _fine_eviction_stride = _max_fine_entries / _fine_eviction_sample_size; 92 } 93 94 _fine_grain_regions = NEW_C_HEAP_ARRAY3(PerRegionTablePtr, _max_fine_entries, 95 mtGC, CURRENT_PC, AllocFailStrategy::RETURN_NULL); 96 97 if (_fine_grain_regions == NULL) { 98 vm_exit_out_of_memory(sizeof(void*)*_max_fine_entries, OOM_MALLOC_ERROR, 99 "Failed to allocate _fine_grain_entries."); 100 } 101 102 for (size_t i = 0; i < _max_fine_entries; i++) { 103 _fine_grain_regions[i] = NULL; 104 } 105 } 106 107 void OtherRegionsTable::link_to_all(PerRegionTable* prt) { 108 // We always append to the beginning of the list for convenience; 109 // the order of entries in this list does not matter. 110 if (_first_all_fine_prts != NULL) { 111 assert(_first_all_fine_prts->prev() == NULL, "invariant"); 112 _first_all_fine_prts->set_prev(prt); 113 prt->set_next(_first_all_fine_prts); 114 } else { 115 // this is the first element we insert. Adjust the "last" pointer 116 _last_all_fine_prts = prt; 117 assert(prt->next() == NULL, "just checking"); 118 } 119 // the new element is always the first element without a predecessor 120 prt->set_prev(NULL); 121 _first_all_fine_prts = prt; 122 123 assert(prt->prev() == NULL, "just checking"); 124 assert(_first_all_fine_prts == prt, "just checking"); 125 assert((_first_all_fine_prts == NULL && _last_all_fine_prts == NULL) || 126 (_first_all_fine_prts != NULL && _last_all_fine_prts != NULL), 127 "just checking"); 128 assert(_last_all_fine_prts == NULL || _last_all_fine_prts->next() == NULL, 129 "just checking"); 130 assert(_first_all_fine_prts == NULL || _first_all_fine_prts->prev() == NULL, 131 "just checking"); 132 } 133 134 void OtherRegionsTable::unlink_from_all(PerRegionTable* prt) { 135 if (prt->prev() != NULL) { 136 assert(_first_all_fine_prts != prt, "just checking"); 137 prt->prev()->set_next(prt->next()); 138 // removing the last element in the list? 139 if (_last_all_fine_prts == prt) { 140 _last_all_fine_prts = prt->prev(); 141 } 142 } else { 143 assert(_first_all_fine_prts == prt, "just checking"); 144 _first_all_fine_prts = prt->next(); 145 // list is empty now? 146 if (_first_all_fine_prts == NULL) { 147 _last_all_fine_prts = NULL; 148 } 149 } 150 151 if (prt->next() != NULL) { 152 prt->next()->set_prev(prt->prev()); 153 } 154 155 prt->set_next(NULL); 156 prt->set_prev(NULL); 157 158 assert((_first_all_fine_prts == NULL && _last_all_fine_prts == NULL) || 159 (_first_all_fine_prts != NULL && _last_all_fine_prts != NULL), 160 "just checking"); 161 assert(_last_all_fine_prts == NULL || _last_all_fine_prts->next() == NULL, 162 "just checking"); 163 assert(_first_all_fine_prts == NULL || _first_all_fine_prts->prev() == NULL, 164 "just checking"); 165 } 166 167 CardIdx_t OtherRegionsTable::card_within_region(OopOrNarrowOopStar within_region, HeapRegion* hr) { 168 assert(hr->is_in_reserved(within_region), 169 "HeapWord " PTR_FORMAT " is outside of region %u [" PTR_FORMAT ", " PTR_FORMAT ")", 170 p2i(within_region), hr->hrm_index(), p2i(hr->bottom()), p2i(hr->end())); 171 CardIdx_t result = (CardIdx_t)(pointer_delta((HeapWord*)within_region, hr->bottom()) >> (CardTable::card_shift - LogHeapWordSize)); 172 return result; 173 } 174 175 void OtherRegionsTable::add_reference(OopOrNarrowOopStar from, uint tid) { 176 // Note that this may be a continued H region. 177 HeapRegion* from_hr = _g1h->heap_region_containing(from); 178 RegionIdx_t from_hrm_ind = (RegionIdx_t) from_hr->hrm_index(); 179 180 // If the region is already coarsened, return. 181 if (_coarse_map.at(from_hrm_ind)) { 182 assert(contains_reference(from), "We just found " PTR_FORMAT " in the Coarse table", p2i(from)); 183 return; 184 } 185 186 // Otherwise find a per-region table to add it to. 187 size_t ind = from_hrm_ind & _mod_max_fine_entries_mask; 188 PerRegionTable* prt = find_region_table(ind, from_hr); 189 if (prt == NULL) { 190 MutexLocker x(_m, Mutex::_no_safepoint_check_flag); 191 // Confirm that it's really not there... 192 prt = find_region_table(ind, from_hr); 193 if (prt == NULL) { 194 195 CardIdx_t card_index = card_within_region(from, from_hr); 196 197 if (_sparse_table.add_card(from_hrm_ind, card_index)) { 198 assert(contains_reference_locked(from), "We just added " PTR_FORMAT " to the Sparse table", p2i(from)); 199 return; 200 } 201 202 if (_n_fine_entries == _max_fine_entries) { 203 prt = delete_region_table(); 204 // There is no need to clear the links to the 'all' list here: 205 // prt will be reused immediately, i.e. remain in the 'all' list. 206 prt->init(from_hr, false /* clear_links_to_all_list */); 207 } else { 208 prt = PerRegionTable::alloc(from_hr); 209 link_to_all(prt); 210 } 211 212 PerRegionTable* first_prt = _fine_grain_regions[ind]; 213 prt->set_collision_list_next(first_prt); 214 // The assignment into _fine_grain_regions allows the prt to 215 // start being used concurrently. In addition to 216 // collision_list_next which must be visible (else concurrent 217 // parsing of the list, if any, may fail to see other entries), 218 // the content of the prt must be visible (else for instance 219 // some mark bits may not yet seem cleared or a 'later' update 220 // performed by a concurrent thread could be undone when the 221 // zeroing becomes visible). This requires store ordering. 222 Atomic::release_store(&_fine_grain_regions[ind], prt); 223 _n_fine_entries++; 224 225 // Transfer from sparse to fine-grain. 226 SparsePRTEntry *sprt_entry = _sparse_table.get_entry(from_hrm_ind); 227 assert(sprt_entry != NULL, "There should have been an entry"); 228 for (int i = 0; i < sprt_entry->num_valid_cards(); i++) { 229 CardIdx_t c = sprt_entry->card(i); 230 prt->add_card(c); 231 } 232 // Now we can delete the sparse entry. 233 bool res = _sparse_table.delete_entry(from_hrm_ind); 234 assert(res, "It should have been there."); 235 } 236 assert(prt != NULL && prt->hr() == from_hr, "consequence"); 237 } 238 // Note that we can't assert "prt->hr() == from_hr", because of the 239 // possibility of concurrent reuse. But see head comment of 240 // OtherRegionsTable for why this is OK. 241 assert(prt != NULL, "Inv"); 242 243 prt->add_reference(from); 244 assert(contains_reference(from), "We just added " PTR_FORMAT " to the PRT (%d)", p2i(from), prt->contains_reference(from)); 245 } 246 247 PerRegionTable* 248 OtherRegionsTable::find_region_table(size_t ind, HeapRegion* hr) const { 249 assert(ind < _max_fine_entries, "Preconditions."); 250 PerRegionTable* prt = _fine_grain_regions[ind]; 251 while (prt != NULL && prt->hr() != hr) { 252 prt = prt->collision_list_next(); 253 } 254 // Loop postcondition is the method postcondition. 255 return prt; 256 } 257 258 jint OtherRegionsTable::_n_coarsenings = 0; 259 260 PerRegionTable* OtherRegionsTable::delete_region_table() { 261 assert(_m->owned_by_self(), "Precondition"); 262 assert(_n_fine_entries == _max_fine_entries, "Precondition"); 263 PerRegionTable* max = NULL; 264 jint max_occ = 0; 265 PerRegionTable** max_prev = NULL; 266 size_t max_ind; 267 268 size_t i = _fine_eviction_start; 269 for (size_t k = 0; k < _fine_eviction_sample_size; k++) { 270 size_t ii = i; 271 // Make sure we get a non-NULL sample. 272 while (_fine_grain_regions[ii] == NULL) { 273 ii++; 274 if (ii == _max_fine_entries) ii = 0; 275 guarantee(ii != i, "We must find one."); 276 } 277 PerRegionTable** prev = &_fine_grain_regions[ii]; 278 PerRegionTable* cur = *prev; 279 while (cur != NULL) { 280 jint cur_occ = cur->occupied(); 281 if (max == NULL || cur_occ > max_occ) { 282 max = cur; 283 max_prev = prev; 284 max_ind = i; 285 max_occ = cur_occ; 286 } 287 prev = cur->collision_list_next_addr(); 288 cur = cur->collision_list_next(); 289 } 290 i = i + _fine_eviction_stride; 291 if (i >= _n_fine_entries) i = i - _n_fine_entries; 292 } 293 294 _fine_eviction_start++; 295 296 if (_fine_eviction_start >= _n_fine_entries) { 297 _fine_eviction_start -= _n_fine_entries; 298 } 299 300 guarantee(max != NULL, "Since _n_fine_entries > 0"); 301 guarantee(max_prev != NULL, "Since max != NULL."); 302 303 // Set the corresponding coarse bit. 304 size_t max_hrm_index = (size_t) max->hr()->hrm_index(); 305 if (!_coarse_map.at(max_hrm_index)) { 306 _coarse_map.at_put(max_hrm_index, true); 307 _n_coarse_entries++; 308 } 309 310 // Unsplice. 311 *max_prev = max->collision_list_next(); 312 Atomic::inc(&_n_coarsenings); 313 _n_fine_entries--; 314 return max; 315 } 316 317 bool OtherRegionsTable::occupancy_less_or_equal_than(size_t limit) const { 318 if (limit <= (size_t)G1RSetSparseRegionEntries) { 319 return occ_coarse() == 0 && _first_all_fine_prts == NULL && occ_sparse() <= limit; 320 } else { 321 // Current uses of this method may only use values less than G1RSetSparseRegionEntries 322 // for the limit. The solution, comparing against occupied() would be too slow 323 // at this time. 324 Unimplemented(); 325 return false; 326 } 327 } 328 329 bool OtherRegionsTable::is_empty() const { 330 return occ_sparse() == 0 && occ_coarse() == 0 && _first_all_fine_prts == NULL; 331 } 332 333 size_t OtherRegionsTable::occupied() const { 334 size_t sum = occ_fine(); 335 sum += occ_sparse(); 336 sum += occ_coarse(); 337 return sum; 338 } 339 340 size_t OtherRegionsTable::occ_fine() const { 341 size_t sum = 0; 342 343 size_t num = 0; 344 PerRegionTable * cur = _first_all_fine_prts; 345 while (cur != NULL) { 346 sum += cur->occupied(); 347 cur = cur->next(); 348 num++; 349 } 350 guarantee(num == _n_fine_entries, "just checking"); 351 return sum; 352 } 353 354 size_t OtherRegionsTable::occ_coarse() const { 355 return (_n_coarse_entries * HeapRegion::CardsPerRegion); 356 } 357 358 size_t OtherRegionsTable::occ_sparse() const { 359 return _sparse_table.occupied(); 360 } 361 362 size_t OtherRegionsTable::mem_size() const { 363 size_t sum = 0; 364 // all PRTs are of the same size so it is sufficient to query only one of them. 365 if (_first_all_fine_prts != NULL) { 366 assert(_last_all_fine_prts != NULL && 367 _first_all_fine_prts->mem_size() == _last_all_fine_prts->mem_size(), "check that mem_size() is constant"); 368 sum += _first_all_fine_prts->mem_size() * _n_fine_entries; 369 } 370 sum += (sizeof(PerRegionTable*) * _max_fine_entries); 371 sum += (_coarse_map.size_in_words() * HeapWordSize); 372 sum += (_sparse_table.mem_size()); 373 sum += sizeof(OtherRegionsTable) - sizeof(_sparse_table); // Avoid double counting above. 374 return sum; 375 } 376 377 size_t OtherRegionsTable::static_mem_size() { 378 return G1FromCardCache::static_mem_size(); 379 } 380 381 size_t OtherRegionsTable::fl_mem_size() { 382 return PerRegionTable::fl_mem_size(); 383 } 384 385 void OtherRegionsTable::clear() { 386 // if there are no entries, skip this step 387 if (_first_all_fine_prts != NULL) { 388 guarantee(_first_all_fine_prts != NULL && _last_all_fine_prts != NULL, "just checking"); 389 PerRegionTable::bulk_free(_first_all_fine_prts, _last_all_fine_prts); 390 memset(_fine_grain_regions, 0, _max_fine_entries * sizeof(_fine_grain_regions[0])); 391 } else { 392 guarantee(_first_all_fine_prts == NULL && _last_all_fine_prts == NULL, "just checking"); 393 } 394 395 _first_all_fine_prts = _last_all_fine_prts = NULL; 396 _sparse_table.clear(); 397 if (_n_coarse_entries > 0) { 398 _coarse_map.clear(); 399 } 400 _n_fine_entries = 0; 401 _n_coarse_entries = 0; 402 } 403 404 bool OtherRegionsTable::contains_reference(OopOrNarrowOopStar from) const { 405 // Cast away const in this case. 406 MutexLocker x((Mutex*)_m, Mutex::_no_safepoint_check_flag); 407 return contains_reference_locked(from); 408 } 409 410 bool OtherRegionsTable::contains_reference_locked(OopOrNarrowOopStar from) const { 411 HeapRegion* hr = _g1h->heap_region_containing(from); 412 RegionIdx_t hr_ind = (RegionIdx_t) hr->hrm_index(); 413 // Is this region in the coarse map? 414 if (_coarse_map.at(hr_ind)) return true; 415 416 PerRegionTable* prt = find_region_table(hr_ind & _mod_max_fine_entries_mask, 417 hr); 418 if (prt != NULL) { 419 return prt->contains_reference(from); 420 421 } else { 422 CardIdx_t card_index = card_within_region(from, hr); 423 return _sparse_table.contains_card(hr_ind, card_index); 424 } 425 } 426 427 HeapRegionRemSet::HeapRegionRemSet(G1BlockOffsetTable* bot, 428 HeapRegion* hr) 429 : _bot(bot), 430 _code_roots(), 431 _m(Mutex::leaf, FormatBuffer<128>("HeapRegionRemSet lock #%u", hr->hrm_index()), true, Mutex::_safepoint_check_never), 432 _other_regions(&_m), 433 _hr(hr), 434 _state(Untracked) 435 { 436 } 437 438 void HeapRegionRemSet::clear_fcc() { 439 G1FromCardCache::clear(_hr->hrm_index()); 440 } 441 442 void HeapRegionRemSet::setup_remset_size() { 443 const int LOG_M = 20; 444 guarantee(HeapRegion::LogOfHRGrainBytes >= LOG_M, "Code assumes the region size >= 1M, but is " SIZE_FORMAT "B", HeapRegion::GrainBytes); 445 446 int region_size_log_mb = HeapRegion::LogOfHRGrainBytes - LOG_M; 447 if (FLAG_IS_DEFAULT(G1RSetSparseRegionEntries)) { 448 G1RSetSparseRegionEntries = G1RSetSparseRegionEntriesBase * ((size_t)1 << (region_size_log_mb + 1)); 449 } 450 if (FLAG_IS_DEFAULT(G1RSetRegionEntries)) { 451 G1RSetRegionEntries = G1RSetRegionEntriesBase * (region_size_log_mb + 1); 452 } 453 guarantee(G1RSetSparseRegionEntries > 0 && G1RSetRegionEntries > 0 , "Sanity"); 454 } 455 456 void HeapRegionRemSet::clear(bool only_cardset) { 457 MutexLocker x(&_m, Mutex::_no_safepoint_check_flag); 458 clear_locked(only_cardset); 459 } 460 461 void HeapRegionRemSet::clear_locked(bool only_cardset) { 462 if (!only_cardset) { 463 _code_roots.clear(); 464 } 465 clear_fcc(); 466 _other_regions.clear(); 467 set_state_empty(); 468 assert(occupied_locked() == 0, "Should be clear."); 469 } 470 471 // Code roots support 472 // 473 // The code root set is protected by two separate locking schemes 474 // When at safepoint the per-hrrs lock must be held during modifications 475 // except when doing a full gc. 476 // When not at safepoint the CodeCache_lock must be held during modifications. 477 // When concurrent readers access the contains() function 478 // (during the evacuation phase) no removals are allowed. 479 480 void HeapRegionRemSet::add_strong_code_root(nmethod* nm) { 481 assert(nm != NULL, "sanity"); 482 assert((!CodeCache_lock->owned_by_self() || SafepointSynchronize::is_at_safepoint()), 483 "should call add_strong_code_root_locked instead. CodeCache_lock->owned_by_self(): %s, is_at_safepoint(): %s", 484 BOOL_TO_STR(CodeCache_lock->owned_by_self()), BOOL_TO_STR(SafepointSynchronize::is_at_safepoint())); 485 // Optimistic unlocked contains-check 486 if (!_code_roots.contains(nm)) { 487 MutexLocker ml(&_m, Mutex::_no_safepoint_check_flag); 488 add_strong_code_root_locked(nm); 489 } 490 } 491 492 void HeapRegionRemSet::add_strong_code_root_locked(nmethod* nm) { 493 assert(nm != NULL, "sanity"); 494 assert((CodeCache_lock->owned_by_self() || 495 (SafepointSynchronize::is_at_safepoint() && 496 (_m.owned_by_self() || Thread::current()->is_VM_thread()))), 497 "not safely locked. CodeCache_lock->owned_by_self(): %s, is_at_safepoint(): %s, _m.owned_by_self(): %s, Thread::current()->is_VM_thread(): %s", 498 BOOL_TO_STR(CodeCache_lock->owned_by_self()), BOOL_TO_STR(SafepointSynchronize::is_at_safepoint()), 499 BOOL_TO_STR(_m.owned_by_self()), BOOL_TO_STR(Thread::current()->is_VM_thread())); 500 _code_roots.add(nm); 501 } 502 503 void HeapRegionRemSet::remove_strong_code_root(nmethod* nm) { 504 assert(nm != NULL, "sanity"); 505 assert_locked_or_safepoint(CodeCache_lock); 506 507 MutexLocker ml(CodeCache_lock->owned_by_self() ? NULL : &_m, Mutex::_no_safepoint_check_flag); 508 _code_roots.remove(nm); 509 510 // Check that there were no duplicates 511 guarantee(!_code_roots.contains(nm), "duplicate entry found"); 512 } 513 514 void HeapRegionRemSet::strong_code_roots_do(CodeBlobClosure* blk) const { 515 _code_roots.nmethods_do(blk); 516 } 517 518 void HeapRegionRemSet::clean_strong_code_roots(HeapRegion* hr) { 519 _code_roots.clean(hr); 520 } 521 522 size_t HeapRegionRemSet::strong_code_roots_mem_size() { 523 return _code_roots.mem_size(); 524 }