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