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