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/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/mutexLocker.hpp"
  34 
  35 // Check that the size of the SparsePRTEntry is evenly divisible by the maximum
  36 // member type to avoid SIGBUS when accessing them.
  37 STATIC_ASSERT(sizeof(SparsePRTEntry) % sizeof(int) == 0);
  38 
  39 void SparsePRTEntry::init(RegionIdx_t region_ind) {
  40   // Check that the card array element type can represent all cards in the region.
  41   // Choose a large SparsePRTEntry::card_elem_t (e.g. CardIdx_t) if required.
  42   assert(((size_t)1 << (sizeof(SparsePRTEntry::card_elem_t) * BitsPerByte)) *
  43          G1CardTable::card_size >= HeapRegionBounds::max_size(), "precondition");
  44   assert(G1RSetSparseRegionEntries > 0, "precondition");
  45   _region_ind = region_ind;
  46   _next_index = RSHashTable::NullEntry;
  47   _next_null = 0;
  48 }
  49 
  50 bool SparsePRTEntry::contains_card(CardIdx_t card_index) const {
  51   for (int i = 0; i < num_valid_cards(); i++) {
  52     if (card(i) == card_index) {
  53       return true;
  54     }
  55   }
  56   return false;
  57 }
  58 
  59 SparsePRTEntry::AddCardResult SparsePRTEntry::add_card(CardIdx_t card_index) {
  60   for (int i = 0; i < num_valid_cards(); i++) {
  61     if (card(i) == card_index) {
  62       return found;
  63     }
  64   }
  65   if (num_valid_cards() < cards_num() - 1) {
  66     _cards[_next_null] = (card_elem_t)card_index;
  67     _next_null++;
  68     return added;
  69    }
  70   // Otherwise, we're full.
  71   return overflow;
  72 }
  73 
  74 void SparsePRTEntry::copy_cards(card_elem_t* cards) const {
  75   memcpy(cards, _cards, cards_num() * sizeof(card_elem_t));
  76 }
  77 
  78 void SparsePRTEntry::copy_cards(SparsePRTEntry* e) const {
  79   copy_cards(e->_cards);
  80   assert(_next_null >= 0, "invariant");
  81   assert(_next_null <= cards_num(), "invariant");
  82   e->_next_null = _next_null;
  83 }
  84 
  85 // ----------------------------------------------------------------------
  86 
  87 float RSHashTable::TableOccupancyFactor = 0.5f;
  88 
  89 RSHashTable::RSHashTable(size_t capacity) :
  90   _num_entries(0),
  91   _capacity(capacity),
  92   _capacity_mask(capacity-1),
  93   _occupied_entries(0),
  94   _occupied_cards(0),
  95   _entries(NULL),
  96   _buckets(NEW_C_HEAP_ARRAY(int, capacity, mtGC)),
  97   _free_region(0),
  98   _free_list(NullEntry)
  99 {
 100   _num_entries = (capacity * TableOccupancyFactor) + 1;
 101   _entries = (SparsePRTEntry*)NEW_C_HEAP_ARRAY(char, _num_entries * SparsePRTEntry::size(), mtGC);
 102   clear();
 103 }
 104 
 105 RSHashTable::~RSHashTable() {
 106   FREE_C_HEAP_ARRAY(SparsePRTEntry, _entries);
 107   FREE_C_HEAP_ARRAY(int, _buckets);
 108 }
 109 
 110 void RSHashTable::clear() {
 111   _occupied_entries = 0;
 112   _occupied_cards = 0;
 113   guarantee(_entries != NULL, "INV");
 114   guarantee(_buckets != NULL, "INV");
 115 
 116   guarantee(_capacity <= ((size_t)1 << (sizeof(int)*BitsPerByte-1)) - 1,
 117                 "_capacity too large");
 118 
 119   // This will put -1 == NullEntry in the key field of all entries.
 120   memset((void*)_entries, NullEntry, _num_entries * SparsePRTEntry::size());
 121   memset((void*)_buckets, NullEntry, _capacity * sizeof(int));
 122   _free_list = NullEntry;
 123   _free_region = 0;
 124 }
 125 
 126 bool RSHashTable::add_card(RegionIdx_t region_ind, CardIdx_t card_index) {
 127   SparsePRTEntry* e = entry_for_region_ind_create(region_ind);
 128   assert(e != NULL && e->r_ind() == region_ind,
 129          "Postcondition of call above.");
 130   SparsePRTEntry::AddCardResult res = e->add_card(card_index);
 131   if (res == SparsePRTEntry::added) _occupied_cards++;
 132   assert(e->num_valid_cards() > 0, "Postcondition");
 133   return res != SparsePRTEntry::overflow;
 134 }
 135 
 136 SparsePRTEntry* RSHashTable::get_entry(RegionIdx_t region_ind) const {
 137   int ind = (int) (region_ind & capacity_mask());
 138   int cur_ind = _buckets[ind];
 139   SparsePRTEntry* cur;
 140   while (cur_ind != NullEntry &&
 141          (cur = entry(cur_ind))->r_ind() != region_ind) {
 142     cur_ind = cur->next_index();
 143   }
 144 
 145   if (cur_ind == NullEntry) return NULL;
 146   // Otherwise...
 147   assert(cur->r_ind() == region_ind, "Postcondition of loop + test above.");
 148   assert(cur->num_valid_cards() > 0, "Inv");
 149   return cur;
 150 }
 151 
 152 bool RSHashTable::delete_entry(RegionIdx_t region_ind) {
 153   int ind = (int) (region_ind & capacity_mask());
 154   int* prev_loc = &_buckets[ind];
 155   int cur_ind = *prev_loc;
 156   SparsePRTEntry* cur;
 157   while (cur_ind != NullEntry &&
 158          (cur = entry(cur_ind))->r_ind() != region_ind) {
 159     prev_loc = cur->next_index_addr();
 160     cur_ind = *prev_loc;
 161   }
 162 
 163   if (cur_ind == NullEntry) return false;
 164   // Otherwise, splice out "cur".
 165   *prev_loc = cur->next_index();
 166   _occupied_cards -= cur->num_valid_cards();
 167   free_entry(cur_ind);
 168   _occupied_entries--;
 169   return true;
 170 }
 171 
 172 SparsePRTEntry*
 173 RSHashTable::entry_for_region_ind_create(RegionIdx_t region_ind) {
 174   SparsePRTEntry* res = get_entry(region_ind);
 175   if (res == NULL) {
 176     int new_ind = alloc_entry();
 177     res = entry(new_ind);
 178     res->init(region_ind);
 179     // Insert at front.
 180     int ind = (int) (region_ind & capacity_mask());
 181     res->set_next_index(_buckets[ind]);
 182     _buckets[ind] = new_ind;
 183     _occupied_entries++;
 184   }
 185   return res;
 186 }
 187 
 188 int RSHashTable::alloc_entry() {
 189   int res;
 190   if (_free_list != NullEntry) {
 191     res = _free_list;
 192     _free_list = entry(res)->next_index();
 193     return res;
 194   } else if ((size_t)_free_region < _num_entries) {
 195     res = _free_region;
 196     _free_region++;
 197     return res;
 198   } else {
 199     return NullEntry;
 200   }
 201 }
 202 
 203 void RSHashTable::free_entry(int fi) {
 204   entry(fi)->set_next_index(_free_list);
 205   _free_list = fi;
 206 }
 207 
 208 void RSHashTable::add_entry(SparsePRTEntry* e) {
 209   assert(e->num_valid_cards() > 0, "Precondition.");
 210   SparsePRTEntry* e2 = entry_for_region_ind_create(e->r_ind());
 211   e->copy_cards(e2);
 212   _occupied_cards += e2->num_valid_cards();
 213   assert(e2->num_valid_cards() > 0, "Postcondition.");
 214 }
 215 
 216 CardIdx_t RSHashTableIter::find_first_card_in_list() {
 217   while (_bl_ind != RSHashTable::NullEntry) {
 218     SparsePRTEntry* sparse_entry = _rsht->entry(_bl_ind);
 219     if (sparse_entry->num_valid_cards() > 0) {
 220       return sparse_entry->card(0);
 221     } else {
 222       _bl_ind = sparse_entry->next_index();
 223     }
 224   }
 225   // Otherwise, none found:
 226   return NoCardFound;
 227 }
 228 
 229 size_t RSHashTableIter::compute_card_ind(CardIdx_t ci) {
 230   return (_rsht->entry(_bl_ind)->r_ind() * HeapRegion::CardsPerRegion) + ci;
 231 }
 232 
 233 bool RSHashTableIter::has_next(size_t& card_index) {
 234   _card_ind++;
 235   if (_bl_ind >= 0) {
 236     SparsePRTEntry* e = _rsht->entry(_bl_ind);
 237     if (_card_ind < e->num_valid_cards()) {
 238       CardIdx_t ci = e->card(_card_ind);
 239       card_index = compute_card_ind(ci);
 240       return true;
 241     }
 242   }
 243 
 244   // Otherwise, must find the next valid entry.
 245   _card_ind = 0;
 246 
 247   if (_bl_ind != RSHashTable::NullEntry) {
 248       _bl_ind = _rsht->entry(_bl_ind)->next_index();
 249       CardIdx_t ci = find_first_card_in_list();
 250       if (ci != NoCardFound) {
 251         card_index = compute_card_ind(ci);
 252         return true;
 253       }
 254   }
 255   // If we didn't return above, must go to the next non-null table index.
 256   _tbl_ind++;
 257   while ((size_t)_tbl_ind < _rsht->capacity()) {
 258     _bl_ind = _rsht->_buckets[_tbl_ind];
 259     CardIdx_t ci = find_first_card_in_list();
 260     if (ci != NoCardFound) {
 261       card_index = compute_card_ind(ci);
 262       return true;
 263     }
 264     // Otherwise, try next entry.
 265     _tbl_ind++;
 266   }
 267   // Otherwise, there were no entry.
 268   return false;
 269 }
 270 
 271 bool RSHashTableBucketIter::has_next(SparsePRTEntry*& entry) {
 272   while (_bl_ind == RSHashTable::NullEntry)  {
 273     if (_tbl_ind == (int)_rsht->capacity() - 1) {
 274       return false;
 275     }
 276     _tbl_ind++;
 277     _bl_ind = _rsht->_buckets[_tbl_ind];
 278   }
 279   entry = _rsht->entry(_bl_ind);
 280   _bl_ind = entry->next_index();
 281   return true;
 282 }
 283 
 284 bool RSHashTable::contains_card(RegionIdx_t region_index, CardIdx_t card_index) const {
 285   SparsePRTEntry* e = get_entry(region_index);
 286   return (e != NULL && e->contains_card(card_index));
 287 }
 288 
 289 size_t RSHashTable::mem_size() const {
 290   return sizeof(RSHashTable) +
 291     _num_entries * (SparsePRTEntry::size() + sizeof(int));
 292 }
 293 
 294 // ----------------------------------------------------------------------
 295 
 296 SparsePRT::SparsePRT() :
 297   _table(new RSHashTable(InitialCapacity)) {
 298 }
 299 
 300 
 301 SparsePRT::~SparsePRT() {
 302   delete _table;
 303 }
 304 
 305 
 306 size_t SparsePRT::mem_size() const {
 307   // We ignore "_cur" here, because it either = _next, or else it is
 308   // on the deleted list.
 309   return sizeof(SparsePRT) + _table->mem_size();
 310 }
 311 
 312 bool SparsePRT::add_card(RegionIdx_t region_id, CardIdx_t card_index) {
 313   if (_table->should_expand()) {
 314     expand();
 315   }
 316   return _table->add_card(region_id, card_index);
 317 }
 318 
 319 SparsePRTEntry* SparsePRT::get_entry(RegionIdx_t region_id) {
 320   return _table->get_entry(region_id);
 321 }
 322 
 323 bool SparsePRT::delete_entry(RegionIdx_t region_id) {
 324   return _table->delete_entry(region_id);
 325 }
 326 
 327 void SparsePRT::clear() {
 328   // If the entry table is not at initial capacity, just create a new one.
 329   if (_table->capacity() != InitialCapacity) {
 330     delete _table;
 331     _table = new RSHashTable(InitialCapacity);
 332   } else {
 333     _table->clear();
 334   }
 335 }
 336 
 337 void SparsePRT::expand() {
 338   RSHashTable* last = _table;
 339   _table = new RSHashTable(last->capacity() * 2);
 340   for (size_t i = 0; i < last->num_entries(); i++) {
 341     SparsePRTEntry* e = last->entry((int)i);
 342     if (e->valid_entry()) {
 343       _table->add_entry(e);
 344     }
 345   }
 346   delete last;
 347 }