Tue, 19 Mar 2013 00:57:39 -0700
8009940: G1: assert(_finger == _heap_end) failed, concurrentMark.cpp:809
Summary: Skip reference processing if the global marking stack overflows during remark. Refactor and rename set_phase(); move code that sets the concurrency level into its own routine. Do not call set_phase() from within parallel reference processing; use the concurrency level routine instead. The marking state should only set reset by CMTask[0] during the concurrent phase of the marking cycle; if an overflow occurs at any stage during the remark, the marking state will be reset after reference processing.
Reviewed-by: brutisso, jmasa
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/ptrQueue.hpp"
27 #include "memory/allocation.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "runtime/mutex.hpp"
30 #include "runtime/mutexLocker.hpp"
31 #include "runtime/thread.inline.hpp"
33 PtrQueue::PtrQueue(PtrQueueSet* qset, bool perm, bool active) :
34 _qset(qset), _buf(NULL), _index(0), _active(active),
35 _perm(perm), _lock(NULL)
36 {}
38 void PtrQueue::flush() {
39 if (!_perm && _buf != NULL) {
40 if (_index == _sz) {
41 // No work to do.
42 qset()->deallocate_buffer(_buf);
43 } else {
44 // We must NULL out the unused entries, then enqueue.
45 for (size_t i = 0; i < _index; i += oopSize) {
46 _buf[byte_index_to_index((int)i)] = NULL;
47 }
48 qset()->enqueue_complete_buffer(_buf);
49 }
50 _buf = NULL;
51 _index = 0;
52 }
53 }
56 static int byte_index_to_index(int ind) {
57 assert((ind % oopSize) == 0, "Invariant.");
58 return ind / oopSize;
59 }
61 static int index_to_byte_index(int byte_ind) {
62 return byte_ind * oopSize;
63 }
65 void PtrQueue::enqueue_known_active(void* ptr) {
66 assert(0 <= _index && _index <= _sz, "Invariant.");
67 assert(_index == 0 || _buf != NULL, "invariant");
69 while (_index == 0) {
70 handle_zero_index();
71 }
73 assert(_index > 0, "postcondition");
74 _index -= oopSize;
75 _buf[byte_index_to_index((int)_index)] = ptr;
76 assert(0 <= _index && _index <= _sz, "Invariant.");
77 }
79 void PtrQueue::locking_enqueue_completed_buffer(void** buf) {
80 assert(_lock->owned_by_self(), "Required.");
82 // We have to unlock _lock (which may be Shared_DirtyCardQ_lock) before
83 // we acquire DirtyCardQ_CBL_mon inside enqeue_complete_buffer as they
84 // have the same rank and we may get the "possible deadlock" message
85 _lock->unlock();
87 qset()->enqueue_complete_buffer(buf);
88 // We must relock only because the caller will unlock, for the normal
89 // case.
90 _lock->lock_without_safepoint_check();
91 }
94 PtrQueueSet::PtrQueueSet(bool notify_when_complete) :
95 _max_completed_queue(0),
96 _cbl_mon(NULL), _fl_lock(NULL),
97 _notify_when_complete(notify_when_complete),
98 _sz(0),
99 _completed_buffers_head(NULL),
100 _completed_buffers_tail(NULL),
101 _n_completed_buffers(0),
102 _process_completed_threshold(0), _process_completed(false),
103 _buf_free_list(NULL), _buf_free_list_sz(0)
104 {
105 _fl_owner = this;
106 }
108 void** PtrQueueSet::allocate_buffer() {
109 assert(_sz > 0, "Didn't set a buffer size.");
110 MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
111 if (_fl_owner->_buf_free_list != NULL) {
112 void** res = BufferNode::make_buffer_from_node(_fl_owner->_buf_free_list);
113 _fl_owner->_buf_free_list = _fl_owner->_buf_free_list->next();
114 _fl_owner->_buf_free_list_sz--;
115 return res;
116 } else {
117 // Allocate space for the BufferNode in front of the buffer.
118 char *b = NEW_C_HEAP_ARRAY(char, _sz + BufferNode::aligned_size(), mtGC);
119 return BufferNode::make_buffer_from_block(b);
120 }
121 }
123 void PtrQueueSet::deallocate_buffer(void** buf) {
124 assert(_sz > 0, "Didn't set a buffer size.");
125 MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
126 BufferNode *node = BufferNode::make_node_from_buffer(buf);
127 node->set_next(_fl_owner->_buf_free_list);
128 _fl_owner->_buf_free_list = node;
129 _fl_owner->_buf_free_list_sz++;
130 }
132 void PtrQueueSet::reduce_free_list() {
133 assert(_fl_owner == this, "Free list reduction is allowed only for the owner");
134 // For now we'll adopt the strategy of deleting half.
135 MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag);
136 size_t n = _buf_free_list_sz / 2;
137 while (n > 0) {
138 assert(_buf_free_list != NULL, "_buf_free_list_sz must be wrong.");
139 void* b = BufferNode::make_block_from_node(_buf_free_list);
140 _buf_free_list = _buf_free_list->next();
141 FREE_C_HEAP_ARRAY(char, b, mtGC);
142 _buf_free_list_sz --;
143 n--;
144 }
145 }
147 void PtrQueue::handle_zero_index() {
148 assert(_index == 0, "Precondition.");
150 // This thread records the full buffer and allocates a new one (while
151 // holding the lock if there is one).
152 if (_buf != NULL) {
153 if (!should_enqueue_buffer()) {
154 assert(_index > 0, "the buffer can only be re-used if it's not full");
155 return;
156 }
158 if (_lock) {
159 assert(_lock->owned_by_self(), "Required.");
161 // The current PtrQ may be the shared dirty card queue and
162 // may be being manipulated by more than one worker thread
163 // during a pause. Since the enqueuing of the completed
164 // buffer unlocks the Shared_DirtyCardQ_lock more than one
165 // worker thread can 'race' on reading the shared queue attributes
166 // (_buf and _index) and multiple threads can call into this
167 // routine for the same buffer. This will cause the completed
168 // buffer to be added to the CBL multiple times.
170 // We "claim" the current buffer by caching value of _buf in
171 // a local and clearing the field while holding _lock. When
172 // _lock is released (while enqueueing the completed buffer)
173 // the thread that acquires _lock will skip this code,
174 // preventing the subsequent the multiple enqueue, and
175 // install a newly allocated buffer below.
177 void** buf = _buf; // local pointer to completed buffer
178 _buf = NULL; // clear shared _buf field
180 locking_enqueue_completed_buffer(buf); // enqueue completed buffer
182 // While the current thread was enqueuing the buffer another thread
183 // may have a allocated a new buffer and inserted it into this pointer
184 // queue. If that happens then we just return so that the current
185 // thread doesn't overwrite the buffer allocated by the other thread
186 // and potentially losing some dirtied cards.
188 if (_buf != NULL) return;
189 } else {
190 if (qset()->process_or_enqueue_complete_buffer(_buf)) {
191 // Recycle the buffer. No allocation.
192 _sz = qset()->buffer_size();
193 _index = _sz;
194 return;
195 }
196 }
197 }
198 // Reallocate the buffer
199 _buf = qset()->allocate_buffer();
200 _sz = qset()->buffer_size();
201 _index = _sz;
202 assert(0 <= _index && _index <= _sz, "Invariant.");
203 }
205 bool PtrQueueSet::process_or_enqueue_complete_buffer(void** buf) {
206 if (Thread::current()->is_Java_thread()) {
207 // We don't lock. It is fine to be epsilon-precise here.
208 if (_max_completed_queue == 0 || _max_completed_queue > 0 &&
209 _n_completed_buffers >= _max_completed_queue + _completed_queue_padding) {
210 bool b = mut_process_buffer(buf);
211 if (b) {
212 // True here means that the buffer hasn't been deallocated and the caller may reuse it.
213 return true;
214 }
215 }
216 }
217 // The buffer will be enqueued. The caller will have to get a new one.
218 enqueue_complete_buffer(buf);
219 return false;
220 }
222 void PtrQueueSet::enqueue_complete_buffer(void** buf, size_t index) {
223 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
224 BufferNode* cbn = BufferNode::new_from_buffer(buf);
225 cbn->set_index(index);
226 if (_completed_buffers_tail == NULL) {
227 assert(_completed_buffers_head == NULL, "Well-formedness");
228 _completed_buffers_head = cbn;
229 _completed_buffers_tail = cbn;
230 } else {
231 _completed_buffers_tail->set_next(cbn);
232 _completed_buffers_tail = cbn;
233 }
234 _n_completed_buffers++;
236 if (!_process_completed && _process_completed_threshold >= 0 &&
237 _n_completed_buffers >= _process_completed_threshold) {
238 _process_completed = true;
239 if (_notify_when_complete)
240 _cbl_mon->notify();
241 }
242 debug_only(assert_completed_buffer_list_len_correct_locked());
243 }
245 int PtrQueueSet::completed_buffers_list_length() {
246 int n = 0;
247 BufferNode* cbn = _completed_buffers_head;
248 while (cbn != NULL) {
249 n++;
250 cbn = cbn->next();
251 }
252 return n;
253 }
255 void PtrQueueSet::assert_completed_buffer_list_len_correct() {
256 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
257 assert_completed_buffer_list_len_correct_locked();
258 }
260 void PtrQueueSet::assert_completed_buffer_list_len_correct_locked() {
261 guarantee(completed_buffers_list_length() == _n_completed_buffers,
262 "Completed buffer length is wrong.");
263 }
265 void PtrQueueSet::set_buffer_size(size_t sz) {
266 assert(_sz == 0 && sz > 0, "Should be called only once.");
267 _sz = sz * oopSize;
268 }
270 // Merge lists of buffers. Notify the processing threads.
271 // The source queue is emptied as a result. The queues
272 // must share the monitor.
273 void PtrQueueSet::merge_bufferlists(PtrQueueSet *src) {
274 assert(_cbl_mon == src->_cbl_mon, "Should share the same lock");
275 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
276 if (_completed_buffers_tail == NULL) {
277 assert(_completed_buffers_head == NULL, "Well-formedness");
278 _completed_buffers_head = src->_completed_buffers_head;
279 _completed_buffers_tail = src->_completed_buffers_tail;
280 } else {
281 assert(_completed_buffers_head != NULL, "Well formedness");
282 if (src->_completed_buffers_head != NULL) {
283 _completed_buffers_tail->set_next(src->_completed_buffers_head);
284 _completed_buffers_tail = src->_completed_buffers_tail;
285 }
286 }
287 _n_completed_buffers += src->_n_completed_buffers;
289 src->_n_completed_buffers = 0;
290 src->_completed_buffers_head = NULL;
291 src->_completed_buffers_tail = NULL;
293 assert(_completed_buffers_head == NULL && _completed_buffers_tail == NULL ||
294 _completed_buffers_head != NULL && _completed_buffers_tail != NULL,
295 "Sanity");
296 }
298 void PtrQueueSet::notify_if_necessary() {
299 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
300 if (_n_completed_buffers >= _process_completed_threshold || _max_completed_queue == 0) {
301 _process_completed = true;
302 if (_notify_when_complete)
303 _cbl_mon->notify();
304 }
305 }