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 assert(ShenandoahThreadLocalData::satb_mark_queue(Thread::current()).is_active(), "Shouldn't be here otherwise"); 136 assert(ShenandoahSATBBarrier, "Shouldn't be here otherwise"); 137 assert(count > 0, "Should have been filtered before"); 138 139 Thread* thread = Thread::current(); 140 ShenandoahMarkingContext* ctx = _heap->marking_context(); 141 bool has_forwarded = _heap->has_forwarded_objects(); 142 T* elem_ptr = dst; 143 for (size_t i = 0; i < count; i++, elem_ptr++) { 144 T heap_oop = RawAccess<>::oop_load(elem_ptr); 145 if (!CompressedOops::is_null(heap_oop)) { 146 oop obj = CompressedOops::decode_not_null(heap_oop); 147 if (has_forwarded) { 148 obj = resolve_forwarded_not_null(obj); 149 } 150 if (!ctx->is_marked(obj)) { 151 ShenandoahThreadLocalData::satb_mark_queue(thread).enqueue_known_active(obj); 152 } 153 } 154 } 155 } 156 157 void ShenandoahBarrierSet::write_ref_array_pre(oop* dst, size_t count, bool dest_uninitialized) { 158 if (! dest_uninitialized) { 159 write_ref_array_pre_work(dst, count); 160 } 161 } 162 163 void ShenandoahBarrierSet::write_ref_array_pre(narrowOop* dst, size_t count, bool dest_uninitialized) { 164 if (! dest_uninitialized) { 165 write_ref_array_pre_work(dst, count); 166 } 167 } 168 169 template <class T> 170 inline void ShenandoahBarrierSet::inline_write_ref_field_pre(T* field, oop new_val) { 171 shenandoah_assert_not_in_cset_loc_except(field, _heap->cancelled_gc()); 172 if (_heap->is_concurrent_mark_in_progress()) { 173 T heap_oop = RawAccess<>::oop_load(field); 174 if (!CompressedOops::is_null(heap_oop)) { 175 enqueue(CompressedOops::decode(heap_oop)); 176 } 177 } 178 } 179 180 // These are the more general virtual versions. 181 void ShenandoahBarrierSet::write_ref_field_pre_work(oop* field, oop new_val) { 182 inline_write_ref_field_pre(field, new_val); 183 } 184 185 void ShenandoahBarrierSet::write_ref_field_pre_work(narrowOop* field, oop new_val) { 186 inline_write_ref_field_pre(field, new_val); 187 } 188 189 void ShenandoahBarrierSet::write_ref_field_pre_work(void* field, oop new_val) { 190 guarantee(false, "Not needed"); 191 } 192 193 void ShenandoahBarrierSet::write_ref_field_work(void* v, oop o, bool release) { 194 shenandoah_assert_not_in_cset_loc_except(v, _heap->cancelled_gc()); 195 shenandoah_assert_not_forwarded_except (v, o, o == NULL || _heap->cancelled_gc() || !_heap->is_concurrent_mark_in_progress()); 196 shenandoah_assert_not_in_cset_except (v, o, o == NULL || _heap->cancelled_gc() || !_heap->is_concurrent_mark_in_progress()); 197 } 198 199 void ShenandoahBarrierSet::write_region(MemRegion mr) { 200 assert(UseShenandoahGC, "should be enabled"); 201 if (!ShenandoahCloneBarrier) return; 202 if (! need_update_refs_barrier()) return; 203 204 // This is called for cloning an object (see jvm.cpp) after the clone 205 // has been made. We are not interested in any 'previous value' because 206 // it would be NULL in any case. But we *are* interested in any oop* 207 // that potentially need to be updated. 208 209 oop obj = oop(mr.start()); 210 shenandoah_assert_correct(NULL, obj); 211 if (_heap->is_concurrent_traversal_in_progress()) { 212 ShenandoahEvacOOMScope oom_evac_scope; 213 ShenandoahUpdateRefsForOopClosure</* wb = */ true> cl; 214 obj->oop_iterate(&cl); 215 } else { 216 ShenandoahUpdateRefsForOopClosure</* wb = */ false> cl; 217 obj->oop_iterate(&cl); 218 } 219 } 220 221 oop ShenandoahBarrierSet::load_reference_barrier_not_null(oop obj) { 222 if (ShenandoahLoadRefBarrier && _heap->has_forwarded_objects()) { 223 return load_reference_barrier_impl(obj); 224 } else { 225 return obj; 226 } 227 } 228 229 oop ShenandoahBarrierSet::load_reference_barrier(oop obj) { 230 if (obj != NULL) { 231 return load_reference_barrier_not_null(obj); 232 } else { 233 return obj; 234 } 235 } 236 237 238 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj) { 239 assert(ShenandoahLoadRefBarrier, "should be enabled"); 240 assert(_heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL), "evac should be in progress"); 241 shenandoah_assert_in_cset(NULL, obj); 242 243 oop fwd = resolve_forwarded_not_null(obj); 244 if (oopDesc::equals_raw(obj, fwd)) { 245 ShenandoahEvacOOMScope oom_evac_scope; 246 247 Thread* thread = Thread::current(); 248 oop res_oop = _heap->evacuate_object(obj, thread); 249 250 // Since we are already here and paid the price of getting through runtime call adapters 251 // and acquiring oom-scope, it makes sense to try and evacuate more adjacent objects, 252 // thus amortizing the overhead. For sparsely live heaps, scan costs easily dominate 253 // total assist costs, and can introduce a lot of evacuation latency. This is why we 254 // only scan for _nearest_ N objects, regardless if they are eligible for evac or not. 255 // The scan itself should also avoid touching the non-marked objects below TAMS, because 256 // their metadata (notably, klasses) may be incorrect already. 257 258 size_t max = ShenandoahEvacAssist; 259 if (max > 0) { 260 // Traversal is special: it uses incomplete marking context, because it coalesces evac with mark. 261 // Other code uses complete marking context, because evac happens after the mark. 262 ShenandoahMarkingContext* ctx = _heap->is_concurrent_traversal_in_progress() ? 263 _heap->marking_context() : _heap->complete_marking_context(); 264 265 ShenandoahHeapRegion* r = _heap->heap_region_containing(obj); 266 assert(r->is_cset(), "sanity"); 267 268 HeapWord* cur = (HeapWord*)obj + obj->size() + ShenandoahBrooksPointer::word_size(); 269 270 size_t count = 0; 271 while ((cur < r->top()) && ctx->is_marked(oop(cur)) && (count++ < max)) { 272 oop cur_oop = oop(cur); 273 if (oopDesc::equals_raw(cur_oop, resolve_forwarded_not_null(cur_oop))) { 274 _heap->evacuate_object(cur_oop, thread); 275 } 276 cur = cur + cur_oop->size() + ShenandoahBrooksPointer::word_size(); 277 } 278 } 279 280 return res_oop; 281 } 282 return fwd; 283 } 284 285 oop ShenandoahBarrierSet::load_reference_barrier_impl(oop obj) { 286 assert(ShenandoahLoadRefBarrier, "should be enabled"); 287 if (!CompressedOops::is_null(obj)) { 288 bool evac_in_progress = _heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL); 289 oop fwd = resolve_forwarded_not_null(obj); 290 if (evac_in_progress && 291 _heap->in_collection_set(obj) && 292 oopDesc::equals_raw(obj, fwd)) { 293 Thread *t = Thread::current(); 294 if (t->is_GC_task_thread()) { 295 return _heap->evacuate_object(obj, t); 296 } else { 297 ShenandoahEvacOOMScope oom_evac_scope; 298 return _heap->evacuate_object(obj, t); 299 } 300 } else { 301 return fwd; 302 } 303 } else { 304 return obj; 305 } 306 } 307 308 void ShenandoahBarrierSet::storeval_barrier(oop obj) { 309 if (ShenandoahStoreValEnqueueBarrier && !CompressedOops::is_null(obj) && _heap->is_concurrent_traversal_in_progress()) { 310 enqueue(obj); 311 } 312 } 313 314 void ShenandoahBarrierSet::keep_alive_barrier(oop obj) { 315 if (ShenandoahKeepAliveBarrier && _heap->is_concurrent_mark_in_progress()) { 316 enqueue(obj); 317 } 318 } 319 320 void ShenandoahBarrierSet::enqueue(oop obj) { 321 shenandoah_assert_not_forwarded_if(NULL, obj, _heap->is_concurrent_traversal_in_progress()); 322 assert(_satb_mark_queue_set.is_active(), "only get here when SATB active"); 323 324 // Filter marked objects before hitting the SATB queues. The same predicate would 325 // be used by SATBMQ::filter to eliminate already marked objects downstream, but 326 // filtering here helps to avoid wasteful SATB queueing work to begin with. 327 if (!_heap->requires_marking<false>(obj)) return; 328 329 ShenandoahThreadLocalData::satb_mark_queue(Thread::current()).enqueue_known_active(obj); 330 } 331 332 void ShenandoahBarrierSet::on_thread_create(Thread* thread) { 333 // Create thread local data 334 ShenandoahThreadLocalData::create(thread); 335 } 336 337 void ShenandoahBarrierSet::on_thread_destroy(Thread* thread) { 338 // Destroy thread local data 339 ShenandoahThreadLocalData::destroy(thread); 340 } 341 342 void ShenandoahBarrierSet::on_thread_attach(Thread *thread) { 343 assert(!thread->is_Java_thread() || !SafepointSynchronize::is_at_safepoint(), 344 "We should not be at a safepoint"); 345 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread); 346 assert(!queue.is_active(), "SATB queue should not be active"); 347 assert( queue.is_empty(), "SATB queue should be empty"); 348 queue.set_active(_satb_mark_queue_set.is_active()); 349 if (thread->is_Java_thread()) { 350 ShenandoahThreadLocalData::set_gc_state(thread, _heap->gc_state()); 351 ShenandoahThreadLocalData::initialize_gclab(thread); 352 } 353 } 354 355 void ShenandoahBarrierSet::on_thread_detach(Thread *thread) { 356 SATBMarkQueue& queue = ShenandoahThreadLocalData::satb_mark_queue(thread); 357 queue.flush(); 358 if (thread->is_Java_thread()) { 359 PLAB* gclab = ShenandoahThreadLocalData::gclab(thread); 360 if (gclab != NULL) { 361 gclab->retire(); 362 } 363 } 364 }