ysr@777: /* brutisso@3923: * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. ysr@777: * ysr@777: */ ysr@777: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/g1/concurrentG1Refine.hpp" stefank@2314: #include "gc_implementation/g1/concurrentG1RefineThread.hpp" stefank@2314: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" stefank@2314: #include "gc_implementation/g1/g1CollectorPolicy.hpp" brutisso@3923: #include "gc_implementation/g1/g1GCPhaseTimes.hpp" stefank@2314: #include "gc_implementation/g1/g1RemSet.hpp" stefank@2314: #include "gc_implementation/g1/heapRegionSeq.inline.hpp" stefank@2314: #include "memory/space.inline.hpp" stefank@2314: #include "runtime/atomic.hpp" johnc@2713: #include "runtime/java.hpp" stefank@2314: #include "utilities/copy.hpp" ysr@777: johnc@1325: // Possible sizes for the card counts cache: odd primes that roughly double in size. johnc@1325: // (See jvmtiTagMap.cpp). johnc@2713: johnc@2713: #define MAX_SIZE ((size_t) -1) johnc@2713: johnc@2713: size_t ConcurrentG1Refine::_cc_cache_sizes[] = { johnc@2713: 16381, 32771, 76831, 150001, 307261, johnc@2713: 614563, 1228891, 2457733, 4915219, 9830479, johnc@2713: 19660831, 39321619, 78643219, 157286461, MAX_SIZE johnc@1325: }; johnc@1325: ysr@777: ConcurrentG1Refine::ConcurrentG1Refine() : johnc@1325: _card_counts(NULL), _card_epochs(NULL), johnc@2713: _n_card_counts(0), _max_cards(0), _max_n_card_counts(0), johnc@1325: _cache_size_index(0), _expand_card_counts(false), ysr@777: _hot_cache(NULL), ysr@777: _def_use_cache(false), _use_cache(false), tonyp@2716: // We initialize the epochs of the array to 0. By initializing tonyp@2716: // _n_periods to 1 and not 0 we automatically invalidate all the tonyp@2716: // entries on the array. Otherwise we might accidentally think that tonyp@2716: // we claimed a card that was in fact never set (see CR7033292). tonyp@2716: _n_periods(1), iveresov@1229: _threads(NULL), _n_threads(0) ysr@777: { iveresov@1546: iveresov@1546: // Ergomonically select initial concurrent refinement parameters tonyp@1717: if (FLAG_IS_DEFAULT(G1ConcRefinementGreenZone)) { tonyp@1717: FLAG_SET_DEFAULT(G1ConcRefinementGreenZone, MAX2(ParallelGCThreads, 1)); iveresov@1546: } tonyp@1717: set_green_zone(G1ConcRefinementGreenZone); iveresov@1546: tonyp@1717: if (FLAG_IS_DEFAULT(G1ConcRefinementYellowZone)) { tonyp@1717: FLAG_SET_DEFAULT(G1ConcRefinementYellowZone, green_zone() * 3); iveresov@1546: } tonyp@1717: set_yellow_zone(MAX2(G1ConcRefinementYellowZone, green_zone())); iveresov@1546: tonyp@1717: if (FLAG_IS_DEFAULT(G1ConcRefinementRedZone)) { tonyp@1717: FLAG_SET_DEFAULT(G1ConcRefinementRedZone, yellow_zone() * 2); iveresov@1546: } tonyp@1717: set_red_zone(MAX2(G1ConcRefinementRedZone, yellow_zone())); iveresov@1546: _n_worker_threads = thread_num(); iveresov@1546: // We need one extra thread to do the young gen rset size sampling. iveresov@1546: _n_threads = _n_worker_threads + 1; iveresov@1546: reset_threshold_step(); iveresov@1546: zgu@3900: _threads = NEW_C_HEAP_ARRAY(ConcurrentG1RefineThread*, _n_threads, mtGC); iveresov@1546: int worker_id_offset = (int)DirtyCardQueueSet::num_par_ids(); iveresov@1546: ConcurrentG1RefineThread *next = NULL; iveresov@1546: for (int i = _n_threads - 1; i >= 0; i--) { iveresov@1546: ConcurrentG1RefineThread* t = new ConcurrentG1RefineThread(this, next, worker_id_offset, i); iveresov@1546: assert(t != NULL, "Conc refine should have been created"); iveresov@1546: assert(t->cg1r() == this, "Conc refine thread should refer to this"); iveresov@1546: _threads[i] = t; iveresov@1546: next = t; ysr@777: } ysr@777: } ysr@777: iveresov@1546: void ConcurrentG1Refine::reset_threshold_step() { tonyp@1717: if (FLAG_IS_DEFAULT(G1ConcRefinementThresholdStep)) { iveresov@1546: _thread_threshold_step = (yellow_zone() - green_zone()) / (worker_thread_num() + 1); iveresov@1546: } else { tonyp@1717: _thread_threshold_step = G1ConcRefinementThresholdStep; iveresov@1230: } iveresov@1546: } iveresov@1546: iveresov@1546: int ConcurrentG1Refine::thread_num() { tonyp@1717: return MAX2((G1ConcRefinementThreads > 0) ? G1ConcRefinementThreads : ParallelGCThreads, 1); iveresov@1230: } iveresov@1230: ysr@777: void ConcurrentG1Refine::init() { johnc@1325: if (G1ConcRSLogCacheSize > 0) { johnc@1325: _g1h = G1CollectedHeap::heap(); johnc@2713: johnc@2713: _max_cards = _g1h->max_capacity() >> CardTableModRefBS::card_shift; johnc@2713: _max_n_card_counts = _max_cards * G1MaxHotCardCountSizePercent / 100; johnc@1325: johnc@1325: size_t max_card_num = ((size_t)1 << (sizeof(unsigned)*BitsPerByte-1)) - 1; johnc@2713: guarantee(_max_cards < max_card_num, "card_num representation"); johnc@1325: johnc@2713: // We need _n_card_counts to be less than _max_n_card_counts here johnc@2713: // so that the expansion call (below) actually allocates the johnc@2713: // _counts and _epochs arrays. johnc@2713: assert(_n_card_counts == 0, "pre-condition"); johnc@2713: assert(_max_n_card_counts > 0, "pre-condition"); johnc@2713: johnc@2713: // Find the index into cache size array that is of a size that's johnc@2713: // large enough to hold desired_sz. johnc@2713: size_t desired_sz = _max_cards / InitialCacheFraction; johnc@2713: int desired_sz_index = 0; johnc@2713: while (_cc_cache_sizes[desired_sz_index] < desired_sz) { johnc@2713: desired_sz_index += 1; johnc@2713: assert(desired_sz_index < MAX_CC_CACHE_INDEX, "invariant"); johnc@1325: } johnc@2713: assert(desired_sz_index < MAX_CC_CACHE_INDEX, "invariant"); johnc@1325: johnc@2713: // If the desired_sz value is between two sizes then johnc@2713: // _cc_cache_sizes[desired_sz_index-1] < desired_sz <= _cc_cache_sizes[desired_sz_index] johnc@2713: // we will start with the lower size in the optimistic expectation that johnc@2713: // we will not need to expand up. Note desired_sz_index could also be 0. johnc@2713: if (desired_sz_index > 0 && johnc@2713: _cc_cache_sizes[desired_sz_index] > desired_sz) { johnc@2713: desired_sz_index -= 1; johnc@2713: } johnc@1325: johnc@2713: if (!expand_card_count_cache(desired_sz_index)) { johnc@2713: // Allocation was unsuccessful - exit johnc@2713: vm_exit_during_initialization("Could not reserve enough space for card count cache"); johnc@2713: } johnc@2713: assert(_n_card_counts > 0, "post-condition"); johnc@2713: assert(_cache_size_index == desired_sz_index, "post-condition"); johnc@1325: johnc@1325: Copy::fill_to_bytes(&_card_counts[0], johnc@1325: _n_card_counts * sizeof(CardCountCacheEntry)); johnc@1325: Copy::fill_to_bytes(&_card_epochs[0], _n_card_counts * sizeof(CardEpochCacheEntry)); johnc@1325: johnc@1325: ModRefBarrierSet* bs = _g1h->mr_bs(); ysr@777: guarantee(bs->is_a(BarrierSet::CardTableModRef), "Precondition"); johnc@1325: _ct_bs = (CardTableModRefBS*)bs; johnc@1325: _ct_bot = _ct_bs->byte_for_const(_g1h->reserved_region().start()); johnc@1325: ysr@777: _def_use_cache = true; ysr@777: _use_cache = true; ysr@777: _hot_cache_size = (1 << G1ConcRSLogCacheSize); zgu@3900: _hot_cache = NEW_C_HEAP_ARRAY(jbyte*, _hot_cache_size, mtGC); ysr@777: _n_hot = 0; ysr@777: _hot_cache_idx = 0; johnc@1324: johnc@1324: // For refining the cards in the hot cache in parallel johnc@1324: int n_workers = (ParallelGCThreads > 0 ? johnc@1325: _g1h->workers()->total_workers() : 1); johnc@1324: _hot_cache_par_chunk_size = MAX2(1, _hot_cache_size / n_workers); johnc@1324: _hot_cache_par_claimed_idx = 0; ysr@777: } ysr@777: } ysr@777: iveresov@1229: void ConcurrentG1Refine::stop() { iveresov@1229: if (_threads != NULL) { iveresov@1229: for (int i = 0; i < _n_threads; i++) { iveresov@1229: _threads[i]->stop(); iveresov@1229: } iveresov@1229: } iveresov@1229: } iveresov@1229: iveresov@1546: void ConcurrentG1Refine::reinitialize_threads() { iveresov@1546: reset_threshold_step(); iveresov@1546: if (_threads != NULL) { iveresov@1546: for (int i = 0; i < _n_threads; i++) { iveresov@1546: _threads[i]->initialize(); iveresov@1546: } iveresov@1546: } iveresov@1546: } iveresov@1546: ysr@777: ConcurrentG1Refine::~ConcurrentG1Refine() { johnc@1325: if (G1ConcRSLogCacheSize > 0) { johnc@2713: // Please see the comment in allocate_card_count_cache johnc@2713: // for why we call os::malloc() and os::free() directly. ysr@777: assert(_card_counts != NULL, "Logic"); zgu@3900: os::free(_card_counts, mtGC); johnc@1325: assert(_card_epochs != NULL, "Logic"); zgu@3900: os::free(_card_epochs, mtGC); johnc@2713: ysr@777: assert(_hot_cache != NULL, "Logic"); zgu@3900: FREE_C_HEAP_ARRAY(jbyte*, _hot_cache, mtGC); ysr@777: } iveresov@1229: if (_threads != NULL) { iveresov@1229: for (int i = 0; i < _n_threads; i++) { iveresov@1229: delete _threads[i]; iveresov@1229: } zgu@3900: FREE_C_HEAP_ARRAY(ConcurrentG1RefineThread*, _threads, mtGC); ysr@777: } ysr@777: } ysr@777: iveresov@1229: void ConcurrentG1Refine::threads_do(ThreadClosure *tc) { iveresov@1229: if (_threads != NULL) { iveresov@1229: for (int i = 0; i < _n_threads; i++) { iveresov@1229: tc->do_thread(_threads[i]); iveresov@1229: } ysr@777: } ysr@777: } ysr@777: johnc@1325: bool ConcurrentG1Refine::is_young_card(jbyte* card_ptr) { johnc@1325: HeapWord* start = _ct_bs->addr_for(card_ptr); johnc@1325: HeapRegion* r = _g1h->heap_region_containing(start); johnc@1325: if (r != NULL && r->is_young()) { johnc@1325: return true; johnc@1325: } johnc@1325: // This card is not associated with a heap region johnc@1325: // so can't be young. johnc@1325: return false; ysr@777: } ysr@777: johnc@1325: jbyte* ConcurrentG1Refine::add_card_count(jbyte* card_ptr, int* count, bool* defer) { johnc@1325: unsigned new_card_num = ptr_2_card_num(card_ptr); johnc@1325: unsigned bucket = hash(new_card_num); johnc@1325: assert(0 <= bucket && bucket < _n_card_counts, "Bounds"); johnc@1325: johnc@1325: CardCountCacheEntry* count_ptr = &_card_counts[bucket]; johnc@1325: CardEpochCacheEntry* epoch_ptr = &_card_epochs[bucket]; johnc@1325: johnc@1325: // We have to construct a new entry if we haven't updated the counts johnc@1325: // during the current period, or if the count was updated for a johnc@1325: // different card number. johnc@1325: unsigned int new_epoch = (unsigned int) _n_periods; johnc@1325: julong new_epoch_entry = make_epoch_entry(new_card_num, new_epoch); johnc@1325: johnc@1325: while (true) { johnc@1325: // Fetch the previous epoch value johnc@1325: julong prev_epoch_entry = epoch_ptr->_value; johnc@1325: julong cas_res; johnc@1325: johnc@1325: if (extract_epoch(prev_epoch_entry) != new_epoch) { johnc@1325: // This entry has not yet been updated during this period. johnc@1325: // Note: we update the epoch value atomically to ensure johnc@1325: // that there is only one winner that updates the cached johnc@1325: // card_ptr value even though all the refine threads share johnc@1325: // the same epoch value. johnc@1325: johnc@1325: cas_res = (julong) Atomic::cmpxchg((jlong) new_epoch_entry, johnc@1325: (volatile jlong*)&epoch_ptr->_value, johnc@1325: (jlong) prev_epoch_entry); johnc@1325: johnc@1325: if (cas_res == prev_epoch_entry) { johnc@1325: // We have successfully won the race to update the johnc@1325: // epoch and card_num value. Make it look like the johnc@1325: // count and eviction count were previously cleared. johnc@1325: count_ptr->_count = 1; johnc@1325: count_ptr->_evict_count = 0; johnc@1325: *count = 0; johnc@1325: // We can defer the processing of card_ptr johnc@1325: *defer = true; johnc@1325: return card_ptr; johnc@1325: } johnc@1325: // We did not win the race to update the epoch field, so some other johnc@1325: // thread must have done it. The value that gets returned by CAS johnc@1325: // should be the new epoch value. johnc@1325: assert(extract_epoch(cas_res) == new_epoch, "unexpected epoch"); johnc@1325: // We could 'continue' here or just re-read the previous epoch value johnc@1325: prev_epoch_entry = epoch_ptr->_value; johnc@1325: } johnc@1325: johnc@1325: // The epoch entry for card_ptr has been updated during this period. johnc@1325: unsigned old_card_num = extract_card_num(prev_epoch_entry); johnc@1325: johnc@1325: // The card count that will be returned to caller johnc@1325: *count = count_ptr->_count; johnc@1325: johnc@1325: // Are we updating the count for the same card? johnc@1325: if (new_card_num == old_card_num) { johnc@1325: // Same card - just update the count. We could have more than one johnc@1325: // thread racing to update count for the current card. It should be johnc@1325: // OK not to use a CAS as the only penalty should be some missed johnc@1325: // increments of the count which delays identifying the card as "hot". johnc@1325: johnc@1325: if (*count < max_jubyte) count_ptr->_count++; johnc@1325: // We can defer the processing of card_ptr johnc@1325: *defer = true; johnc@1325: return card_ptr; johnc@1325: } johnc@1325: johnc@1325: // Different card - evict old card info johnc@1325: if (count_ptr->_evict_count < max_jubyte) count_ptr->_evict_count++; johnc@1325: if (count_ptr->_evict_count > G1CardCountCacheExpandThreshold) { johnc@1325: // Trigger a resize the next time we clear johnc@1325: _expand_card_counts = true; johnc@1325: } johnc@1325: johnc@1325: cas_res = (julong) Atomic::cmpxchg((jlong) new_epoch_entry, johnc@1325: (volatile jlong*)&epoch_ptr->_value, johnc@1325: (jlong) prev_epoch_entry); johnc@1325: johnc@1325: if (cas_res == prev_epoch_entry) { johnc@1325: // We successfully updated the card num value in the epoch entry johnc@1325: count_ptr->_count = 0; // initialize counter for new card num johnc@2021: jbyte* old_card_ptr = card_num_2_ptr(old_card_num); johnc@1325: johnc@1325: // Even though the region containg the card at old_card_num was not johnc@1325: // in the young list when old_card_num was recorded in the epoch johnc@1325: // cache it could have been added to the free list and subsequently johnc@2021: // added to the young list in the intervening time. See CR 6817995. johnc@2021: // We do not deal with this case here - it will be handled in johnc@2021: // HeapRegion::oops_on_card_seq_iterate_careful after it has been johnc@2021: // determined that the region containing the card has been allocated johnc@2021: // to, and it's safe to check the young type of the region. johnc@1325: johnc@1325: // We do not want to defer processing of card_ptr in this case johnc@1325: // (we need to refine old_card_ptr and card_ptr) johnc@1325: *defer = false; johnc@1325: return old_card_ptr; johnc@1325: } johnc@1325: // Someone else beat us - try again. johnc@1325: } johnc@1325: } johnc@1325: johnc@1325: jbyte* ConcurrentG1Refine::cache_insert(jbyte* card_ptr, bool* defer) { johnc@1325: int count; johnc@1325: jbyte* cached_ptr = add_card_count(card_ptr, &count, defer); johnc@1325: assert(cached_ptr != NULL, "bad cached card ptr"); johnc@1681: johnc@2021: // We've just inserted a card pointer into the card count cache johnc@2021: // and got back the card that we just inserted or (evicted) the johnc@2021: // previous contents of that count slot. johnc@1681: johnc@2021: // The card we got back could be in a young region. When the johnc@2021: // returned card (if evicted) was originally inserted, we had johnc@2021: // determined that its containing region was not young. However johnc@2021: // it is possible for the region to be freed during a cleanup johnc@2021: // pause, then reallocated and tagged as young which will result johnc@2021: // in the returned card residing in a young region. johnc@2021: // johnc@2021: // We do not deal with this case here - the change from non-young johnc@2021: // to young could be observed at any time - it will be handled in johnc@2021: // HeapRegion::oops_on_card_seq_iterate_careful after it has been johnc@2021: // determined that the region containing the card has been allocated johnc@2021: // to. johnc@1325: johnc@1325: // The card pointer we obtained from card count cache is not hot johnc@1325: // so do not store it in the cache; return it for immediate johnc@1325: // refining. ysr@777: if (count < G1ConcRSHotCardLimit) { johnc@1325: return cached_ptr; ysr@777: } johnc@1325: johnc@2021: // Otherwise, the pointer we got from the _card_counts cache is hot. ysr@777: jbyte* res = NULL; ysr@777: MutexLockerEx x(HotCardCache_lock, Mutex::_no_safepoint_check_flag); ysr@777: if (_n_hot == _hot_cache_size) { ysr@777: res = _hot_cache[_hot_cache_idx]; ysr@777: _n_hot--; ysr@777: } ysr@777: // Now _n_hot < _hot_cache_size, and we can insert at _hot_cache_idx. johnc@1325: _hot_cache[_hot_cache_idx] = cached_ptr; ysr@777: _hot_cache_idx++; ysr@777: if (_hot_cache_idx == _hot_cache_size) _hot_cache_idx = 0; ysr@777: _n_hot++; johnc@1325: johnc@2021: // The card obtained from the hot card cache could be in a young johnc@2021: // region. See above on how this can happen. johnc@1325: ysr@777: return res; ysr@777: } ysr@777: johnc@2060: void ConcurrentG1Refine::clean_up_cache(int worker_i, johnc@2060: G1RemSet* g1rs, johnc@2060: DirtyCardQueue* into_cset_dcq) { ysr@777: assert(!use_cache(), "cache should be disabled"); johnc@1324: int start_idx; johnc@1324: johnc@1324: while ((start_idx = _hot_cache_par_claimed_idx) < _n_hot) { // read once johnc@1324: int end_idx = start_idx + _hot_cache_par_chunk_size; johnc@1324: johnc@1324: if (start_idx == johnc@1324: Atomic::cmpxchg(end_idx, &_hot_cache_par_claimed_idx, start_idx)) { johnc@1324: // The current worker has successfully claimed the chunk [start_idx..end_idx) johnc@1324: end_idx = MIN2(end_idx, _n_hot); johnc@1324: for (int i = start_idx; i < end_idx; i++) { johnc@1324: jbyte* entry = _hot_cache[i]; johnc@1324: if (entry != NULL) { johnc@2060: if (g1rs->concurrentRefineOneCard(entry, worker_i, true)) { johnc@2060: // 'entry' contains references that point into the current johnc@2060: // collection set. We need to record 'entry' in the DCQS johnc@2060: // that's used for that purpose. johnc@2060: // johnc@2060: // The only time we care about recording cards that contain johnc@2060: // references that point into the collection set is during johnc@2060: // RSet updating while within an evacuation pause. johnc@2060: // In this case worker_i should be the id of a GC worker thread johnc@2060: assert(SafepointSynchronize::is_at_safepoint(), "not during an evacuation pause"); brutisso@2646: assert(worker_i < (int) (ParallelGCThreads == 0 ? 1 : ParallelGCThreads), "incorrect worker id"); johnc@2060: into_cset_dcq->enqueue(entry); johnc@2060: } johnc@1324: } johnc@1324: } ysr@777: } ysr@777: } ysr@777: } ysr@777: johnc@2713: // The arrays used to hold the card counts and the epochs must have johnc@2713: // a 1:1 correspondence. Hence they are allocated and freed together johnc@2713: // Returns true if the allocations of both the counts and epochs johnc@2713: // were successful; false otherwise. johnc@2713: bool ConcurrentG1Refine::allocate_card_count_cache(size_t n, johnc@2713: CardCountCacheEntry** counts, johnc@2713: CardEpochCacheEntry** epochs) { johnc@2713: // We call the allocation/free routines directly for the counts johnc@2713: // and epochs arrays. The NEW_C_HEAP_ARRAY/FREE_C_HEAP_ARRAY johnc@2713: // macros call AllocateHeap and FreeHeap respectively. johnc@2713: // AllocateHeap will call vm_exit_out_of_memory in the event johnc@2713: // of an allocation failure and abort the JVM. With the johnc@2713: // _counts/epochs arrays we only need to abort the JVM if the johnc@2713: // initial allocation of these arrays fails. johnc@2713: // johnc@2713: // Additionally AllocateHeap/FreeHeap do some tracing of johnc@2713: // allocate/free calls so calling one without calling the johnc@2713: // other can cause inconsistencies in the tracing. So we johnc@2713: // call neither. johnc@2713: johnc@2713: assert(*counts == NULL, "out param"); johnc@2713: assert(*epochs == NULL, "out param"); johnc@2713: johnc@2713: size_t counts_size = n * sizeof(CardCountCacheEntry); johnc@2713: size_t epochs_size = n * sizeof(CardEpochCacheEntry); johnc@2713: zgu@3900: *counts = (CardCountCacheEntry*) os::malloc(counts_size, mtGC); johnc@2713: if (*counts == NULL) { johnc@2713: // allocation was unsuccessful johnc@2713: return false; johnc@2713: } johnc@2713: zgu@3900: *epochs = (CardEpochCacheEntry*) os::malloc(epochs_size, mtGC); johnc@2713: if (*epochs == NULL) { johnc@2713: // allocation was unsuccessful - free counts array johnc@2713: assert(*counts != NULL, "must be"); zgu@3900: os::free(*counts, mtGC); johnc@2713: *counts = NULL; johnc@2713: return false; johnc@2713: } johnc@2713: johnc@2713: // We successfully allocated both counts and epochs johnc@2713: return true; johnc@2713: } johnc@2713: johnc@2713: // Returns true if the card counts/epochs cache was johnc@2713: // successfully expanded; false otherwise. johnc@2713: bool ConcurrentG1Refine::expand_card_count_cache(int cache_size_idx) { johnc@2713: // Can we expand the card count and epoch tables? johnc@1325: if (_n_card_counts < _max_n_card_counts) { johnc@2713: assert(cache_size_idx >= 0 && cache_size_idx < MAX_CC_CACHE_INDEX, "oob"); johnc@1325: johnc@2713: size_t cache_size = _cc_cache_sizes[cache_size_idx]; johnc@1325: // Make sure we don't go bigger than we will ever need johnc@2713: cache_size = MIN2(cache_size, _max_n_card_counts); johnc@1325: johnc@2713: // Should we expand the card count and card epoch tables? johnc@2713: if (cache_size > _n_card_counts) { johnc@2713: // We have been asked to allocate new, larger, arrays for johnc@2713: // the card counts and the epochs. Attempt the allocation johnc@2713: // of both before we free the existing arrays in case johnc@2713: // the allocation is unsuccessful... johnc@2713: CardCountCacheEntry* counts = NULL; johnc@2713: CardEpochCacheEntry* epochs = NULL; johnc@2713: johnc@2713: if (allocate_card_count_cache(cache_size, &counts, &epochs)) { johnc@2713: // Allocation was successful. johnc@2713: // We can just free the old arrays; we're johnc@2713: // not interested in preserving the contents zgu@3900: if (_card_counts != NULL) os::free(_card_counts, mtGC); zgu@3900: if (_card_epochs != NULL) os::free(_card_epochs, mtGC); johnc@2713: johnc@2713: // Cache the size of the arrays and the index that got us there. johnc@2713: _n_card_counts = cache_size; johnc@2713: _cache_size_index = cache_size_idx; johnc@2713: johnc@2713: _card_counts = counts; johnc@2713: _card_epochs = epochs; johnc@2713: johnc@2713: // We successfully allocated/expanded the caches. johnc@2713: return true; johnc@2713: } ysr@777: } ysr@777: } johnc@2713: johnc@2713: // We did not successfully expand the caches. johnc@2713: return false; ysr@777: } ysr@777: johnc@1325: void ConcurrentG1Refine::clear_and_record_card_counts() { brutisso@3923: if (G1ConcRSLogCacheSize == 0) { brutisso@3923: return; brutisso@3923: } johnc@1325: johnc@1325: double start = os::elapsedTime(); johnc@1325: johnc@1325: if (_expand_card_counts) { johnc@2713: int new_idx = _cache_size_index + 1; johnc@2713: johnc@2713: if (expand_card_count_cache(new_idx)) { johnc@2713: // Allocation was successful and _n_card_counts has johnc@2713: // been updated to the new size. We only need to clear johnc@2713: // the epochs so we don't read a bogus epoch value johnc@2713: // when inserting a card into the hot card cache. johnc@2713: Copy::fill_to_bytes(&_card_epochs[0], _n_card_counts * sizeof(CardEpochCacheEntry)); johnc@2713: } johnc@1325: _expand_card_counts = false; ysr@777: } ysr@777: johnc@1325: int this_epoch = (int) _n_periods; johnc@1325: assert((this_epoch+1) <= max_jint, "to many periods"); johnc@1325: // Update epoch johnc@1325: _n_periods++; brutisso@3923: double cc_clear_time_ms = (os::elapsedTime() - start) * 1000; brutisso@3923: _g1h->g1_policy()->phase_times()->record_cc_clear_time_ms(cc_clear_time_ms); ysr@777: } tonyp@1454: tonyp@1454: void ConcurrentG1Refine::print_worker_threads_on(outputStream* st) const { tonyp@1454: for (int i = 0; i < _n_threads; ++i) { tonyp@1454: _threads[i]->print_on(st); tonyp@1454: st->cr(); tonyp@1454: } tonyp@1454: }