src/share/vm/gc_implementation/g1/g1HotCardCache.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7653
b6a1bf5222c5
parent 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
redestad@7653 2 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "gc_implementation/g1/dirtyCardQueue.hpp"
aoqi@0 27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
aoqi@0 28 #include "gc_implementation/g1/g1HotCardCache.hpp"
aoqi@0 29 #include "gc_implementation/g1/g1RemSet.hpp"
aoqi@0 30 #include "runtime/atomic.hpp"
aoqi@0 31
aoqi@0 32 G1HotCardCache::G1HotCardCache(G1CollectedHeap *g1h):
aoqi@0 33 _g1h(g1h), _hot_cache(NULL), _use_cache(false), _card_counts(g1h) {}
aoqi@0 34
tschatzl@7051 35 void G1HotCardCache::initialize(G1RegionToSpaceMapper* card_counts_storage) {
aoqi@0 36 if (default_use_cache()) {
aoqi@0 37 _use_cache = true;
aoqi@0 38
redestad@7653 39 _hot_cache_size = (size_t)1 << G1ConcRSLogCacheSize;
aoqi@0 40 _hot_cache = NEW_C_HEAP_ARRAY(jbyte*, _hot_cache_size, mtGC);
aoqi@0 41
redestad@7653 42 reset_hot_cache_internal();
aoqi@0 43
aoqi@0 44 // For refining the cards in the hot cache in parallel
mlarsson@7652 45 _hot_cache_par_chunk_size = (int)(ParallelGCThreads > 0 ? ClaimChunkSize : _hot_cache_size);
aoqi@0 46 _hot_cache_par_claimed_idx = 0;
aoqi@0 47
tschatzl@7051 48 _card_counts.initialize(card_counts_storage);
aoqi@0 49 }
aoqi@0 50 }
aoqi@0 51
aoqi@0 52 G1HotCardCache::~G1HotCardCache() {
aoqi@0 53 if (default_use_cache()) {
aoqi@0 54 assert(_hot_cache != NULL, "Logic");
aoqi@0 55 FREE_C_HEAP_ARRAY(jbyte*, _hot_cache, mtGC);
aoqi@0 56 }
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 jbyte* G1HotCardCache::insert(jbyte* card_ptr) {
aoqi@0 60 uint count = _card_counts.add_card_count(card_ptr);
aoqi@0 61 if (!_card_counts.is_hot(count)) {
aoqi@0 62 // The card is not hot so do not store it in the cache;
aoqi@0 63 // return it for immediate refining.
aoqi@0 64 return card_ptr;
aoqi@0 65 }
redestad@7653 66 // Otherwise, the card is hot.
redestad@7653 67 size_t index = Atomic::add_ptr((intptr_t)1, (volatile intptr_t*)&_hot_cache_idx) - 1;
redestad@7653 68 size_t masked_index = index & (_hot_cache_size - 1);
redestad@7653 69 jbyte* current_ptr = _hot_cache[masked_index];
aoqi@0 70
redestad@7653 71 // Try to store the new card pointer into the cache. Compare-and-swap to guard
redestad@7653 72 // against the unlikely event of a race resulting in another card pointer to
redestad@7653 73 // have already been written to the cache. In this case we will return
redestad@7653 74 // card_ptr in favor of the other option, which would be starting over. This
redestad@7653 75 // should be OK since card_ptr will likely be the older card already when/if
redestad@7653 76 // this ever happens.
redestad@7653 77 jbyte* previous_ptr = (jbyte*)Atomic::cmpxchg_ptr(card_ptr,
redestad@7653 78 &_hot_cache[masked_index],
redestad@7653 79 current_ptr);
redestad@7653 80 return (previous_ptr == current_ptr) ? previous_ptr : card_ptr;
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 void G1HotCardCache::drain(uint worker_i,
aoqi@0 84 G1RemSet* g1rs,
aoqi@0 85 DirtyCardQueue* into_cset_dcq) {
aoqi@0 86 if (!default_use_cache()) {
aoqi@0 87 assert(_hot_cache == NULL, "Logic");
aoqi@0 88 return;
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 assert(_hot_cache != NULL, "Logic");
aoqi@0 92 assert(!use_cache(), "cache should be disabled");
redestad@7653 93 while (_hot_cache_par_claimed_idx < _hot_cache_size) {
redestad@7653 94 size_t end_idx = Atomic::add_ptr((intptr_t)_hot_cache_par_chunk_size,
redestad@7653 95 (volatile intptr_t*)&_hot_cache_par_claimed_idx);
redestad@7653 96 size_t start_idx = end_idx - _hot_cache_par_chunk_size;
redestad@7653 97 // The current worker has successfully claimed the chunk [start_idx..end_idx)
redestad@7653 98 end_idx = MIN2(end_idx, _hot_cache_size);
redestad@7653 99 for (size_t i = start_idx; i < end_idx; i++) {
redestad@7653 100 jbyte* card_ptr = _hot_cache[i];
redestad@7653 101 if (card_ptr != NULL) {
redestad@7653 102 if (g1rs->refine_card(card_ptr, worker_i, true)) {
redestad@7653 103 // The part of the heap spanned by the card contains references
redestad@7653 104 // that point into the current collection set.
redestad@7653 105 // We need to record the card pointer in the DirtyCardQueueSet
redestad@7653 106 // that we use for such cards.
redestad@7653 107 //
redestad@7653 108 // The only time we care about recording cards that contain
redestad@7653 109 // references that point into the collection set is during
redestad@7653 110 // RSet updating while within an evacuation pause.
redestad@7653 111 // In this case worker_i should be the id of a GC worker thread
redestad@7653 112 assert(SafepointSynchronize::is_at_safepoint(), "Should be at a safepoint");
redestad@7653 113 assert(worker_i < ParallelGCThreads,
redestad@7653 114 err_msg("incorrect worker id: %u", worker_i));
aoqi@0 115
redestad@7653 116 into_cset_dcq->enqueue(card_ptr);
aoqi@0 117 }
redestad@7653 118 } else {
redestad@7653 119 break;
aoqi@0 120 }
aoqi@0 121 }
aoqi@0 122 }
redestad@7653 123
aoqi@0 124 // The existing entries in the hot card cache, which were just refined
aoqi@0 125 // above, are discarded prior to re-enabling the cache near the end of the GC.
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 void G1HotCardCache::reset_card_counts(HeapRegion* hr) {
aoqi@0 129 _card_counts.clear_region(hr);
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 void G1HotCardCache::reset_card_counts() {
aoqi@0 133 _card_counts.clear_all();
aoqi@0 134 }

mercurial