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

Wed, 30 Sep 2009 14:50:51 -0400

author
tonyp
date
Wed, 30 Sep 2009 14:50:51 -0400
changeset 1479
6270f80a7331
parent 1454
035d2e036a9b
child 1546
44f61c24ddab
permissions
-rw-r--r--

6890137: G1: revamp reachable object dump
Summary: Revamp the reachable object dump debugging facility.
Reviewed-by: jmasa, apetrusenko

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/_concurrentG1Refine.cpp.incl"
ysr@777 27
johnc@1325 28 // Possible sizes for the card counts cache: odd primes that roughly double in size.
johnc@1325 29 // (See jvmtiTagMap.cpp).
johnc@1325 30 int ConcurrentG1Refine::_cc_cache_sizes[] = {
johnc@1325 31 16381, 32771, 76831, 150001, 307261,
johnc@1325 32 614563, 1228891, 2457733, 4915219, 9830479,
johnc@1325 33 19660831, 39321619, 78643219, 157286461, -1
johnc@1325 34 };
johnc@1325 35
ysr@777 36 ConcurrentG1Refine::ConcurrentG1Refine() :
johnc@1325 37 _card_counts(NULL), _card_epochs(NULL),
johnc@1325 38 _n_card_counts(0), _max_n_card_counts(0),
johnc@1325 39 _cache_size_index(0), _expand_card_counts(false),
ysr@777 40 _hot_cache(NULL),
ysr@777 41 _def_use_cache(false), _use_cache(false),
johnc@1325 42 _n_periods(0),
iveresov@1229 43 _threads(NULL), _n_threads(0)
ysr@777 44 {
ysr@777 45 if (G1ConcRefine) {
iveresov@1230 46 _n_threads = (int)thread_num();
iveresov@1229 47 if (_n_threads > 0) {
iveresov@1229 48 _threads = NEW_C_HEAP_ARRAY(ConcurrentG1RefineThread*, _n_threads);
iveresov@1230 49 int worker_id_offset = (int)DirtyCardQueueSet::num_par_ids();
iveresov@1229 50 ConcurrentG1RefineThread *next = NULL;
iveresov@1229 51 for (int i = _n_threads - 1; i >= 0; i--) {
iveresov@1230 52 ConcurrentG1RefineThread* t = new ConcurrentG1RefineThread(this, next, worker_id_offset, i);
iveresov@1229 53 assert(t != NULL, "Conc refine should have been created");
iveresov@1229 54 assert(t->cg1r() == this, "Conc refine thread should refer to this");
iveresov@1229 55 _threads[i] = t;
iveresov@1229 56 next = t;
iveresov@1229 57 }
iveresov@1229 58 }
ysr@777 59 }
ysr@777 60 }
ysr@777 61
iveresov@1230 62 size_t ConcurrentG1Refine::thread_num() {
iveresov@1230 63 if (G1ConcRefine) {
iveresov@1230 64 return (G1ParallelRSetThreads > 0) ? G1ParallelRSetThreads : ParallelGCThreads;
iveresov@1230 65 }
iveresov@1230 66 return 0;
iveresov@1230 67 }
iveresov@1230 68
ysr@777 69 void ConcurrentG1Refine::init() {
johnc@1325 70 if (G1ConcRSLogCacheSize > 0) {
johnc@1325 71 _g1h = G1CollectedHeap::heap();
johnc@1325 72 _max_n_card_counts =
johnc@1325 73 (unsigned) (_g1h->g1_reserved_obj_bytes() >> CardTableModRefBS::card_shift);
johnc@1325 74
johnc@1325 75 size_t max_card_num = ((size_t)1 << (sizeof(unsigned)*BitsPerByte-1)) - 1;
johnc@1325 76 guarantee(_max_n_card_counts < max_card_num, "card_num representation");
johnc@1325 77
johnc@1325 78 int desired = _max_n_card_counts / InitialCacheFraction;
johnc@1325 79 for (_cache_size_index = 0;
johnc@1325 80 _cc_cache_sizes[_cache_size_index] >= 0; _cache_size_index++) {
johnc@1325 81 if (_cc_cache_sizes[_cache_size_index] >= desired) break;
johnc@1325 82 }
johnc@1325 83 _cache_size_index = MAX2(0, (_cache_size_index - 1));
johnc@1325 84
johnc@1325 85 int initial_size = _cc_cache_sizes[_cache_size_index];
johnc@1325 86 if (initial_size < 0) initial_size = _max_n_card_counts;
johnc@1325 87
johnc@1325 88 // Make sure we don't go bigger than we will ever need
johnc@1325 89 _n_card_counts = MIN2((unsigned) initial_size, _max_n_card_counts);
johnc@1325 90
johnc@1325 91 _card_counts = NEW_C_HEAP_ARRAY(CardCountCacheEntry, _n_card_counts);
johnc@1325 92 _card_epochs = NEW_C_HEAP_ARRAY(CardEpochCacheEntry, _n_card_counts);
johnc@1325 93
johnc@1325 94 Copy::fill_to_bytes(&_card_counts[0],
johnc@1325 95 _n_card_counts * sizeof(CardCountCacheEntry));
johnc@1325 96 Copy::fill_to_bytes(&_card_epochs[0], _n_card_counts * sizeof(CardEpochCacheEntry));
johnc@1325 97
johnc@1325 98 ModRefBarrierSet* bs = _g1h->mr_bs();
ysr@777 99 guarantee(bs->is_a(BarrierSet::CardTableModRef), "Precondition");
johnc@1325 100 _ct_bs = (CardTableModRefBS*)bs;
johnc@1325 101 _ct_bot = _ct_bs->byte_for_const(_g1h->reserved_region().start());
johnc@1325 102
ysr@777 103 _def_use_cache = true;
ysr@777 104 _use_cache = true;
ysr@777 105 _hot_cache_size = (1 << G1ConcRSLogCacheSize);
ysr@777 106 _hot_cache = NEW_C_HEAP_ARRAY(jbyte*, _hot_cache_size);
ysr@777 107 _n_hot = 0;
ysr@777 108 _hot_cache_idx = 0;
johnc@1324 109
johnc@1324 110 // For refining the cards in the hot cache in parallel
johnc@1324 111 int n_workers = (ParallelGCThreads > 0 ?
johnc@1325 112 _g1h->workers()->total_workers() : 1);
johnc@1324 113 _hot_cache_par_chunk_size = MAX2(1, _hot_cache_size / n_workers);
johnc@1324 114 _hot_cache_par_claimed_idx = 0;
ysr@777 115 }
ysr@777 116 }
ysr@777 117
iveresov@1229 118 void ConcurrentG1Refine::stop() {
iveresov@1229 119 if (_threads != NULL) {
iveresov@1229 120 for (int i = 0; i < _n_threads; i++) {
iveresov@1229 121 _threads[i]->stop();
iveresov@1229 122 }
iveresov@1229 123 }
iveresov@1229 124 }
iveresov@1229 125
ysr@777 126 ConcurrentG1Refine::~ConcurrentG1Refine() {
johnc@1325 127 if (G1ConcRSLogCacheSize > 0) {
ysr@777 128 assert(_card_counts != NULL, "Logic");
johnc@1325 129 FREE_C_HEAP_ARRAY(CardCountCacheEntry, _card_counts);
johnc@1325 130 assert(_card_epochs != NULL, "Logic");
johnc@1325 131 FREE_C_HEAP_ARRAY(CardEpochCacheEntry, _card_epochs);
ysr@777 132 assert(_hot_cache != NULL, "Logic");
ysr@777 133 FREE_C_HEAP_ARRAY(jbyte*, _hot_cache);
ysr@777 134 }
iveresov@1229 135 if (_threads != NULL) {
iveresov@1229 136 for (int i = 0; i < _n_threads; i++) {
iveresov@1229 137 delete _threads[i];
iveresov@1229 138 }
iveresov@1234 139 FREE_C_HEAP_ARRAY(ConcurrentG1RefineThread*, _threads);
ysr@777 140 }
ysr@777 141 }
ysr@777 142
iveresov@1229 143 void ConcurrentG1Refine::threads_do(ThreadClosure *tc) {
iveresov@1229 144 if (_threads != NULL) {
iveresov@1229 145 for (int i = 0; i < _n_threads; i++) {
iveresov@1229 146 tc->do_thread(_threads[i]);
iveresov@1229 147 }
ysr@777 148 }
ysr@777 149 }
ysr@777 150
johnc@1325 151 bool ConcurrentG1Refine::is_young_card(jbyte* card_ptr) {
johnc@1325 152 HeapWord* start = _ct_bs->addr_for(card_ptr);
johnc@1325 153 HeapRegion* r = _g1h->heap_region_containing(start);
johnc@1325 154 if (r != NULL && r->is_young()) {
johnc@1325 155 return true;
johnc@1325 156 }
johnc@1325 157 // This card is not associated with a heap region
johnc@1325 158 // so can't be young.
johnc@1325 159 return false;
ysr@777 160 }
ysr@777 161
johnc@1325 162 jbyte* ConcurrentG1Refine::add_card_count(jbyte* card_ptr, int* count, bool* defer) {
johnc@1325 163 unsigned new_card_num = ptr_2_card_num(card_ptr);
johnc@1325 164 unsigned bucket = hash(new_card_num);
johnc@1325 165 assert(0 <= bucket && bucket < _n_card_counts, "Bounds");
johnc@1325 166
johnc@1325 167 CardCountCacheEntry* count_ptr = &_card_counts[bucket];
johnc@1325 168 CardEpochCacheEntry* epoch_ptr = &_card_epochs[bucket];
johnc@1325 169
johnc@1325 170 // We have to construct a new entry if we haven't updated the counts
johnc@1325 171 // during the current period, or if the count was updated for a
johnc@1325 172 // different card number.
johnc@1325 173 unsigned int new_epoch = (unsigned int) _n_periods;
johnc@1325 174 julong new_epoch_entry = make_epoch_entry(new_card_num, new_epoch);
johnc@1325 175
johnc@1325 176 while (true) {
johnc@1325 177 // Fetch the previous epoch value
johnc@1325 178 julong prev_epoch_entry = epoch_ptr->_value;
johnc@1325 179 julong cas_res;
johnc@1325 180
johnc@1325 181 if (extract_epoch(prev_epoch_entry) != new_epoch) {
johnc@1325 182 // This entry has not yet been updated during this period.
johnc@1325 183 // Note: we update the epoch value atomically to ensure
johnc@1325 184 // that there is only one winner that updates the cached
johnc@1325 185 // card_ptr value even though all the refine threads share
johnc@1325 186 // the same epoch value.
johnc@1325 187
johnc@1325 188 cas_res = (julong) Atomic::cmpxchg((jlong) new_epoch_entry,
johnc@1325 189 (volatile jlong*)&epoch_ptr->_value,
johnc@1325 190 (jlong) prev_epoch_entry);
johnc@1325 191
johnc@1325 192 if (cas_res == prev_epoch_entry) {
johnc@1325 193 // We have successfully won the race to update the
johnc@1325 194 // epoch and card_num value. Make it look like the
johnc@1325 195 // count and eviction count were previously cleared.
johnc@1325 196 count_ptr->_count = 1;
johnc@1325 197 count_ptr->_evict_count = 0;
johnc@1325 198 *count = 0;
johnc@1325 199 // We can defer the processing of card_ptr
johnc@1325 200 *defer = true;
johnc@1325 201 return card_ptr;
johnc@1325 202 }
johnc@1325 203 // We did not win the race to update the epoch field, so some other
johnc@1325 204 // thread must have done it. The value that gets returned by CAS
johnc@1325 205 // should be the new epoch value.
johnc@1325 206 assert(extract_epoch(cas_res) == new_epoch, "unexpected epoch");
johnc@1325 207 // We could 'continue' here or just re-read the previous epoch value
johnc@1325 208 prev_epoch_entry = epoch_ptr->_value;
johnc@1325 209 }
johnc@1325 210
johnc@1325 211 // The epoch entry for card_ptr has been updated during this period.
johnc@1325 212 unsigned old_card_num = extract_card_num(prev_epoch_entry);
johnc@1325 213
johnc@1325 214 // The card count that will be returned to caller
johnc@1325 215 *count = count_ptr->_count;
johnc@1325 216
johnc@1325 217 // Are we updating the count for the same card?
johnc@1325 218 if (new_card_num == old_card_num) {
johnc@1325 219 // Same card - just update the count. We could have more than one
johnc@1325 220 // thread racing to update count for the current card. It should be
johnc@1325 221 // OK not to use a CAS as the only penalty should be some missed
johnc@1325 222 // increments of the count which delays identifying the card as "hot".
johnc@1325 223
johnc@1325 224 if (*count < max_jubyte) count_ptr->_count++;
johnc@1325 225 // We can defer the processing of card_ptr
johnc@1325 226 *defer = true;
johnc@1325 227 return card_ptr;
johnc@1325 228 }
johnc@1325 229
johnc@1325 230 // Different card - evict old card info
johnc@1325 231 if (count_ptr->_evict_count < max_jubyte) count_ptr->_evict_count++;
johnc@1325 232 if (count_ptr->_evict_count > G1CardCountCacheExpandThreshold) {
johnc@1325 233 // Trigger a resize the next time we clear
johnc@1325 234 _expand_card_counts = true;
johnc@1325 235 }
johnc@1325 236
johnc@1325 237 cas_res = (julong) Atomic::cmpxchg((jlong) new_epoch_entry,
johnc@1325 238 (volatile jlong*)&epoch_ptr->_value,
johnc@1325 239 (jlong) prev_epoch_entry);
johnc@1325 240
johnc@1325 241 if (cas_res == prev_epoch_entry) {
johnc@1325 242 // We successfully updated the card num value in the epoch entry
johnc@1325 243 count_ptr->_count = 0; // initialize counter for new card num
johnc@1325 244
johnc@1325 245 // Even though the region containg the card at old_card_num was not
johnc@1325 246 // in the young list when old_card_num was recorded in the epoch
johnc@1325 247 // cache it could have been added to the free list and subsequently
johnc@1325 248 // added to the young list in the intervening time. If the evicted
johnc@1325 249 // card is in a young region just return the card_ptr and the evicted
johnc@1325 250 // card will not be cleaned. See CR 6817995.
johnc@1325 251
johnc@1325 252 jbyte* old_card_ptr = card_num_2_ptr(old_card_num);
johnc@1325 253 if (is_young_card(old_card_ptr)) {
johnc@1325 254 *count = 0;
johnc@1325 255 // We can defer the processing of card_ptr
johnc@1325 256 *defer = true;
johnc@1325 257 return card_ptr;
johnc@1325 258 }
johnc@1325 259
johnc@1325 260 // We do not want to defer processing of card_ptr in this case
johnc@1325 261 // (we need to refine old_card_ptr and card_ptr)
johnc@1325 262 *defer = false;
johnc@1325 263 return old_card_ptr;
johnc@1325 264 }
johnc@1325 265 // Someone else beat us - try again.
johnc@1325 266 }
johnc@1325 267 }
johnc@1325 268
johnc@1325 269 jbyte* ConcurrentG1Refine::cache_insert(jbyte* card_ptr, bool* defer) {
johnc@1325 270 int count;
johnc@1325 271 jbyte* cached_ptr = add_card_count(card_ptr, &count, defer);
johnc@1325 272 assert(cached_ptr != NULL, "bad cached card ptr");
johnc@1325 273 assert(!is_young_card(cached_ptr), "shouldn't get a card in young region");
johnc@1325 274
johnc@1325 275 // The card pointer we obtained from card count cache is not hot
johnc@1325 276 // so do not store it in the cache; return it for immediate
johnc@1325 277 // refining.
ysr@777 278 if (count < G1ConcRSHotCardLimit) {
johnc@1325 279 return cached_ptr;
ysr@777 280 }
johnc@1325 281
johnc@1325 282 // Otherwise, the pointer we got from the _card_counts is hot.
ysr@777 283 jbyte* res = NULL;
ysr@777 284 MutexLockerEx x(HotCardCache_lock, Mutex::_no_safepoint_check_flag);
ysr@777 285 if (_n_hot == _hot_cache_size) {
ysr@777 286 res = _hot_cache[_hot_cache_idx];
ysr@777 287 _n_hot--;
ysr@777 288 }
ysr@777 289 // Now _n_hot < _hot_cache_size, and we can insert at _hot_cache_idx.
johnc@1325 290 _hot_cache[_hot_cache_idx] = cached_ptr;
ysr@777 291 _hot_cache_idx++;
ysr@777 292 if (_hot_cache_idx == _hot_cache_size) _hot_cache_idx = 0;
ysr@777 293 _n_hot++;
johnc@1325 294
johnc@1325 295 if (res != NULL) {
johnc@1325 296 // Even though the region containg res was not in the young list
johnc@1325 297 // when it was recorded in the hot cache it could have been added
johnc@1325 298 // to the free list and subsequently added to the young list in
johnc@1325 299 // the intervening time. If res is in a young region, return NULL
johnc@1325 300 // so that res is not cleaned. See CR 6817995.
johnc@1325 301
johnc@1325 302 if (is_young_card(res)) {
johnc@1325 303 res = NULL;
johnc@1325 304 }
johnc@1325 305 }
johnc@1325 306
ysr@777 307 return res;
ysr@777 308 }
ysr@777 309
ysr@777 310 void ConcurrentG1Refine::clean_up_cache(int worker_i, G1RemSet* g1rs) {
ysr@777 311 assert(!use_cache(), "cache should be disabled");
johnc@1324 312 int start_idx;
johnc@1324 313
johnc@1324 314 while ((start_idx = _hot_cache_par_claimed_idx) < _n_hot) { // read once
johnc@1324 315 int end_idx = start_idx + _hot_cache_par_chunk_size;
johnc@1324 316
johnc@1324 317 if (start_idx ==
johnc@1324 318 Atomic::cmpxchg(end_idx, &_hot_cache_par_claimed_idx, start_idx)) {
johnc@1324 319 // The current worker has successfully claimed the chunk [start_idx..end_idx)
johnc@1324 320 end_idx = MIN2(end_idx, _n_hot);
johnc@1324 321 for (int i = start_idx; i < end_idx; i++) {
johnc@1324 322 jbyte* entry = _hot_cache[i];
johnc@1324 323 if (entry != NULL) {
johnc@1324 324 g1rs->concurrentRefineOneCard(entry, worker_i);
johnc@1324 325 }
johnc@1324 326 }
ysr@777 327 }
ysr@777 328 }
ysr@777 329 }
ysr@777 330
johnc@1325 331 void ConcurrentG1Refine::expand_card_count_cache() {
johnc@1325 332 if (_n_card_counts < _max_n_card_counts) {
johnc@1325 333 int new_idx = _cache_size_index+1;
johnc@1325 334 int new_size = _cc_cache_sizes[new_idx];
johnc@1325 335 if (new_size < 0) new_size = _max_n_card_counts;
johnc@1325 336
johnc@1325 337 // Make sure we don't go bigger than we will ever need
johnc@1325 338 new_size = MIN2((unsigned) new_size, _max_n_card_counts);
johnc@1325 339
johnc@1325 340 // Expand the card count and card epoch tables
johnc@1325 341 if (new_size > (int)_n_card_counts) {
johnc@1325 342 // We can just free and allocate a new array as we're
johnc@1325 343 // not interested in preserving the contents
johnc@1325 344 assert(_card_counts != NULL, "Logic!");
johnc@1325 345 assert(_card_epochs != NULL, "Logic!");
johnc@1325 346 FREE_C_HEAP_ARRAY(CardCountCacheEntry, _card_counts);
johnc@1325 347 FREE_C_HEAP_ARRAY(CardEpochCacheEntry, _card_epochs);
johnc@1325 348 _n_card_counts = new_size;
johnc@1325 349 _card_counts = NEW_C_HEAP_ARRAY(CardCountCacheEntry, _n_card_counts);
johnc@1325 350 _card_epochs = NEW_C_HEAP_ARRAY(CardEpochCacheEntry, _n_card_counts);
johnc@1325 351 _cache_size_index = new_idx;
ysr@777 352 }
ysr@777 353 }
ysr@777 354 }
ysr@777 355
johnc@1325 356 void ConcurrentG1Refine::clear_and_record_card_counts() {
johnc@1325 357 if (G1ConcRSLogCacheSize == 0) return;
johnc@1325 358
johnc@1325 359 #ifndef PRODUCT
johnc@1325 360 double start = os::elapsedTime();
johnc@1325 361 #endif
johnc@1325 362
johnc@1325 363 if (_expand_card_counts) {
johnc@1325 364 expand_card_count_cache();
johnc@1325 365 _expand_card_counts = false;
johnc@1325 366 // Only need to clear the epochs.
johnc@1325 367 Copy::fill_to_bytes(&_card_epochs[0], _n_card_counts * sizeof(CardEpochCacheEntry));
ysr@777 368 }
ysr@777 369
johnc@1325 370 int this_epoch = (int) _n_periods;
johnc@1325 371 assert((this_epoch+1) <= max_jint, "to many periods");
johnc@1325 372 // Update epoch
johnc@1325 373 _n_periods++;
johnc@1325 374
johnc@1325 375 #ifndef PRODUCT
johnc@1325 376 double elapsed = os::elapsedTime() - start;
johnc@1325 377 _g1h->g1_policy()->record_cc_clear_time(elapsed * 1000.0);
johnc@1325 378 #endif
ysr@777 379 }
tonyp@1454 380
tonyp@1454 381 void ConcurrentG1Refine::print_worker_threads_on(outputStream* st) const {
tonyp@1454 382 for (int i = 0; i < _n_threads; ++i) {
tonyp@1454 383 _threads[i]->print_on(st);
tonyp@1454 384 st->cr();
tonyp@1454 385 }
tonyp@1454 386 }
tonyp@1454 387

mercurial