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/g1CardTable.inline.hpp" 28 #include "gc/g1/g1CollectedHeap.inline.hpp" 29 #include "gc/g1/heapRegion.hpp" 30 #include "gc/g1/satbMarkQueue.hpp" 31 #include "logging/log.hpp" 32 #include "oops/access.inline.hpp" 33 #include "oops/compressedOops.inline.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "runtime/mutexLocker.hpp" 36 #include "runtime/thread.inline.hpp" 37 38 G1BarrierSet::G1BarrierSet(G1CardTable* card_table) : 39 CardTableModRefBS(card_table, BarrierSet::FakeRtti(BarrierSet::G1BarrierSet)), 40 _dcqs(JavaThread::dirty_card_queue_set()) 41 { } 42 43 void G1BarrierSet::enqueue(oop pre_val) { 44 // Nulls should have been already filtered. 45 assert(oopDesc::is_oop(pre_val, true), "Error"); 46 47 if (!JavaThread::satb_mark_queue_set().is_active()) return; 48 Thread* thr = Thread::current(); 49 if (thr->is_Java_thread()) { 50 JavaThread* jt = (JavaThread*)thr; 51 jt->satb_mark_queue().enqueue(pre_val); 52 } else { 53 MutexLockerEx x(Shared_SATB_Q_lock, Mutex::_no_safepoint_check_flag); 54 JavaThread::satb_mark_queue_set().shared_satb_queue()->enqueue(pre_val); 55 } 56 } 57 58 template <class T> void 59 G1BarrierSet::write_ref_array_pre_work(T* dst, int count) { 60 if (!JavaThread::satb_mark_queue_set().is_active()) return; 61 T* elem_ptr = dst; 62 for (int i = 0; i < count; i++, elem_ptr++) { 63 T heap_oop = RawAccess<>::oop_load(elem_ptr); 64 if (!CompressedOops::is_null(heap_oop)) { 65 enqueue(CompressedOops::decode_not_null(heap_oop)); 66 } 67 } 68 } 69 70 void G1BarrierSet::write_ref_array_pre(oop* dst, int count, bool dest_uninitialized) { 71 if (!dest_uninitialized) { 72 write_ref_array_pre_work(dst, count); 73 } 74 } 75 76 void G1BarrierSet::write_ref_array_pre(narrowOop* dst, int count, bool dest_uninitialized) { 77 if (!dest_uninitialized) { 78 write_ref_array_pre_work(dst, count); 79 } 80 } 81 82 void G1BarrierSet::write_ref_field_post_slow(volatile jbyte* byte) { 83 // In the slow path, we know a card is not young 84 assert(*byte != G1CardTable::g1_young_card_val(), "slow path invoked without filtering"); 85 OrderAccess::storeload(); 86 if (*byte != G1CardTable::dirty_card_val()) { 87 *byte = G1CardTable::dirty_card_val(); 88 Thread* thr = Thread::current(); 89 if (thr->is_Java_thread()) { 90 JavaThread* jt = (JavaThread*)thr; 91 jt->dirty_card_queue().enqueue(byte); 92 } else { 93 MutexLockerEx x(Shared_DirtyCardQ_lock, 94 Mutex::_no_safepoint_check_flag); 95 _dcqs.shared_dirty_card_queue()->enqueue(byte); 96 } 97 } 98 } 99 100 void G1BarrierSet::invalidate(MemRegion mr) { 101 if (mr.is_empty()) { 102 return; 103 } 104 volatile jbyte* byte = _card_table->byte_for(mr.start()); 105 jbyte* last_byte = _card_table->byte_for(mr.last()); 106 Thread* thr = Thread::current(); 107 // skip all consecutive young cards 108 for (; byte <= last_byte && *byte == G1CardTable::g1_young_card_val(); byte++); 109 110 if (byte <= last_byte) { 111 OrderAccess::storeload(); 112 // Enqueue if necessary. 113 if (thr->is_Java_thread()) { 114 JavaThread* jt = (JavaThread*)thr; 115 for (; byte <= last_byte; byte++) { 116 if (*byte == G1CardTable::g1_young_card_val()) { 117 continue; 118 } 119 if (*byte != G1CardTable::dirty_card_val()) { 120 *byte = G1CardTable::dirty_card_val(); 121 jt->dirty_card_queue().enqueue(byte); 122 } 123 } 124 } else { 125 MutexLockerEx x(Shared_DirtyCardQ_lock, 126 Mutex::_no_safepoint_check_flag); 127 for (; byte <= last_byte; byte++) { 128 if (*byte == G1CardTable::g1_young_card_val()) { 129 continue; 130 } 131 if (*byte != G1CardTable::dirty_card_val()) { 132 *byte = G1CardTable::dirty_card_val(); 133 _dcqs.shared_dirty_card_queue()->enqueue(byte); 134 } 135 } 136 } 137 } 138 } 139 140 void G1BarrierSet::on_thread_attach(JavaThread* thread) { 141 // This method initializes the SATB and dirty card queues before a 142 // JavaThread is added to the Java thread list. Right now, we don't 143 // have to do anything to the dirty card queue (it should have been 144 // activated when the thread was created), but we have to activate 145 // the SATB queue if the thread is created while a marking cycle is 146 // in progress. The activation / de-activation of the SATB queues at 147 // the beginning / end of a marking cycle is done during safepoints 148 // so we have to make sure this method is called outside one to be 149 // able to safely read the active field of the SATB queue set. Right 150 // now, it is called just before the thread is added to the Java 151 // thread list in the Threads::add() method. That method is holding 152 // the Threads_lock which ensures we are outside a safepoint. We 153 // cannot do the obvious and set the active field of the SATB queue 154 // when the thread is created given that, in some cases, safepoints 155 // might happen between the JavaThread constructor being called and the 156 // thread being added to the Java thread list (an example of this is 157 // when the structure for the DestroyJavaVM thread is created). 158 assert(!SafepointSynchronize::is_at_safepoint(), "We should not be at a safepoint"); 159 assert(!thread->satb_mark_queue().is_active(), "SATB queue should not be active"); 160 assert(thread->satb_mark_queue().is_empty(), "SATB queue should be empty"); 161 assert(thread->dirty_card_queue().is_active(), "Dirty card queue should be active"); 162 163 // If we are creating the thread during a marking cycle, we should 164 // set the active field of the SATB queue to true. 165 if (thread->satb_mark_queue_set().is_active()) { 166 thread->satb_mark_queue().set_active(true); 167 } 168 } 169 170 void G1BarrierSet::on_thread_detach(JavaThread* thread) { 171 // Flush any deferred card marks, SATB buffers and dirty card queue buffers 172 CardTableModRefBS::on_thread_detach(thread); 173 thread->satb_mark_queue().flush(); 174 thread->dirty_card_queue().flush(); 175 }