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

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 10026
8c95980d0b66
parent 7535
7ae4e26cb1e0
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f

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

mercurial