< prev index next >

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

Print this page
rev 7695 : 8069273: Reduce Hot Card Cache Lock contention
Reviewed-by: tschatzl
rev 7696 : [mq]: atomicadd


  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/dirtyCardQueue.hpp"
  27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc_implementation/g1/g1HotCardCache.hpp"
  29 #include "gc_implementation/g1/g1RemSet.hpp"
  30 #include "runtime/atomic.inline.hpp"
  31 
  32 G1HotCardCache::G1HotCardCache(G1CollectedHeap *g1h):
  33   _g1h(g1h), _hot_cache(NULL), _use_cache(false), _card_counts(g1h) {}
  34 
  35 void G1HotCardCache::initialize(G1RegionToSpaceMapper* card_counts_storage) {
  36   if (default_use_cache()) {
  37     _use_cache = true;
  38 
  39     _hot_cache_size = (1 << G1ConcRSLogCacheSize);
  40     _hot_cache = NEW_C_HEAP_ARRAY(jbyte*, _hot_cache_size, mtGC);



  41 
  42     _n_hot = 0;
  43     _hot_cache_idx = 0;
  44 
  45     // For refining the cards in the hot cache in parallel
  46     _hot_cache_par_chunk_size = ClaimChunkSize;
  47     _hot_cache_par_claimed_idx = 0;
  48 
  49     _card_counts.initialize(card_counts_storage);
  50   }
  51 }
  52 
  53 G1HotCardCache::~G1HotCardCache() {
  54   if (default_use_cache()) {
  55     assert(_hot_cache != NULL, "Logic");
  56     FREE_C_HEAP_ARRAY(jbyte*, _hot_cache);
  57   }
  58 }
  59 
  60 jbyte* G1HotCardCache::insert(jbyte* card_ptr) {
  61   uint count = _card_counts.add_card_count(card_ptr);
  62   if (!_card_counts.is_hot(count)) {
  63     // The card is not hot so do not store it in the cache;
  64     // return it for immediate refining.
  65     return card_ptr;
  66   }
  67 
  68   // Otherwise, the card is hot.
  69   jbyte* res = NULL;
  70   MutexLockerEx x(HotCardCache_lock, Mutex::_no_safepoint_check_flag);
  71   if (_n_hot == _hot_cache_size) {
  72     res = _hot_cache[_hot_cache_idx];
  73     _n_hot--;
  74   }
  75 
  76   // Now _n_hot < _hot_cache_size, and we can insert at _hot_cache_idx.
  77   _hot_cache[_hot_cache_idx] = card_ptr;
  78   _hot_cache_idx++;
  79 
  80   if (_hot_cache_idx == _hot_cache_size) {
  81     // Wrap around
  82     _hot_cache_idx = 0;
  83   }
  84   _n_hot++;
  85 
  86   return res;
  87 }
  88 
  89 void G1HotCardCache::drain(uint worker_i,
  90                            G1RemSet* g1rs,
  91                            DirtyCardQueue* into_cset_dcq) {
  92   if (!default_use_cache()) {
  93     assert(_hot_cache == NULL, "Logic");
  94     return;
  95   }
  96 
  97   assert(_hot_cache != NULL, "Logic");
  98   assert(!use_cache(), "cache should be disabled");
  99   int start_idx;
 100 
 101   while ((start_idx = _hot_cache_par_claimed_idx) < _n_hot) { // read once
 102     int end_idx = start_idx + _hot_cache_par_chunk_size;
 103 
 104     if (start_idx ==
 105         Atomic::cmpxchg(end_idx, &_hot_cache_par_claimed_idx, start_idx)) {


 106       // The current worker has successfully claimed the chunk [start_idx..end_idx)
 107       end_idx = MIN2(end_idx, _n_hot);
 108       for (int i = start_idx; i < end_idx; i++) {
 109         jbyte* card_ptr = _hot_cache[i];
 110         if (card_ptr != NULL) {
 111           if (g1rs->refine_card(card_ptr, worker_i, true)) {
 112             // The part of the heap spanned by the card contains references
 113             // that point into the current collection set.
 114             // We need to record the card pointer in the DirtyCardQueueSet
 115             // that we use for such cards.
 116             //
 117             // The only time we care about recording cards that contain
 118             // references that point into the collection set is during
 119             // RSet updating while within an evacuation pause.
 120             // In this case worker_i should be the id of a GC worker thread
 121             assert(SafepointSynchronize::is_at_safepoint(), "Should be at a safepoint");
 122             assert(worker_i < ParallelGCThreads,
 123                    err_msg("incorrect worker id: %u", worker_i));
 124 
 125             into_cset_dcq->enqueue(card_ptr);
 126           }


 127         }
 128       }
 129     }
 130   }
 131   // The existing entries in the hot card cache, which were just refined
 132   // above, are discarded prior to re-enabling the cache near the end of the GC.
 133 }
 134 
 135 void G1HotCardCache::reset_card_counts(HeapRegion* hr) {
 136   _card_counts.clear_region(hr);
 137 }
 138 
 139 void G1HotCardCache::reset_card_counts() {
 140   _card_counts.clear_all();
 141 }


  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/dirtyCardQueue.hpp"
  27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc_implementation/g1/g1HotCardCache.hpp"
  29 #include "gc_implementation/g1/g1RemSet.hpp"
  30 #include "runtime/atomic.inline.hpp"
  31 
  32 G1HotCardCache::G1HotCardCache(G1CollectedHeap *g1h):
  33   _g1h(g1h), _hot_cache(NULL), _use_cache(false), _card_counts(g1h) {}
  34 
  35 void G1HotCardCache::initialize(G1RegionToSpaceMapper* card_counts_storage) {
  36   if (default_use_cache()) {
  37     _use_cache = true;
  38 
  39     _hot_cache_size = (size_t)1 << G1ConcRSLogCacheSize;
  40     _hot_cache = NEW_C_HEAP_ARRAY(jbyte*, _hot_cache_size, mtGC);
  41     for (size_t i = 0; i < _hot_cache_size; i++) {
  42       _hot_cache[i] = NULL;
  43     }
  44 

  45     _hot_cache_idx = 0;
  46 
  47     // For refining the cards in the hot cache in parallel
  48     _hot_cache_par_chunk_size = ClaimChunkSize;
  49     _hot_cache_par_claimed_idx = 0;
  50 
  51     _card_counts.initialize(card_counts_storage);
  52   }
  53 }
  54 
  55 G1HotCardCache::~G1HotCardCache() {
  56   if (default_use_cache()) {
  57     assert(_hot_cache != NULL, "Logic");
  58     FREE_C_HEAP_ARRAY(jbyte*, _hot_cache);
  59   }
  60 }
  61 
  62 jbyte* G1HotCardCache::insert(jbyte* card_ptr) {
  63   uint count = _card_counts.add_card_count(card_ptr);
  64   if (!_card_counts.is_hot(count)) {
  65     // The card is not hot so do not store it in the cache;
  66     // return it for immediate refining.
  67     return card_ptr;
  68   }

  69   // Otherwise, the card is hot.
  70   size_t index = Atomic::add(1, &_hot_cache_idx) - 1;
  71   size_t masked_index = index & (_hot_cache_size - 1);
  72   jbyte* current_ptr = _hot_cache[masked_index];
  73   
  74   // Try to store the new card pointer into the cache. Compare-and-swap to guard
  75   // against the unlikely event of a race resulting in another card pointer to
  76   // have already been written to the cache. In this case we will return
  77   // card_ptr in favor of the other option, which would be starting over. This
  78   // should be OK since card_ptr will likely be the older card already when/if
  79   // this ever happens.
  80   jbyte* previous_ptr = (jbyte*)Atomic::cmpxchg_ptr(card_ptr,
  81                                                     &_hot_cache[masked_index],
  82                                                     current_ptr);
  83   return (previous_ptr == current_ptr) ? previous_ptr : card_ptr;




  84 }
  85 
  86 void G1HotCardCache::drain(uint worker_i,
  87                            G1RemSet* g1rs,
  88                            DirtyCardQueue* into_cset_dcq) {
  89   if (!default_use_cache()) {
  90     assert(_hot_cache == NULL, "Logic");
  91     return;
  92   }
  93 
  94   assert(_hot_cache != NULL, "Logic");
  95   assert(!use_cache(), "cache should be disabled");




  96 
  97   while (_hot_cache_par_claimed_idx < _hot_cache_size) {
  98     size_t end_idx = Atomic::add(_hot_cache_par_chunk_size,
  99                                  &_hot_cache_par_claimed_idx);
 100     size_t start_idx = end_idx - _hot_cache_par_chunk_size;
 101     // The current worker has successfully claimed the chunk [start_idx..end_idx)
 102     end_idx = MIN2(end_idx, _hot_cache_size);
 103     for (size_t i = start_idx; i < end_idx; i++) {
 104       jbyte* card_ptr = _hot_cache[i];
 105       if (card_ptr != NULL) {
 106         if (g1rs->refine_card(card_ptr, worker_i, true)) {
 107           // The part of the heap spanned by the card contains references
 108           // that point into the current collection set.
 109           // We need to record the card pointer in the DirtyCardQueueSet
 110           // that we use for such cards.
 111           //
 112           // The only time we care about recording cards that contain
 113           // references that point into the collection set is during
 114           // RSet updating while within an evacuation pause.
 115           // In this case worker_i should be the id of a GC worker thread
 116           assert(SafepointSynchronize::is_at_safepoint(), "Should be at a safepoint");
 117           assert(worker_i < ParallelGCThreads,
 118                  err_msg("incorrect worker id: %u", worker_i));
 119 
 120           into_cset_dcq->enqueue(card_ptr);
 121         }
 122       } else {
 123         break;
 124       }
 125     }
 126   }
 127   
 128   // The existing entries in the hot card cache, which were just refined
 129   // above, are discarded prior to re-enabling the cache near the end of the GC.
 130 }
 131 
 132 void G1HotCardCache::reset_card_counts(HeapRegion* hr) {
 133   _card_counts.clear_region(hr);
 134 }
 135 
 136 void G1HotCardCache::reset_card_counts() {
 137   _card_counts.clear_all();
 138 }
< prev index next >