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