1 /* 2 * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "gc/g1/concurrentG1Refine.hpp" 27 #include "gc/g1/dirtyCardQueue.hpp" 28 #include "gc/g1/g1BlockOffsetTable.inline.hpp" 29 #include "gc/g1/g1CollectedHeap.inline.hpp" 30 #include "gc/g1/g1FromCardCache.hpp" 31 #include "gc/g1/g1GCPhaseTimes.hpp" 32 #include "gc/g1/g1HotCardCache.hpp" 33 #include "gc/g1/g1OopClosures.inline.hpp" 34 #include "gc/g1/g1RemSet.inline.hpp" 35 #include "gc/g1/g1SATBCardTableModRefBS.inline.hpp" 36 #include "gc/g1/heapRegion.inline.hpp" 37 #include "gc/g1/heapRegionManager.inline.hpp" 38 #include "gc/g1/heapRegionRemSet.hpp" 39 #include "gc/shared/gcTraceTime.inline.hpp" 40 #include "memory/iterator.hpp" 41 #include "memory/resourceArea.hpp" 42 #include "oops/oop.inline.hpp" 43 #include "utilities/align.hpp" 44 #include "utilities/globalDefinitions.hpp" 45 #include "utilities/intHisto.hpp" 46 #include "utilities/stack.inline.hpp" 47 48 // Collects information about the overall remembered set scan progress during an evacuation. 49 class G1RemSetScanState : public CHeapObj<mtGC> { 50 private: 51 class G1ClearCardTableTask : public AbstractGangTask { 52 G1CollectedHeap* _g1h; 53 uint* _dirty_region_list; 54 size_t _num_dirty_regions; 55 size_t _chunk_length; 56 57 size_t volatile _cur_dirty_regions; 58 public: 59 G1ClearCardTableTask(G1CollectedHeap* g1h, 60 uint* dirty_region_list, 61 size_t num_dirty_regions, 62 size_t chunk_length) : 63 AbstractGangTask("G1 Clear Card Table Task"), 64 _g1h(g1h), 65 _dirty_region_list(dirty_region_list), 66 _num_dirty_regions(num_dirty_regions), 67 _chunk_length(chunk_length), 68 _cur_dirty_regions(0) { 69 70 assert(chunk_length > 0, "must be"); 71 } 72 73 static size_t chunk_size() { return M; } 74 75 void work(uint worker_id) { 76 G1SATBCardTableModRefBS* ct_bs = _g1h->g1_barrier_set(); 77 78 while (_cur_dirty_regions < _num_dirty_regions) { 79 size_t next = Atomic::add(_chunk_length, &_cur_dirty_regions) - _chunk_length; 80 size_t max = MIN2(next + _chunk_length, _num_dirty_regions); 81 82 for (size_t i = next; i < max; i++) { 83 HeapRegion* r = _g1h->region_at(_dirty_region_list[i]); 84 if (!r->is_survivor()) { 85 ct_bs->clear(MemRegion(r->bottom(), r->end())); 86 } 87 } 88 } 89 } 90 }; 91 92 size_t _max_regions; 93 94 // Scan progress for the remembered set of a single region. Transitions from 95 // Unclaimed -> Claimed -> Complete. 96 // At each of the transitions the thread that does the transition needs to perform 97 // some special action once. This is the reason for the extra "Claimed" state. 98 typedef jint G1RemsetIterState; 99 100 static const G1RemsetIterState Unclaimed = 0; // The remembered set has not been scanned yet. 101 static const G1RemsetIterState Claimed = 1; // The remembered set is currently being scanned. 102 static const G1RemsetIterState Complete = 2; // The remembered set has been completely scanned. 103 104 G1RemsetIterState volatile* _iter_states; 105 // The current location where the next thread should continue scanning in a region's 106 // remembered set. 107 size_t volatile* _iter_claims; 108 109 // Temporary buffer holding the regions we used to store remembered set scan duplicate 110 // information. These are also called "dirty". Valid entries are from [0.._cur_dirty_region) 111 uint* _dirty_region_buffer; 112 113 typedef jbyte IsDirtyRegionState; 114 static const IsDirtyRegionState Clean = 0; 115 static const IsDirtyRegionState Dirty = 1; 116 // Holds a flag for every region whether it is in the _dirty_region_buffer already 117 // to avoid duplicates. Uses jbyte since there are no atomic instructions for bools. 118 IsDirtyRegionState* _in_dirty_region_buffer; 119 size_t _cur_dirty_region; 120 121 // Creates a snapshot of the current _top values at the start of collection to 122 // filter out card marks that we do not want to scan. 123 class G1ResetScanTopClosure : public HeapRegionClosure { 124 private: 125 HeapWord** _scan_top; 126 public: 127 G1ResetScanTopClosure(HeapWord** scan_top) : _scan_top(scan_top) { } 128 129 virtual bool doHeapRegion(HeapRegion* r) { 130 uint hrm_index = r->hrm_index(); 131 if (!r->in_collection_set() && r->is_old_or_humongous()) { 132 _scan_top[hrm_index] = r->top(); 133 } else { 134 _scan_top[hrm_index] = r->bottom(); 135 } 136 return false; 137 } 138 }; 139 140 // For each region, contains the maximum top() value to be used during this garbage 141 // collection. Subsumes common checks like filtering out everything but old and 142 // humongous regions outside the collection set. 143 // This is valid because we are not interested in scanning stray remembered set 144 // entries from free or archive regions. 145 HeapWord** _scan_top; 146 public: 147 G1RemSetScanState() : 148 _max_regions(0), 149 _iter_states(NULL), 150 _iter_claims(NULL), 151 _dirty_region_buffer(NULL), 152 _in_dirty_region_buffer(NULL), 153 _cur_dirty_region(0), 154 _scan_top(NULL) { 155 } 156 157 ~G1RemSetScanState() { 158 if (_iter_states != NULL) { 159 FREE_C_HEAP_ARRAY(G1RemsetIterState, _iter_states); 160 } 161 if (_iter_claims != NULL) { 162 FREE_C_HEAP_ARRAY(size_t, _iter_claims); 163 } 164 if (_dirty_region_buffer != NULL) { 165 FREE_C_HEAP_ARRAY(uint, _dirty_region_buffer); 166 } 167 if (_in_dirty_region_buffer != NULL) { 168 FREE_C_HEAP_ARRAY(IsDirtyRegionState, _in_dirty_region_buffer); 169 } 170 if (_scan_top != NULL) { 171 FREE_C_HEAP_ARRAY(HeapWord*, _scan_top); 172 } 173 } 174 175 void initialize(uint max_regions) { 176 assert(_iter_states == NULL, "Must not be initialized twice"); 177 assert(_iter_claims == NULL, "Must not be initialized twice"); 178 _max_regions = max_regions; 179 _iter_states = NEW_C_HEAP_ARRAY(G1RemsetIterState, max_regions, mtGC); 180 _iter_claims = NEW_C_HEAP_ARRAY(size_t, max_regions, mtGC); 181 _dirty_region_buffer = NEW_C_HEAP_ARRAY(uint, max_regions, mtGC); 182 _in_dirty_region_buffer = NEW_C_HEAP_ARRAY(IsDirtyRegionState, max_regions, mtGC); 183 _scan_top = NEW_C_HEAP_ARRAY(HeapWord*, max_regions, mtGC); 184 } 185 186 void reset() { 187 for (uint i = 0; i < _max_regions; i++) { 188 _iter_states[i] = Unclaimed; 189 } 190 191 G1ResetScanTopClosure cl(_scan_top); 192 G1CollectedHeap::heap()->heap_region_iterate(&cl); 193 194 memset((void*)_iter_claims, 0, _max_regions * sizeof(size_t)); 195 memset(_in_dirty_region_buffer, Clean, _max_regions * sizeof(IsDirtyRegionState)); 196 _cur_dirty_region = 0; 197 } 198 199 // Attempt to claim the remembered set of the region for iteration. Returns true 200 // if this call caused the transition from Unclaimed to Claimed. 201 inline bool claim_iter(uint region) { 202 assert(region < _max_regions, "Tried to access invalid region %u", region); 203 if (_iter_states[region] != Unclaimed) { 204 return false; 205 } 206 jint res = Atomic::cmpxchg(Claimed, (jint*)(&_iter_states[region]), Unclaimed); 207 return (res == Unclaimed); 208 } 209 210 // Try to atomically sets the iteration state to "complete". Returns true for the 211 // thread that caused the transition. 212 inline bool set_iter_complete(uint region) { 213 if (iter_is_complete(region)) { 214 return false; 215 } 216 jint res = Atomic::cmpxchg(Complete, (jint*)(&_iter_states[region]), Claimed); 217 return (res == Claimed); 218 } 219 220 // Returns true if the region's iteration is complete. 221 inline bool iter_is_complete(uint region) const { 222 assert(region < _max_regions, "Tried to access invalid region %u", region); 223 return _iter_states[region] == Complete; 224 } 225 226 // The current position within the remembered set of the given region. 227 inline size_t iter_claimed(uint region) const { 228 assert(region < _max_regions, "Tried to access invalid region %u", region); 229 return _iter_claims[region]; 230 } 231 232 // Claim the next block of cards within the remembered set of the region with 233 // step size. 234 inline size_t iter_claimed_next(uint region, size_t step) { 235 return Atomic::add(step, &_iter_claims[region]) - step; 236 } 237 238 void add_dirty_region(uint region) { 239 if (_in_dirty_region_buffer[region] == Dirty) { 240 return; 241 } 242 243 bool marked_as_dirty = Atomic::cmpxchg(Dirty, &_in_dirty_region_buffer[region], Clean) == Clean; 244 if (marked_as_dirty) { 245 size_t allocated = Atomic::add(1, &_cur_dirty_region) - 1; 246 _dirty_region_buffer[allocated] = region; 247 } 248 } 249 250 HeapWord* scan_top(uint region_idx) const { 251 return _scan_top[region_idx]; 252 } 253 254 // Clear the card table of "dirty" regions. 255 void clear_card_table(WorkGang* workers) { 256 if (_cur_dirty_region == 0) { 257 return; 258 } 259 260 size_t const num_chunks = align_up(_cur_dirty_region * HeapRegion::CardsPerRegion, G1ClearCardTableTask::chunk_size()) / G1ClearCardTableTask::chunk_size(); 261 uint const num_workers = (uint)MIN2(num_chunks, (size_t)workers->active_workers()); 262 size_t const chunk_length = G1ClearCardTableTask::chunk_size() / HeapRegion::CardsPerRegion; 263 264 // Iterate over the dirty cards region list. 265 G1ClearCardTableTask cl(G1CollectedHeap::heap(), _dirty_region_buffer, _cur_dirty_region, chunk_length); 266 267 log_debug(gc, ergo)("Running %s using %u workers for " SIZE_FORMAT " " 268 "units of work for " SIZE_FORMAT " regions.", 269 cl.name(), num_workers, num_chunks, _cur_dirty_region); 270 workers->run_task(&cl, num_workers); 271 272 #ifndef PRODUCT 273 // Need to synchronize with concurrent cleanup since it needs to 274 // finish its card table clearing before we can verify. 275 G1CollectedHeap::heap()->wait_while_free_regions_coming(); 276 G1CollectedHeap::heap()->verifier()->verify_card_table_cleanup(); 277 #endif 278 } 279 }; 280 281 G1RemSet::G1RemSet(G1CollectedHeap* g1, 282 CardTableModRefBS* ct_bs, 283 G1HotCardCache* hot_card_cache) : 284 _g1(g1), 285 _scan_state(new G1RemSetScanState()), 286 _num_conc_refined_cards(0), 287 _ct_bs(ct_bs), 288 _g1p(_g1->g1_policy()), 289 _hot_card_cache(hot_card_cache), 290 _prev_period_summary(), 291 _into_cset_dirty_card_queue_set(false) 292 { 293 // Initialize the card queue set used to hold cards containing 294 // references into the collection set. 295 _into_cset_dirty_card_queue_set.initialize(NULL, // Should never be called by the Java code 296 DirtyCardQ_CBL_mon, 297 DirtyCardQ_FL_lock, 298 -1, // never trigger processing 299 -1, // no limit on length 300 Shared_DirtyCardQ_lock, 301 &JavaThread::dirty_card_queue_set()); 302 } 303 304 G1RemSet::~G1RemSet() { 305 if (_scan_state != NULL) { 306 delete _scan_state; 307 } 308 } 309 310 uint G1RemSet::num_par_rem_sets() { 311 return MAX2(DirtyCardQueueSet::num_par_ids() + ConcurrentG1Refine::thread_num(), ParallelGCThreads); 312 } 313 314 void G1RemSet::initialize(size_t capacity, uint max_regions) { 315 G1FromCardCache::initialize(num_par_rem_sets(), max_regions); 316 _scan_state->initialize(max_regions); 317 { 318 GCTraceTime(Debug, gc, marking)("Initialize Card Live Data"); 319 _card_live_data.initialize(capacity, max_regions); 320 } 321 if (G1PretouchAuxiliaryMemory) { 322 GCTraceTime(Debug, gc, marking)("Pre-Touch Card Live Data"); 323 _card_live_data.pretouch(); 324 } 325 } 326 327 G1ScanRSForRegionClosure::G1ScanRSForRegionClosure(G1RemSetScanState* scan_state, 328 G1ScanObjsDuringScanRSClosure* scan_obj_on_card, 329 CodeBlobClosure* code_root_cl, 330 uint worker_i) : 331 _scan_state(scan_state), 332 _scan_objs_on_card_cl(scan_obj_on_card), 333 _code_root_cl(code_root_cl), 334 _strong_code_root_scan_time_sec(0.0), 335 _cards_claimed(0), 336 _cards_scanned(0), 337 _cards_skipped(0), 338 _worker_i(worker_i) { 339 _g1h = G1CollectedHeap::heap(); 340 _bot = _g1h->bot(); 341 _ct_bs = _g1h->g1_barrier_set(); 342 } 343 344 void G1ScanRSForRegionClosure::scan_card(MemRegion mr, uint region_idx_for_card) { 345 HeapRegion* const card_region = _g1h->region_at(region_idx_for_card); 346 _scan_objs_on_card_cl->set_region(card_region); 347 card_region->oops_on_card_seq_iterate_careful<true>(mr, _scan_objs_on_card_cl); 348 _cards_scanned++; 349 } 350 351 void G1ScanRSForRegionClosure::scan_strong_code_roots(HeapRegion* r) { 352 double scan_start = os::elapsedTime(); 353 r->strong_code_roots_do(_code_root_cl); 354 _strong_code_root_scan_time_sec += (os::elapsedTime() - scan_start); 355 } 356 357 void G1ScanRSForRegionClosure::claim_card(size_t card_index, const uint region_idx_for_card){ 358 _ct_bs->set_card_claimed(card_index); 359 _scan_state->add_dirty_region(region_idx_for_card); 360 } 361 362 bool G1ScanRSForRegionClosure::doHeapRegion(HeapRegion* r) { 363 assert(r->in_collection_set(), "should only be called on elements of CS."); 364 uint region_idx = r->hrm_index(); 365 366 if (_scan_state->iter_is_complete(region_idx)) { 367 return false; 368 } 369 if (_scan_state->claim_iter(region_idx)) { 370 // If we ever free the collection set concurrently, we should also 371 // clear the card table concurrently therefore we won't need to 372 // add regions of the collection set to the dirty cards region. 373 _scan_state->add_dirty_region(region_idx); 374 } 375 376 // We claim cards in blocks so as to reduce the contention. 377 size_t const block_size = G1RSetScanBlockSize; 378 379 HeapRegionRemSetIterator iter(r->rem_set()); 380 size_t card_index; 381 382 size_t claimed_card_block = _scan_state->iter_claimed_next(region_idx, block_size); 383 for (size_t current_card = 0; iter.has_next(card_index); current_card++) { 384 if (current_card >= claimed_card_block + block_size) { 385 claimed_card_block = _scan_state->iter_claimed_next(region_idx, block_size); 386 } 387 if (current_card < claimed_card_block) { 388 _cards_skipped++; 389 continue; 390 } 391 _cards_claimed++; 392 393 // If the card is dirty, then G1 will scan it during Update RS. 394 if (_ct_bs->is_card_claimed(card_index) || _ct_bs->is_card_dirty(card_index)) { 395 continue; 396 } 397 398 HeapWord* const card_start = _g1h->bot()->address_for_index(card_index); 399 uint const region_idx_for_card = _g1h->addr_to_region(card_start); 400 401 assert(_g1h->region_at(region_idx_for_card)->is_in_reserved(card_start), 402 "Card start " PTR_FORMAT " to scan outside of region %u", p2i(card_start), _g1h->region_at(region_idx_for_card)->hrm_index()); 403 HeapWord* const top = _scan_state->scan_top(region_idx_for_card); 404 if (card_start >= top) { 405 continue; 406 } 407 408 // We claim lazily (so races are possible but they're benign), which reduces the 409 // number of duplicate scans (the rsets of the regions in the cset can intersect). 410 // Claim the card after checking bounds above: the remembered set may contain 411 // random cards into current survivor, and we would then have an incorrectly 412 // claimed card in survivor space. Card table clear does not reset the card table 413 // of survivor space regions. 414 claim_card(card_index, region_idx_for_card); 415 416 MemRegion const mr(card_start, MIN2(card_start + BOTConstants::N_words, top)); 417 418 scan_card(mr, region_idx_for_card); 419 } 420 if (_scan_state->set_iter_complete(region_idx)) { 421 // Scan the strong code root list attached to the current region 422 scan_strong_code_roots(r); 423 } 424 return false; 425 } 426 427 void G1RemSet::scan_rem_set(G1ParScanThreadState* pss, 428 CodeBlobClosure* heap_region_codeblobs, 429 uint worker_i) { 430 double rs_time_start = os::elapsedTime(); 431 432 G1ScanObjsDuringScanRSClosure scan_cl(_g1, pss); 433 G1ScanRSForRegionClosure cl(_scan_state, &scan_cl, heap_region_codeblobs, worker_i); 434 _g1->collection_set_iterate_from(&cl, worker_i); 435 436 double scan_rs_time_sec = (os::elapsedTime() - rs_time_start) - 437 cl.strong_code_root_scan_time_sec(); 438 439 G1GCPhaseTimes* p = _g1p->phase_times(); 440 441 p->record_time_secs(G1GCPhaseTimes::ScanRS, worker_i, scan_rs_time_sec); 442 p->record_thread_work_item(G1GCPhaseTimes::ScanRS, worker_i, cl.cards_scanned(), G1GCPhaseTimes::ScannedCards); 443 p->record_thread_work_item(G1GCPhaseTimes::ScanRS, worker_i, cl.cards_claimed(), G1GCPhaseTimes::ClaimedCards); 444 p->record_thread_work_item(G1GCPhaseTimes::ScanRS, worker_i, cl.cards_skipped(), G1GCPhaseTimes::SkippedCards); 445 446 p->record_time_secs(G1GCPhaseTimes::CodeRoots, worker_i, cl.strong_code_root_scan_time_sec()); 447 } 448 449 // Closure used for updating RSets and recording references that 450 // point into the collection set. Only called during an 451 // evacuation pause. 452 class G1RefineCardClosure: public CardTableEntryClosure { 453 G1RemSet* _g1rs; 454 DirtyCardQueue* _into_cset_dcq; 455 G1ScanObjsDuringUpdateRSClosure* _update_rs_cl; 456 public: 457 G1RefineCardClosure(G1CollectedHeap* g1h, 458 DirtyCardQueue* into_cset_dcq, 459 G1ScanObjsDuringUpdateRSClosure* update_rs_cl) : 460 _g1rs(g1h->g1_rem_set()), _into_cset_dcq(into_cset_dcq), _update_rs_cl(update_rs_cl) 461 {} 462 463 bool do_card_ptr(jbyte* card_ptr, uint worker_i) { 464 // The only time we care about recording cards that 465 // contain references that point into the collection set 466 // is during RSet updating within an evacuation pause. 467 // In this case worker_i should be the id of a GC worker thread. 468 assert(SafepointSynchronize::is_at_safepoint(), "not during an evacuation pause"); 469 470 if (_g1rs->refine_card_during_gc(card_ptr, _update_rs_cl)) { 471 // 'card_ptr' contains references that point into the collection 472 // set. We need to record the card in the DCQS 473 // (_into_cset_dirty_card_queue_set) 474 // that's used for that purpose. 475 // 476 // Enqueue the card 477 _into_cset_dcq->enqueue(card_ptr); 478 } 479 return true; 480 } 481 }; 482 483 void G1RemSet::update_rem_set(DirtyCardQueue* into_cset_dcq, 484 G1ParScanThreadState* pss, 485 uint worker_i) { 486 G1ScanObjsDuringUpdateRSClosure update_rs_cl(_g1, pss, worker_i); 487 G1RefineCardClosure refine_card_cl(_g1, into_cset_dcq, &update_rs_cl); 488 489 G1GCParPhaseTimesTracker x(_g1p->phase_times(), G1GCPhaseTimes::UpdateRS, worker_i); 490 if (G1HotCardCache::default_use_cache()) { 491 // Apply the closure to the entries of the hot card cache. 492 G1GCParPhaseTimesTracker y(_g1p->phase_times(), G1GCPhaseTimes::ScanHCC, worker_i); 493 _g1->iterate_hcc_closure(&refine_card_cl, worker_i); 494 } 495 // Apply the closure to all remaining log entries. 496 _g1->iterate_dirty_card_closure(&refine_card_cl, worker_i); 497 } 498 499 void G1RemSet::cleanupHRRS() { 500 HeapRegionRemSet::cleanup(); 501 } 502 503 void G1RemSet::oops_into_collection_set_do(G1ParScanThreadState* pss, 504 CodeBlobClosure* heap_region_codeblobs, 505 uint worker_i) { 506 // A DirtyCardQueue that is used to hold cards containing references 507 // that point into the collection set. This DCQ is associated with a 508 // special DirtyCardQueueSet (see g1CollectedHeap.hpp). Under normal 509 // circumstances (i.e. the pause successfully completes), these cards 510 // are just discarded (there's no need to update the RSets of regions 511 // that were in the collection set - after the pause these regions 512 // are wholly 'free' of live objects. In the event of an evacuation 513 // failure the cards/buffers in this queue set are passed to the 514 // DirtyCardQueueSet that is used to manage RSet updates 515 DirtyCardQueue into_cset_dcq(&_into_cset_dirty_card_queue_set); 516 517 update_rem_set(&into_cset_dcq, pss, worker_i); 518 scan_rem_set(pss, heap_region_codeblobs, worker_i);; 519 } 520 521 void G1RemSet::prepare_for_oops_into_collection_set_do() { 522 _g1->set_refine_cte_cl_concurrency(false); 523 DirtyCardQueueSet& dcqs = JavaThread::dirty_card_queue_set(); 524 dcqs.concatenate_logs(); 525 526 _scan_state->reset(); 527 } 528 529 void G1RemSet::cleanup_after_oops_into_collection_set_do() { 530 G1GCPhaseTimes* phase_times = _g1->g1_policy()->phase_times(); 531 // Cleanup after copy 532 _g1->set_refine_cte_cl_concurrency(true); 533 534 // Set all cards back to clean. 535 double start = os::elapsedTime(); 536 _scan_state->clear_card_table(_g1->workers()); 537 phase_times->record_clear_ct_time((os::elapsedTime() - start) * 1000.0); 538 539 DirtyCardQueueSet& into_cset_dcqs = _into_cset_dirty_card_queue_set; 540 541 if (_g1->evacuation_failed()) { 542 double restore_remembered_set_start = os::elapsedTime(); 543 544 // Restore remembered sets for the regions pointing into the collection set. 545 // We just need to transfer the completed buffers from the DirtyCardQueueSet 546 // used to hold cards that contain references that point into the collection set 547 // to the DCQS used to hold the deferred RS updates. 548 _g1->dirty_card_queue_set().merge_bufferlists(&into_cset_dcqs); 549 phase_times->record_evac_fail_restore_remsets((os::elapsedTime() - restore_remembered_set_start) * 1000.0); 550 } 551 552 // Free any completed buffers in the DirtyCardQueueSet used to hold cards 553 // which contain references that point into the collection. 554 _into_cset_dirty_card_queue_set.clear(); 555 assert(_into_cset_dirty_card_queue_set.completed_buffers_num() == 0, 556 "all buffers should be freed"); 557 _into_cset_dirty_card_queue_set.clear_n_completed_buffers(); 558 } 559 560 class G1ScrubRSClosure: public HeapRegionClosure { 561 G1CollectedHeap* _g1h; 562 G1CardLiveData* _live_data; 563 public: 564 G1ScrubRSClosure(G1CardLiveData* live_data) : 565 _g1h(G1CollectedHeap::heap()), 566 _live_data(live_data) { } 567 568 bool doHeapRegion(HeapRegion* r) { 569 if (!r->is_continues_humongous()) { 570 r->rem_set()->scrub(_live_data); 571 } 572 return false; 573 } 574 }; 575 576 void G1RemSet::scrub(uint worker_num, HeapRegionClaimer *hrclaimer) { 577 G1ScrubRSClosure scrub_cl(&_card_live_data); 578 _g1->heap_region_par_iterate(&scrub_cl, worker_num, hrclaimer); 579 } 580 581 inline void check_card_ptr(jbyte* card_ptr, CardTableModRefBS* ct_bs) { 582 #ifdef ASSERT 583 G1CollectedHeap* g1 = G1CollectedHeap::heap(); 584 assert(g1->is_in_exact(ct_bs->addr_for(card_ptr)), 585 "Card at " PTR_FORMAT " index " SIZE_FORMAT " representing heap at " PTR_FORMAT " (%u) must be in committed heap", 586 p2i(card_ptr), 587 ct_bs->index_for(ct_bs->addr_for(card_ptr)), 588 p2i(ct_bs->addr_for(card_ptr)), 589 g1->addr_to_region(ct_bs->addr_for(card_ptr))); 590 #endif 591 } 592 593 void G1RemSet::refine_card_concurrently(jbyte* card_ptr, 594 uint worker_i) { 595 assert(!_g1->is_gc_active(), "Only call concurrently"); 596 597 check_card_ptr(card_ptr, _ct_bs); 598 599 // If the card is no longer dirty, nothing to do. 600 if (*card_ptr != CardTableModRefBS::dirty_card_val()) { 601 return; 602 } 603 604 // Construct the region representing the card. 605 HeapWord* start = _ct_bs->addr_for(card_ptr); 606 // And find the region containing it. 607 HeapRegion* r = _g1->heap_region_containing(start); 608 609 // This check is needed for some uncommon cases where we should 610 // ignore the card. 611 // 612 // The region could be young. Cards for young regions are 613 // distinctly marked (set to g1_young_gen), so the post-barrier will 614 // filter them out. However, that marking is performed 615 // concurrently. A write to a young object could occur before the 616 // card has been marked young, slipping past the filter. 617 // 618 // The card could be stale, because the region has been freed since 619 // the card was recorded. In this case the region type could be 620 // anything. If (still) free or (reallocated) young, just ignore 621 // it. If (reallocated) old or humongous, the later card trimming 622 // and additional checks in iteration may detect staleness. At 623 // worst, we end up processing a stale card unnecessarily. 624 // 625 // In the normal (non-stale) case, the synchronization between the 626 // enqueueing of the card and processing it here will have ensured 627 // we see the up-to-date region type here. 628 if (!r->is_old_or_humongous()) { 629 return; 630 } 631 632 // While we are processing RSet buffers during the collection, we 633 // actually don't want to scan any cards on the collection set, 634 // since we don't want to update remembered sets with entries that 635 // point into the collection set, given that live objects from the 636 // collection set are about to move and such entries will be stale 637 // very soon. This change also deals with a reliability issue which 638 // involves scanning a card in the collection set and coming across 639 // an array that was being chunked and looking malformed. Note, 640 // however, that if evacuation fails, we have to scan any objects 641 // that were not moved and create any missing entries. 642 if (r->in_collection_set()) { 643 return; 644 } 645 646 // The result from the hot card cache insert call is either: 647 // * pointer to the current card 648 // (implying that the current card is not 'hot'), 649 // * null 650 // (meaning we had inserted the card ptr into the "hot" card cache, 651 // which had some headroom), 652 // * a pointer to a "hot" card that was evicted from the "hot" cache. 653 // 654 655 if (_hot_card_cache->use_cache()) { 656 assert(!SafepointSynchronize::is_at_safepoint(), "sanity"); 657 658 const jbyte* orig_card_ptr = card_ptr; 659 card_ptr = _hot_card_cache->insert(card_ptr); 660 if (card_ptr == NULL) { 661 // There was no eviction. Nothing to do. 662 return; 663 } else if (card_ptr != orig_card_ptr) { 664 // Original card was inserted and an old card was evicted. 665 start = _ct_bs->addr_for(card_ptr); 666 r = _g1->heap_region_containing(start); 667 668 // Check whether the region formerly in the cache should be 669 // ignored, as discussed earlier for the original card. The 670 // region could have been freed while in the cache. The cset is 671 // not relevant here, since we're in concurrent phase. 672 if (!r->is_old_or_humongous()) { 673 return; 674 } 675 } // Else we still have the original card. 676 } 677 678 // Trim the region designated by the card to what's been allocated 679 // in the region. The card could be stale, or the card could cover 680 // (part of) an object at the end of the allocated space and extend 681 // beyond the end of allocation. 682 683 // Non-humongous objects are only allocated in the old-gen during 684 // GC, so if region is old then top is stable. Humongous object 685 // allocation sets top last; if top has not yet been set, this is 686 // a stale card and we'll end up with an empty intersection. If 687 // this is not a stale card, the synchronization between the 688 // enqueuing of the card and processing it here will have ensured 689 // we see the up-to-date top here. 690 HeapWord* scan_limit = r->top(); 691 692 if (scan_limit <= start) { 693 // If the trimmed region is empty, the card must be stale. 694 return; 695 } 696 697 // Okay to clean and process the card now. There are still some 698 // stale card cases that may be detected by iteration and dealt with 699 // as iteration failure. 700 *const_cast<volatile jbyte*>(card_ptr) = CardTableModRefBS::clean_card_val(); 701 702 // This fence serves two purposes. First, the card must be cleaned 703 // before processing the contents. Second, we can't proceed with 704 // processing until after the read of top, for synchronization with 705 // possibly concurrent humongous object allocation. It's okay that 706 // reading top and reading type were racy wrto each other. We need 707 // both set, in any order, to proceed. 708 OrderAccess::fence(); 709 710 // Don't use addr_for(card_ptr + 1) which can ask for 711 // a card beyond the heap. 712 HeapWord* end = start + CardTableModRefBS::card_size_in_words; 713 MemRegion dirty_region(start, MIN2(scan_limit, end)); 714 assert(!dirty_region.is_empty(), "sanity"); 715 716 G1ConcurrentRefineOopClosure conc_refine_cl(_g1, worker_i); 717 718 bool card_processed = 719 r->oops_on_card_seq_iterate_careful<false>(dirty_region, &conc_refine_cl); 720 721 // If unable to process the card then we encountered an unparsable 722 // part of the heap (e.g. a partially allocated object) while 723 // processing a stale card. Despite the card being stale, redirty 724 // and re-enqueue, because we've already cleaned the card. Without 725 // this we could incorrectly discard a non-stale card. 726 if (!card_processed) { 727 // The card might have gotten re-dirtied and re-enqueued while we 728 // worked. (In fact, it's pretty likely.) 729 if (*card_ptr != CardTableModRefBS::dirty_card_val()) { 730 *card_ptr = CardTableModRefBS::dirty_card_val(); 731 MutexLockerEx x(Shared_DirtyCardQ_lock, 732 Mutex::_no_safepoint_check_flag); 733 DirtyCardQueue* sdcq = 734 JavaThread::dirty_card_queue_set().shared_dirty_card_queue(); 735 sdcq->enqueue(card_ptr); 736 } 737 } else { 738 _num_conc_refined_cards++; // Unsynchronized update, only used for logging. 739 } 740 } 741 742 bool G1RemSet::refine_card_during_gc(jbyte* card_ptr, 743 G1ScanObjsDuringUpdateRSClosure* update_rs_cl) { 744 assert(_g1->is_gc_active(), "Only call during GC"); 745 746 check_card_ptr(card_ptr, _ct_bs); 747 748 // If the card is no longer dirty, nothing to do. This covers cards that were already 749 // scanned as parts of the remembered sets. 750 if (*card_ptr != CardTableModRefBS::dirty_card_val()) { 751 // No need to return that this card contains refs that point 752 // into the collection set. 753 return false; 754 } 755 756 // During GC we can immediately clean the card since we will not re-enqueue stale 757 // cards as we know they can be disregarded. 758 *card_ptr = CardTableModRefBS::clean_card_val(); 759 760 // Construct the region representing the card. 761 HeapWord* card_start = _ct_bs->addr_for(card_ptr); 762 // And find the region containing it. 763 HeapRegion* r = _g1->heap_region_containing(card_start); 764 765 HeapWord* scan_limit = _scan_state->scan_top(r->hrm_index()); 766 if (scan_limit <= card_start) { 767 // If the card starts above the area in the region containing objects to scan, skip it. 768 return false; 769 } 770 771 // Don't use addr_for(card_ptr + 1) which can ask for 772 // a card beyond the heap. 773 HeapWord* card_end = card_start + CardTableModRefBS::card_size_in_words; 774 MemRegion dirty_region(card_start, MIN2(scan_limit, card_end)); 775 assert(!dirty_region.is_empty(), "sanity"); 776 777 update_rs_cl->set_region(r); 778 update_rs_cl->reset_has_refs_into_cset(); 779 780 bool card_processed = r->oops_on_card_seq_iterate_careful<true>(dirty_region, update_rs_cl); 781 assert(card_processed, "must be"); 782 783 return update_rs_cl->has_refs_into_cset(); 784 } 785 786 void G1RemSet::print_periodic_summary_info(const char* header, uint period_count) { 787 if ((G1SummarizeRSetStatsPeriod > 0) && log_is_enabled(Trace, gc, remset) && 788 (period_count % G1SummarizeRSetStatsPeriod == 0)) { 789 790 G1RemSetSummary current(this); 791 _prev_period_summary.subtract_from(¤t); 792 793 Log(gc, remset) log; 794 log.trace("%s", header); 795 ResourceMark rm; 796 _prev_period_summary.print_on(log.trace_stream()); 797 798 _prev_period_summary.set(¤t); 799 } 800 } 801 802 void G1RemSet::print_summary_info() { 803 Log(gc, remset, exit) log; 804 if (log.is_trace()) { 805 log.trace(" Cumulative RS summary"); 806 G1RemSetSummary current; 807 ResourceMark rm; 808 current.print_on(log.trace_stream()); 809 } 810 } 811 812 void G1RemSet::create_card_live_data(WorkGang* workers, G1CMBitMap* mark_bitmap) { 813 _card_live_data.create(workers, mark_bitmap); 814 } 815 816 void G1RemSet::finalize_card_live_data(WorkGang* workers, G1CMBitMap* mark_bitmap) { 817 _card_live_data.finalize(workers, mark_bitmap); 818 } 819 820 void G1RemSet::verify_card_live_data(WorkGang* workers, G1CMBitMap* bitmap) { 821 _card_live_data.verify(workers, bitmap); 822 } 823 824 void G1RemSet::clear_card_live_data(WorkGang* workers) { 825 _card_live_data.clear(workers); 826 } 827 828 #ifdef ASSERT 829 void G1RemSet::verify_card_live_data_is_clear() { 830 _card_live_data.verify_is_clear(); 831 } 832 #endif