1 /* 2 * Copyright (c) 2001, 2010, 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_IMPLEMENTATION_G1_HEAPREGIONREMSET_HPP 26 #define SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONREMSET_HPP 27 28 #include "gc_implementation/g1/sparsePRT.hpp" 29 30 // Remembered set for a heap region. Represent a set of "cards" that 31 // contain pointers into the owner heap region. Cards are defined somewhat 32 // abstractly, in terms of what the "BlockOffsetTable" in use can parse. 33 34 class G1CollectedHeap; 35 class G1BlockOffsetSharedArray; 36 class HeapRegion; 37 class HeapRegionRemSetIterator; 38 class PosParPRT; 39 class SparsePRT; 40 41 42 // The "_coarse_map" is a bitmap with one bit for each region, where set 43 // bits indicate that the corresponding region may contain some pointer 44 // into the owning region. 45 46 // The "_fine_grain_entries" array is an open hash table of PerRegionTables 47 // (PRTs), indicating regions for which we're keeping the RS as a set of 48 // cards. The strategy is to cap the size of the fine-grain table, 49 // deleting an entry and setting the corresponding coarse-grained bit when 50 // we would overflow this cap. 51 52 // We use a mixture of locking and lock-free techniques here. We allow 53 // threads to locate PRTs without locking, but threads attempting to alter 54 // a bucket list obtain a lock. This means that any failing attempt to 55 // find a PRT must be retried with the lock. It might seem dangerous that 56 // a read can find a PRT that is concurrently deleted. This is all right, 57 // because: 58 // 59 // 1) We only actually free PRT's at safe points (though we reuse them at 60 // other times). 61 // 2) We find PRT's in an attempt to add entries. If a PRT is deleted, 62 // it's _coarse_map bit is set, so the that we were attempting to add 63 // is represented. If a deleted PRT is re-used, a thread adding a bit, 64 // thinking the PRT is for a different region, does no harm. 65 66 class OtherRegionsTable VALUE_OBJ_CLASS_SPEC { 67 friend class HeapRegionRemSetIterator; 68 69 G1CollectedHeap* _g1h; 70 Mutex _m; 71 HeapRegion* _hr; 72 73 // These are protected by "_m". 74 BitMap _coarse_map; 75 size_t _n_coarse_entries; 76 static jint _n_coarsenings; 77 78 PosParPRT** _fine_grain_regions; 79 size_t _n_fine_entries; 80 81 #define SAMPLE_FOR_EVICTION 1 82 #if SAMPLE_FOR_EVICTION 83 size_t _fine_eviction_start; 84 static size_t _fine_eviction_stride; 85 static size_t _fine_eviction_sample_size; 86 #endif 87 88 SparsePRT _sparse_table; 89 90 // These are static after init. 91 static size_t _max_fine_entries; 92 static size_t _mod_max_fine_entries_mask; 93 94 // Requires "prt" to be the first element of the bucket list appropriate 95 // for "hr". If this list contains an entry for "hr", return it, 96 // otherwise return "NULL". 97 PosParPRT* find_region_table(size_t ind, HeapRegion* hr) const; 98 99 // Find, delete, and return a candidate PosParPRT, if any exists, 100 // adding the deleted region to the coarse bitmap. Requires the caller 101 // to hold _m, and the fine-grain table to be full. 102 PosParPRT* delete_region_table(); 103 104 // If a PRT for "hr" is in the bucket list indicated by "ind" (which must 105 // be the correct index for "hr"), delete it and return true; else return 106 // false. 107 bool del_single_region_table(size_t ind, HeapRegion* hr); 108 109 static jint _cache_probes; 110 static jint _cache_hits; 111 112 // Indexed by thread X heap region, to minimize thread contention. 113 static int** _from_card_cache; 114 static size_t _from_card_cache_max_regions; 115 static size_t _from_card_cache_mem_size; 116 117 public: 118 OtherRegionsTable(HeapRegion* hr); 119 120 HeapRegion* hr() const { return _hr; } 121 122 // For now. Could "expand" some tables in the future, so that this made 123 // sense. 124 void add_reference(OopOrNarrowOopStar from, int tid); 125 126 void add_reference(OopOrNarrowOopStar from) { 127 return add_reference(from, 0); 128 } 129 130 // Removes any entries shown by the given bitmaps to contain only dead 131 // objects. 132 void scrub(CardTableModRefBS* ctbs, BitMap* region_bm, BitMap* card_bm); 133 134 // Not const because it takes a lock. 135 size_t occupied() const; 136 size_t occ_fine() const; 137 size_t occ_coarse() const; 138 size_t occ_sparse() const; 139 140 static jint n_coarsenings() { return _n_coarsenings; } 141 142 // Returns size in bytes. 143 // Not const because it takes a lock. 144 size_t mem_size() const; 145 static size_t static_mem_size(); 146 static size_t fl_mem_size(); 147 148 bool contains_reference(OopOrNarrowOopStar from) const; 149 bool contains_reference_locked(OopOrNarrowOopStar from) const; 150 151 void clear(); 152 153 // Specifically clear the from_card_cache. 154 void clear_fcc(); 155 156 // "from_hr" is being cleared; remove any entries from it. 157 void clear_incoming_entry(HeapRegion* from_hr); 158 159 // Declare the heap size (in # of regions) to the OtherRegionsTable. 160 // (Uses it to initialize from_card_cache). 161 static void init_from_card_cache(size_t max_regions); 162 163 // Declares that only regions i s.t. 0 <= i < new_n_regs are in use. 164 // Make sure any entries for higher regions are invalid. 165 static void shrink_from_card_cache(size_t new_n_regs); 166 167 static void print_from_card_cache(); 168 169 }; 170 171 172 class HeapRegionRemSet : public CHeapObj { 173 friend class VMStructs; 174 friend class HeapRegionRemSetIterator; 175 176 public: 177 enum Event { 178 Event_EvacStart, Event_EvacEnd, Event_RSUpdateEnd 179 }; 180 181 private: 182 G1BlockOffsetSharedArray* _bosa; 183 G1BlockOffsetSharedArray* bosa() const { return _bosa; } 184 185 OtherRegionsTable _other_regions; 186 187 enum ParIterState { Unclaimed, Claimed, Complete }; 188 volatile ParIterState _iter_state; 189 volatile jlong _iter_claimed; 190 191 // Unused unless G1RecordHRRSOops is true. 192 193 static const int MaxRecorded = 1000000; 194 static OopOrNarrowOopStar* _recorded_oops; 195 static HeapWord** _recorded_cards; 196 static HeapRegion** _recorded_regions; 197 static int _n_recorded; 198 199 static const int MaxRecordedEvents = 1000; 200 static Event* _recorded_events; 201 static int* _recorded_event_index; 202 static int _n_recorded_events; 203 204 static void print_event(outputStream* str, Event evnt); 205 206 public: 207 HeapRegionRemSet(G1BlockOffsetSharedArray* bosa, 208 HeapRegion* hr); 209 210 static int num_par_rem_sets(); 211 static void setup_remset_size(); 212 213 HeapRegion* hr() const { 214 return _other_regions.hr(); 215 } 216 217 size_t occupied() const { 218 return _other_regions.occupied(); 219 } 220 size_t occ_fine() const { 221 return _other_regions.occ_fine(); 222 } 223 size_t occ_coarse() const { 224 return _other_regions.occ_coarse(); 225 } 226 size_t occ_sparse() const { 227 return _other_regions.occ_sparse(); 228 } 229 230 static jint n_coarsenings() { return OtherRegionsTable::n_coarsenings(); } 231 232 /* Used in the sequential case. Returns "true" iff this addition causes 233 the size limit to be reached. */ 234 void add_reference(OopOrNarrowOopStar from) { 235 _other_regions.add_reference(from); 236 } 237 238 /* Used in the parallel case. Returns "true" iff this addition causes 239 the size limit to be reached. */ 240 void add_reference(OopOrNarrowOopStar from, int tid) { 241 _other_regions.add_reference(from, tid); 242 } 243 244 // Removes any entries shown by the given bitmaps to contain only dead 245 // objects. 246 void scrub(CardTableModRefBS* ctbs, BitMap* region_bm, BitMap* card_bm); 247 248 // The region is being reclaimed; clear its remset, and any mention of 249 // entries for this region in other remsets. 250 void clear(); 251 252 // Forget any entries due to pointers from "from_hr". 253 void clear_incoming_entry(HeapRegion* from_hr) { 254 _other_regions.clear_incoming_entry(from_hr); 255 } 256 257 #if 0 258 virtual void cleanup() = 0; 259 #endif 260 261 // Should be called from single-threaded code. 262 void init_for_par_iteration(); 263 // Attempt to claim the region. Returns true iff this call caused an 264 // atomic transition from Unclaimed to Claimed. 265 bool claim_iter(); 266 // Sets the iteration state to "complete". 267 void set_iter_complete(); 268 // Returns "true" iff the region's iteration is complete. 269 bool iter_is_complete(); 270 271 // Support for claiming blocks of cards during iteration 272 void set_iter_claimed(size_t x) { _iter_claimed = (jlong)x; } 273 size_t iter_claimed() const { return (size_t)_iter_claimed; } 274 // Claim the next block of cards 275 size_t iter_claimed_next(size_t step) { 276 size_t current, next; 277 do { 278 current = iter_claimed(); 279 next = current + step; 280 } while (Atomic::cmpxchg((jlong)next, &_iter_claimed, (jlong)current) != (jlong)current); 281 return current; 282 } 283 284 // Initialize the given iterator to iterate over this rem set. 285 void init_iterator(HeapRegionRemSetIterator* iter) const; 286 287 #if 0 288 // Apply the "do_card" method to the start address of every card in the 289 // rem set. Returns false if some application of the closure aborted. 290 virtual bool card_iterate(CardClosure* iter) = 0; 291 #endif 292 293 // The actual # of bytes this hr_remset takes up. 294 size_t mem_size() { 295 return _other_regions.mem_size() 296 // This correction is necessary because the above includes the second 297 // part. 298 + sizeof(this) - sizeof(OtherRegionsTable); 299 } 300 301 // Returns the memory occupancy of all static data structures associated 302 // with remembered sets. 303 static size_t static_mem_size() { 304 return OtherRegionsTable::static_mem_size(); 305 } 306 307 // Returns the memory occupancy of all free_list data structures associated 308 // with remembered sets. 309 static size_t fl_mem_size() { 310 return OtherRegionsTable::fl_mem_size(); 311 } 312 313 bool contains_reference(OopOrNarrowOopStar from) const { 314 return _other_regions.contains_reference(from); 315 } 316 void print() const; 317 318 // Called during a stop-world phase to perform any deferred cleanups. 319 // The second version may be called by parallel threads after then finish 320 // collection work. 321 static void cleanup(); 322 static void par_cleanup(); 323 324 // Declare the heap size (in # of regions) to the HeapRegionRemSet(s). 325 // (Uses it to initialize from_card_cache). 326 static void init_heap(size_t max_regions) { 327 OtherRegionsTable::init_from_card_cache(max_regions); 328 } 329 330 // Declares that only regions i s.t. 0 <= i < new_n_regs are in use. 331 static void shrink_heap(size_t new_n_regs) { 332 OtherRegionsTable::shrink_from_card_cache(new_n_regs); 333 } 334 335 #ifndef PRODUCT 336 static void print_from_card_cache() { 337 OtherRegionsTable::print_from_card_cache(); 338 } 339 #endif 340 341 static void record(HeapRegion* hr, OopOrNarrowOopStar f); 342 static void print_recorded(); 343 static void record_event(Event evnt); 344 345 // Run unit tests. 346 #ifndef PRODUCT 347 static void test(); 348 #endif 349 350 }; 351 352 class HeapRegionRemSetIterator : public CHeapObj { 353 354 // The region over which we're iterating. 355 const HeapRegionRemSet* _hrrs; 356 357 // Local caching of HRRS fields. 358 const BitMap* _coarse_map; 359 PosParPRT** _fine_grain_regions; 360 361 G1BlockOffsetSharedArray* _bosa; 362 G1CollectedHeap* _g1h; 363 364 // The number yielded since initialization. 365 size_t _n_yielded_fine; 366 size_t _n_yielded_coarse; 367 size_t _n_yielded_sparse; 368 369 // If true we're iterating over the coarse table; if false the fine 370 // table. 371 enum IterState { 372 Sparse, 373 Fine, 374 Coarse 375 }; 376 IterState _is; 377 378 // In both kinds of iteration, heap offset of first card of current 379 // region. 380 size_t _cur_region_card_offset; 381 // Card offset within cur region. 382 size_t _cur_region_cur_card; 383 384 // Coarse table iteration fields: 385 386 // Current region index; 387 int _coarse_cur_region_index; 388 int _coarse_cur_region_cur_card; 389 390 bool coarse_has_next(size_t& card_index); 391 392 // Fine table iteration fields: 393 394 // Index of bucket-list we're working on. 395 int _fine_array_index; 396 // Per Region Table we're doing within current bucket list. 397 PosParPRT* _fine_cur_prt; 398 399 /* SparsePRT::*/ SparsePRTIter _sparse_iter; 400 401 void fine_find_next_non_null_prt(); 402 403 bool fine_has_next(); 404 bool fine_has_next(size_t& card_index); 405 406 public: 407 // We require an iterator to be initialized before use, so the 408 // constructor does little. 409 HeapRegionRemSetIterator(); 410 411 void initialize(const HeapRegionRemSet* hrrs); 412 413 // If there remains one or more cards to be yielded, returns true and 414 // sets "card_index" to one of those cards (which is then considered 415 // yielded.) Otherwise, returns false (and leaves "card_index" 416 // undefined.) 417 bool has_next(size_t& card_index); 418 419 size_t n_yielded_fine() { return _n_yielded_fine; } 420 size_t n_yielded_coarse() { return _n_yielded_coarse; } 421 size_t n_yielded_sparse() { return _n_yielded_sparse; } 422 size_t n_yielded() { 423 return n_yielded_fine() + n_yielded_coarse() + n_yielded_sparse(); 424 } 425 }; 426 427 #if 0 428 class CardClosure: public Closure { 429 public: 430 virtual void do_card(HeapWord* card_start) = 0; 431 }; 432 433 #endif 434 435 #endif // SHARE_VM_GC_IMPLEMENTATION_G1_HEAPREGIONREMSET_HPP