annotate src/share/vm/gc_implementation/g1/dirtyCardQueue.cpp @ 342:37f87013dfd8

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