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