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