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