1 /* 2 * Copyright (c) 2001, 2015, 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/heapRegionRemSet.hpp" 28 #include "gc/g1/sparsePRT.hpp" 29 #include "gc/shared/cardTableModRefBS.hpp" 30 #include "gc/shared/space.inline.hpp" 31 #include "memory/allocation.inline.hpp" 32 #include "runtime/atomic.inline.hpp" 33 #include "runtime/mutexLocker.hpp" 34 35 #define SPARSE_PRT_VERBOSE 0 36 37 void SparsePRTEntry::init(RegionIdx_t region_ind) { 38 _region_ind = region_ind; 39 _next_index = NullEntry; 40 41 for (int i = 0; i < cards_num(); i++) { 42 _cards[i] = NullEntry; 43 } 44 } 45 46 bool SparsePRTEntry::contains_card(CardIdx_t card_index) const { 47 for (int i = 0; i < cards_num(); i++) { 48 if (_cards[i] == card_index) return true; 49 } 50 return false; 51 } 52 53 int SparsePRTEntry::num_valid_cards() const { 54 int sum = 0; 55 for (int i = 0; i < cards_num(); i++) { 56 sum += (_cards[i] != NullEntry); 57 } 58 return sum; 59 } 60 61 SparsePRTEntry::AddCardResult SparsePRTEntry::add_card(CardIdx_t card_index) { 62 for (int i = 0; i < cards_num(); i++) { 63 CardIdx_t c = _cards[i]; 64 if (c == card_index) return found; 65 if (c == NullEntry) { _cards[i] = card_index; return added; } 66 } 67 // Otherwise, we're full. 68 return overflow; 69 } 70 71 void SparsePRTEntry::copy_cards(CardIdx_t* cards) const { 72 memcpy(cards, _cards, cards_num() * sizeof(CardIdx_t)); 73 } 74 75 void SparsePRTEntry::copy_cards(SparsePRTEntry* e) const { 76 copy_cards(&e->_cards[0]); 77 } 78 79 // ---------------------------------------------------------------------- 80 81 RSHashTable::RSHashTable(size_t capacity) : 82 _capacity(capacity), _capacity_mask(capacity-1), 83 _occupied_entries(0), _occupied_cards(0), 84 _entries((SparsePRTEntry*)NEW_C_HEAP_ARRAY(char, SparsePRTEntry::size() * capacity, mtGC)), 85 _buckets(NEW_C_HEAP_ARRAY(int, capacity, mtGC)), 86 _free_list(NullEntry), _free_region(0) 87 { 88 clear(); 89 } 90 91 RSHashTable::~RSHashTable() { 92 if (_entries != NULL) { 93 FREE_C_HEAP_ARRAY(SparsePRTEntry, _entries); 94 _entries = NULL; 95 } 96 if (_buckets != NULL) { 97 FREE_C_HEAP_ARRAY(int, _buckets); 98 _buckets = NULL; 99 } 100 } 101 102 void RSHashTable::clear() { 103 _occupied_entries = 0; 104 _occupied_cards = 0; 105 guarantee(_entries != NULL, "INV"); 106 guarantee(_buckets != NULL, "INV"); 107 108 guarantee(_capacity <= ((size_t)1 << (sizeof(int)*BitsPerByte-1)) - 1, 109 "_capacity too large"); 110 111 // This will put -1 == NullEntry in the key field of all entries. 112 memset(_entries, NullEntry, _capacity * SparsePRTEntry::size()); 113 memset(_buckets, NullEntry, _capacity * sizeof(int)); 114 _free_list = NullEntry; 115 _free_region = 0; 116 } 117 118 bool RSHashTable::add_card(RegionIdx_t region_ind, CardIdx_t card_index) { 119 SparsePRTEntry* e = entry_for_region_ind_create(region_ind); 120 assert(e != NULL && e->r_ind() == region_ind, 121 "Postcondition of call above."); 122 SparsePRTEntry::AddCardResult res = e->add_card(card_index); 123 if (res == SparsePRTEntry::added) _occupied_cards++; 124 #if SPARSE_PRT_VERBOSE 125 gclog_or_tty->print_cr(" after add_card[%d]: valid-cards = %d.", 126 pointer_delta(e, _entries, SparsePRTEntry::size()), 127 e->num_valid_cards()); 128 #endif 129 assert(e->num_valid_cards() > 0, "Postcondition"); 130 return res != SparsePRTEntry::overflow; 131 } 132 133 bool RSHashTable::get_cards(RegionIdx_t region_ind, CardIdx_t* cards) { 134 SparsePRTEntry* entry = get_entry(region_ind); 135 if (entry == NULL) { 136 return false; 137 } 138 // Otherwise... 139 entry->copy_cards(cards); 140 return true; 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 assert(0 <= new_ind && (size_t)new_ind < capacity(), "There should be room."); 185 res = entry(new_ind); 186 res->init(region_ind); 187 // Insert at front. 188 int ind = (int) (region_ind & capacity_mask()); 189 res->set_next_index(_buckets[ind]); 190 _buckets[ind] = new_ind; 191 _occupied_entries++; 192 } 193 return res; 194 } 195 196 int RSHashTable::alloc_entry() { 197 int res; 198 if (_free_list != NullEntry) { 199 res = _free_list; 200 _free_list = entry(res)->next_index(); 201 return res; 202 } else if ((size_t) _free_region+1 < capacity()) { 203 res = _free_region; 204 _free_region++; 205 return res; 206 } else { 207 return NullEntry; 208 } 209 } 210 211 void RSHashTable::free_entry(int fi) { 212 entry(fi)->set_next_index(_free_list); 213 _free_list = fi; 214 } 215 216 void RSHashTable::add_entry(SparsePRTEntry* e) { 217 assert(e->num_valid_cards() > 0, "Precondition."); 218 SparsePRTEntry* e2 = entry_for_region_ind_create(e->r_ind()); 219 e->copy_cards(e2); 220 _occupied_cards += e2->num_valid_cards(); 221 assert(e2->num_valid_cards() > 0, "Postcondition."); 222 } 223 224 CardIdx_t RSHashTableIter::find_first_card_in_list() { 225 CardIdx_t res; 226 while (_bl_ind != RSHashTable::NullEntry) { 227 res = _rsht->entry(_bl_ind)->card(0); 228 if (res != SparsePRTEntry::NullEntry) { 229 return res; 230 } else { 231 _bl_ind = _rsht->entry(_bl_ind)->next_index(); 232 } 233 } 234 // Otherwise, none found: 235 return SparsePRTEntry::NullEntry; 236 } 237 238 size_t RSHashTableIter::compute_card_ind(CardIdx_t ci) { 239 return (_rsht->entry(_bl_ind)->r_ind() * HeapRegion::CardsPerRegion) + ci; 240 } 241 242 bool RSHashTableIter::has_next(size_t& card_index) { 243 _card_ind++; 244 CardIdx_t ci; 245 if (_card_ind < SparsePRTEntry::cards_num() && 246 ((ci = _rsht->entry(_bl_ind)->card(_card_ind)) != 247 SparsePRTEntry::NullEntry)) { 248 card_index = compute_card_ind(ci); 249 return true; 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 ci = find_first_card_in_list(); 257 if (ci != SparsePRTEntry::NullEntry) { 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 ci = find_first_card_in_list(); 267 if (ci != SparsePRTEntry::NullEntry) { 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 capacity() * (SparsePRTEntry::size() + sizeof(int)); 286 } 287 288 // ---------------------------------------------------------------------- 289 290 SparsePRT* 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 = 300 (SparsePRT*) 301 Atomic::cmpxchg_ptr(sprt, &_head_expanded_list, hd); 302 if (res == hd) return; 303 else hd = res; 304 } 305 } 306 307 308 SparsePRT* SparsePRT::get_from_expanded_list() { 309 SparsePRT* hd = _head_expanded_list; 310 while (hd != NULL) { 311 SparsePRT* next = hd->next_expanded(); 312 SparsePRT* res = 313 (SparsePRT*) 314 Atomic::cmpxchg_ptr(next, &_head_expanded_list, hd); 315 if (res == hd) { 316 hd->set_next_expanded(NULL); 317 return hd; 318 } else { 319 hd = res; 320 } 321 } 322 return NULL; 323 } 324 325 void SparsePRT::reset_for_cleanup_tasks() { 326 _head_expanded_list = NULL; 327 } 328 329 void SparsePRT::do_cleanup_work(SparsePRTCleanupTask* sprt_cleanup_task) { 330 if (should_be_on_expanded_list()) { 331 sprt_cleanup_task->add(this); 332 } 333 } 334 335 void SparsePRT::finish_cleanup_task(SparsePRTCleanupTask* sprt_cleanup_task) { 336 assert(ParGCRareEvent_lock->owned_by_self(), "pre-condition"); 337 SparsePRT* head = sprt_cleanup_task->head(); 338 SparsePRT* tail = sprt_cleanup_task->tail(); 339 if (head != NULL) { 340 assert(tail != NULL, "if head is not NULL, so should tail"); 341 342 tail->set_next_expanded(_head_expanded_list); 343 _head_expanded_list = head; 344 } else { 345 assert(tail == NULL, "if head is NULL, so should tail"); 346 } 347 } 348 349 bool SparsePRT::should_be_on_expanded_list() { 350 if (_expanded) { 351 assert(_cur != _next, "if _expanded is true, cur should be != _next"); 352 } else { 353 assert(_cur == _next, "if _expanded is false, cur should be == _next"); 354 } 355 return expanded(); 356 } 357 358 void SparsePRT::cleanup_all() { 359 // First clean up all expanded tables so they agree on next and cur. 360 SparsePRT* sprt = get_from_expanded_list(); 361 while (sprt != NULL) { 362 sprt->cleanup(); 363 sprt = get_from_expanded_list(); 364 } 365 } 366 367 368 SparsePRT::SparsePRT(HeapRegion* hr) : 369 _hr(hr), _expanded(false), _next_expanded(NULL) 370 { 371 _cur = new RSHashTable(InitialCapacity); 372 _next = _cur; 373 } 374 375 376 SparsePRT::~SparsePRT() { 377 assert(_next != NULL && _cur != NULL, "Inv"); 378 if (_cur != _next) { delete _cur; } 379 delete _next; 380 } 381 382 383 size_t SparsePRT::mem_size() const { 384 // We ignore "_cur" here, because it either = _next, or else it is 385 // on the deleted list. 386 return sizeof(SparsePRT) + _next->mem_size(); 387 } 388 389 bool SparsePRT::add_card(RegionIdx_t region_id, CardIdx_t card_index) { 390 #if SPARSE_PRT_VERBOSE 391 gclog_or_tty->print_cr(" Adding card %d from region %d to region %u sparse.", 392 card_index, region_id, _hr->hrm_index()); 393 #endif 394 if (_next->occupied_entries() * 2 > _next->capacity()) { 395 expand(); 396 } 397 return _next->add_card(region_id, card_index); 398 } 399 400 bool SparsePRT::get_cards(RegionIdx_t region_id, CardIdx_t* cards) { 401 return _next->get_cards(region_id, cards); 402 } 403 404 SparsePRTEntry* SparsePRT::get_entry(RegionIdx_t region_id) { 405 return _next->get_entry(region_id); 406 } 407 408 bool SparsePRT::delete_entry(RegionIdx_t region_id) { 409 return _next->delete_entry(region_id); 410 } 411 412 void SparsePRT::clear() { 413 // If they differ, _next is bigger then cur, so next has no chance of 414 // being the initial size. 415 if (_next != _cur) { 416 delete _next; 417 } 418 419 if (_cur->capacity() != InitialCapacity) { 420 delete _cur; 421 _cur = new RSHashTable(InitialCapacity); 422 } else { 423 _cur->clear(); 424 } 425 _next = _cur; 426 _expanded = false; 427 } 428 429 void SparsePRT::cleanup() { 430 // Make sure that the current and next tables agree. 431 if (_cur != _next) { 432 delete _cur; 433 } 434 _cur = _next; 435 set_expanded(false); 436 } 437 438 void SparsePRT::expand() { 439 RSHashTable* last = _next; 440 _next = new RSHashTable(last->capacity() * 2); 441 442 #if SPARSE_PRT_VERBOSE 443 gclog_or_tty->print_cr(" Expanded sparse table for %u to %d.", 444 _hr->hrm_index(), _next->capacity()); 445 #endif 446 for (size_t i = 0; i < last->capacity(); i++) { 447 SparsePRTEntry* e = last->entry((int)i); 448 if (e->valid_entry()) { 449 #if SPARSE_PRT_VERBOSE 450 gclog_or_tty->print_cr(" During expansion, transferred entry for %d.", 451 e->r_ind()); 452 #endif 453 _next->add_entry(e); 454 } 455 } 456 if (last != _cur) { 457 delete last; 458 } 459 add_to_expanded_list(this); 460 } 461 462 void SparsePRTCleanupTask::add(SparsePRT* sprt) { 463 assert(sprt->should_be_on_expanded_list(), "pre-condition"); 464 465 sprt->set_next_expanded(NULL); 466 if (_tail != NULL) { 467 _tail->set_next_expanded(sprt); 468 } else { 469 _head = sprt; 470 } 471 _tail = sprt; 472 }