1 /*
   2  * Copyright (c) 2013, 2019, 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/shenandoahConcurrentRoots.hpp"
  31 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  32 #include "gc/shenandoah/shenandoahHeuristics.hpp"
  33 #include "gc/shenandoah/shenandoahTraversalGC.hpp"
  34 #include "memory/iterator.inline.hpp"
  35 #include "runtime/interfaceSupport.inline.hpp"
  36 #ifdef COMPILER1
  37 #include "gc/shenandoah/c1/shenandoahBarrierSetC1.hpp"
  38 #endif
  39 #ifdef COMPILER2
  40 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
  41 #endif
  42 
  43 class ShenandoahBarrierSetC1;
  44 class ShenandoahBarrierSetC2;
  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              NULL /* barrier_set_nmethod */,
  51              BarrierSet::FakeRtti(BarrierSet::ShenandoahBarrierSet)),
  52   _heap(heap),
  53   _satb_mark_queue_buffer_allocator("SATB Buffer Allocator", ShenandoahSATBBufferSize),
  54   _satb_mark_queue_set(&_satb_mark_queue_buffer_allocator)
  55 {
  56 }
  57 
  58 ShenandoahBarrierSetAssembler* ShenandoahBarrierSet::assembler() {
  59   BarrierSetAssembler* const bsa = BarrierSet::barrier_set()->barrier_set_assembler();
  60   return reinterpret_cast<ShenandoahBarrierSetAssembler*>(bsa);
  61 }
  62 
  63 void ShenandoahBarrierSet::print_on(outputStream* st) const {
  64   st->print("ShenandoahBarrierSet");
  65 }
  66 
  67 bool ShenandoahBarrierSet::is_a(BarrierSet::Name bsn) {
  68   return bsn == BarrierSet::ShenandoahBarrierSet;
  69 }
  70 
  71 bool ShenandoahBarrierSet::is_aligned(HeapWord* hw) {
  72   return true;
  73 }
  74 
  75 bool ShenandoahBarrierSet::need_load_reference_barrier(DecoratorSet decorators, BasicType type) {
  76   if (!ShenandoahLoadRefBarrier) return false;
  77   // Only needed for references
  78   return is_reference_type(type);
  79 }
  80 
  81 bool ShenandoahBarrierSet::use_load_reference_barrier_native(DecoratorSet decorators, BasicType type) {
  82   assert(need_load_reference_barrier(decorators, type), "Should be subset of LRB");
  83   assert(is_reference_type(type), "Why we here?");
  84   // Native load reference barrier is only needed for concurrent root processing
  85   if (!ShenandoahConcurrentRoots::can_do_concurrent_roots()) {
  86     return false;
  87   }
  88 
  89   return (decorators & IN_NATIVE) != 0;
  90 }
  91 
  92 bool ShenandoahBarrierSet::need_keep_alive_barrier(DecoratorSet decorators,BasicType type) {
  93   if (!ShenandoahKeepAliveBarrier) return false;
  94   // Only needed for references
  95   if (!is_reference_type(type)) return false;
  96 
  97   bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0;
  98   bool unknown = (decorators & ON_UNKNOWN_OOP_REF) != 0;
  99   bool is_traversal_mode = ShenandoahHeap::heap()->is_traversal_mode();
 100   bool on_weak_ref = (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) != 0;
 101   return (on_weak_ref || unknown) && (keep_alive || is_traversal_mode);
 102 }
 103 
 104 oop ShenandoahBarrierSet::load_reference_barrier_not_null(oop obj) {
 105   if (ShenandoahLoadRefBarrier && _heap->has_forwarded_objects()) {
 106     return load_reference_barrier_impl(obj);
 107   } else {
 108     return obj;
 109   }
 110 }
 111 
 112 oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
 113   if (obj != NULL) {
 114     return load_reference_barrier_not_null(obj);
 115   } else {
 116     return obj;
 117   }
 118 }
 119 
 120 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, oop* load_addr) {
 121   return load_reference_barrier_mutator_work(obj, load_addr);
 122 }
 123 
 124 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, narrowOop* load_addr) {
 125   return load_reference_barrier_mutator_work(obj, load_addr);
 126 }
 127 
 128 template <class T>
 129 oop ShenandoahBarrierSet::load_reference_barrier_mutator_work(oop obj, T* load_addr) {
 130   assert(ShenandoahLoadRefBarrier, "should be enabled");
 131   shenandoah_assert_in_cset(load_addr, obj);
 132 
 133   oop fwd = resolve_forwarded_not_null(obj);
 134   if (obj == fwd) {
 135     assert(_heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL),
 136            "evac should be in progress");
 137 
 138     ShenandoahEvacOOMScope oom_evac_scope;
 139 
 140     Thread* thread = Thread::current();
 141     oop res_oop = _heap->evacuate_object(obj, thread);
 142 
 143     // Since we are already here and paid the price of getting through runtime call adapters
 144     // and acquiring oom-scope, it makes sense to try and evacuate more adjacent objects,
 145     // thus amortizing the overhead. For sparsely live heaps, scan costs easily dominate
 146     // total assist costs, and can introduce a lot of evacuation latency. This is why we
 147     // only scan for _nearest_ N objects, regardless if they are eligible for evac or not.
 148     // The scan itself should also avoid touching the non-marked objects below TAMS, because
 149     // their metadata (notably, klasses) may be incorrect already.
 150 
 151     size_t max = ShenandoahEvacAssist;
 152     if (max > 0) {
 153       // Traversal is special: it uses incomplete marking context, because it coalesces evac with mark.
 154       // Other code uses complete marking context, because evac happens after the mark.
 155       ShenandoahMarkingContext* ctx = _heap->is_concurrent_traversal_in_progress() ?
 156                                       _heap->marking_context() : _heap->complete_marking_context();
 157 
 158       ShenandoahHeapRegion* r = _heap->heap_region_containing(obj);
 159       assert(r->is_cset(), "sanity");
 160 
 161       HeapWord* cur = (HeapWord*)obj + obj->size();
 162 
 163       size_t count = 0;
 164       while ((cur < r->top()) && ctx->is_marked(oop(cur)) && (count++ < max)) {
 165         oop cur_oop = oop(cur);
 166         if (cur_oop == resolve_forwarded_not_null(cur_oop)) {
 167           _heap->evacuate_object(cur_oop, thread);
 168         }
 169         cur = cur + cur_oop->size();
 170       }
 171     }
 172 
 173     fwd = res_oop;
 174   }
 175 
 176   if (load_addr != NULL && fwd != obj) {
 177     // Since we are here and we know the load address, update the reference.
 178     ShenandoahHeap::cas_oop(fwd, load_addr, obj);
 179   }
 180 
 181   return fwd;
 182 }
 183 
 184 oop ShenandoahBarrierSet::load_reference_barrier_impl(oop obj) {
 185   assert(ShenandoahLoadRefBarrier, "should be enabled");
 186   if (!CompressedOops::is_null(obj)) {
 187     bool evac_in_progress = _heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL);
 188     oop fwd = resolve_forwarded_not_null(obj);
 189     if (evac_in_progress &&
 190         _heap->in_collection_set(obj) &&
 191         obj == fwd) {
 192       Thread *t = Thread::current();
 193       if (t->is_GC_task_thread()) {
 194         return _heap->evacuate_object(obj, t);
 195       } else {
 196         ShenandoahEvacOOMScope oom_evac_scope;
 197         return _heap->evacuate_object(obj, t);
 198       }
 199     } else {
 200       return fwd;
 201     }
 202   } else {
 203     return obj;
 204   }
 205 }
 206 
 207 void ShenandoahBarrierSet::on_thread_create(Thread* thread) {
 208   // Create thread local data
 209   ShenandoahThreadLocalData::create(thread);
 210 }
 211 
 212 void ShenandoahBarrierSet::on_thread_destroy(Thread* thread) {
 213   // Destroy thread local data
 214   ShenandoahThreadLocalData::destroy(thread);
 215 }
 216 
 217 void ShenandoahBarrierSet::on_thread_attach(Thread *thread) {
 218   assert(!thread->is_Java_thread() || !SafepointSynchronize::is_at_safepoint(),
 219          "We should not be at a safepoint");
 220   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 221   assert(!queue.is_active(), "SATB queue should not be active");
 222   assert( queue.is_empty(),  "SATB queue should be empty");
 223   queue.set_active(_satb_mark_queue_set.is_active());
 224   if (thread->is_Java_thread()) {
 225     ShenandoahThreadLocalData::set_gc_state(thread, _heap->gc_state());
 226     ShenandoahThreadLocalData::initialize_gclab(thread);
 227   }
 228 }
 229 
 230 void ShenandoahBarrierSet::on_thread_detach(Thread *thread) {
 231   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 232   queue.flush();
 233   if (thread->is_Java_thread()) {
 234     PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
 235     if (gclab != NULL) {
 236       gclab->retire();
 237     }
 238   }
 239 }
 240 
 241 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, oop* load_addr) {
 242   return load_reference_barrier_native_impl(obj, load_addr);
 243 }
 244 
 245 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, narrowOop* load_addr) {
 246   // Assumption: narrow oop version should not be used anywhere.
 247   ShouldNotReachHere();
 248   return NULL;
 249 }
 250 
 251 template <class T>
 252 oop ShenandoahBarrierSet::load_reference_barrier_native_impl(oop obj, T* load_addr) {
 253   if (CompressedOops::is_null(obj)) {
 254     return NULL;
 255   }
 256 
 257   ShenandoahMarkingContext* const marking_context = _heap->marking_context();
 258   if (_heap->is_evacuation_in_progress() && !marking_context->is_marked(obj)) {
 259     Thread* thr = Thread::current();
 260     if (thr->is_Java_thread()) {
 261       return NULL;
 262     } else {
 263       return obj;
 264     }
 265   }
 266 
 267   oop fwd = load_reference_barrier_not_null(obj);
 268   if (load_addr != NULL && fwd != obj) {
 269     // Since we are here and we know the load address, update the reference.
 270     ShenandoahHeap::cas_oop(fwd, load_addr, obj);
 271   }
 272 
 273   return fwd;
 274 }
 275 
 276 void ShenandoahBarrierSet::clone_barrier_runtime(oop src) {
 277   if (_heap->has_forwarded_objects()) {
 278     clone_barrier(src);
 279   }
 280 }
 281