342N/A/*
3067N/A * Copyright (c) 2001, 2012, 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"
2034N/A#include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
1879N/A#include "gc_implementation/g1/satbQueue.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "memory/sharedHeap.hpp"
1879N/A#include "runtime/mutexLocker.hpp"
1879N/A#include "runtime/thread.hpp"
2815N/A#include "runtime/vmThread.hpp"
342N/A
3067N/Avoid ObjPtrQueue::flush() {
3067N/A // The buffer might contain refs into the CSet. We have to filter it
3067N/A // first before we flush it, otherwise we might end up with an
3067N/A // enqueued buffer with refs into the CSet which breaks our invariants.
3067N/A filter();
3067N/A PtrQueue::flush();
3067N/A}
3067N/A
2034N/A// This method removes entries from an SATB buffer that will not be
2034N/A// useful to the concurrent marking threads. An entry is removed if it
2034N/A// satisfies one of the following conditions:
2034N/A//
2034N/A// * it points to an object outside the G1 heap (G1's concurrent
2034N/A// marking only visits objects inside the G1 heap),
2034N/A// * it points to an object that has been allocated since marking
2034N/A// started (according to SATB those objects do not need to be
2034N/A// visited during marking), or
2034N/A// * it points to an object that has already been marked (no need to
2034N/A// process it again).
2034N/A//
2034N/A// The rest of the entries will be retained and are compacted towards
3067N/A// the top of the buffer. Note that, because we do not allow old
3067N/A// regions in the CSet during marking, all objects on the CSet regions
3067N/A// are young (eden or survivors) and therefore implicitly live. So any
3067N/A// references into the CSet will be removed during filtering.
2034N/A
3067N/Avoid ObjPtrQueue::filter() {
2034N/A G1CollectedHeap* g1h = G1CollectedHeap::heap();
2034N/A void** buf = _buf;
2034N/A size_t sz = _sz;
2034N/A
3067N/A if (buf == NULL) {
3067N/A // nothing to do
3067N/A return;
3067N/A }
3067N/A
2034N/A // Used for sanity checking at the end of the loop.
2034N/A debug_only(size_t entries = 0; size_t retained = 0;)
2034N/A
2034N/A size_t i = sz;
2034N/A size_t new_index = sz;
2034N/A
2034N/A while (i > _index) {
2034N/A assert(i > 0, "we should have at least one more entry to process");
2034N/A i -= oopSize;
2034N/A debug_only(entries += 1;)
2034N/A oop* p = (oop*) &buf[byte_index_to_index((int) i)];
2034N/A oop obj = *p;
2034N/A // NULL the entry so that unused parts of the buffer contain NULLs
2034N/A // at the end. If we are going to retain it we will copy it to its
2034N/A // final place. If we have retained all entries we have visited so
2034N/A // far, we'll just end up copying it to the same place.
2034N/A *p = NULL;
2034N/A
2034N/A bool retain = g1h->is_obj_ill(obj);
2034N/A if (retain) {
2034N/A assert(new_index > 0, "we should not have already filled up the buffer");
2034N/A new_index -= oopSize;
2034N/A assert(new_index >= i,
2034N/A "new_index should never be below i, as we alwaysr compact 'up'");
2034N/A oop* new_p = (oop*) &buf[byte_index_to_index((int) new_index)];
2034N/A assert(new_p >= p, "the destination location should never be below "
2034N/A "the source as we always compact 'up'");
2034N/A assert(*new_p == NULL,
2034N/A "we should have already cleared the destination location");
2034N/A *new_p = obj;
2034N/A debug_only(retained += 1;)
2034N/A }
2034N/A }
3067N/A
3067N/A#ifdef ASSERT
2034N/A size_t entries_calc = (sz - _index) / oopSize;
2034N/A assert(entries == entries_calc, "the number of entries we counted "
2034N/A "should match the number of entries we calculated");
2034N/A size_t retained_calc = (sz - new_index) / oopSize;
2034N/A assert(retained == retained_calc, "the number of retained entries we counted "
2034N/A "should match the number of retained entries we calculated");
3067N/A#endif // ASSERT
3067N/A
3067N/A _index = new_index;
3067N/A}
3067N/A
3067N/A// This method will first apply the above filtering to the buffer. If
3067N/A// post-filtering a large enough chunk of the buffer has been cleared
3067N/A// we can re-use the buffer (instead of enqueueing it) and we can just
3067N/A// allow the mutator to carry on executing using the same buffer
3067N/A// instead of replacing it.
3067N/A
3067N/Abool ObjPtrQueue::should_enqueue_buffer() {
3067N/A assert(_lock == NULL || _lock->owned_by_self(),
3067N/A "we should have taken the lock before calling this");
3067N/A
3067N/A // Even if G1SATBBufferEnqueueingThresholdPercent == 0 we have to
3067N/A // filter the buffer given that this will remove any references into
3067N/A // the CSet as we currently assume that no such refs will appear in
3067N/A // enqueued buffers.
3067N/A
3067N/A // This method should only be called if there is a non-NULL buffer
3067N/A // that is full.
3067N/A assert(_index == 0, "pre-condition");
3067N/A assert(_buf != NULL, "pre-condition");
3067N/A
3067N/A filter();
3067N/A
3067N/A size_t sz = _sz;
3067N/A size_t all_entries = sz / oopSize;
3067N/A size_t retained_entries = (sz - _index) / oopSize;
3067N/A size_t perc = retained_entries * 100 / all_entries;
2034N/A bool should_enqueue = perc > (size_t) G1SATBBufferEnqueueingThresholdPercent;
2034N/A return should_enqueue;
2034N/A}
2034N/A
342N/Avoid ObjPtrQueue::apply_closure(ObjectClosure* cl) {
342N/A if (_buf != NULL) {
342N/A apply_closure_to_buffer(cl, _buf, _index, _sz);
3067N/A }
3067N/A}
3067N/A
3067N/Avoid ObjPtrQueue::apply_closure_and_empty(ObjectClosure* cl) {
3067N/A if (_buf != NULL) {
3067N/A apply_closure_to_buffer(cl, _buf, _index, _sz);
342N/A _index = _sz;
342N/A }
342N/A}
342N/A
342N/Avoid ObjPtrQueue::apply_closure_to_buffer(ObjectClosure* cl,
342N/A void** buf, size_t index, size_t sz) {
342N/A if (cl == NULL) return;
342N/A for (size_t i = index; i < sz; i += oopSize) {
342N/A oop obj = (oop)buf[byte_index_to_index((int)i)];
342N/A // There can be NULL entries because of destructors.
342N/A if (obj != NULL) {
342N/A cl->do_object(obj);
342N/A }
342N/A }
342N/A}
845N/A
3067N/A#ifndef PRODUCT
3067N/A// Helpful for debugging
3067N/A
3067N/Avoid ObjPtrQueue::print(const char* name) {
3067N/A print(name, _buf, _index, _sz);
3067N/A}
3067N/A
3067N/Avoid ObjPtrQueue::print(const char* name,
3067N/A void** buf, size_t index, size_t sz) {
3067N/A gclog_or_tty->print_cr(" SATB BUFFER [%s] buf: "PTR_FORMAT" "
3067N/A "index: "SIZE_FORMAT" sz: "SIZE_FORMAT,
3067N/A name, buf, index, sz);
3067N/A}
3067N/A#endif // PRODUCT
3067N/A
845N/A#ifdef ASSERT
845N/Avoid ObjPtrQueue::verify_oops_in_buffer() {
845N/A if (_buf == NULL) return;
845N/A for (size_t i = _index; i < _sz; i += oopSize) {
845N/A oop obj = (oop)_buf[byte_index_to_index((int)i)];
845N/A assert(obj != NULL && obj->is_oop(true /* ignore mark word */),
845N/A "Not an oop");
845N/A }
845N/A}
845N/A#endif
845N/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
342N/ASATBMarkQueueSet::SATBMarkQueueSet() :
3067N/A PtrQueueSet(), _closure(NULL), _par_closures(NULL),
3067N/A _shared_satb_queue(this, true /*perm*/) { }
342N/A
342N/Avoid SATBMarkQueueSet::initialize(Monitor* cbl_mon, Mutex* fl_lock,
1111N/A int process_completed_threshold,
342N/A Mutex* lock) {
1111N/A PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold, -1);
342N/A _shared_satb_queue.set_lock(lock);
342N/A if (ParallelGCThreads > 0) {
3863N/A _par_closures = NEW_C_HEAP_ARRAY(ObjectClosure*, ParallelGCThreads, mtGC);
342N/A }
342N/A}
342N/A
342N/Avoid SATBMarkQueueSet::handle_zero_index_for_thread(JavaThread* t) {
845N/A DEBUG_ONLY(t->satb_mark_queue().verify_oops_in_buffer();)
342N/A t->satb_mark_queue().handle_zero_index();
342N/A}
342N/A
1317N/A#ifdef ASSERT
1317N/Avoid SATBMarkQueueSet::dump_active_values(JavaThread* first,
1317N/A bool expected_active) {
1317N/A gclog_or_tty->print_cr("SATB queue active values for Java Threads");
1317N/A gclog_or_tty->print_cr(" SATB queue set: active is %s",
1317N/A (is_active()) ? "TRUE" : "FALSE");
1317N/A gclog_or_tty->print_cr(" expected_active is %s",
1317N/A (expected_active) ? "TRUE" : "FALSE");
1317N/A for (JavaThread* t = first; t; t = t->next()) {
1317N/A bool active = t->satb_mark_queue().is_active();
1317N/A gclog_or_tty->print_cr(" thread %s, active is %s",
1317N/A t->name(), (active) ? "TRUE" : "FALSE");
1317N/A }
1317N/A}
1317N/A#endif // ASSERT
1317N/A
1317N/Avoid SATBMarkQueueSet::set_active_all_threads(bool b,
1317N/A bool expected_active) {
1317N/A assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
1317N/A JavaThread* first = Threads::first();
1317N/A
1317N/A#ifdef ASSERT
1317N/A if (_all_active != expected_active) {
1317N/A dump_active_values(first, expected_active);
1317N/A
1317N/A // I leave this here as a guarantee, instead of an assert, so
1317N/A // that it will still be compiled in if we choose to uncomment
1317N/A // the #ifdef ASSERT in a product build. The whole block is
1317N/A // within an #ifdef ASSERT so the guarantee will not be compiled
1317N/A // in a product build anyway.
1317N/A guarantee(false,
1317N/A "SATB queue set has an unexpected active value");
1317N/A }
1317N/A#endif // ASSERT
342N/A _all_active = b;
1317N/A
1317N/A for (JavaThread* t = first; t; t = t->next()) {
1317N/A#ifdef ASSERT
1317N/A bool active = t->satb_mark_queue().is_active();
1317N/A if (active != expected_active) {
1317N/A dump_active_values(first, expected_active);
1317N/A
1317N/A // I leave this here as a guarantee, instead of an assert, so
1317N/A // that it will still be compiled in if we choose to uncomment
1317N/A // the #ifdef ASSERT in a product build. The whole block is
1317N/A // within an #ifdef ASSERT so the guarantee will not be compiled
1317N/A // in a product build anyway.
1317N/A guarantee(false,
1317N/A "thread has an unexpected active value in its SATB queue");
1317N/A }
1317N/A#endif // ASSERT
342N/A t->satb_mark_queue().set_active(b);
342N/A }
342N/A}
342N/A
3067N/Avoid SATBMarkQueueSet::filter_thread_buffers() {
3067N/A for(JavaThread* t = Threads::first(); t; t = t->next()) {
3067N/A t->satb_mark_queue().filter();
3067N/A }
3067N/A shared_satb_queue()->filter();
3067N/A}
3067N/A
342N/Avoid SATBMarkQueueSet::set_closure(ObjectClosure* closure) {
342N/A _closure = closure;
342N/A}
342N/A
342N/Avoid SATBMarkQueueSet::set_par_closure(int i, ObjectClosure* par_closure) {
342N/A assert(ParallelGCThreads > 0 && _par_closures != NULL, "Precondition");
342N/A _par_closures[i] = par_closure;
342N/A}
342N/A
342N/Avoid SATBMarkQueueSet::iterate_closure_all_threads() {
342N/A for(JavaThread* t = Threads::first(); t; t = t->next()) {
3067N/A t->satb_mark_queue().apply_closure_and_empty(_closure);
342N/A }
3067N/A shared_satb_queue()->apply_closure_and_empty(_closure);
342N/A}
342N/A
342N/Avoid SATBMarkQueueSet::par_iterate_closure_all_threads(int worker) {
342N/A SharedHeap* sh = SharedHeap::heap();
342N/A int parity = sh->strong_roots_parity();
342N/A
342N/A for(JavaThread* t = Threads::first(); t; t = t->next()) {
342N/A if (t->claim_oops_do(true, parity)) {
3067N/A t->satb_mark_queue().apply_closure_and_empty(_par_closures[worker]);
342N/A }
342N/A }
2815N/A
2815N/A // We also need to claim the VMThread so that its parity is updated
2815N/A // otherwise the next call to Thread::possibly_parallel_oops_do inside
2815N/A // a StrongRootsScope might skip the VMThread because it has a stale
2815N/A // parity that matches the parity set by the StrongRootsScope
2815N/A //
2815N/A // Whichever worker succeeds in claiming the VMThread gets to do
2815N/A // the shared queue.
2815N/A
2815N/A VMThread* vmt = VMThread::vm_thread();
2815N/A if (vmt->claim_oops_do(true, parity)) {
3067N/A shared_satb_queue()->apply_closure_and_empty(_par_closures[worker]);
342N/A }
342N/A}
342N/A
342N/Abool SATBMarkQueueSet::apply_closure_to_completed_buffer_work(bool par,
342N/A int worker) {
1111N/A BufferNode* nd = NULL;
342N/A {
342N/A MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
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) _completed_buffers_tail = NULL;
342N/A _n_completed_buffers--;
342N/A if (_n_completed_buffers == 0) _process_completed = false;
342N/A }
342N/A }
342N/A ObjectClosure* cl = (par ? _par_closures[worker] : _closure);
342N/A if (nd != NULL) {
1111N/A void **buf = BufferNode::make_buffer_from_node(nd);
1111N/A ObjPtrQueue::apply_closure_to_buffer(cl, buf, 0, _sz);
1111N/A deallocate_buffer(buf);
342N/A return true;
342N/A } else {
342N/A return false;
342N/A }
342N/A}
342N/A
3067N/Avoid SATBMarkQueueSet::iterate_completed_buffers_read_only(ObjectClosure* cl) {
3067N/A assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
3067N/A assert(cl != NULL, "pre-condition");
3067N/A
3067N/A BufferNode* nd = _completed_buffers_head;
3067N/A while (nd != NULL) {
3067N/A void** buf = BufferNode::make_buffer_from_node(nd);
3067N/A ObjPtrQueue::apply_closure_to_buffer(cl, buf, 0, _sz);
3067N/A nd = nd->next();
3067N/A }
3067N/A}
3067N/A
3067N/Avoid SATBMarkQueueSet::iterate_thread_buffers_read_only(ObjectClosure* cl) {
3067N/A assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
3067N/A assert(cl != NULL, "pre-condition");
3067N/A
3067N/A for (JavaThread* t = Threads::first(); t; t = t->next()) {
3067N/A t->satb_mark_queue().apply_closure(cl);
3067N/A }
3067N/A shared_satb_queue()->apply_closure(cl);
3067N/A}
3067N/A
3067N/A#ifndef PRODUCT
3067N/A// Helpful for debugging
3067N/A
3067N/A#define SATB_PRINTER_BUFFER_SIZE 256
3067N/A
3067N/Avoid SATBMarkQueueSet::print_all(const char* msg) {
3067N/A char buffer[SATB_PRINTER_BUFFER_SIZE];
3067N/A assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
3067N/A
3067N/A gclog_or_tty->cr();
3067N/A gclog_or_tty->print_cr("SATB BUFFERS [%s]", msg);
3067N/A
3067N/A BufferNode* nd = _completed_buffers_head;
3067N/A int i = 0;
3067N/A while (nd != NULL) {
3067N/A void** buf = BufferNode::make_buffer_from_node(nd);
3067N/A jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Enqueued: %d", i);
3067N/A ObjPtrQueue::print(buffer, buf, 0, _sz);
3067N/A nd = nd->next();
3067N/A i += 1;
3067N/A }
3067N/A
3067N/A for (JavaThread* t = Threads::first(); t; t = t->next()) {
3067N/A jio_snprintf(buffer, SATB_PRINTER_BUFFER_SIZE, "Thread: %s", t->name());
3067N/A t->satb_mark_queue().print(buffer);
3067N/A }
3067N/A
3067N/A shared_satb_queue()->print("Shared");
3067N/A
3067N/A gclog_or_tty->cr();
3067N/A}
3067N/A#endif // PRODUCT
3067N/A
342N/Avoid SATBMarkQueueSet::abandon_partial_marking() {
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 _completed_buffers_tail = NULL;
342N/A _n_completed_buffers = 0;
845N/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 }
342N/A assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
342N/A // So we can safely manipulate these queues.
342N/A for (JavaThread* t = Threads::first(); t; t = t->next()) {
342N/A t->satb_mark_queue().reset();
342N/A }
3067N/A shared_satb_queue()->reset();
342N/A}