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/g1BarrierSet.hpp"
  27 #include "gc/g1/g1BlockOffsetTable.inline.hpp"
  28 #include "gc/g1/g1CardTable.inline.hpp"
  29 #include "gc/g1/g1CardTableEntryClosure.hpp"
  30 #include "gc/g1/g1CollectedHeap.inline.hpp"
  31 #include "gc/g1/g1ConcurrentRefine.hpp"
  32 #include "gc/g1/g1DirtyCardQueue.hpp"
  33 #include "gc/g1/g1FromCardCache.hpp"
  34 #include "gc/g1/g1GCPhaseTimes.hpp"
  35 #include "gc/g1/g1HotCardCache.hpp"
  36 #include "gc/g1/g1OopClosures.inline.hpp"
  37 #include "gc/g1/g1RootClosures.hpp"
  38 #include "gc/g1/g1RemSet.hpp"
  39 #include "gc/g1/g1SharedDirtyCardQueue.hpp"
  40 #include "gc/g1/heapRegion.inline.hpp"
  41 #include "gc/g1/heapRegionManager.inline.hpp"
  42 #include "gc/g1/heapRegionRemSet.inline.hpp"
  43 #include "gc/g1/sparsePRT.hpp"
  44 #include "gc/shared/gcTraceTime.inline.hpp"
  45 #include "gc/shared/ptrQueue.hpp"
  46 #include "gc/shared/suspendibleThreadSet.hpp"
  47 #include "jfr/jfrEvents.hpp"
  48 #include "memory/iterator.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "oops/access.inline.hpp"
  51 #include "oops/oop.inline.hpp"
  52 #include "runtime/os.hpp"
  53 #include "utilities/align.hpp"
  54 #include "utilities/globalDefinitions.hpp"
  55 #include "utilities/stack.inline.hpp"
  56 #include "utilities/ticks.hpp"
  57 
  58 // Collects information about the overall heap root scan progress during an evacuation.
  59 //
  60 // Scanning the remembered sets works by first merging all sources of cards to be
  61 // scanned (log buffers, hcc, remembered sets) into a single data structure to remove
  62 // duplicates and simplify work distribution.
  63 //
  64 // During the following card scanning we not only scan this combined set of cards, but
  65 // also remember that these were completely scanned. The following evacuation passes
  66 // do not scan these cards again, and so need to be preserved across increments.
  67 //
  68 // The representation for all the cards to scan is the card table: cards can have
  69 // one of three states during GC:
  70 // - clean: these cards will not be scanned in this pass
  71 // - dirty: these cards will be scanned in this pass
  72 // - scanned: these cards have already been scanned in a previous pass
  73 //
  74 // After all evacuation is done, we reset the card table to clean.
  75 //
  76 // Work distribution occurs on "chunk" basis, i.e. contiguous ranges of cards. As an
  77 // additional optimization, during card merging we remember which regions and which
  78 // chunks actually contain cards to be scanned. Threads iterate only across these
  79 // regions, and only compete for chunks containing any cards.
  80 //
  81 // Within these chunks, a worker scans the card table on "blocks" of cards, i.e.
  82 // contiguous ranges of dirty cards to be scanned. These blocks are converted to actual
  83 // memory ranges and then passed on to actual scanning.
  84 class G1RemSetScanState : public CHeapObj<mtGC> {
  85   class G1DirtyRegions;
  86 
  87   size_t _max_regions;
  88 
  89   // Has this region that is part of the regions in the collection set been processed yet.
  90   typedef bool G1RemsetIterState;
  91 
  92   G1RemsetIterState volatile* _collection_set_iter_state;
  93 
  94   // Card table iteration claim for each heap region, from 0 (completely unscanned)
  95   // to (>=) HeapRegion::CardsPerRegion (completely scanned).
  96   uint volatile* _card_table_scan_state;
  97 
  98   // Return "optimal" number of chunks per region we want to use for claiming areas
  99   // within a region to claim. Dependent on the region size as proxy for the heap
 100   // size, we limit the total number of chunks to limit memory usage and maintenance
 101   // effort of that table vs. granularity of distributing scanning work.
 102   // Testing showed that 8 for 1M/2M region, 16 for 4M/8M regions, 32 for 16/32M regions
 103   // seems to be such a good trade-off.
 104   static uint get_chunks_per_region(uint log_region_size) {
 105     // Limit the expected input values to current known possible values of the
 106     // (log) region size. Adjust as necessary after testing if changing the permissible
 107     // values for region size.
 108     assert(log_region_size >= 20 && log_region_size <= 25,
 109            "expected value in [20,25], but got %u", log_region_size);
 110     return 1u << (log_region_size / 2 - 7);
 111   }
 112 
 113   uint _scan_chunks_per_region;         // Number of chunks per region.
 114   uint8_t _log_scan_chunks_per_region;  // Log of number of chunks per region.
 115   bool* _region_scan_chunks;
 116   size_t _num_total_scan_chunks;        // Total number of elements in _region_scan_chunks.
 117   uint8_t _scan_chunks_shift;           // For conversion between card index and chunk index.
 118 public:
 119   uint scan_chunk_size() const { return (uint)1 << _scan_chunks_shift; }
 120 
 121   // Returns whether the chunk corresponding to the given region/card in region contain a
 122   // dirty card, i.e. actually needs scanning.
 123   bool chunk_needs_scan(uint const region_idx, uint const card_in_region) const {
 124     size_t const idx = ((size_t)region_idx << _log_scan_chunks_per_region) + (card_in_region >> _scan_chunks_shift);
 125     assert(idx < _num_total_scan_chunks, "Index " SIZE_FORMAT " out of bounds " SIZE_FORMAT,
 126            idx, _num_total_scan_chunks);
 127     return _region_scan_chunks[idx];
 128   }
 129 
 130 private:
 131   // The complete set of regions which card table needs to be cleared at the end of GC because
 132   // we scribbled all over them.
 133   G1DirtyRegions* _all_dirty_regions;
 134   // The set of regions which card table needs to be scanned for new dirty cards
 135   // in the current evacuation pass.
 136   G1DirtyRegions* _next_dirty_regions;
 137 
 138   // Set of (unique) regions that can be added to concurrently.
 139   class G1DirtyRegions : public CHeapObj<mtGC> {
 140     uint* _buffer;
 141     uint _cur_idx;
 142     size_t _max_regions;
 143 
 144     bool* _contains;
 145 
 146   public:
 147     G1DirtyRegions(size_t max_regions) :
 148       _buffer(NEW_C_HEAP_ARRAY(uint, max_regions, mtGC)),
 149       _cur_idx(0),
 150       _max_regions(max_regions),
 151       _contains(NEW_C_HEAP_ARRAY(bool, max_regions, mtGC)) {
 152 
 153       reset();
 154     }
 155 
 156     static size_t chunk_size() { return M; }
 157 
 158     ~G1DirtyRegions() {
 159       FREE_C_HEAP_ARRAY(uint, _buffer);
 160       FREE_C_HEAP_ARRAY(bool, _contains);
 161     }
 162 
 163     void reset() {
 164       _cur_idx = 0;
 165       ::memset(_contains, false, _max_regions * sizeof(bool));
 166     }
 167 
 168     uint size() const { return _cur_idx; }
 169 
 170     uint at(uint idx) const {
 171       assert(idx < _cur_idx, "Index %u beyond valid regions", idx);
 172       return _buffer[idx];
 173     }
 174 
 175     void add_dirty_region(uint region) {
 176       if (_contains[region]) {
 177         return;
 178       }
 179 
 180       bool marked_as_dirty = Atomic::cmpxchg(true, &_contains[region], false) == false;
 181       if (marked_as_dirty) {
 182         uint allocated = Atomic::add(1u, &_cur_idx) - 1;
 183         _buffer[allocated] = region;
 184       }
 185     }
 186 
 187     // Creates the union of this and the other G1DirtyRegions.
 188     void merge(const G1DirtyRegions* other) {
 189       for (uint i = 0; i < other->size(); i++) {
 190         uint region = other->at(i);
 191         if (!_contains[region]) {
 192           _buffer[_cur_idx++] = region;
 193           _contains[region] = true;
 194         }
 195       }
 196     }
 197   };
 198 
 199   // For each region, contains the maximum top() value to be used during this garbage
 200   // collection. Subsumes common checks like filtering out everything but old and
 201   // humongous regions outside the collection set.
 202   // This is valid because we are not interested in scanning stray remembered set
 203   // entries from free or archive regions.
 204   HeapWord** _scan_top;
 205 
 206   class G1ClearCardTableTask : public AbstractGangTask {
 207     G1CollectedHeap* _g1h;
 208     G1DirtyRegions* _regions;
 209     uint _chunk_length;
 210 
 211     uint volatile _cur_dirty_regions;
 212 
 213     G1RemSetScanState* _scan_state;
 214 
 215   public:
 216     G1ClearCardTableTask(G1CollectedHeap* g1h,
 217                          G1DirtyRegions* regions,
 218                          uint chunk_length,
 219                          G1RemSetScanState* scan_state) :
 220       AbstractGangTask("G1 Clear Card Table Task"),
 221       _g1h(g1h),
 222       _regions(regions),
 223       _chunk_length(chunk_length),
 224       _cur_dirty_regions(0),
 225       _scan_state(scan_state) {
 226 
 227       assert(chunk_length > 0, "must be");
 228     }
 229 
 230     static uint chunk_size() { return M; }
 231 
 232     void work(uint worker_id) {
 233       while (_cur_dirty_regions < _regions->size()) {
 234         uint next = Atomic::add(_chunk_length, &_cur_dirty_regions) - _chunk_length;
 235         uint max = MIN2(next + _chunk_length, _regions->size());
 236 
 237         for (uint i = next; i < max; i++) {
 238           HeapRegion* r = _g1h->region_at(_regions->at(i));
 239           if (!r->is_survivor()) {
 240             r->clear_cardtable();
 241           }
 242         }
 243       }
 244     }
 245   };
 246 
 247   // Clear the card table of "dirty" regions.
 248   void clear_card_table(WorkGang* workers) {
 249     uint num_regions = _all_dirty_regions->size();
 250 
 251     if (num_regions == 0) {
 252       return;
 253     }
 254 
 255     uint const num_chunks = (uint)(align_up((size_t)num_regions << HeapRegion::LogCardsPerRegion, G1ClearCardTableTask::chunk_size()) / G1ClearCardTableTask::chunk_size());
 256     uint const num_workers = MIN2(num_chunks, workers->active_workers());
 257     uint const chunk_length = G1ClearCardTableTask::chunk_size() / (uint)HeapRegion::CardsPerRegion;
 258 
 259     // Iterate over the dirty cards region list.
 260     G1ClearCardTableTask cl(G1CollectedHeap::heap(), _all_dirty_regions, chunk_length, this);
 261 
 262     log_debug(gc, ergo)("Running %s using %u workers for %u "
 263                         "units of work for %u regions.",
 264                         cl.name(), num_workers, num_chunks, num_regions);
 265     workers->run_task(&cl, num_workers);
 266 
 267 #ifndef PRODUCT
 268     G1CollectedHeap::heap()->verifier()->verify_card_table_cleanup();
 269 #endif
 270   }
 271 
 272 public:
 273   G1RemSetScanState() :
 274     _max_regions(0),
 275     _collection_set_iter_state(NULL),
 276     _card_table_scan_state(NULL),
 277     _scan_chunks_per_region(get_chunks_per_region(HeapRegion::LogOfHRGrainBytes)),
 278     _log_scan_chunks_per_region(log2_uint(_scan_chunks_per_region)),
 279     _region_scan_chunks(NULL),
 280     _num_total_scan_chunks(0),
 281     _scan_chunks_shift(0),
 282     _all_dirty_regions(NULL),
 283     _next_dirty_regions(NULL),
 284     _scan_top(NULL) {
 285   }
 286 
 287   ~G1RemSetScanState() {
 288     FREE_C_HEAP_ARRAY(G1RemsetIterState, _collection_set_iter_state);
 289     FREE_C_HEAP_ARRAY(uint, _card_table_scan_state);
 290     FREE_C_HEAP_ARRAY(bool, _region_scan_chunks);
 291     FREE_C_HEAP_ARRAY(HeapWord*, _scan_top);
 292   }
 293 
 294   void initialize(size_t max_regions) {
 295     assert(_collection_set_iter_state == NULL, "Must not be initialized twice");
 296     _max_regions = max_regions;
 297     _collection_set_iter_state = NEW_C_HEAP_ARRAY(G1RemsetIterState, max_regions, mtGC);
 298     _card_table_scan_state = NEW_C_HEAP_ARRAY(uint, max_regions, mtGC);
 299     _num_total_scan_chunks = max_regions * _scan_chunks_per_region;
 300     _region_scan_chunks = NEW_C_HEAP_ARRAY(bool, _num_total_scan_chunks, mtGC);
 301 
 302     _scan_chunks_shift = (uint8_t)log2_intptr(HeapRegion::CardsPerRegion / _scan_chunks_per_region);
 303     _scan_top = NEW_C_HEAP_ARRAY(HeapWord*, max_regions, mtGC);
 304   }
 305 
 306   void prepare() {
 307     _all_dirty_regions = new G1DirtyRegions(_max_regions);
 308     _next_dirty_regions = new G1DirtyRegions(_max_regions);
 309   }
 310 
 311   void prepare_for_merge_heap_roots() {
 312     _all_dirty_regions->merge(_next_dirty_regions);
 313 
 314     _next_dirty_regions->reset();
 315     for (size_t i = 0; i < _max_regions; i++) {
 316       _card_table_scan_state[i] = 0;
 317     }
 318 
 319     ::memset(_region_scan_chunks, false, _num_total_scan_chunks * sizeof(*_region_scan_chunks));
 320   }
 321 
 322   // Returns whether the given region contains cards we need to scan. The remembered
 323   // set and other sources may contain cards that
 324   // - are in uncommitted regions
 325   // - are located in the collection set
 326   // - are located in free regions
 327   // as we do not clean up remembered sets before merging heap roots.
 328   bool contains_cards_to_process(uint const region_idx) const {
 329     HeapRegion* hr = G1CollectedHeap::heap()->region_at_or_null(region_idx);
 330     return (hr != NULL && !hr->in_collection_set() && hr->is_old_or_humongous_or_archive());
 331   }
 332 
 333   size_t num_visited_cards() const {
 334     size_t result = 0;
 335     for (uint i = 0; i < _num_total_scan_chunks; i++) {
 336       if (_region_scan_chunks[i]) {
 337         result++;
 338       }
 339     }
 340     return result * (HeapRegion::CardsPerRegion / _scan_chunks_per_region);
 341   }
 342 
 343   size_t num_cards_in_dirty_regions() const {
 344     return _next_dirty_regions->size() * HeapRegion::CardsPerRegion;
 345   }
 346 
 347   void set_chunk_region_dirty(size_t const region_card_idx) {
 348     size_t chunk_idx = region_card_idx >> _scan_chunks_shift;
 349     for (uint i = 0; i < _scan_chunks_per_region; i++) {
 350       _region_scan_chunks[chunk_idx++] = true;
 351     }
 352   }
 353 
 354   void set_chunk_dirty(size_t const card_idx) {
 355     assert((card_idx >> _scan_chunks_shift) < _num_total_scan_chunks,
 356            "Trying to access index " SIZE_FORMAT " out of bounds " SIZE_FORMAT,
 357            card_idx >> _scan_chunks_shift, _num_total_scan_chunks);
 358     size_t const chunk_idx = card_idx >> _scan_chunks_shift;
 359     if (!_region_scan_chunks[chunk_idx]) {
 360       _region_scan_chunks[chunk_idx] = true;
 361     }
 362   }
 363 
 364   void cleanup(WorkGang* workers) {
 365     _all_dirty_regions->merge(_next_dirty_regions);
 366 
 367     clear_card_table(workers);
 368 
 369     delete _all_dirty_regions;
 370     _all_dirty_regions = NULL;
 371 
 372     delete _next_dirty_regions;
 373     _next_dirty_regions = NULL;
 374   }
 375 
 376   void iterate_dirty_regions_from(HeapRegionClosure* cl, uint worker_id) {
 377     uint num_regions = _next_dirty_regions->size();
 378 
 379     if (num_regions == 0) {
 380       return;
 381     }
 382 
 383     G1CollectedHeap* g1h = G1CollectedHeap::heap();
 384 
 385     WorkGang* workers = g1h->workers();
 386     uint const max_workers = workers->active_workers();
 387 
 388     uint const start_pos = num_regions * worker_id / max_workers;
 389     uint cur = start_pos;
 390 
 391     do {
 392       bool result = cl->do_heap_region(g1h->region_at(_next_dirty_regions->at(cur)));
 393       guarantee(!result, "Not allowed to ask for early termination.");
 394       cur++;
 395       if (cur == _next_dirty_regions->size()) {
 396         cur = 0;
 397       }
 398     } while (cur != start_pos);
 399   }
 400 
 401   void reset_region_claim(uint region_idx) {
 402     _collection_set_iter_state[region_idx] = false;
 403   }
 404 
 405   // Attempt to claim the given region in the collection set for iteration. Returns true
 406   // if this call caused the transition from Unclaimed to Claimed.
 407   inline bool claim_collection_set_region(uint region) {
 408     assert(region < _max_regions, "Tried to access invalid region %u", region);
 409     if (_collection_set_iter_state[region]) {
 410       return false;
 411     }
 412     return !Atomic::cmpxchg(true, &_collection_set_iter_state[region], false);
 413   }
 414 
 415   bool has_cards_to_scan(uint region) {
 416     assert(region < _max_regions, "Tried to access invalid region %u", region);
 417     return _card_table_scan_state[region] < HeapRegion::CardsPerRegion;
 418   }
 419 
 420   uint claim_cards_to_scan(uint region, uint increment) {
 421     assert(region < _max_regions, "Tried to access invalid region %u", region);
 422     return Atomic::add(increment, &_card_table_scan_state[region]) - increment;
 423   }
 424 
 425   void add_dirty_region(uint const region) {
 426 #ifdef ASSERT
 427    HeapRegion* hr = G1CollectedHeap::heap()->region_at(region);
 428    assert(!hr->in_collection_set() && hr->is_old_or_humongous_or_archive(),
 429           "Region %u is not suitable for scanning, is %sin collection set or %s",
 430           hr->hrm_index(), hr->in_collection_set() ? "" : "not ", hr->get_short_type_str());
 431 #endif
 432     _next_dirty_regions->add_dirty_region(region);
 433   }
 434 
 435   void add_all_dirty_region(uint region) {
 436 #ifdef ASSERT
 437     HeapRegion* hr = G1CollectedHeap::heap()->region_at(region);
 438     assert(hr->in_collection_set(),
 439            "Only add young regions to all dirty regions directly but %u is %s",
 440            hr->hrm_index(), hr->get_short_type_str());
 441 #endif
 442     _all_dirty_regions->add_dirty_region(region);
 443   }
 444 
 445   void set_scan_top(uint region_idx, HeapWord* value) {
 446     _scan_top[region_idx] = value;
 447   }
 448 
 449   HeapWord* scan_top(uint region_idx) const {
 450     return _scan_top[region_idx];
 451   }
 452 
 453   void clear_scan_top(uint region_idx) {
 454     set_scan_top(region_idx, NULL);
 455   }
 456 };
 457 
 458 G1RemSet::G1RemSet(G1CollectedHeap* g1h,
 459                    G1CardTable* ct,
 460                    G1HotCardCache* hot_card_cache) :
 461   _scan_state(new G1RemSetScanState()),
 462   _prev_period_summary(false),
 463   _g1h(g1h),
 464   _ct(ct),
 465   _g1p(_g1h->policy()),
 466   _hot_card_cache(hot_card_cache) {
 467 }
 468 
 469 G1RemSet::~G1RemSet() {
 470   delete _scan_state;
 471 }
 472 
 473 uint G1RemSet::num_par_rem_sets() {
 474   return G1DirtyCardQueueSet::num_par_ids() + G1ConcurrentRefine::max_num_threads() + MAX2(ConcGCThreads, ParallelGCThreads);
 475 }
 476 
 477 void G1RemSet::initialize(size_t capacity, uint max_regions) {
 478   G1FromCardCache::initialize(num_par_rem_sets(), max_regions);
 479   _scan_state->initialize(max_regions);
 480 }
 481 
 482 // Helper class to scan and detect ranges of cards that need to be scanned on the
 483 // card table.
 484 class G1CardTableScanner : public StackObj {
 485 public:
 486   typedef CardTable::CardValue CardValue;
 487 
 488 private:
 489   CardValue* const _base_addr;
 490 
 491   CardValue* _cur_addr;
 492   CardValue* const _end_addr;
 493 
 494   static const size_t ToScanMask = G1CardTable::g1_card_already_scanned;
 495   static const size_t ExpandedToScanMask = G1CardTable::WordAlreadyScanned;
 496 
 497   bool cur_addr_aligned() const {
 498     return ((uintptr_t)_cur_addr) % sizeof(size_t) == 0;
 499   }
 500 
 501   bool cur_card_is_dirty() const {
 502     CardValue value = *_cur_addr;
 503     return (value & ToScanMask) == 0;
 504   }
 505 
 506   bool cur_word_of_cards_contains_any_dirty_card() const {
 507     assert(cur_addr_aligned(), "Current address should be aligned");
 508     size_t const value = *(size_t*)_cur_addr;
 509     return (~value & ExpandedToScanMask) != 0;
 510   }
 511 
 512   bool cur_word_of_cards_all_dirty_cards() const {
 513     size_t const value = *(size_t*)_cur_addr;
 514     return value == G1CardTable::WordAllDirty;
 515   }
 516 
 517   size_t get_and_advance_pos() {
 518     _cur_addr++;
 519     return pointer_delta(_cur_addr, _base_addr, sizeof(CardValue)) - 1;
 520   }
 521 
 522 public:
 523   G1CardTableScanner(CardValue* start_card, size_t size) :
 524     _base_addr(start_card),
 525     _cur_addr(start_card),
 526     _end_addr(start_card + size) {
 527 
 528     assert(is_aligned(start_card, sizeof(size_t)), "Unaligned start addr " PTR_FORMAT, p2i(start_card));
 529     assert(is_aligned(size, sizeof(size_t)), "Unaligned size " SIZE_FORMAT, size);
 530   }
 531 
 532   size_t find_next_dirty() {
 533     while (!cur_addr_aligned()) {
 534       if (cur_card_is_dirty()) {
 535         return get_and_advance_pos();
 536       }
 537       _cur_addr++;
 538     }
 539 
 540     assert(cur_addr_aligned(), "Current address should be aligned now.");
 541     while (_cur_addr != _end_addr) {
 542       if (cur_word_of_cards_contains_any_dirty_card()) {
 543         for (size_t i = 0; i < sizeof(size_t); i++) {
 544           if (cur_card_is_dirty()) {
 545             return get_and_advance_pos();
 546           }
 547           _cur_addr++;
 548         }
 549         assert(false, "Should not reach here given we detected a dirty card in the word.");
 550       }
 551       _cur_addr += sizeof(size_t);
 552     }
 553     return get_and_advance_pos();
 554   }
 555 
 556   size_t find_next_non_dirty() {
 557     assert(_cur_addr <= _end_addr, "Not allowed to search for marks after area.");
 558 
 559     while (!cur_addr_aligned()) {
 560       if (!cur_card_is_dirty()) {
 561         return get_and_advance_pos();
 562       }
 563       _cur_addr++;
 564     }
 565 
 566     assert(cur_addr_aligned(), "Current address should be aligned now.");
 567     while (_cur_addr != _end_addr) {
 568       if (!cur_word_of_cards_all_dirty_cards()) {
 569         for (size_t i = 0; i < sizeof(size_t); i++) {
 570           if (!cur_card_is_dirty()) {
 571             return get_and_advance_pos();
 572           }
 573           _cur_addr++;
 574         }
 575         assert(false, "Should not reach here given we detected a non-dirty card in the word.");
 576       }
 577       _cur_addr += sizeof(size_t);
 578     }
 579     return get_and_advance_pos();
 580   }
 581 };
 582 
 583 // Helper class to claim dirty chunks within the card table.
 584 class G1CardTableChunkClaimer {
 585   G1RemSetScanState* _scan_state;
 586   uint _region_idx;
 587   uint _cur_claim;
 588 
 589 public:
 590   G1CardTableChunkClaimer(G1RemSetScanState* scan_state, uint region_idx) :
 591     _scan_state(scan_state),
 592     _region_idx(region_idx),
 593     _cur_claim(0) {
 594     guarantee(size() <= HeapRegion::CardsPerRegion, "Should not claim more space than possible.");
 595   }
 596 
 597   bool has_next() {
 598     while (true) {
 599       _cur_claim = _scan_state->claim_cards_to_scan(_region_idx, size());
 600       if (_cur_claim >= HeapRegion::CardsPerRegion) {
 601         return false;
 602       }
 603       if (_scan_state->chunk_needs_scan(_region_idx, _cur_claim)) {
 604         return true;
 605       }
 606     }
 607   }
 608 
 609   uint value() const { return _cur_claim; }
 610   uint size() const { return _scan_state->scan_chunk_size(); }
 611 };
 612 
 613 // Scans a heap region for dirty cards.
 614 class G1ScanHRForRegionClosure : public HeapRegionClosure {
 615   G1CollectedHeap* _g1h;
 616   G1CardTable* _ct;
 617   G1BlockOffsetTable* _bot;
 618 
 619   G1ParScanThreadState* _pss;
 620 
 621   G1RemSetScanState* _scan_state;
 622 
 623   G1GCPhaseTimes::GCParPhases _phase;
 624 
 625   uint   _worker_id;
 626 
 627   size_t _cards_scanned;
 628   size_t _blocks_scanned;
 629   size_t _chunks_claimed;
 630 
 631   Tickspan _rem_set_root_scan_time;
 632   Tickspan _rem_set_trim_partially_time;
 633 
 634   // The address to which this thread already scanned (walked the heap) up to during
 635   // card scanning (exclusive).
 636   HeapWord* _scanned_to;
 637 
 638   HeapWord* scan_memregion(uint region_idx_for_card, MemRegion mr) {
 639     HeapRegion* const card_region = _g1h->region_at(region_idx_for_card);
 640     G1ScanCardClosure card_cl(_g1h, _pss);
 641 
 642     HeapWord* const scanned_to = card_region->oops_on_memregion_seq_iterate_careful<true>(mr, &card_cl);
 643     assert(scanned_to != NULL, "Should be able to scan range");
 644     assert(scanned_to >= mr.end(), "Scanned to " PTR_FORMAT " less than range " PTR_FORMAT, p2i(scanned_to), p2i(mr.end()));
 645 
 646     _pss->trim_queue_partially();
 647     return scanned_to;
 648   }
 649 
 650   void do_claimed_block(uint const region_idx_for_card, size_t const first_card, size_t const num_cards) {
 651     HeapWord* const card_start = _bot->address_for_index_raw(first_card);
 652 #ifdef ASSERT
 653     HeapRegion* hr = _g1h->region_at_or_null(region_idx_for_card);
 654     assert(hr == NULL || hr->is_in_reserved(card_start),
 655              "Card start " PTR_FORMAT " to scan outside of region %u", p2i(card_start), _g1h->region_at(region_idx_for_card)->hrm_index());
 656 #endif
 657     HeapWord* const top = _scan_state->scan_top(region_idx_for_card);
 658     if (card_start >= top) {
 659       return;
 660     }
 661 
 662     HeapWord* scan_end = MIN2(card_start + (num_cards << BOTConstants::LogN_words), top);
 663     if (_scanned_to >= scan_end) {
 664       return;
 665     }
 666     MemRegion mr(MAX2(card_start, _scanned_to), scan_end);
 667     _scanned_to = scan_memregion(region_idx_for_card, mr);
 668 
 669     _cards_scanned += num_cards;
 670   }
 671 
 672   ALWAYSINLINE void do_card_block(uint const region_idx, size_t const first_card, size_t const num_cards) {
 673     _ct->mark_as_scanned(first_card, num_cards);
 674     do_claimed_block(region_idx, first_card, num_cards);
 675     _blocks_scanned++;
 676   }
 677 
 678    void scan_heap_roots(HeapRegion* r) {
 679     EventGCPhaseParallel event;
 680     uint const region_idx = r->hrm_index();
 681 
 682     ResourceMark rm;
 683 
 684     G1CardTableChunkClaimer claim(_scan_state, region_idx);
 685 
 686     // Set the current scan "finger" to NULL for every heap region to scan. Since
 687     // the claim value is monotonically increasing, the check to not scan below this
 688     // will filter out objects spanning chunks within the region too then, as opposed
 689     // to resetting this value for every claim.
 690     _scanned_to = NULL;
 691 
 692     while (claim.has_next()) {
 693       size_t const region_card_base_idx = ((size_t)region_idx << HeapRegion::LogCardsPerRegion) + claim.value();
 694       CardTable::CardValue* const base_addr = _ct->byte_for_index(region_card_base_idx);
 695 
 696       G1CardTableScanner scan(base_addr, claim.size());
 697 
 698       size_t first_scan_idx = scan.find_next_dirty();
 699       while (first_scan_idx != claim.size()) {
 700         assert(*_ct->byte_for_index(region_card_base_idx + first_scan_idx) <= 0x1, "is %d at region %u idx " SIZE_FORMAT, *_ct->byte_for_index(region_card_base_idx + first_scan_idx), region_idx, first_scan_idx);
 701 
 702         size_t const last_scan_idx = scan.find_next_non_dirty();
 703         size_t const len = last_scan_idx - first_scan_idx;
 704 
 705         do_card_block(region_idx, region_card_base_idx + first_scan_idx, len);
 706 
 707         if (last_scan_idx == claim.size()) {
 708           break;
 709         }
 710 
 711         first_scan_idx = scan.find_next_dirty();
 712       }
 713       _chunks_claimed++;
 714     }
 715 
 716     event.commit(GCId::current(), _worker_id, G1GCPhaseTimes::phase_name(G1GCPhaseTimes::ScanHR));
 717   }
 718 
 719 public:
 720   G1ScanHRForRegionClosure(G1RemSetScanState* scan_state,
 721                            G1ParScanThreadState* pss,
 722                            uint worker_id,
 723                            G1GCPhaseTimes::GCParPhases phase) :
 724     _g1h(G1CollectedHeap::heap()),
 725     _ct(_g1h->card_table()),
 726     _bot(_g1h->bot()),
 727     _pss(pss),
 728     _scan_state(scan_state),
 729     _phase(phase),
 730     _worker_id(worker_id),
 731     _cards_scanned(0),
 732     _blocks_scanned(0),
 733     _chunks_claimed(0),
 734     _rem_set_root_scan_time(),
 735     _rem_set_trim_partially_time(),
 736     _scanned_to(NULL) {
 737   }
 738 
 739   bool do_heap_region(HeapRegion* r) {
 740     assert(!r->in_collection_set() && r->is_old_or_humongous_or_archive(),
 741            "Should only be called on old gen non-collection set regions but region %u is not.",
 742            r->hrm_index());
 743     uint const region_idx = r->hrm_index();
 744 
 745     if (_scan_state->has_cards_to_scan(region_idx)) {
 746       G1EvacPhaseWithTrimTimeTracker timer(_pss, _rem_set_root_scan_time, _rem_set_trim_partially_time);
 747       scan_heap_roots(r);
 748     }
 749     return false;
 750   }
 751 
 752   Tickspan rem_set_root_scan_time() const { return _rem_set_root_scan_time; }
 753   Tickspan rem_set_trim_partially_time() const { return _rem_set_trim_partially_time; }
 754 
 755   size_t cards_scanned() const { return _cards_scanned; }
 756   size_t blocks_scanned() const { return _blocks_scanned; }
 757   size_t chunks_claimed() const { return _chunks_claimed; }
 758 };
 759 
 760 void G1RemSet::scan_heap_roots(G1ParScanThreadState* pss,
 761                             uint worker_id,
 762                             G1GCPhaseTimes::GCParPhases scan_phase,
 763                             G1GCPhaseTimes::GCParPhases objcopy_phase) {
 764   G1ScanHRForRegionClosure cl(_scan_state, pss, worker_id, scan_phase);
 765   _scan_state->iterate_dirty_regions_from(&cl, worker_id);
 766 
 767   G1GCPhaseTimes* p = _g1p->phase_times();
 768 
 769   p->record_or_add_time_secs(objcopy_phase, worker_id, cl.rem_set_trim_partially_time().seconds());
 770 
 771   p->record_or_add_time_secs(scan_phase, worker_id, cl.rem_set_root_scan_time().seconds());
 772   p->record_or_add_thread_work_item(scan_phase, worker_id, cl.cards_scanned(), G1GCPhaseTimes::ScanHRScannedCards);
 773   p->record_or_add_thread_work_item(scan_phase, worker_id, cl.blocks_scanned(), G1GCPhaseTimes::ScanHRScannedBlocks);
 774   p->record_or_add_thread_work_item(scan_phase, worker_id, cl.chunks_claimed(), G1GCPhaseTimes::ScanHRClaimedChunks);
 775 }
 776 
 777 // Heap region closure to be applied to all regions in the current collection set
 778 // increment to fix up non-card related roots.
 779 class G1ScanCollectionSetRegionClosure : public HeapRegionClosure {
 780   G1ParScanThreadState* _pss;
 781   G1RemSetScanState* _scan_state;
 782 
 783   G1GCPhaseTimes::GCParPhases _scan_phase;
 784   G1GCPhaseTimes::GCParPhases _code_roots_phase;
 785 
 786   uint _worker_id;
 787 
 788   size_t _opt_refs_scanned;
 789   size_t _opt_refs_memory_used;
 790 
 791   Tickspan _strong_code_root_scan_time;
 792   Tickspan _strong_code_trim_partially_time;
 793 
 794   Tickspan _rem_set_opt_root_scan_time;
 795   Tickspan _rem_set_opt_trim_partially_time;
 796 
 797   void scan_opt_rem_set_roots(HeapRegion* r) {
 798     EventGCPhaseParallel event;
 799 
 800     G1OopStarChunkedList* opt_rem_set_list = _pss->oops_into_optional_region(r);
 801 
 802     G1ScanCardClosure scan_cl(G1CollectedHeap::heap(), _pss);
 803     G1ScanRSForOptionalClosure cl(G1CollectedHeap::heap(), &scan_cl);
 804     _opt_refs_scanned += opt_rem_set_list->oops_do(&cl, _pss->closures()->strong_oops());
 805     _opt_refs_memory_used += opt_rem_set_list->used_memory();
 806 
 807     event.commit(GCId::current(), _worker_id, G1GCPhaseTimes::phase_name(_scan_phase));
 808   }
 809 
 810 public:
 811   G1ScanCollectionSetRegionClosure(G1RemSetScanState* scan_state,
 812                                    G1ParScanThreadState* pss,
 813                                    uint worker_id,
 814                                    G1GCPhaseTimes::GCParPhases scan_phase,
 815                                    G1GCPhaseTimes::GCParPhases code_roots_phase) :
 816     _pss(pss),
 817     _scan_state(scan_state),
 818     _scan_phase(scan_phase),
 819     _code_roots_phase(code_roots_phase),
 820     _worker_id(worker_id),
 821     _opt_refs_scanned(0),
 822     _opt_refs_memory_used(0),
 823     _strong_code_root_scan_time(),
 824     _strong_code_trim_partially_time(),
 825     _rem_set_opt_root_scan_time(),
 826     _rem_set_opt_trim_partially_time() { }
 827 
 828   bool do_heap_region(HeapRegion* r) {
 829     uint const region_idx = r->hrm_index();
 830 
 831     // The individual references for the optional remembered set are per-worker, so we
 832     // always need to scan them.
 833     if (r->has_index_in_opt_cset()) {
 834       G1EvacPhaseWithTrimTimeTracker timer(_pss, _rem_set_opt_root_scan_time, _rem_set_opt_trim_partially_time);
 835       scan_opt_rem_set_roots(r);
 836     }
 837 
 838     if (_scan_state->claim_collection_set_region(region_idx)) {
 839       EventGCPhaseParallel event;
 840 
 841       G1EvacPhaseWithTrimTimeTracker timer(_pss, _strong_code_root_scan_time, _strong_code_trim_partially_time);
 842       // Scan the strong code root list attached to the current region
 843       r->strong_code_roots_do(_pss->closures()->weak_codeblobs());
 844 
 845       event.commit(GCId::current(), _worker_id, G1GCPhaseTimes::phase_name(_code_roots_phase));
 846     }
 847 
 848     return false;
 849   }
 850 
 851   Tickspan strong_code_root_scan_time() const { return _strong_code_root_scan_time;  }
 852   Tickspan strong_code_root_trim_partially_time() const { return _strong_code_trim_partially_time; }
 853 
 854   Tickspan rem_set_opt_root_scan_time() const { return _rem_set_opt_root_scan_time; }
 855   Tickspan rem_set_opt_trim_partially_time() const { return _rem_set_opt_trim_partially_time; }
 856 
 857   size_t opt_refs_scanned() const { return _opt_refs_scanned; }
 858   size_t opt_refs_memory_used() const { return _opt_refs_memory_used; }
 859 };
 860 
 861 void G1RemSet::scan_collection_set_regions(G1ParScanThreadState* pss,
 862                                            uint worker_id,
 863                                            G1GCPhaseTimes::GCParPhases scan_phase,
 864                                            G1GCPhaseTimes::GCParPhases coderoots_phase,
 865                                            G1GCPhaseTimes::GCParPhases objcopy_phase) {
 866   G1ScanCollectionSetRegionClosure cl(_scan_state, pss, worker_id, scan_phase, coderoots_phase);
 867   _g1h->collection_set_iterate_increment_from(&cl, worker_id);
 868 
 869   G1GCPhaseTimes* p = _g1h->phase_times();
 870 
 871   p->record_or_add_time_secs(scan_phase, worker_id, cl.rem_set_opt_root_scan_time().seconds());
 872   p->record_or_add_time_secs(scan_phase, worker_id, cl.rem_set_opt_trim_partially_time().seconds());
 873 
 874   p->record_or_add_time_secs(coderoots_phase, worker_id, cl.strong_code_root_scan_time().seconds());
 875   p->add_time_secs(objcopy_phase, worker_id, cl.strong_code_root_trim_partially_time().seconds());
 876 
 877   // At this time we record some metrics only for the evacuations after the initial one.
 878   if (scan_phase == G1GCPhaseTimes::OptScanHR) {
 879     p->record_or_add_thread_work_item(scan_phase, worker_id, cl.opt_refs_scanned(), G1GCPhaseTimes::ScanHRScannedOptRefs);
 880     p->record_or_add_thread_work_item(scan_phase, worker_id, cl.opt_refs_memory_used(), G1GCPhaseTimes::ScanHRUsedMemory);
 881   }
 882 }
 883 
 884 void G1RemSet::prepare_region_for_scan(HeapRegion* region) {
 885   uint hrm_index = region->hrm_index();
 886 
 887   _scan_state->reset_region_claim(hrm_index);
 888   if (region->in_collection_set()) {
 889     // Young regions had their card table marked as young at their allocation;
 890     // we need to make sure that these marks are cleared at the end of GC, *but*
 891     // they should not be scanned for cards.
 892     // So directly add them to the "all_dirty_regions".
 893     // Same for regions in the (initial) collection set: they may contain cards from
 894     // the log buffers, make sure they are cleaned.
 895     _scan_state->clear_scan_top(hrm_index);
 896     _scan_state->add_all_dirty_region(hrm_index);
 897   } else {
 898     assert(region->is_old_or_humongous_or_archive(), "All other regions should be in the collection set");
 899     _scan_state->set_scan_top(hrm_index, region->top());
 900   }
 901 }
 902 
 903 void G1RemSet::prepare_for_scan_heap_roots() {
 904   G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
 905   dcqs.concatenate_logs();
 906 
 907   _scan_state->prepare();
 908 }
 909 
 910 class G1MergeHeapRootsTask : public AbstractGangTask {
 911 
 912   // Visitor for remembered sets, dropping entries onto the card table.
 913   class G1MergeCardSetClosure : public HeapRegionClosure {
 914     G1RemSetScanState* _scan_state;
 915     G1CardTable* _ct;
 916 
 917     uint _merged_sparse;
 918     uint _merged_fine;
 919     uint _merged_coarse;
 920 
 921     // Returns if the region contains cards we need to scan. If so, remember that
 922     // region in the current set of dirty regions.
 923     bool remember_if_interesting(uint const region_idx) {
 924       if (!_scan_state->contains_cards_to_process(region_idx)) {
 925         return false;
 926       }
 927       _scan_state->add_dirty_region(region_idx);
 928       return true;
 929     }
 930   public:
 931     G1MergeCardSetClosure(G1RemSetScanState* scan_state) :
 932       _scan_state(scan_state),
 933       _ct(G1CollectedHeap::heap()->card_table()),
 934       _merged_sparse(0),
 935       _merged_fine(0),
 936       _merged_coarse(0) { }
 937 
 938     void next_coarse_prt(uint const region_idx) {
 939       if (!remember_if_interesting(region_idx)) {
 940         return;
 941       }
 942 
 943       _merged_coarse++;
 944 
 945       size_t region_base_idx = (size_t)region_idx << HeapRegion::LogCardsPerRegion;
 946       _ct->mark_region_dirty(region_base_idx, HeapRegion::CardsPerRegion);
 947       _scan_state->set_chunk_region_dirty(region_base_idx);
 948     }
 949 
 950     void next_fine_prt(uint const region_idx, BitMap* bm) {
 951       if (!remember_if_interesting(region_idx)) {
 952         return;
 953       }
 954 
 955       _merged_fine++;
 956 
 957       size_t const region_base_idx = (size_t)region_idx << HeapRegion::LogCardsPerRegion;
 958       BitMap::idx_t cur = bm->get_next_one_offset(0);
 959       while (cur != bm->size()) {
 960         _ct->mark_clean_as_dirty(region_base_idx + cur);
 961         _scan_state->set_chunk_dirty(region_base_idx + cur);
 962         cur = bm->get_next_one_offset(cur + 1);
 963       }
 964     }
 965 
 966     void next_sparse_prt(uint const region_idx, SparsePRTEntry::card_elem_t* cards, uint const num_cards) {
 967       if (!remember_if_interesting(region_idx)) {
 968         return;
 969       }
 970 
 971       _merged_sparse++;
 972 
 973       size_t const region_base_idx = (size_t)region_idx << HeapRegion::LogCardsPerRegion;
 974       for (uint i = 0; i < num_cards; i++) {
 975         size_t card_idx = region_base_idx + cards[i];
 976         _ct->mark_clean_as_dirty(card_idx);
 977         _scan_state->set_chunk_dirty(card_idx);
 978       }
 979     }
 980 
 981     virtual bool do_heap_region(HeapRegion* r) {
 982       assert(r->in_collection_set() || r->is_starts_humongous(), "must be");
 983 
 984       HeapRegionRemSet* rem_set = r->rem_set();
 985       if (!rem_set->is_empty()) {
 986         rem_set->iterate_prts(*this);
 987       }
 988 
 989       return false;
 990     }
 991 
 992     size_t merged_sparse() const { return _merged_sparse; }
 993     size_t merged_fine() const { return _merged_fine; }
 994     size_t merged_coarse() const { return _merged_coarse; }
 995   };
 996 
 997   // Visitor for the remembered sets of humongous candidate regions to merge their
 998   // remembered set into the card table.
 999   class G1FlushHumongousCandidateRemSets : public HeapRegionClosure {
1000     G1MergeCardSetClosure _cl;
1001 
1002   public:
1003     G1FlushHumongousCandidateRemSets(G1RemSetScanState* scan_state) : _cl(scan_state) { }
1004 
1005     virtual bool do_heap_region(HeapRegion* r) {
1006       G1CollectedHeap* g1h = G1CollectedHeap::heap();
1007 
1008       if (!r->is_starts_humongous() ||
1009           !g1h->region_attr(r->hrm_index()).is_humongous() ||
1010           r->rem_set()->is_empty()) {
1011         return false;
1012       }
1013 
1014       guarantee(r->rem_set()->occupancy_less_or_equal_than(G1RSetSparseRegionEntries),
1015                 "Found a not-small remembered set here. This is inconsistent with previous assumptions.");
1016 
1017       _cl.do_heap_region(r);
1018 
1019       // We should only clear the card based remembered set here as we will not
1020       // implicitly rebuild anything else during eager reclaim. Note that at the moment
1021       // (and probably never) we do not enter this path if there are other kind of
1022       // remembered sets for this region.
1023       r->rem_set()->clear_locked(true /* only_cardset */);
1024       // Clear_locked() above sets the state to Empty. However we want to continue
1025       // collecting remembered set entries for humongous regions that were not
1026       // reclaimed.
1027       r->rem_set()->set_state_complete();
1028 #ifdef ASSERT
1029       G1HeapRegionAttr region_attr = g1h->region_attr(r->hrm_index());
1030       assert(region_attr.needs_remset_update(), "must be");
1031 #endif
1032       assert(r->rem_set()->is_empty(), "At this point any humongous candidate remembered set must be empty.");
1033 
1034       return false;
1035     }
1036 
1037     size_t merged_sparse() const { return _cl.merged_sparse(); }
1038     size_t merged_fine() const { return _cl.merged_fine(); }
1039     size_t merged_coarse() const { return _cl.merged_coarse(); }
1040   };
1041 
1042   // Visitor for the log buffer entries to merge them into the card table.
1043   class G1MergeLogBufferCardsClosure : public G1CardTableEntryClosure {
1044     G1RemSetScanState* _scan_state;
1045     G1CardTable* _ct;
1046 
1047     size_t _cards_dirty;
1048     size_t _cards_skipped;
1049   public:
1050     G1MergeLogBufferCardsClosure(G1CollectedHeap* g1h, G1RemSetScanState* scan_state) :
1051       _scan_state(scan_state), _ct(g1h->card_table()), _cards_dirty(0), _cards_skipped(0)
1052     {}
1053 
1054     void do_card_ptr(CardValue* card_ptr, uint worker_id) {
1055       // The only time we care about recording cards that
1056       // contain references that point into the collection set
1057       // is during RSet updating within an evacuation pause.
1058       // In this case worker_id should be the id of a GC worker thread.
1059       assert(SafepointSynchronize::is_at_safepoint(), "not during an evacuation pause");
1060 
1061       uint const region_idx = _ct->region_idx_for(card_ptr);
1062 
1063       // The second clause must come after - the log buffers might contain cards to uncommited
1064       // regions.
1065       // This code may count duplicate entries in the log buffers (even if rare) multiple
1066       // times.
1067       if (_scan_state->contains_cards_to_process(region_idx) && (*card_ptr == G1CardTable::dirty_card_val())) {
1068         _scan_state->add_dirty_region(region_idx);
1069         _scan_state->set_chunk_dirty(_ct->index_for_cardvalue(card_ptr));
1070         _cards_dirty++;
1071       } else {
1072         // We may have had dirty cards in the (initial) collection set (or the
1073         // young regions which are always in the initial collection set). We do
1074         // not fix their cards here: we already added these regions to the set of
1075         // regions to clear the card table at the end during the prepare() phase.
1076         _cards_skipped++;
1077       }
1078     }
1079 
1080     size_t cards_dirty() const { return _cards_dirty; }
1081     size_t cards_skipped() const { return _cards_skipped; }
1082   };
1083 
1084   HeapRegionClaimer _hr_claimer;
1085   G1RemSetScanState* _scan_state;
1086   BufferNode::Stack _dirty_card_buffers;
1087   bool _initial_evacuation;
1088 
1089   volatile bool _fast_reclaim_handled;
1090 
1091   void apply_closure_to_dirty_card_buffers(G1MergeLogBufferCardsClosure* cl, uint worker_id) {
1092     G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
1093     size_t buffer_size = dcqs.buffer_size();
1094     while (BufferNode* node = _dirty_card_buffers.pop()) {
1095       cl->apply_to_buffer(node, buffer_size, worker_id);
1096       dcqs.deallocate_buffer(node);
1097     }
1098   }
1099 
1100 public:
1101   G1MergeHeapRootsTask(G1RemSetScanState* scan_state, uint num_workers, bool initial_evacuation) :
1102     AbstractGangTask("G1 Merge Heap Roots"),
1103     _hr_claimer(num_workers),
1104     _scan_state(scan_state),
1105     _dirty_card_buffers(),
1106     _initial_evacuation(initial_evacuation),
1107     _fast_reclaim_handled(false)
1108   {
1109     if (initial_evacuation) {
1110       G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
1111       G1BufferNodeList buffers = dcqs.take_all_completed_buffers();
1112       if (buffers._entry_count != 0) {
1113         _dirty_card_buffers.prepend(*buffers._head, *buffers._tail);
1114       }
1115     }
1116   }
1117 
1118   virtual void work(uint worker_id) {
1119     G1CollectedHeap* g1h = G1CollectedHeap::heap();
1120     G1GCPhaseTimes* p = g1h->phase_times();
1121 
1122     G1GCPhaseTimes::GCParPhases merge_remset_phase = _initial_evacuation ?
1123                                                      G1GCPhaseTimes::MergeRS :
1124                                                      G1GCPhaseTimes::OptMergeRS;
1125 
1126     // We schedule flushing the remembered sets of humongous fast reclaim candidates
1127     // onto the card table first to allow the remaining parallelized tasks hide it.
1128     if (_initial_evacuation &&
1129         p->fast_reclaim_humongous_candidates() > 0 &&
1130         !_fast_reclaim_handled &&
1131         !Atomic::cmpxchg(true, &_fast_reclaim_handled, false)) {
1132 
1133       G1GCParPhaseTimesTracker x(p, G1GCPhaseTimes::MergeER, worker_id);
1134 
1135       G1FlushHumongousCandidateRemSets cl(_scan_state);
1136       g1h->heap_region_iterate(&cl);
1137 
1138       p->record_or_add_thread_work_item(merge_remset_phase, worker_id, cl.merged_sparse(), G1GCPhaseTimes::MergeRSMergedSparse);
1139       p->record_or_add_thread_work_item(merge_remset_phase, worker_id, cl.merged_fine(), G1GCPhaseTimes::MergeRSMergedFine);
1140       p->record_or_add_thread_work_item(merge_remset_phase, worker_id, cl.merged_coarse(), G1GCPhaseTimes::MergeRSMergedCoarse);
1141     }
1142 
1143     // Merge remembered sets of current candidates.
1144     {
1145       G1GCParPhaseTimesTracker x(p, merge_remset_phase, worker_id, _initial_evacuation /* must_record */);
1146       G1MergeCardSetClosure cl(_scan_state);
1147       g1h->collection_set_iterate_increment_from(&cl, &_hr_claimer, worker_id);
1148 
1149       p->record_or_add_thread_work_item(merge_remset_phase, worker_id, cl.merged_sparse(), G1GCPhaseTimes::MergeRSMergedSparse);
1150       p->record_or_add_thread_work_item(merge_remset_phase, worker_id, cl.merged_fine(), G1GCPhaseTimes::MergeRSMergedFine);
1151       p->record_or_add_thread_work_item(merge_remset_phase, worker_id, cl.merged_coarse(), G1GCPhaseTimes::MergeRSMergedCoarse);
1152     }
1153 
1154     // Apply closure to log entries in the HCC.
1155     if (_initial_evacuation && G1HotCardCache::default_use_cache()) {
1156       assert(merge_remset_phase == G1GCPhaseTimes::MergeRS, "Wrong merge phase");
1157       G1GCParPhaseTimesTracker x(p, G1GCPhaseTimes::MergeHCC, worker_id);
1158       G1MergeLogBufferCardsClosure cl(g1h, _scan_state);
1159       g1h->iterate_hcc_closure(&cl, worker_id);
1160 
1161       p->record_thread_work_item(G1GCPhaseTimes::MergeHCC, worker_id, cl.cards_dirty(), G1GCPhaseTimes::MergeHCCDirtyCards);
1162       p->record_thread_work_item(G1GCPhaseTimes::MergeHCC, worker_id, cl.cards_skipped(), G1GCPhaseTimes::MergeHCCSkippedCards);
1163     }
1164 
1165     // Now apply the closure to all remaining log entries.
1166     if (_initial_evacuation) {
1167       assert(merge_remset_phase == G1GCPhaseTimes::MergeRS, "Wrong merge phase");
1168       G1GCParPhaseTimesTracker x(p, G1GCPhaseTimes::MergeLB, worker_id);
1169 
1170       G1MergeLogBufferCardsClosure cl(g1h, _scan_state);
1171       apply_closure_to_dirty_card_buffers(&cl, worker_id);
1172 
1173       p->record_thread_work_item(G1GCPhaseTimes::MergeLB, worker_id, cl.cards_dirty(), G1GCPhaseTimes::MergeLBDirtyCards);
1174       p->record_thread_work_item(G1GCPhaseTimes::MergeLB, worker_id, cl.cards_skipped(), G1GCPhaseTimes::MergeLBSkippedCards);
1175     }
1176   }
1177 };
1178 
1179 void G1RemSet::print_merge_heap_roots_stats() {
1180   size_t num_visited_cards = _scan_state->num_visited_cards();
1181 
1182   size_t total_dirty_region_cards = _scan_state->num_cards_in_dirty_regions();
1183 
1184   G1CollectedHeap* g1h = G1CollectedHeap::heap();
1185   size_t total_old_region_cards =
1186     (g1h->num_regions() - (g1h->num_free_regions() - g1h->collection_set()->cur_length())) * HeapRegion::CardsPerRegion;
1187 
1188   log_debug(gc,remset)("Visited cards " SIZE_FORMAT " Total dirty " SIZE_FORMAT " (%.2lf%%) Total old " SIZE_FORMAT " (%.2lf%%)",
1189                        num_visited_cards,
1190                        total_dirty_region_cards,
1191                        percent_of(num_visited_cards, total_dirty_region_cards),
1192                        total_old_region_cards,
1193                        percent_of(num_visited_cards, total_old_region_cards));
1194 }
1195 
1196 void G1RemSet::merge_heap_roots(bool initial_evacuation) {
1197   G1CollectedHeap* g1h = G1CollectedHeap::heap();
1198 
1199   {
1200     Ticks start = Ticks::now();
1201 
1202     _scan_state->prepare_for_merge_heap_roots();
1203 
1204     Tickspan total = Ticks::now() - start;
1205     if (initial_evacuation) {
1206       g1h->phase_times()->record_prepare_merge_heap_roots_time(total.seconds() * 1000.0);
1207     } else {
1208       g1h->phase_times()->record_or_add_optional_prepare_merge_heap_roots_time(total.seconds() * 1000.0);
1209     }
1210   }
1211 
1212   WorkGang* workers = g1h->workers();
1213   size_t const increment_length = g1h->collection_set()->increment_length();
1214 
1215   uint const num_workers = initial_evacuation ? workers->active_workers() :
1216                                                 MIN2(workers->active_workers(), (uint)increment_length);
1217 
1218   {
1219     G1MergeHeapRootsTask cl(_scan_state, num_workers, initial_evacuation);
1220     log_debug(gc, ergo)("Running %s using %u workers for " SIZE_FORMAT " regions",
1221                         cl.name(), num_workers, increment_length);
1222     workers->run_task(&cl, num_workers);
1223   }
1224 
1225   if (log_is_enabled(Debug, gc, remset)) {
1226     print_merge_heap_roots_stats();
1227   }
1228 }
1229 
1230 void G1RemSet::prepare_for_scan_heap_roots(uint region_idx) {
1231   _scan_state->clear_scan_top(region_idx);
1232 }
1233 
1234 void G1RemSet::cleanup_after_scan_heap_roots() {
1235   G1GCPhaseTimes* phase_times = _g1h->phase_times();
1236 
1237   // Set all cards back to clean.
1238   double start = os::elapsedTime();
1239   _scan_state->cleanup(_g1h->workers());
1240   phase_times->record_clear_ct_time((os::elapsedTime() - start) * 1000.0);
1241 }
1242 
1243 inline void check_card_ptr(CardTable::CardValue* card_ptr, G1CardTable* ct) {
1244 #ifdef ASSERT
1245   G1CollectedHeap* g1h = G1CollectedHeap::heap();
1246   assert(g1h->is_in_exact(ct->addr_for(card_ptr)),
1247          "Card at " PTR_FORMAT " index " SIZE_FORMAT " representing heap at " PTR_FORMAT " (%u) must be in committed heap",
1248          p2i(card_ptr),
1249          ct->index_for(ct->addr_for(card_ptr)),
1250          p2i(ct->addr_for(card_ptr)),
1251          g1h->addr_to_region(ct->addr_for(card_ptr)));
1252 #endif
1253 }
1254 
1255 void G1RemSet::refine_card_concurrently(CardValue* card_ptr,
1256                                         uint worker_id) {
1257   assert(!_g1h->is_gc_active(), "Only call concurrently");
1258 
1259   // Construct the region representing the card.
1260   HeapWord* start = _ct->addr_for(card_ptr);
1261   // And find the region containing it.
1262   HeapRegion* r = _g1h->heap_region_containing_or_null(start);
1263 
1264   // If this is a (stale) card into an uncommitted region, exit.
1265   if (r == NULL) {
1266     return;
1267   }
1268 
1269   check_card_ptr(card_ptr, _ct);
1270 
1271   // If the card is no longer dirty, nothing to do.
1272   if (*card_ptr != G1CardTable::dirty_card_val()) {
1273     return;
1274   }
1275 
1276   // This check is needed for some uncommon cases where we should
1277   // ignore the card.
1278   //
1279   // The region could be young.  Cards for young regions are
1280   // distinctly marked (set to g1_young_gen), so the post-barrier will
1281   // filter them out.  However, that marking is performed
1282   // concurrently.  A write to a young object could occur before the
1283   // card has been marked young, slipping past the filter.
1284   //
1285   // The card could be stale, because the region has been freed since
1286   // the card was recorded. In this case the region type could be
1287   // anything.  If (still) free or (reallocated) young, just ignore
1288   // it.  If (reallocated) old or humongous, the later card trimming
1289   // and additional checks in iteration may detect staleness.  At
1290   // worst, we end up processing a stale card unnecessarily.
1291   //
1292   // In the normal (non-stale) case, the synchronization between the
1293   // enqueueing of the card and processing it here will have ensured
1294   // we see the up-to-date region type here.
1295   if (!r->is_old_or_humongous_or_archive()) {
1296     return;
1297   }
1298 
1299   // The result from the hot card cache insert call is either:
1300   //   * pointer to the current card
1301   //     (implying that the current card is not 'hot'),
1302   //   * null
1303   //     (meaning we had inserted the card ptr into the "hot" card cache,
1304   //     which had some headroom),
1305   //   * a pointer to a "hot" card that was evicted from the "hot" cache.
1306   //
1307 
1308   if (_hot_card_cache->use_cache()) {
1309     assert(!SafepointSynchronize::is_at_safepoint(), "sanity");
1310 
1311     const CardValue* orig_card_ptr = card_ptr;
1312     card_ptr = _hot_card_cache->insert(card_ptr);
1313     if (card_ptr == NULL) {
1314       // There was no eviction. Nothing to do.
1315       return;
1316     } else if (card_ptr != orig_card_ptr) {
1317       // Original card was inserted and an old card was evicted.
1318       start = _ct->addr_for(card_ptr);
1319       r = _g1h->heap_region_containing(start);
1320 
1321       // Check whether the region formerly in the cache should be
1322       // ignored, as discussed earlier for the original card.  The
1323       // region could have been freed while in the cache.
1324       if (!r->is_old_or_humongous_or_archive()) {
1325         return;
1326       }
1327     } // Else we still have the original card.
1328   }
1329 
1330   // Trim the region designated by the card to what's been allocated
1331   // in the region.  The card could be stale, or the card could cover
1332   // (part of) an object at the end of the allocated space and extend
1333   // beyond the end of allocation.
1334 
1335   // Non-humongous objects are only allocated in the old-gen during
1336   // GC, so if region is old then top is stable.  Humongous object
1337   // allocation sets top last; if top has not yet been set, this is
1338   // a stale card and we'll end up with an empty intersection.  If
1339   // this is not a stale card, the synchronization between the
1340   // enqueuing of the card and processing it here will have ensured
1341   // we see the up-to-date top here.
1342   HeapWord* scan_limit = r->top();
1343 
1344   if (scan_limit <= start) {
1345     // If the trimmed region is empty, the card must be stale.
1346     return;
1347   }
1348 
1349   // Okay to clean and process the card now.  There are still some
1350   // stale card cases that may be detected by iteration and dealt with
1351   // as iteration failure.
1352   *const_cast<volatile CardValue*>(card_ptr) = G1CardTable::clean_card_val();
1353 
1354   // This fence serves two purposes.  First, the card must be cleaned
1355   // before processing the contents.  Second, we can't proceed with
1356   // processing until after the read of top, for synchronization with
1357   // possibly concurrent humongous object allocation.  It's okay that
1358   // reading top and reading type were racy wrto each other.  We need
1359   // both set, in any order, to proceed.
1360   OrderAccess::fence();
1361 
1362   // Don't use addr_for(card_ptr + 1) which can ask for
1363   // a card beyond the heap.
1364   HeapWord* end = start + G1CardTable::card_size_in_words;
1365   MemRegion dirty_region(start, MIN2(scan_limit, end));
1366   assert(!dirty_region.is_empty(), "sanity");
1367 
1368   G1ConcurrentRefineOopClosure conc_refine_cl(_g1h, worker_id);
1369   if (r->oops_on_memregion_seq_iterate_careful<false>(dirty_region, &conc_refine_cl) != NULL) {
1370     return;
1371   }
1372 
1373   // If unable to process the card then we encountered an unparsable
1374   // part of the heap (e.g. a partially allocated object, so only
1375   // temporarily a problem) while processing a stale card.  Despite
1376   // the card being stale, we can't simply ignore it, because we've
1377   // already marked the card cleaned, so taken responsibility for
1378   // ensuring the card gets scanned.
1379   //
1380   // However, the card might have gotten re-dirtied and re-enqueued
1381   // while we worked.  (In fact, it's pretty likely.)
1382   if (*card_ptr == G1CardTable::dirty_card_val()) {
1383     return;
1384   }
1385 
1386   // Re-dirty the card and enqueue in the *shared* queue.  Can't use
1387   // the thread-local queue, because that might be the queue that is
1388   // being processed by us; we could be a Java thread conscripted to
1389   // perform refinement on our queue's current buffer.
1390   *card_ptr = G1CardTable::dirty_card_val();
1391   G1BarrierSet::shared_dirty_card_queue().enqueue(card_ptr);
1392 }
1393 
1394 void G1RemSet::print_periodic_summary_info(const char* header, uint period_count) {
1395   if ((G1SummarizeRSetStatsPeriod > 0) && log_is_enabled(Trace, gc, remset) &&
1396       (period_count % G1SummarizeRSetStatsPeriod == 0)) {
1397 
1398     G1RemSetSummary current;
1399     _prev_period_summary.subtract_from(&current);
1400 
1401     Log(gc, remset) log;
1402     log.trace("%s", header);
1403     ResourceMark rm;
1404     LogStream ls(log.trace());
1405     _prev_period_summary.print_on(&ls);
1406 
1407     _prev_period_summary.set(&current);
1408   }
1409 }
1410 
1411 void G1RemSet::print_summary_info() {
1412   Log(gc, remset, exit) log;
1413   if (log.is_trace()) {
1414     log.trace(" Cumulative RS summary");
1415     G1RemSetSummary current;
1416     ResourceMark rm;
1417     LogStream ls(log.trace());
1418     current.print_on(&ls);
1419   }
1420 }
1421 
1422 class G1RebuildRemSetTask: public AbstractGangTask {
1423   // Aggregate the counting data that was constructed concurrently
1424   // with marking.
1425   class G1RebuildRemSetHeapRegionClosure : public HeapRegionClosure {
1426     G1ConcurrentMark* _cm;
1427     G1RebuildRemSetClosure _update_cl;
1428 
1429     // Applies _update_cl to the references of the given object, limiting objArrays
1430     // to the given MemRegion. Returns the amount of words actually scanned.
1431     size_t scan_for_references(oop const obj, MemRegion mr) {
1432       size_t const obj_size = obj->size();
1433       // All non-objArrays and objArrays completely within the mr
1434       // can be scanned without passing the mr.
1435       if (!obj->is_objArray() || mr.contains(MemRegion((HeapWord*)obj, obj_size))) {
1436         obj->oop_iterate(&_update_cl);
1437         return obj_size;
1438       }
1439       // This path is for objArrays crossing the given MemRegion. Only scan the
1440       // area within the MemRegion.
1441       obj->oop_iterate(&_update_cl, mr);
1442       return mr.intersection(MemRegion((HeapWord*)obj, obj_size)).word_size();
1443     }
1444 
1445     // A humongous object is live (with respect to the scanning) either
1446     // a) it is marked on the bitmap as such
1447     // b) its TARS is larger than TAMS, i.e. has been allocated during marking.
1448     bool is_humongous_live(oop const humongous_obj, const G1CMBitMap* const bitmap, HeapWord* tams, HeapWord* tars) const {
1449       return bitmap->is_marked(humongous_obj) || (tars > tams);
1450     }
1451 
1452     // Iterator over the live objects within the given MemRegion.
1453     class LiveObjIterator : public StackObj {
1454       const G1CMBitMap* const _bitmap;
1455       const HeapWord* _tams;
1456       const MemRegion _mr;
1457       HeapWord* _current;
1458 
1459       bool is_below_tams() const {
1460         return _current < _tams;
1461       }
1462 
1463       bool is_live(HeapWord* obj) const {
1464         return !is_below_tams() || _bitmap->is_marked(obj);
1465       }
1466 
1467       HeapWord* bitmap_limit() const {
1468         return MIN2(const_cast<HeapWord*>(_tams), _mr.end());
1469       }
1470 
1471       void move_if_below_tams() {
1472         if (is_below_tams() && has_next()) {
1473           _current = _bitmap->get_next_marked_addr(_current, bitmap_limit());
1474         }
1475       }
1476     public:
1477       LiveObjIterator(const G1CMBitMap* const bitmap, const HeapWord* tams, const MemRegion mr, HeapWord* first_oop_into_mr) :
1478           _bitmap(bitmap),
1479           _tams(tams),
1480           _mr(mr),
1481           _current(first_oop_into_mr) {
1482 
1483         assert(_current <= _mr.start(),
1484                "First oop " PTR_FORMAT " should extend into mr [" PTR_FORMAT ", " PTR_FORMAT ")",
1485                p2i(first_oop_into_mr), p2i(mr.start()), p2i(mr.end()));
1486 
1487         // Step to the next live object within the MemRegion if needed.
1488         if (is_live(_current)) {
1489           // Non-objArrays were scanned by the previous part of that region.
1490           if (_current < mr.start() && !oop(_current)->is_objArray()) {
1491             _current += oop(_current)->size();
1492             // We might have positioned _current on a non-live object. Reposition to the next
1493             // live one if needed.
1494             move_if_below_tams();
1495           }
1496         } else {
1497           // The object at _current can only be dead if below TAMS, so we can use the bitmap.
1498           // immediately.
1499           _current = _bitmap->get_next_marked_addr(_current, bitmap_limit());
1500           assert(_current == _mr.end() || is_live(_current),
1501                  "Current " PTR_FORMAT " should be live (%s) or beyond the end of the MemRegion (" PTR_FORMAT ")",
1502                  p2i(_current), BOOL_TO_STR(is_live(_current)), p2i(_mr.end()));
1503         }
1504       }
1505 
1506       void move_to_next() {
1507         _current += next()->size();
1508         move_if_below_tams();
1509       }
1510 
1511       oop next() const {
1512         oop result = oop(_current);
1513         assert(is_live(_current),
1514                "Object " PTR_FORMAT " must be live TAMS " PTR_FORMAT " below %d mr " PTR_FORMAT " " PTR_FORMAT " outside %d",
1515                p2i(_current), p2i(_tams), _tams > _current, p2i(_mr.start()), p2i(_mr.end()), _mr.contains(result));
1516         return result;
1517       }
1518 
1519       bool has_next() const {
1520         return _current < _mr.end();
1521       }
1522     };
1523 
1524     // Rebuild remembered sets in the part of the region specified by mr and hr.
1525     // Objects between the bottom of the region and the TAMS are checked for liveness
1526     // using the given bitmap. Objects between TAMS and TARS are assumed to be live.
1527     // Returns the number of live words between bottom and TAMS.
1528     size_t rebuild_rem_set_in_region(const G1CMBitMap* const bitmap,
1529                                      HeapWord* const top_at_mark_start,
1530                                      HeapWord* const top_at_rebuild_start,
1531                                      HeapRegion* hr,
1532                                      MemRegion mr) {
1533       size_t marked_words = 0;
1534 
1535       if (hr->is_humongous()) {
1536         oop const humongous_obj = oop(hr->humongous_start_region()->bottom());
1537         if (is_humongous_live(humongous_obj, bitmap, top_at_mark_start, top_at_rebuild_start)) {
1538           // We need to scan both [bottom, TAMS) and [TAMS, top_at_rebuild_start);
1539           // however in case of humongous objects it is sufficient to scan the encompassing
1540           // area (top_at_rebuild_start is always larger or equal to TAMS) as one of the
1541           // two areas will be zero sized. I.e. TAMS is either
1542           // the same as bottom or top(_at_rebuild_start). There is no way TAMS has a different
1543           // value: this would mean that TAMS points somewhere into the object.
1544           assert(hr->top() == top_at_mark_start || hr->top() == top_at_rebuild_start,
1545                  "More than one object in the humongous region?");
1546           humongous_obj->oop_iterate(&_update_cl, mr);
1547           return top_at_mark_start != hr->bottom() ? mr.intersection(MemRegion((HeapWord*)humongous_obj, humongous_obj->size())).byte_size() : 0;
1548         } else {
1549           return 0;
1550         }
1551       }
1552 
1553       for (LiveObjIterator it(bitmap, top_at_mark_start, mr, hr->block_start(mr.start())); it.has_next(); it.move_to_next()) {
1554         oop obj = it.next();
1555         size_t scanned_size = scan_for_references(obj, mr);
1556         if ((HeapWord*)obj < top_at_mark_start) {
1557           marked_words += scanned_size;
1558         }
1559       }
1560 
1561       return marked_words * HeapWordSize;
1562     }
1563 public:
1564   G1RebuildRemSetHeapRegionClosure(G1CollectedHeap* g1h,
1565                                    G1ConcurrentMark* cm,
1566                                    uint worker_id) :
1567     HeapRegionClosure(),
1568     _cm(cm),
1569     _update_cl(g1h, worker_id) { }
1570 
1571     bool do_heap_region(HeapRegion* hr) {
1572       if (_cm->has_aborted()) {
1573         return true;
1574       }
1575 
1576       uint const region_idx = hr->hrm_index();
1577       DEBUG_ONLY(HeapWord* const top_at_rebuild_start_check = _cm->top_at_rebuild_start(region_idx);)
1578       assert(top_at_rebuild_start_check == NULL ||
1579              top_at_rebuild_start_check > hr->bottom(),
1580              "A TARS (" PTR_FORMAT ") == bottom() (" PTR_FORMAT ") indicates the old region %u is empty (%s)",
1581              p2i(top_at_rebuild_start_check), p2i(hr->bottom()),  region_idx, hr->get_type_str());
1582 
1583       size_t total_marked_bytes = 0;
1584       size_t const chunk_size_in_words = G1RebuildRemSetChunkSize / HeapWordSize;
1585 
1586       HeapWord* const top_at_mark_start = hr->prev_top_at_mark_start();
1587 
1588       HeapWord* cur = hr->bottom();
1589       while (cur < hr->end()) {
1590         // After every iteration (yield point) we need to check whether the region's
1591         // TARS changed due to e.g. eager reclaim.
1592         HeapWord* const top_at_rebuild_start = _cm->top_at_rebuild_start(region_idx);
1593         if (top_at_rebuild_start == NULL) {
1594           return false;
1595         }
1596 
1597         MemRegion next_chunk = MemRegion(hr->bottom(), top_at_rebuild_start).intersection(MemRegion(cur, chunk_size_in_words));
1598         if (next_chunk.is_empty()) {
1599           break;
1600         }
1601 
1602         const Ticks start = Ticks::now();
1603         size_t marked_bytes = rebuild_rem_set_in_region(_cm->prev_mark_bitmap(),
1604                                                         top_at_mark_start,
1605                                                         top_at_rebuild_start,
1606                                                         hr,
1607                                                         next_chunk);
1608         Tickspan time = Ticks::now() - start;
1609 
1610         log_trace(gc, remset, tracking)("Rebuilt region %u "
1611                                         "live " SIZE_FORMAT " "
1612                                         "time %.3fms "
1613                                         "marked bytes " SIZE_FORMAT " "
1614                                         "bot " PTR_FORMAT " "
1615                                         "TAMS " PTR_FORMAT " "
1616                                         "TARS " PTR_FORMAT,
1617                                         region_idx,
1618                                         _cm->liveness(region_idx) * HeapWordSize,
1619                                         time.seconds() * 1000.0,
1620                                         marked_bytes,
1621                                         p2i(hr->bottom()),
1622                                         p2i(top_at_mark_start),
1623                                         p2i(top_at_rebuild_start));
1624 
1625         if (marked_bytes > 0) {
1626           total_marked_bytes += marked_bytes;
1627         }
1628         cur += chunk_size_in_words;
1629 
1630         _cm->do_yield_check();
1631         if (_cm->has_aborted()) {
1632           return true;
1633         }
1634       }
1635       // In the final iteration of the loop the region might have been eagerly reclaimed.
1636       // Simply filter out those regions. We can not just use region type because there
1637       // might have already been new allocations into these regions.
1638       DEBUG_ONLY(HeapWord* const top_at_rebuild_start = _cm->top_at_rebuild_start(region_idx);)
1639       assert(top_at_rebuild_start == NULL ||
1640              total_marked_bytes == hr->marked_bytes(),
1641              "Marked bytes " SIZE_FORMAT " for region %u (%s) in [bottom, TAMS) do not match calculated marked bytes " SIZE_FORMAT " "
1642              "(" PTR_FORMAT " " PTR_FORMAT " " PTR_FORMAT ")",
1643              total_marked_bytes, hr->hrm_index(), hr->get_type_str(), hr->marked_bytes(),
1644              p2i(hr->bottom()), p2i(top_at_mark_start), p2i(top_at_rebuild_start));
1645        // Abort state may have changed after the yield check.
1646       return _cm->has_aborted();
1647     }
1648   };
1649 
1650   HeapRegionClaimer _hr_claimer;
1651   G1ConcurrentMark* _cm;
1652 
1653   uint _worker_id_offset;
1654 public:
1655   G1RebuildRemSetTask(G1ConcurrentMark* cm,
1656                       uint n_workers,
1657                       uint worker_id_offset) :
1658       AbstractGangTask("G1 Rebuild Remembered Set"),
1659       _hr_claimer(n_workers),
1660       _cm(cm),
1661       _worker_id_offset(worker_id_offset) {
1662   }
1663 
1664   void work(uint worker_id) {
1665     SuspendibleThreadSetJoiner sts_join;
1666 
1667     G1CollectedHeap* g1h = G1CollectedHeap::heap();
1668 
1669     G1RebuildRemSetHeapRegionClosure cl(g1h, _cm, _worker_id_offset + worker_id);
1670     g1h->heap_region_par_iterate_from_worker_offset(&cl, &_hr_claimer, worker_id);
1671   }
1672 };
1673 
1674 void G1RemSet::rebuild_rem_set(G1ConcurrentMark* cm,
1675                                WorkGang* workers,
1676                                uint worker_id_offset) {
1677   uint num_workers = workers->active_workers();
1678 
1679   G1RebuildRemSetTask cl(cm,
1680                          num_workers,
1681                          worker_id_offset);
1682   workers->run_task(&cl, num_workers);
1683 }