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/heuristics/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_impl(oop obj) {
 129   assert(ShenandoahLoadRefBarrier, "should be enabled");
 130   if (!CompressedOops::is_null(obj)) {
 131     bool evac_in_progress = _heap->is_evacuation_in_progress();
 132     oop fwd = resolve_forwarded_not_null(obj);
 133     if (evac_in_progress &&
 134         _heap->in_collection_set(obj) &&
 135         obj == fwd) {
 136       Thread *t = Thread::current();
 137       return _heap->evacuate_object(obj, t);
 138     } else {
 139       return fwd;
 140     }
 141   } else {
 142     return obj;
 143   }
 144 }
 145 
 146 void ShenandoahBarrierSet::on_thread_create(Thread* thread) {
 147   // Create thread local data
 148   ShenandoahThreadLocalData::create(thread);
 149 }
 150 
 151 void ShenandoahBarrierSet::on_thread_destroy(Thread* thread) {
 152   // Destroy thread local data
 153   ShenandoahThreadLocalData::destroy(thread);
 154 }
 155 
 156 void ShenandoahBarrierSet::on_thread_attach(Thread *thread) {
 157   assert(!thread->is_Java_thread() || !SafepointSynchronize::is_at_safepoint(),
 158          "We should not be at a safepoint");
 159   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 160   assert(!queue.is_active(), "SATB queue should not be active");
 161   assert( queue.is_empty(),  "SATB queue should be empty");
 162   queue.set_active(_satb_mark_queue_set.is_active());
 163   if (thread->is_Java_thread()) {
 164     ShenandoahThreadLocalData::set_gc_state(thread, _heap->gc_state());
 165     ShenandoahThreadLocalData::initialize_gclab(thread);
 166     ShenandoahThreadLocalData::set_disarmed_value(thread, ShenandoahCodeRoots::disarmed_value());
 167   }
 168 }
 169 
 170 void ShenandoahBarrierSet::on_thread_detach(Thread *thread) {
 171   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 172   queue.flush();
 173   if (thread->is_Java_thread()) {
 174     PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
 175     if (gclab != NULL) {
 176       gclab->retire();
 177     }
 178   }
 179 }
 180 
 181 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, oop* load_addr) {
 182   return load_reference_barrier_native_impl(obj, load_addr);
 183 }
 184 
 185 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, narrowOop* load_addr) {
 186   return load_reference_barrier_native_impl(obj, load_addr);
 187 }
 188 
 189 template <class T>
 190 oop ShenandoahBarrierSet::load_reference_barrier_native_impl(oop obj, T* load_addr) {
 191   if (CompressedOops::is_null(obj)) {
 192     return NULL;
 193   }
 194 
 195   ShenandoahMarkingContext* const marking_context = _heap->marking_context();
 196   if (_heap->is_concurrent_weak_root_in_progress() && !marking_context->is_marked(obj)) {
 197     Thread* thr = Thread::current();
 198     if (thr->is_Java_thread()) {
 199       return NULL;
 200     } else {
 201       return obj;
 202     }
 203   }
 204 
 205   oop fwd = load_reference_barrier_not_null(obj);
 206   if (load_addr != NULL && fwd != obj) {
 207     // Since we are here and we know the load address, update the reference.
 208     ShenandoahHeap::cas_oop(fwd, load_addr, obj);
 209   }
 210 
 211   return fwd;
 212 }
 213 
 214 void ShenandoahBarrierSet::clone_barrier_runtime(oop src) {
 215   if (_heap->has_forwarded_objects() || (ShenandoahStoreValEnqueueBarrier && _heap->is_concurrent_mark_in_progress())) {
 216     clone_barrier(src);
 217   }
 218 }
 219