src/share/vm/gc_implementation/g1/dirtyCardQueue.cpp

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7535
7ae4e26cb1e0
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "gc_implementation/g1/dirtyCardQueue.hpp"
aoqi@0 27 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
aoqi@0 28 #include "gc_implementation/g1/heapRegionRemSet.hpp"
aoqi@0 29 #include "runtime/atomic.hpp"
aoqi@0 30 #include "runtime/mutexLocker.hpp"
aoqi@0 31 #include "runtime/safepoint.hpp"
aoqi@0 32 #include "runtime/thread.inline.hpp"
aoqi@0 33 #include "utilities/workgroup.hpp"
aoqi@0 34
aoqi@0 35 bool DirtyCardQueue::apply_closure(CardTableEntryClosure* cl,
aoqi@0 36 bool consume,
aoqi@0 37 uint worker_i) {
aoqi@0 38 bool res = true;
aoqi@0 39 if (_buf != NULL) {
aoqi@0 40 res = apply_closure_to_buffer(cl, _buf, _index, _sz,
aoqi@0 41 consume,
aoqi@0 42 worker_i);
aoqi@0 43 if (res && consume) _index = _sz;
aoqi@0 44 }
aoqi@0 45 return res;
aoqi@0 46 }
aoqi@0 47
aoqi@0 48 bool DirtyCardQueue::apply_closure_to_buffer(CardTableEntryClosure* cl,
aoqi@0 49 void** buf,
aoqi@0 50 size_t index, size_t sz,
aoqi@0 51 bool consume,
aoqi@0 52 uint worker_i) {
aoqi@0 53 if (cl == NULL) return true;
aoqi@0 54 for (size_t i = index; i < sz; i += oopSize) {
aoqi@0 55 int ind = byte_index_to_index((int)i);
aoqi@0 56 jbyte* card_ptr = (jbyte*)buf[ind];
aoqi@0 57 if (card_ptr != NULL) {
aoqi@0 58 // Set the entry to null, so we don't do it again (via the test
aoqi@0 59 // above) if we reconsider this buffer.
aoqi@0 60 if (consume) buf[ind] = NULL;
aoqi@0 61 if (!cl->do_card_ptr(card_ptr, worker_i)) return false;
aoqi@0 62 }
aoqi@0 63 }
aoqi@0 64 return true;
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 #ifdef _MSC_VER // the use of 'this' below gets a warning, make it go away
aoqi@0 68 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
aoqi@0 69 #endif // _MSC_VER
aoqi@0 70
aoqi@0 71 DirtyCardQueueSet::DirtyCardQueueSet(bool notify_when_complete) :
aoqi@0 72 PtrQueueSet(notify_when_complete),
tschatzl@6930 73 _mut_process_closure(NULL),
aoqi@0 74 _shared_dirty_card_queue(this, true /*perm*/),
aoqi@0 75 _free_ids(NULL),
aoqi@0 76 _processed_buffers_mut(0), _processed_buffers_rs_thread(0)
aoqi@0 77 {
aoqi@0 78 _all_active = true;
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 // Determines how many mutator threads can process the buffers in parallel.
aoqi@0 82 uint DirtyCardQueueSet::num_par_ids() {
aoqi@0 83 return (uint)os::processor_count();
aoqi@0 84 }
aoqi@0 85
tschatzl@6930 86 void DirtyCardQueueSet::initialize(CardTableEntryClosure* cl, Monitor* cbl_mon, Mutex* fl_lock,
aoqi@0 87 int process_completed_threshold,
aoqi@0 88 int max_completed_queue,
aoqi@0 89 Mutex* lock, PtrQueueSet* fl_owner) {
tschatzl@6930 90 _mut_process_closure = cl;
aoqi@0 91 PtrQueueSet::initialize(cbl_mon, fl_lock, process_completed_threshold,
aoqi@0 92 max_completed_queue, fl_owner);
aoqi@0 93 set_buffer_size(G1UpdateBufferSize);
aoqi@0 94 _shared_dirty_card_queue.set_lock(lock);
aoqi@0 95 _free_ids = new FreeIdSet((int) num_par_ids(), _cbl_mon);
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 void DirtyCardQueueSet::handle_zero_index_for_thread(JavaThread* t) {
aoqi@0 99 t->dirty_card_queue().handle_zero_index();
aoqi@0 100 }
aoqi@0 101
tschatzl@6930 102 void DirtyCardQueueSet::iterate_closure_all_threads(CardTableEntryClosure* cl,
tschatzl@6930 103 bool consume,
aoqi@0 104 uint worker_i) {
aoqi@0 105 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
aoqi@0 106 for(JavaThread* t = Threads::first(); t; t = t->next()) {
tschatzl@6930 107 bool b = t->dirty_card_queue().apply_closure(cl, consume);
aoqi@0 108 guarantee(b, "Should not be interrupted.");
aoqi@0 109 }
tschatzl@6930 110 bool b = shared_dirty_card_queue()->apply_closure(cl,
aoqi@0 111 consume,
aoqi@0 112 worker_i);
aoqi@0 113 guarantee(b, "Should not be interrupted.");
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 bool DirtyCardQueueSet::mut_process_buffer(void** buf) {
aoqi@0 117
aoqi@0 118 // Used to determine if we had already claimed a par_id
aoqi@0 119 // before entering this method.
aoqi@0 120 bool already_claimed = false;
aoqi@0 121
aoqi@0 122 // We grab the current JavaThread.
aoqi@0 123 JavaThread* thread = JavaThread::current();
aoqi@0 124
aoqi@0 125 // We get the the number of any par_id that this thread
aoqi@0 126 // might have already claimed.
aoqi@0 127 uint worker_i = thread->get_claimed_par_id();
aoqi@0 128
aoqi@0 129 // If worker_i is not UINT_MAX then the thread has already claimed
aoqi@0 130 // a par_id. We make note of it using the already_claimed value
aoqi@0 131 if (worker_i != UINT_MAX) {
aoqi@0 132 already_claimed = true;
aoqi@0 133 } else {
aoqi@0 134
aoqi@0 135 // Otherwise we need to claim a par id
aoqi@0 136 worker_i = _free_ids->claim_par_id();
aoqi@0 137
aoqi@0 138 // And store the par_id value in the thread
aoqi@0 139 thread->set_claimed_par_id(worker_i);
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 bool b = false;
aoqi@0 143 if (worker_i != UINT_MAX) {
tschatzl@6930 144 b = DirtyCardQueue::apply_closure_to_buffer(_mut_process_closure, buf, 0,
aoqi@0 145 _sz, true, worker_i);
aoqi@0 146 if (b) Atomic::inc(&_processed_buffers_mut);
aoqi@0 147
aoqi@0 148 // If we had not claimed an id before entering the method
aoqi@0 149 // then we must release the id.
aoqi@0 150 if (!already_claimed) {
aoqi@0 151
aoqi@0 152 // we release the id
aoqi@0 153 _free_ids->release_par_id(worker_i);
aoqi@0 154
aoqi@0 155 // and set the claimed_id in the thread to UINT_MAX
aoqi@0 156 thread->set_claimed_par_id(UINT_MAX);
aoqi@0 157 }
aoqi@0 158 }
aoqi@0 159 return b;
aoqi@0 160 }
aoqi@0 161
aoqi@0 162
aoqi@0 163 BufferNode*
aoqi@0 164 DirtyCardQueueSet::get_completed_buffer(int stop_at) {
aoqi@0 165 BufferNode* nd = NULL;
aoqi@0 166 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
aoqi@0 167
aoqi@0 168 if ((int)_n_completed_buffers <= stop_at) {
aoqi@0 169 _process_completed = false;
aoqi@0 170 return NULL;
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 if (_completed_buffers_head != NULL) {
aoqi@0 174 nd = _completed_buffers_head;
aoqi@0 175 _completed_buffers_head = nd->next();
aoqi@0 176 if (_completed_buffers_head == NULL)
aoqi@0 177 _completed_buffers_tail = NULL;
aoqi@0 178 _n_completed_buffers--;
aoqi@0 179 assert(_n_completed_buffers >= 0, "Invariant");
aoqi@0 180 }
aoqi@0 181 debug_only(assert_completed_buffer_list_len_correct_locked());
aoqi@0 182 return nd;
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 bool DirtyCardQueueSet::
aoqi@0 186 apply_closure_to_completed_buffer_helper(CardTableEntryClosure* cl,
aoqi@0 187 uint worker_i,
aoqi@0 188 BufferNode* nd) {
aoqi@0 189 if (nd != NULL) {
aoqi@0 190 void **buf = BufferNode::make_buffer_from_node(nd);
aoqi@0 191 size_t index = nd->index();
aoqi@0 192 bool b =
aoqi@0 193 DirtyCardQueue::apply_closure_to_buffer(cl, buf,
aoqi@0 194 index, _sz,
aoqi@0 195 true, worker_i);
aoqi@0 196 if (b) {
aoqi@0 197 deallocate_buffer(buf);
aoqi@0 198 return true; // In normal case, go on to next buffer.
aoqi@0 199 } else {
aoqi@0 200 enqueue_complete_buffer(buf, index);
aoqi@0 201 return false;
aoqi@0 202 }
aoqi@0 203 } else {
aoqi@0 204 return false;
aoqi@0 205 }
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 bool DirtyCardQueueSet::apply_closure_to_completed_buffer(CardTableEntryClosure* cl,
aoqi@0 209 uint worker_i,
aoqi@0 210 int stop_at,
aoqi@0 211 bool during_pause) {
aoqi@0 212 assert(!during_pause || stop_at == 0, "Should not leave any completed buffers during a pause");
aoqi@0 213 BufferNode* nd = get_completed_buffer(stop_at);
aoqi@0 214 bool res = apply_closure_to_completed_buffer_helper(cl, worker_i, nd);
aoqi@0 215 if (res) Atomic::inc(&_processed_buffers_rs_thread);
aoqi@0 216 return res;
aoqi@0 217 }
aoqi@0 218
tschatzl@6930 219 void DirtyCardQueueSet::apply_closure_to_all_completed_buffers(CardTableEntryClosure* cl) {
aoqi@0 220 BufferNode* nd = _completed_buffers_head;
aoqi@0 221 while (nd != NULL) {
aoqi@0 222 bool b =
tschatzl@6930 223 DirtyCardQueue::apply_closure_to_buffer(cl,
aoqi@0 224 BufferNode::make_buffer_from_node(nd),
aoqi@0 225 0, _sz, false);
aoqi@0 226 guarantee(b, "Should not stop early.");
aoqi@0 227 nd = nd->next();
aoqi@0 228 }
aoqi@0 229 }
aoqi@0 230
tschatzl@6930 231 void DirtyCardQueueSet::par_apply_closure_to_all_completed_buffers(CardTableEntryClosure* cl) {
tschatzl@6930 232 BufferNode* nd = _cur_par_buffer_node;
tschatzl@6930 233 while (nd != NULL) {
tschatzl@6930 234 BufferNode* next = (BufferNode*)nd->next();
tschatzl@6930 235 BufferNode* actual = (BufferNode*)Atomic::cmpxchg_ptr((void*)next, (volatile void*)&_cur_par_buffer_node, (void*)nd);
tschatzl@6930 236 if (actual == nd) {
tschatzl@6930 237 bool b =
tschatzl@6930 238 DirtyCardQueue::apply_closure_to_buffer(cl,
tschatzl@6930 239 BufferNode::make_buffer_from_node(actual),
tschatzl@6930 240 0, _sz, false);
tschatzl@6930 241 guarantee(b, "Should not stop early.");
tschatzl@6930 242 nd = next;
tschatzl@6930 243 } else {
tschatzl@6930 244 nd = actual;
tschatzl@6930 245 }
tschatzl@6930 246 }
tschatzl@6930 247 }
tschatzl@6930 248
aoqi@0 249 // Deallocates any completed log buffers
aoqi@0 250 void DirtyCardQueueSet::clear() {
aoqi@0 251 BufferNode* buffers_to_delete = NULL;
aoqi@0 252 {
aoqi@0 253 MutexLockerEx x(_cbl_mon, Mutex::_no_safepoint_check_flag);
aoqi@0 254 while (_completed_buffers_head != NULL) {
aoqi@0 255 BufferNode* nd = _completed_buffers_head;
aoqi@0 256 _completed_buffers_head = nd->next();
aoqi@0 257 nd->set_next(buffers_to_delete);
aoqi@0 258 buffers_to_delete = nd;
aoqi@0 259 }
aoqi@0 260 _n_completed_buffers = 0;
aoqi@0 261 _completed_buffers_tail = NULL;
aoqi@0 262 debug_only(assert_completed_buffer_list_len_correct_locked());
aoqi@0 263 }
aoqi@0 264 while (buffers_to_delete != NULL) {
aoqi@0 265 BufferNode* nd = buffers_to_delete;
aoqi@0 266 buffers_to_delete = nd->next();
aoqi@0 267 deallocate_buffer(BufferNode::make_buffer_from_node(nd));
aoqi@0 268 }
aoqi@0 269
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 void DirtyCardQueueSet::abandon_logs() {
aoqi@0 273 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
aoqi@0 274 clear();
aoqi@0 275 // Since abandon is done only at safepoints, we can safely manipulate
aoqi@0 276 // these queues.
aoqi@0 277 for (JavaThread* t = Threads::first(); t; t = t->next()) {
aoqi@0 278 t->dirty_card_queue().reset();
aoqi@0 279 }
aoqi@0 280 shared_dirty_card_queue()->reset();
aoqi@0 281 }
aoqi@0 282
aoqi@0 283
aoqi@0 284 void DirtyCardQueueSet::concatenate_logs() {
aoqi@0 285 // Iterate over all the threads, if we find a partial log add it to
aoqi@0 286 // the global list of logs. Temporarily turn off the limit on the number
aoqi@0 287 // of outstanding buffers.
aoqi@0 288 int save_max_completed_queue = _max_completed_queue;
aoqi@0 289 _max_completed_queue = max_jint;
aoqi@0 290 assert(SafepointSynchronize::is_at_safepoint(), "Must be at safepoint.");
aoqi@0 291 for (JavaThread* t = Threads::first(); t; t = t->next()) {
aoqi@0 292 DirtyCardQueue& dcq = t->dirty_card_queue();
aoqi@0 293 if (dcq.size() != 0) {
aoqi@0 294 void **buf = t->dirty_card_queue().get_buf();
aoqi@0 295 // We must NULL out the unused entries, then enqueue.
aoqi@0 296 for (size_t i = 0; i < t->dirty_card_queue().get_index(); i += oopSize) {
aoqi@0 297 buf[PtrQueue::byte_index_to_index((int)i)] = NULL;
aoqi@0 298 }
aoqi@0 299 enqueue_complete_buffer(dcq.get_buf(), dcq.get_index());
aoqi@0 300 dcq.reinitialize();
aoqi@0 301 }
aoqi@0 302 }
aoqi@0 303 if (_shared_dirty_card_queue.size() != 0) {
aoqi@0 304 enqueue_complete_buffer(_shared_dirty_card_queue.get_buf(),
aoqi@0 305 _shared_dirty_card_queue.get_index());
aoqi@0 306 _shared_dirty_card_queue.reinitialize();
aoqi@0 307 }
aoqi@0 308 // Restore the completed buffer queue limit.
aoqi@0 309 _max_completed_queue = save_max_completed_queue;
aoqi@0 310 }

mercurial