1 /*
2  * Copyright (c) 2001, 2020, 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/g1BlockOffsetTable.inline.hpp"
27 #include "gc/g1/g1CollectedHeap.inline.hpp"
28 #include "gc/g1/g1ConcurrentRefine.hpp"
29 #include "gc/g1/heapRegionManager.inline.hpp"
30 #include "gc/g1/heapRegionRemSet.inline.hpp"
31 #include "gc/g1/sparsePRT.inline.hpp"
32 #include "memory/allocation.hpp"
33 #include "memory/padded.inline.hpp"
34 #include "oops/oop.inline.hpp"
35 #include "runtime/atomic.hpp"
36 #include "runtime/globals_extension.hpp"
37 #include "utilities/bitMap.inline.hpp"
38 #include "utilities/debug.hpp"
39 #include "utilities/formatBuffer.hpp"
40 #include "utilities/globalDefinitions.hpp"
41 #include "utilities/growableArray.hpp"
42 #include "utilities/powerOfTwo.hpp"
43 
44 const char* HeapRegionRemSet::_state_strings[] =  {"Untracked", "Updating", "Complete"};
45 const char* HeapRegionRemSet::_short_state_strings[] =  {"UNTRA", "UPDAT", "CMPLT"};
46 
alloc(HeapRegion * hr)47 PerRegionTable* PerRegionTable::alloc(HeapRegion* hr) {
48   PerRegionTable* fl = _free_list;
49   while (fl != NULL) {
50     PerRegionTable* nxt = fl->next();
51     PerRegionTable* res = Atomic::cmpxchg(&_free_list, fl, nxt);
52     if (res == fl) {
53       fl->init(hr, true);
54       return fl;
55     } else {
56       fl = _free_list;
57     }
58   }
59   assert(fl == NULL, "Loop condition.");
60   return new PerRegionTable(hr);
61 }
62 
63 PerRegionTable* volatile PerRegionTable::_free_list = NULL;
64 
65 size_t OtherRegionsTable::_max_fine_entries = 0;
66 size_t OtherRegionsTable::_mod_max_fine_entries_mask = 0;
67 size_t OtherRegionsTable::_fine_eviction_stride = 0;
68 size_t OtherRegionsTable::_fine_eviction_sample_size = 0;
69 
OtherRegionsTable(Mutex * m)70 OtherRegionsTable::OtherRegionsTable(Mutex* m) :
71   _g1h(G1CollectedHeap::heap()),
72   _m(m),
73   _num_occupied(0),
74   _coarse_map(mtGC),
75   _has_coarse_entries(false),
76   _fine_grain_regions(NULL),
77   _n_fine_entries(0),
78   _first_all_fine_prts(NULL),
79   _last_all_fine_prts(NULL),
80   _fine_eviction_start(0),
81   _sparse_table()
82 {
83   typedef PerRegionTable* PerRegionTablePtr;
84 
85   if (_max_fine_entries == 0) {
86     assert(_mod_max_fine_entries_mask == 0, "Both or none.");
87     size_t max_entries_log = (size_t)log2i(G1RSetRegionEntries);
88     _max_fine_entries = (size_t)1 << max_entries_log;
89     _mod_max_fine_entries_mask = _max_fine_entries - 1;
90 
91     assert(_fine_eviction_sample_size == 0
92            && _fine_eviction_stride == 0, "All init at same time.");
93     _fine_eviction_sample_size = MAX2((size_t)4, max_entries_log);
94     _fine_eviction_stride = _max_fine_entries / _fine_eviction_sample_size;
95   }
96 
97   _fine_grain_regions = NEW_C_HEAP_ARRAY(PerRegionTablePtr, _max_fine_entries, mtGC);
98   for (size_t i = 0; i < _max_fine_entries; i++) {
99     _fine_grain_regions[i] = NULL;
100   }
101 }
102 
link_to_all(PerRegionTable * prt)103 void OtherRegionsTable::link_to_all(PerRegionTable* prt) {
104   // We always append to the beginning of the list for convenience;
105   // the order of entries in this list does not matter.
106   if (_first_all_fine_prts != NULL) {
107     prt->set_next(_first_all_fine_prts);
108   } else {
109     // this is the first element we insert. Adjust the "last" pointer
110     _last_all_fine_prts = prt;
111     assert(prt->next() == NULL, "just checking");
112   }
113   _first_all_fine_prts = prt;
114 
115   assert(_first_all_fine_prts == prt, "just checking");
116   assert((_first_all_fine_prts == NULL && _last_all_fine_prts == NULL) ||
117          (_first_all_fine_prts != NULL && _last_all_fine_prts != NULL),
118          "just checking");
119   assert(_last_all_fine_prts == NULL || _last_all_fine_prts->next() == NULL,
120          "just checking");
121 }
122 
card_within_region(OopOrNarrowOopStar within_region,HeapRegion * hr)123 CardIdx_t OtherRegionsTable::card_within_region(OopOrNarrowOopStar within_region, HeapRegion* hr) {
124   assert(hr->is_in_reserved(within_region),
125          "HeapWord " PTR_FORMAT " is outside of region %u [" PTR_FORMAT ", " PTR_FORMAT ")",
126          p2i(within_region), hr->hrm_index(), p2i(hr->bottom()), p2i(hr->end()));
127   CardIdx_t result = (CardIdx_t)(pointer_delta((HeapWord*)within_region, hr->bottom()) >> (CardTable::card_shift - LogHeapWordSize));
128   return result;
129 }
130 
add_reference(OopOrNarrowOopStar from,uint tid)131 void OtherRegionsTable::add_reference(OopOrNarrowOopStar from, uint tid) {
132   // Note that this may be a continued H region.
133   HeapRegion* from_hr = _g1h->heap_region_containing(from);
134   RegionIdx_t from_hrm_ind = (RegionIdx_t) from_hr->hrm_index();
135 
136   // If the region is already coarsened, return.
137   if (is_region_coarsened(from_hrm_ind)) {
138     assert(contains_reference(from), "We just found " PTR_FORMAT " in the Coarse table", p2i(from));
139     return;
140   }
141 
142   size_t num_added_by_coarsening = 0;
143   // Otherwise find a per-region table to add it to.
144   size_t ind = from_hrm_ind & _mod_max_fine_entries_mask;
145   PerRegionTable* prt = find_region_table(ind, from_hr);
146   if (prt == NULL) {
147     MutexLocker x(_m, Mutex::_no_safepoint_check_flag);
148 
149     // Rechecking if the region is coarsened, while holding the lock.
150     if (is_region_coarsened(from_hrm_ind)) {
151       assert(contains_reference_locked(from), "We just found " PTR_FORMAT " in the Coarse table", p2i(from));
152       return;
153     }
154 
155     // Confirm that it's really not there...
156     prt = find_region_table(ind, from_hr);
157     if (prt == NULL) {
158 
159       CardIdx_t card_index = card_within_region(from, from_hr);
160 
161       SparsePRT::AddCardResult result = _sparse_table.add_card(from_hrm_ind, card_index);
162       if (result != SparsePRT::overflow) {
163         if (result == SparsePRT::added) {
164           Atomic::inc(&_num_occupied, memory_order_relaxed);
165         }
166         assert(contains_reference_locked(from), "We just added " PTR_FORMAT " to the Sparse table", p2i(from));
167         return;
168       }
169 
170       // Sparse PRT returned overflow (sparse table is full)
171 
172       if (_n_fine_entries == _max_fine_entries) {
173         prt = delete_region_table(num_added_by_coarsening);
174         // There is no need to clear the links to the 'all' list here:
175         // prt will be reused immediately, i.e. remain in the 'all' list.
176         prt->init(from_hr, false /* clear_links_to_all_list */);
177       } else {
178         prt = PerRegionTable::alloc(from_hr);
179         link_to_all(prt);
180       }
181 
182       PerRegionTable* first_prt = _fine_grain_regions[ind];
183       prt->set_collision_list_next(first_prt);
184       // The assignment into _fine_grain_regions allows the prt to
185       // start being used concurrently. In addition to
186       // collision_list_next which must be visible (else concurrent
187       // parsing of the list, if any, may fail to see other entries),
188       // the content of the prt must be visible (else for instance
189       // some mark bits may not yet seem cleared or a 'later' update
190       // performed by a concurrent thread could be undone when the
191       // zeroing becomes visible). This requires store ordering.
192       Atomic::release_store(&_fine_grain_regions[ind], prt);
193       _n_fine_entries++;
194 
195       // Transfer from sparse to fine-grain. The cards from the sparse table
196       // were already added to the total in _num_occupied.
197       SparsePRTEntry *sprt_entry = _sparse_table.get_entry(from_hrm_ind);
198       assert(sprt_entry != NULL, "There should have been an entry");
199       for (int i = 0; i < sprt_entry->num_valid_cards(); i++) {
200         CardIdx_t c = sprt_entry->card(i);
201         prt->add_card(c);
202       }
203       // Now we can delete the sparse entry.
204       bool res = _sparse_table.delete_entry(from_hrm_ind);
205       assert(res, "It should have been there.");
206     }
207     assert(prt != NULL && prt->hr() == from_hr, "consequence");
208   }
209   // Note that we can't assert "prt->hr() == from_hr", because of the
210   // possibility of concurrent reuse.  But see head comment of
211   // OtherRegionsTable for why this is OK.
212   assert(prt != NULL, "Inv");
213 
214   if (prt->add_reference(from)) {
215     num_added_by_coarsening++;
216   }
217   Atomic::add(&_num_occupied, num_added_by_coarsening, memory_order_relaxed);
218   assert(contains_reference(from), "We just added " PTR_FORMAT " to the PRT (%d)", p2i(from), prt->contains_reference(from));
219 }
220 
221 PerRegionTable*
find_region_table(size_t ind,HeapRegion * hr) const222 OtherRegionsTable::find_region_table(size_t ind, HeapRegion* hr) const {
223   assert(ind < _max_fine_entries, "Preconditions.");
224   PerRegionTable* prt = _fine_grain_regions[ind];
225   while (prt != NULL && prt->hr() != hr) {
226     prt = prt->collision_list_next();
227   }
228   // Loop postcondition is the method postcondition.
229   return prt;
230 }
231 
232 jint OtherRegionsTable::_n_coarsenings = 0;
233 
delete_region_table(size_t & added_by_deleted)234 PerRegionTable* OtherRegionsTable::delete_region_table(size_t& added_by_deleted) {
235   assert(_m->owned_by_self(), "Precondition");
236   assert(_n_fine_entries == _max_fine_entries, "Precondition");
237   PerRegionTable* max = NULL;
238   jint max_occ = 0;
239   PerRegionTable** max_prev = NULL;
240 
241   size_t i = _fine_eviction_start;
242   for (size_t k = 0; k < _fine_eviction_sample_size; k++) {
243     size_t ii = i;
244     // Make sure we get a non-NULL sample.
245     while (_fine_grain_regions[ii] == NULL) {
246       ii++;
247       if (ii == _max_fine_entries) ii = 0;
248       guarantee(ii != i, "We must find one.");
249     }
250     PerRegionTable** prev = &_fine_grain_regions[ii];
251     PerRegionTable* cur = *prev;
252     while (cur != NULL) {
253       jint cur_occ = cur->occupied();
254       if (max == NULL || cur_occ > max_occ) {
255         max = cur;
256         max_prev = prev;
257         max_occ = cur_occ;
258       }
259       prev = cur->collision_list_next_addr();
260       cur = cur->collision_list_next();
261     }
262     i = i + _fine_eviction_stride;
263     if (i >= _n_fine_entries) i = i - _n_fine_entries;
264   }
265 
266   _fine_eviction_start++;
267 
268   if (_fine_eviction_start >= _n_fine_entries) {
269     _fine_eviction_start -= _n_fine_entries;
270   }
271 
272   guarantee(max != NULL, "Since _n_fine_entries > 0");
273   guarantee(max_prev != NULL, "Since max != NULL.");
274 
275   // Ensure the corresponding coarse bit is set.
276   size_t max_hrm_index = (size_t) max->hr()->hrm_index();
277   if (Atomic::load(&_has_coarse_entries)) {
278     _coarse_map.at_put(max_hrm_index, true);
279   } else {
280     // This will lazily initialize an uninitialized bitmap
281     _coarse_map.reinitialize(G1CollectedHeap::heap()->max_reserved_regions());
282     assert(!_coarse_map.at(max_hrm_index), "No coarse entries");
283     _coarse_map.at_put(max_hrm_index, true);
284     // Release store guarantees that the bitmap has initialized before any
285     // concurrent reader will ever see _has_coarse_entries is true
286     // (when read with load_acquire)
287     Atomic::release_store(&_has_coarse_entries, true);
288   }
289 
290   added_by_deleted = HeapRegion::CardsPerRegion - max_occ;
291   // Unsplice.
292   *max_prev = max->collision_list_next();
293   Atomic::inc(&_n_coarsenings);
294   _n_fine_entries--;
295   return max;
296 }
297 
occupancy_less_or_equal_than(size_t limit) const298 bool OtherRegionsTable::occupancy_less_or_equal_than(size_t limit) const {
299   return occupied() <= limit;
300 }
301 
is_empty() const302 bool OtherRegionsTable::is_empty() const {
303   return occupied() == 0;
304 }
305 
occupied() const306 size_t OtherRegionsTable::occupied() const {
307   return _num_occupied;
308 }
309 
mem_size() const310 size_t OtherRegionsTable::mem_size() const {
311   size_t sum = 0;
312   // all PRTs are of the same size so it is sufficient to query only one of them.
313   if (_first_all_fine_prts != NULL) {
314     assert(_last_all_fine_prts != NULL &&
315       _first_all_fine_prts->mem_size() == _last_all_fine_prts->mem_size(), "check that mem_size() is constant");
316     sum += _first_all_fine_prts->mem_size() * _n_fine_entries;
317   }
318   sum += (sizeof(PerRegionTable*) * _max_fine_entries);
319   sum += (_coarse_map.size_in_words() * HeapWordSize);
320   sum += (_sparse_table.mem_size());
321   sum += sizeof(OtherRegionsTable) - sizeof(_sparse_table); // Avoid double counting above.
322   return sum;
323 }
324 
static_mem_size()325 size_t OtherRegionsTable::static_mem_size() {
326   return G1FromCardCache::static_mem_size();
327 }
328 
fl_mem_size()329 size_t OtherRegionsTable::fl_mem_size() {
330   return PerRegionTable::fl_mem_size();
331 }
332 
clear()333 void OtherRegionsTable::clear() {
334   // if there are no entries, skip this step
335   if (_first_all_fine_prts != NULL) {
336     guarantee(_first_all_fine_prts != NULL && _last_all_fine_prts != NULL, "just checking");
337     PerRegionTable::bulk_free(_first_all_fine_prts, _last_all_fine_prts);
338     memset(_fine_grain_regions, 0, _max_fine_entries * sizeof(_fine_grain_regions[0]));
339   } else {
340     guarantee(_first_all_fine_prts == NULL && _last_all_fine_prts == NULL, "just checking");
341   }
342 
343   _first_all_fine_prts = _last_all_fine_prts = NULL;
344   _sparse_table.clear();
345   if (Atomic::load(&_has_coarse_entries)) {
346     _coarse_map.clear();
347   }
348   _n_fine_entries = 0;
349   Atomic::store(&_has_coarse_entries, false);
350 
351   _num_occupied = 0;
352 }
353 
contains_reference(OopOrNarrowOopStar from) const354 bool OtherRegionsTable::contains_reference(OopOrNarrowOopStar from) const {
355   // Cast away const in this case.
356   MutexLocker x((Mutex*)_m, Mutex::_no_safepoint_check_flag);
357   return contains_reference_locked(from);
358 }
359 
contains_reference_locked(OopOrNarrowOopStar from) const360 bool OtherRegionsTable::contains_reference_locked(OopOrNarrowOopStar from) const {
361   HeapRegion* hr = _g1h->heap_region_containing(from);
362   RegionIdx_t hr_ind = (RegionIdx_t) hr->hrm_index();
363   // Is this region in the coarse map?
364   if (is_region_coarsened(hr_ind)) return true;
365 
366   PerRegionTable* prt = find_region_table(hr_ind & _mod_max_fine_entries_mask,
367                                           hr);
368   if (prt != NULL) {
369     return prt->contains_reference(from);
370   } else {
371     CardIdx_t card_index = card_within_region(from, hr);
372     return _sparse_table.contains_card(hr_ind, card_index);
373   }
374 }
375 
376 // A load_acquire on _has_coarse_entries - coupled with the release_store in
377 // delete_region_table - guarantees we don't access _coarse_map before
378 // it's been properly initialized.
is_region_coarsened(RegionIdx_t from_hrm_ind) const379 bool OtherRegionsTable::is_region_coarsened(RegionIdx_t from_hrm_ind) const {
380   return Atomic::load_acquire(&_has_coarse_entries) && _coarse_map.at(from_hrm_ind);
381 }
382 
HeapRegionRemSet(G1BlockOffsetTable * bot,HeapRegion * hr)383 HeapRegionRemSet::HeapRegionRemSet(G1BlockOffsetTable* bot,
384                                    HeapRegion* hr)
385   : _bot(bot),
386     _code_roots(),
387     _m(Mutex::leaf, FormatBuffer<128>("HeapRegionRemSet lock #%u", hr->hrm_index()), true, Mutex::_safepoint_check_never),
388     _other_regions(&_m),
389     _hr(hr),
390     _state(Untracked)
391 {
392 }
393 
clear_fcc()394 void HeapRegionRemSet::clear_fcc() {
395   G1FromCardCache::clear(_hr->hrm_index());
396 }
397 
setup_remset_size()398 void HeapRegionRemSet::setup_remset_size() {
399   const int LOG_M = 20;
400   guarantee(HeapRegion::LogOfHRGrainBytes >= LOG_M, "Code assumes the region size >= 1M, but is " SIZE_FORMAT "B", HeapRegion::GrainBytes);
401 
402   int region_size_log_mb = HeapRegion::LogOfHRGrainBytes - LOG_M;
403   if (FLAG_IS_DEFAULT(G1RSetSparseRegionEntries)) {
404     G1RSetSparseRegionEntries = G1RSetSparseRegionEntriesBase * ((size_t)1 << (region_size_log_mb + 1));
405   }
406   if (FLAG_IS_DEFAULT(G1RSetRegionEntries)) {
407     G1RSetRegionEntries = G1RSetRegionEntriesBase * (region_size_log_mb + 1);
408   }
409   guarantee(G1RSetSparseRegionEntries > 0 && G1RSetRegionEntries > 0 , "Sanity");
410 }
411 
clear(bool only_cardset)412 void HeapRegionRemSet::clear(bool only_cardset) {
413   MutexLocker x(&_m, Mutex::_no_safepoint_check_flag);
414   clear_locked(only_cardset);
415 }
416 
clear_locked(bool only_cardset)417 void HeapRegionRemSet::clear_locked(bool only_cardset) {
418   if (!only_cardset) {
419     _code_roots.clear();
420   }
421   clear_fcc();
422   _other_regions.clear();
423   set_state_empty();
424   assert(occupied() == 0, "Should be clear.");
425 }
426 
427 // Code roots support
428 //
429 // The code root set is protected by two separate locking schemes
430 // When at safepoint the per-hrrs lock must be held during modifications
431 // except when doing a full gc.
432 // When not at safepoint the CodeCache_lock must be held during modifications.
433 // When concurrent readers access the contains() function
434 // (during the evacuation phase) no removals are allowed.
435 
add_strong_code_root(nmethod * nm)436 void HeapRegionRemSet::add_strong_code_root(nmethod* nm) {
437   assert(nm != NULL, "sanity");
438   assert((!CodeCache_lock->owned_by_self() || SafepointSynchronize::is_at_safepoint()),
439           "should call add_strong_code_root_locked instead. CodeCache_lock->owned_by_self(): %s, is_at_safepoint(): %s",
440           BOOL_TO_STR(CodeCache_lock->owned_by_self()), BOOL_TO_STR(SafepointSynchronize::is_at_safepoint()));
441   // Optimistic unlocked contains-check
442   if (!_code_roots.contains(nm)) {
443     MutexLocker ml(&_m, Mutex::_no_safepoint_check_flag);
444     add_strong_code_root_locked(nm);
445   }
446 }
447 
add_strong_code_root_locked(nmethod * nm)448 void HeapRegionRemSet::add_strong_code_root_locked(nmethod* nm) {
449   assert(nm != NULL, "sanity");
450   assert((CodeCache_lock->owned_by_self() ||
451          (SafepointSynchronize::is_at_safepoint() &&
452           (_m.owned_by_self() || Thread::current()->is_VM_thread()))),
453           "not safely locked. CodeCache_lock->owned_by_self(): %s, is_at_safepoint(): %s, _m.owned_by_self(): %s, Thread::current()->is_VM_thread(): %s",
454           BOOL_TO_STR(CodeCache_lock->owned_by_self()), BOOL_TO_STR(SafepointSynchronize::is_at_safepoint()),
455           BOOL_TO_STR(_m.owned_by_self()), BOOL_TO_STR(Thread::current()->is_VM_thread()));
456   _code_roots.add(nm);
457 }
458 
remove_strong_code_root(nmethod * nm)459 void HeapRegionRemSet::remove_strong_code_root(nmethod* nm) {
460   assert(nm != NULL, "sanity");
461   assert_locked_or_safepoint(CodeCache_lock);
462 
463   MutexLocker ml(CodeCache_lock->owned_by_self() ? NULL : &_m, Mutex::_no_safepoint_check_flag);
464   _code_roots.remove(nm);
465 
466   // Check that there were no duplicates
467   guarantee(!_code_roots.contains(nm), "duplicate entry found");
468 }
469 
strong_code_roots_do(CodeBlobClosure * blk) const470 void HeapRegionRemSet::strong_code_roots_do(CodeBlobClosure* blk) const {
471   _code_roots.nmethods_do(blk);
472 }
473 
clean_strong_code_roots(HeapRegion * hr)474 void HeapRegionRemSet::clean_strong_code_roots(HeapRegion* hr) {
475   _code_roots.clean(hr);
476 }
477 
strong_code_roots_mem_size()478 size_t HeapRegionRemSet::strong_code_roots_mem_size() {
479   return _code_roots.mem_size();
480 }
481