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