aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "gc_implementation/g1/ptrQueue.hpp" aoqi@0: #include "memory/allocation.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "runtime/mutex.hpp" aoqi@0: #include "runtime/mutexLocker.hpp" aoqi@0: #include "runtime/thread.inline.hpp" aoqi@0: aoqi@0: PtrQueue::PtrQueue(PtrQueueSet* qset, bool perm, bool active) : aoqi@0: _qset(qset), _buf(NULL), _index(0), _active(active), aoqi@0: _perm(perm), _lock(NULL) aoqi@0: {} aoqi@0: aoqi@0: void PtrQueue::flush() { aoqi@0: if (!_perm && _buf != NULL) { aoqi@0: if (_index == _sz) { aoqi@0: // No work to do. aoqi@0: qset()->deallocate_buffer(_buf); aoqi@0: } else { aoqi@0: // We must NULL out the unused entries, then enqueue. aoqi@0: for (size_t i = 0; i < _index; i += oopSize) { aoqi@0: _buf[byte_index_to_index((int)i)] = NULL; aoqi@0: } aoqi@0: qset()->enqueue_complete_buffer(_buf); aoqi@0: } aoqi@0: _buf = NULL; aoqi@0: _index = 0; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void PtrQueue::enqueue_known_active(void* ptr) { aoqi@0: assert(0 <= _index && _index <= _sz, "Invariant."); aoqi@0: assert(_index == 0 || _buf != NULL, "invariant"); aoqi@0: aoqi@0: while (_index == 0) { aoqi@0: handle_zero_index(); aoqi@0: } aoqi@0: aoqi@0: assert(_index > 0, "postcondition"); aoqi@0: _index -= oopSize; aoqi@0: _buf[byte_index_to_index((int)_index)] = ptr; aoqi@0: assert(0 <= _index && _index <= _sz, "Invariant."); aoqi@0: } aoqi@0: aoqi@0: void PtrQueue::locking_enqueue_completed_buffer(void** buf) { aoqi@0: assert(_lock->owned_by_self(), "Required."); aoqi@0: aoqi@0: // We have to unlock _lock (which may be Shared_DirtyCardQ_lock) before aoqi@0: // we acquire DirtyCardQ_CBL_mon inside enqeue_complete_buffer as they aoqi@0: // have the same rank and we may get the "possible deadlock" message aoqi@0: _lock->unlock(); aoqi@0: aoqi@0: qset()->enqueue_complete_buffer(buf); aoqi@0: // We must relock only because the caller will unlock, for the normal aoqi@0: // case. aoqi@0: _lock->lock_without_safepoint_check(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: PtrQueueSet::PtrQueueSet(bool notify_when_complete) : aoqi@0: _max_completed_queue(0), aoqi@0: _cbl_mon(NULL), _fl_lock(NULL), aoqi@0: _notify_when_complete(notify_when_complete), aoqi@0: _sz(0), aoqi@0: _completed_buffers_head(NULL), aoqi@0: _completed_buffers_tail(NULL), aoqi@0: _n_completed_buffers(0), aoqi@0: _process_completed_threshold(0), _process_completed(false), aoqi@0: _buf_free_list(NULL), _buf_free_list_sz(0) aoqi@0: { aoqi@0: _fl_owner = this; aoqi@0: } aoqi@0: aoqi@0: void** PtrQueueSet::allocate_buffer() { aoqi@0: assert(_sz > 0, "Didn't set a buffer size."); aoqi@0: MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag); aoqi@0: if (_fl_owner->_buf_free_list != NULL) { aoqi@0: void** res = BufferNode::make_buffer_from_node(_fl_owner->_buf_free_list); aoqi@0: _fl_owner->_buf_free_list = _fl_owner->_buf_free_list->next(); aoqi@0: _fl_owner->_buf_free_list_sz--; aoqi@0: return res; aoqi@0: } else { aoqi@0: // Allocate space for the BufferNode in front of the buffer. aoqi@0: char *b = NEW_C_HEAP_ARRAY(char, _sz + BufferNode::aligned_size(), mtGC); aoqi@0: return BufferNode::make_buffer_from_block(b); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void PtrQueueSet::deallocate_buffer(void** buf) { aoqi@0: assert(_sz > 0, "Didn't set a buffer size."); aoqi@0: MutexLockerEx x(_fl_owner->_fl_lock, Mutex::_no_safepoint_check_flag); aoqi@0: BufferNode *node = BufferNode::make_node_from_buffer(buf); aoqi@0: node->set_next(_fl_owner->_buf_free_list); aoqi@0: _fl_owner->_buf_free_list = node; aoqi@0: _fl_owner->_buf_free_list_sz++; aoqi@0: } aoqi@0: aoqi@0: void PtrQueueSet::reduce_free_list() { aoqi@0: assert(_fl_owner == this, "Free list reduction is allowed only for the owner"); aoqi@0: // For now we'll adopt the strategy of deleting half. aoqi@0: MutexLockerEx x(_fl_lock, Mutex::_no_safepoint_check_flag); aoqi@0: size_t n = _buf_free_list_sz / 2; aoqi@0: while (n > 0) { aoqi@0: assert(_buf_free_list != NULL, "_buf_free_list_sz must be wrong."); aoqi@0: void* b = BufferNode::make_block_from_node(_buf_free_list); aoqi@0: _buf_free_list = _buf_free_list->next(); aoqi@0: FREE_C_HEAP_ARRAY(char, b, mtGC); aoqi@0: _buf_free_list_sz --; aoqi@0: n--; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void PtrQueue::handle_zero_index() { aoqi@0: assert(_index == 0, "Precondition."); aoqi@0: aoqi@0: // This thread records the full buffer and allocates a new one (while aoqi@0: // holding the lock if there is one). aoqi@0: if (_buf != NULL) { aoqi@0: if (!should_enqueue_buffer()) { aoqi@0: assert(_index > 0, "the buffer can only be re-used if it's not full"); aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: if (_lock) { aoqi@0: assert(_lock->owned_by_self(), "Required."); aoqi@0: aoqi@0: // The current PtrQ may be the shared dirty card queue and aoqi@0: // may be being manipulated by more than one worker thread aoqi@0: // during a pause. Since the enqueuing of the completed aoqi@0: // buffer unlocks the Shared_DirtyCardQ_lock more than one aoqi@0: // worker thread can 'race' on reading the shared queue attributes aoqi@0: // (_buf and _index) and multiple threads can call into this aoqi@0: // routine for the same buffer. This will cause the completed aoqi@0: // buffer to be added to the CBL multiple times. aoqi@0: aoqi@0: // We "claim" the current buffer by caching value of _buf in aoqi@0: // a local and clearing the field while holding _lock. When aoqi@0: // _lock is released (while enqueueing the completed buffer) aoqi@0: // the thread that acquires _lock will skip this code, aoqi@0: // preventing the subsequent the multiple enqueue, and aoqi@0: // install a newly allocated buffer below. aoqi@0: aoqi@0: void** buf = _buf; // local pointer to completed buffer aoqi@0: _buf = NULL; // clear shared _buf field aoqi@0: aoqi@0: locking_enqueue_completed_buffer(buf); // enqueue completed buffer aoqi@0: aoqi@0: // While the current thread was enqueuing the buffer another thread aoqi@0: // may have a allocated a new buffer and inserted it into this pointer aoqi@0: // queue. If that happens then we just return so that the current aoqi@0: // thread doesn't overwrite the buffer allocated by the other thread aoqi@0: // and potentially losing some dirtied cards. aoqi@0: aoqi@0: if (_buf != NULL) return; aoqi@0: } else { aoqi@0: if (qset()->process_or_enqueue_complete_buffer(_buf)) { aoqi@0: // Recycle the buffer. No allocation. aoqi@0: _sz = qset()->buffer_size(); aoqi@0: _index = _sz; aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // Reallocate the buffer aoqi@0: _buf = qset()->allocate_buffer(); aoqi@0: _sz = qset()->buffer_size(); aoqi@0: _index = _sz; aoqi@0: assert(0 <= _index && _index <= _sz, "Invariant."); aoqi@0: } aoqi@0: aoqi@0: bool PtrQueueSet::process_or_enqueue_complete_buffer(void** buf) { aoqi@0: if (Thread::current()->is_Java_thread()) { aoqi@0: // We don't lock. It is fine to be epsilon-precise here. aoqi@0: if (_max_completed_queue == 0 || _max_completed_queue > 0 && aoqi@0: _n_completed_buffers >= _max_completed_queue + _completed_queue_padding) { aoqi@0: bool b = mut_process_buffer(buf); aoqi@0: if (b) { aoqi@0: // True here means that the buffer hasn't been deallocated and the caller may reuse it. aoqi@0: return true; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // The buffer will be enqueued. The caller will have to get a new one. aoqi@0: enqueue_complete_buffer(buf); aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: void PtrQueueSet::enqueue_complete_buffer(void** buf, size_t index) { aoqi@0: MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); aoqi@0: BufferNode* cbn = BufferNode::new_from_buffer(buf); aoqi@0: cbn->set_index(index); aoqi@0: if (_completed_buffers_tail == NULL) { aoqi@0: assert(_completed_buffers_head == NULL, "Well-formedness"); aoqi@0: _completed_buffers_head = cbn; aoqi@0: _completed_buffers_tail = cbn; aoqi@0: } else { aoqi@0: _completed_buffers_tail->set_next(cbn); aoqi@0: _completed_buffers_tail = cbn; aoqi@0: } aoqi@0: _n_completed_buffers++; aoqi@0: aoqi@0: if (!_process_completed && _process_completed_threshold >= 0 && aoqi@0: _n_completed_buffers >= _process_completed_threshold) { aoqi@0: _process_completed = true; aoqi@0: if (_notify_when_complete) aoqi@0: _cbl_mon->notify(); aoqi@0: } aoqi@0: debug_only(assert_completed_buffer_list_len_correct_locked()); aoqi@0: } aoqi@0: aoqi@0: int PtrQueueSet::completed_buffers_list_length() { aoqi@0: int n = 0; aoqi@0: BufferNode* cbn = _completed_buffers_head; aoqi@0: while (cbn != NULL) { aoqi@0: n++; aoqi@0: cbn = cbn->next(); aoqi@0: } aoqi@0: return n; aoqi@0: } aoqi@0: aoqi@0: void PtrQueueSet::assert_completed_buffer_list_len_correct() { aoqi@0: MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); aoqi@0: assert_completed_buffer_list_len_correct_locked(); aoqi@0: } aoqi@0: aoqi@0: void PtrQueueSet::assert_completed_buffer_list_len_correct_locked() { aoqi@0: guarantee(completed_buffers_list_length() == _n_completed_buffers, aoqi@0: "Completed buffer length is wrong."); aoqi@0: } aoqi@0: aoqi@0: void PtrQueueSet::set_buffer_size(size_t sz) { aoqi@0: assert(_sz == 0 && sz > 0, "Should be called only once."); aoqi@0: _sz = sz * oopSize; aoqi@0: } aoqi@0: aoqi@0: // Merge lists of buffers. Notify the processing threads. aoqi@0: // The source queue is emptied as a result. The queues aoqi@0: // must share the monitor. aoqi@0: void PtrQueueSet::merge_bufferlists(PtrQueueSet *src) { aoqi@0: assert(_cbl_mon == src->_cbl_mon, "Should share the same lock"); aoqi@0: MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); aoqi@0: if (_completed_buffers_tail == NULL) { aoqi@0: assert(_completed_buffers_head == NULL, "Well-formedness"); aoqi@0: _completed_buffers_head = src->_completed_buffers_head; aoqi@0: _completed_buffers_tail = src->_completed_buffers_tail; aoqi@0: } else { aoqi@0: assert(_completed_buffers_head != NULL, "Well formedness"); aoqi@0: if (src->_completed_buffers_head != NULL) { aoqi@0: _completed_buffers_tail->set_next(src->_completed_buffers_head); aoqi@0: _completed_buffers_tail = src->_completed_buffers_tail; aoqi@0: } aoqi@0: } aoqi@0: _n_completed_buffers += src->_n_completed_buffers; aoqi@0: aoqi@0: src->_n_completed_buffers = 0; aoqi@0: src->_completed_buffers_head = NULL; aoqi@0: src->_completed_buffers_tail = NULL; aoqi@0: aoqi@0: assert(_completed_buffers_head == NULL && _completed_buffers_tail == NULL || aoqi@0: _completed_buffers_head != NULL && _completed_buffers_tail != NULL, aoqi@0: "Sanity"); aoqi@0: } aoqi@0: aoqi@0: void PtrQueueSet::notify_if_necessary() { aoqi@0: MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); aoqi@0: if (_n_completed_buffers >= _process_completed_threshold || _max_completed_queue == 0) { aoqi@0: _process_completed = true; aoqi@0: if (_notify_when_complete) aoqi@0: _cbl_mon->notify(); aoqi@0: } aoqi@0: }