1 /* 2 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "gc/g1/g1BarrierSet.inline.hpp" 27 #include "gc/g1/g1BSCodeGen.hpp" 28 #include "gc/g1/g1CardTable.inline.hpp" 29 #include "gc/g1/g1CollectedHeap.inline.hpp" 30 #include "gc/g1/heapRegion.hpp" 31 #include "gc/g1/satbMarkQueue.hpp" 32 #include "logging/log.hpp" 33 #include "oops/oop.inline.hpp" 34 #include "runtime/mutexLocker.hpp" 35 #include "runtime/thread.inline.hpp" 36 37 G1BarrierSet::G1BarrierSet(G1CardTable* card_table) : 38 CardTableModRefBS(card_table, BarrierSet::FakeRtti(BarrierSet::G1BarrierSet)), 39 _dcqs(JavaThread::dirty_card_queue_set()) 40 { } 41 42 void G1BarrierSet::enqueue(oop pre_val) { 43 // Nulls should have been already filtered. 44 assert(oopDesc::is_oop(pre_val, true), "Error"); 45 46 if (!JavaThread::satb_mark_queue_set().is_active()) return; 47 Thread* thr = Thread::current(); 48 if (thr->is_Java_thread()) { 49 JavaThread* jt = (JavaThread*)thr; 50 jt->satb_mark_queue().enqueue(pre_val); 51 } else { 52 MutexLockerEx x(Shared_SATB_Q_lock, Mutex::_no_safepoint_check_flag); 53 JavaThread::satb_mark_queue_set().shared_satb_queue()->enqueue(pre_val); 54 } 55 } 56 57 BarrierSetCodeGen *G1BarrierSet::make_code_gen() { 58 return new G1BSCodeGen(); 59 } 60 61 void G1BarrierSet::write_ref_array_pre_oop_entry(oop* dst, size_t length) { 62 assert(length <= (size_t)max_intx, "count too large"); 63 G1BarrierSet *bs = barrier_set_cast<G1BarrierSet>(BarrierSet::barrier_set()); 64 bs->G1BarrierSet::write_ref_array_pre(dst, (int)length, false); 65 } 66 67 void G1BarrierSet::write_ref_array_pre_narrow_oop_entry(narrowOop* dst, size_t length) { 68 assert(length <= (size_t)max_intx, "count too large"); 69 G1BarrierSet *bs = barrier_set_cast<G1BarrierSet>(BarrierSet::barrier_set()); 70 bs->G1BarrierSet::write_ref_array_pre(dst, (int)length, false); 71 } 72 73 void G1BarrierSet::write_ref_array_post_entry(HeapWord* dst, size_t length) { 74 G1BarrierSet *bs = barrier_set_cast<G1BarrierSet>(BarrierSet::barrier_set()); 75 bs->G1BarrierSet::write_ref_array(dst, (int)length); 76 } 77 78 79 template <class T> void 80 G1BarrierSet::write_ref_array_pre_work(T* dst, int count) { 81 if (!JavaThread::satb_mark_queue_set().is_active()) return; 82 T* elem_ptr = dst; 83 for (int i = 0; i < count; i++, elem_ptr++) { 84 T heap_oop = oopDesc::load_heap_oop(elem_ptr); 85 if (!oopDesc::is_null(heap_oop)) { 86 enqueue(oopDesc::decode_heap_oop_not_null(heap_oop)); 87 } 88 } 89 } 90 91 void G1BarrierSet::write_ref_array_pre(oop* dst, int count, bool dest_uninitialized) { 92 if (!dest_uninitialized) { 93 write_ref_array_pre_work(dst, count); 94 } 95 } 96 97 void G1BarrierSet::write_ref_array_pre(narrowOop* dst, int count, bool dest_uninitialized) { 98 if (!dest_uninitialized) { 99 write_ref_array_pre_work(dst, count); 100 } 101 } 102 103 void G1BarrierSet::write_ref_field_post_slow(volatile jbyte* byte) { 104 // In the slow path, we know a card is not young 105 assert(*byte != G1CardTable::g1_young_card_val(), "slow path invoked without filtering"); 106 OrderAccess::storeload(); 107 if (*byte != G1CardTable::dirty_card_val()) { 108 *byte = G1CardTable::dirty_card_val(); 109 Thread* thr = Thread::current(); 110 if (thr->is_Java_thread()) { 111 JavaThread* jt = (JavaThread*)thr; 112 jt->dirty_card_queue().enqueue(byte); 113 } else { 114 MutexLockerEx x(Shared_DirtyCardQ_lock, 115 Mutex::_no_safepoint_check_flag); 116 _dcqs.shared_dirty_card_queue()->enqueue(byte); 117 } 118 } 119 } 120 121 void G1BarrierSet::invalidate(MemRegion mr) { 122 if (mr.is_empty()) { 123 return; 124 } 125 volatile jbyte* byte = _card_table->byte_for(mr.start()); 126 jbyte* last_byte = _card_table->byte_for(mr.last()); 127 Thread* thr = Thread::current(); 128 // skip all consecutive young cards 129 for (; byte <= last_byte && *byte == G1CardTable::g1_young_card_val(); byte++); 130 131 if (byte <= last_byte) { 132 OrderAccess::storeload(); 133 // Enqueue if necessary. 134 if (thr->is_Java_thread()) { 135 JavaThread* jt = (JavaThread*)thr; 136 for (; byte <= last_byte; byte++) { 137 if (*byte == G1CardTable::g1_young_card_val()) { 138 continue; 139 } 140 if (*byte != G1CardTable::dirty_card_val()) { 141 *byte = G1CardTable::dirty_card_val(); 142 jt->dirty_card_queue().enqueue(byte); 143 } 144 } 145 } else { 146 MutexLockerEx x(Shared_DirtyCardQ_lock, 147 Mutex::_no_safepoint_check_flag); 148 for (; byte <= last_byte; byte++) { 149 if (*byte == G1CardTable::g1_young_card_val()) { 150 continue; 151 } 152 if (*byte != G1CardTable::dirty_card_val()) { 153 *byte = G1CardTable::dirty_card_val(); 154 _dcqs.shared_dirty_card_queue()->enqueue(byte); 155 } 156 } 157 } 158 } 159 } 160 161 void G1BarrierSet::on_thread_attach(JavaThread* thread) { 162 // This method initializes the SATB and dirty card queues before a 163 // JavaThread is added to the Java thread list. Right now, we don't 164 // have to do anything to the dirty card queue (it should have been 165 // activated when the thread was created), but we have to activate 166 // the SATB queue if the thread is created while a marking cycle is 167 // in progress. The activation / de-activation of the SATB queues at 168 // the beginning / end of a marking cycle is done during safepoints 169 // so we have to make sure this method is called outside one to be 170 // able to safely read the active field of the SATB queue set. Right 171 // now, it is called just before the thread is added to the Java 172 // thread list in the Threads::add() method. That method is holding 173 // the Threads_lock which ensures we are outside a safepoint. We 174 // cannot do the obvious and set the active field of the SATB queue 175 // when the thread is created given that, in some cases, safepoints 176 // might happen between the JavaThread constructor being called and the 177 // thread being added to the Java thread list (an example of this is 178 // when the structure for the DestroyJavaVM thread is created). 179 assert(!SafepointSynchronize::is_at_safepoint(), "We should not be at a safepoint"); 180 assert(!thread->satb_mark_queue().is_active(), "SATB queue should not be active"); 181 assert(thread->satb_mark_queue().is_empty(), "SATB queue should be empty"); 182 assert(thread->dirty_card_queue().is_active(), "Dirty card queue should be active"); 183 184 // If we are creating the thread during a marking cycle, we should 185 // set the active field of the SATB queue to true. 186 if (thread->satb_mark_queue_set().is_active()) { 187 thread->satb_mark_queue().set_active(true); 188 } 189 } 190 191 void G1BarrierSet::on_thread_detach(JavaThread* thread) { 192 // Flush any deferred card marks, SATB buffers and dirty card queue buffers 193 CardTableModRefBS::on_thread_detach(thread); 194 thread->satb_mark_queue().flush(); 195 thread->dirty_card_queue().flush(); 196 }