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