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