src/share/vm/gc_implementation/g1/ptrQueue.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6198
55fb97c4c58d
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "gc_implementation/g1/ptrQueue.hpp"
aoqi@0 27 #include "memory/allocation.hpp"
aoqi@0 28 #include "memory/allocation.inline.hpp"
aoqi@0 29 #include "runtime/mutex.hpp"
aoqi@0 30 #include "runtime/mutexLocker.hpp"
aoqi@0 31 #include "runtime/thread.inline.hpp"
aoqi@0 32
aoqi@0 33 PtrQueue::PtrQueue(PtrQueueSet* qset, bool perm, bool active) :
aoqi@0 34 _qset(qset), _buf(NULL), _index(0), _active(active),
aoqi@0 35 _perm(perm), _lock(NULL)
aoqi@0 36 {}
aoqi@0 37
aoqi@0 38 void PtrQueue::flush() {
aoqi@0 39 if (!_perm && _buf != NULL) {
aoqi@0 40 if (_index == _sz) {
aoqi@0 41 // No work to do.
aoqi@0 42 qset()->deallocate_buffer(_buf);
aoqi@0 43 } else {
aoqi@0 44 // We must NULL out the unused entries, then enqueue.
aoqi@0 45 for (size_t i = 0; i < _index; i += oopSize) {
aoqi@0 46 _buf[byte_index_to_index((int)i)] = NULL;
aoqi@0 47 }
aoqi@0 48 qset()->enqueue_complete_buffer(_buf);
aoqi@0 49 }
aoqi@0 50 _buf = NULL;
aoqi@0 51 _index = 0;
aoqi@0 52 }
aoqi@0 53 }
aoqi@0 54
aoqi@0 55
aoqi@0 56 void PtrQueue::enqueue_known_active(void* ptr) {
aoqi@0 57 assert(0 <= _index && _index <= _sz, "Invariant.");
aoqi@0 58 assert(_index == 0 || _buf != NULL, "invariant");
aoqi@0 59
aoqi@0 60 while (_index == 0) {
aoqi@0 61 handle_zero_index();
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 assert(_index > 0, "postcondition");
aoqi@0 65 _index -= oopSize;
aoqi@0 66 _buf[byte_index_to_index((int)_index)] = ptr;
aoqi@0 67 assert(0 <= _index && _index <= _sz, "Invariant.");
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 void PtrQueue::locking_enqueue_completed_buffer(void** buf) {
aoqi@0 71 assert(_lock->owned_by_self(), "Required.");
aoqi@0 72
aoqi@0 73 // We have to unlock _lock (which may be Shared_DirtyCardQ_lock) before
aoqi@0 74 // we acquire DirtyCardQ_CBL_mon inside enqeue_complete_buffer as they
aoqi@0 75 // have the same rank and we may get the "possible deadlock" message
aoqi@0 76 _lock->unlock();
aoqi@0 77
aoqi@0 78 qset()->enqueue_complete_buffer(buf);
aoqi@0 79 // We must relock only because the caller will unlock, for the normal
aoqi@0 80 // case.
aoqi@0 81 _lock->lock_without_safepoint_check();
aoqi@0 82 }
aoqi@0 83
aoqi@0 84
aoqi@0 85 PtrQueueSet::PtrQueueSet(bool notify_when_complete) :
aoqi@0 86 _max_completed_queue(0),
aoqi@0 87 _cbl_mon(NULL), _fl_lock(NULL),
aoqi@0 88 _notify_when_complete(notify_when_complete),
aoqi@0 89 _sz(0),
aoqi@0 90 _completed_buffers_head(NULL),
aoqi@0 91 _completed_buffers_tail(NULL),
aoqi@0 92 _n_completed_buffers(0),
aoqi@0 93 _process_completed_threshold(0), _process_completed(false),
aoqi@0 94 _buf_free_list(NULL), _buf_free_list_sz(0)
aoqi@0 95 {
aoqi@0 96 _fl_owner = this;
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 void** PtrQueueSet::allocate_buffer() {
aoqi@0 100 assert(_sz > 0, "Didn't set a buffer size.");
aoqi@0 101 MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 102 if (_fl_owner->_buf_free_list != NULL) {
aoqi@0 103 void** res = BufferNode::make_buffer_from_node(_fl_owner->_buf_free_list);
aoqi@0 104 _fl_owner->_buf_free_list = _fl_owner->_buf_free_list->next();
aoqi@0 105 _fl_owner->_buf_free_list_sz--;
aoqi@0 106 return res;
aoqi@0 107 } else {
aoqi@0 108 // Allocate space for the BufferNode in front of the buffer.
aoqi@0 109 char *b = NEW_C_HEAP_ARRAY(char, _sz + BufferNode::aligned_size(), mtGC);
aoqi@0 110 return BufferNode::make_buffer_from_block(b);
aoqi@0 111 }
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 void PtrQueueSet::deallocate_buffer(void** buf) {
aoqi@0 115 assert(_sz > 0, "Didn't set a buffer size.");
aoqi@0 116 MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 117 BufferNode *node = BufferNode::make_node_from_buffer(buf);
aoqi@0 118 node->set_next(_fl_owner->_buf_free_list);
aoqi@0 119 _fl_owner->_buf_free_list = node;
aoqi@0 120 _fl_owner->_buf_free_list_sz++;
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 void PtrQueueSet::reduce_free_list() {
aoqi@0 124 assert(_fl_owner == this, "Free list reduction is allowed only for the owner");
aoqi@0 125 // For now we'll adopt the strategy of deleting half.
aoqi@0 126 MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 127 size_t n = _buf_free_list_sz / 2;
aoqi@0 128 while (n > 0) {
aoqi@0 129 assert(_buf_free_list != NULL, "_buf_free_list_sz must be wrong.");
aoqi@0 130 void* b = BufferNode::make_block_from_node(_buf_free_list);
aoqi@0 131 _buf_free_list = _buf_free_list->next();
aoqi@0 132 FREE_C_HEAP_ARRAY(char, b, mtGC);
aoqi@0 133 _buf_free_list_sz --;
aoqi@0 134 n--;
aoqi@0 135 }
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 void PtrQueue::handle_zero_index() {
aoqi@0 139 assert(_index == 0, "Precondition.");
aoqi@0 140
aoqi@0 141 // This thread records the full buffer and allocates a new one (while
aoqi@0 142 // holding the lock if there is one).
aoqi@0 143 if (_buf != NULL) {
aoqi@0 144 if (!should_enqueue_buffer()) {
aoqi@0 145 assert(_index > 0, "the buffer can only be re-used if it's not full");
aoqi@0 146 return;
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 if (_lock) {
aoqi@0 150 assert(_lock->owned_by_self(), "Required.");
aoqi@0 151
aoqi@0 152 // The current PtrQ may be the shared dirty card queue and
aoqi@0 153 // may be being manipulated by more than one worker thread
aoqi@0 154 // during a pause. Since the enqueuing of the completed
aoqi@0 155 // buffer unlocks the Shared_DirtyCardQ_lock more than one
aoqi@0 156 // worker thread can 'race' on reading the shared queue attributes
aoqi@0 157 // (_buf and _index) and multiple threads can call into this
aoqi@0 158 // routine for the same buffer. This will cause the completed
aoqi@0 159 // buffer to be added to the CBL multiple times.
aoqi@0 160
aoqi@0 161 // We "claim" the current buffer by caching value of _buf in
aoqi@0 162 // a local and clearing the field while holding _lock. When
aoqi@0 163 // _lock is released (while enqueueing the completed buffer)
aoqi@0 164 // the thread that acquires _lock will skip this code,
aoqi@0 165 // preventing the subsequent the multiple enqueue, and
aoqi@0 166 // install a newly allocated buffer below.
aoqi@0 167
aoqi@0 168 void** buf = _buf; // local pointer to completed buffer
aoqi@0 169 _buf = NULL; // clear shared _buf field
aoqi@0 170
aoqi@0 171 locking_enqueue_completed_buffer(buf); // enqueue completed buffer
aoqi@0 172
aoqi@0 173 // While the current thread was enqueuing the buffer another thread
aoqi@0 174 // may have a allocated a new buffer and inserted it into this pointer
aoqi@0 175 // queue. If that happens then we just return so that the current
aoqi@0 176 // thread doesn't overwrite the buffer allocated by the other thread
aoqi@0 177 // and potentially losing some dirtied cards.
aoqi@0 178
aoqi@0 179 if (_buf != NULL) return;
aoqi@0 180 } else {
aoqi@0 181 if (qset()->process_or_enqueue_complete_buffer(_buf)) {
aoqi@0 182 // Recycle the buffer. No allocation.
aoqi@0 183 _sz = qset()->buffer_size();
aoqi@0 184 _index = _sz;
aoqi@0 185 return;
aoqi@0 186 }
aoqi@0 187 }
aoqi@0 188 }
aoqi@0 189 // Reallocate the buffer
aoqi@0 190 _buf = qset()->allocate_buffer();
aoqi@0 191 _sz = qset()->buffer_size();
aoqi@0 192 _index = _sz;
aoqi@0 193 assert(0 <= _index && _index <= _sz, "Invariant.");
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 bool PtrQueueSet::process_or_enqueue_complete_buffer(void** buf) {
aoqi@0 197 if (Thread::current()->is_Java_thread()) {
aoqi@0 198 // We don't lock. It is fine to be epsilon-precise here.
aoqi@0 199 if (_max_completed_queue == 0 || _max_completed_queue > 0 &&
aoqi@0 200 _n_completed_buffers >= _max_completed_queue + _completed_queue_padding) {
aoqi@0 201 bool b = mut_process_buffer(buf);
aoqi@0 202 if (b) {
aoqi@0 203 // True here means that the buffer hasn't been deallocated and the caller may reuse it.
aoqi@0 204 return true;
aoqi@0 205 }
aoqi@0 206 }
aoqi@0 207 }
aoqi@0 208 // The buffer will be enqueued. The caller will have to get a new one.
aoqi@0 209 enqueue_complete_buffer(buf);
aoqi@0 210 return false;
aoqi@0 211 }
aoqi@0 212
aoqi@0 213 void PtrQueueSet::enqueue_complete_buffer(void** buf, size_t index) {
aoqi@0 214 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
aoqi@0 215 BufferNode* cbn = BufferNode::new_from_buffer(buf);
aoqi@0 216 cbn->set_index(index);
aoqi@0 217 if (_completed_buffers_tail == NULL) {
aoqi@0 218 assert(_completed_buffers_head == NULL, "Well-formedness");
aoqi@0 219 _completed_buffers_head = cbn;
aoqi@0 220 _completed_buffers_tail = cbn;
aoqi@0 221 } else {
aoqi@0 222 _completed_buffers_tail->set_next(cbn);
aoqi@0 223 _completed_buffers_tail = cbn;
aoqi@0 224 }
aoqi@0 225 _n_completed_buffers++;
aoqi@0 226
aoqi@0 227 if (!_process_completed && _process_completed_threshold >= 0 &&
aoqi@0 228 _n_completed_buffers >= _process_completed_threshold) {
aoqi@0 229 _process_completed = true;
aoqi@0 230 if (_notify_when_complete)
aoqi@0 231 _cbl_mon->notify();
aoqi@0 232 }
aoqi@0 233 debug_only(assert_completed_buffer_list_len_correct_locked());
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 int PtrQueueSet::completed_buffers_list_length() {
aoqi@0 237 int n = 0;
aoqi@0 238 BufferNode* cbn = _completed_buffers_head;
aoqi@0 239 while (cbn != NULL) {
aoqi@0 240 n++;
aoqi@0 241 cbn = cbn->next();
aoqi@0 242 }
aoqi@0 243 return n;
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 void PtrQueueSet::assert_completed_buffer_list_len_correct() {
aoqi@0 247 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
aoqi@0 248 assert_completed_buffer_list_len_correct_locked();
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 void PtrQueueSet::assert_completed_buffer_list_len_correct_locked() {
aoqi@0 252 guarantee(completed_buffers_list_length() == _n_completed_buffers,
aoqi@0 253 "Completed buffer length is wrong.");
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 void PtrQueueSet::set_buffer_size(size_t sz) {
aoqi@0 257 assert(_sz == 0 && sz > 0, "Should be called only once.");
aoqi@0 258 _sz = sz * oopSize;
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 // Merge lists of buffers. Notify the processing threads.
aoqi@0 262 // The source queue is emptied as a result. The queues
aoqi@0 263 // must share the monitor.
aoqi@0 264 void PtrQueueSet::merge_bufferlists(PtrQueueSet *src) {
aoqi@0 265 assert(_cbl_mon == src->_cbl_mon, "Should share the same lock");
aoqi@0 266 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
aoqi@0 267 if (_completed_buffers_tail == NULL) {
aoqi@0 268 assert(_completed_buffers_head == NULL, "Well-formedness");
aoqi@0 269 _completed_buffers_head = src->_completed_buffers_head;
aoqi@0 270 _completed_buffers_tail = src->_completed_buffers_tail;
aoqi@0 271 } else {
aoqi@0 272 assert(_completed_buffers_head != NULL, "Well formedness");
aoqi@0 273 if (src->_completed_buffers_head != NULL) {
aoqi@0 274 _completed_buffers_tail->set_next(src->_completed_buffers_head);
aoqi@0 275 _completed_buffers_tail = src->_completed_buffers_tail;
aoqi@0 276 }
aoqi@0 277 }
aoqi@0 278 _n_completed_buffers += src->_n_completed_buffers;
aoqi@0 279
aoqi@0 280 src->_n_completed_buffers = 0;
aoqi@0 281 src->_completed_buffers_head = NULL;
aoqi@0 282 src->_completed_buffers_tail = NULL;
aoqi@0 283
aoqi@0 284 assert(_completed_buffers_head == NULL && _completed_buffers_tail == NULL ||
aoqi@0 285 _completed_buffers_head != NULL && _completed_buffers_tail != NULL,
aoqi@0 286 "Sanity");
aoqi@0 287 }
aoqi@0 288
aoqi@0 289 void PtrQueueSet::notify_if_necessary() {
aoqi@0 290 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
aoqi@0 291 if (_n_completed_buffers >= _process_completed_threshold || _max_completed_queue == 0) {
aoqi@0 292 _process_completed = true;
aoqi@0 293 if (_notify_when_complete)
aoqi@0 294 _cbl_mon->notify();
aoqi@0 295 }
aoqi@0 296 }

mercurial