1 /*
   2  * Copyright (c) 2000, 2015, 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/c1CardTableModRefBSCodeGen.hpp"
  28 #include "gc/shared/c2CardTableModRefBSCodeGen.hpp"
  29 #include "gc/shared/cardTableModRefBS.inline.hpp"
  30 
  31 // This kind of "BarrierSet" allows a "CollectedHeap" to detect and
  32 // enumerate ref fields that have been modified (since the last
  33 // enumeration.)
  34 
  35 void CardTableModRefBS::write_region(MemRegion mr) {
  36   _card_table->dirty_MemRegion(mr);
  37 }
  38 
  39 void CardTableModRefBS::write_ref_array_region(MemRegion mr) {
  40   _card_table->dirty_MemRegion(mr);
  41 }
  42 
  43 BarrierSetCodeGen* CardTableModRefBS::make_code_gen() {
  44   return new CardTableModRefBSCodeGen();
  45 }
  46 
  47 C1BarrierSetCodeGen* CardTableModRefBS::make_c1_code_gen() {
  48   return new C1CardTableModRefBSCodeGen();
  49 }
  50 
  51 C2BarrierSetCodeGen* CardTableModRefBS::make_c2_code_gen() {
  52   return new C2CardTableModRefBSCodeGen();
  53 }
  54 
  55 void CardTableModRefBS::initialize() {
  56   ModRefBarrierSet::initialize();
  57   // Used for ReduceInitialCardMarks (when COMPILER2 or JVMCI is used);
  58   // otherwise remains unused.
  59 #if defined(COMPILER2) || INCLUDE_JVMCI
  60   _can_elide_tlab_store_barriers = can_elide_tlab_store_barriers();
  61   _defer_initial_card_mark = is_server_compilation_mode_vm() && ReduceInitialCardMarks && _can_elide_tlab_store_barriers
  62                              && (DeferInitialCardMark || card_mark_must_follow_store());
  63 #else
  64   assert(_defer_initial_card_mark == false, "Who would set it?");
  65   assert(_can_elide_tlab_store_barriers == false, "Who would set it?");
  66 #endif
  67 }
  68 
  69 CardTableModRefBS::CardTableModRefBS(
  70   CardTable* card_table,
  71   const BarrierSet::FakeRtti& fake_rtti) :
  72   ModRefBarrierSet(fake_rtti.add_tag(BarrierSet::CardTableModRef)),
  73   _defer_initial_card_mark(false),
  74   _can_elide_tlab_store_barriers(false),
  75   _card_table(card_table)
  76 {}
  77 
  78 CardTableModRefBS::CardTableModRefBS(CardTable* card_table) :
  79   ModRefBarrierSet(BarrierSet::FakeRtti(BarrierSet::CardTableModRef)),
  80   _defer_initial_card_mark(false),
  81   _can_elide_tlab_store_barriers(false),
  82   _card_table(card_table)
  83 {}
  84 
  85 CardTableModRefBS::~CardTableModRefBS() {
  86   delete _card_table;
  87 }
  88 
  89 void CardTableModRefBS::invalidate(MemRegion mr) {
  90   _card_table->invalidate(mr);
  91 }
  92 
  93 void CardTableModRefBS::print_on(outputStream* st) const {
  94   _card_table->print_on(st);
  95 }
  96 
  97 // Helper for ReduceInitialCardMarks. For performance,
  98 // compiled code may elide card-marks for initializing stores
  99 // to a newly allocated object along the fast-path. We
 100 // compensate for such elided card-marks as follows:
 101 // (a) Generational, non-concurrent collectors, such as
 102 //     GenCollectedHeap(ParNew,DefNew,Tenured) and
 103 //     ParallelScavengeHeap(ParallelGC, ParallelOldGC)
 104 //     need the card-mark if and only if the region is
 105 //     in the old gen, and do not care if the card-mark
 106 //     succeeds or precedes the initializing stores themselves,
 107 //     so long as the card-mark is completed before the next
 108 //     scavenge. For all these cases, we can do a card mark
 109 //     at the point at which we do a slow path allocation
 110 //     in the old gen, i.e. in this call.
 111 // (b) GenCollectedHeap(ConcurrentMarkSweepGeneration) requires
 112 //     in addition that the card-mark for an old gen allocated
 113 //     object strictly follow any associated initializing stores.
 114 //     In these cases, the memRegion remembered below is
 115 //     used to card-mark the entire region either just before the next
 116 //     slow-path allocation by this thread or just before the next scavenge or
 117 //     CMS-associated safepoint, whichever of these events happens first.
 118 //     (The implicit assumption is that the object has been fully
 119 //     initialized by this point, a fact that we assert when doing the
 120 //     card-mark.)
 121 // (c) G1CollectedHeap(G1) uses two kinds of write barriers. When a
 122 //     G1 concurrent marking is in progress an SATB (pre-write-)barrier
 123 //     is used to remember the pre-value of any store. Initializing
 124 //     stores will not need this barrier, so we need not worry about
 125 //     compensating for the missing pre-barrier here. Turning now
 126 //     to the post-barrier, we note that G1 needs a RS update barrier
 127 //     which simply enqueues a (sequence of) dirty cards which may
 128 //     optionally be refined by the concurrent update threads. Note
 129 //     that this barrier need only be applied to a non-young write,
 130 //     but, like in CMS, because of the presence of concurrent refinement
 131 //     (much like CMS' precleaning), must strictly follow the oop-store.
 132 //     Thus, using the same protocol for maintaining the intended
 133 //     invariants turns out, serendepitously, to be the same for both
 134 //     G1 and CMS.
 135 //
 136 // For any future collector, this code should be reexamined with
 137 // that specific collector in mind, and the documentation above suitably
 138 // extended and updated.
 139 void CardTableModRefBS::new_deferred_store_barrier(JavaThread* thread, oop new_obj) {
 140   // If a previous card-mark was deferred, flush it now.
 141   flush_deferred_store_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       write_region(mr);
 156     }
 157   }
 158 }
 159 
 160 void CardTableModRefBS::flush_deferred_store_barrier(JavaThread* thread) {
 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 new_deferred_store_barrier()");
 169       assert(old_obj->is_oop(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 }
 179 
 180 void CardTableModRefBS::on_destroy_thread(JavaThread* thread) {
 181   ModRefBarrierSet::on_destroy_thread(thread);
 182   flush_deferred_store_barrier(thread);
 183 }
 184 
 185 void CardTableModRefBS::make_parsable(JavaThread* thread) {
 186   ModRefBarrierSet::make_parsable(thread);
 187 #if defined(COMPILER2) || INCLUDE_JVMCI
 188   // The deferred store barriers must all have been flushed to the
 189   // card-table (or other remembered set structure) before GC starts
 190   // processing the card-table (or other remembered set).
 191   if (_defer_initial_card_mark) flush_deferred_store_barrier(thread);
 192 #else
 193   assert(!_defer_initial_card_mark, "Should be false");
 194   assert(thread->deferred_card_mark().is_empty(), "Should be empty");
 195 #endif
 196 }
 197 
 198 bool CardTableModRefBS::card_mark_must_follow_store() const {
 199   return _card_table->scanned_concurrently();
 200 }