342N/A/*
1625N/A * Copyright (c) 2001, 2010, 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/dirtyCardQueue.hpp"
1879N/A#include "gc_implementation/g1/heapRegionRemSet.hpp"
1879N/A#include "runtime/atomic.hpp"
1879N/A#include "runtime/mutexLocker.hpp"
1879N/A#include "runtime/safepoint.hpp"
1879N/A#include "runtime/thread.hpp"
1879N/A#include "utilities/workgroup.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
342N/Abool DirtyCardQueue::apply_closure(CardTableEntryClosure* cl,
342N/A bool consume,
342N/A size_t worker_i) {
342N/A bool res = true;
342N/A if (_buf != NULL) {
342N/A res = apply_closure_to_buffer(cl, _buf, _index, _sz,
342N/A consume,
342N/A (int) worker_i);
342N/A if (res && consume) _index = _sz;
342N/A }
342N/A return res;
342N/A}
342N/A
342N/Abool DirtyCardQueue::apply_closure_to_buffer(CardTableEntryClosure* cl,
342N/A void** buf,
342N/A size_t index, size_t sz,
342N/A bool consume,
342N/A int worker_i) {
342N/A if (cl == NULL) return true;
342N/A for (size_t i = index; i < sz; i += oopSize) {
342N/A int ind = byte_index_to_index((int)i);
342N/A jbyte* card_ptr = (jbyte*)buf[ind];
342N/A if (card_ptr != NULL) {
342N/A // Set the entry to null, so we don't do it again (via the test
342N/A // above) if we reconsider this buffer.
342N/A if (consume) buf[ind] = NULL;
342N/A if (!cl->do_card_ptr(card_ptr, worker_i)) return false;
342N/A }
342N/A }
342N/A return true;
342N/A}
342N/A
342N/A#ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
342N/A#pragma warning( disable:4355 ) // 'this' : used in base member initializer list
342N/A#endif // _MSC_VER
342N/A
1111N/ADirtyCardQueueSet::DirtyCardQueueSet(bool notify_when_complete) :
1111N/A PtrQueueSet(notify_when_complete),
342N/A _closure(NULL),
342N/A _shared_dirty_card_queue(this, true /*perm*/),
342N/A _free_ids(NULL),
342N/A _processed_buffers_mut(0), _processed_buffers_rs_thread(0)
342N/A{
342N/A _all_active = true;
342N/A}
342N/A
795N/A// Determines how many mutator threads can process the buffers in parallel.
342N/Asize_t DirtyCardQueueSet::num_par_ids() {
795N/A return os::processor_count();
342N/A}
342N/A
342N/Avoid DirtyCardQueueSet::initialize(Monitor* cbl_mon, Mutex* fl_lock,
1111N/A int process_completed_threshold,
342N/A int max_completed_queue,
616N/A Mutex* lock, PtrQueueSet* fl_owner) {
1111N/A PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold,
1111N/A max_completed_queue, fl_owner);
883N/A set_buffer_size(G1UpdateBufferSize);
342N/A _shared_dirty_card_queue.set_lock(lock);
342N/A _free_ids = new FreeIdSet((int) num_par_ids(), _cbl_mon);
342N/A}
342N/A
342N/Avoid DirtyCardQueueSet::handle_zero_index_for_thread(JavaThread* t) {
342N/A t->dirty_card_queue().handle_zero_index();
342N/A}
342N/A
342N/Avoid DirtyCardQueueSet::set_closure(CardTableEntryClosure* closure) {
342N/A _closure = closure;
342N/A}
342N/A
342N/Avoid DirtyCardQueueSet::iterate_closure_all_threads(bool consume,
342N/A size_t worker_i) {
342N/A assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
342N/A for(JavaThread* t = Threads::first(); t; t = t->next()) {
342N/A bool b = t->dirty_card_queue().apply_closure(_closure, consume);
342N/A guarantee(b, "Should not be interrupted.");
342N/A }
342N/A bool b = shared_dirty_card_queue()->apply_closure(_closure,
342N/A consume,
342N/A worker_i);
342N/A guarantee(b, "Should not be interrupted.");
342N/A}
342N/A
342N/Abool DirtyCardQueueSet::mut_process_buffer(void** buf) {
342N/A
342N/A // Used to determine if we had already claimed a par_id
342N/A // before entering this method.
342N/A bool already_claimed = false;
342N/A
342N/A // We grab the current JavaThread.
342N/A JavaThread* thread = JavaThread::current();
342N/A
342N/A // We get the the number of any par_id that this thread
342N/A // might have already claimed.
342N/A int worker_i = thread->get_claimed_par_id();
342N/A
342N/A // If worker_i is not -1 then the thread has already claimed
342N/A // a par_id. We make note of it using the already_claimed value
342N/A if (worker_i != -1) {
342N/A already_claimed = true;
342N/A } else {
342N/A
342N/A // Otherwise we need to claim a par id
342N/A worker_i = _free_ids->claim_par_id();
342N/A
342N/A // And store the par_id value in the thread
342N/A thread->set_claimed_par_id(worker_i);
342N/A }
342N/A
342N/A bool b = false;
342N/A if (worker_i != -1) {
342N/A b = DirtyCardQueue::apply_closure_to_buffer(_closure, buf, 0,
342N/A _sz, true, worker_i);
342N/A if (b) Atomic::inc(&_processed_buffers_mut);
342N/A
342N/A // If we had not claimed an id before entering the method
342N/A // then we must release the id.
342N/A if (!already_claimed) {
342N/A
342N/A // we release the id
342N/A _free_ids->release_par_id(worker_i);
342N/A
342N/A // and set the claimed_id in the thread to -1
342N/A thread->set_claimed_par_id(-1);
342N/A }
342N/A }
342N/A return b;
342N/A}
342N/A
1111N/A
1111N/ABufferNode*
1090N/ADirtyCardQueueSet::get_completed_buffer(int stop_at) {
1111N/A BufferNode* nd = NULL;
342N/A MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
342N/A
342N/A if ((int)_n_completed_buffers <= stop_at) {
342N/A _process_completed = false;
342N/A return NULL;
342N/A }
342N/A
342N/A if (_completed_buffers_head != NULL) {
342N/A nd = _completed_buffers_head;
1111N/A _completed_buffers_head = nd->next();
342N/A if (_completed_buffers_head == NULL)
342N/A _completed_buffers_tail = NULL;
342N/A _n_completed_buffers--;
1111N/A assert(_n_completed_buffers >= 0, "Invariant");
342N/A }
342N/A debug_only(assert_completed_buffer_list_len_correct_locked());
342N/A return nd;
342N/A}
342N/A
342N/Abool DirtyCardQueueSet::
1625N/Aapply_closure_to_completed_buffer_helper(CardTableEntryClosure* cl,
1625N/A int worker_i,
1111N/A BufferNode* nd) {
342N/A if (nd != NULL) {
1111N/A void **buf = BufferNode::make_buffer_from_node(nd);
1111N/A size_t index = nd->index();
342N/A bool b =
1625N/A DirtyCardQueue::apply_closure_to_buffer(cl, buf,
1111N/A index, _sz,
342N/A true, worker_i);
342N/A if (b) {
342N/A deallocate_buffer(buf);
342N/A return true; // In normal case, go on to next buffer.
342N/A } else {
1111N/A enqueue_complete_buffer(buf, index);
342N/A return false;
342N/A }
342N/A } else {
342N/A return false;
342N/A }
342N/A}
342N/A
1625N/Abool DirtyCardQueueSet::apply_closure_to_completed_buffer(CardTableEntryClosure* cl,
1625N/A int worker_i,
1625N/A int stop_at,
1625N/A bool during_pause) {
1625N/A assert(!during_pause || stop_at == 0, "Should not leave any completed buffers during a pause");
1625N/A BufferNode* nd = get_completed_buffer(stop_at);
1625N/A bool res = apply_closure_to_completed_buffer_helper(cl, worker_i, nd);
1625N/A if (res) Atomic::inc(&_processed_buffers_rs_thread);
1625N/A return res;
1625N/A}
1625N/A
342N/Abool DirtyCardQueueSet::apply_closure_to_completed_buffer(int worker_i,
342N/A int stop_at,
1625N/A bool during_pause) {
1625N/A return apply_closure_to_completed_buffer(_closure, worker_i,
1625N/A stop_at, during_pause);
342N/A}
342N/A
342N/Avoid DirtyCardQueueSet::apply_closure_to_all_completed_buffers() {
1111N/A BufferNode* nd = _completed_buffers_head;
342N/A while (nd != NULL) {
342N/A bool b =
1111N/A DirtyCardQueue::apply_closure_to_buffer(_closure,
1111N/A BufferNode::make_buffer_from_node(nd),
1111N/A 0, _sz, false);
342N/A guarantee(b, "Should not stop early.");
1111N/A nd = nd->next();
342N/A }
342N/A}
342N/A
1625N/A// Deallocates any completed log buffers
1625N/Avoid DirtyCardQueueSet::clear() {
1111N/A BufferNode* buffers_to_delete = NULL;
342N/A {
342N/A MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
342N/A while (_completed_buffers_head != NULL) {
1111N/A BufferNode* nd = _completed_buffers_head;
1111N/A _completed_buffers_head = nd->next();
1111N/A nd->set_next(buffers_to_delete);
342N/A buffers_to_delete = nd;
342N/A }
342N/A _n_completed_buffers = 0;
342N/A _completed_buffers_tail = NULL;
342N/A debug_only(assert_completed_buffer_list_len_correct_locked());
342N/A }
342N/A while (buffers_to_delete != NULL) {
1111N/A BufferNode* nd = buffers_to_delete;
1111N/A buffers_to_delete = nd->next();
1111N/A deallocate_buffer(BufferNode::make_buffer_from_node(nd));
342N/A }
1625N/A
1625N/A}
1625N/A
1625N/Avoid DirtyCardQueueSet::abandon_logs() {
1625N/A assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
1625N/A clear();
342N/A // Since abandon is done only at safepoints, we can safely manipulate
342N/A // these queues.
342N/A for (JavaThread* t = Threads::first(); t; t = t->next()) {
342N/A t->dirty_card_queue().reset();
342N/A }
342N/A shared_dirty_card_queue()->reset();
342N/A}
342N/A
342N/A
342N/Avoid DirtyCardQueueSet::concatenate_logs() {
342N/A // Iterate over all the threads, if we find a partial log add it to
342N/A // the global list of logs. Temporarily turn off the limit on the number
342N/A // of outstanding buffers.
342N/A int save_max_completed_queue = _max_completed_queue;
342N/A _max_completed_queue = max_jint;
342N/A assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
342N/A for (JavaThread* t = Threads::first(); t; t = t->next()) {
342N/A DirtyCardQueue& dcq = t->dirty_card_queue();
342N/A if (dcq.size() != 0) {
342N/A void **buf = t->dirty_card_queue().get_buf();
342N/A // We must NULL out the unused entries, then enqueue.
342N/A for (size_t i = 0; i < t->dirty_card_queue().get_index(); i += oopSize) {
342N/A buf[PtrQueue::byte_index_to_index((int)i)] = NULL;
342N/A }
342N/A enqueue_complete_buffer(dcq.get_buf(), dcq.get_index());
342N/A dcq.reinitialize();
342N/A }
342N/A }
342N/A if (_shared_dirty_card_queue.size() != 0) {
342N/A enqueue_complete_buffer(_shared_dirty_card_queue.get_buf(),
342N/A _shared_dirty_card_queue.get_index());
342N/A _shared_dirty_card_queue.reinitialize();
342N/A }
342N/A // Restore the completed buffer queue limit.
342N/A _max_completed_queue = save_max_completed_queue;
342N/A}