johnc@5078: /* johnc@5078: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. johnc@5078: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. johnc@5078: * johnc@5078: * This code is free software; you can redistribute it and/or modify it johnc@5078: * under the terms of the GNU General Public License version 2 only, as johnc@5078: * published by the Free Software Foundation. johnc@5078: * johnc@5078: * This code is distributed in the hope that it will be useful, but WITHOUT johnc@5078: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or johnc@5078: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License johnc@5078: * version 2 for more details (a copy is included in the LICENSE file that johnc@5078: * accompanied this code). johnc@5078: * johnc@5078: * You should have received a copy of the GNU General Public License version johnc@5078: * 2 along with this work; if not, write to the Free Software Foundation, johnc@5078: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. johnc@5078: * johnc@5078: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA johnc@5078: * or visit www.oracle.com if you need additional information or have any johnc@5078: * questions. johnc@5078: * johnc@5078: */ johnc@5078: johnc@5078: #include "precompiled.hpp" johnc@5078: #include "gc_implementation/g1/dirtyCardQueue.hpp" johnc@5078: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" johnc@5078: #include "gc_implementation/g1/g1HotCardCache.hpp" johnc@5078: #include "gc_implementation/g1/g1RemSet.hpp" johnc@5078: #include "gc_implementation/g1/heapRegion.hpp" johnc@5078: #include "runtime/atomic.hpp" johnc@5078: johnc@5078: G1HotCardCache::G1HotCardCache(G1CollectedHeap *g1h): johnc@5078: _g1h(g1h), _hot_cache(NULL), _use_cache(false), _card_counts(g1h) {} johnc@5078: johnc@5078: void G1HotCardCache::initialize() { johnc@5078: if (default_use_cache()) { johnc@5078: _use_cache = true; johnc@5078: johnc@5078: _hot_cache_size = (1 << G1ConcRSLogCacheSize); johnc@5078: _hot_cache = NEW_C_HEAP_ARRAY(jbyte*, _hot_cache_size, mtGC); johnc@5078: johnc@5078: _n_hot = 0; johnc@5078: _hot_cache_idx = 0; johnc@5078: johnc@5078: // For refining the cards in the hot cache in parallel johnc@5078: int n_workers = (ParallelGCThreads > 0 ? johnc@5078: _g1h->workers()->total_workers() : 1); johnc@5078: _hot_cache_par_chunk_size = MAX2(1, _hot_cache_size / n_workers); johnc@5078: _hot_cache_par_claimed_idx = 0; johnc@5078: johnc@5078: _card_counts.initialize(); johnc@5078: } johnc@5078: } johnc@5078: johnc@5078: G1HotCardCache::~G1HotCardCache() { johnc@5078: if (default_use_cache()) { johnc@5078: assert(_hot_cache != NULL, "Logic"); johnc@5078: FREE_C_HEAP_ARRAY(jbyte*, _hot_cache, mtGC); johnc@5078: } johnc@5078: } johnc@5078: johnc@5078: jbyte* G1HotCardCache::insert(jbyte* card_ptr) { johnc@5078: uint count = _card_counts.add_card_count(card_ptr); johnc@5078: if (!_card_counts.is_hot(count)) { johnc@5078: // The card is not hot so do not store it in the cache; johnc@5078: // return it for immediate refining. johnc@5078: return card_ptr; johnc@5078: } johnc@5078: johnc@5078: // Otherwise, the card is hot. johnc@5078: jbyte* res = NULL; johnc@5078: MutexLockerEx x(HotCardCache_lock, Mutex::_no_safepoint_check_flag); johnc@5078: if (_n_hot == _hot_cache_size) { johnc@5078: res = _hot_cache[_hot_cache_idx]; johnc@5078: _n_hot--; johnc@5078: } johnc@5078: johnc@5078: // Now _n_hot < _hot_cache_size, and we can insert at _hot_cache_idx. johnc@5078: _hot_cache[_hot_cache_idx] = card_ptr; johnc@5078: _hot_cache_idx++; johnc@5078: johnc@5078: if (_hot_cache_idx == _hot_cache_size) { johnc@5078: // Wrap around johnc@5078: _hot_cache_idx = 0; johnc@5078: } johnc@5078: _n_hot++; johnc@5078: johnc@5078: return res; johnc@5078: } johnc@5078: johnc@5078: void G1HotCardCache::drain(int worker_i, johnc@5078: G1RemSet* g1rs, johnc@5078: DirtyCardQueue* into_cset_dcq) { johnc@5078: if (!default_use_cache()) { johnc@5078: assert(_hot_cache == NULL, "Logic"); johnc@5078: return; johnc@5078: } johnc@5078: johnc@5078: assert(_hot_cache != NULL, "Logic"); johnc@5078: assert(!use_cache(), "cache should be disabled"); johnc@5078: int start_idx; johnc@5078: johnc@5078: while ((start_idx = _hot_cache_par_claimed_idx) < _n_hot) { // read once johnc@5078: int end_idx = start_idx + _hot_cache_par_chunk_size; johnc@5078: johnc@5078: if (start_idx == johnc@5078: Atomic::cmpxchg(end_idx, &_hot_cache_par_claimed_idx, start_idx)) { johnc@5078: // The current worker has successfully claimed the chunk [start_idx..end_idx) johnc@5078: end_idx = MIN2(end_idx, _n_hot); johnc@5078: for (int i = start_idx; i < end_idx; i++) { johnc@5078: jbyte* card_ptr = _hot_cache[i]; johnc@5078: if (card_ptr != NULL) { johnc@5078: if (g1rs->refine_card(card_ptr, worker_i, true)) { johnc@5078: // The part of the heap spanned by the card contains references johnc@5078: // that point into the current collection set. johnc@5078: // We need to record the card pointer in the DirtyCardQueueSet johnc@5078: // that we use for such cards. johnc@5078: // johnc@5078: // The only time we care about recording cards that contain johnc@5078: // references that point into the collection set is during johnc@5078: // RSet updating while within an evacuation pause. johnc@5078: // In this case worker_i should be the id of a GC worker thread johnc@5078: assert(SafepointSynchronize::is_at_safepoint(), "Should be at a safepoint"); johnc@5078: assert(worker_i < (int) (ParallelGCThreads == 0 ? 1 : ParallelGCThreads), johnc@5078: err_msg("incorrect worker id: "INT32_FORMAT, worker_i)); johnc@5078: johnc@5078: into_cset_dcq->enqueue(card_ptr); johnc@5078: } johnc@5078: } johnc@5078: } johnc@5078: } johnc@5078: } johnc@5078: // The existing entries in the hot card cache, which were just refined johnc@5078: // above, are discarded prior to re-enabling the cache near the end of the GC. johnc@5078: } johnc@5078: johnc@5078: void G1HotCardCache::resize_card_counts(size_t heap_capacity) { johnc@5078: _card_counts.resize(heap_capacity); johnc@5078: } johnc@5078: johnc@5078: void G1HotCardCache::reset_card_counts(HeapRegion* hr) { johnc@5078: _card_counts.clear_region(hr); johnc@5078: } johnc@5078: johnc@5078: void G1HotCardCache::reset_card_counts() { johnc@5078: _card_counts.clear_all(); johnc@5078: }