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

Mon, 03 Aug 2009 12:59:30 -0700

author
johnc
date
Mon, 03 Aug 2009 12:59:30 -0700
changeset 1324
15c5903cf9e1
parent 1320
7f807f55161a
child 1525
fa357420e7d2
permissions
-rw-r--r--

6865703: G1: Parallelize hot card cache cleanup
Summary: Have the GC worker threads clear the hot card cache in parallel by having each worker thread claim a chunk of the card cache and process the cards in that chunk. The size of the chunks that each thread will claim is determined at VM initialization from the size of the card cache and the number of worker threads.
Reviewed-by: jmasa, tonyp

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

mercurial