1 /*
   2  * Copyright (c) 2013, 2020, Red Hat, Inc. 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/shenandoah/shenandoahAsserts.hpp"
  27 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  28 #include "gc/shenandoah/shenandoahBarrierSetClone.inline.hpp"
  29 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
  30 #include "gc/shenandoah/shenandoahBarrierSetNMethod.hpp"
  31 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  32 #include "gc/shenandoah/shenandoahConcurrentRoots.hpp"
  33 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  34 #include "gc/shenandoah/shenandoahHeuristics.hpp"
  35 #include "memory/iterator.inline.hpp"
  36 #include "runtime/interfaceSupport.inline.hpp"
  37 #ifdef COMPILER1
  38 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
  39 #endif
  40 #ifdef COMPILER2
  41 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
  42 #endif
  43 
  44 class ShenandoahBarrierSetC1;
  45 class ShenandoahBarrierSetC2;
  46 
  47 static BarrierSetNMethod* make_barrier_set_nmethod(ShenandoahHeap* heap) {
  48   // NMethod barriers are only used when concurrent nmethod unloading is enabled
  49   if (!ShenandoahConcurrentRoots::can_do_concurrent_class_unloading()) {
  50     return NULL;
  51   }
  52   return new ShenandoahBarrierSetNMethod(heap);
  53 }
  54 
  55 ShenandoahBarrierSet::ShenandoahBarrierSet(ShenandoahHeap* heap) :
  56   BarrierSet(make_barrier_set_assembler<ShenandoahBarrierSetAssembler>(),
  57              make_barrier_set_c1<ShenandoahBarrierSetC1>(),
  58              make_barrier_set_c2<ShenandoahBarrierSetC2>(),
  59              make_barrier_set_nmethod(heap),
  60              BarrierSet::FakeRtti(BarrierSet::ShenandoahBarrierSet)),
  61   _heap(heap),
  62   _satb_mark_queue_buffer_allocator("SATB Buffer Allocator", ShenandoahSATBBufferSize),
  63   _satb_mark_queue_set(&_satb_mark_queue_buffer_allocator)
  64 {
  65 }
  66 
  67 ShenandoahBarrierSetAssembler* ShenandoahBarrierSet::assembler() {
  68   BarrierSetAssembler* const bsa = BarrierSet::barrier_set()->barrier_set_assembler();
  69   return reinterpret_cast<ShenandoahBarrierSetAssembler*>(bsa);
  70 }
  71 
  72 void ShenandoahBarrierSet::print_on(outputStream* st) const {
  73   st->print("ShenandoahBarrierSet");
  74 }
  75 
  76 bool ShenandoahBarrierSet::is_a(BarrierSet::Name bsn) {
  77   return bsn == BarrierSet::ShenandoahBarrierSet;
  78 }
  79 
  80 bool ShenandoahBarrierSet::is_aligned(HeapWord* hw) {
  81   return true;
  82 }
  83 
  84 bool ShenandoahBarrierSet::need_load_reference_barrier(DecoratorSet decorators, BasicType type) {
  85   if (!ShenandoahLoadRefBarrier) return false;
  86   // Only needed for references
  87   return is_reference_type(type);
  88 }
  89 
  90 bool ShenandoahBarrierSet::use_load_reference_barrier_native(DecoratorSet decorators, BasicType type) {
  91   assert(need_load_reference_barrier(decorators, type), "Should be subset of LRB");
  92   assert(is_reference_type(type), "Why we here?");
  93   // Native load reference barrier is only needed for concurrent root processing
  94   if (!ShenandoahConcurrentRoots::can_do_concurrent_roots()) {
  95     return false;
  96   }
  97 
  98   return (decorators & IN_NATIVE) != 0;
  99 }
 100 
 101 bool ShenandoahBarrierSet::need_keep_alive_barrier(DecoratorSet decorators,BasicType type) {
 102   if (!ShenandoahSATBBarrier) return false;
 103   // Only needed for references
 104   if (!is_reference_type(type)) return false;
 105 
 106   bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0;
 107   bool unknown = (decorators & ON_UNKNOWN_OOP_REF) != 0;
 108   bool on_weak_ref = (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) != 0;
 109   return (on_weak_ref || unknown) && keep_alive;
 110 }
 111 
 112 oop ShenandoahBarrierSet::load_reference_barrier_not_null(oop obj) {
 113   if (ShenandoahLoadRefBarrier && _heap->has_forwarded_objects()) {
 114     return load_reference_barrier_impl(obj);
 115   } else {
 116     return obj;
 117   }
 118 }
 119 
 120 oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
 121   if (obj != NULL) {
 122     return load_reference_barrier_not_null(obj);
 123   } else {
 124     return obj;
 125   }
 126 }
 127 
 128 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, oop* load_addr) {
 129   return load_reference_barrier_mutator_work(obj, load_addr);
 130 }
 131 
 132 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, narrowOop* load_addr) {
 133   return load_reference_barrier_mutator_work(obj, load_addr);
 134 }
 135 
 136 template <class T>
 137 oop ShenandoahBarrierSet::load_reference_barrier_mutator_work(oop obj, T* load_addr) {
 138   assert(ShenandoahLoadRefBarrier, "should be enabled");
 139   shenandoah_assert_in_cset(load_addr, obj);
 140 
 141   oop fwd = resolve_forwarded_not_null_mutator(obj);
 142   if (obj == fwd) {
 143     assert(_heap->is_evacuation_in_progress(),
 144            "evac should be in progress");
 145     ShenandoahEvacOOMScope scope;
 146     fwd = _heap->evacuate_object(obj, Thread::current());
 147   }
 148 
 149   if (load_addr != NULL && fwd != obj) {
 150     // Since we are here and we know the load address, update the reference.
 151     ShenandoahHeap::cas_oop(fwd, load_addr, obj);
 152   }
 153 
 154   return fwd;
 155 }
 156 
 157 oop ShenandoahBarrierSet::load_reference_barrier_impl(oop obj) {
 158   assert(ShenandoahLoadRefBarrier, "should be enabled");
 159   if (!CompressedOops::is_null(obj)) {
 160     bool evac_in_progress = _heap->is_evacuation_in_progress();
 161     oop fwd = resolve_forwarded_not_null(obj);
 162     if (evac_in_progress &&
 163         _heap->in_collection_set(obj) &&
 164         obj == fwd) {
 165       Thread *t = Thread::current();
 166       ShenandoahEvacOOMScope oom_evac_scope;
 167       return _heap->evacuate_object(obj, t);
 168     } else {
 169       return fwd;
 170     }
 171   } else {
 172     return obj;
 173   }
 174 }
 175 
 176 void ShenandoahBarrierSet::on_thread_create(Thread* thread) {
 177   // Create thread local data
 178   ShenandoahThreadLocalData::create(thread);
 179 }
 180 
 181 void ShenandoahBarrierSet::on_thread_destroy(Thread* thread) {
 182   // Destroy thread local data
 183   ShenandoahThreadLocalData::destroy(thread);
 184 }
 185 
 186 void ShenandoahBarrierSet::on_thread_attach(Thread *thread) {
 187   assert(!thread->is_Java_thread() || !SafepointSynchronize::is_at_safepoint(),
 188          "We should not be at a safepoint");
 189   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 190   assert(!queue.is_active(), "SATB queue should not be active");
 191   assert( queue.is_empty(),  "SATB queue should be empty");
 192   queue.set_active(_satb_mark_queue_set.is_active());
 193   if (thread->is_Java_thread()) {
 194     ShenandoahThreadLocalData::set_gc_state(thread, _heap->gc_state());
 195     ShenandoahThreadLocalData::initialize_gclab(thread);
 196   }
 197 }
 198 
 199 void ShenandoahBarrierSet::on_thread_detach(Thread *thread) {
 200   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 201   queue.flush();
 202   if (thread->is_Java_thread()) {
 203     PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
 204     if (gclab != NULL) {
 205       gclab->retire();
 206     }
 207   }
 208 }
 209 
 210 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, oop* load_addr) {
 211   return load_reference_barrier_native_impl(obj, load_addr);
 212 }
 213 
 214 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, narrowOop* load_addr) {
 215   return load_reference_barrier_native_impl(obj, load_addr);
 216 }
 217 
 218 template <class T>
 219 oop ShenandoahBarrierSet::load_reference_barrier_native_impl(oop obj, T* load_addr) {
 220   if (CompressedOops::is_null(obj)) {
 221     return NULL;
 222   }
 223 
 224   ShenandoahMarkingContext* const marking_context = _heap->marking_context();
 225   if (_heap->is_concurrent_root_in_progress() && !marking_context->is_marked(obj)) {
 226     Thread* thr = Thread::current();
 227     if (thr->is_Java_thread()) {
 228       return NULL;
 229     } else {
 230       return obj;
 231     }
 232   }
 233 
 234   oop fwd = load_reference_barrier_not_null(obj);
 235   if (load_addr != NULL && fwd != obj) {
 236     // Since we are here and we know the load address, update the reference.
 237     ShenandoahHeap::cas_oop(fwd, load_addr, obj);
 238   }
 239 
 240   return fwd;
 241 }
 242 
 243 void ShenandoahBarrierSet::clone_barrier_runtime(oop src) {
 244   if (_heap->has_forwarded_objects() || (ShenandoahStoreValEnqueueBarrier && _heap->is_concurrent_mark_in_progress())) {
 245     clone_barrier(src);
 246   }
 247 }
 248