Mon, 08 Dec 2014 18:57:33 +0100
8067655: Clean up G1 remembered set oop iteration
Summary: Pass on the static type G1ParPushHeapRSClosure to allow oop_iterate devirtualization
Reviewed-by: jmasa, kbarrett
1 /*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 #include "precompiled.hpp"
26 #include "gc_implementation/g1/dirtyCardQueue.hpp"
27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
28 #include "gc_implementation/g1/g1HotCardCache.hpp"
29 #include "gc_implementation/g1/g1RemSet.hpp"
30 #include "runtime/atomic.hpp"
32 G1HotCardCache::G1HotCardCache(G1CollectedHeap *g1h):
33 _g1h(g1h), _hot_cache(NULL), _use_cache(false), _card_counts(g1h) {}
35 void G1HotCardCache::initialize(G1RegionToSpaceMapper* card_counts_storage) {
36 if (default_use_cache()) {
37 _use_cache = true;
39 _hot_cache_size = (1 << G1ConcRSLogCacheSize);
40 _hot_cache = NEW_C_HEAP_ARRAY(jbyte*, _hot_cache_size, mtGC);
42 _n_hot = 0;
43 _hot_cache_idx = 0;
45 // For refining the cards in the hot cache in parallel
46 uint n_workers = (ParallelGCThreads > 0 ?
47 _g1h->workers()->total_workers() : 1);
48 _hot_cache_par_chunk_size = MAX2(1, _hot_cache_size / (int)n_workers);
49 _hot_cache_par_claimed_idx = 0;
51 _card_counts.initialize(card_counts_storage);
52 }
53 }
55 G1HotCardCache::~G1HotCardCache() {
56 if (default_use_cache()) {
57 assert(_hot_cache != NULL, "Logic");
58 FREE_C_HEAP_ARRAY(jbyte*, _hot_cache, mtGC);
59 }
60 }
62 jbyte* G1HotCardCache::insert(jbyte* card_ptr) {
63 uint count = _card_counts.add_card_count(card_ptr);
64 if (!_card_counts.is_hot(count)) {
65 // The card is not hot so do not store it in the cache;
66 // return it for immediate refining.
67 return card_ptr;
68 }
70 // Otherwise, the card is hot.
71 jbyte* res = NULL;
72 MutexLockerEx x(HotCardCache_lock, Mutex::_no_safepoint_check_flag);
73 if (_n_hot == _hot_cache_size) {
74 res = _hot_cache[_hot_cache_idx];
75 _n_hot--;
76 }
78 // Now _n_hot < _hot_cache_size, and we can insert at _hot_cache_idx.
79 _hot_cache[_hot_cache_idx] = card_ptr;
80 _hot_cache_idx++;
82 if (_hot_cache_idx == _hot_cache_size) {
83 // Wrap around
84 _hot_cache_idx = 0;
85 }
86 _n_hot++;
88 return res;
89 }
91 void G1HotCardCache::drain(uint worker_i,
92 G1RemSet* g1rs,
93 DirtyCardQueue* into_cset_dcq) {
94 if (!default_use_cache()) {
95 assert(_hot_cache == NULL, "Logic");
96 return;
97 }
99 assert(_hot_cache != NULL, "Logic");
100 assert(!use_cache(), "cache should be disabled");
101 int start_idx;
103 while ((start_idx = _hot_cache_par_claimed_idx) < _n_hot) { // read once
104 int end_idx = start_idx + _hot_cache_par_chunk_size;
106 if (start_idx ==
107 Atomic::cmpxchg(end_idx, &_hot_cache_par_claimed_idx, start_idx)) {
108 // The current worker has successfully claimed the chunk [start_idx..end_idx)
109 end_idx = MIN2(end_idx, _n_hot);
110 for (int i = start_idx; i < end_idx; i++) {
111 jbyte* card_ptr = _hot_cache[i];
112 if (card_ptr != NULL) {
113 if (g1rs->refine_card(card_ptr, worker_i, true)) {
114 // The part of the heap spanned by the card contains references
115 // that point into the current collection set.
116 // We need to record the card pointer in the DirtyCardQueueSet
117 // that we use for such cards.
118 //
119 // The only time we care about recording cards that contain
120 // references that point into the collection set is during
121 // RSet updating while within an evacuation pause.
122 // In this case worker_i should be the id of a GC worker thread
123 assert(SafepointSynchronize::is_at_safepoint(), "Should be at a safepoint");
124 assert(worker_i < (ParallelGCThreads == 0 ? 1 : ParallelGCThreads),
125 err_msg("incorrect worker id: "UINT32_FORMAT, worker_i));
127 into_cset_dcq->enqueue(card_ptr);
128 }
129 }
130 }
131 }
132 }
133 // The existing entries in the hot card cache, which were just refined
134 // above, are discarded prior to re-enabling the cache near the end of the GC.
135 }
137 void G1HotCardCache::reset_card_counts(HeapRegion* hr) {
138 _card_counts.clear_region(hr);
139 }
141 void G1HotCardCache::reset_card_counts() {
142 _card_counts.clear_all();
143 }