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 template <class T>
 105 inline void ShenandoahBarrierSet::inline_write_ref_field_pre(T* field, oop new_val) {
 106   shenandoah_assert_not_in_cset_loc_except(field, _heap->cancelled_gc());
 107   if (_heap->is_concurrent_mark_in_progress()) {
 108     T heap_oop = RawAccess<>::oop_load(field);
 109     if (!CompressedOops::is_null(heap_oop)) {
 110       enqueue(CompressedOops::decode(heap_oop));
 111     }
 112   }
 113 }
 114 
 115 // These are the more general virtual versions.
 116 void ShenandoahBarrierSet::write_ref_field_pre_work(oop* field, oop new_val) {
 117   inline_write_ref_field_pre(field, new_val);
 118 }
 119 
 120 void ShenandoahBarrierSet::write_ref_field_pre_work(narrowOop* field, oop new_val) {
 121   inline_write_ref_field_pre(field, new_val);
 122 }
 123 
 124 void ShenandoahBarrierSet::write_ref_field_pre_work(void* field, oop new_val) {
 125   guarantee(false, "Not needed");
 126 }
 127 
 128 void ShenandoahBarrierSet::write_ref_field_work(void* v, oop o, bool release) {
 129   shenandoah_assert_not_in_cset_loc_except(v, _heap->cancelled_gc());
 130   shenandoah_assert_not_forwarded_except  (v, o, o == NULL || _heap->cancelled_gc() || !_heap->is_concurrent_mark_in_progress());
 131   shenandoah_assert_not_in_cset_except    (v, o, o == NULL || _heap->cancelled_gc() || !_heap->is_concurrent_mark_in_progress());
 132 }
 133 
 134 oop ShenandoahBarrierSet::load_reference_barrier_not_null(oop obj) {
 135   if (ShenandoahLoadRefBarrier && _heap->has_forwarded_objects()) {
 136     return load_reference_barrier_impl(obj);
 137   } else {
 138     return obj;
 139   }
 140 }
 141 
 142 oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
 143   if (obj != NULL) {
 144     return load_reference_barrier_not_null(obj);
 145   } else {
 146     return obj;
 147   }
 148 }
 149 
 150 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, oop* load_addr) {
 151   return load_reference_barrier_mutator_work(obj, load_addr);
 152 }
 153 
 154 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, narrowOop* load_addr) {
 155   return load_reference_barrier_mutator_work(obj, load_addr);
 156 }
 157 
 158 template <class T>
 159 oop ShenandoahBarrierSet::load_reference_barrier_mutator_work(oop obj, T* load_addr) {
 160   assert(ShenandoahLoadRefBarrier, "should be enabled");
 161   shenandoah_assert_in_cset(load_addr, obj);
 162 
 163   oop fwd = resolve_forwarded_not_null(obj);
 164   if (obj == fwd) {
 165     assert(_heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL),
 166            "evac should be in progress");
 167 
 168     ShenandoahEvacOOMScope oom_evac_scope;
 169 
 170     Thread* thread = Thread::current();
 171     oop res_oop = _heap->evacuate_object(obj, thread);
 172 
 173     // Since we are already here and paid the price of getting through runtime call adapters
 174     // and acquiring oom-scope, it makes sense to try and evacuate more adjacent objects,
 175     // thus amortizing the overhead. For sparsely live heaps, scan costs easily dominate
 176     // total assist costs, and can introduce a lot of evacuation latency. This is why we
 177     // only scan for _nearest_ N objects, regardless if they are eligible for evac or not.
 178     // The scan itself should also avoid touching the non-marked objects below TAMS, because
 179     // their metadata (notably, klasses) may be incorrect already.
 180 
 181     size_t max = ShenandoahEvacAssist;
 182     if (max > 0) {
 183       // Traversal is special: it uses incomplete marking context, because it coalesces evac with mark.
 184       // Other code uses complete marking context, because evac happens after the mark.
 185       ShenandoahMarkingContext* ctx = _heap->is_concurrent_traversal_in_progress() ?
 186                                       _heap->marking_context() : _heap->complete_marking_context();
 187 
 188       ShenandoahHeapRegion* r = _heap->heap_region_containing(obj);
 189       assert(r->is_cset(), "sanity");
 190 
 191       HeapWord* cur = (HeapWord*)obj + obj->size();
 192 
 193       size_t count = 0;
 194       while ((cur < r->top()) && ctx->is_marked(oop(cur)) && (count++ < max)) {
 195         oop cur_oop = oop(cur);
 196         if (cur_oop == resolve_forwarded_not_null(cur_oop)) {
 197           _heap->evacuate_object(cur_oop, thread);
 198         }
 199         cur = cur + cur_oop->size();
 200       }
 201     }
 202 
 203     fwd = res_oop;
 204   }
 205 
 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 oop ShenandoahBarrierSet::load_reference_barrier_impl(oop obj) {
 215   assert(ShenandoahLoadRefBarrier, "should be enabled");
 216   if (!CompressedOops::is_null(obj)) {
 217     bool evac_in_progress = _heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL);
 218     oop fwd = resolve_forwarded_not_null(obj);
 219     if (evac_in_progress &&
 220         _heap->in_collection_set(obj) &&
 221         obj == fwd) {
 222       Thread *t = Thread::current();
 223       if (t->is_GC_task_thread()) {
 224         return _heap->evacuate_object(obj, t);
 225       } else {
 226         ShenandoahEvacOOMScope oom_evac_scope;
 227         return _heap->evacuate_object(obj, t);
 228       }
 229     } else {
 230       return fwd;
 231     }
 232   } else {
 233     return obj;
 234   }
 235 }
 236 
 237 void ShenandoahBarrierSet::storeval_barrier(oop obj) {
 238   if (ShenandoahStoreValEnqueueBarrier && !CompressedOops::is_null(obj) && _heap->is_concurrent_traversal_in_progress()) {
 239     enqueue(obj);
 240   }
 241 }
 242 
 243 void ShenandoahBarrierSet::keep_alive_barrier(oop obj) {
 244   if (ShenandoahKeepAliveBarrier && _heap->is_concurrent_mark_in_progress()) {
 245     enqueue(obj);
 246   }
 247 }
 248 
 249 void ShenandoahBarrierSet::enqueue(oop obj) {
 250   shenandoah_assert_not_forwarded_if(NULL, obj, _heap->is_concurrent_traversal_in_progress());
 251   assert(_satb_mark_queue_set.is_active(), "only get here when SATB active");
 252 
 253   // Filter marked objects before hitting the SATB queues. The same predicate would
 254   // be used by SATBMQ::filter to eliminate already marked objects downstream, but
 255   // filtering here helps to avoid wasteful SATB queueing work to begin with.
 256   if (!_heap->requires_marking<false>(obj)) return;
 257 
 258   ShenandoahThreadLocalData::satb_mark_queue(Thread::current()).enqueue_known_active(obj);
 259 }
 260 
 261 void ShenandoahBarrierSet::on_thread_create(Thread* thread) {
 262   // Create thread local data
 263   ShenandoahThreadLocalData::create(thread);
 264 }
 265 
 266 void ShenandoahBarrierSet::on_thread_destroy(Thread* thread) {
 267   // Destroy thread local data
 268   ShenandoahThreadLocalData::destroy(thread);
 269 }
 270 
 271 void ShenandoahBarrierSet::on_thread_attach(Thread *thread) {
 272   assert(!thread->is_Java_thread() || !SafepointSynchronize::is_at_safepoint(),
 273          "We should not be at a safepoint");
 274   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 275   assert(!queue.is_active(), "SATB queue should not be active");
 276   assert( queue.is_empty(),  "SATB queue should be empty");
 277   queue.set_active(_satb_mark_queue_set.is_active());
 278   if (thread->is_Java_thread()) {
 279     ShenandoahThreadLocalData::set_gc_state(thread, _heap->gc_state());
 280     ShenandoahThreadLocalData::initialize_gclab(thread);
 281   }
 282 }
 283 
 284 void ShenandoahBarrierSet::on_thread_detach(Thread *thread) {
 285   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 286   queue.flush();
 287   if (thread->is_Java_thread()) {
 288     PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
 289     if (gclab != NULL) {
 290       gclab->retire();
 291     }
 292   }
 293 }
 294 
 295 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, oop* load_addr) {
 296   return load_reference_barrier_native_impl(obj, load_addr);
 297 }
 298 
 299 oop ShenandoahBarrierSet::load_reference_barrier_native(oop obj, narrowOop* load_addr) {
 300   // Assumption: narrow oop version should not be used anywhere.
 301   ShouldNotReachHere();
 302   return NULL;
 303 }
 304 
 305 template <class T>
 306 oop ShenandoahBarrierSet::load_reference_barrier_native_impl(oop obj, T* load_addr) {
 307   if (CompressedOops::is_null(obj)) {
 308     return NULL;
 309   }
 310 
 311   ShenandoahMarkingContext* const marking_context = _heap->marking_context();
 312   if (_heap->is_evacuation_in_progress() && !marking_context->is_marked(obj)) {
 313     Thread* thr = Thread::current();
 314     if (thr->is_Java_thread()) {
 315       return NULL;
 316     } else {
 317       return obj;
 318     }
 319   }
 320 
 321   oop fwd = load_reference_barrier_not_null(obj);
 322   if (load_addr != NULL && fwd != obj) {
 323     // Since we are here and we know the load address, update the reference.
 324     ShenandoahHeap::cas_oop(fwd, load_addr, obj);
 325   }
 326 
 327   return fwd;
 328 }
 329 
 330 void ShenandoahBarrierSet::clone_barrier_runtime(oop src) {
 331   if (_heap->has_forwarded_objects()) {
 332     clone_barrier(src);
 333   }
 334 }
 335