< prev index next >

src/share/vm/gc/g1/g1RemSet.cpp

Print this page
rev 10750 : [mq]: 8153503-cleanup-remset-iteration

@@ -44,24 +44,98 @@
 #include "oops/oop.inline.hpp"
 #include "utilities/globalDefinitions.hpp"
 #include "utilities/intHisto.hpp"
 #include "utilities/stack.inline.hpp"
 
+// Collects information about the remembered set scan progress during an evacuation.
+class G1RemSetScanState : public CHeapObj<mtGC> {
+private:
+  // Scan progress for the remembered set of a single region. Transitions from
+  // Unclaimed -> Claimed -> Complete.
+  // At each of the transitions the thread that does the transition needs to perform
+  // some special action once. This is the reason for the extra "Claimed" state.
+  typedef jint G1RemsetIterState;
+
+  static const G1RemsetIterState Unclaimed = 0; // The remembered set has not been scanned yet.
+  static const G1RemsetIterState Claimed = 1;   // The remembered set is currently being scanned.
+  static const G1RemsetIterState Complete = 2;  // The remembered set has been completely scanned.
+  
+  G1RemsetIterState* _iter_state;
+  // The current location where the next thread should continue scanning in a region's
+  // remembered set.
+  size_t* _iter_claimed;
+
+public:
+  G1RemSetScanState() :
+    _iter_state(NULL),
+    _iter_claimed(NULL) {
+
+  }
+
+  ~G1RemSetScanState() {
+    if (_iter_state != NULL) {
+      FREE_C_HEAP_ARRAY(G1RemsetIterState, _iter_state);
+    }
+    if (_iter_claimed != NULL) {
+      FREE_C_HEAP_ARRAY(size_t, _iter_claimed);
+    }
+  }
+
+  void initialize(uint max_regions) {
+    _iter_state = NEW_C_HEAP_ARRAY(G1RemsetIterState, max_regions, mtGC);
+    _iter_claimed = NEW_C_HEAP_ARRAY(size_t, max_regions, mtGC);
+  }
+
+  void reset(uint max_regions) {
+    for (uint i = 0; i < max_regions; i++) {
+      _iter_state[i] = Unclaimed;
+    }
+    memset(_iter_claimed, 0, max_regions * sizeof(size_t));
+  }
+  // Attempt to claim the remembered set of the region for iteration. Returns true
+  // if this call caused the transition from Unclaimed to Claimed.
+  inline bool claim_iter(uint region) {
+    if (_iter_state[region] != Unclaimed) {
+      return false;
+    }
+    jint res = Atomic::cmpxchg(Claimed, (jint*)(&_iter_state[region]), Unclaimed);
+    return (res == Unclaimed);
+  }
+  // Try to atomically sets the iteration state to "complete". Returns true for the
+  // thread that caused the transition.
+  inline bool set_iter_complete(uint region) {
+    if (iter_is_complete(region)) {
+      return false;
+    }
+    jint res = Atomic::cmpxchg(Complete, (jint*)(&_iter_state[region]), Claimed);
+    return (res == Claimed);
+  }
+  // Returns true if the region's iteration is complete.
+  inline bool iter_is_complete(uint region) {
+    return _iter_state[region] == Complete;
+  }
+  // The current position within the remembered set of the given region.
+  inline size_t iter_claimed(uint region) const {
+    return _iter_claimed[region];
+  }
+  // Claim the next block of cards within the remembered set of the region with
+  // step size.
+  inline size_t iter_claimed_next(uint region, size_t step) {
+    return Atomic::add(step, &_iter_claimed[region]) - step;
+  }
+};
+
 G1RemSet::G1RemSet(G1CollectedHeap* g1, CardTableModRefBS* ct_bs) :
   _g1(g1),
+  _scan_state(new G1RemSetScanState()),
   _conc_refine_cards(0),
   _ct_bs(ct_bs),
   _g1p(_g1->g1_policy()),
   _cg1r(g1->concurrent_g1_refine()),
