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/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   FREE_C_HEAP_ARRAY(SparsePRTEntry, _entries);
 108   FREE_C_HEAP_ARRAY(int, _buckets);
 109 }
 110 
 111 void RSHashTable::clear() {
 112   _occupied_entries = 0;
 113   _occupied_cards = 0;
 114   guarantee(_entries != NULL, "INV");
 115   guarantee(_buckets != NULL, "INV");
 116 
 117   guarantee(_capacity <= ((size_t)1 << (sizeof(int)*BitsPerByte-1)) - 1,
 118                 "_capacity too large");
 119 
 120   // This will put -1 == NullEntry in the key field of all entries.
 121   memset((void*)_entries, NullEntry, _num_entries * SparsePRTEntry::size());
 122   memset((void*)_buckets, NullEntry, _capacity * sizeof(int));
 123   _free_list = NullEntry;
 124   _free_region = 0;
 125 }
 126 
 127 bool RSHashTable::add_card(RegionIdx_t region_ind, CardIdx_t card_index) {
 128   SparsePRTEntry* e = entry_for_region_ind_create(region_ind);
 129   assert(e != NULL && e->r_ind() == region_ind,
 130          "Postcondition of call above.");
 131   SparsePRTEntry::AddCardResult res = e->add_card(card_index);
 132   if (res == SparsePRTEntry::added) _occupied_cards++;
 133   assert(e->num_valid_cards() > 0, "Postcondition");
 134   return res != SparsePRTEntry::overflow;
 135 }
 136 
 137 SparsePRTEntry* RSHashTable::get_entry(RegionIdx_t region_ind) const {
 138   int ind = (int) (region_ind & capacity_mask());
 139   int cur_ind = _buckets[ind];
 140   SparsePRTEntry* cur;
 141   while (cur_ind != NullEntry &&
 142          (cur = entry(cur_ind))->r_ind() != region_ind) {
 143     cur_ind = cur->next_index();
 144   }
 145 
 146   if (cur_ind == NullEntry) return NULL;
 147   // Otherwise...
 148   assert(cur->r_ind() == region_ind, "Postcondition of loop + test above.");
 149   assert(cur->num_valid_cards() > 0, "Inv");
 150   return cur;
 151 }
 152 
 153 bool RSHashTable::delete_entry(RegionIdx_t region_ind) {
 154   int ind = (int) (region_ind & capacity_mask());
 155   int* prev_loc = &_buckets[ind];
 156   int cur_ind = *prev_loc;
 157   SparsePRTEntry* cur;
 158   while (cur_ind != NullEntry &&
 159          (cur = entry(cur_ind))->r_ind() != region_ind) {
 160     prev_loc = cur->next_index_addr();
 161     cur_ind = *prev_loc;
 162   }
 163 
 164   if (cur_ind == NullEntry) return false;
 165   // Otherwise, splice out "cur".
 166   *prev_loc = cur->next_index();
 167   _occupied_cards -= cur->num_valid_cards();
 168   free_entry(cur_ind);
 169   _occupied_entries--;
 170   return true;
 171 }
 172 
 173 SparsePRTEntry*
 174 RSHashTable::entry_for_region_ind_create(RegionIdx_t region_ind) {
 175   SparsePRTEntry* res = get_entry(region_ind);
 176   if (res == NULL) {
 177     int new_ind = alloc_entry();
 178     res = entry(new_ind);
 179     res->init(region_ind);
 180     // Insert at front.
 181     int ind = (int) (region_ind & capacity_mask());
 182     res->set_next_index(_buckets[ind]);
 183     _buckets[ind] = new_ind;
 184     _occupied_entries++;
 185   }
 186   return res;
 187 }
 188 
 189 int RSHashTable::alloc_entry() {
 190   int res;
 191   if (_free_list != NullEntry) {
 192     res = _free_list;
 193     _free_list = entry(res)->next_index();
 194     return res;
 195   } else if ((size_t)_free_region < _num_entries) {
 196     res = _free_region;
 197     _free_region++;
 198     return res;
 199   } else {
 200     return NullEntry;
 201   }
 202 }
 203 
 204 void RSHashTable::free_entry(int fi) {
 205   entry(fi)->set_next_index(_free_list);
 206   _free_list = fi;
 207 }
 208 
 209 void RSHashTable::add_entry(SparsePRTEntry* e) {
 210   assert(e->num_valid_cards() > 0, "Precondition.");
 211   SparsePRTEntry* e2 = entry_for_region_ind_create(e->r_ind());
 212   e->copy_cards(e2);
 213   _occupied_cards += e2->num_valid_cards();
 214   assert(e2->num_valid_cards() > 0, "Postcondition.");
 215 }
 216 
 217 CardIdx_t RSHashTableIter::find_first_card_in_list() {
 218   while (_bl_ind != RSHashTable::NullEntry) {
 219     SparsePRTEntry* sparse_entry = _rsht->entry(_bl_ind);
 220     if (sparse_entry->num_valid_cards() > 0) {
 221       return sparse_entry->card(0);
 222     } else {
 223       _bl_ind = sparse_entry->next_index();
 224     }
 225   }
 226   // Otherwise, none found:
 227   return NoCardFound;
 228 }
 229 
 230 size_t RSHashTableIter::compute_card_ind(CardIdx_t ci) {
 231   return (_rsht->entry(_bl_ind)->r_ind() * HeapRegion::CardsPerRegion) + ci;
 232 }
 233 
 234 bool RSHashTableIter::has_next(size_t& card_index) {
 235   _card_ind++;
 236   if (_bl_ind >= 0) {
 237     SparsePRTEntry* e = _rsht->entry(_bl_ind);
 238     if (_card_ind < e->num_valid_cards()) {
 239       CardIdx_t ci = e->card(_card_ind);
 240       card_index = compute_card_ind(ci);
 241       return true;
 242     }
 243   }
 244 
 245   // Otherwise, must find the next valid entry.
 246   _card_ind = 0;
 247 
 248   if (_bl_ind != RSHashTable::NullEntry) {
 249       _bl_ind = _rsht->entry(_bl_ind)->next_index();
 250       CardIdx_t ci = find_first_card_in_list();
 251       if (ci != NoCardFound) {
 252         card_index = compute_card_ind(ci);
 253         return true;
 254       }
 255   }
 256   // If we didn't return above, must go to the next non-null table index.
 257   _tbl_ind++;
 258   while ((size_t)_tbl_ind < _rsht->capacity()) {
 259     _bl_ind = _rsht->_buckets[_tbl_ind];
 260     CardIdx_t ci = find_first_card_in_list();
 261     if (ci != NoCardFound) {
 262       card_index = compute_card_ind(ci);
 263       return true;
 264     }
 265     // Otherwise, try next entry.
 266     _tbl_ind++;
 267   }
 268   // Otherwise, there were no entry.
 269   return false;
 270 }
 271 
 272 bool RSHashTableBucketIter::has_next(SparsePRTEntry*& entry) {
 273   while (_bl_ind == RSHashTable::NullEntry)  {
 274     if (_tbl_ind == (int)_rsht->capacity() - 1) {
 275       return false;
 276     }
 277     _tbl_ind++;
 278     _bl_ind = _rsht->_buckets[_tbl_ind];
 279   }
 280   entry = _rsht->entry(_bl_ind);
 281   _bl_ind = entry->next_index();
 282   return true;
 283 }
 284 
 285 bool RSHashTable::contains_card(RegionIdx_t region_index, CardIdx_t card_index) const {
 286   SparsePRTEntry* e = get_entry(region_index);
 287   return (e != NULL && e->contains_card(card_index));
 288 }
 289 
 290 size_t RSHashTable::mem_size() const {
 291   return sizeof(RSHashTable) +
 292     _num_entries * (SparsePRTEntry::size() + sizeof(int));
 293 }
 294 
 295 // ----------------------------------------------------------------------
 296 
 297 SparsePRT::SparsePRT() :
 298   _table(new RSHashTable(InitialCapacity)) {
 299 }
 300 
 301 
 302 SparsePRT::~SparsePRT() {
 303   delete _table;
 304 }
 305 
 306 
 307 size_t SparsePRT::mem_size() const {
 308   // We ignore "_cur" here, because it either = _next, or else it is
 309   // on the deleted list.
 310   return sizeof(SparsePRT) + _table->mem_size();
 311 }
 312 
 313 bool SparsePRT::add_card(RegionIdx_t region_id, CardIdx_t card_index) {
 314   if (_table->should_expand()) {
 315     expand();
 316   }
 317   return _table->add_card(region_id, card_index);
 318 }
 319 
 320 SparsePRTEntry* SparsePRT::get_entry(RegionIdx_t region_id) {
 321   return _table->get_entry(region_id);
 322 }
 323 
 324 bool SparsePRT::delete_entry(RegionIdx_t region_id) {
 325   return _table->delete_entry(region_id);
 326 }
 327 
 328 void SparsePRT::clear() {
 329   // If the entry table is not at initial capacity, just create a new one.
 330   if (_table->capacity() != InitialCapacity) {
 331     delete _table;
 332     _table = new RSHashTable(InitialCapacity);
 333   } else {
 334     _table->clear();
 335   }
 336 }
 337 
 338 void SparsePRT::expand() {
 339   RSHashTable* last = _table;
 340   _table = new RSHashTable(last->capacity() * 2);
 341   for (size_t i = 0; i < last->num_entries(); i++) {
 342     SparsePRTEntry* e = last->entry((int)i);
 343     if (e->valid_entry()) {
 344       _table->add_entry(e);
 345     }
 346   }
 347   delete last;
 348 }