1 /*
   2  * Copyright (c) 2013, 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "gc/shenandoah/shenandoahAsserts.hpp"
  26 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  27 #include "gc/shenandoah/shenandoahBarrierSetClone.inline.hpp"
  28 #include "gc/shenandoah/shenandoahBarrierSetAssembler.hpp"
  29 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  31 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
  32 #include "memory/iterator.inline.hpp"
  33 #include "runtime/interfaceSupport.inline.hpp"
  34 #ifdef COMPILER1
  35 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
  36 #endif
  37 #ifdef COMPILER2
  38 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
  39 #endif
  40 
  41 class ShenandoahBarrierSetC1;
  42 class ShenandoahBarrierSetC2;
  43 
  44 ShenandoahSATBMarkQueueSet ShenandoahBarrierSet::_satb_mark_queue_set;
  45 
  46 ShenandoahBarrierSet::ShenandoahBarrierSet(ShenandoahHeap* heap) :
  47   BarrierSet(make_barrier_set_assembler<ShenandoahBarrierSetAssembler>(),
  48              make_barrier_set_c1<ShenandoahBarrierSetC1>(),
  49              make_barrier_set_c2<ShenandoahBarrierSetC2>(),
  50              BarrierSet::FakeRtti(BarrierSet::ShenandoahBarrierSet)),
  51   _heap(heap)
  52 {
  53 }
  54 
  55 ShenandoahBarrierSetAssembler* ShenandoahBarrierSet::assembler() {
  56   BarrierSetAssembler* const bsa = BarrierSet::barrier_set()->barrier_set_assembler();
  57   return reinterpret_cast<ShenandoahBarrierSetAssembler*>(bsa);
  58 }
  59 
  60 void ShenandoahBarrierSet::print_on(outputStream* st) const {
  61   st->print("ShenandoahBarrierSet");
  62 }
  63 
  64 bool ShenandoahBarrierSet::is_a(BarrierSet::Name bsn) {
  65   return bsn == BarrierSet::ShenandoahBarrierSet;
  66 }
  67 
  68 bool ShenandoahBarrierSet::is_aligned(HeapWord* hw) {
  69   return true;
  70 }
  71 
  72 oop ShenandoahBarrierSet::load_reference_barrier_not_null(oop obj) {
  73   if (ShenandoahLoadRefBarrier && _heap->has_forwarded_objects()) {
  74     return load_reference_barrier_impl(obj);
  75   } else {
  76     return obj;
  77   }
  78 }
  79 
  80 oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
  81   if (obj != NULL) {
  82     return load_reference_barrier_not_null(obj);
  83   } else {
  84     return obj;
  85   }
  86 }
  87 
  88 bool ShenandoahBarrierSet::need_load_reference_barrier(DecoratorSet decorators, BasicType type) {
  89   if (!ShenandoahLoadRefBarrier) return false;
  90   // Only needed for references
  91   return is_reference_type(type);
  92 }
  93 
  94 bool ShenandoahBarrierSet::need_keep_alive_barrier(DecoratorSet decorators,BasicType type) {
  95   if (!ShenandoahSATBBarrier) return false;
  96   // Only needed for references
  97   if (!is_reference_type(type)) return false;
  98 
  99   bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0;
 100   bool unknown = (decorators & ON_UNKNOWN_OOP_REF) != 0;
 101   bool on_weak_ref = (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) != 0;
 102   return (on_weak_ref || unknown) && keep_alive;
 103 }
 104 
 105 oop ShenandoahBarrierSet::load_reference_barrier_impl(oop obj) {
 106   assert(ShenandoahLoadRefBarrier, "should be enabled");
 107   if (!CompressedOops::is_null(obj)) {
 108     bool evac_in_progress = _heap->is_evacuation_in_progress();
 109     oop fwd = resolve_forwarded_not_null(obj);
 110     if (evac_in_progress &&
 111         _heap->in_collection_set(obj) &&
 112         obj == fwd) {
 113       Thread *t = Thread::current();
 114       ShenandoahEvacOOMScope oom_evac_scope;
 115       return _heap->evacuate_object(obj, t);
 116     } else {
 117       return fwd;
 118     }
 119   } else {
 120     return obj;
 121   }
 122 }
 123 
 124 void ShenandoahBarrierSet::on_thread_create(Thread* thread) {
 125   // Create thread local data
 126   ShenandoahThreadLocalData::create(thread);
 127 }
 128 
 129 void ShenandoahBarrierSet::on_thread_destroy(Thread* thread) {
 130   // Destroy thread local data
 131   ShenandoahThreadLocalData::destroy(thread);
 132 }
 133 
 134 void ShenandoahBarrierSet::on_thread_attach(JavaThread* thread) {
 135   assert(!SafepointSynchronize::is_at_safepoint(), "We should not be at a safepoint");
 136   assert(!ShenandoahThreadLocalData::satb_mark_queue(thread).is_active(), "SATB queue should not be active");
 137   assert(ShenandoahThreadLocalData::satb_mark_queue(thread).is_empty(), "SATB queue should be empty");
 138   if (ShenandoahBarrierSet::satb_mark_queue_set().is_active()) {
 139     ShenandoahThreadLocalData::satb_mark_queue(thread).set_active(true);
 140   }
 141   ShenandoahThreadLocalData::set_gc_state(thread, _heap->gc_state());
 142   ShenandoahThreadLocalData::initialize_gclab(thread);
 143 }
 144 
 145 void ShenandoahBarrierSet::on_thread_detach(JavaThread* thread) {
 146   ShenandoahThreadLocalData::satb_mark_queue(thread).flush();
 147   PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
 148   if (gclab != NULL) {
 149     gclab->retire();
 150   }
 151 }
 152 
 153 void ShenandoahBarrierSet::clone_barrier_runtime(oop src) {
 154   if (_heap->has_forwarded_objects() || (ShenandoahStoreValEnqueueBarrier && _heap->is_concurrent_mark_in_progress())) {
 155     clone_barrier(src);
 156   }
 157 }
 158