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