< prev index next >

src/hotspot/share/gc/g1/g1HotCardCache.cpp

Print this page
rev 57124 : imported patch 8227739-merge-scan-rs-update-rs-cost
rev 57126 : [mq]: 8227739-kbarrett-review


  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/g1CardTableEntryClosure.hpp"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc/g1/g1DirtyCardQueue.hpp"
  29 #include "gc/g1/g1HotCardCache.hpp"
  30 #include "runtime/atomic.hpp"
  31 
  32 G1HotCardCache::G1HotCardCache(G1CollectedHeap *g1h):
  33   _g1h(g1h), _use_cache(false), _card_counts(g1h),
  34   _hot_cache(NULL), _hot_cache_size(0), _hot_cache_par_chunk_size(0),
  35   _hot_cache_idx(0), _hot_cache_par_claimed_idx(0)
  36 {}
  37 
  38 void G1HotCardCache::initialize(G1RegionToSpaceMapper* card_counts_storage) {
  39   if (default_use_cache()) {
  40     _use_cache = true;
  41 
  42     _hot_cache_size = (size_t)1 << G1ConcRSLogCacheSize;
  43     _hot_cache = ArrayAllocator<CardValue*>::allocate(_hot_cache_size, mtGC);
  44 
  45     reset_hot_cache_internal();
  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     ArrayAllocator<CardValue*>::free(_hot_cache, _hot_cache_size);
  59     _hot_cache = NULL;
  60   }
  61 }
  62 
  63 CardTable::CardValue* G1HotCardCache::insert(CardValue* card_ptr) {
  64   uint count = _card_counts.add_card_count(card_ptr);
  65   if (!_card_counts.is_hot(count)) {
  66     // The card is not hot so do not store it in the cache;
  67     // return it for immediate refining.
  68     return card_ptr;
  69   }
  70   // Otherwise, the card is hot.
  71   size_t index = Atomic::add(&_hot_cache_idx, 1u) - 1;





  72   size_t masked_index = index & (_hot_cache_size - 1);
  73   CardValue* current_ptr = _hot_cache[masked_index];
  74 
  75   // Try to store the new card pointer into the cache. Compare-and-swap to guard
  76   // against the unlikely event of a race resulting in another card pointer to
  77   // have already been written to the cache. In this case we will return
  78   // card_ptr in favor of the other option, which would be starting over. This
  79   // should be OK since card_ptr will likely be the older card already when/if
  80   // this ever happens.
  81   CardValue* previous_ptr = Atomic::cmpxchg(&_hot_cache[masked_index],
  82                                             current_ptr,
  83                                             card_ptr);
  84   return (previous_ptr == current_ptr) ? previous_ptr : card_ptr;
  85 }
  86 
  87 void G1HotCardCache::drain(G1CardTableEntryClosure* cl, uint worker_id) {
  88   assert(default_use_cache(), "Drain only necessary if we use the hot card cache.");
  89 
  90   assert(_hot_cache != NULL, "Logic");
  91   assert(!use_cache(), "cache should be disabled");




  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/g1CardTableEntryClosure.hpp"
  27 #include "gc/g1/g1CollectedHeap.inline.hpp"
  28 #include "gc/g1/g1DirtyCardQueue.hpp"
  29 #include "gc/g1/g1HotCardCache.hpp"
  30 #include "runtime/atomic.hpp"
  31 
  32 G1HotCardCache::G1HotCardCache(G1CollectedHeap *g1h):
  33   _g1h(g1h), _use_cache(false), _card_counts(g1h),
  34   _hot_cache(NULL), _hot_cache_size(0), _hot_cache_par_chunk_size(0),
  35   _hot_cache_idx(0), _hot_cache_par_claimed_idx(0), _cache_wrapped_around(false)
  36 {}
  37 
  38 void G1HotCardCache::initialize(G1RegionToSpaceMapper* card_counts_storage) {
  39   if (default_use_cache()) {
  40     _use_cache = true;
  41 
  42     _hot_cache_size = (size_t)1 << G1ConcRSLogCacheSize;
  43     _hot_cache = ArrayAllocator<CardValue*>::allocate(_hot_cache_size, mtGC);
  44 
  45     reset_hot_cache_internal();
  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     _cache_wrapped_around = false;
  52 
  53     _card_counts.initialize(card_counts_storage);
  54   }
  55 }
  56 
  57 G1HotCardCache::~G1HotCardCache() {
  58   if (default_use_cache()) {
  59     assert(_hot_cache != NULL, "Logic");
  60     ArrayAllocator<CardValue*>::free(_hot_cache, _hot_cache_size);
  61     _hot_cache = NULL;
  62   }
  63 }
  64 
  65 CardTable::CardValue* G1HotCardCache::insert(CardValue* card_ptr) {
  66   uint count = _card_counts.add_card_count(card_ptr);
  67   if (!_card_counts.is_hot(count)) {
  68     // The card is not hot so do not store it in the cache;
  69     // return it for immediate refining.
  70     return card_ptr;
  71   }
  72   // Otherwise, the card is hot.
  73   size_t index = Atomic::add(&_hot_cache_idx, 1u) - 1;
  74   // This does not need an atomic update. Racing threads may at most write the
  75   // same value.
  76   if (index == _hot_cache_size) {
  77     Atomic::store(&_cache_wrapped_around, true);
  78   }
  79   size_t masked_index = index & (_hot_cache_size - 1);
  80   CardValue* current_ptr = _hot_cache[masked_index];
  81 
  82   // Try to store the new card pointer into the cache. Compare-and-swap to guard
  83   // against the unlikely event of a race resulting in another card pointer to
  84   // have already been written to the cache. In this case we will return
  85   // card_ptr in favor of the other option, which would be starting over. This
  86   // should be OK since card_ptr will likely be the older card already when/if
  87   // this ever happens.
  88   CardValue* previous_ptr = Atomic::cmpxchg(&_hot_cache[masked_index],
  89                                             current_ptr,
  90                                             card_ptr);
  91   return (previous_ptr == current_ptr) ? previous_ptr : card_ptr;
  92 }
  93 
  94 void G1HotCardCache::drain(G1CardTableEntryClosure* cl, uint worker_id) {
  95   assert(default_use_cache(), "Drain only necessary if we use the hot card cache.");
  96 
  97   assert(_hot_cache != NULL, "Logic");
  98   assert(!use_cache(), "cache should be disabled");


< prev index next >