-  _cset_rs_update_cl(NULL),
   _prev_period_summary(),
   _into_cset_dirty_card_queue_set(false)
 {
-  _cset_rs_update_cl = NEW_C_HEAP_ARRAY(G1ParPushHeapRSClosure*, n_workers(), mtGC);
-  for (uint i = 0; i < n_workers(); i++) {
-    _cset_rs_update_cl[i] = NULL;
-  }
   if (log_is_enabled(Trace, gc, remset)) {
     _prev_period_summary.initialize(this);
   }
   // Initialize the card queue set used to hold cards containing
   // references into the collection set.

@@ -73,55 +147,55 @@
                                              Shared_DirtyCardQ_lock,
                                              &JavaThread::dirty_card_queue_set());
 }
 
 G1RemSet::~G1RemSet() {
-  for (uint i = 0; i < n_workers(); i++) {
-    assert(_cset_rs_update_cl[i] == NULL, "it should be");
+  if (_scan_state != NULL) {
+    delete _scan_state;
   }
-  FREE_C_HEAP_ARRAY(G1ParPushHeapRSClosure*, _cset_rs_update_cl);
 }
 
 uint G1RemSet::num_par_rem_sets() {
   return MAX2(DirtyCardQueueSet::num_par_ids() + ConcurrentG1Refine::thread_num(), ParallelGCThreads);
 }
 
 void G1RemSet::initialize(size_t capacity, uint max_regions) {
   G1FromCardCache::initialize(num_par_rem_sets(), max_regions);
+  _scan_state->initialize(max_regions);
   {
     GCTraceTime(Debug, gc, marking)("Initialize Card Live Data");
     _card_live_data.initialize(capacity, max_regions);
   }
   if (G1PretouchAuxiliaryMemory) {
     GCTraceTime(Debug, gc, marking)("Pre-Touch Card Live Data");
     _card_live_data.pretouch();
   }
 }
 
-ScanRSClosure::ScanRSClosure(G1ParPushHeapRSClosure* oc,
+G1ScanRSClosure::G1ScanRSClosure(G1RemSetScanState* scan_state,
+                                 G1ParPushHeapRSClosure* push_heap_cl,
                              CodeBlobClosure* code_root_cl,
                              uint worker_i) :
-  _oc(oc),
+  _scan_state(scan_state),
+  _push_heap_cl(push_heap_cl),
   _code_root_cl(code_root_cl),
   _strong_code_root_scan_time_sec(0.0),
   _cards(0),
   _cards_done(0),
-  _worker_i(worker_i),
-  _try_claimed(false) {
+  _worker_i(worker_i) {
   _g1h = G1CollectedHeap::heap();
   _bot = _g1h->bot();
   _ct_bs = _g1h->g1_barrier_set();
   _block_size = MAX2<size_t>(G1RSetScanBlockSize, 1);
 }
 
-void ScanRSClosure::scanCard(size_t index, HeapRegion *r) {
+void G1ScanRSClosure::scan_card(size_t index, HeapRegion *r) {
   // Stack allocate the DirtyCardToOopClosure instance
-  HeapRegionDCTOC cl(_g1h, r, _oc,
-      CardTableModRefBS::Precise);
+  HeapRegionDCTOC cl(_g1h, r, _push_heap_cl, CardTableModRefBS::Precise);
 
   // Set the "from" region in the closure.
-  _oc->set_region(r);
+  _push_heap_cl->set_region(r);
   MemRegion card_region(_bot->address_for_index(index), BOTConstants::N_words);
   MemRegion pre_gc_allocated(r->bottom(), r->scan_top());
   MemRegion mr = pre_gc_allocated.intersection(card_region);
   if (!mr.is_empty() && !_ct_bs->is_card_claimed(index)) {
     // We make the card as "claimed" lazily (so races are possible

@@ -131,39 +205,39 @@
     _cards_done++;
     cl.do_MemRegion(mr);
   }
 }
 
-void ScanRSClosure::scan_strong_code_roots(HeapRegion* r) {
+void G1ScanRSClosure::scan_strong_code_roots(HeapRegion* r) {
   double scan_start = os::elapsedTime();
   r->strong_code_roots_do(_code_root_cl);
   _strong_code_root_scan_time_sec += (os::elapsedTime() - scan_start);
 }
 
-bool ScanRSClosure::doHeapRegion(HeapRegion* r) {
+bool G1ScanRSClosure::doHeapRegion(HeapRegion* r) {
   assert(r->in_collection_set(), "should only be called on elements of CS.");
-  HeapRegionRemSet* hrrs = r->rem_set();
-  if (hrrs->iter_is_complete()) return false; // All done.
-  if (!_try_claimed && !hrrs->claim_iter()) return false;
+  uint region_idx = r->hrm_index();
+
+  if (_scan_state->iter_is_complete(region_idx)) {
+    return false;
+  }
+  if (_scan_state->claim_iter(region_idx)) {
   // If we ever free the collection set concurrently, we should also
   // clear the card table concurrently therefore we won't need to
   // add regions of the collection set to the dirty cards region.
   _g1h->push_dirty_cards_region(r);
-  // If we didn't return above, then
-  //   _try_claimed || r->claim_iter()
-  // is true: either we're supposed to work on claimed-but-not-complete
-  // regions, or we successfully claimed the region.
+  }
 
-  HeapRegionRemSetIterator iter(hrrs);
+  HeapRegionRemSetIterator iter(r->rem_set());
   size_t card_index;
 
   // We claim cards in block so as to reduce the contention. The block size is determined by
   // the G1RSetScanBlockSize parameter.
-  size_t jump_to_card = hrrs->iter_claimed_next(_block_size);
+  size_t jump_to_card = _scan_state->iter_claimed_next(region_idx, _block_size);
   for (size_t current_card = 0; iter.has_next(card_index); current_card++) {
     if (current_card >= jump_to_card + _block_size) {
-      jump_to_card = hrrs->iter_claimed_next(_block_size);
+      jump_to_card = _scan_state->iter_claimed_next(region_idx, _block_size);
     }
     if (current_card < jump_to_card) continue;
     HeapWord* card_start = _g1h->bot()->address_for_index(card_index);
 
     HeapRegion* card_region = _g1h->heap_region_containing(card_start);

@@ -174,66 +248,63 @@
     }
 
     // If the card is dirty, then we will scan it during updateRS.
     if (!card_region->in_collection_set() &&
         !_ct_bs->is_card_dirty(card_index)) {
-      scanCard(card_index, card_region);
+      scan_card(card_index, card_region);
     }
   }
-  if (!_try_claimed) {
+  if (_scan_state->set_iter_complete(region_idx)) {
     // Scan the strong code root list attached to the current region
     scan_strong_code_roots(r);
-
-    hrrs->set_iter_complete();
   }
   return false;
 }
 
-size_t G1RemSet::scanRS(G1ParPushHeapRSClosure* oc,
+size_t G1RemSet::scan_rem_set(G1ParPushHeapRSClosure* oops_in_heap_closure,
                         CodeBlobClosure* heap_region_codeblobs,
                         uint worker_i) {
   double rs_time_start = os::elapsedTime();
 
   HeapRegion *startRegion = _g1->start_cset_region_for_worker(worker_i);
 
-  ScanRSClosure scanRScl(oc, heap_region_codeblobs, worker_i);
-
-  _g1->collection_set_iterate_from(startRegion, &scanRScl);
-  scanRScl.set_try_claimed();
-  _g1->collection_set_iterate_from(startRegion, &scanRScl);
+  G1ScanRSClosure cl(_scan_state, oops_in_heap_closure, heap_region_codeblobs, worker_i);
+  _g1->collection_set_iterate_from(startRegion, &cl);
 
-  double scan_rs_time_sec = (os::elapsedTime() - rs_time_start)
-                            - scanRScl.strong_code_root_scan_time_sec();
+   double scan_rs_time_sec = (os::elapsedTime() - rs_time_start) -
+                              cl.strong_code_root_scan_time_sec();
 
   _g1p->phase_times()->record_time_secs(G1GCPhaseTimes::ScanRS, worker_i, scan_rs_time_sec);
-  _g1p->phase_times()->record_time_secs(G1GCPhaseTimes::CodeRoots, worker_i, scanRScl.strong_code_root_scan_time_sec());
+  _g1p->phase_times()->record_time_secs(G1GCPhaseTimes::CodeRoots, worker_i, cl.strong_code_root_scan_time_sec());
 
-  return scanRScl.cards_done();
+  return cl.cards_done();
 }
 
 // Closure used for updating RSets and recording references that
 // point into the collection set. Only called during an
 // evacuation pause.
 
 class RefineRecordRefsIntoCSCardTableEntryClosure: public CardTableEntryClosure {
   G1RemSet* _g1rs;
   DirtyCardQueue* _into_cset_dcq;
+  G1ParPushHeapRSClosure* _cl;
 public:
   RefineRecordRefsIntoCSCardTableEntryClosure(G1CollectedHeap* g1h,
-                                              DirtyCardQueue* into_cset_dcq) :
-    _g1rs(g1h->g1_rem_set()), _into_cset_dcq(into_cset_dcq)
+                                              DirtyCardQueue* into_cset_dcq,
+                                              G1ParPushHeapRSClosure* cl) :
+    _g1rs(g1h->g1_rem_set()), _into_cset_dcq(into_cset_dcq), _cl(cl)
   {}
 
   bool do_card_ptr(jbyte* card_ptr, uint worker_i) {
     // The only time we care about recording cards that
     // contain references that point into the collection set
     // is during RSet updating within an evacuation pause.
     // In this case worker_i should be the id of a GC worker thread.
     assert(SafepointSynchronize::is_at_safepoint(), "not during an evacuation pause");
     assert(worker_i < ParallelGCThreads, "should be a GC worker");
 
-    if (_g1rs->refine_card(card_ptr, worker_i, true)) {
+    if (_g1rs->refine_card(card_ptr, worker_i, _cl)) {
       // 'card_ptr' contains references that point into the collection
       // set. We need to record the card in the DCQS
       // (_into_cset_dirty_card_queue_set)
       // that's used for that purpose.
       //

@@ -242,12 +313,14 @@
     }
     return true;
   }
 };
 
-void G1RemSet::updateRS(DirtyCardQueue* into_cset_dcq, uint worker_i) {
-  RefineRecordRefsIntoCSCardTableEntryClosure into_cset_update_rs_cl(_g1, into_cset_dcq);
+void G1RemSet::update_rem_set(DirtyCardQueue* into_cset_dcq,
+                              G1ParPushHeapRSClosure* oops_in_heap_closure,
+                              uint worker_i) {
+  RefineRecordRefsIntoCSCardTableEntryClosure into_cset_update_rs_cl(_g1, into_cset_dcq, oops_in_heap_closure);
 
   G1GCParPhaseTimesTracker x(_g1p->phase_times(), G1GCPhaseTimes::UpdateRS, worker_i);
   if (ConcurrentG1Refine::hot_card_cache_enabled()) {
     // Apply the closure to the entries of the hot card cache.
     G1GCParPhaseTimesTracker y(_g1p->phase_times(), G1GCPhaseTimes::ScanHCC, worker_i);

@@ -259,18 +332,13 @@
 
 void G1RemSet::cleanupHRRS() {
   HeapRegionRemSet::cleanup();
 }
 
-size_t G1RemSet::oops_into_collection_set_do(G1ParPushHeapRSClosure* oc,
+size_t G1RemSet::oops_into_collection_set_do(G1ParPushHeapRSClosure* cl,
                                              CodeBlobClosure* heap_region_codeblobs,
                                              uint worker_i) {
-  // We cache the value of 'oc' closure into the appropriate slot in the
-  // _cset_rs_update_cl for this worker
-  assert(worker_i < n_workers(), "sanity");
-  _cset_rs_update_cl[worker_i] = oc;
-
   // A DirtyCardQueue that is used to hold cards containing references
   // that point into the collection set. This DCQ is associated with a
   // special DirtyCardQueueSet (see g1CollectedHeap.hpp).  Under normal
   // circumstances (i.e. the pause successfully completes), these cards
   // are just discarded (there's no need to update the RSets of regions

@@ -278,22 +346,20 @@
   // are wholly 'free' of live objects. In the event of an evacuation
   // failure the cards/buffers in this queue set are passed to the
   // DirtyCardQueueSet that is used to manage RSet updates
   DirtyCardQueue into_cset_dcq(&_into_cset_dirty_card_queue_set);
 
-  updateRS(&into_cset_dcq, worker_i);
-  size_t cards_scanned = scanRS(oc, heap_region_codeblobs, worker_i);
-
-  // We now clear the cached values of _cset_rs_update_cl for this worker
-  _cset_rs_update_cl[worker_i] = NULL;
-  return cards_scanned;
+  update_rem_set(&into_cset_dcq, cl, worker_i);
+  return scan_rem_set(cl, heap_region_codeblobs, worker_i);;
 }
 
 void G1RemSet::prepare_for_oops_into_collection_set_do() {
   _g1->set_refine_cte_cl_concurrency(false);
   DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set();
   dcqs.concatenate_logs();
+
+  _scan_state->reset(_g1->max_regions());
 }
 
 void G1RemSet::cleanup_after_oops_into_collection_set_do() {
   // Cleanup after copy
   _g1->set_refine_cte_cl_concurrency(true);

@@ -364,19 +430,22 @@
 
 // Returns true if the given card contains references that point
 // into the collection set, if we're checking for such references;
 // false otherwise.
 
-bool G1RemSet::refine_card(jbyte* card_ptr, uint worker_i,
-                           bool check_for_refs_into_cset) {
+bool G1RemSet::refine_card(jbyte* card_ptr,
+                           uint worker_i,
+                           G1ParPushHeapRSClosure*  oops_in_heap_closure) {
   assert(_g1->is_in_exact(_ct_bs->addr_for(card_ptr)),
          "Card at " PTR_FORMAT " index " SIZE_FORMAT " representing heap at " PTR_FORMAT " (%u) must be in committed heap",
          p2i(card_ptr),
          _ct_bs->index_for(_ct_bs->addr_for(card_ptr)),
          p2i(_ct_bs->addr_for(card_ptr)),
          _g1->addr_to_region(_ct_bs->addr_for(card_ptr)));
 
+  bool check_for_refs_into_cset = oops_in_heap_closure != NULL;
+
   // If the card is no longer dirty, nothing to do.
   if (*card_ptr != CardTableModRefBS::dirty_card_val()) {
     // No need to return that this card contains refs that point
     // into the collection set.
     return false;

@@ -449,19 +518,10 @@
   // a card beyond the heap.  This is not safe without a perm
   // gen at the upper end of the heap.
   HeapWord* end   = start + CardTableModRefBS::card_size_in_words;
   MemRegion dirtyRegion(start, end);
 
-  G1ParPushHeapRSClosure* oops_in_heap_closure = NULL;
-  if (check_for_refs_into_cset) {
-    // ConcurrentG1RefineThreads have worker numbers larger than what
-    // _cset_rs_update_cl[] is set up to handle. But those threads should
-    // only be active outside of a collection which means that when they
-    // reach here they should have check_for_refs_into_cset == false.
-    assert((size_t)worker_i < n_workers(), "index of worker larger than _cset_rs_update_cl[].length");
-    oops_in_heap_closure = _cset_rs_update_cl[worker_i];
-  }
   G1UpdateRSOrPushRefOopClosure update_rs_oop_cl(_g1,
                                                  _g1->g1_rem_set(),
                                                  oops_in_heap_closure,
                                                  check_for_refs_into_cset,
                                                  worker_i);

@@ -577,11 +637,11 @@
     G1HotCardCache* hot_card_cache = _cg1r->hot_card_cache();
     bool use_hot_card_cache = hot_card_cache->use_cache();
     hot_card_cache->set_use_cache(false);
 
     DirtyCardQueue into_cset_dcq(&_into_cset_dirty_card_queue_set);
-    updateRS(&into_cset_dcq, 0);
+    update_rem_set(&into_cset_dcq, NULL, 0);
     _into_cset_dirty_card_queue_set.clear();
 
     hot_card_cache->set_use_cache(use_hot_card_cache);
     assert(JavaThread::dirty_card_queue_set().completed_buffers_num() == 0, "All should be consumed");
   }
< prev index next >