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/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/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 ShenandoahBarrierSet::ShenandoahBarrierSet(ShenandoahHeap* heap) : 48 BarrierSet(make_barrier_set_assembler<ShenandoahBarrierSetAssembler>(), 49 make_barrier_set_c1<ShenandoahBarrierSetC1>(), 50 make_barrier_set_c2<ShenandoahBarrierSetC2>(), 51 BarrierSet::FakeRtti(BarrierSet::ShenandoahBarrierSet)), 52 _heap(heap) 53 { 54 } 55 56 ShenandoahBarrierSetAssembler* ShenandoahBarrierSet::assembler() { 57 BarrierSetAssembler* const bsa = BarrierSet::barrier_set()->barrier_set_assembler(); 58 return reinterpret_cast<ShenandoahBarrierSetAssembler*>(bsa); 59 } 60 61 void ShenandoahBarrierSet::print_on(outputStream* st) const { 62 st->print("ShenandoahBarrierSet"); 63 } 64 65 bool ShenandoahBarrierSet::is_a(BarrierSet::Name bsn) { 66 return bsn == BarrierSet::ShenandoahBarrierSet; 67 } 68 69 bool ShenandoahBarrierSet::is_aligned(HeapWord* hw) { 70 return true; 71 } 72 73 bool ShenandoahBarrierSet::need_load_reference_barrier(DecoratorSet decorators, BasicType type) { 74 if (!ShenandoahLoadRefBarrier) return false; 75 // Only needed for references 76 return is_reference_type(type); 77 } 78 79 bool ShenandoahBarrierSet::need_keep_alive_barrier(DecoratorSet decorators,BasicType type) { 80 if (!ShenandoahKeepAliveBarrier) return false; 81 // Only needed for references 82 if (!is_reference_type(type)) return false; 83 84 bool keep_alive = (decorators & AS_NO_KEEPALIVE) == 0; 85 bool unknown = (decorators & ON_UNKNOWN_OOP_REF) != 0; 86 bool is_traversal_mode = ShenandoahHeap::heap()->is_traversal_mode(); 87 bool on_weak_ref = (decorators & (ON_WEAK_OOP_REF | ON_PHANTOM_OOP_REF)) != 0; 88 return (on_weak_ref || unknown) && (keep_alive || is_traversal_mode); 89 } 90 91 oop ShenandoahBarrierSet::load_reference_barrier_not_null(oop obj) { 92 if (ShenandoahLoadRefBarrier && _heap->has_forwarded_objects()) { 93 return load_reference_barrier_impl(obj); 94 } else { 95 return obj; 96 } 97 } 98 99 oop ShenandoahBarrierSet::load_reference_barrier(oop obj) { 100 if (obj != NULL) { 101 return load_reference_barrier_not_null(obj); 102 } else { 103 return obj; 104 } 105 } 106 107 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, oop* load_addr) { 108 return load_reference_barrier_mutator_work(obj, load_addr); 109 } 110 111 oop ShenandoahBarrierSet::load_reference_barrier_mutator(oop obj, narrowOop* load_addr) { 112 return load_reference_barrier_mutator_work(obj, load_addr); 113 } 114 115 template <class T> 116 oop ShenandoahBarrierSet::load_reference_barrier_mutator_work(oop obj, T* load_addr) { 117 assert(ShenandoahLoadRefBarrier, "should be enabled"); 118 shenandoah_assert_in_cset(load_addr, obj); 119 120 oop fwd = resolve_forwarded_not_null(obj); 121 if (obj == fwd) { 122 assert(_heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL), 123 "evac should be in progress"); 124 125 ShenandoahEvacOOMScope oom_evac_scope; 126 127 Thread* thread = Thread::current(); 128 oop res_oop = _heap->evacuate_object(obj, thread); 129 130 // Since we are already here and paid the price of getting through runtime call adapters 131 // and acquiring oom-scope, it makes sense to try and evacuate more adjacent objects, 132 // thus amortizing the overhead. For sparsely live heaps, scan costs easily dominate 133 // total assist costs, and can introduce a lot of evacuation latency. This is why we 134 // only scan for _nearest_ N objects, regardless if they are eligible for evac or not. 135 // The scan itself should also avoid touching the non-marked objects below TAMS, because 136 // their metadata (notably, klasses) may be incorrect already. 137 138 size_t max = ShenandoahEvacAssist; 139 if (max > 0) { 140 // Traversal is special: it uses incomplete marking context, because it coalesces evac with mark. 141 // Other code uses complete marking context, because evac happens after the mark. 142 ShenandoahMarkingContext* ctx = _heap->is_concurrent_traversal_in_progress() ? 143 _heap->marking_context() : _heap->complete_marking_context(); 144 145 ShenandoahHeapRegion* r = _heap->heap_region_containing(obj); 146 assert(r->is_cset(), "sanity"); 147 148 HeapWord* cur = (HeapWord*)obj + obj->size(); 149 150 size_t count = 0; 151 while ((cur < r->top()) && ctx->is_marked(oop(cur)) && (count++ < max)) { 152 oop cur_oop = oop(cur); 153 if (cur_oop == resolve_forwarded_not_null(cur_oop)) { 154 _heap->evacuate_object(cur_oop, thread); 155 } 156 cur = cur + cur_oop->size(); 157 } 158 } 159 160 fwd = res_oop; 161 } 162 163 if (load_addr != NULL && fwd != obj) { 164 // Since we are here and we know the load address, update the reference. 165 ShenandoahHeap::cas_oop(fwd, load_addr, obj); 166 } 167 168 return fwd; 169 } 170 171 oop ShenandoahBarrierSet::load_reference_barrier_impl(oop obj) { 172 assert(ShenandoahLoadRefBarrier, "should be enabled"); 173 if (!CompressedOops::is_null(obj)) { 174 bool evac_in_progress = _heap->is_gc_in_progress_mask(ShenandoahHeap::EVACUATION | ShenandoahHeap::TRAVERSAL); 175 oop fwd = resolve_forwarded_not_null(obj); 176 if (evac_in_progress && 177 _heap->in_collection_set(obj) && 178 obj == fwd) { 179 Thread *t = Thread::current(); 180 ShenandoahEvacOOMScope oom_evac_scope; 181 return _heap->evacuate_object(obj, t); 182 } else { 183 return fwd; 184 } 185 } else { 186 return obj; 187 } 188 } 189 190 void ShenandoahBarrierSet::on_thread_create(Thread* thread) { 191 // Create thread local data 192 ShenandoahThreadLocalData::create(thread); 193 } 194 195 void ShenandoahBarrierSet::on_thread_destroy(Thread* thread) { 196 // Destroy thread local data 197 ShenandoahThreadLocalData::destroy(thread); 198 } 199 200 void ShenandoahBarrierSet::on_thread_attach(JavaThread* thread) { 201 assert(!SafepointSynchronize::is_at_safepoint(), "We should not be at a safepoint"); 202 assert(!ShenandoahThreadLocalData::satb_mark_queue(thread).is_active(), "SATB queue should not be active"); 203 assert(ShenandoahThreadLocalData::satb_mark_queue(thread).is_empty(), "SATB queue should be empty"); 204 if (ShenandoahBarrierSet::satb_mark_queue_set().is_active()) { 205 ShenandoahThreadLocalData::satb_mark_queue(thread).set_active(true); 206 } 207 ShenandoahThreadLocalData::set_gc_state(thread, _heap->gc_state()); 208 ShenandoahThreadLocalData::initialize_gclab(thread); 209 } 210 211 void ShenandoahBarrierSet::on_thread_detach(JavaThread* thread) { 212 ShenandoahThreadLocalData::satb_mark_queue(thread).flush(); 213 PLAB* gclab = ShenandoahThreadLocalData::gclab(thread); 214 if (gclab != NULL) { 215 gclab->retire(); 216 } 217 } 218 219 void ShenandoahBarrierSet::clone_barrier_runtime(oop src) { 220 if (_heap->has_forwarded_objects()) { 221 clone_barrier(src); 222 } 223 } 224