annotate src/share/vm/gc_implementation/g1/heapRegionRemSet.hpp @ 2981:2ace1c4ee8da

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