Mon, 12 Mar 2012 14:59:00 -0700
7147724: G1: hang in SurrogateLockerThread::manipulatePLL
Summary: Attempting to initiate a marking cycle when allocating a humongous object can, if a marking cycle is successfully initiated by another thread, result in the allocating thread spinning until the marking cycle is complete. Eliminate a deadlock between the main ConcurrentMarkThread, the SurrogateLocker thread, the VM thread, and a mutator thread waiting on the SecondaryFreeList_lock (while free regions are going to become available) by not manipulating the pending list lock during the prologue and epilogue of the cleanup pause.
Reviewed-by: brutisso, jcoomes, tonyp
1 /*
2 * Copyright (c) 2001, 2012, 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 */
25 #include "precompiled.hpp"
26 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
27 #include "gc_implementation/g1/satbQueue.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "memory/sharedHeap.hpp"
30 #include "runtime/mutexLocker.hpp"
31 #include "runtime/thread.hpp"
32 #include "runtime/vmThread.hpp"
34 void ObjPtrQueue::flush() {
35 // The buffer might contain refs into the CSet. We have to filter it
36 // first before we flush it, otherwise we might end up with an
37 // enqueued buffer with refs into the CSet which breaks our invariants.
38 filter();
39 PtrQueue::flush();
40 }
42 // This method removes entries from an SATB buffer that will not be
43 // useful to the concurrent marking threads. An entry is removed if it
44 // satisfies one of the following conditions:
45 //
46 // * it points to an object outside the G1 heap (G1's concurrent
47 // marking only visits objects inside the G1 heap),
48 // * it points to an object that has been allocated since marking
49 // started (according to SATB those objects do not need to be
50 // visited during marking), or
51 // * it points to an object that has already been marked (no need to
52 // process it again).
53 //
54 // The rest of the entries will be retained and are compacted towards
55 // the top of the buffer. Note that, because we do not allow old
56 // regions in the CSet during marking, all objects on the CSet regions
57 // are young (eden or survivors) and therefore implicitly live. So any
58 // references into the CSet will be removed during filtering.
60 void ObjPtrQueue::filter() {
61 G1CollectedHeap* g1h = G1CollectedHeap::heap();
62 void** buf = _buf;
63 size_t sz = _sz;
65 if (buf == NULL) {
66 // nothing to do
67 return;
68 }
70 // Used for sanity checking at the end of the loop.
71 debug_only(size_t entries = 0; size_t retained = 0;)
73 size_t i = sz;
74 size_t new_index = sz;
76 while (i > _index) {
77 assert(i > 0, "we should have at least one more entry to process");
78 i -= oopSize;
79 debug_only(entries += 1;)
80 oop* p = (oop*) &buf[byte_index_to_index((int) i)];
81 oop obj = *p;
82 // NULL the entry so that unused parts of the buffer contain NULLs
83 // at the end. If we are going to retain it we will copy it to its
84 // final place. If we have retained all entries we have visited so
85 // far, we'll just end up copying it to the same place.
86 *p = NULL;
88 bool retain = g1h->is_obj_ill(obj);
89 if (retain) {
90 assert(new_index > 0, "we should not have already filled up the buffer");
91 new_index -= oopSize;
92 assert(new_index >= i,
93 "new_index should never be below i, as we alwaysr compact 'up'");
94 oop* new_p = (oop*) &buf[byte_index_to_index((int) new_index)];
95 assert(new_p >= p, "the destination location should never be below "
96 "the source as we always compact 'up'");
97 assert(*new_p == NULL,
98 "we should have already cleared the destination location");
99 *new_p = obj;
100 debug_only(retained += 1;)
101 }
102 }
104 #ifdef ASSERT
105 size_t entries_calc = (sz - _index) / oopSize;
106 assert(entries == entries_calc, "the number of entries we counted "
107 "should match the number of entries we calculated");
108 size_t retained_calc = (sz - new_index) / oopSize;
109 assert(retained == retained_calc, "the number of retained entries we counted "
110 "should match the number of retained entries we calculated");
111 #endif // ASSERT
113 _index = new_index;
114 }
116 // This method will first apply the above filtering to the buffer. If
117 // post-filtering a large enough chunk of the buffer has been cleared
118 // we can re-use the buffer (instead of enqueueing it) and we can just
119 // allow the mutator to carry on executing using the same buffer
120 // instead of replacing it.
122 bool ObjPtrQueue::should_enqueue_buffer() {
123 assert(_lock == NULL || _lock->owned_by_self(),
124 "we should have taken the lock before calling this");
126 // Even if G1SATBBufferEnqueueingThresholdPercent == 0 we have to
127 // filter the buffer given that this will remove any references into
128 // the CSet as we currently assume that no such refs will appear in
129 // enqueued buffers.
131 // This method should only be called if there is a non-NULL buffer
132 // that is full.
133 assert(_index == 0, "pre-condition");
134 assert(_buf != NULL, "pre-condition");
136 filter();
138 size_t sz = _sz;
139 size_t all_entries = sz / oopSize;
140 size_t retained_entries = (sz - _index) / oopSize;
141 size_t perc = retained_entries * 100 / all_entries;
142 bool should_enqueue = perc > (size_t) G1SATBBufferEnqueueingThresholdPercent;
143 return should_enqueue;
144 }
146 void ObjPtrQueue::apply_closure(ObjectClosure* cl) {
147 if (_buf != NULL) {
148 apply_closure_to_buffer(cl, _buf, _index, _sz);
149 }
150 }
152 void ObjPtrQueue::apply_closure_and_empty(ObjectClosure* cl) {
153 if (_buf != NULL) {
154 apply_closure_to_buffer(cl, _buf, _index, _sz);
155 _index = _sz;
156 }
157 }
159 void ObjPtrQueue::apply_closure_to_buffer(ObjectClosure* cl,
160 void** buf, size_t index, size_t sz) {
161 if (cl == NULL) return;
162 for (size_t i = index; i < sz; i += oopSize) {
163 oop obj = (oop)buf[byte_index_to_index((int)i)];
164 // There can be NULL entries because of destructors.
165 if (obj != NULL) {
166 cl->do_object(obj);
167 }
168 }
169 }
171 #ifndef PRODUCT
172 // Helpful for debugging
174 void ObjPtrQueue::print(const char* name) {
175 print(name, _buf, _index, _sz);
176 }
178 void ObjPtrQueue::print(const char* name,
179 void** buf, size_t index, size_t sz) {
180 gclog_or_tty->print_cr(" SATB BUFFER [%s] buf: "PTR_FORMAT" "
181 "index: "SIZE_FORMAT" sz: "SIZE_FORMAT,
182 name, buf, index, sz);
183 }
184 #endif // PRODUCT
186 #ifdef ASSERT
187 void ObjPtrQueue::verify_oops_in_buffer() {
188 if (_buf == NULL) return;
189 for (size_t i = _index; i < _sz; i += oopSize) {
190 oop obj = (oop)_buf[byte_index_to_index((int)i)];
191 assert(obj != NULL && obj->is_oop(true /* ignore mark word */),
192 "Not an oop");
193 }
194 }
195 #endif
197 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
198 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
199 #endif // _MSC_VER
201 SATBMarkQueueSet::SATBMarkQueueSet() :
202 PtrQueueSet(), _closure(NULL), _par_closures(NULL),
203 _shared_satb_queue(this, true /*perm*/) { }
205 void SATBMarkQueueSet::initialize(Monitor* cbl_mon, Mutex* fl_lock,
206 int process_completed_threshold,
207 Mutex* lock) {
208 PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold, -1);
209 _shared_satb_queue.set_lock(lock);
210 if (ParallelGCThreads > 0) {
211 _par_closures = NEW_C_HEAP_ARRAY(ObjectClosure*, ParallelGCThreads);
212 }
213 }
215 void SATBMarkQueueSet::handle_zero_index_for_thread(JavaThread* t) {
216 DEBUG_ONLY(t->satb_mark_queue().verify_oops_in_buffer();)
217 t->satb_mark_queue().handle_zero_index();
218 }
220 #ifdef ASSERT
221 void SATBMarkQueueSet::dump_active_values(JavaThread* first,
222 bool expected_active) {
223 gclog_or_tty->print_cr("SATB queue active values for Java Threads");
224 gclog_or_tty->print_cr(" SATB queue set: active is %s",
225 (is_active()) ? "TRUE" : "FALSE");
226 gclog_or_tty->print_cr(" expected_active is %s",
227 (expected_active) ? "TRUE" : "FALSE");
228 for (JavaThread* t = first; t; t = t->next()) {
229 bool active = t->satb_mark_queue().is_active();
230 gclog_or_tty->print_cr(" thread %s, active is %s",
231 t->name(), (active) ? "TRUE" : "FALSE");
232 }
233 }
234 #endif // ASSERT
236 void SATBMarkQueueSet::set_active_all_threads(bool b,
237 bool expected_active) {
238 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
239 JavaThread* first = Threads::first();
241 #ifdef ASSERT
242 if (_all_active != expected_active) {
243 dump_active_values(first, expected_active);
245 // I leave this here as a guarantee, instead of an assert, so
246 // that it will still be compiled in if we choose to uncomment
247 // the #ifdef ASSERT in a product build. The whole block is
248 // within an #ifdef ASSERT so the guarantee will not be compiled
249 // in a product build anyway.
250 guarantee(false,
251 "SATB queue set has an unexpected active value");
252 }
253 #endif // ASSERT
254 _all_active = b;
256 for (JavaThread* t = first; t; t = t->next()) {
257 #ifdef ASSERT
258 bool active = t->satb_mark_queue().is_active();
259 if (active != expected_active) {
260 dump_active_values(first, expected_active);
262 // I leave this here as a guarantee, instead of an assert, so
263 // that it will still be compiled in if we choose to uncomment
264 // the #ifdef ASSERT in a product build. The whole block is
265 // within an #ifdef ASSERT so the guarantee will not be compiled
266 // in a product build anyway.
267 guarantee(false,
268 "thread has an unexpected active value in its SATB queue");
269 }
270 #endif // ASSERT
271 t->satb_mark_queue().set_active(b);
272 }
273 }
275 void SATBMarkQueueSet::filter_thread_buffers() {
276 for(JavaThread* t = Threads::first(); t; t = t->next()) {
277 t->satb_mark_queue().filter();
278 }
279 shared_satb_queue()->filter();
280 }
282 void SATBMarkQueueSet::set_closure(ObjectClosure* closure) {
283 _closure = closure;
284 }
286 void SATBMarkQueueSet::set_par_closure(int i, ObjectClosure* par_closure) {
287 assert(ParallelGCThreads > 0 && _par_closures != NULL, "Precondition");
288 _par_closures[i] = par_closure;
289 }
291 void SATBMarkQueueSet::iterate_closure_all_threads() {
292 for(JavaThread* t = Threads::first(); t; t = t->next()) {
293 t->satb_mark_queue().apply_closure_and_empty(_closure);
294 }
295 shared_satb_queue()->apply_closure_and_empty(_closure);
296 }
298 void SATBMarkQueueSet::par_iterate_closure_all_threads(int worker) {
299 SharedHeap* sh = SharedHeap::heap();
300 int parity = sh->strong_roots_parity();
302 for(JavaThread* t = Threads::first(); t; t = t->next()) {
303 if (t->claim_oops_do(true, parity)) {
304 t->satb_mark_queue().apply_closure_and_empty(_par_closures[worker]);
305 }
306 }
308 // We also need to claim the VMThread so that its parity is updated
309 // otherwise the next call to Thread::possibly_parallel_oops_do inside
310 // a StrongRootsScope might skip the VMThread because it has a stale
311 // parity that matches the parity set by the StrongRootsScope
312 //
313 // Whichever worker succeeds in claiming the VMThread gets to do
314 // the shared queue.
316 VMThread* vmt = VMThread::vm_thread();
317 if (vmt->claim_oops_do(true, parity)) {
318 shared_satb_queue()->apply_closure_and_empty(_par_closures[worker]);
319 }
320 }
322 bool SATBMarkQueueSet::apply_closure_to_completed_buffer_work(bool par,
323 int worker) {
324 BufferNode* nd = NULL;
325 {
326 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
327 if (_completed_buffers_head != NULL) {
328 nd = _completed_buffers_head;
329 _completed_buffers_head = nd->next();
330 if (_completed_buffers_head == NULL) _completed_buffers_tail = NULL;
331 _n_completed_buffers--;
332 if (_n_completed_buffers == 0) _process_completed = false;
333 }
334 }
335 ObjectClosure* cl = (par ? _par_closures[worker] : _closure);
336 if (nd != NULL) {
337 void **buf = BufferNode::make_buffer_from_node(nd);
338 ObjPtrQueue::apply_closure_to_buffer(cl, buf, 0, _sz);
339 deallocate_buffer(buf);
340 return true;
341 } else {
342 return false;
343 }
344 }
346 void SATBMarkQueueSet::iterate_completed_buffers_read_only(ObjectClosure* cl) {
347 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
348 assert(cl != NULL, "pre-condition");
350 BufferNode* nd = _completed_buffers_head;
351 while (nd != NULL) {
352 void** buf = BufferNode::make_buffer_from_node(nd);
353 ObjPtrQueue::apply_closure_to_buffer(cl, buf, 0, _sz);
354 nd = nd->next();
355 }
356 }
358 void SATBMarkQueueSet::iterate_thread_buffers_read_only(ObjectClosure* cl) {
359 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
360 assert(cl != NULL, "pre-condition");
362 for (JavaThread* t = Threads::first(); t; t = t->next()) {
363 t->satb_mark_queue().apply_closure(cl);
364 }
365 shared_satb_queue()->apply_closure(cl);
366 }
368 #ifndef PRODUCT
369 // Helpful for debugging
371 #define SATB_PRINTER_BUFFER_SIZE 256
373 void SATBMarkQueueSet::print_all(const char* msg) {
374 char buffer[SATB_PRINTER_BUFFER_SIZE];
375 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
377 gclog_or_tty->cr();
378 gclog_or_tty->print_cr("SATB BUFFERS [%s]", msg);
380 BufferNode* nd = _completed_buffers_head;
381 int i = 0;
382 while (nd != NULL) {
383 void** buf = BufferNode::make_buffer_from_node(nd);
384 jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i);
385 ObjPtrQueue::print(buffer, buf, 0, _sz);
386 nd = nd->next();
387 i += 1;
388 }
390 for (JavaThread* t = Threads::first(); t; t = t->next()) {
391 jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name());
392 t->satb_mark_queue().print(buffer);
393 }
395 shared_satb_queue()->print("Shared");
397 gclog_or_tty->cr();
398 }
399 #endif // PRODUCT
401 void SATBMarkQueueSet::abandon_partial_marking() {
402 BufferNode* buffers_to_delete = NULL;
403 {
404 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
405 while (_completed_buffers_head != NULL) {
406 BufferNode* nd = _completed_buffers_head;
407 _completed_buffers_head = nd->next();
408 nd->set_next(buffers_to_delete);
409 buffers_to_delete = nd;
410 }
411 _completed_buffers_tail = NULL;
412 _n_completed_buffers = 0;
413 DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked());
414 }
415 while (buffers_to_delete != NULL) {
416 BufferNode* nd = buffers_to_delete;
417 buffers_to_delete = nd->next();
418 deallocate_buffer(BufferNode::make_buffer_from_node(nd));
419 }
420 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
421 // So we can safely manipulate these queues.
422 for (JavaThread* t = Threads::first(); t; t = t->next()) {
423 t->satb_mark_queue().reset();
424 }
425 shared_satb_queue()->reset();
426 }