1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/g1/g1HotCardCache.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,148 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "gc_implementation/g1/dirtyCardQueue.hpp" 1.30 +#include "gc_implementation/g1/g1CollectedHeap.inline.hpp" 1.31 +#include "gc_implementation/g1/g1HotCardCache.hpp" 1.32 +#include "gc_implementation/g1/g1RemSet.hpp" 1.33 +#include "gc_implementation/g1/heapRegion.hpp" 1.34 +#include "runtime/atomic.hpp" 1.35 + 1.36 +G1HotCardCache::G1HotCardCache(G1CollectedHeap *g1h): 1.37 + _g1h(g1h), _hot_cache(NULL), _use_cache(false), _card_counts(g1h) {} 1.38 + 1.39 +void G1HotCardCache::initialize() { 1.40 + if (default_use_cache()) { 1.41 + _use_cache = true; 1.42 + 1.43 + _hot_cache_size = (1 << G1ConcRSLogCacheSize); 1.44 + _hot_cache = NEW_C_HEAP_ARRAY(jbyte*, _hot_cache_size, mtGC); 1.45 + 1.46 + _n_hot = 0; 1.47 + _hot_cache_idx = 0; 1.48 + 1.49 + // For refining the cards in the hot cache in parallel 1.50 + uint n_workers = (ParallelGCThreads > 0 ? 1.51 + _g1h->workers()->total_workers() : 1); 1.52 + _hot_cache_par_chunk_size = MAX2(1, _hot_cache_size / (int)n_workers); 1.53 + _hot_cache_par_claimed_idx = 0; 1.54 + 1.55 + _card_counts.initialize(); 1.56 + } 1.57 +} 1.58 + 1.59 +G1HotCardCache::~G1HotCardCache() { 1.60 + if (default_use_cache()) { 1.61 + assert(_hot_cache != NULL, "Logic"); 1.62 + FREE_C_HEAP_ARRAY(jbyte*, _hot_cache, mtGC); 1.63 + } 1.64 +} 1.65 + 1.66 +jbyte* G1HotCardCache::insert(jbyte* card_ptr) { 1.67 + uint count = _card_counts.add_card_count(card_ptr); 1.68 + if (!_card_counts.is_hot(count)) { 1.69 + // The card is not hot so do not store it in the cache; 1.70 + // return it for immediate refining. 1.71 + return card_ptr; 1.72 + } 1.73 + 1.74 + // Otherwise, the card is hot. 1.75 + jbyte* res = NULL; 1.76 + MutexLockerEx x(HotCardCache_lock, Mutex::_no_safepoint_check_flag); 1.77 + if (_n_hot == _hot_cache_size) { 1.78 + res = _hot_cache[_hot_cache_idx]; 1.79 + _n_hot--; 1.80 + } 1.81 + 1.82 + // Now _n_hot < _hot_cache_size, and we can insert at _hot_cache_idx. 1.83 + _hot_cache[_hot_cache_idx] = card_ptr; 1.84 + _hot_cache_idx++; 1.85 + 1.86 + if (_hot_cache_idx == _hot_cache_size) { 1.87 + // Wrap around 1.88 + _hot_cache_idx = 0; 1.89 + } 1.90 + _n_hot++; 1.91 + 1.92 + return res; 1.93 +} 1.94 + 1.95 +void G1HotCardCache::drain(uint worker_i, 1.96 + G1RemSet* g1rs, 1.97 + DirtyCardQueue* into_cset_dcq) { 1.98 + if (!default_use_cache()) { 1.99 + assert(_hot_cache == NULL, "Logic"); 1.100 + return; 1.101 + } 1.102 + 1.103 + assert(_hot_cache != NULL, "Logic"); 1.104 + assert(!use_cache(), "cache should be disabled"); 1.105 + int start_idx; 1.106 + 1.107 + while ((start_idx = _hot_cache_par_claimed_idx) < _n_hot) { // read once 1.108 + int end_idx = start_idx + _hot_cache_par_chunk_size; 1.109 + 1.110 + if (start_idx == 1.111 + Atomic::cmpxchg(end_idx, &_hot_cache_par_claimed_idx, start_idx)) { 1.112 + // The current worker has successfully claimed the chunk [start_idx..end_idx) 1.113 + end_idx = MIN2(end_idx, _n_hot); 1.114 + for (int i = start_idx; i < end_idx; i++) { 1.115 + jbyte* card_ptr = _hot_cache[i]; 1.116 + if (card_ptr != NULL) { 1.117 + if (g1rs->refine_card(card_ptr, worker_i, true)) { 1.118 + // The part of the heap spanned by the card contains references 1.119 + // that point into the current collection set. 1.120 + // We need to record the card pointer in the DirtyCardQueueSet 1.121 + // that we use for such cards. 1.122 + // 1.123 + // The only time we care about recording cards that contain 1.124 + // references that point into the collection set is during 1.125 + // RSet updating while within an evacuation pause. 1.126 + // In this case worker_i should be the id of a GC worker thread 1.127 + assert(SafepointSynchronize::is_at_safepoint(), "Should be at a safepoint"); 1.128 + assert(worker_i < (ParallelGCThreads == 0 ? 1 : ParallelGCThreads), 1.129 + err_msg("incorrect worker id: "UINT32_FORMAT, worker_i)); 1.130 + 1.131 + into_cset_dcq->enqueue(card_ptr); 1.132 + } 1.133 + } 1.134 + } 1.135 + } 1.136 + } 1.137 + // The existing entries in the hot card cache, which were just refined 1.138 + // above, are discarded prior to re-enabling the cache near the end of the GC. 1.139 +} 1.140 + 1.141 +void G1HotCardCache::resize_card_counts(size_t heap_capacity) { 1.142 + _card_counts.resize(heap_capacity); 1.143 +} 1.144 + 1.145 +void G1HotCardCache::reset_card_counts(HeapRegion* hr) { 1.146 + _card_counts.clear_region(hr); 1.147 +} 1.148 + 1.149 +void G1HotCardCache::reset_card_counts() { 1.150 + _card_counts.clear_all(); 1.151 +}