47 #endif
48
49 class G1BarrierSetC1;
50 class G1BarrierSetC2;
51
52 G1BarrierSet::G1BarrierSet(G1CardTable* card_table) :
53 CardTableBarrierSet(make_barrier_set_assembler<G1BarrierSetAssembler>(),
54 make_barrier_set_c1<G1BarrierSetC1>(),
55 make_barrier_set_c2<G1BarrierSetC2>(),
56 card_table,
57 BarrierSet::FakeRtti(BarrierSet::G1BarrierSet)),
58 _satb_mark_queue_buffer_allocator("SATB Buffer Allocator", G1SATBBufferSize),
59 _dirty_card_queue_buffer_allocator("DC Buffer Allocator", G1UpdateBufferSize),
60 _satb_mark_queue_set(),
61 _dirty_card_queue_set()
62 {}
63
64 void G1BarrierSet::enqueue(oop pre_val) {
65 // Nulls should have been already filtered.
66 assert(oopDesc::is_oop(pre_val, true), "Error");
67
68 G1SATBMarkQueueSet& queue_set = satb_mark_queue_set();
69 if (!queue_set.is_active()) {
70 return;
71 }
72 Thread* thr = Thread::current();
73 if (thr->is_Java_thread()) {
74 G1ThreadLocalData::satb_mark_queue(thr).enqueue(pre_val);
75 } else {
76 MutexLockerEx x(Shared_SATB_Q_lock, Mutex::_no_safepoint_check_flag);
77 queue_set.shared_satb_queue()->enqueue(pre_val);
78 }
79 }
80
81 template <class T> void
82 G1BarrierSet::write_ref_array_pre_work(T* dst, size_t count) {
83 if (!_satb_mark_queue_set.is_active()) return;
84 T* elem_ptr = dst;
85 for (size_t i = 0; i < count; i++, elem_ptr++) {
86 T heap_oop = RawAccess<>::oop_load(elem_ptr);
87 if (!CompressedOops::is_null(heap_oop)) {
88 enqueue(CompressedOops::decode_not_null(heap_oop));
89 }
90 }
91 }
92
93 void G1BarrierSet::write_ref_array_pre(oop* 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_array_pre(narrowOop* dst, size_t count, bool dest_uninitialized) {
100 if (!dest_uninitialized) {
101 write_ref_array_pre_work(dst, count);
102 }
103 }
104
105 void G1BarrierSet::write_ref_field_post_slow(volatile jbyte* byte) {
106 // In the slow path, we know a card is not young
107 assert(*byte != G1CardTable::g1_young_card_val(), "slow path invoked without filtering");
108 OrderAccess::storeload();
109 if (*byte != G1CardTable::dirty_card_val()) {
110 *byte = G1CardTable::dirty_card_val();
111 Thread* thr = Thread::current();
112 if (thr->is_Java_thread()) {
113 G1ThreadLocalData::dirty_card_queue(thr).enqueue(byte);
114 } else {
115 MutexLockerEx x(Shared_DirtyCardQ_lock,
116 Mutex::_no_safepoint_check_flag);
117 _dirty_card_queue_set.shared_dirty_card_queue()->enqueue(byte);
118 }
119 }
120 }
121
122 void G1BarrierSet::invalidate(MemRegion mr) {
123 if (mr.is_empty()) {
124 return;
125 }
126 volatile jbyte* byte = _card_table->byte_for(mr.start());
127 jbyte* last_byte = _card_table->byte_for(mr.last());
128 Thread* thr = Thread::current();
129 // skip all consecutive young cards
130 for (; byte <= last_byte && *byte == G1CardTable::g1_young_card_val(); byte++);
131
132 if (byte <= last_byte) {
133 OrderAccess::storeload();
134 // Enqueue if necessary.
135 if (thr->is_Java_thread()) {
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 G1ThreadLocalData::dirty_card_queue(thr).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 _dirty_card_queue_set.shared_dirty_card_queue()->enqueue(byte);
155 }
156 }
157 }
158 }
159 }
160
161 void G1BarrierSet::on_thread_create(Thread* thread) {
162 // Create thread local data
163 G1ThreadLocalData::create(thread);
164 }
165
166 void G1BarrierSet::on_thread_destroy(Thread* thread) {
167 // Destroy thread local data
168 G1ThreadLocalData::destroy(thread);
169 }
170
171 void G1BarrierSet::on_thread_attach(JavaThread* thread) {
172 // This method initializes the SATB and dirty card queues before a
173 // JavaThread is added to the Java thread list. Right now, we don't
174 // have to do anything to the dirty card queue (it should have been
175 // activated when the thread was created), but we have to activate
176 // the SATB queue if the thread is created while a marking cycle is
177 // in progress. The activation / de-activation of the SATB queues at
178 // the beginning / end of a marking cycle is done during safepoints
179 // so we have to make sure this method is called outside one to be
180 // able to safely read the active field of the SATB queue set. Right
181 // now, it is called just before the thread is added to the Java
182 // thread list in the Threads::add() method. That method is holding
183 // the Threads_lock which ensures we are outside a safepoint. We
184 // cannot do the obvious and set the active field of the SATB queue
185 // when the thread is created given that, in some cases, safepoints
186 // might happen between the JavaThread constructor being called and the
187 // thread being added to the Java thread list (an example of this is
188 // when the structure for the DestroyJavaVM thread is created).
189 assert(!SafepointSynchronize::is_at_safepoint(), "We should not be at a safepoint");
190 assert(!G1ThreadLocalData::satb_mark_queue(thread).is_active(), "SATB queue should not be active");
191 assert(G1ThreadLocalData::satb_mark_queue(thread).is_empty(), "SATB queue should be empty");
192 assert(G1ThreadLocalData::dirty_card_queue(thread).is_active(), "Dirty card queue should be active");
193
194 // If we are creating the thread during a marking cycle, we should
195 // set the active field of the SATB queue to true.
196 if (_satb_mark_queue_set.is_active()) {
197 G1ThreadLocalData::satb_mark_queue(thread).set_active(true);
198 }
199 }
200
201 void G1BarrierSet::on_thread_detach(JavaThread* thread) {
202 // Flush any deferred card marks, SATB buffers and dirty card queue buffers
203 CardTableBarrierSet::on_thread_detach(thread);
204 G1ThreadLocalData::satb_mark_queue(thread).flush();
205 G1ThreadLocalData::dirty_card_queue(thread).flush();
206 }
207
208 BufferNode::Allocator& G1BarrierSet::satb_mark_queue_buffer_allocator() {
209 return _satb_mark_queue_buffer_allocator;
210 }
211
212 BufferNode::Allocator& G1BarrierSet::dirty_card_queue_buffer_allocator() {
213 return _dirty_card_queue_buffer_allocator;
214 }
|
47 #endif
48
49 class G1BarrierSetC1;
50 class G1BarrierSetC2;
51
52 G1BarrierSet::G1BarrierSet(G1CardTable* card_table) :
53 CardTableBarrierSet(make_barrier_set_assembler<G1BarrierSetAssembler>(),
54 make_barrier_set_c1<G1BarrierSetC1>(),
55 make_barrier_set_c2<G1BarrierSetC2>(),
56 card_table,
57 BarrierSet::FakeRtti(BarrierSet::G1BarrierSet)),
58 _satb_mark_queue_buffer_allocator("SATB Buffer Allocator", G1SATBBufferSize),
59 _dirty_card_queue_buffer_allocator("DC Buffer Allocator", G1UpdateBufferSize),
60 _satb_mark_queue_set(),
61 _dirty_card_queue_set()
62 {}
63
64 void G1BarrierSet::enqueue(oop pre_val) {
65 // Nulls should have been already filtered.
66 assert(oopDesc::is_oop(pre_val, true), "Error");
67 G1ThreadLocalData::satb_mark_queue(Thread::current()).enqueue(pre_val);
68 }
69
70 template <class T> void
71 G1BarrierSet::write_ref_array_pre_work(T* dst, size_t count) {
72 if (!_satb_mark_queue_set.is_active()) return;
73 T* elem_ptr = dst;
74 for (size_t i = 0; i < count; i++, elem_ptr++) {
75 T heap_oop = RawAccess<>::oop_load(elem_ptr);
76 if (!CompressedOops::is_null(heap_oop)) {
77 enqueue(CompressedOops::decode_not_null(heap_oop));
78 }
79 }
80 }
81
82 void G1BarrierSet::write_ref_array_pre(oop* dst, size_t count, bool dest_uninitialized) {
83 if (!dest_uninitialized) {
84 write_ref_array_pre_work(dst, count);
85 }
86 }
87
88 void G1BarrierSet::write_ref_array_pre(narrowOop* dst, size_t count, bool dest_uninitialized) {
89 if (!dest_uninitialized) {
90 write_ref_array_pre_work(dst, count);
91 }
92 }
93
94 void G1BarrierSet::write_ref_field_post_slow(volatile jbyte* byte) {
95 // In the slow path, we know a card is not young
96 assert(*byte != G1CardTable::g1_young_card_val(), "slow path invoked without filtering");
97 OrderAccess::storeload();
98 if (*byte != G1CardTable::dirty_card_val()) {
99 *byte = G1CardTable::dirty_card_val();
100 Thread* thr = Thread::current();
101 G1ThreadLocalData::dirty_card_queue(thr).enqueue(byte);
102 }
103 }
104
105 void G1BarrierSet::invalidate(MemRegion mr) {
106 if (mr.is_empty()) {
107 return;
108 }
109 volatile jbyte* byte = _card_table->byte_for(mr.start());
110 jbyte* last_byte = _card_table->byte_for(mr.last());
111 // skip initial young cards
112 for (; byte <= last_byte && *byte == G1CardTable::g1_young_card_val(); byte++);
113
114 if (byte <= last_byte) {
115 OrderAccess::storeload();
116 // Enqueue if necessary.
117 Thread* thr = Thread::current();
118 G1DirtyCardQueue& queue = G1ThreadLocalData::dirty_card_queue(thr);
119 for (; byte <= last_byte; byte++) {
120 jbyte bv = *byte;
121 if ((bv != G1CardTable::g1_young_card_val()) &&
122 (bv != G1CardTable::dirty_card_val())) {
123 *byte = G1CardTable::dirty_card_val();
124 queue.enqueue(byte);
125 }
126 }
127 }
128 }
129
130 void G1BarrierSet::on_thread_create(Thread* thread) {
131 // Create thread local data
132 G1ThreadLocalData::create(thread);
133 }
134
135 void G1BarrierSet::on_thread_destroy(Thread* thread) {
136 // Destroy thread local data
137 G1ThreadLocalData::destroy(thread);
138 }
139
140 void G1BarrierSet::on_thread_attach(Thread* thread) {
141 assert(!G1ThreadLocalData::satb_mark_queue(thread).is_active(), "SATB queue should not be active");
142 assert(G1ThreadLocalData::satb_mark_queue(thread).is_empty(), "SATB queue should be empty");
143 assert(G1ThreadLocalData::dirty_card_queue(thread).is_active(), "Dirty card queue should be active");
144 // Can't assert that the DCQ is empty. There is early execution on
145 // the main thread, before it gets added to the threads list, which
146 // is where this is called. That execution may enqueue dirty cards.
147
148 // If we are creating the thread during a marking cycle, we should
149 // set the active field of the SATB queue to true. That involves
150 // copying the global is_active value to this thread's queue, which
151 // is done without any direct synchronization here.
152 //
153 // The activation and deactivation of the SATB queues occurs at the
154 // beginning / end of a marking cycle, and is done during
155 // safepoints. This function is called just before a thread is
156 // added to its corresponding threads list (for Java or non-Java
157 // threads, respectively).
158 //
159 // For Java threads, that's done while holding the Threads_lock,
160 // which ensures we're not at a safepoint, so reading the global
161 // is_active state is synchronized against update.
162 assert(!thread->is_Java_thread() || !SafepointSynchronize::is_at_safepoint(),
163 "Should not be at a safepoint");
164 // For non-Java threads, thread creation (and list addition) may,
165 // and indeed usually does, occur during a safepoint. But such
166 // creation isn't concurrent with updating the global SATB active
167 // state.
168 bool is_satb_active = _satb_mark_queue_set.is_active();
169 G1ThreadLocalData::satb_mark_queue(thread).set_active(is_satb_active);
170 }
171
172 void G1BarrierSet::on_thread_detach(Thread* thread) {
173 // Flush any deferred card marks.
174 CardTableBarrierSet::on_thread_detach(thread);
175 G1ThreadLocalData::satb_mark_queue(thread).flush();
176 G1ThreadLocalData::dirty_card_queue(thread).flush();
177 }
178
179 BufferNode::Allocator& G1BarrierSet::satb_mark_queue_buffer_allocator() {
180 return _satb_mark_queue_buffer_allocator;
181 }
182
183 BufferNode::Allocator& G1BarrierSet::dirty_card_queue_buffer_allocator() {
184 return _dirty_card_queue_buffer_allocator;
185 }
|