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