ysr@777: /* tschatzl@8662: * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. ysr@777: * ysr@777: */ ysr@777: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/g1/dirtyCardQueue.hpp" tschatzl@6541: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" stefank@2314: #include "gc_implementation/g1/heapRegionRemSet.hpp" stefank@2314: #include "runtime/atomic.hpp" stefank@2314: #include "runtime/mutexLocker.hpp" stefank@2314: #include "runtime/safepoint.hpp" stefank@4299: #include "runtime/thread.inline.hpp" stefank@2314: #include "utilities/workgroup.hpp" ysr@777: ysr@777: bool DirtyCardQueue::apply_closure(CardTableEntryClosure* cl, ysr@777: bool consume, vkempik@6552: uint worker_i) { ysr@777: bool res = true; ysr@777: if (_buf != NULL) { ysr@777: res = apply_closure_to_buffer(cl, _buf, _index, _sz, ysr@777: consume, vkempik@6552: worker_i); ysr@777: if (res && consume) _index = _sz; ysr@777: } ysr@777: return res; ysr@777: } ysr@777: ysr@777: bool DirtyCardQueue::apply_closure_to_buffer(CardTableEntryClosure* cl, ysr@777: void** buf, ysr@777: size_t index, size_t sz, ysr@777: bool consume, vkempik@6552: uint worker_i) { ysr@777: if (cl == NULL) return true; ysr@777: for (size_t i = index; i < sz; i += oopSize) { ysr@777: int ind = byte_index_to_index((int)i); ysr@777: jbyte* card_ptr = (jbyte*)buf[ind]; ysr@777: if (card_ptr != NULL) { ysr@777: // Set the entry to null, so we don't do it again (via the test ysr@777: // above) if we reconsider this buffer. ysr@777: if (consume) buf[ind] = NULL; ysr@777: if (!cl->do_card_ptr(card_ptr, worker_i)) return false; ysr@777: } ysr@777: } ysr@777: return true; ysr@777: } ysr@777: ysr@777: #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away ysr@777: #pragma warning( disable:4355 ) // 'this' : used in base member initializer list ysr@777: #endif // _MSC_VER ysr@777: iveresov@1546: DirtyCardQueueSet::DirtyCardQueueSet(bool notify_when_complete) : iveresov@1546: PtrQueueSet(notify_when_complete), tschatzl@6930: _mut_process_closure(NULL), ysr@777: _shared_dirty_card_queue(this, true /*perm*/), ysr@777: _free_ids(NULL), ysr@777: _processed_buffers_mut(0), _processed_buffers_rs_thread(0) ysr@777: { ysr@777: _all_active = true; ysr@777: } ysr@777: iveresov@1230: // Determines how many mutator threads can process the buffers in parallel. vkempik@6552: uint DirtyCardQueueSet::num_par_ids() { tschatzl@8662: return (uint)os::initial_active_processor_count(); ysr@777: } ysr@777: tschatzl@6930: void DirtyCardQueueSet::initialize(CardTableEntryClosure* cl, Monitor* cbl_mon, Mutex* fl_lock, iveresov@1546: int process_completed_threshold, ysr@777: int max_completed_queue, iveresov@1051: Mutex* lock, PtrQueueSet* fl_owner) { tschatzl@6930: _mut_process_closure = cl; iveresov@1546: PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold, iveresov@1546: max_completed_queue, fl_owner); tonyp@1318: set_buffer_size(G1UpdateBufferSize); ysr@777: _shared_dirty_card_queue.set_lock(lock); ysr@777: _free_ids = new FreeIdSet((int) num_par_ids(), _cbl_mon); ysr@777: } ysr@777: ysr@777: void DirtyCardQueueSet::handle_zero_index_for_thread(JavaThread* t) { ysr@777: t->dirty_card_queue().handle_zero_index(); ysr@777: } ysr@777: tschatzl@6930: void DirtyCardQueueSet::iterate_closure_all_threads(CardTableEntryClosure* cl, tschatzl@6930: bool consume, vkempik@6552: uint worker_i) { ysr@777: assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); ysr@777: for(JavaThread* t = Threads::first(); t; t = t->next()) { tschatzl@6930: bool b = t->dirty_card_queue().apply_closure(cl, consume); ysr@777: guarantee(b, "Should not be interrupted."); ysr@777: } tschatzl@6930: bool b = shared_dirty_card_queue()->apply_closure(cl, ysr@777: consume, ysr@777: worker_i); ysr@777: guarantee(b, "Should not be interrupted."); ysr@777: } ysr@777: ysr@777: bool DirtyCardQueueSet::mut_process_buffer(void** buf) { ysr@777: ysr@777: // Used to determine if we had already claimed a par_id ysr@777: // before entering this method. ysr@777: bool already_claimed = false; ysr@777: ysr@777: // We grab the current JavaThread. ysr@777: JavaThread* thread = JavaThread::current(); ysr@777: ysr@777: // We get the the number of any par_id that this thread ysr@777: // might have already claimed. vkempik@6552: uint worker_i = thread->get_claimed_par_id(); ysr@777: vkempik@6552: // If worker_i is not UINT_MAX then the thread has already claimed ysr@777: // a par_id. We make note of it using the already_claimed value vkempik@6552: if (worker_i != UINT_MAX) { ysr@777: already_claimed = true; ysr@777: } else { ysr@777: ysr@777: // Otherwise we need to claim a par id ysr@777: worker_i = _free_ids->claim_par_id(); ysr@777: ysr@777: // And store the par_id value in the thread ysr@777: thread->set_claimed_par_id(worker_i); ysr@777: } ysr@777: ysr@777: bool b = false; vkempik@6552: if (worker_i != UINT_MAX) { tschatzl@6930: b = DirtyCardQueue::apply_closure_to_buffer(_mut_process_closure, buf, 0, ysr@777: _sz, true, worker_i); ysr@777: if (b) Atomic::inc(&_processed_buffers_mut); ysr@777: ysr@777: // If we had not claimed an id before entering the method ysr@777: // then we must release the id. ysr@777: if (!already_claimed) { ysr@777: ysr@777: // we release the id ysr@777: _free_ids->release_par_id(worker_i); ysr@777: vkempik@6552: // and set the claimed_id in the thread to UINT_MAX vkempik@6552: thread->set_claimed_par_id(UINT_MAX); ysr@777: } ysr@777: } ysr@777: return b; ysr@777: } ysr@777: iveresov@1546: iveresov@1546: BufferNode* johnc@1525: DirtyCardQueueSet::get_completed_buffer(int stop_at) { iveresov@1546: BufferNode* nd = NULL; ysr@777: MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); ysr@777: ysr@777: if ((int)_n_completed_buffers <= stop_at) { ysr@777: _process_completed = false; ysr@777: return NULL; ysr@777: } ysr@777: ysr@777: if (_completed_buffers_head != NULL) { ysr@777: nd = _completed_buffers_head; iveresov@1546: _completed_buffers_head = nd->next(); ysr@777: if (_completed_buffers_head == NULL) ysr@777: _completed_buffers_tail = NULL; ysr@777: _n_completed_buffers--; iveresov@1546: assert(_n_completed_buffers >= 0, "Invariant"); ysr@777: } ysr@777: debug_only(assert_completed_buffer_list_len_correct_locked()); ysr@777: return nd; ysr@777: } ysr@777: ysr@777: bool DirtyCardQueueSet:: johnc@2060: apply_closure_to_completed_buffer_helper(CardTableEntryClosure* cl, vkempik@6552: uint worker_i, iveresov@1546: BufferNode* nd) { ysr@777: if (nd != NULL) { iveresov@1546: void **buf = BufferNode::make_buffer_from_node(nd); iveresov@1546: size_t index = nd->index(); ysr@777: bool b = johnc@2060: DirtyCardQueue::apply_closure_to_buffer(cl, buf, iveresov@1546: index, _sz, ysr@777: true, worker_i); ysr@777: if (b) { ysr@777: deallocate_buffer(buf); ysr@777: return true; // In normal case, go on to next buffer. ysr@777: } else { iveresov@1546: enqueue_complete_buffer(buf, index); ysr@777: return false; ysr@777: } ysr@777: } else { ysr@777: return false; ysr@777: } ysr@777: } ysr@777: johnc@2060: bool DirtyCardQueueSet::apply_closure_to_completed_buffer(CardTableEntryClosure* cl, vkempik@6552: uint worker_i, johnc@2060: int stop_at, johnc@2060: bool during_pause) { johnc@2060: assert(!during_pause || stop_at == 0, "Should not leave any completed buffers during a pause"); johnc@2060: BufferNode* nd = get_completed_buffer(stop_at); johnc@2060: bool res = apply_closure_to_completed_buffer_helper(cl, worker_i, nd); johnc@2060: if (res) Atomic::inc(&_processed_buffers_rs_thread); johnc@2060: return res; johnc@2060: } johnc@2060: tschatzl@6930: void DirtyCardQueueSet::apply_closure_to_all_completed_buffers(CardTableEntryClosure* cl) { iveresov@1546: BufferNode* nd = _completed_buffers_head; ysr@777: while (nd != NULL) { ysr@777: bool b = tschatzl@6930: DirtyCardQueue::apply_closure_to_buffer(cl, iveresov@1546: BufferNode::make_buffer_from_node(nd), iveresov@1546: 0, _sz, false); ysr@777: guarantee(b, "Should not stop early."); iveresov@1546: nd = nd->next(); ysr@777: } ysr@777: } ysr@777: tschatzl@6930: void DirtyCardQueueSet::par_apply_closure_to_all_completed_buffers(CardTableEntryClosure* cl) { tschatzl@6930: BufferNode* nd = _cur_par_buffer_node; tschatzl@6930: while (nd != NULL) { tschatzl@6930: BufferNode* next = (BufferNode*)nd->next(); tschatzl@6930: BufferNode* actual = (BufferNode*)Atomic::cmpxchg_ptr((void*)next, (volatile void*)&_cur_par_buffer_node, (void*)nd); tschatzl@6930: if (actual == nd) { tschatzl@6930: bool b = tschatzl@6930: DirtyCardQueue::apply_closure_to_buffer(cl, tschatzl@6930: BufferNode::make_buffer_from_node(actual), tschatzl@6930: 0, _sz, false); tschatzl@6930: guarantee(b, "Should not stop early."); tschatzl@6930: nd = next; tschatzl@6930: } else { tschatzl@6930: nd = actual; tschatzl@6930: } tschatzl@6930: } tschatzl@6930: } tschatzl@6930: johnc@2060: // Deallocates any completed log buffers johnc@2060: void DirtyCardQueueSet::clear() { iveresov@1546: BufferNode* buffers_to_delete = NULL; ysr@777: { ysr@777: MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag); ysr@777: while (_completed_buffers_head != NULL) { iveresov@1546: BufferNode* nd = _completed_buffers_head; iveresov@1546: _completed_buffers_head = nd->next(); iveresov@1546: nd->set_next(buffers_to_delete); ysr@777: buffers_to_delete = nd; ysr@777: } ysr@777: _n_completed_buffers = 0; ysr@777: _completed_buffers_tail = NULL; ysr@777: debug_only(assert_completed_buffer_list_len_correct_locked()); ysr@777: } ysr@777: while (buffers_to_delete != NULL) { iveresov@1546: BufferNode* nd = buffers_to_delete; iveresov@1546: buffers_to_delete = nd->next(); iveresov@1546: deallocate_buffer(BufferNode::make_buffer_from_node(nd)); ysr@777: } johnc@2060: johnc@2060: } johnc@2060: johnc@2060: void DirtyCardQueueSet::abandon_logs() { johnc@2060: assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); johnc@2060: clear(); ysr@777: // Since abandon is done only at safepoints, we can safely manipulate ysr@777: // these queues. ysr@777: for (JavaThread* t = Threads::first(); t; t = t->next()) { ysr@777: t->dirty_card_queue().reset(); ysr@777: } ysr@777: shared_dirty_card_queue()->reset(); ysr@777: } ysr@777: ysr@777: ysr@777: void DirtyCardQueueSet::concatenate_logs() { ysr@777: // Iterate over all the threads, if we find a partial log add it to ysr@777: // the global list of logs. Temporarily turn off the limit on the number ysr@777: // of outstanding buffers. ysr@777: int save_max_completed_queue = _max_completed_queue; ysr@777: _max_completed_queue = max_jint; ysr@777: assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint."); ysr@777: for (JavaThread* t = Threads::first(); t; t = t->next()) { ysr@777: DirtyCardQueue& dcq = t->dirty_card_queue(); ysr@777: if (dcq.size() != 0) { ysr@777: void **buf = t->dirty_card_queue().get_buf(); ysr@777: // We must NULL out the unused entries, then enqueue. ysr@777: for (size_t i = 0; i < t->dirty_card_queue().get_index(); i += oopSize) { ysr@777: buf[PtrQueue::byte_index_to_index((int)i)] = NULL; ysr@777: } ysr@777: enqueue_complete_buffer(dcq.get_buf(), dcq.get_index()); ysr@777: dcq.reinitialize(); ysr@777: } ysr@777: } ysr@777: if (_shared_dirty_card_queue.size() != 0) { ysr@777: enqueue_complete_buffer(_shared_dirty_card_queue.get_buf(), ysr@777: _shared_dirty_card_queue.get_index()); ysr@777: _shared_dirty_card_queue.reinitialize(); ysr@777: } ysr@777: // Restore the completed buffer queue limit. ysr@777: _max_completed_queue = save_max_completed_queue; ysr@777: }