1 /*
2 * Copyright (c) 2000, 2018, 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 */
24
25 #include "precompiled.hpp"
26 #include "gc/shared/cardTableBarrierSetAssembler.hpp"
27 #include "gc/shared/cardTableBarrierSet.inline.hpp"
28 #include "gc/shared/collectedHeap.hpp"
29 #include "gc/shared/space.inline.hpp"
30 #include "logging/log.hpp"
31 #include "memory/virtualspace.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/thread.hpp"
34 #include "services/memTracker.hpp"
35 #include "utilities/align.hpp"
36 #include "utilities/macros.hpp"
37 #ifdef COMPILER1
38 #include "gc/shared/c1/cardTableBarrierSetC1.hpp"
39 #endif
40 #ifdef COMPILER2
41 #include "gc/shared/c2/cardTableBarrierSetC2.hpp"
42 #endif
43
44 class CardTableBarrierSetC1;
45 class CardTableBarrierSetC2;
46
47 // This kind of "BarrierSet" allows a "CollectedHeap" to detect and
48 // enumerate ref fields that have been modified (since the last
49 // enumeration.)
50
CardTableBarrierSet(BarrierSetAssembler * barrier_set_assembler,BarrierSetC1 * barrier_set_c1,BarrierSetC2 * barrier_set_c2,CardTable * card_table,const BarrierSet::FakeRtti & fake_rtti)51 CardTableBarrierSet::CardTableBarrierSet(BarrierSetAssembler* barrier_set_assembler,
52 BarrierSetC1* barrier_set_c1,
53 BarrierSetC2* barrier_set_c2,
54 CardTable* card_table,
55 const BarrierSet::FakeRtti& fake_rtti) :
56 ModRefBarrierSet(barrier_set_assembler,
57 barrier_set_c1,
58 barrier_set_c2,
59 fake_rtti.add_tag(BarrierSet::CardTableBarrierSet)),
60 _defer_initial_card_mark(false),
61 _card_table(card_table)
62 {}
63
CardTableBarrierSet(CardTable * card_table)64 CardTableBarrierSet::CardTableBarrierSet(CardTable* card_table) :
65 ModRefBarrierSet(make_barrier_set_assembler<CardTableBarrierSetAssembler>(),
66 make_barrier_set_c1<CardTableBarrierSetC1>(),
67 make_barrier_set_c2<CardTableBarrierSetC2>(),
68 BarrierSet::FakeRtti(BarrierSet::CardTableBarrierSet)),
69 _defer_initial_card_mark(false),
70 _card_table(card_table)
71 {}
72
initialize()73 void CardTableBarrierSet::initialize() {
74 initialize_deferred_card_mark_barriers();
75 }
76
~CardTableBarrierSet()77 CardTableBarrierSet::~CardTableBarrierSet() {
78 delete _card_table;
79 }
80
write_ref_array_work(MemRegion mr)81 void CardTableBarrierSet::write_ref_array_work(MemRegion mr) {
82 if (_card_table->scanned_concurrently()) {
83 // The array stores must not be reordered with the card marks if the
84 // card table is scanned concurrently.
85 OrderAccess::storestore();
86 }
87 _card_table->dirty_MemRegion(mr);
88 }
89
invalidate(MemRegion mr)90 void CardTableBarrierSet::invalidate(MemRegion mr) {
91 _card_table->invalidate(mr);
92 }
93
print_on(outputStream * st) const94 void CardTableBarrierSet::print_on(outputStream* st) const {
95 _card_table->print_on(st);
96 }
97
98 // Helper for ReduceInitialCardMarks. For performance,
99 // compiled code may elide card-marks for initializing stores
100 // to a newly allocated object along the fast-path. We
101 // compensate for such elided card-marks as follows:
102 // (a) Generational, non-concurrent collectors, such as
103 // GenCollectedHeap(ParNew,DefNew,Tenured) and
104 // ParallelScavengeHeap(ParallelGC, ParallelOldGC)
105 // need the card-mark if and only if the region is
106 // in the old gen, and do not care if the card-mark
107 // succeeds or precedes the initializing stores themselves,
108 // so long as the card-mark is completed before the next
109 // scavenge. For all these cases, we can do a card mark
110 // at the point at which we do a slow path allocation
111 // in the old gen, i.e. in this call.
112 // (b) GenCollectedHeap(ConcurrentMarkSweepGeneration) requires
113 // in addition that the card-mark for an old gen allocated
114 // object strictly follow any associated initializing stores.
115 // In these cases, the memRegion remembered below is
116 // used to card-mark the entire region either just before the next
117 // slow-path allocation by this thread or just before the next scavenge or
118 // CMS-associated safepoint, whichever of these events happens first.
119 // (The implicit assumption is that the object has been fully
120 // initialized by this point, a fact that we assert when doing the
121 // card-mark.)
122 // (c) G1CollectedHeap(G1) uses two kinds of write barriers. When a
123 // G1 concurrent marking is in progress an SATB (pre-write-)barrier
124 // is used to remember the pre-value of any store. Initializing
125 // stores will not need this barrier, so we need not worry about
126 // compensating for the missing pre-barrier here. Turning now
127 // to the post-barrier, we note that G1 needs a RS update barrier
128 // which simply enqueues a (sequence of) dirty cards which may
129 // optionally be refined by the concurrent update threads. Note
130 // that this barrier need only be applied to a non-young write,
131 // but, like in CMS, because of the presence of concurrent refinement
132 // (much like CMS' precleaning), must strictly follow the oop-store.
133 // Thus, using the same protocol for maintaining the intended
134 // invariants turns out, serendepitously, to be the same for both
135 // G1 and CMS.
136 //
137 // For any future collector, this code should be reexamined with
138 // that specific collector in mind, and the documentation above suitably
139 // extended and updated.
on_slowpath_allocation_exit(JavaThread * thread,oop new_obj)140 void CardTableBarrierSet::on_slowpath_allocation_exit(JavaThread* thread, oop new_obj) {
141 #if COMPILER2_OR_JVMCI
142 if (!ReduceInitialCardMarks) {
143 return;
144 }
145 // If a previous card-mark was deferred, flush it now.
146 flush_deferred_card_mark_barrier(thread);
147 if (new_obj->is_typeArray() || _card_table->is_in_young(new_obj)) {
148 // Arrays of non-references don't need a post-barrier.
149 // The deferred_card_mark region should be empty
150 // following the flush above.
151 assert(thread->deferred_card_mark().is_empty(), "Error");
152 } else {
153 MemRegion mr((HeapWord*)new_obj, new_obj->size());
154 assert(!mr.is_empty(), "Error");
155 if (_defer_initial_card_mark) {
156 // Defer the card mark
157 thread->set_deferred_card_mark(mr);
158 } else {
159 // Do the card mark
160 invalidate(mr);
161 }
162 }
163 #endif // COMPILER2_OR_JVMCI
164 }
165
initialize_deferred_card_mark_barriers()166 void CardTableBarrierSet::initialize_deferred_card_mark_barriers() {
167 // Used for ReduceInitialCardMarks (when COMPILER2 or JVMCI is used);
168 // otherwise remains unused.
169 #if COMPILER2_OR_JVMCI
170 _defer_initial_card_mark = is_server_compilation_mode_vm() && ReduceInitialCardMarks
171 && (DeferInitialCardMark || card_mark_must_follow_store());
172 #else
173 assert(_defer_initial_card_mark == false, "Who would set it?");
174 #endif
175 }
176
flush_deferred_card_mark_barrier(JavaThread * thread)177 void CardTableBarrierSet::flush_deferred_card_mark_barrier(JavaThread* thread) {
178 #if COMPILER2_OR_JVMCI
179 MemRegion deferred = thread->deferred_card_mark();
180 if (!deferred.is_empty()) {
181 assert(_defer_initial_card_mark, "Otherwise should be empty");
182 {
183 // Verify that the storage points to a parsable object in heap
184 DEBUG_ONLY(oop old_obj = oop(deferred.start());)
185 assert(!_card_table->is_in_young(old_obj),
186 "Else should have been filtered in on_slowpath_allocation_exit()");
187 assert(oopDesc::is_oop(old_obj, true), "Not an oop");
188 assert(deferred.word_size() == (size_t)(old_obj->size()),
189 "Mismatch: multiple objects?");
190 }
191 write_region(deferred);
192 // "Clear" the deferred_card_mark field
193 thread->set_deferred_card_mark(MemRegion());
194 }
195 assert(thread->deferred_card_mark().is_empty(), "invariant");
196 #else
197 assert(!_defer_initial_card_mark, "Should be false");
198 assert(thread->deferred_card_mark().is_empty(), "Should be empty");
199 #endif
200 }
201
on_thread_detach(JavaThread * thread)202 void CardTableBarrierSet::on_thread_detach(JavaThread* thread) {
203 // The deferred store barriers must all have been flushed to the
204 // card-table (or other remembered set structure) before GC starts
205 // processing the card-table (or other remembered set).
206 flush_deferred_card_mark_barrier(thread);
207 }
208
card_mark_must_follow_store() const209 bool CardTableBarrierSet::card_mark_must_follow_store() const {
210 return _card_table->scanned_concurrently();
211 }
212