|
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/shared/satbMarkQueue.hpp" |
|
27 #include "gc/shared/collectedHeap.hpp" |
|
28 #include "logging/log.hpp" |
|
29 #include "memory/allocation.inline.hpp" |
|
30 #include "oops/oop.inline.hpp" |
|
31 #include "runtime/mutexLocker.hpp" |
|
32 #include "runtime/os.hpp" |
|
33 #include "runtime/safepoint.hpp" |
|
34 #include "runtime/thread.hpp" |
|
35 #include "runtime/threadSMR.hpp" |
|
36 #include "runtime/vmThread.hpp" |
|
37 |
|
38 SATBMarkQueue::SATBMarkQueue(SATBMarkQueueSet* qset, bool permanent) : |
|
39 // SATB queues are only active during marking cycles. We create |
|
40 // them with their active field set to false. If a thread is |
|
41 // created during a cycle and its SATB queue needs to be activated |
|
42 // before the thread starts running, we'll need to set its active |
|
43 // field to true. This must be done in the collector-specific |
|
44 // BarrierSet::on_thread_attach() implementation. |
|
45 PtrQueue(qset, permanent, false /* active */) |
|
46 { } |
|
47 |
|
48 void SATBMarkQueue::flush() { |
|
49 // Filter now to possibly save work later. If filtering empties the |
|
50 // buffer then flush_impl can deallocate the buffer. |
|
51 filter(); |
|
52 flush_impl(); |
|
53 } |
|
54 |
|
55 // This method will first apply filtering to the buffer. If filtering |
|
56 // retains a small enough collection in the buffer, we can continue to |
|
57 // use the buffer as-is, instead of enqueueing and replacing it. |
|
58 |
|
59 bool SATBMarkQueue::should_enqueue_buffer() { |
|
60 assert(_lock == NULL || _lock->owned_by_self(), |
|
61 "we should have taken the lock before calling this"); |
|
62 |
|
63 // This method should only be called if there is a non-NULL buffer |
|
64 // that is full. |
|
65 assert(index() == 0, "pre-condition"); |
|
66 assert(_buf != NULL, "pre-condition"); |
|
67 |
|
68 filter(); |
|
69 |
|
70 SATBMarkQueueSet* satb_qset = static_cast<SATBMarkQueueSet*>(qset()); |
|
71 size_t threshold = satb_qset->buffer_enqueue_threshold(); |
|
72 // Ensure we'll enqueue completely full buffers. |
|
73 assert(threshold > 0, "enqueue threshold = 0"); |
|
74 // Ensure we won't enqueue empty buffers. |
|
75 assert(threshold <= capacity(), |
|
76 "enqueue threshold " SIZE_FORMAT " exceeds capacity " SIZE_FORMAT, |
|
77 threshold, capacity()); |
|
78 return index() < threshold; |
|
79 } |
|
80 |
|
81 void SATBMarkQueue::apply_closure_and_empty(SATBBufferClosure* cl) { |
|
82 assert(SafepointSynchronize::is_at_safepoint(), |
|
83 "SATB queues must only be processed at safepoints"); |
|
84 if (_buf != NULL) { |
|
85 cl->do_buffer(&_buf[index()], size()); |
|
86 reset(); |
|
87 } |
|
88 } |
|
89 |
|
90 #ifndef PRODUCT |
|
91 // Helpful for debugging |
|
92 |
|
93 static void print_satb_buffer(const char* name, |
|
94 void** buf, |
|
95 size_t index, |
|
96 size_t capacity) { |
|
97 tty->print_cr(" SATB BUFFER [%s] buf: " PTR_FORMAT " index: " SIZE_FORMAT |
|
98 " capacity: " SIZE_FORMAT, |
|
99 name, p2i(buf), index, capacity); |
|
100 } |
|
101 |
|
102 void SATBMarkQueue::print(const char* name) { |
|
103 print_satb_buffer(name, _buf, index(), capacity()); |
|
104 } |
|
105 |
|
106 #endif // PRODUCT |
|
107 |
|
108 SATBMarkQueueSet::SATBMarkQueueSet() : |
|
109 PtrQueueSet(), |
|
110 _shared_satb_queue(this, true /* permanent */), |
|
111 _buffer_enqueue_threshold(0) |
|
112 {} |
|
113 |
|
114 void SATBMarkQueueSet::initialize(Monitor* cbl_mon, Mutex* fl_lock, |
|
115 int process_completed_threshold, |
|
116 uint buffer_enqueue_threshold_percentage, |
|
117 Mutex* lock) { |
|
118 PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold, -1); |
|
119 _shared_satb_queue.set_lock(lock); |
|
120 assert(buffer_size() != 0, "buffer size not initialized"); |
|
121 // Minimum threshold of 1 ensures enqueuing of completely full buffers. |
|
122 size_t size = buffer_size(); |
|
123 size_t enqueue_qty = (size * buffer_enqueue_threshold_percentage) / 100; |
|
124 _buffer_enqueue_threshold = MAX2(size - enqueue_qty, (size_t)1); |
|
125 } |
|
126 |
|
127 #ifdef ASSERT |
|
128 void SATBMarkQueueSet::dump_active_states(bool expected_active) { |
|
129 log_error(gc, verify)("Expected SATB active state: %s", expected_active ? "ACTIVE" : "INACTIVE"); |
|
130 log_error(gc, verify)("Actual SATB active states:"); |
|
131 log_error(gc, verify)(" Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE"); |
|
132 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { |
|
133 log_error(gc, verify)(" Thread \"%s\" queue: %s", t->name(), satb_queue_for_thread(t).is_active() ? "ACTIVE" : "INACTIVE"); |
|
134 } |
|
135 log_error(gc, verify)(" Shared queue: %s", shared_satb_queue()->is_active() ? "ACTIVE" : "INACTIVE"); |
|
136 } |
|
137 |
|
138 void SATBMarkQueueSet::verify_active_states(bool expected_active) { |
|
139 // Verify queue set state |
|
140 if (is_active() != expected_active) { |
|
141 dump_active_states(expected_active); |
|
142 guarantee(false, "SATB queue set has an unexpected active state"); |
|
143 } |
|
144 |
|
145 // Verify thread queue states |
|
146 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { |
|
147 if (satb_queue_for_thread(t).is_active() != expected_active) { |
|
148 dump_active_states(expected_active); |
|
149 guarantee(false, "Thread SATB queue has an unexpected active state"); |
|
150 } |
|
151 } |
|
152 |
|
153 // Verify shared queue state |
|
154 if (shared_satb_queue()->is_active() != expected_active) { |
|
155 dump_active_states(expected_active); |
|
156 guarantee(false, "Shared SATB queue has an unexpected active state"); |
|
157 } |
|
158 } |
|
159 #endif // ASSERT |
|
160 |
|
161 void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) { |
|
162 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); |
|
163 #ifdef ASSERT |
|
164 verify_active_states(expected_active); |
|
165 #endif // ASSERT |
|
166 _all_active = active; |
|
167 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { |
|
168 satb_queue_for_thread(t).set_active(active); |
|
169 } |
|
170 shared_satb_queue()->set_active(active); |
|
171 } |
|
172 |
|
173 void SATBMarkQueueSet::filter_thread_buffers() { |
|
174 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { |
|
175 satb_queue_for_thread(t).filter(); |
|
176 } |
|
177 shared_satb_queue()->filter(); |
|
178 } |
|
179 |
|
180 bool SATBMarkQueueSet::apply_closure_to_completed_buffer(SATBBufferClosure* cl) { |
|
181 BufferNode* nd = NULL; |
|
182 { |
|
183 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); |
|
184 if (_completed_buffers_head != NULL) { |
|
185 nd = _completed_buffers_head; |
|
186 _completed_buffers_head = nd->next(); |
|
187 if (_completed_buffers_head == NULL) _completed_buffers_tail = NULL; |
|
188 _n_completed_buffers--; |
|
189 if (_n_completed_buffers == 0) _process_completed = false; |
|
190 } |
|
191 } |
|
192 if (nd != NULL) { |
|
193 void **buf = BufferNode::make_buffer_from_node(nd); |
|
194 size_t index = nd->index(); |
|
195 size_t size = buffer_size(); |
|
196 assert(index <= size, "invariant"); |
|
197 cl->do_buffer(buf + index, size - index); |
|
198 deallocate_buffer(nd); |
|
199 return true; |
|
200 } else { |
|
201 return false; |
|
202 } |
|
203 } |
|
204 |
|
205 #ifndef PRODUCT |
|
206 // Helpful for debugging |
|
207 |
|
208 #define SATB_PRINTER_BUFFER_SIZE 256 |
|
209 |
|
210 void SATBMarkQueueSet::print_all(const char* msg) { |
|
211 char buffer[SATB_PRINTER_BUFFER_SIZE]; |
|
212 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); |
|
213 |
|
214 tty->cr(); |
|
215 tty->print_cr("SATB BUFFERS [%s]", msg); |
|
216 |
|
217 BufferNode* nd = _completed_buffers_head; |
|
218 int i = 0; |
|
219 while (nd != NULL) { |
|
220 void** buf = BufferNode::make_buffer_from_node(nd); |
|
221 os::snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i); |
|
222 print_satb_buffer(buffer, buf, nd->index(), buffer_size()); |
|
223 nd = nd->next(); |
|
224 i += 1; |
|
225 } |
|
226 |
|
227 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { |
|
228 os::snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name()); |
|
229 satb_queue_for_thread(t).print(buffer); |
|
230 } |
|
231 |
|
232 shared_satb_queue()->print("Shared"); |
|
233 |
|
234 tty->cr(); |
|
235 } |
|
236 #endif // PRODUCT |
|
237 |
|
238 void SATBMarkQueueSet::abandon_partial_marking() { |
|
239 BufferNode* buffers_to_delete = NULL; |
|
240 { |
|
241 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); |
|
242 while (_completed_buffers_head != NULL) { |
|
243 BufferNode* nd = _completed_buffers_head; |
|
244 _completed_buffers_head = nd->next(); |
|
245 nd->set_next(buffers_to_delete); |
|
246 buffers_to_delete = nd; |
|
247 } |
|
248 _completed_buffers_tail = NULL; |
|
249 _n_completed_buffers = 0; |
|
250 DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked()); |
|
251 } |
|
252 while (buffers_to_delete != NULL) { |
|
253 BufferNode* nd = buffers_to_delete; |
|
254 buffers_to_delete = nd->next(); |
|
255 deallocate_buffer(nd); |
|
256 } |
|
257 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); |
|
258 // So we can safely manipulate these queues. |
|
259 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) { |
|
260 satb_queue_for_thread(t).reset(); |
|
261 } |
|
262 shared_satb_queue()->reset(); |
|
263 } |