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/shenandoahBarrierSetAssembler.hpp"
  28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  30 #include "gc/shenandoah/shenandoahHeuristics.hpp"
  31 #include "gc/shenandoah/shenandoahTraversalGC.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 template <bool STOREVAL_WRITE_BARRIER>
  45 class ShenandoahUpdateRefsForOopClosure: public BasicOopIterateClosure {
  46 private:
  47   ShenandoahHeap* _heap;
  48   ShenandoahBarrierSet* _bs;
  49 
  50   template <class T>
  51   inline void do_oop_work(T* p) {
  52     oop o;
  53     if (STOREVAL_WRITE_BARRIER) {
  54       o = _heap->evac_update_with_forwarded(p);
  55       if (!CompressedOops::is_null(o)) {
  56         _bs->enqueue(o);
  57       }
  58     } else {
  59       _heap->maybe_update_with_forwarded(p);
  60     }
  61   }
  62 public:
  63   ShenandoahUpdateRefsForOopClosure() : _heap(ShenandoahHeap::heap()), _bs(ShenandoahBarrierSet::barrier_set()) {
  64     assert(UseShenandoahGC && ShenandoahCloneBarrier, "should be enabled");
  65   }
  66 
  67   virtual void do_oop(oop* p)       { do_oop_work(p); }
  68   virtual void do_oop(narrowOop* p) { do_oop_work(p); }
  69 };
  70 
  71 ShenandoahBarrierSet::ShenandoahBarrierSet(ShenandoahHeap* heap) :
  72   BarrierSet(make_barrier_set_assembler<ShenandoahBarrierSetAssembler>(),
  73              make_barrier_set_c1<ShenandoahBarrierSetC1>(),
  74              make_barrier_set_c2<ShenandoahBarrierSetC2>(),
  75              NULL /* barrier_set_nmethod */,
  76              BarrierSet::FakeRtti(BarrierSet::ShenandoahBarrierSet)),
  77   _heap(heap),
  78   _satb_mark_queue_set()
  79 {
  80 }
  81 
  82 ShenandoahBarrierSetAssembler* ShenandoahBarrierSet::assembler() {
  83   BarrierSetAssembler* const bsa = BarrierSet::barrier_set()->barrier_set_assembler();
  84   return reinterpret_cast<ShenandoahBarrierSetAssembler*>(bsa);
  85 }
  86 
  87 void ShenandoahBarrierSet::print_on(outputStream* st) const {
  88   st->print("ShenandoahBarrierSet");
  89 }
  90 
  91 bool ShenandoahBarrierSet::is_a(BarrierSet::Name bsn) {
  92   return bsn == BarrierSet::ShenandoahBarrierSet;
  93 }
  94 
  95 bool ShenandoahBarrierSet::is_aligned(HeapWord* hw) {
  96   return true;
  97 }
  98 
  99 template <class T, bool STOREVAL_WRITE_BARRIER>
 100 void ShenandoahBarrierSet::write_ref_array_loop(HeapWord* start, size_t count) {
 101   assert(UseShenandoahGC && ShenandoahCloneBarrier, "should be enabled");
 102   ShenandoahUpdateRefsForOopClosure<STOREVAL_WRITE_BARRIER> cl;
 103   T* dst = (T*) start;
 104   for (size_t i = 0; i < count; i++) {
 105     cl.do_oop(dst++);
 106   }
 107 }
 108 
 109 void ShenandoahBarrierSet::write_ref_array(HeapWord* start, size_t count) {
 110   assert(UseShenandoahGC, "should be enabled");
 111   if (count == 0) return;
 112   if (!ShenandoahCloneBarrier) return;
 113 
 114   if (!need_update_refs_barrier()) return;
 115 
 116   if (_heap->is_concurrent_traversal_in_progress()) {
 117     ShenandoahEvacOOMScope oom_evac_scope;
 118     if (UseCompressedOops) {
 119       write_ref_array_loop<narrowOop, /* wb = */ true>(start, count);
 120     } else {
 121       write_ref_array_loop<oop,       /* wb = */ true>(start, count);
 122     }
 123   } else {
 124     if (UseCompressedOops) {
 125       write_ref_array_loop<narrowOop, /* wb = */ false>(start, count);
 126     } else {
 127       write_ref_array_loop<oop,       /* wb = */ false>(start, count);
 128     }
 129   }
 130 }
 131 
 132 template <class T>
 133 void ShenandoahBarrierSet::write_ref_array_pre_work(T* dst, size_t count) {
 134   shenandoah_assert_not_in_cset_loc_except(dst, _heap->cancelled_gc());
 135   if (ShenandoahSATBBarrier && _heap->is_concurrent_mark_in_progress()) {
 136     T* elem_ptr = dst;
 137     for (size_t i = 0; i < count; i++, elem_ptr++) {
 138       T heap_oop = RawAccess<>::oop_load(elem_ptr);
 139       if (!CompressedOops::is_null(heap_oop)) {
 140         enqueue(CompressedOops::decode_not_null(heap_oop));
 141       }
 142     }
 143   }
 144 }
 145 
 146 void ShenandoahBarrierSet::write_ref_array_pre(oop* dst, size_t count, bool dest_uninitialized) {
 147   if (! dest_uninitialized) {
 148     write_ref_array_pre_work(dst, count);
 149   }
 150 }
 151 
 152 void ShenandoahBarrierSet::write_ref_array_pre(narrowOop* dst, size_t count, bool dest_uninitialized) {
 153   if (! dest_uninitialized) {
 154     write_ref_array_pre_work(dst, count);
 155   }
 156 }
 157 
 158 template <class T>
 159 inline void ShenandoahBarrierSet::inline_write_ref_field_pre(T* field, oop new_val) {
 160   shenandoah_assert_not_in_cset_loc_except(field, _heap->cancelled_gc());
 161   if (_heap->is_concurrent_mark_in_progress()) {
 162     T heap_oop = RawAccess<>::oop_load(field);
 163     if (!CompressedOops::is_null(heap_oop)) {
 164       enqueue(CompressedOops::decode(heap_oop));
 165     }
 166   }
 167 }
 168 
 169 // These are the more general virtual versions.
 170 void ShenandoahBarrierSet::write_ref_field_pre_work(oop* field, oop new_val) {
 171   inline_write_ref_field_pre(field, new_val);
 172 }
 173 
 174 void ShenandoahBarrierSet::write_ref_field_pre_work(narrowOop* field, oop new_val) {
 175   inline_write_ref_field_pre(field, new_val);
 176 }
 177 
 178 void ShenandoahBarrierSet::write_ref_field_pre_work(void* field, oop new_val) {
 179   guarantee(false, "Not needed");
 180 }
 181 
 182 void ShenandoahBarrierSet::write_ref_field_work(void* v, oop o, bool release) {
 183   shenandoah_assert_not_in_cset_loc_except(v, _heap->cancelled_gc());
 184   shenandoah_assert_not_forwarded_except  (v, o, o == NULL || _heap->cancelled_gc() || !_heap->is_concurrent_mark_in_progress());
 185   shenandoah_assert_not_in_cset_except    (v, o, o == NULL || _heap->cancelled_gc() || !_heap->is_concurrent_mark_in_progress());
 186 }
 187 
 188 void ShenandoahBarrierSet::write_region(MemRegion mr) {
 189   assert(UseShenandoahGC, "should be enabled");
 190   if (!ShenandoahCloneBarrier) return;
 191   if (! need_update_refs_barrier()) return;
 192 
 193   // This is called for cloning an object (see jvm.cpp) after the clone
 194   // has been made. We are not interested in any 'previous value' because
 195   // it would be NULL in any case. But we *are* interested in any oop*
 196   // that potentially need to be updated.
 197 
 198   oop obj = oop(mr.start());
 199   shenandoah_assert_correct(NULL, obj);
 200   if (_heap->is_concurrent_traversal_in_progress()) {
 201     ShenandoahEvacOOMScope oom_evac_scope;
 202     ShenandoahUpdateRefsForOopClosure</* wb = */ true> cl;
 203     obj->oop_iterate(&cl);
 204   } else {
 205     ShenandoahUpdateRefsForOopClosure</* wb = */ false> cl;
 206     obj->oop_iterate(&cl);
 207   }
 208 }
 209 
 210 oop ShenandoahBarrierSet::load_reference_barrier_not_null(oop obj) {
 211   if (ShenandoahLoadRefBarrier && _heap->has_forwarded_objects()) {
 212     return load_reference_barrier_impl(obj);
 213   } else {
 214     return obj;
 215   }
 216 }
 217 
 218 oop ShenandoahBarrierSet::load_reference_barrier(oop obj) {
 219   if (obj != NULL) {
 220     return load_reference_barrier_not_null(obj);
 221   } else {
 222     return obj;
 223   }
 224 }
 225 
 226 
 227 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj) {
 228   assert(ShenandoahLoadRefBarrier, "should be enabled");
 229   assert(_heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL), "evac should be in progress");
 230   shenandoah_assert_in_cset(NULL, obj);
 231 
 232   oop fwd = resolve_forwarded_not_null(obj);
 233   if (oopDesc::equals_raw(obj, fwd)) {
 234     ShenandoahEvacOOMScope oom_evac_scope;
 235 
 236     Thread* thread = Thread::current();
 237     oop res_oop = _heap->evacuate_object(obj, thread);
 238 
 239     // Since we are already here and paid the price of getting through runtime call adapters
 240     // and acquiring oom-scope, it makes sense to try and evacuate more adjacent objects,
 241     // thus amortizing the overhead. For sparsely live heaps, scan costs easily dominate
 242     // total assist costs, and can introduce a lot of evacuation latency. This is why we
 243     // only scan for _nearest_ N objects, regardless if they are eligible for evac or not.
 244     // The scan itself should also avoid touching the non-marked objects below TAMS, because
 245     // their metadata (notably, klasses) may be incorrect already.
 246 
 247     size_t max = ShenandoahEvacAssist;
 248     if (max > 0) {
 249       // Traversal is special: it uses incomplete marking context, because it coalesces evac with mark.
 250       // Other code uses complete marking context, because evac happens after the mark.
 251       ShenandoahMarkingContext* ctx = _heap->is_concurrent_traversal_in_progress() ?
 252                                       _heap->marking_context() : _heap->complete_marking_context();
 253 
 254       ShenandoahHeapRegion* r = _heap->heap_region_containing(obj);
 255       assert(r->is_cset(), "sanity");
 256 
 257       HeapWord* cur = (HeapWord*)obj + obj->size() + ShenandoahBrooksPointer::word_size();
 258 
 259       size_t count = 0;
 260       while ((cur < r->top()) && ctx->is_marked(oop(cur)) && (count++ < max)) {
 261         oop cur_oop = oop(cur);
 262         if (oopDesc::equals_raw(cur_oop, resolve_forwarded_not_null(cur_oop))) {
 263           _heap->evacuate_object(cur_oop, thread);
 264         }
 265         cur = cur + cur_oop->size() + ShenandoahBrooksPointer::word_size();
 266       }
 267     }
 268 
 269     return res_oop;
 270   }
 271   return fwd;
 272 }
 273 
 274 oop ShenandoahBarrierSet::load_reference_barrier_impl(oop obj) {
 275   assert(ShenandoahLoadRefBarrier, "should be enabled");
 276   if (!CompressedOops::is_null(obj)) {
 277     bool evac_in_progress = _heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL);
 278     oop fwd = resolve_forwarded_not_null(obj);
 279     if (evac_in_progress &&
 280         _heap->in_collection_set(obj) &&
 281         oopDesc::equals_raw(obj, fwd)) {
 282       Thread *t = Thread::current();
 283       if (t->is_GC_task_thread()) {
 284         return _heap->evacuate_object(obj, t);
 285       } else {
 286         ShenandoahEvacOOMScope oom_evac_scope;
 287         return _heap->evacuate_object(obj, t);
 288       }
 289     } else {
 290       return fwd;
 291     }
 292   } else {
 293     return obj;
 294   }
 295 }
 296 
 297 void ShenandoahBarrierSet::storeval_barrier(oop obj) {
 298   if (ShenandoahStoreValEnqueueBarrier && !CompressedOops::is_null(obj)) {
 299     enqueue(obj);
 300   }
 301 }
 302 
 303 void ShenandoahBarrierSet::keep_alive_barrier(oop obj) {
 304   if (ShenandoahKeepAliveBarrier && _heap->is_concurrent_mark_in_progress()) {
 305     enqueue(obj);
 306   }
 307 }
 308 
 309 void ShenandoahBarrierSet::enqueue(oop obj) {
 310   shenandoah_assert_not_forwarded_if(NULL, obj, _heap->is_concurrent_traversal_in_progress());
 311   if (!_satb_mark_queue_set.is_active()) return;
 312 
 313   // Filter marked objects before hitting the SATB queues. The same predicate would
 314   // be used by SATBMQ::filter to eliminate already marked objects downstream, but
 315   // filtering here helps to avoid wasteful SATB queueing work to begin with.
 316   if (!_heap->requires_marking<false>(obj)) return;
 317 
 318   ShenandoahThreadLocalData::satb_mark_queue(Thread::current()).enqueue(obj);
 319 }
 320 
 321 void ShenandoahBarrierSet::on_thread_create(Thread* thread) {
 322   // Create thread local data
 323   ShenandoahThreadLocalData::create(thread);
 324 }
 325 
 326 void ShenandoahBarrierSet::on_thread_destroy(Thread* thread) {
 327   // Destroy thread local data
 328   ShenandoahThreadLocalData::destroy(thread);
 329 }
 330 
 331 void ShenandoahBarrierSet::on_thread_attach(Thread *thread) {
 332   assert(!thread->is_Java_thread() || !SafepointSynchronize::is_at_safepoint(),
 333          "We should not be at a safepoint");
 334   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 335   assert(!queue.is_active(), "SATB queue should not be active");
 336   assert( queue.is_empty(),  "SATB queue should be empty");
 337   queue.set_active(_satb_mark_queue_set.is_active());
 338   if (thread->is_Java_thread()) {
 339     ShenandoahThreadLocalData::set_gc_state(thread, _heap->gc_state());
 340     ShenandoahThreadLocalData::initialize_gclab(thread);
 341   }
 342 }
 343 
 344 void ShenandoahBarrierSet::on_thread_detach(Thread *thread) {
 345   SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread);
 346   queue.flush();
 347   if (thread->is_Java_thread()) {
 348     PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
 349     if (gclab != NULL) {
 350       gclab->retire();
 351     }
 352   }
 353 }