2008-06-05 15:57:56 -07:00
|
|
|
/*
|
2016-02-17 16:00:27 -05:00
|
|
|
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
|
2008-06-05 15:57:56 -07:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-27 19:08:38 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2008-06-05 15:57:56 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "precompiled.hpp"
|
2015-05-13 15:16:06 +02:00
|
|
|
#include "gc/g1/dirtyCardQueue.hpp"
|
|
|
|
#include "gc/g1/g1CollectedHeap.inline.hpp"
|
|
|
|
#include "gc/g1/heapRegionRemSet.hpp"
|
|
|
|
#include "gc/shared/workgroup.hpp"
|
2014-06-04 11:56:44 +02:00
|
|
|
#include "runtime/atomic.inline.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
#include "runtime/mutexLocker.hpp"
|
|
|
|
#include "runtime/safepoint.hpp"
|
2012-11-27 14:20:21 +01:00
|
|
|
#include "runtime/thread.inline.hpp"
|
2008-06-05 15:57:56 -07:00
|
|
|
|
2016-01-08 15:41:44 -05:00
|
|
|
// Represents a set of free small integer ids.
|
|
|
|
class FreeIdSet : public CHeapObj<mtGC> {
|
|
|
|
enum {
|
|
|
|
end_of_list = UINT_MAX,
|
|
|
|
claimed = UINT_MAX - 1
|
|
|
|
};
|
|
|
|
|
|
|
|
uint _size;
|
|
|
|
Monitor* _mon;
|
|
|
|
|
|
|
|
uint* _ids;
|
|
|
|
uint _hd;
|
|
|
|
uint _waiters;
|
|
|
|
uint _claimed;
|
|
|
|
|
|
|
|
public:
|
|
|
|
FreeIdSet(uint size, Monitor* mon);
|
|
|
|
~FreeIdSet();
|
|
|
|
|
|
|
|
// Returns an unclaimed parallel id (waiting for one to be released if
|
|
|
|
// necessary).
|
|
|
|
uint claim_par_id();
|
|
|
|
|
|
|
|
void release_par_id(uint id);
|
|
|
|
};
|
|
|
|
|
|
|
|
FreeIdSet::FreeIdSet(uint size, Monitor* mon) :
|
|
|
|
_size(size), _mon(mon), _hd(0), _waiters(0), _claimed(0)
|
|
|
|
{
|
|
|
|
guarantee(size != 0, "must be");
|
|
|
|
_ids = NEW_C_HEAP_ARRAY(uint, size, mtGC);
|
|
|
|
for (uint i = 0; i < size - 1; i++) {
|
|
|
|
_ids[i] = i+1;
|
|
|
|
}
|
|
|
|
_ids[size-1] = end_of_list; // end of list.
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeIdSet::~FreeIdSet() {
|
|
|
|
FREE_C_HEAP_ARRAY(uint, _ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint FreeIdSet::claim_par_id() {
|
|
|
|
MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
|
|
|
|
while (_hd == end_of_list) {
|
|
|
|
_waiters++;
|
|
|
|
_mon->wait(Mutex::_no_safepoint_check_flag);
|
|
|
|
_waiters--;
|
|
|
|
}
|
|
|
|
uint res = _hd;
|
|
|
|
_hd = _ids[res];
|
|
|
|
_ids[res] = claimed; // For debugging.
|
|
|
|
_claimed++;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeIdSet::release_par_id(uint id) {
|
|
|
|
MutexLockerEx x(_mon, Mutex::_no_safepoint_check_flag);
|
|
|
|
assert(_ids[id] == claimed, "Precondition.");
|
|
|
|
_ids[id] = _hd;
|
|
|
|
_hd = id;
|
|
|
|
_claimed--;
|
|
|
|
if (_waiters > 0) {
|
|
|
|
_mon->notify_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-04 13:09:57 -05:00
|
|
|
DirtyCardQueue::DirtyCardQueue(DirtyCardQueueSet* qset, bool permanent) :
|
|
|
|
// Dirty card queues are always active, so we create them with their
|
|
|
|
// active field set to true.
|
|
|
|
PtrQueue(qset, permanent, true /* active */)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
DirtyCardQueue::~DirtyCardQueue() {
|
|
|
|
if (!is_permanent()) {
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-16 15:12:51 -08:00
|
|
|
DirtyCardQueueSet::DirtyCardQueueSet(bool notify_when_complete) :
|
|
|
|
PtrQueueSet(notify_when_complete),
|
2014-04-16 16:46:58 +02:00
|
|
|
_mut_process_closure(NULL),
|
2015-11-04 13:09:57 -05:00
|
|
|
_shared_dirty_card_queue(this, true /* permanent */),
|
2008-06-05 15:57:56 -07:00
|
|
|
_free_ids(NULL),
|
|
|
|
_processed_buffers_mut(0), _processed_buffers_rs_thread(0)
|
|
|
|
{
|
|
|
|
_all_active = true;
|
|
|
|
}
|
|
|
|
|
2009-05-18 11:52:46 -07:00
|
|
|
// Determines how many mutator threads can process the buffers in parallel.
|
2014-04-03 17:49:31 +04:00
|
|
|
uint DirtyCardQueueSet::num_par_ids() {
|
2016-07-26 11:04:20 +02:00
|
|
|
return (uint)os::initial_active_processor_count();
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
|
|
|
|
2015-11-04 13:09:57 -05:00
|
|
|
void DirtyCardQueueSet::initialize(CardTableEntryClosure* cl,
|
|
|
|
Monitor* cbl_mon,
|
|
|
|
Mutex* fl_lock,
|
2009-12-16 15:12:51 -08:00
|
|
|
int process_completed_threshold,
|
2008-06-05 15:57:56 -07:00
|
|
|
int max_completed_queue,
|
2015-11-04 13:09:57 -05:00
|
|
|
Mutex* lock,
|
2016-01-08 15:41:44 -05:00
|
|
|
DirtyCardQueueSet* fl_owner,
|
|
|
|
bool init_free_ids) {
|
2014-04-16 16:46:58 +02:00
|
|
|
_mut_process_closure = cl;
|
2015-11-04 13:09:57 -05:00
|
|
|
PtrQueueSet::initialize(cbl_mon,
|
|
|
|
fl_lock,
|
|
|
|
process_completed_threshold,
|
|
|
|
max_completed_queue,
|
|
|
|
fl_owner);
|
2009-07-29 11:01:26 -04:00
|
|
|
set_buffer_size(G1UpdateBufferSize);
|
2008-06-05 15:57:56 -07:00
|
|
|
_shared_dirty_card_queue.set_lock(lock);
|
2016-01-08 15:41:44 -05:00
|
|
|
if (init_free_ids) {
|
|
|
|
_free_ids = new FreeIdSet(num_par_ids(), _cbl_mon);
|
|
|
|
}
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void DirtyCardQueueSet::handle_zero_index_for_thread(JavaThread* t) {
|
|
|
|
t->dirty_card_queue().handle_zero_index();
|
|
|
|
}
|
|
|
|
|
2016-03-16 00:28:33 -04:00
|
|
|
bool DirtyCardQueueSet::apply_closure_to_buffer(CardTableEntryClosure* cl,
|
|
|
|
BufferNode* node,
|
|
|
|
bool consume,
|
|
|
|
uint worker_i) {
|
|
|
|
if (cl == NULL) return true;
|
2016-03-25 15:50:31 -04:00
|
|
|
bool result = true;
|
2016-03-16 00:28:33 -04:00
|
|
|
void** buf = BufferNode::make_buffer_from_node(node);
|
|
|
|
size_t limit = DirtyCardQueue::byte_index_to_index(buffer_size());
|
2016-03-25 15:50:31 -04:00
|
|
|
size_t i = DirtyCardQueue::byte_index_to_index(node->index());
|
|
|
|
for ( ; i < limit; ++i) {
|
2016-03-16 00:28:33 -04:00
|
|
|
jbyte* card_ptr = static_cast<jbyte*>(buf[i]);
|
|
|
|
assert(card_ptr != NULL, "invariant");
|
|
|
|
if (!cl->do_card_ptr(card_ptr, worker_i)) {
|
2016-03-25 15:50:31 -04:00
|
|
|
result = false; // Incomplete processing.
|
|
|
|
break;
|
2016-03-16 00:28:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (consume) {
|
2016-03-25 15:50:31 -04:00
|
|
|
size_t new_index = DirtyCardQueue::index_to_byte_index(i);
|
|
|
|
assert(new_index <= buffer_size(), "invariant");
|
|
|
|
node->set_index(new_index);
|
2016-03-16 00:28:33 -04:00
|
|
|
}
|
2016-03-25 15:50:31 -04:00
|
|
|
return result;
|
2016-03-16 00:28:33 -04:00
|
|
|
}
|
|
|
|
|
2016-03-25 15:50:31 -04:00
|
|
|
#ifndef ASSERT
|
|
|
|
#define assert_fully_consumed(node, buffer_size)
|
|
|
|
#else
|
|
|
|
#define assert_fully_consumed(node, buffer_size) \
|
|
|
|
do { \
|
|
|
|
size_t _afc_index = (node)->index(); \
|
|
|
|
size_t _afc_size = (buffer_size); \
|
|
|
|
assert(_afc_index == _afc_size, \
|
|
|
|
"Buffer was not fully consumed as claimed: index: " \
|
|
|
|
SIZE_FORMAT ", size: " SIZE_FORMAT, \
|
|
|
|
_afc_index, _afc_size); \
|
|
|
|
} while (0)
|
|
|
|
#endif // ASSERT
|
|
|
|
|
2016-03-10 16:21:46 -05:00
|
|
|
bool DirtyCardQueueSet::mut_process_buffer(BufferNode* node) {
|
2016-01-08 15:41:44 -05:00
|
|
|
guarantee(_free_ids != NULL, "must be");
|
2008-06-05 15:57:56 -07:00
|
|
|
|
2016-03-25 15:50:31 -04:00
|
|
|
uint worker_i = _free_ids->claim_par_id(); // temporarily claim an id
|
|
|
|
bool result = apply_closure_to_buffer(_mut_process_closure, node, true, worker_i);
|
|
|
|
_free_ids->release_par_id(worker_i); // release the id
|
2008-06-05 15:57:56 -07:00
|
|
|
|
2016-03-25 15:50:31 -04:00
|
|
|
if (result) {
|
|
|
|
assert_fully_consumed(node, buffer_size());
|
2016-01-11 14:26:00 -05:00
|
|
|
Atomic::inc(&_processed_buffers_mut);
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
2016-03-25 15:50:31 -04:00
|
|
|
return result;
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
|
|
|
|
2009-12-16 15:12:51 -08:00
|
|
|
|
2016-02-26 14:02:39 -05:00
|
|
|
BufferNode* DirtyCardQueueSet::get_completed_buffer(size_t stop_at) {
|
2009-12-16 15:12:51 -08:00
|
|
|
BufferNode* nd = NULL;
|
2008-06-05 15:57:56 -07:00
|
|
|
MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
|
|
|
|
|
2016-02-26 14:02:39 -05:00
|
|
|
if (_n_completed_buffers <= stop_at) {
|
2008-06-05 15:57:56 -07:00
|
|
|
_process_completed = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_completed_buffers_head != NULL) {
|
|
|
|
nd = _completed_buffers_head;
|
2016-02-26 14:02:39 -05:00
|
|
|
assert(_n_completed_buffers > 0, "Invariant");
|
2009-12-16 15:12:51 -08:00
|
|
|
_completed_buffers_head = nd->next();
|
2008-06-05 15:57:56 -07:00
|
|
|
_n_completed_buffers--;
|
2016-02-26 14:02:39 -05:00
|
|
|
if (_completed_buffers_head == NULL) {
|
|
|
|
assert(_n_completed_buffers == 0, "Invariant");
|
|
|
|
_completed_buffers_tail = NULL;
|
|
|
|
}
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
2015-11-04 13:09:57 -05:00
|
|
|
DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked());
|
2008-06-05 15:57:56 -07:00
|
|
|
return nd;
|
|
|
|
}
|
|
|
|
|
2016-02-17 16:00:27 -05:00
|
|
|
bool DirtyCardQueueSet::apply_closure_to_completed_buffer(CardTableEntryClosure* cl,
|
|
|
|
uint worker_i,
|
2016-02-26 14:02:39 -05:00
|
|
|
size_t stop_at,
|
2016-02-17 16:00:27 -05:00
|
|
|
bool during_pause) {
|
|
|
|
assert(!during_pause || stop_at == 0, "Should not leave any completed buffers during a pause");
|
|
|
|
BufferNode* nd = get_completed_buffer(stop_at);
|
|
|
|
if (nd == NULL) {
|
|
|
|
return false;
|
|
|
|
} else {
|
2016-03-16 00:28:33 -04:00
|
|
|
if (apply_closure_to_buffer(cl, nd, true, worker_i)) {
|
2016-03-25 15:50:31 -04:00
|
|
|
assert_fully_consumed(nd, buffer_size());
|
2016-02-17 16:00:27 -05:00
|
|
|
// Done with fully processed buffer.
|
2016-03-10 16:21:46 -05:00
|
|
|
deallocate_buffer(nd);
|
2016-02-17 16:00:27 -05:00
|
|
|
Atomic::inc(&_processed_buffers_rs_thread);
|
2008-06-05 15:57:56 -07:00
|
|
|
} else {
|
2016-02-17 16:00:27 -05:00
|
|
|
// Return partially processed buffer to the queue.
|
2016-03-25 15:50:31 -04:00
|
|
|
guarantee(!during_pause, "Should never stop early");
|
2016-03-10 16:21:46 -05:00
|
|
|
enqueue_complete_buffer(nd);
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
2016-03-25 15:50:31 -04:00
|
|
|
return true;
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-16 16:46:58 +02:00
|
|
|
void DirtyCardQueueSet::par_apply_closure_to_all_completed_buffers(CardTableEntryClosure* cl) {
|
|
|
|
BufferNode* nd = _cur_par_buffer_node;
|
|
|
|
while (nd != NULL) {
|
2016-03-10 16:21:46 -05:00
|
|
|
BufferNode* next = nd->next();
|
|
|
|
void* actual = Atomic::cmpxchg_ptr(next, &_cur_par_buffer_node, nd);
|
2014-04-16 16:46:58 +02:00
|
|
|
if (actual == nd) {
|
2016-03-16 00:28:33 -04:00
|
|
|
bool b = apply_closure_to_buffer(cl, nd, false);
|
2014-04-16 16:46:58 +02:00
|
|
|
guarantee(b, "Should not stop early.");
|
|
|
|
nd = next;
|
|
|
|
} else {
|
2016-03-10 16:21:46 -05:00
|
|
|
nd = static_cast<BufferNode*>(actual);
|
2014-04-16 16:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-02 12:51:43 -07:00
|
|
|
// Deallocates any completed log buffers
|
|
|
|
void DirtyCardQueueSet::clear() {
|
2009-12-16 15:12:51 -08:00
|
|
|
BufferNode* buffers_to_delete = NULL;
|
2008-06-05 15:57:56 -07:00
|
|
|
{
|
|
|
|
MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
|
|
|
|
while (_completed_buffers_head != NULL) {
|
2009-12-16 15:12:51 -08:00
|
|
|
BufferNode* nd = _completed_buffers_head;
|
|
|
|
_completed_buffers_head = nd->next();
|
|
|
|
nd->set_next(buffers_to_delete);
|
2008-06-05 15:57:56 -07:00
|
|
|
buffers_to_delete = nd;
|
|
|
|
}
|
|
|
|
_n_completed_buffers = 0;
|
|
|
|
_completed_buffers_tail = NULL;
|
2015-11-04 13:09:57 -05:00
|
|
|
DEBUG_ONLY(assert_completed_buffer_list_len_correct_locked());
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
|
|
|
while (buffers_to_delete != NULL) {
|
2009-12-16 15:12:51 -08:00
|
|
|
BufferNode* nd = buffers_to_delete;
|
|
|
|
buffers_to_delete = nd->next();
|
2016-03-10 16:21:46 -05:00
|
|
|
deallocate_buffer(nd);
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
2010-08-02 12:51:43 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirtyCardQueueSet::abandon_logs() {
|
|
|
|
assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
|
|
|
|
clear();
|
2008-06-05 15:57:56 -07:00
|
|
|
// Since abandon is done only at safepoints, we can safely manipulate
|
|
|
|
// these queues.
|
|
|
|
for (JavaThread* t = Threads::first(); t; t = t->next()) {
|
|
|
|
t->dirty_card_queue().reset();
|
|
|
|
}
|
|
|
|
shared_dirty_card_queue()->reset();
|
|
|
|
}
|
|
|
|
|
2016-03-10 16:21:46 -05:00
|
|
|
void DirtyCardQueueSet::concatenate_log(DirtyCardQueue& dcq) {
|
|
|
|
if (!dcq.is_empty()) {
|
|
|
|
enqueue_complete_buffer(
|
|
|
|
BufferNode::make_node_from_buffer(dcq.get_buf(), dcq.get_index()));
|
|
|
|
dcq.reinitialize();
|
|
|
|
}
|
|
|
|
}
|
2008-06-05 15:57:56 -07:00
|
|
|
|
|
|
|
void DirtyCardQueueSet::concatenate_logs() {
|
|
|
|
// Iterate over all the threads, if we find a partial log add it to
|
|
|
|
// the global list of logs. Temporarily turn off the limit on the number
|
|
|
|
// of outstanding buffers.
|
|
|
|
int save_max_completed_queue = _max_completed_queue;
|
|
|
|
_max_completed_queue = max_jint;
|
|
|
|
assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
|
|
|
|
for (JavaThread* t = Threads::first(); t; t = t->next()) {
|
2016-03-10 16:21:46 -05:00
|
|
|
concatenate_log(t->dirty_card_queue());
|
2008-06-05 15:57:56 -07:00
|
|
|
}
|
2016-03-10 16:21:46 -05:00
|
|
|
concatenate_log(_shared_dirty_card_queue);
|
2008-06-05 15:57:56 -07:00
|
|
|
// Restore the completed buffer queue limit.
|
|
|
|
_max_completed_queue = save_max_completed_queue;
|
|
|
|
}
|