1 /* 2 * Copyright (c) 2001, 2018, 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 #ifndef SHARE_VM_GC_G1_HEAPREGIONREMSET_HPP 26 #define SHARE_VM_GC_G1_HEAPREGIONREMSET_HPP 27 28 #include "gc/g1/g1CodeCacheRemSet.hpp" 29 #include "gc/g1/g1FromCardCache.hpp" 30 #include "gc/g1/sparsePRT.hpp" 31 32 // Remembered set for a heap region. Represent a set of "cards" that 33 // contain pointers into the owner heap region. Cards are defined somewhat 34 // abstractly, in terms of what the "BlockOffsetTable" in use can parse. 35 36 class G1CollectedHeap; 37 class G1BlockOffsetTable; 38 class G1CardLiveData; 39 class HeapRegion; 40 class HeapRegionRemSetIterator; 41 class PerRegionTable; 42 class SparsePRT; 43 class nmethod; 44 45 // The "_coarse_map" is a bitmap with one bit for each region, where set 46 // bits indicate that the corresponding region may contain some pointer 47 // into the owning region. 48 49 // The "_fine_grain_entries" array is an open hash table of PerRegionTables 50 // (PRTs), indicating regions for which we're keeping the RS as a set of 51 // cards. The strategy is to cap the size of the fine-grain table, 52 // deleting an entry and setting the corresponding coarse-grained bit when 53 // we would overflow this cap. 54 55 // We use a mixture of locking and lock-free techniques here. We allow 56 // threads to locate PRTs without locking, but threads attempting to alter 57 // a bucket list obtain a lock. This means that any failing attempt to 58 // find a PRT must be retried with the lock. It might seem dangerous that 59 // a read can find a PRT that is concurrently deleted. This is all right, 60 // because: 61 // 62 // 1) We only actually free PRT's at safe points (though we reuse them at 63 // other times). 64 // 2) We find PRT's in an attempt to add entries. If a PRT is deleted, 65 // it's _coarse_map bit is set, so the that we were attempting to add 66 // is represented. If a deleted PRT is re-used, a thread adding a bit, 67 // thinking the PRT is for a different region, does no harm. 68 69 class OtherRegionsTable { 70 friend class HeapRegionRemSetIterator; 71 72 G1CollectedHeap* _g1h; 73 Mutex* _m; 74 75 // These are protected by "_m". 76 CHeapBitMap _coarse_map; 77 size_t _n_coarse_entries; 78 static jint _n_coarsenings; 79 80 PerRegionTable** _fine_grain_regions; 81 size_t _n_fine_entries; 82 83 // The fine grain remembered sets are doubly linked together using 84 // their 'next' and 'prev' fields. 85 // This allows fast bulk freeing of all the fine grain remembered 86 // set entries, and fast finding of all of them without iterating 87 // over the _fine_grain_regions table. 88 PerRegionTable * _first_all_fine_prts; 89 PerRegionTable * _last_all_fine_prts; 90 91 // Used to sample a subset of the fine grain PRTs to determine which 92 // PRT to evict and coarsen. 93 size_t _fine_eviction_start; 94 static size_t _fine_eviction_stride; 95 static size_t _fine_eviction_sample_size; 96 97 SparsePRT _sparse_table; 98 99 // These are static after init. 100 static size_t _max_fine_entries; 101 static size_t _mod_max_fine_entries_mask; 102 103 // Requires "prt" to be the first element of the bucket list appropriate 104 // for "hr". If this list contains an entry for "hr", return it, 105 // otherwise return "NULL". 106 PerRegionTable* find_region_table(size_t ind, HeapRegion* hr) const; 107 108 // Find, delete, and return a candidate PerRegionTable, if any exists, 109 // adding the deleted region to the coarse bitmap. Requires the caller 110 // to hold _m, and the fine-grain table to be full. 111 PerRegionTable* delete_region_table(); 112 113 // link/add the given fine grain remembered set into the "all" list 114 void link_to_all(PerRegionTable * prt); 115 // unlink/remove the given fine grain remembered set into the "all" list 116 void unlink_from_all(PerRegionTable * prt); 117 118 bool contains_reference_locked(OopOrNarrowOopStar from) const; 119 120 public: 121 // Create a new remembered set. The given mutex is used to ensure consistency. 122 OtherRegionsTable(Mutex* m); 123 124 // Returns the card index of the given within_region pointer relative to the bottom 125 // of the given heap region. 126 static CardIdx_t card_within_region(OopOrNarrowOopStar within_region, HeapRegion* hr); 127 // Adds the reference from "from to this remembered set. 128 void add_reference(OopOrNarrowOopStar from, uint tid); 129 130 // Returns whether the remembered set contains the given reference. 131 bool contains_reference(OopOrNarrowOopStar from) const; 132 133 // Returns whether this remembered set (and all sub-sets) have an occupancy 134 // that is less or equal than the given occupancy. 135 bool occupancy_less_or_equal_than(size_t limit) const; 136 137 // Returns whether this remembered set (and all sub-sets) does not contain any entry. 138 bool is_empty() const; 139 140 // Returns the number of cards contained in this remembered set. 141 size_t occupied() const; 142 size_t occ_fine() const; 143 size_t occ_coarse() const; 144 size_t occ_sparse() const; 145 146 static jint n_coarsenings() { return _n_coarsenings; } 147 148 // Returns size of the actual remembered set containers in bytes. 149 size_t mem_size() const; 150 // Returns the size of static data in bytes. 151 static size_t static_mem_size(); 152 // Returns the size of the free list content in bytes. 153 static size_t fl_mem_size(); 154 155 // Clear the entire contents of this remembered set. 156 void clear(); 157 }; 158 159 class HeapRegionRemSet : public CHeapObj<mtGC> { 160 friend class VMStructs; 161 friend class HeapRegionRemSetIterator; 162 163 private: 164 G1BlockOffsetTable* _bot; 165 166 // A set of code blobs (nmethods) whose code contains pointers into 167 // the region that owns this RSet. 168 G1CodeRootSet _code_roots; 169 170 Mutex _m; 171 172 OtherRegionsTable _other_regions; 173 174 HeapRegion* _hr; 175 176 void clear_fcc(); 177 178 public: 179 HeapRegionRemSet(G1BlockOffsetTable* bot, HeapRegion* hr); 180 181 static void setup_remset_size(); 182 183 bool cardset_is_empty() const { 184 return _other_regions.is_empty(); 185 } 186 187 bool is_empty() const { 188 return (strong_code_roots_list_length() == 0) && cardset_is_empty(); 189 } 190 191 bool occupancy_less_or_equal_than(size_t occ) const { 192 return (strong_code_roots_list_length() == 0) && _other_regions.occupancy_less_or_equal_than(occ); 193 } 194 195 size_t occupied() { 196 MutexLockerEx x(&_m, Mutex::_no_safepoint_check_flag); 197 return occupied_locked(); 198 } 199 size_t occupied_locked() { 200 return _other_regions.occupied(); 201 } 202 size_t occ_fine() const { 203 return _other_regions.occ_fine(); 204 } 205 size_t occ_coarse() const { 206 return _other_regions.occ_coarse(); 207 } 208 size_t occ_sparse() const { 209 return _other_regions.occ_sparse(); 210 } 211 212 static jint n_coarsenings() { return OtherRegionsTable::n_coarsenings(); } 213 214 private: 215 enum RemSetState { 216 Untracked, 217 Updating, 218 Complete 219 }; 220 221 RemSetState _state; 222 223 static const char* _state_strings[]; 224 static const char* _short_state_strings[]; 225 public: 226 227 const char* get_state_str() const { return _state_strings[_state]; } 228 const char* get_short_state_str() const { return _short_state_strings[_state]; } 229 230 bool is_tracked() { return _state != Untracked; } 231 bool is_updating() { return _state == Updating; } 232 bool is_complete() { return _state == Complete; } 233 234 void set_state_empty() { 235 guarantee(SafepointSynchronize::is_at_safepoint() || !is_tracked(), "Should only set to Untracked during safepoint but is %s.", get_state_str()); 236 if (_state == Untracked) { 237 return; 238 } 239 clear_fcc(); 240 _state = Untracked; 241 } 242 243 void set_state_updating() { 244 guarantee(SafepointSynchronize::is_at_safepoint() && !is_tracked(), "Should only set to Updating from Untracked during safepoint but is %s", get_state_str()); 245 clear_fcc(); 246 _state = Updating; 247 } 248 249 void set_state_complete() { 250 clear_fcc(); 251 _state = Complete; 252 } 253 254 // Used in the sequential case. 255 void add_reference(OopOrNarrowOopStar from) { 256 add_reference(from, 0); 257 } 258 259 // Used in the parallel case. 260 void add_reference(OopOrNarrowOopStar from, uint tid) { 261 RemSetState state = _state; 262 if (state == Untracked) { 263 return; 264 } 265 266 uint cur_idx = _hr->hrm_index(); 267 uintptr_t from_card = uintptr_t(from) >> CardTable::card_shift; 268 269 if (G1FromCardCache::contains_or_replace(tid, cur_idx, from_card)) { 270 assert(contains_reference(from), "We just found " PTR_FORMAT " in the FromCardCache", p2i(from)); 271 return; 272 } 273 274 _other_regions.add_reference(from, tid); 275 } 276 277 // The region is being reclaimed; clear its remset, and any mention of 278 // entries for this region in other remsets. 279 void clear(bool only_cardset = false); 280 void clear_locked(bool only_cardset = false); 281 282 // The actual # of bytes this hr_remset takes up. 283 // Note also includes the strong code root set. 284 size_t mem_size() { 285 MutexLockerEx x(&_m, Mutex::_no_safepoint_check_flag); 286 return _other_regions.mem_size() 287 // This correction is necessary because the above includes the second 288 // part. 289 + (sizeof(HeapRegionRemSet) - sizeof(OtherRegionsTable)) 290 + strong_code_roots_mem_size(); 291 } 292 293 // Returns the memory occupancy of all static data structures associated 294 // with remembered sets. 295 static size_t static_mem_size() { 296 return OtherRegionsTable::static_mem_size() + G1CodeRootSet::static_mem_size(); 297 } 298 299 // Returns the memory occupancy of all free_list data structures associated 300 // with remembered sets. 301 static size_t fl_mem_size() { 302 return OtherRegionsTable::fl_mem_size(); 303 } 304 305 bool contains_reference(OopOrNarrowOopStar from) const { 306 return _other_regions.contains_reference(from); 307 } 308 309 // Routines for managing the list of code roots that point into 310 // the heap region that owns this RSet. 311 void add_strong_code_root(nmethod* nm); 312 void add_strong_code_root_locked(nmethod* nm); 313 void remove_strong_code_root(nmethod* nm); 314 315 // Applies blk->do_code_blob() to each of the entries in 316 // the strong code roots list 317 void strong_code_roots_do(CodeBlobClosure* blk) const; 318 319 void clean_strong_code_roots(HeapRegion* hr); 320 321 // Returns the number of elements in the strong code roots list 322 size_t strong_code_roots_list_length() const { 323 return _code_roots.length(); 324 } 325 326 // Returns true if the strong code roots contains the given 327 // nmethod. 328 bool strong_code_roots_list_contains(nmethod* nm) { 329 return _code_roots.contains(nm); 330 } 331 332 // Returns the amount of memory, in bytes, currently 333 // consumed by the strong code roots. 334 size_t strong_code_roots_mem_size(); 335 336 static void invalidate_from_card_cache(uint start_idx, size_t num_regions) { 337 G1FromCardCache::invalidate(start_idx, num_regions); 338 } 339 340 #ifndef PRODUCT 341 static void print_from_card_cache() { 342 G1FromCardCache::print(); 343 } 344 345 static void test(); 346 #endif 347 }; 348 349 class HeapRegionRemSetIterator : public StackObj { 350 private: 351 // The region RSet over which we are iterating. 352 HeapRegionRemSet* _hrrs; 353 354 // Local caching of HRRS fields. 355 const BitMap* _coarse_map; 356 357 G1BlockOffsetTable* _bot; 358 G1CollectedHeap* _g1h; 359 360 // The number of cards yielded since initialization. 361 size_t _n_yielded_fine; 362 size_t _n_yielded_coarse; 363 size_t _n_yielded_sparse; 364 365 // Indicates what granularity of table that we are currently iterating over. 366 // We start iterating over the sparse table, progress to the fine grain 367 // table, and then finish with the coarse table. 368 enum IterState { 369 Sparse, 370 Fine, 371 Coarse 372 }; 373 IterState _is; 374 375 // For both Coarse and Fine remembered set iteration this contains the 376 // first card number of the heap region we currently iterate over. 377 size_t _cur_region_card_offset; 378 379 // Current region index for the Coarse remembered set iteration. 380 int _coarse_cur_region_index; 381 size_t _coarse_cur_region_cur_card; 382 383 bool coarse_has_next(size_t& card_index); 384 385 // The PRT we are currently iterating over. 386 PerRegionTable* _fine_cur_prt; 387 // Card offset within the current PRT. 388 size_t _cur_card_in_prt; 389 390 // Update internal variables when switching to the given PRT. 391 void switch_to_prt(PerRegionTable* prt); 392 bool fine_has_next(); 393 bool fine_has_next(size_t& card_index); 394 395 // The Sparse remembered set iterator. 396 SparsePRTIter _sparse_iter; 397 398 public: 399 HeapRegionRemSetIterator(HeapRegionRemSet* hrrs); 400 401 // If there remains one or more cards to be yielded, returns true and 402 // sets "card_index" to one of those cards (which is then considered 403 // yielded.) Otherwise, returns false (and leaves "card_index" 404 // undefined.) 405 bool has_next(size_t& card_index); 406 407 size_t n_yielded_fine() { return _n_yielded_fine; } 408 size_t n_yielded_coarse() { return _n_yielded_coarse; } 409 size_t n_yielded_sparse() { return _n_yielded_sparse; } 410 size_t n_yielded() { 411 return n_yielded_fine() + n_yielded_coarse() + n_yielded_sparse(); 412 } 413 }; 414 415 #endif // SHARE_VM_GC_G1_HEAPREGIONREMSET_HPP