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