src/share/vm/gc_implementation/g1/heapRegionRemSet.cpp

Print this page
rev 6923 : imported patch 8054819-rename-heapregionseq


  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_implementation/g1/concurrentG1Refine.hpp"
  27 #include "gc_implementation/g1/g1BlockOffsetTable.inline.hpp"
  28 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  29 #include "gc_implementation/g1/heapRegionRemSet.hpp"
  30 #include "gc_implementation/g1/heapRegionSeq.inline.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/padded.inline.hpp"
  33 #include "memory/space.inline.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "runtime/atomic.inline.hpp"
  36 #include "utilities/bitMap.inline.hpp"
  37 #include "utilities/globalDefinitions.hpp"
  38 #include "utilities/growableArray.hpp"
  39 
  40 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  41 
  42 class PerRegionTable: public CHeapObj<mtGC> {
  43   friend class OtherRegionsTable;
  44   friend class HeapRegionRemSetIterator;
  45 
  46   HeapRegion*     _hr;
  47   BitMap          _bm;
  48   jint            _occupied;
  49 
  50   // next pointer for free/allocated 'all' list


 403 void FromCardCache::clear(uint region_idx) {
 404   uint num_par_remsets = HeapRegionRemSet::num_par_rem_sets();
 405   for (uint i = 0; i < num_par_remsets; i++) {
 406     set(i, region_idx, InvalidCard);
 407   }
 408 }
 409 
 410 void OtherRegionsTable::initialize(uint max_regions) {
 411   FromCardCache::initialize(HeapRegionRemSet::num_par_rem_sets(), max_regions);
 412 }
 413 
 414 void OtherRegionsTable::invalidate(uint start_idx, size_t num_regions) {
 415   FromCardCache::invalidate(start_idx, num_regions);
 416 }
 417 
 418 void OtherRegionsTable::print_from_card_cache() {
 419   FromCardCache::print();
 420 }
 421 
 422 void OtherRegionsTable::add_reference(OopOrNarrowOopStar from, int tid) {
 423   uint cur_hrs_ind = hr()->hrs_index();
 424 
 425   if (G1TraceHeapRegionRememberedSet) {
 426     gclog_or_tty->print_cr("ORT::add_reference_work(" PTR_FORMAT "->" PTR_FORMAT ").",
 427                                                     from,
 428                                                     UseCompressedOops
 429                                                     ? (void *)oopDesc::load_decode_heap_oop((narrowOop*)from)
 430                                                     : (void *)oopDesc::load_decode_heap_oop((oop*)from));
 431   }
 432 
 433   int from_card = (int)(uintptr_t(from) >> CardTableModRefBS::card_shift);
 434 
 435   if (G1TraceHeapRegionRememberedSet) {
 436     gclog_or_tty->print_cr("Table for [" PTR_FORMAT "...): card %d (cache = %d)",
 437                   hr()->bottom(), from_card,
 438                   FromCardCache::at((uint)tid, cur_hrs_ind));
 439   }
 440 
 441   if (FromCardCache::contains_or_replace((uint)tid, cur_hrs_ind, from_card)) {
 442     if (G1TraceHeapRegionRememberedSet) {
 443       gclog_or_tty->print_cr("  from-card cache hit.");
 444     }
 445     assert(contains_reference(from), "We just added it!");
 446     return;
 447   }
 448 
 449   // Note that this may be a continued H region.
 450   HeapRegion* from_hr = _g1h->heap_region_containing_raw(from);
 451   RegionIdx_t from_hrs_ind = (RegionIdx_t) from_hr->hrs_index();
 452 
 453   // If the region is already coarsened, return.
 454   if (_coarse_map.at(from_hrs_ind)) {
 455     if (G1TraceHeapRegionRememberedSet) {
 456       gclog_or_tty->print_cr("  coarse map hit.");
 457     }
 458     assert(contains_reference(from), "We just added it!");
 459     return;
 460   }
 461 
 462   // Otherwise find a per-region table to add it to.
 463   size_t ind = from_hrs_ind & _mod_max_fine_entries_mask;
 464   PerRegionTable* prt = find_region_table(ind, from_hr);
 465   if (prt == NULL) {
 466     MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
 467     // Confirm that it's really not there...
 468     prt = find_region_table(ind, from_hr);
 469     if (prt == NULL) {
 470 
 471       uintptr_t from_hr_bot_card_index =


 478           _sparse_table.add_card(from_hrs_ind, card_index)) {
 479         if (G1RecordHRRSOops) {
 480           HeapRegionRemSet::record(hr(), from);
 481           if (G1TraceHeapRegionRememberedSet) {
 482             gclog_or_tty->print("   Added card " PTR_FORMAT " to region "
 483                                 "[" PTR_FORMAT "...) for ref " PTR_FORMAT ".\n",
 484                                 align_size_down(uintptr_t(from),
 485                                                 CardTableModRefBS::card_size),
 486                                 hr()->bottom(), from);
 487           }
 488         }
 489         if (G1TraceHeapRegionRememberedSet) {
 490           gclog_or_tty->print_cr("   added card to sparse table.");
 491         }
 492         assert(contains_reference_locked(from), "We just added it!");
 493         return;
 494       } else {
 495         if (G1TraceHeapRegionRememberedSet) {
 496           gclog_or_tty->print_cr("   [tid %d] sparse table entry "
 497                         "overflow(f: %d, t: %u)",
 498                         tid, from_hrs_ind, cur_hrs_ind);
 499         }
 500       }
 501 
 502       if (_n_fine_entries == _max_fine_entries) {
 503         prt = delete_region_table();
 504         // There is no need to clear the links to the 'all' list here:
 505         // prt will be reused immediately, i.e. remain in the 'all' list.
 506         prt->init(from_hr, false /* clear_links_to_all_list */);
 507       } else {
 508         prt = PerRegionTable::alloc(from_hr);
 509         link_to_all(prt);
 510       }
 511 
 512       PerRegionTable* first_prt = _fine_grain_regions[ind];
 513       prt->set_collision_list_next(first_prt);
 514       _fine_grain_regions[ind] = prt;
 515       _n_fine_entries++;
 516 
 517       if (G1HRRSUseSparseTable) {
 518         // Transfer from sparse to fine-grain.


 590         max_prev = prev;
 591         max_ind = i;
 592         max_occ = cur_occ;
 593       }
 594       prev = cur->collision_list_next_addr();
 595       cur = cur->collision_list_next();
 596     }
 597     i = i + _fine_eviction_stride;
 598     if (i >= _n_fine_entries) i = i - _n_fine_entries;
 599   }
 600 
 601   _fine_eviction_start++;
 602 
 603   if (_fine_eviction_start >= _n_fine_entries) {
 604     _fine_eviction_start -= _n_fine_entries;
 605   }
 606 
 607   guarantee(max != NULL, "Since _n_fine_entries > 0");
 608 
 609   // Set the corresponding coarse bit.
 610   size_t max_hrs_index = (size_t) max->hr()->hrs_index();
 611   if (!_coarse_map.at(max_hrs_index)) {
 612     _coarse_map.at_put(max_hrs_index, true);
 613     _n_coarse_entries++;
 614     if (G1TraceHeapRegionRememberedSet) {
 615       gclog_or_tty->print("Coarsened entry in region [" PTR_FORMAT "...] "
 616                  "for region [" PTR_FORMAT "...] (" SIZE_FORMAT " coarse entries).\n",
 617                  hr()->bottom(),
 618                  max->hr()->bottom(),
 619                  _n_coarse_entries);
 620     }
 621   }
 622 
 623   // Unsplice.
 624   *max_prev = max->collision_list_next();
 625   Atomic::inc(&_n_coarsenings);
 626   _n_fine_entries--;
 627   return max;
 628 }
 629 
 630 
 631 // At present, this must be called stop-world single-threaded.
 632 void OtherRegionsTable::scrub(CardTableModRefBS* ctbs,
 633                               BitMap* region_bm, BitMap* card_bm) {
 634   // First eliminated garbage regions from the coarse map.
 635   if (G1RSScrubVerbose) {
 636     gclog_or_tty->print_cr("Scrubbing region %u:", hr()->hrs_index());
 637   }
 638 
 639   assert(_coarse_map.size() == region_bm->size(), "Precondition");
 640   if (G1RSScrubVerbose) {
 641     gclog_or_tty->print("   Coarse map: before = "SIZE_FORMAT"...",
 642                         _n_coarse_entries);
 643   }
 644   _coarse_map.set_intersection(*region_bm);
 645   _n_coarse_entries = _coarse_map.count_one_bits();
 646   if (G1RSScrubVerbose) {
 647     gclog_or_tty->print_cr("   after = "SIZE_FORMAT".", _n_coarse_entries);
 648   }
 649 
 650   // Now do the fine-grained maps.
 651   for (size_t i = 0; i < _max_fine_entries; i++) {
 652     PerRegionTable* cur = _fine_grain_regions[i];
 653     PerRegionTable** prev = &_fine_grain_regions[i];
 654     while (cur != NULL) {
 655       PerRegionTable* nxt = cur->collision_list_next();
 656       // If the entire region is dead, eliminate.
 657       if (G1RSScrubVerbose) {
 658         gclog_or_tty->print_cr("     For other region %u:",
 659                                cur->hr()->hrs_index());
 660       }
 661       if (!region_bm->at((size_t) cur->hr()->hrs_index())) {
 662         *prev = nxt;
 663         cur->set_collision_list_next(NULL);
 664         _n_fine_entries--;
 665         if (G1RSScrubVerbose) {
 666           gclog_or_tty->print_cr("          deleted via region map.");
 667         }
 668         unlink_from_all(cur);
 669         PerRegionTable::free(cur);
 670       } else {
 671         // Do fine-grain elimination.
 672         if (G1RSScrubVerbose) {
 673           gclog_or_tty->print("          occ: before = %4d.", cur->occupied());
 674         }
 675         cur->scrub(ctbs, card_bm);
 676         if (G1RSScrubVerbose) {
 677           gclog_or_tty->print_cr("          after = %4d.", cur->occupied());
 678         }
 679         // Did that empty the table completely?
 680         if (cur->occupied() == 0) {
 681           *prev = nxt;


 735     assert(_last_all_fine_prts != NULL &&
 736       _first_all_fine_prts->mem_size() == _last_all_fine_prts->mem_size(), "check that mem_size() is constant");
 737     sum += _first_all_fine_prts->mem_size() * _n_fine_entries;
 738   }
 739   sum += (sizeof(PerRegionTable*) * _max_fine_entries);
 740   sum += (_coarse_map.size_in_words() * HeapWordSize);
 741   sum += (_sparse_table.mem_size());
 742   sum += sizeof(OtherRegionsTable) - sizeof(_sparse_table); // Avoid double counting above.
 743   return sum;
 744 }
 745 
 746 size_t OtherRegionsTable::static_mem_size() {
 747   return FromCardCache::static_mem_size();
 748 }
 749 
 750 size_t OtherRegionsTable::fl_mem_size() {
 751   return PerRegionTable::fl_mem_size();
 752 }
 753 
 754 void OtherRegionsTable::clear_fcc() {
 755   FromCardCache::clear(hr()->hrs_index());
 756 }
 757 
 758 void OtherRegionsTable::clear() {
 759   // if there are no entries, skip this step
 760   if (_first_all_fine_prts != NULL) {
 761     guarantee(_first_all_fine_prts != NULL && _last_all_fine_prts != NULL, "just checking");
 762     PerRegionTable::bulk_free(_first_all_fine_prts, _last_all_fine_prts);
 763     memset(_fine_grain_regions, 0, _max_fine_entries * sizeof(_fine_grain_regions[0]));
 764   } else {
 765     guarantee(_first_all_fine_prts == NULL && _last_all_fine_prts == NULL, "just checking");
 766   }
 767 
 768   _first_all_fine_prts = _last_all_fine_prts = NULL;
 769   _sparse_table.clear();
 770   _coarse_map.clear();
 771   _n_fine_entries = 0;
 772   _n_coarse_entries = 0;
 773 
 774   clear_fcc();
 775 }


 786   if (prt != NULL) {
 787     assert(prt->hr() == hr, "Loop postcondition.");
 788     *prev_addr = prt->collision_list_next();
 789     unlink_from_all(prt);
 790     PerRegionTable::free(prt);
 791     _n_fine_entries--;
 792     return true;
 793   } else {
 794     return false;
 795   }
 796 }
 797 
 798 bool OtherRegionsTable::contains_reference(OopOrNarrowOopStar from) const {
 799   // Cast away const in this case.
 800   MutexLockerEx x((Mutex*)_m, Mutex::_no_safepoint_check_flag);
 801   return contains_reference_locked(from);
 802 }
 803 
 804 bool OtherRegionsTable::contains_reference_locked(OopOrNarrowOopStar from) const {
 805   HeapRegion* hr = _g1h->heap_region_containing_raw(from);
 806   RegionIdx_t hr_ind = (RegionIdx_t) hr->hrs_index();
 807   // Is this region in the coarse map?
 808   if (_coarse_map.at(hr_ind)) return true;
 809 
 810   PerRegionTable* prt = find_region_table(hr_ind & _mod_max_fine_entries_mask,
 811                                      hr);
 812   if (prt != NULL) {
 813     return prt->contains_reference(from);
 814 
 815   } else {
 816     uintptr_t from_card =
 817       (uintptr_t(from) >> CardTableModRefBS::card_shift);
 818     uintptr_t hr_bot_card_index =
 819       uintptr_t(hr->bottom()) >> CardTableModRefBS::card_shift;
 820     assert(from_card >= hr_bot_card_index, "Inv");
 821     CardIdx_t card_index = from_card - hr_bot_card_index;
 822     assert(0 <= card_index && (size_t)card_index < HeapRegion::CardsPerRegion,
 823            "Must be in range.");
 824     return _sparse_table.contains_card(hr_ind, card_index);
 825   }
 826 }
 827 
 828 void
 829 OtherRegionsTable::do_cleanup_work(HRRSCleanupTask* hrrs_cleanup_task) {
 830   _sparse_table.do_cleanup_work(hrrs_cleanup_task);
 831 }
 832 
 833 // Determines how many threads can add records to an rset in parallel.
 834 // This can be done by either mutator threads together with the
 835 // concurrent refinement threads or GC threads.
 836 uint HeapRegionRemSet::num_par_rem_sets() {
 837   return MAX2(DirtyCardQueueSet::num_par_ids() + ConcurrentG1Refine::thread_num(), (uint)ParallelGCThreads);
 838 }
 839 
 840 HeapRegionRemSet::HeapRegionRemSet(G1BlockOffsetSharedArray* bosa,
 841                                    HeapRegion* hr)
 842   : _bosa(bosa),
 843     _m(Mutex::leaf, FormatBuffer<128>("HeapRegionRemSet lock #%u", hr->hrs_index()), true),
 844     _code_roots(), _other_regions(hr, &_m), _iter_state(Unclaimed), _iter_claimed(0) {
 845   reset_for_par_iteration();
 846 }
 847 
 848 void HeapRegionRemSet::setup_remset_size() {
 849   // Setup sparse and fine-grain tables sizes.
 850   // table_size = base * (log(region_size / 1M) + 1)
 851   const int LOG_M = 20;
 852   int region_size_log_mb = MAX2(HeapRegion::LogOfHRGrainBytes - LOG_M, 0);
 853   if (FLAG_IS_DEFAULT(G1RSetSparseRegionEntries)) {
 854     G1RSetSparseRegionEntries = G1RSetSparseRegionEntriesBase * (region_size_log_mb + 1);
 855   }
 856   if (FLAG_IS_DEFAULT(G1RSetRegionEntries)) {
 857     G1RSetRegionEntries = G1RSetRegionEntriesBase * (region_size_log_mb + 1);
 858   }
 859   guarantee(G1RSetSparseRegionEntries > 0 && G1RSetRegionEntries > 0 , "Sanity");
 860 }
 861 
 862 bool HeapRegionRemSet::claim_iter() {
 863   if (_iter_state != Unclaimed) return false;




  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_implementation/g1/concurrentG1Refine.hpp"
  27 #include "gc_implementation/g1/g1BlockOffsetTable.inline.hpp"
  28 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  29 #include "gc_implementation/g1/heapRegionRemSet.hpp"
  30 #include "gc_implementation/g1/heapRegionManager.inline.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/padded.inline.hpp"
  33 #include "memory/space.inline.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "runtime/atomic.inline.hpp"
  36 #include "utilities/bitMap.inline.hpp"
  37 #include "utilities/globalDefinitions.hpp"
  38 #include "utilities/growableArray.hpp"
  39 
  40 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  41 
  42 class PerRegionTable: public CHeapObj<mtGC> {
  43   friend class OtherRegionsTable;
  44   friend class HeapRegionRemSetIterator;
  45 
  46   HeapRegion*     _hr;
  47   BitMap          _bm;
  48   jint            _occupied;
  49 
  50   // next pointer for free/allocated 'all' list


 403 void FromCardCache::clear(uint region_idx) {
 404   uint num_par_remsets = HeapRegionRemSet::num_par_rem_sets();
 405   for (uint i = 0; i < num_par_remsets; i++) {
 406     set(i, region_idx, InvalidCard);
 407   }
 408 }
 409 
 410 void OtherRegionsTable::initialize(uint max_regions) {
 411   FromCardCache::initialize(HeapRegionRemSet::num_par_rem_sets(), max_regions);
 412 }
 413 
 414 void OtherRegionsTable::invalidate(uint start_idx, size_t num_regions) {
 415   FromCardCache::invalidate(start_idx, num_regions);
 416 }
 417 
 418 void OtherRegionsTable::print_from_card_cache() {
 419   FromCardCache::print();
 420 }
 421 
 422 void OtherRegionsTable::add_reference(OopOrNarrowOopStar from, int tid) {
 423   uint cur_hrm_ind = hr()->hrm_index();
 424 
 425   if (G1TraceHeapRegionRememberedSet) {
 426     gclog_or_tty->print_cr("ORT::add_reference_work(" PTR_FORMAT "->" PTR_FORMAT ").",
 427                                                     from,
 428                                                     UseCompressedOops
 429                                                     ? (void *)oopDesc::load_decode_heap_oop((narrowOop*)from)
 430                                                     : (void *)oopDesc::load_decode_heap_oop((oop*)from));
 431   }
 432 
 433   int from_card = (int)(uintptr_t(from) >> CardTableModRefBS::card_shift);
 434 
 435   if (G1TraceHeapRegionRememberedSet) {
 436     gclog_or_tty->print_cr("Table for [" PTR_FORMAT "...): card %d (cache = %d)",
 437                   hr()->bottom(), from_card,
 438                   FromCardCache::at((uint)tid, cur_hrm_ind));
 439   }
 440 
 441   if (FromCardCache::contains_or_replace((uint)tid, cur_hrm_ind, from_card)) {
 442     if (G1TraceHeapRegionRememberedSet) {
 443       gclog_or_tty->print_cr("  from-card cache hit.");
 444     }
 445     assert(contains_reference(from), "We just added it!");
 446     return;
 447   }
 448 
 449   // Note that this may be a continued H region.
 450   HeapRegion* from_hr = _g1h->heap_region_containing_raw(from);
 451   RegionIdx_t from_hrs_ind = (RegionIdx_t) from_hr->hrm_index();
 452 
 453   // If the region is already coarsened, return.
 454   if (_coarse_map.at(from_hrs_ind)) {
 455     if (G1TraceHeapRegionRememberedSet) {
 456       gclog_or_tty->print_cr("  coarse map hit.");
 457     }
 458     assert(contains_reference(from), "We just added it!");
 459     return;
 460   }
 461 
 462   // Otherwise find a per-region table to add it to.
 463   size_t ind = from_hrs_ind & _mod_max_fine_entries_mask;
 464   PerRegionTable* prt = find_region_table(ind, from_hr);
 465   if (prt == NULL) {
 466     MutexLockerEx x(_m, Mutex::_no_safepoint_check_flag);
 467     // Confirm that it's really not there...
 468     prt = find_region_table(ind, from_hr);
 469     if (prt == NULL) {
 470 
 471       uintptr_t from_hr_bot_card_index =


 478           _sparse_table.add_card(from_hrs_ind, card_index)) {
 479         if (G1RecordHRRSOops) {
 480           HeapRegionRemSet::record(hr(), from);
 481           if (G1TraceHeapRegionRememberedSet) {
 482             gclog_or_tty->print("   Added card " PTR_FORMAT " to region "
 483                                 "[" PTR_FORMAT "...) for ref " PTR_FORMAT ".\n",
 484                                 align_size_down(uintptr_t(from),
 485                                                 CardTableModRefBS::card_size),
 486                                 hr()->bottom(), from);
 487           }
 488         }
 489         if (G1TraceHeapRegionRememberedSet) {
 490           gclog_or_tty->print_cr("   added card to sparse table.");
 491         }
 492         assert(contains_reference_locked(from), "We just added it!");
 493         return;
 494       } else {
 495         if (G1TraceHeapRegionRememberedSet) {
 496           gclog_or_tty->print_cr("   [tid %d] sparse table entry "
 497                         "overflow(f: %d, t: %u)",
 498                         tid, from_hrs_ind, cur_hrm_ind);
 499         }
 500       }
 501 
 502       if (_n_fine_entries == _max_fine_entries) {
 503         prt = delete_region_table();
 504         // There is no need to clear the links to the 'all' list here:
 505         // prt will be reused immediately, i.e. remain in the 'all' list.
 506         prt->init(from_hr, false /* clear_links_to_all_list */);
 507       } else {
 508         prt = PerRegionTable::alloc(from_hr);
 509         link_to_all(prt);
 510       }
 511 
 512       PerRegionTable* first_prt = _fine_grain_regions[ind];
 513       prt->set_collision_list_next(first_prt);
 514       _fine_grain_regions[ind] = prt;
 515       _n_fine_entries++;
 516 
 517       if (G1HRRSUseSparseTable) {
 518         // Transfer from sparse to fine-grain.


 590         max_prev = prev;
 591         max_ind = i;
 592         max_occ = cur_occ;
 593       }
 594       prev = cur->collision_list_next_addr();
 595       cur = cur->collision_list_next();
 596     }
 597     i = i + _fine_eviction_stride;
 598     if (i >= _n_fine_entries) i = i - _n_fine_entries;
 599   }
 600 
 601   _fine_eviction_start++;
 602 
 603   if (_fine_eviction_start >= _n_fine_entries) {
 604     _fine_eviction_start -= _n_fine_entries;
 605   }
 606 
 607   guarantee(max != NULL, "Since _n_fine_entries > 0");
 608 
 609   // Set the corresponding coarse bit.
 610   size_t max_hrm_index = (size_t) max->hr()->hrm_index();
 611   if (!_coarse_map.at(max_hrm_index)) {
 612     _coarse_map.at_put(max_hrm_index, true);
 613     _n_coarse_entries++;
 614     if (G1TraceHeapRegionRememberedSet) {
 615       gclog_or_tty->print("Coarsened entry in region [" PTR_FORMAT "...] "
 616                  "for region [" PTR_FORMAT "...] (" SIZE_FORMAT " coarse entries).\n",
 617                  hr()->bottom(),
 618                  max->hr()->bottom(),
 619                  _n_coarse_entries);
 620     }
 621   }
 622 
 623   // Unsplice.
 624   *max_prev = max->collision_list_next();
 625   Atomic::inc(&_n_coarsenings);
 626   _n_fine_entries--;
 627   return max;
 628 }
 629 
 630 
 631 // At present, this must be called stop-world single-threaded.
 632 void OtherRegionsTable::scrub(CardTableModRefBS* ctbs,
 633                               BitMap* region_bm, BitMap* card_bm) {
 634   // First eliminated garbage regions from the coarse map.
 635   if (G1RSScrubVerbose) {
 636     gclog_or_tty->print_cr("Scrubbing region %u:", hr()->hrm_index());
 637   }
 638 
 639   assert(_coarse_map.size() == region_bm->size(), "Precondition");
 640   if (G1RSScrubVerbose) {
 641     gclog_or_tty->print("   Coarse map: before = "SIZE_FORMAT"...",
 642                         _n_coarse_entries);
 643   }
 644   _coarse_map.set_intersection(*region_bm);
 645   _n_coarse_entries = _coarse_map.count_one_bits();
 646   if (G1RSScrubVerbose) {
 647     gclog_or_tty->print_cr("   after = "SIZE_FORMAT".", _n_coarse_entries);
 648   }
 649 
 650   // Now do the fine-grained maps.
 651   for (size_t i = 0; i < _max_fine_entries; i++) {
 652     PerRegionTable* cur = _fine_grain_regions[i];
 653     PerRegionTable** prev = &_fine_grain_regions[i];
 654     while (cur != NULL) {
 655       PerRegionTable* nxt = cur->collision_list_next();
 656       // If the entire region is dead, eliminate.
 657       if (G1RSScrubVerbose) {
 658         gclog_or_tty->print_cr("     For other region %u:",
 659                                cur->hr()->hrm_index());
 660       }
 661       if (!region_bm->at((size_t) cur->hr()->hrm_index())) {
 662         *prev = nxt;
 663         cur->set_collision_list_next(NULL);
 664         _n_fine_entries--;
 665         if (G1RSScrubVerbose) {
 666           gclog_or_tty->print_cr("          deleted via region map.");
 667         }
 668         unlink_from_all(cur);
 669         PerRegionTable::free(cur);
 670       } else {
 671         // Do fine-grain elimination.
 672         if (G1RSScrubVerbose) {
 673           gclog_or_tty->print("          occ: before = %4d.", cur->occupied());
 674         }
 675         cur->scrub(ctbs, card_bm);
 676         if (G1RSScrubVerbose) {
 677           gclog_or_tty->print_cr("          after = %4d.", cur->occupied());
 678         }
 679         // Did that empty the table completely?
 680         if (cur->occupied() == 0) {
 681           *prev = nxt;


 735     assert(_last_all_fine_prts != NULL &&
 736       _first_all_fine_prts->mem_size() == _last_all_fine_prts->mem_size(), "check that mem_size() is constant");
 737     sum += _first_all_fine_prts->mem_size() * _n_fine_entries;
 738   }
 739   sum += (sizeof(PerRegionTable*) * _max_fine_entries);
 740   sum += (_coarse_map.size_in_words() * HeapWordSize);
 741   sum += (_sparse_table.mem_size());
 742   sum += sizeof(OtherRegionsTable) - sizeof(_sparse_table); // Avoid double counting above.
 743   return sum;
 744 }
 745 
 746 size_t OtherRegionsTable::static_mem_size() {
 747   return FromCardCache::static_mem_size();
 748 }
 749 
 750 size_t OtherRegionsTable::fl_mem_size() {
 751   return PerRegionTable::fl_mem_size();
 752 }
 753 
 754 void OtherRegionsTable::clear_fcc() {
 755   FromCardCache::clear(hr()->hrm_index());
 756 }
 757 
 758 void OtherRegionsTable::clear() {
 759   // if there are no entries, skip this step
 760   if (_first_all_fine_prts != NULL) {
 761     guarantee(_first_all_fine_prts != NULL && _last_all_fine_prts != NULL, "just checking");
 762     PerRegionTable::bulk_free(_first_all_fine_prts, _last_all_fine_prts);
 763     memset(_fine_grain_regions, 0, _max_fine_entries * sizeof(_fine_grain_regions[0]));
 764   } else {
 765     guarantee(_first_all_fine_prts == NULL && _last_all_fine_prts == NULL, "just checking");
 766   }
 767 
 768   _first_all_fine_prts = _last_all_fine_prts = NULL;
 769   _sparse_table.clear();
 770   _coarse_map.clear();
 771   _n_fine_entries = 0;
 772   _n_coarse_entries = 0;
 773 
 774   clear_fcc();
 775 }


 786   if (prt != NULL) {
 787     assert(prt->hr() == hr, "Loop postcondition.");
 788     *prev_addr = prt->collision_list_next();
 789     unlink_from_all(prt);
 790     PerRegionTable::free(prt);
 791     _n_fine_entries--;
 792     return true;
 793   } else {
 794     return false;
 795   }
 796 }
 797 
 798 bool OtherRegionsTable::contains_reference(OopOrNarrowOopStar from) const {
 799   // Cast away const in this case.
 800   MutexLockerEx x((Mutex*)_m, Mutex::_no_safepoint_check_flag);
 801   return contains_reference_locked(from);
 802 }
 803 
 804 bool OtherRegionsTable::contains_reference_locked(OopOrNarrowOopStar from) const {
 805   HeapRegion* hr = _g1h->heap_region_containing_raw(from);
 806   RegionIdx_t hr_ind = (RegionIdx_t) hr->hrm_index();
 807   // Is this region in the coarse map?
 808   if (_coarse_map.at(hr_ind)) return true;
 809 
 810   PerRegionTable* prt = find_region_table(hr_ind & _mod_max_fine_entries_mask,
 811                                      hr);
 812   if (prt != NULL) {
 813     return prt->contains_reference(from);
 814 
 815   } else {
 816     uintptr_t from_card =
 817       (uintptr_t(from) >> CardTableModRefBS::card_shift);
 818     uintptr_t hr_bot_card_index =
 819       uintptr_t(hr->bottom()) >> CardTableModRefBS::card_shift;
 820     assert(from_card >= hr_bot_card_index, "Inv");
 821     CardIdx_t card_index = from_card - hr_bot_card_index;
 822     assert(0 <= card_index && (size_t)card_index < HeapRegion::CardsPerRegion,
 823            "Must be in range.");
 824     return _sparse_table.contains_card(hr_ind, card_index);
 825   }
 826 }
 827 
 828 void
 829 OtherRegionsTable::do_cleanup_work(HRRSCleanupTask* hrrs_cleanup_task) {
 830   _sparse_table.do_cleanup_work(hrrs_cleanup_task);
 831 }
 832 
 833 // Determines how many threads can add records to an rset in parallel.
 834 // This can be done by either mutator threads together with the
 835 // concurrent refinement threads or GC threads.
 836 uint HeapRegionRemSet::num_par_rem_sets() {
 837   return MAX2(DirtyCardQueueSet::num_par_ids() + ConcurrentG1Refine::thread_num(), (uint)ParallelGCThreads);
 838 }
 839 
 840 HeapRegionRemSet::HeapRegionRemSet(G1BlockOffsetSharedArray* bosa,
 841                                    HeapRegion* hr)
 842   : _bosa(bosa),
 843     _m(Mutex::leaf, FormatBuffer<128>("HeapRegionRemSet lock #%u", hr->hrm_index()), true),
 844     _code_roots(), _other_regions(hr, &_m), _iter_state(Unclaimed), _iter_claimed(0) {
 845   reset_for_par_iteration();
 846 }
 847 
 848 void HeapRegionRemSet::setup_remset_size() {
 849   // Setup sparse and fine-grain tables sizes.
 850   // table_size = base * (log(region_size / 1M) + 1)
 851   const int LOG_M = 20;
 852   int region_size_log_mb = MAX2(HeapRegion::LogOfHRGrainBytes - LOG_M, 0);
 853   if (FLAG_IS_DEFAULT(G1RSetSparseRegionEntries)) {
 854     G1RSetSparseRegionEntries = G1RSetSparseRegionEntriesBase * (region_size_log_mb + 1);
 855   }
 856   if (FLAG_IS_DEFAULT(G1RSetRegionEntries)) {
 857     G1RSetRegionEntries = G1RSetRegionEntriesBase * (region_size_log_mb + 1);
 858   }
 859   guarantee(G1RSetSparseRegionEntries > 0 && G1RSetRegionEntries > 0 , "Sanity");
 860 }
 861 
 862 bool HeapRegionRemSet::claim_iter() {
 863   if (_iter_state != Unclaimed) return false;