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/heapRegion.hpp" 27 #include "gc/g1/heapRegionBounds.inline.hpp" 28 #include "gc/g1/heapRegionRemSet.hpp" 29 #include "gc/g1/sparsePRT.hpp" 30 #include "gc/shared/cardTableBarrierSet.hpp" 31 #include "gc/shared/space.inline.hpp" 32 #include "memory/allocation.inline.hpp" 33 #include "runtime/atomic.hpp" 34 #include "runtime/mutexLocker.hpp" 35 36 // Check that the size of the SparsePRTEntry is evenly divisible by the maximum 37 // member type to avoid SIGBUS when accessing them. 38 STATIC_ASSERT(sizeof(SparsePRTEntry) % sizeof(int) == 0); 39 40 void SparsePRTEntry::init(RegionIdx_t region_ind) { 41 // Check that the card array element type can represent all cards in the region. 42 // Choose a large SparsePRTEntry::card_elem_t (e.g. CardIdx_t) if required. 43 assert(((size_t)1 << (sizeof(SparsePRTEntry::card_elem_t) * BitsPerByte)) * 44 G1CardTable::card_size >= HeapRegionBounds::max_size(), "precondition"); 45 assert(G1RSetSparseRegionEntries > 0, "precondition"); 46 _region_ind = region_ind; 47 _next_index = RSHashTable::NullEntry; 48 _next_null = 0; 49 } 50 51 bool SparsePRTEntry::contains_card(CardIdx_t card_index) const { 52 for (int i = 0; i < num_valid_cards(); i++) { 53 if (card(i) == card_index) { 54 return true; 55 } 56 } 57 return false; 58 } 59 60 SparsePRTEntry::AddCardResult SparsePRTEntry::add_card(CardIdx_t card_index) { 61 for (int i = 0; i < num_valid_cards(); i++) { 62 if (card(i) == card_index) { 63 return found; 64 } 65 } 66 if (num_valid_cards() < cards_num() - 1) { 67 _cards[_next_null] = (card_elem_t)card_index; 68 _next_null++; 69 return added; 70 } 71 // Otherwise, we're full. 72 return overflow; 73 } 74 75 void SparsePRTEntry::copy_cards(card_elem_t* cards) const { 76 memcpy(cards, _cards, cards_num() * sizeof(card_elem_t)); 77 } 78 79 void SparsePRTEntry::copy_cards(SparsePRTEntry* e) const { 80 copy_cards(e->_cards); 81 assert(_next_null >= 0, "invariant"); 82 assert(_next_null <= cards_num(), "invariant"); 83 e->_next_null = _next_null; 84 } 85 86 // ---------------------------------------------------------------------- 87 88 float RSHashTable::TableOccupancyFactor = 0.5f; 89 90 RSHashTable::RSHashTable(size_t capacity) : 91 _num_entries(0), 92 _capacity(capacity), 93 _capacity_mask(capacity-1), 94 _occupied_entries(0), 95 _occupied_cards(0), 96 _entries(NULL), 97 _buckets(NEW_C_HEAP_ARRAY(int, capacity, mtGC)), 98 _free_region(0), 99 _free_list(NullEntry) 100 { 101 _num_entries = (capacity * TableOccupancyFactor) + 1; 102 _entries = (SparsePRTEntry*)NEW_C_HEAP_ARRAY(char, _num_entries * SparsePRTEntry::size(), mtGC); 103 clear(); 104 } 105 106 RSHashTable::~RSHashTable() { 107 if (_entries != NULL) { 108 FREE_C_HEAP_ARRAY(SparsePRTEntry, _entries); 109 _entries = NULL; 110 } 111 if (_buckets != NULL) { 112 FREE_C_HEAP_ARRAY(int, _buckets); 113 _buckets = NULL; 114 } 115 } 116 117 void RSHashTable::clear() { 118 _occupied_entries = 0; 119 _occupied_cards = 0; 120 guarantee(_entries != NULL, "INV"); 121 guarantee(_buckets != NULL, "INV"); 122 123 guarantee(_capacity <= ((size_t)1 << (sizeof(int)*BitsPerByte-1)) - 1, 124 "_capacity too large"); 125 126 // This will put -1 == NullEntry in the key field of all entries. 127 memset((void*)_entries, NullEntry, _num_entries * SparsePRTEntry::size()); 128 memset((void*)_buckets, NullEntry, _capacity * sizeof(int)); 129 _free_list = NullEntry; 130 _free_region = 0; 131 } 132 133 bool RSHashTable::add_card(RegionIdx_t region_ind, CardIdx_t card_index) { 134 SparsePRTEntry* e = entry_for_region_ind_create(region_ind); 135 assert(e != NULL && e->r_ind() == region_ind, 136 "Postcondition of call above."); 137 SparsePRTEntry::AddCardResult res = e->add_card(card_index); 138 if (res == SparsePRTEntry::added) _occupied_cards++; 139 assert(e->num_valid_cards() > 0, "Postcondition"); 140 return res != SparsePRTEntry::overflow; 141 } 142 143 SparsePRTEntry* RSHashTable::get_entry(RegionIdx_t region_ind) const { 144 int ind = (int) (region_ind & capacity_mask()); 145 int cur_ind = _buckets[ind]; 146 SparsePRTEntry* cur; 147 while (cur_ind != NullEntry && 148 (cur = entry(cur_ind))->r_ind() != region_ind) { 149 cur_ind = cur->next_index(); 150 } 151 152 if (cur_ind == NullEntry) return NULL; 153 // Otherwise... 154 assert(cur->r_ind() == region_ind, "Postcondition of loop + test above."); 155 assert(cur->num_valid_cards() > 0, "Inv"); 156 return cur; 157 } 158 159 bool RSHashTable::delete_entry(RegionIdx_t region_ind) { 160 int ind = (int) (region_ind & capacity_mask()); 161 int* prev_loc = &_buckets[ind]; 162 int cur_ind = *prev_loc; 163 SparsePRTEntry* cur; 164 while (cur_ind != NullEntry && 165 (cur = entry(cur_ind))->r_ind() != region_ind) { 166 prev_loc = cur->next_index_addr(); 167 cur_ind = *prev_loc; 168 } 169 170 if (cur_ind == NullEntry) return false; 171 // Otherwise, splice out "cur". 172 *prev_loc = cur->next_index(); 173 _occupied_cards -= cur->num_valid_cards(); 174 free_entry(cur_ind); 175 _occupied_entries--; 176 return true; 177 } 178 179 SparsePRTEntry* 180 RSHashTable::entry_for_region_ind_create(RegionIdx_t region_ind) { 181 SparsePRTEntry* res = get_entry(region_ind); 182 if (res == NULL) { 183 int new_ind = alloc_entry(); 184 res = entry(new_ind); 185 res->init(region_ind); 186 // Insert at front. 187 int ind = (int) (region_ind & capacity_mask()); 188 res->set_next_index(_buckets[ind]); 189 _buckets[ind] = new_ind; 190 _occupied_entries++; 191 } 192 return res; 193 } 194 195 int RSHashTable::alloc_entry() { 196 int res; 197 if (_free_list != NullEntry) { 198 res = _free_list; 199 _free_list = entry(res)->next_index(); 200 return res; 201 } else if ((size_t)_free_region < _num_entries) { 202 res = _free_region; 203 _free_region++; 204 return res; 205 } else { 206 return NullEntry; 207 } 208 } 209 210 void RSHashTable::free_entry(int fi) { 211 entry(fi)->set_next_index(_free_list); 212 _free_list = fi; 213 } 214 215 void RSHashTable::add_entry(SparsePRTEntry* e) { 216 assert(e->num_valid_cards() > 0, "Precondition."); 217 SparsePRTEntry* e2 = entry_for_region_ind_create(e->r_ind()); 218 e->copy_cards(e2); 219 _occupied_cards += e2->num_valid_cards(); 220 assert(e2->num_valid_cards() > 0, "Postcondition."); 221 } 222 223 CardIdx_t RSHashTableIter::find_first_card_in_list() { 224 while (_bl_ind != RSHashTable::NullEntry) { 225 SparsePRTEntry* sparse_entry = _rsht->entry(_bl_ind); 226 if (sparse_entry->num_valid_cards() > 0) { 227 return sparse_entry->card(0); 228 } else { 229 _bl_ind = sparse_entry->next_index(); 230 } 231 } 232 // Otherwise, none found: 233 return NoCardFound; 234 } 235 236 size_t RSHashTableIter::compute_card_ind(CardIdx_t ci) { 237 return (_rsht->entry(_bl_ind)->r_ind() * HeapRegion::CardsPerRegion) + ci; 238 } 239 240 bool RSHashTableIter::has_next(size_t& card_index) { 241 _card_ind++; 242 if (_bl_ind >= 0) { 243 SparsePRTEntry* e = _rsht->entry(_bl_ind); 244 if (_card_ind < e->num_valid_cards()) { 245 CardIdx_t ci = e->card(_card_ind); 246 card_index = compute_card_ind(ci); 247 return true; 248 } 249 } 250 251 // Otherwise, must find the next valid entry. 252 _card_ind = 0; 253 254 if (_bl_ind != RSHashTable::NullEntry) { 255 _bl_ind = _rsht->entry(_bl_ind)->next_index(); 256 CardIdx_t ci = find_first_card_in_list(); 257 if (ci != NoCardFound) { 258 card_index = compute_card_ind(ci); 259 return true; 260 } 261 } 262 // If we didn't return above, must go to the next non-null table index. 263 _tbl_ind++; 264 while ((size_t)_tbl_ind < _rsht->capacity()) { 265 _bl_ind = _rsht->_buckets[_tbl_ind]; 266 CardIdx_t ci = find_first_card_in_list(); 267 if (ci != NoCardFound) { 268 card_index = compute_card_ind(ci); 269 return true; 270 } 271 // Otherwise, try next entry. 272 _tbl_ind++; 273 } 274 // Otherwise, there were no entry. 275 return false; 276 } 277 278 bool RSHashTable::contains_card(RegionIdx_t region_index, CardIdx_t card_index) const { 279 SparsePRTEntry* e = get_entry(region_index); 280 return (e != NULL && e->contains_card(card_index)); 281 } 282 283 size_t RSHashTable::mem_size() const { 284 return sizeof(RSHashTable) + 285 _num_entries * (SparsePRTEntry::size() + sizeof(int)); 286 } 287 288 // ---------------------------------------------------------------------- 289 290 SparsePRT* volatile SparsePRT::_head_expanded_list = NULL; 291 292 void SparsePRT::add_to_expanded_list(SparsePRT* sprt) { 293 // We could expand multiple times in a pause -- only put on list once. 294 if (sprt->expanded()) return; 295 sprt->set_expanded(true); 296 SparsePRT* hd = _head_expanded_list; 297 while (true) { 298 sprt->_next_expanded = hd; 299 SparsePRT* res = Atomic::cmpxchg(sprt, &_head_expanded_list, hd); 300 if (res == hd) return; 301 else hd = res; 302 } 303 } 304 305 306 SparsePRT* SparsePRT::get_from_expanded_list() { 307 SparsePRT* hd = _head_expanded_list; 308 while (hd != NULL) { 309 SparsePRT* next = hd->next_expanded(); 310 SparsePRT* res = Atomic::cmpxchg(next, &_head_expanded_list, hd); 311 if (res == hd) { 312 hd->set_next_expanded(NULL); 313 return hd; 314 } else { 315 hd = res; 316 } 317 } 318 return NULL; 319 } 320 321 void SparsePRT::reset_for_cleanup_tasks() { 322 _head_expanded_list = NULL; 323 } 324 325 void SparsePRT::do_cleanup_work(SparsePRTCleanupTask* sprt_cleanup_task) { 326 if (should_be_on_expanded_list()) { 327 sprt_cleanup_task->add(this); 328 } 329 } 330 331 void SparsePRT::finish_cleanup_task(SparsePRTCleanupTask* sprt_cleanup_task) { 332 assert(ParGCRareEvent_lock->owned_by_self(), "pre-condition"); 333 SparsePRT* head = sprt_cleanup_task->head(); 334 SparsePRT* tail = sprt_cleanup_task->tail(); 335 if (head != NULL) { 336 assert(tail != NULL, "if head is not NULL, so should tail"); 337 338 tail->set_next_expanded(_head_expanded_list); 339 _head_expanded_list = head; 340 } else { 341 assert(tail == NULL, "if head is NULL, so should tail"); 342 } 343 } 344 345 bool SparsePRT::should_be_on_expanded_list() { 346 if (_expanded) { 347 assert(_cur != _next, "if _expanded is true, cur should be != _next"); 348 } else { 349 assert(_cur == _next, "if _expanded is false, cur should be == _next"); 350 } 351 return expanded(); 352 } 353 354 void SparsePRT::cleanup_all() { 355 // First clean up all expanded tables so they agree on next and cur. 356 SparsePRT* sprt = get_from_expanded_list(); 357 while (sprt != NULL) { 358 sprt->cleanup(); 359 sprt = get_from_expanded_list(); 360 } 361 } 362 363 364 SparsePRT::SparsePRT() : 365 _expanded(false), _next_expanded(NULL) 366 { 367 _cur = new RSHashTable(InitialCapacity); 368 _next = _cur; 369 } 370 371 372 SparsePRT::~SparsePRT() { 373 assert(_next != NULL && _cur != NULL, "Inv"); 374 if (_cur != _next) { delete _cur; } 375 delete _next; 376 } 377 378 379 size_t SparsePRT::mem_size() const { 380 // We ignore "_cur" here, because it either = _next, or else it is 381 // on the deleted list. 382 return sizeof(SparsePRT) + _next->mem_size(); 383 } 384 385 bool SparsePRT::add_card(RegionIdx_t region_id, CardIdx_t card_index) { 386 if (_next->should_expand()) { 387 expand(); 388 } 389 return _next->add_card(region_id, card_index); 390 } 391 392 SparsePRTEntry* SparsePRT::get_entry(RegionIdx_t region_id) { 393 return _next->get_entry(region_id); 394 } 395 396 bool SparsePRT::delete_entry(RegionIdx_t region_id) { 397 return _next->delete_entry(region_id); 398 } 399 400 void SparsePRT::clear() { 401 // If they differ, _next is bigger then cur, so next has no chance of 402 // being the initial size. 403 if (_next != _cur) { 404 delete _next; 405 } 406 407 if (_cur->capacity() != InitialCapacity) { 408 delete _cur; 409 _cur = new RSHashTable(InitialCapacity); 410 } else { 411 _cur->clear(); 412 } 413 _next = _cur; 414 _expanded = false; 415 } 416 417 void SparsePRT::cleanup() { 418 // Make sure that the current and next tables agree. 419 if (_cur != _next) { 420 delete _cur; 421 } 422 _cur = _next; 423 set_expanded(false); 424 } 425 426 void SparsePRT::expand() { 427 RSHashTable* last = _next; 428 _next = new RSHashTable(last->capacity() * 2); 429 for (size_t i = 0; i < last->num_entries(); i++) { 430 SparsePRTEntry* e = last->entry((int)i); 431 if (e->valid_entry()) { 432 _next->add_entry(e); 433 } 434 } 435 if (last != _cur) { 436 delete last; 437 } 438 add_to_expanded_list(this); 439 } 440 441 void SparsePRTCleanupTask::add(SparsePRT* sprt) { 442 assert(sprt->should_be_on_expanded_list(), "pre-condition"); 443 444 sprt->set_next_expanded(NULL); 445 if (_tail != NULL) { 446 _tail->set_next_expanded(sprt); 447 } else { 448 _head = sprt; 449 } 450 _tail = sprt; 451 }