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 "jvm.h"
27 #include "gc/g1/g1CollectedHeap.inline.hpp"
28 #include "gc/g1/g1ThreadLocalData.hpp"
29 #include "gc/g1/satbMarkQueue.hpp"
30 #include "gc/shared/collectedHeap.hpp"
31 #include "memory/allocation.inline.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "runtime/mutexLocker.hpp"
34 #include "runtime/safepoint.hpp"
35 #include "runtime/thread.hpp"
36 #include "runtime/threadSMR.hpp"
37 #include "runtime/vmThread.hpp"
38
39 SATBMarkQueue::SATBMarkQueue(SATBMarkQueueSet* qset, bool permanent) :
40 // SATB queues are only active during marking cycles. We create
41 // them with their active field set to false. If a thread is
42 // created during a cycle and its SATB queue needs to be activated
43 // before the thread starts running, we'll need to set its active
44 // field to true. This is done in G1SBarrierSet::on_thread_attach().
45 PtrQueue(qset, permanent, false /* active */)
46 { }
47
48 void SATBMarkQueue::flush() {
100 print_satb_buffer(name, _buf, index(), capacity());
101 }
102
103 #endif // PRODUCT
104
105 SATBMarkQueueSet::SATBMarkQueueSet() :
106 PtrQueueSet(),
107 _shared_satb_queue(this, true /* permanent */),
108 _filter(NULL)
109 {}
110
111 void SATBMarkQueueSet::initialize(SATBMarkQueueFilter* filter,
112 Monitor* cbl_mon, Mutex* fl_lock,
113 int process_completed_threshold,
114 Mutex* lock) {
115 PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold, -1);
116 _shared_satb_queue.set_lock(lock);
117 _filter = filter;
118 }
119
120 void SATBMarkQueueSet::handle_zero_index_for_thread(JavaThread* t) {
121 G1ThreadLocalData::satb_mark_queue(t).handle_zero_index();
122 }
123
124 #ifdef ASSERT
125 void SATBMarkQueueSet::dump_active_states(bool expected_active) {
126 log_error(gc, verify)("Expected SATB active state: %s", expected_active ? "ACTIVE" : "INACTIVE");
127 log_error(gc, verify)("Actual SATB active states:");
128 log_error(gc, verify)(" Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE");
129 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
130 log_error(gc, verify)(" Thread \"%s\" queue: %s", t->name(), G1ThreadLocalData::satb_mark_queue(t).is_active() ? "ACTIVE" : "INACTIVE");
131 }
132 log_error(gc, verify)(" Shared queue: %s", shared_satb_queue()->is_active() ? "ACTIVE" : "INACTIVE");
133 }
134
135 void SATBMarkQueueSet::verify_active_states(bool expected_active) {
136 // Verify queue set state
137 if (is_active() != expected_active) {
138 dump_active_states(expected_active);
139 guarantee(false, "SATB queue set has an unexpected active state");
140 }
141
142 // Verify thread queue states
143 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
144 if (G1ThreadLocalData::satb_mark_queue(t).is_active() != expected_active) {
145 dump_active_states(expected_active);
146 guarantee(false, "Thread SATB queue has an unexpected active state");
147 }
148 }
149
150 // Verify shared queue state
151 if (shared_satb_queue()->is_active() != expected_active) {
152 dump_active_states(expected_active);
153 guarantee(false, "Shared SATB queue has an unexpected active state");
154 }
155 }
156 #endif // ASSERT
157
158 void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) {
159 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
160 #ifdef ASSERT
161 verify_active_states(expected_active);
162 #endif // ASSERT
163 _all_active = active;
164 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
165 G1ThreadLocalData::satb_mark_queue(t).set_active(active);
166 }
167 shared_satb_queue()->set_active(active);
168 }
169
170 void SATBMarkQueueSet::filter_thread_buffers() {
171 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
172 G1ThreadLocalData::satb_mark_queue(t).filter();
173 }
174 shared_satb_queue()->filter();
175 }
176
177 bool SATBMarkQueueSet::apply_closure_to_completed_buffer(SATBBufferClosure* cl) {
178 BufferNode* nd = NULL;
179 {
180 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
181 if (_completed_buffers_head != NULL) {
182 nd = _completed_buffers_head;
183 _completed_buffers_head = nd->next();
184 if (_completed_buffers_head == NULL) _completed_buffers_tail = NULL;
185 _n_completed_buffers--;
186 if (_n_completed_buffers == 0) _process_completed = false;
187 }
188 }
189 if (nd != NULL) {
190 void **buf = BufferNode::make_buffer_from_node(nd);
191 size_t index = nd->index();
192 size_t size = buffer_size();
206
207 void SATBMarkQueueSet::print_all(const char* msg) {
208 char buffer[SATB_PRINTER_BUFFER_SIZE];
209 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
210
211 tty->cr();
212 tty->print_cr("SATB BUFFERS [%s]", msg);
213
214 BufferNode* nd = _completed_buffers_head;
215 int i = 0;
216 while (nd != NULL) {
217 void** buf = BufferNode::make_buffer_from_node(nd);
218 jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i);
219 print_satb_buffer(buffer, buf, nd->index(), buffer_size());
220 nd = nd->next();
221 i += 1;
222 }
223
224 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
225 jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name());
226 G1ThreadLocalData::satb_mark_queue(t).print(buffer);
227 }
228
229 shared_satb_queue()->print("Shared");
230
231 tty->cr();
232 }
233 #endif // PRODUCT
234
235 void SATBMarkQueueSet::abandon_partial_marking() {
236 BufferNode* buffers_to_delete = NULL;
237 {
238 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
239 while (_completed_buffers_head != NULL) {
240 BufferNode* nd = _completed_buffers_head;
241 _completed_buffers_head = nd->next();
242 nd->set_next(buffers_to_delete);
243 buffers_to_delete = nd;
244 }
245 _completed_buffers_tail = NULL;
246 _n_completed_buffers = 0;
247 DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked());
248 }
249 while (buffers_to_delete != NULL) {
250 BufferNode* nd = buffers_to_delete;
251 buffers_to_delete = nd->next();
252 deallocate_buffer(nd);
253 }
254 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
255 // So we can safely manipulate these queues.
256 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
257 G1ThreadLocalData::satb_mark_queue(t).reset();
258 }
259 shared_satb_queue()->reset();
260 }
|
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 "jvm.h"
27 #include "gc/g1/g1CollectedHeap.inline.hpp"
28 #include "gc/g1/satbMarkQueue.hpp"
29 #include "gc/shared/collectedHeap.hpp"
30 #include "memory/allocation.inline.hpp"
31 #include "oops/oop.inline.hpp"
32 #include "runtime/mutexLocker.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 is done in G1SBarrierSet::on_thread_attach().
44 PtrQueue(qset, permanent, false /* active */)
45 { }
46
47 void SATBMarkQueue::flush() {
99 print_satb_buffer(name, _buf, index(), capacity());
100 }
101
102 #endif // PRODUCT
103
104 SATBMarkQueueSet::SATBMarkQueueSet() :
105 PtrQueueSet(),
106 _shared_satb_queue(this, true /* permanent */),
107 _filter(NULL)
108 {}
109
110 void SATBMarkQueueSet::initialize(SATBMarkQueueFilter* filter,
111 Monitor* cbl_mon, Mutex* fl_lock,
112 int process_completed_threshold,
113 Mutex* lock) {
114 PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold, -1);
115 _shared_satb_queue.set_lock(lock);
116 _filter = filter;
117 }
118
119 #ifdef ASSERT
120 void SATBMarkQueueSet::dump_active_states(bool expected_active) {
121 log_error(gc, verify)("Expected SATB active state: %s", expected_active ? "ACTIVE" : "INACTIVE");
122 log_error(gc, verify)("Actual SATB active states:");
123 log_error(gc, verify)(" Queue set: %s", is_active() ? "ACTIVE" : "INACTIVE");
124 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
125 log_error(gc, verify)(" Thread \"%s\" queue: %s", t->name(), satb_queue_for_thread(t).is_active() ? "ACTIVE" : "INACTIVE");
126 }
127 log_error(gc, verify)(" Shared queue: %s", shared_satb_queue()->is_active() ? "ACTIVE" : "INACTIVE");
128 }
129
130 void SATBMarkQueueSet::verify_active_states(bool expected_active) {
131 // Verify queue set state
132 if (is_active() != expected_active) {
133 dump_active_states(expected_active);
134 guarantee(false, "SATB queue set has an unexpected active state");
135 }
136
137 // Verify thread queue states
138 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
139 if (satb_queue_for_thread(t).is_active() != expected_active) {
140 dump_active_states(expected_active);
141 guarantee(false, "Thread SATB queue has an unexpected active state");
142 }
143 }
144
145 // Verify shared queue state
146 if (shared_satb_queue()->is_active() != expected_active) {
147 dump_active_states(expected_active);
148 guarantee(false, "Shared SATB queue has an unexpected active state");
149 }
150 }
151 #endif // ASSERT
152
153 void SATBMarkQueueSet::set_active_all_threads(bool active, bool expected_active) {
154 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
155 #ifdef ASSERT
156 verify_active_states(expected_active);
157 #endif // ASSERT
158 _all_active = active;
159 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
160 satb_queue_for_thread(t).set_active(active);
161 }
162 shared_satb_queue()->set_active(active);
163 }
164
165 void SATBMarkQueueSet::filter_thread_buffers() {
166 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
167 satb_queue_for_thread(t).filter();
168 }
169 shared_satb_queue()->filter();
170 }
171
172 bool SATBMarkQueueSet::apply_closure_to_completed_buffer(SATBBufferClosure* cl) {
173 BufferNode* nd = NULL;
174 {
175 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
176 if (_completed_buffers_head != NULL) {
177 nd = _completed_buffers_head;
178 _completed_buffers_head = nd->next();
179 if (_completed_buffers_head == NULL) _completed_buffers_tail = NULL;
180 _n_completed_buffers--;
181 if (_n_completed_buffers == 0) _process_completed = false;
182 }
183 }
184 if (nd != NULL) {
185 void **buf = BufferNode::make_buffer_from_node(nd);
186 size_t index = nd->index();
187 size_t size = buffer_size();
201
202 void SATBMarkQueueSet::print_all(const char* msg) {
203 char buffer[SATB_PRINTER_BUFFER_SIZE];
204 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
205
206 tty->cr();
207 tty->print_cr("SATB BUFFERS [%s]", msg);
208
209 BufferNode* nd = _completed_buffers_head;
210 int i = 0;
211 while (nd != NULL) {
212 void** buf = BufferNode::make_buffer_from_node(nd);
213 jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i);
214 print_satb_buffer(buffer, buf, nd->index(), buffer_size());
215 nd = nd->next();
216 i += 1;
217 }
218
219 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
220 jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name());
221 satb_queue_for_thread(t).print(buffer);
222 }
223
224 shared_satb_queue()->print("Shared");
225
226 tty->cr();
227 }
228 #endif // PRODUCT
229
230 void SATBMarkQueueSet::abandon_partial_marking() {
231 BufferNode* buffers_to_delete = NULL;
232 {
233 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
234 while (_completed_buffers_head != NULL) {
235 BufferNode* nd = _completed_buffers_head;
236 _completed_buffers_head = nd->next();
237 nd->set_next(buffers_to_delete);
238 buffers_to_delete = nd;
239 }
240 _completed_buffers_tail = NULL;
241 _n_completed_buffers = 0;
242 DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked());
243 }
244 while (buffers_to_delete != NULL) {
245 BufferNode* nd = buffers_to_delete;
246 buffers_to_delete = nd->next();
247 deallocate_buffer(nd);
248 }
249 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
250 // So we can safely manipulate these queues.
251 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
252 satb_queue_for_thread(t).reset();
253 }
254 shared_satb_queue()->reset();
255 }
|