| 1 | /* |
| 2 | * Copyright (c) 2001, 2019, 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/g1BarrierSet.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/g1DirtyCardQueue.hpp" |
| 32 | #include "gc/g1/g1FromCardCache.hpp" |
| 33 | #include "gc/g1/g1GCPhaseTimes.hpp" |
| 34 | #include "gc/g1/g1HotCardCache.hpp" |
| 35 | #include "gc/g1/g1OopClosures.inline.hpp" |
| 36 | #include "gc/g1/g1RootClosures.hpp" |
| 37 | #include "gc/g1/g1RemSet.hpp" |
| 38 | #include "gc/g1/g1SharedDirtyCardQueue.hpp" |
| 39 | #include "gc/g1/heapRegion.inline.hpp" |
| 40 | #include "gc/g1/heapRegionManager.inline.hpp" |
| 41 | #include "gc/g1/heapRegionRemSet.hpp" |
| 42 | #include "gc/shared/gcTraceTime.inline.hpp" |
| 43 | #include "gc/shared/suspendibleThreadSet.hpp" |
| 44 | #include "jfr/jfrEvents.hpp" |
| 45 | #include "memory/iterator.hpp" |
| 46 | #include "memory/resourceArea.hpp" |
| 47 | #include "oops/access.inline.hpp" |
| 48 | #include "oops/oop.inline.hpp" |
| 49 | #include "runtime/os.hpp" |
| 50 | #include "utilities/align.hpp" |
| 51 | #include "utilities/globalDefinitions.hpp" |
| 52 | #include "utilities/stack.inline.hpp" |
| 53 | #include "utilities/ticks.hpp" |
| 54 | |
| 55 | // Collects information about the overall remembered set scan progress during an evacuation. |
| 56 | class G1RemSetScanState : public CHeapObj<mtGC> { |
| 57 | private: |
| 58 | class G1ClearCardTableTask : public AbstractGangTask { |
| 59 | G1CollectedHeap* _g1h; |
| 60 | uint* _dirty_region_list; |
| 61 | size_t _num_dirty_regions; |
| 62 | size_t _chunk_length; |
| 63 | |
| 64 | size_t volatile _cur_dirty_regions; |
| 65 | public: |
| 66 | G1ClearCardTableTask(G1CollectedHeap* g1h, |
| 67 | uint* dirty_region_list, |
| 68 | size_t num_dirty_regions, |
| 69 | size_t chunk_length) : |
| 70 | AbstractGangTask("G1 Clear Card Table Task" ), |
| 71 | _g1h(g1h), |
| 72 | _dirty_region_list(dirty_region_list), |
| 73 | _num_dirty_regions(num_dirty_regions), |
| 74 | _chunk_length(chunk_length), |
| 75 | _cur_dirty_regions(0) { |
| 76 | |
| 77 | assert(chunk_length > 0, "must be" ); |
| 78 | } |
| 79 | |
| 80 | static size_t chunk_size() { return M; } |
| 81 | |
| 82 | void work(uint worker_id) { |
| 83 | while (_cur_dirty_regions < _num_dirty_regions) { |
| 84 | size_t next = Atomic::add(_chunk_length, &_cur_dirty_regions) - _chunk_length; |
| 85 | size_t max = MIN2(next + _chunk_length, _num_dirty_regions); |
| 86 | |
| 87 | for (size_t i = next; i < max; i++) { |
| 88 | HeapRegion* r = _g1h->region_at(_dirty_region_list[i]); |
| 89 | if (!r->is_survivor()) { |
| 90 | r->clear_cardtable(); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | size_t _max_regions; |
| 98 | |
| 99 | // Scan progress for the remembered set of a single region. Transitions from |
| 100 | // Unclaimed -> Claimed -> Complete. |
| 101 | // At each of the transitions the thread that does the transition needs to perform |
| 102 | // some special action once. This is the reason for the extra "Claimed" state. |
| 103 | typedef jint G1RemsetIterState; |
| 104 | |
| 105 | static const G1RemsetIterState Unclaimed = 0; // The remembered set has not been scanned yet. |
| 106 | static const G1RemsetIterState Claimed = 1; // The remembered set is currently being scanned. |
| 107 | static const G1RemsetIterState Complete = 2; // The remembered set has been completely scanned. |
| 108 | |
| 109 | G1RemsetIterState volatile* _iter_states; |
| 110 | // The current location where the next thread should continue scanning in a region's |
| 111 | // remembered set. |
| 112 | size_t volatile* _iter_claims; |
| 113 | |
| 114 | // Temporary buffer holding the regions we used to store remembered set scan duplicate |
| 115 | // information. These are also called "dirty". Valid entries are from [0.._cur_dirty_region) |
| 116 | uint* _dirty_region_buffer; |
| 117 | |
| 118 | // Flag for every region whether it is in the _dirty_region_buffer already |
| 119 | // to avoid duplicates. |
| 120 | bool volatile* _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_or_archive() && !r->is_empty()) { |
| 134 | _scan_top[hrm_index] = r->top(); |
| 135 | } else { |
| 136 | _scan_top[hrm_index] = NULL; |
| 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(bool, _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(bool, 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 | clear_scan_top(i); |
| 192 | } |
| 193 | |
| 194 | G1ResetScanTopClosure cl(_scan_top); |
| 195 | G1CollectedHeap::heap()->heap_region_iterate(&cl); |
| 196 | |
| 197 | memset((void*)_iter_claims, 0, _max_regions * sizeof(size_t)); |
| 198 | memset((void*)_in_dirty_region_buffer, false, _max_regions * sizeof(bool)); |
| 199 | _cur_dirty_region = 0; |
| 200 | } |
| 201 | |
| 202 | // Attempt to claim the remembered set of the region for iteration. Returns true |
| 203 | // if this call caused the transition from Unclaimed to Claimed. |
| 204 | inline bool claim_iter(uint region) { |
| 205 | assert(region < _max_regions, "Tried to access invalid region %u" , region); |
| 206 | if (_iter_states[region] != Unclaimed) { |
| 207 | return false; |
| 208 | } |
| 209 | G1RemsetIterState res = Atomic::cmpxchg(Claimed, &_iter_states[region], Unclaimed); |
| 210 | return (res == Unclaimed); |
| 211 | } |
| 212 | |
| 213 | // Try to atomically sets the iteration state to "complete". Returns true for the |
| 214 | // thread that caused the transition. |
| 215 | inline bool set_iter_complete(uint region) { |
| 216 | if (iter_is_complete(region)) { |
| 217 | return false; |
| 218 | } |
| 219 | G1RemsetIterState res = Atomic::cmpxchg(Complete, &_iter_states[region], Claimed); |
| 220 | return (res == Claimed); |
| 221 | } |
| 222 | |
| 223 | // Returns true if the region's iteration is complete. |
| 224 | inline bool iter_is_complete(uint region) const { |
| 225 | assert(region < _max_regions, "Tried to access invalid region %u" , region); |
| 226 | return _iter_states[region] == Complete; |
| 227 | } |
| 228 | |
| 229 | // The current position within the remembered set of the given region. |
| 230 | inline size_t iter_claimed(uint region) const { |
| 231 | assert(region < _max_regions, "Tried to access invalid region %u" , region); |
| 232 | return _iter_claims[region]; |
| 233 | } |
| 234 | |
| 235 | // Claim the next block of cards within the remembered set of the region with |
| 236 | // step size. |
| 237 | inline size_t iter_claimed_next(uint region, size_t step) { |
| 238 | return Atomic::add(step, &_iter_claims[region]) - step; |
| 239 | } |
| 240 | |
| 241 | void add_dirty_region(uint region) { |
| 242 | if (_in_dirty_region_buffer[region]) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | if (!Atomic::cmpxchg(true, &_in_dirty_region_buffer[region], false)) { |
| 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 | void clear_scan_top(uint region_idx) { |
| 257 | _scan_top[region_idx] = NULL; |
| 258 | } |
| 259 | |
| 260 | // Clear the card table of "dirty" regions. |
| 261 | void clear_card_table(WorkGang* workers) { |
| 262 | if (_cur_dirty_region == 0) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | size_t const num_chunks = align_up(_cur_dirty_region * HeapRegion::CardsPerRegion, G1ClearCardTableTask::chunk_size()) / G1ClearCardTableTask::chunk_size(); |
| 267 | uint const num_workers = (uint)MIN2(num_chunks, (size_t)workers->active_workers()); |
| 268 | size_t const chunk_length = G1ClearCardTableTask::chunk_size() / HeapRegion::CardsPerRegion; |
| 269 | |
| 270 | // Iterate over the dirty cards region list. |
| 271 | G1ClearCardTableTask cl(G1CollectedHeap::heap(), _dirty_region_buffer, _cur_dirty_region, chunk_length); |
| 272 | |
| 273 | log_debug(gc, ergo)("Running %s using %u workers for " SIZE_FORMAT " " |
| 274 | "units of work for " SIZE_FORMAT " regions." , |
| 275 | cl.name(), num_workers, num_chunks, _cur_dirty_region); |
| 276 | workers->run_task(&cl, num_workers); |
| 277 | |
| 278 | #ifndef PRODUCT |
| 279 | G1CollectedHeap::heap()->verifier()->verify_card_table_cleanup(); |
| 280 | #endif |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | G1RemSet::G1RemSet(G1CollectedHeap* g1h, |
| 285 | G1CardTable* ct, |
| 286 | G1HotCardCache* hot_card_cache) : |
| 287 | _scan_state(new G1RemSetScanState()), |
| 288 | _prev_period_summary(), |
| 289 | _g1h(g1h), |
| 290 | _num_conc_refined_cards(0), |
| 291 | _ct(ct), |
| 292 | _g1p(_g1h->policy()), |
| 293 | _hot_card_cache(hot_card_cache) { |
| 294 | } |
| 295 | |
| 296 | G1RemSet::~G1RemSet() { |
| 297 | if (_scan_state != NULL) { |
| 298 | delete _scan_state; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | uint G1RemSet::num_par_rem_sets() { |
| 303 | return G1DirtyCardQueueSet::num_par_ids() + G1ConcurrentRefine::max_num_threads() + MAX2(ConcGCThreads, ParallelGCThreads); |
| 304 | } |
| 305 | |
| 306 | void G1RemSet::initialize(size_t capacity, uint max_regions) { |
| 307 | G1FromCardCache::initialize(num_par_rem_sets(), max_regions); |
| 308 | _scan_state->initialize(max_regions); |
| 309 | } |
| 310 | |
| 311 | class G1ScanRSForRegionClosure : public HeapRegionClosure { |
| 312 | G1CollectedHeap* _g1h; |
| 313 | G1CardTable *_ct; |
| 314 | |
| 315 | G1ParScanThreadState* _pss; |
| 316 | G1ScanCardClosure* _scan_objs_on_card_cl; |
| 317 | |
| 318 | G1RemSetScanState* _scan_state; |
| 319 | |
| 320 | G1GCPhaseTimes::GCParPhases _phase; |
| 321 | |
| 322 | uint _worker_i; |
| 323 | |
| 324 | size_t _opt_refs_scanned; |
| 325 | size_t _opt_refs_memory_used; |
| 326 | |
| 327 | size_t _cards_scanned; |
| 328 | size_t _cards_claimed; |
| 329 | size_t _cards_skipped; |
| 330 | |
| 331 | Tickspan _rem_set_root_scan_time; |
| 332 | Tickspan _rem_set_trim_partially_time; |
| 333 | |
| 334 | Tickspan _strong_code_root_scan_time; |
| 335 | Tickspan _strong_code_trim_partially_time; |
| 336 | |
| 337 | void claim_card(size_t card_index, const uint region_idx_for_card) { |
| 338 | _ct->set_card_claimed(card_index); |
| 339 | _scan_state->add_dirty_region(region_idx_for_card); |
| 340 | } |
| 341 | |
| 342 | void scan_card(MemRegion mr, uint region_idx_for_card) { |
| 343 | HeapRegion* const card_region = _g1h->region_at(region_idx_for_card); |
| 344 | assert(!card_region->is_young(), "Should not scan card in young region %u" , region_idx_for_card); |
| 345 | card_region->oops_on_card_seq_iterate_careful<true>(mr, _scan_objs_on_card_cl); |
| 346 | _scan_objs_on_card_cl->trim_queue_partially(); |
| 347 | _cards_scanned++; |
| 348 | } |
| 349 | |
| 350 | void scan_opt_rem_set_roots(HeapRegion* r) { |
| 351 | EventGCPhaseParallel event; |
| 352 | |
| 353 | G1OopStarChunkedList* opt_rem_set_list = _pss->oops_into_optional_region(r); |
| 354 | |
| 355 | G1ScanCardClosure scan_cl(_g1h, _pss); |
| 356 | G1ScanRSForOptionalClosure cl(_g1h, &scan_cl); |
| 357 | _opt_refs_scanned += opt_rem_set_list->oops_do(&cl, _pss->closures()->raw_strong_oops()); |
| 358 | _opt_refs_memory_used += opt_rem_set_list->used_memory(); |
| 359 | |
| 360 | event.commit(GCId::current(), _worker_i, G1GCPhaseTimes::phase_name(_phase)); |
| 361 | } |
| 362 | |
| 363 | void scan_rem_set_roots(HeapRegion* r) { |
| 364 | EventGCPhaseParallel event; |
| 365 | uint const region_idx = r->hrm_index(); |
| 366 | |
| 367 | if (_scan_state->claim_iter(region_idx)) { |
| 368 | // If we ever free the collection set concurrently, we should also |
| 369 | // clear the card table concurrently therefore we won't need to |
| 370 | // add regions of the collection set to the dirty cards region. |
| 371 | _scan_state->add_dirty_region(region_idx); |
| 372 | } |
| 373 | |
| 374 | if (r->rem_set()->cardset_is_empty()) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | // We claim cards in blocks so as to reduce the contention. |
| 379 | size_t const block_size = G1RSetScanBlockSize; |
| 380 | |
| 381 | HeapRegionRemSetIterator iter(r->rem_set()); |
| 382 | size_t card_index; |
| 383 | |
| 384 | size_t claimed_card_block = _scan_state->iter_claimed_next(region_idx, block_size); |
| 385 | for (size_t current_card = 0; iter.has_next(card_index); current_card++) { |
| 386 | if (current_card >= claimed_card_block + block_size) { |
| 387 | claimed_card_block = _scan_state->iter_claimed_next(region_idx, block_size); |
| 388 | } |
| 389 | if (current_card < claimed_card_block) { |
| 390 | _cards_skipped++; |
| 391 | continue; |
| 392 | } |
| 393 | _cards_claimed++; |
| 394 | |
| 395 | HeapWord* const card_start = _g1h->bot()->address_for_index_raw(card_index); |
| 396 | uint const region_idx_for_card = _g1h->addr_to_region(card_start); |
| 397 | |
| 398 | #ifdef ASSERT |
| 399 | HeapRegion* hr = _g1h->region_at_or_null(region_idx_for_card); |
| 400 | assert(hr == NULL || hr->is_in_reserved(card_start), |
| 401 | "Card start " PTR_FORMAT " to scan outside of region %u" , p2i(card_start), _g1h->region_at(region_idx_for_card)->hrm_index()); |
| 402 | #endif |
| 403 | HeapWord* const top = _scan_state->scan_top(region_idx_for_card); |
| 404 | if (card_start >= top) { |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | // If the card is dirty, then G1 will scan it during Update RS. |
| 409 | if (_ct->is_card_claimed(card_index) || _ct->is_card_dirty(card_index)) { |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | // We claim lazily (so races are possible but they're benign), which reduces the |
| 414 | // number of duplicate scans (the rsets of the regions in the cset can intersect). |
| 415 | // Claim the card after checking bounds above: the remembered set may contain |
| 416 | // random cards into current survivor, and we would then have an incorrectly |
| 417 | // claimed card in survivor space. Card table clear does not reset the card table |
| 418 | // of survivor space regions. |
| 419 | claim_card(card_index, region_idx_for_card); |
| 420 | |
| 421 | MemRegion const mr(card_start, MIN2(card_start + BOTConstants::N_words, top)); |
| 422 | |
| 423 | scan_card(mr, region_idx_for_card); |
| 424 | } |
| 425 | event.commit(GCId::current(), _worker_i, G1GCPhaseTimes::phase_name(_phase)); |
| 426 | } |
| 427 | |
| 428 | void scan_strong_code_roots(HeapRegion* r) { |
| 429 | EventGCPhaseParallel event; |
| 430 | // We pass a weak code blobs closure to the remembered set scanning because we want to avoid |
| 431 | // treating the nmethods visited to act as roots for concurrent marking. |
| 432 | // We only want to make sure that the oops in the nmethods are adjusted with regard to the |
| 433 | // objects copied by the current evacuation. |
| 434 | r->strong_code_roots_do(_pss->closures()->weak_codeblobs()); |
| 435 | event.commit(GCId::current(), _worker_i, G1GCPhaseTimes::phase_name(G1GCPhaseTimes::CodeRoots)); |
| 436 | } |
| 437 | |
| 438 | public: |
| 439 | G1ScanRSForRegionClosure(G1RemSetScanState* scan_state, |
| 440 | G1ScanCardClosure* scan_obj_on_card, |
| 441 | G1ParScanThreadState* pss, |
| 442 | G1GCPhaseTimes::GCParPhases phase, |
| 443 | uint worker_i) : |
| 444 | _g1h(G1CollectedHeap::heap()), |
| 445 | _ct(_g1h->card_table()), |
| 446 | _pss(pss), |
| 447 | _scan_objs_on_card_cl(scan_obj_on_card), |
| 448 | _scan_state(scan_state), |
| 449 | _phase(phase), |
| 450 | _worker_i(worker_i), |
| 451 | _opt_refs_scanned(0), |
| 452 | _opt_refs_memory_used(0), |
| 453 | _cards_scanned(0), |
| 454 | _cards_claimed(0), |
| 455 | _cards_skipped(0), |
| 456 | _rem_set_root_scan_time(), |
| 457 | _rem_set_trim_partially_time(), |
| 458 | _strong_code_root_scan_time(), |
| 459 | _strong_code_trim_partially_time() { } |
| 460 | |
| 461 | bool do_heap_region(HeapRegion* r) { |
| 462 | assert(r->in_collection_set(), "Region %u is not in the collection set." , r->hrm_index()); |
| 463 | uint const region_idx = r->hrm_index(); |
| 464 | |
| 465 | // The individual references for the optional remembered set are per-worker, so we |
| 466 | // always need to scan them. |
| 467 | if (r->has_index_in_opt_cset()) { |
| 468 | G1EvacPhaseWithTrimTimeTracker timer(_pss, _rem_set_root_scan_time, _rem_set_trim_partially_time); |
| 469 | scan_opt_rem_set_roots(r); |
| 470 | } |
| 471 | |
| 472 | // Do an early out if we know we are complete. |
| 473 | if (_scan_state->iter_is_complete(region_idx)) { |
| 474 | return false; |
| 475 | } |
| 476 | |
| 477 | { |
| 478 | G1EvacPhaseWithTrimTimeTracker timer(_pss, _rem_set_root_scan_time, _rem_set_trim_partially_time); |
| 479 | scan_rem_set_roots(r); |
| 480 | } |
| 481 | |
| 482 | if (_scan_state->set_iter_complete(region_idx)) { |
| 483 | G1EvacPhaseWithTrimTimeTracker timer(_pss, _strong_code_root_scan_time, _strong_code_trim_partially_time); |
| 484 | // Scan the strong code root list attached to the current region |
| 485 | scan_strong_code_roots(r); |
| 486 | } |
| 487 | return false; |
| 488 | } |
| 489 | |
| 490 | Tickspan rem_set_root_scan_time() const { return _rem_set_root_scan_time; } |
| 491 | Tickspan rem_set_trim_partially_time() const { return _rem_set_trim_partially_time; } |
| 492 | |
| 493 | Tickspan strong_code_root_scan_time() const { return _strong_code_root_scan_time; } |
| 494 | Tickspan strong_code_root_trim_partially_time() const { return _strong_code_trim_partially_time; } |
| 495 | |
| 496 | size_t cards_scanned() const { return _cards_scanned; } |
| 497 | size_t cards_claimed() const { return _cards_claimed; } |
| 498 | size_t cards_skipped() const { return _cards_skipped; } |
| 499 | |
| 500 | size_t opt_refs_scanned() const { return _opt_refs_scanned; } |
| 501 | size_t opt_refs_memory_used() const { return _opt_refs_memory_used; } |
| 502 | }; |
| 503 | |
| 504 | void G1RemSet::scan_rem_set(G1ParScanThreadState* pss, |
| 505 | uint worker_i, |
| 506 | G1GCPhaseTimes::GCParPhases scan_phase, |
| 507 | G1GCPhaseTimes::GCParPhases objcopy_phase, |
| 508 | G1GCPhaseTimes::GCParPhases coderoots_phase) { |
| 509 | assert(pss->trim_ticks().value() == 0, "Queues must have been trimmed before entering." ); |
| 510 | |
| 511 | G1ScanCardClosure scan_cl(_g1h, pss); |
| 512 | G1ScanRSForRegionClosure cl(_scan_state, &scan_cl, pss, scan_phase, worker_i); |
| 513 | _g1h->collection_set_iterate_increment_from(&cl, worker_i); |
| 514 | |
| 515 | G1GCPhaseTimes* p = _g1p->phase_times(); |
| 516 | |
| 517 | p->record_or_add_time_secs(objcopy_phase, worker_i, cl.rem_set_trim_partially_time().seconds()); |
| 518 | |
| 519 | p->record_or_add_time_secs(scan_phase, worker_i, cl.rem_set_root_scan_time().seconds()); |
| 520 | p->record_or_add_thread_work_item(scan_phase, worker_i, cl.cards_scanned(), G1GCPhaseTimes::ScanRSScannedCards); |
| 521 | p->record_or_add_thread_work_item(scan_phase, worker_i, cl.cards_claimed(), G1GCPhaseTimes::ScanRSClaimedCards); |
| 522 | p->record_or_add_thread_work_item(scan_phase, worker_i, cl.cards_skipped(), G1GCPhaseTimes::ScanRSSkippedCards); |
| 523 | // At this time we only record some metrics for the optional remembered set. |
| 524 | if (scan_phase == G1GCPhaseTimes::OptScanRS) { |
| 525 | p->record_or_add_thread_work_item(scan_phase, worker_i, cl.opt_refs_scanned(), G1GCPhaseTimes::ScanRSScannedOptRefs); |
| 526 | p->record_or_add_thread_work_item(scan_phase, worker_i, cl.opt_refs_memory_used(), G1GCPhaseTimes::ScanRSUsedMemory); |
| 527 | } |
| 528 | |
| 529 | p->record_or_add_time_secs(coderoots_phase, worker_i, cl.strong_code_root_scan_time().seconds()); |
| 530 | p->add_time_secs(objcopy_phase, worker_i, cl.strong_code_root_trim_partially_time().seconds()); |
| 531 | } |
| 532 | |
| 533 | // Closure used for updating rem sets. Only called during an evacuation pause. |
| 534 | class G1RefineCardClosure: public G1CardTableEntryClosure { |
| 535 | G1RemSet* _g1rs; |
| 536 | G1ScanCardClosure* _update_rs_cl; |
| 537 | |
| 538 | size_t _cards_scanned; |
| 539 | size_t _cards_skipped; |
| 540 | public: |
| 541 | G1RefineCardClosure(G1CollectedHeap* g1h, G1ScanCardClosure* update_rs_cl) : |
| 542 | _g1rs(g1h->rem_set()), _update_rs_cl(update_rs_cl), _cards_scanned(0), _cards_skipped(0) |
| 543 | {} |
| 544 | |
| 545 | bool do_card_ptr(CardValue* card_ptr, uint worker_i) { |
| 546 | // The only time we care about recording cards that |
| 547 | // contain references that point into the collection set |
| 548 | // is during RSet updating within an evacuation pause. |
| 549 | // In this case worker_i should be the id of a GC worker thread. |
| 550 | assert(SafepointSynchronize::is_at_safepoint(), "not during an evacuation pause" ); |
| 551 | |
| 552 | bool card_scanned = _g1rs->refine_card_during_gc(card_ptr, _update_rs_cl); |
| 553 | |
| 554 | if (card_scanned) { |
| 555 | _update_rs_cl->trim_queue_partially(); |
| 556 | _cards_scanned++; |
| 557 | } else { |
| 558 | _cards_skipped++; |
| 559 | } |
| 560 | return true; |
| 561 | } |
| 562 | |
| 563 | size_t cards_scanned() const { return _cards_scanned; } |
| 564 | size_t cards_skipped() const { return _cards_skipped; } |
| 565 | }; |
| 566 | |
| 567 | void G1RemSet::update_rem_set(G1ParScanThreadState* pss, uint worker_i) { |
| 568 | G1GCPhaseTimes* p = _g1p->phase_times(); |
| 569 | |
| 570 | // Apply closure to log entries in the HCC. |
| 571 | if (G1HotCardCache::default_use_cache()) { |
| 572 | G1EvacPhaseTimesTracker x(p, pss, G1GCPhaseTimes::ScanHCC, worker_i); |
| 573 | |
| 574 | G1ScanCardClosure scan_hcc_cl(_g1h, pss); |
| 575 | G1RefineCardClosure refine_card_cl(_g1h, &scan_hcc_cl); |
| 576 | _g1h->iterate_hcc_closure(&refine_card_cl, worker_i); |
| 577 | } |
| 578 | |
| 579 | // Now apply the closure to all remaining log entries. |
| 580 | { |
| 581 | G1EvacPhaseTimesTracker x(p, pss, G1GCPhaseTimes::UpdateRS, worker_i); |
| 582 | |
| 583 | G1ScanCardClosure update_rs_cl(_g1h, pss); |
| 584 | G1RefineCardClosure refine_card_cl(_g1h, &update_rs_cl); |
| 585 | _g1h->iterate_dirty_card_closure(&refine_card_cl, worker_i); |
| 586 | |
| 587 | p->record_thread_work_item(G1GCPhaseTimes::UpdateRS, worker_i, refine_card_cl.cards_scanned(), G1GCPhaseTimes::UpdateRSScannedCards); |
| 588 | p->record_thread_work_item(G1GCPhaseTimes::UpdateRS, worker_i, refine_card_cl.cards_skipped(), G1GCPhaseTimes::UpdateRSSkippedCards); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | void G1RemSet::prepare_for_scan_rem_set() { |
| 593 | G1BarrierSet::dirty_card_queue_set().concatenate_logs(); |
| 594 | _scan_state->reset(); |
| 595 | } |
| 596 | |
| 597 | void G1RemSet::prepare_for_scan_rem_set(uint region_idx) { |
| 598 | _scan_state->clear_scan_top(region_idx); |
| 599 | } |
| 600 | |
| 601 | void G1RemSet::cleanup_after_scan_rem_set() { |
| 602 | G1GCPhaseTimes* phase_times = _g1h->phase_times(); |
| 603 | |
| 604 | // Set all cards back to clean. |
| 605 | double start = os::elapsedTime(); |
| 606 | _scan_state->clear_card_table(_g1h->workers()); |
| 607 | phase_times->record_clear_ct_time((os::elapsedTime() - start) * 1000.0); |
| 608 | } |
| 609 | |
| 610 | inline void check_card_ptr(CardTable::CardValue* card_ptr, G1CardTable* ct) { |
| 611 | #ifdef ASSERT |
| 612 | G1CollectedHeap* g1h = G1CollectedHeap::heap(); |
| 613 | assert(g1h->is_in_exact(ct->addr_for(card_ptr)), |
| 614 | "Card at " PTR_FORMAT " index " SIZE_FORMAT " representing heap at " PTR_FORMAT " (%u) must be in committed heap" , |
| 615 | p2i(card_ptr), |
| 616 | ct->index_for(ct->addr_for(card_ptr)), |
| 617 | p2i(ct->addr_for(card_ptr)), |
| 618 | g1h->addr_to_region(ct->addr_for(card_ptr))); |
| 619 | #endif |
| 620 | } |
| 621 | |
| 622 | void G1RemSet::refine_card_concurrently(CardValue* card_ptr, |
| 623 | uint worker_i) { |
| 624 | assert(!_g1h->is_gc_active(), "Only call concurrently" ); |
| 625 | |
| 626 | // Construct the region representing the card. |
| 627 | HeapWord* start = _ct->addr_for(card_ptr); |
| 628 | // And find the region containing it. |
| 629 | HeapRegion* r = _g1h->heap_region_containing_or_null(start); |
| 630 | |
| 631 | // If this is a (stale) card into an uncommitted region, exit. |
| 632 | if (r == NULL) { |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | check_card_ptr(card_ptr, _ct); |
| 637 | |
| 638 | // If the card is no longer dirty, nothing to do. |
| 639 | if (*card_ptr != G1CardTable::dirty_card_val()) { |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | // This check is needed for some uncommon cases where we should |
| 644 | // ignore the card. |
| 645 | // |
| 646 | // The region could be young. Cards for young regions are |
| 647 | // distinctly marked (set to g1_young_gen), so the post-barrier will |
| 648 | // filter them out. However, that marking is performed |
| 649 | // concurrently. A write to a young object could occur before the |
| 650 | // card has been marked young, slipping past the filter. |
| 651 | // |
| 652 | // The card could be stale, because the region has been freed since |
| 653 | // the card was recorded. In this case the region type could be |
| 654 | // anything. If (still) free or (reallocated) young, just ignore |
| 655 | // it. If (reallocated) old or humongous, the later card trimming |
| 656 | // and additional checks in iteration may detect staleness. At |
| 657 | // worst, we end up processing a stale card unnecessarily. |
| 658 | // |
| 659 | // In the normal (non-stale) case, the synchronization between the |
| 660 | // enqueueing of the card and processing it here will have ensured |
| 661 | // we see the up-to-date region type here. |
| 662 | if (!r->is_old_or_humongous_or_archive()) { |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | // The result from the hot card cache insert call is either: |
| 667 | // * pointer to the current card |
| 668 | // (implying that the current card is not 'hot'), |
| 669 | // * null |
| 670 | // (meaning we had inserted the card ptr into the "hot" card cache, |
| 671 | // which had some headroom), |
| 672 | // * a pointer to a "hot" card that was evicted from the "hot" cache. |
| 673 | // |
| 674 | |
| 675 | if (_hot_card_cache->use_cache()) { |
| 676 | assert(!SafepointSynchronize::is_at_safepoint(), "sanity" ); |
| 677 | |
| 678 | const CardValue* orig_card_ptr = card_ptr; |
| 679 | card_ptr = _hot_card_cache->insert(card_ptr); |
| 680 | if (card_ptr == NULL) { |
| 681 | // There was no eviction. Nothing to do. |
| 682 | return; |
| 683 | } else if (card_ptr != orig_card_ptr) { |
| 684 | // Original card was inserted and an old card was evicted. |
| 685 | start = _ct->addr_for(card_ptr); |
| 686 | r = _g1h->heap_region_containing(start); |
| 687 | |
| 688 | // Check whether the region formerly in the cache should be |
| 689 | // ignored, as discussed earlier for the original card. The |
| 690 | // region could have been freed while in the cache. |
| 691 | if (!r->is_old_or_humongous_or_archive()) { |
| 692 | return; |
| 693 | } |
| 694 | } // Else we still have the original card. |
| 695 | } |
| 696 | |
| 697 | // Trim the region designated by the card to what's been allocated |
| 698 | // in the region. The card could be stale, or the card could cover |
| 699 | // (part of) an object at the end of the allocated space and extend |
| 700 | // beyond the end of allocation. |
| 701 | |
| 702 | // Non-humongous objects are only allocated in the old-gen during |
| 703 | // GC, so if region is old then top is stable. Humongous object |
| 704 | // allocation sets top last; if top has not yet been set, this is |
| 705 | // a stale card and we'll end up with an empty intersection. If |
| 706 | // this is not a stale card, the synchronization between the |
| 707 | // enqueuing of the card and processing it here will have ensured |
| 708 | // we see the up-to-date top here. |
| 709 | HeapWord* scan_limit = r->top(); |
| 710 | |
| 711 | if (scan_limit <= start) { |
| 712 | // If the trimmed region is empty, the card must be stale. |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | // Okay to clean and process the card now. There are still some |
| 717 | // stale card cases that may be detected by iteration and dealt with |
| 718 | // as iteration failure. |
| 719 | *const_cast<volatile CardValue*>(card_ptr) = G1CardTable::clean_card_val(); |
| 720 | |
| 721 | // This fence serves two purposes. First, the card must be cleaned |
| 722 | // before processing the contents. Second, we can't proceed with |
| 723 | // processing until after the read of top, for synchronization with |
| 724 | // possibly concurrent humongous object allocation. It's okay that |
| 725 | // reading top and reading type were racy wrto each other. We need |
| 726 | // both set, in any order, to proceed. |
| 727 | OrderAccess::fence(); |
| 728 | |
| 729 | // Don't use addr_for(card_ptr + 1) which can ask for |
| 730 | // a card beyond the heap. |
| 731 | HeapWord* end = start + G1CardTable::card_size_in_words; |
| 732 | MemRegion dirty_region(start, MIN2(scan_limit, end)); |
| 733 | assert(!dirty_region.is_empty(), "sanity" ); |
| 734 | |
| 735 | G1ConcurrentRefineOopClosure conc_refine_cl(_g1h, worker_i); |
| 736 | if (r->oops_on_card_seq_iterate_careful<false>(dirty_region, &conc_refine_cl)) { |
| 737 | _num_conc_refined_cards++; // Unsynchronized update, only used for logging. |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | // If unable to process the card then we encountered an unparsable |
| 742 | // part of the heap (e.g. a partially allocated object, so only |
| 743 | // temporarily a problem) while processing a stale card. Despite |
| 744 | // the card being stale, we can't simply ignore it, because we've |
| 745 | // already marked the card cleaned, so taken responsibility for |
| 746 | // ensuring the card gets scanned. |
| 747 | // |
| 748 | // However, the card might have gotten re-dirtied and re-enqueued |
| 749 | // while we worked. (In fact, it's pretty likely.) |
| 750 | if (*card_ptr == G1CardTable::dirty_card_val()) { |
| 751 | return; |
| 752 | } |
| 753 | |
| 754 | // Re-dirty the card and enqueue in the *shared* queue. Can't use |
| 755 | // the thread-local queue, because that might be the queue that is |
| 756 | // being processed by us; we could be a Java thread conscripted to |
| 757 | // perform refinement on our queue's current buffer. |
| 758 | *card_ptr = G1CardTable::dirty_card_val(); |
| 759 | G1BarrierSet::shared_dirty_card_queue().enqueue(card_ptr); |
| 760 | } |
| 761 | |
| 762 | bool G1RemSet::refine_card_during_gc(CardValue* card_ptr, |
| 763 | G1ScanCardClosure* update_rs_cl) { |
| 764 | assert(_g1h->is_gc_active(), "Only call during GC" ); |
| 765 | |
| 766 | // Construct the region representing the card. |
| 767 | HeapWord* card_start = _ct->addr_for(card_ptr); |
| 768 | // And find the region containing it. |
| 769 | uint const card_region_idx = _g1h->addr_to_region(card_start); |
| 770 | |
| 771 | HeapWord* scan_limit = _scan_state->scan_top(card_region_idx); |
| 772 | if (scan_limit == NULL) { |
| 773 | // This is a card into an uncommitted region. We need to bail out early as we |
| 774 | // should not access the corresponding card table entry. |
| 775 | return false; |
| 776 | } |
| 777 | |
| 778 | check_card_ptr(card_ptr, _ct); |
| 779 | |
| 780 | // If the card is no longer dirty, nothing to do. This covers cards that were already |
| 781 | // scanned as parts of the remembered sets. |
| 782 | if (*card_ptr != G1CardTable::dirty_card_val()) { |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | // We claim lazily (so races are possible but they're benign), which reduces the |
| 787 | // number of potential duplicate scans (multiple threads may enqueue the same card twice). |
| 788 | *card_ptr = G1CardTable::clean_card_val() | G1CardTable::claimed_card_val(); |
| 789 | |
| 790 | _scan_state->add_dirty_region(card_region_idx); |
| 791 | if (scan_limit <= card_start) { |
| 792 | // If the card starts above the area in the region containing objects to scan, skip it. |
| 793 | return false; |
| 794 | } |
| 795 | |
| 796 | // Don't use addr_for(card_ptr + 1) which can ask for |
| 797 | // a card beyond the heap. |
| 798 | HeapWord* card_end = card_start + G1CardTable::card_size_in_words; |
| 799 | MemRegion dirty_region(card_start, MIN2(scan_limit, card_end)); |
| 800 | assert(!dirty_region.is_empty(), "sanity" ); |
| 801 | |
| 802 | HeapRegion* const card_region = _g1h->region_at(card_region_idx); |
| 803 | assert(!card_region->is_young(), "Should not scan card in young region %u" , card_region_idx); |
| 804 | bool card_processed = card_region->oops_on_card_seq_iterate_careful<true>(dirty_region, update_rs_cl); |
| 805 | assert(card_processed, "must be" ); |
| 806 | return true; |
| 807 | } |
| 808 | |
| 809 | void G1RemSet::print_periodic_summary_info(const char* , uint period_count) { |
| 810 | if ((G1SummarizeRSetStatsPeriod > 0) && log_is_enabled(Trace, gc, remset) && |
| 811 | (period_count % G1SummarizeRSetStatsPeriod == 0)) { |
| 812 | |
| 813 | G1RemSetSummary current(this); |
| 814 | _prev_period_summary.subtract_from(¤t); |
| 815 | |
| 816 | Log(gc, remset) log; |
| 817 | log.trace("%s" , header); |
| 818 | ResourceMark rm; |
| 819 | LogStream ls(log.trace()); |
| 820 | _prev_period_summary.print_on(&ls); |
| 821 | |
| 822 | _prev_period_summary.set(¤t); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | void G1RemSet::print_summary_info() { |
| 827 | Log(gc, remset, exit) log; |
| 828 | if (log.is_trace()) { |
| 829 | log.trace(" Cumulative RS summary" ); |
| 830 | G1RemSetSummary current(this); |
| 831 | ResourceMark rm; |
| 832 | LogStream ls(log.trace()); |
| 833 | current.print_on(&ls); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | class G1RebuildRemSetTask: public AbstractGangTask { |
| 838 | // Aggregate the counting data that was constructed concurrently |
| 839 | // with marking. |
| 840 | class G1RebuildRemSetHeapRegionClosure : public HeapRegionClosure { |
| 841 | G1ConcurrentMark* _cm; |
| 842 | G1RebuildRemSetClosure _update_cl; |
| 843 | |
| 844 | // Applies _update_cl to the references of the given object, limiting objArrays |
| 845 | // to the given MemRegion. Returns the amount of words actually scanned. |
| 846 | size_t scan_for_references(oop const obj, MemRegion mr) { |
| 847 | size_t const obj_size = obj->size(); |
| 848 | // All non-objArrays and objArrays completely within the mr |
| 849 | // can be scanned without passing the mr. |
| 850 | if (!obj->is_objArray() || mr.contains(MemRegion((HeapWord*)obj, obj_size))) { |
| 851 | obj->oop_iterate(&_update_cl); |
| 852 | return obj_size; |
| 853 | } |
| 854 | // This path is for objArrays crossing the given MemRegion. Only scan the |
| 855 | // area within the MemRegion. |
| 856 | obj->oop_iterate(&_update_cl, mr); |
| 857 | return mr.intersection(MemRegion((HeapWord*)obj, obj_size)).word_size(); |
| 858 | } |
| 859 | |
| 860 | // A humongous object is live (with respect to the scanning) either |
| 861 | // a) it is marked on the bitmap as such |
| 862 | // b) its TARS is larger than TAMS, i.e. has been allocated during marking. |
| 863 | bool is_humongous_live(oop const humongous_obj, const G1CMBitMap* const bitmap, HeapWord* tams, HeapWord* tars) const { |
| 864 | return bitmap->is_marked(humongous_obj) || (tars > tams); |
| 865 | } |
| 866 | |
| 867 | // Iterator over the live objects within the given MemRegion. |
| 868 | class LiveObjIterator : public StackObj { |
| 869 | const G1CMBitMap* const _bitmap; |
| 870 | const HeapWord* _tams; |
| 871 | const MemRegion _mr; |
| 872 | HeapWord* _current; |
| 873 | |
| 874 | bool is_below_tams() const { |
| 875 | return _current < _tams; |
| 876 | } |
| 877 | |
| 878 | bool is_live(HeapWord* obj) const { |
| 879 | return !is_below_tams() || _bitmap->is_marked(obj); |
| 880 | } |
| 881 | |
| 882 | HeapWord* bitmap_limit() const { |
| 883 | return MIN2(const_cast<HeapWord*>(_tams), _mr.end()); |
| 884 | } |
| 885 | |
| 886 | void move_if_below_tams() { |
| 887 | if (is_below_tams() && has_next()) { |
| 888 | _current = _bitmap->get_next_marked_addr(_current, bitmap_limit()); |
| 889 | } |
| 890 | } |
| 891 | public: |
| 892 | LiveObjIterator(const G1CMBitMap* const bitmap, const HeapWord* tams, const MemRegion mr, HeapWord* first_oop_into_mr) : |
| 893 | _bitmap(bitmap), |
| 894 | _tams(tams), |
| 895 | _mr(mr), |
| 896 | _current(first_oop_into_mr) { |
| 897 | |
| 898 | assert(_current <= _mr.start(), |
| 899 | "First oop " PTR_FORMAT " should extend into mr [" PTR_FORMAT ", " PTR_FORMAT ")" , |
| 900 | p2i(first_oop_into_mr), p2i(mr.start()), p2i(mr.end())); |
| 901 | |
| 902 | // Step to the next live object within the MemRegion if needed. |
| 903 | if (is_live(_current)) { |
| 904 | // Non-objArrays were scanned by the previous part of that region. |
| 905 | if (_current < mr.start() && !oop(_current)->is_objArray()) { |
| 906 | _current += oop(_current)->size(); |
| 907 | // We might have positioned _current on a non-live object. Reposition to the next |
| 908 | // live one if needed. |
| 909 | move_if_below_tams(); |
| 910 | } |
| 911 | } else { |
| 912 | // The object at _current can only be dead if below TAMS, so we can use the bitmap. |
| 913 | // immediately. |
| 914 | _current = _bitmap->get_next_marked_addr(_current, bitmap_limit()); |
| 915 | assert(_current == _mr.end() || is_live(_current), |
| 916 | "Current " PTR_FORMAT " should be live (%s) or beyond the end of the MemRegion (" PTR_FORMAT ")" , |
| 917 | p2i(_current), BOOL_TO_STR(is_live(_current)), p2i(_mr.end())); |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | void move_to_next() { |
| 922 | _current += next()->size(); |
| 923 | move_if_below_tams(); |
| 924 | } |
| 925 | |
| 926 | oop next() const { |
| 927 | oop result = oop(_current); |
| 928 | assert(is_live(_current), |
| 929 | "Object " PTR_FORMAT " must be live TAMS " PTR_FORMAT " below %d mr " PTR_FORMAT " " PTR_FORMAT " outside %d" , |
| 930 | p2i(_current), p2i(_tams), _tams > _current, p2i(_mr.start()), p2i(_mr.end()), _mr.contains(result)); |
| 931 | return result; |
| 932 | } |
| 933 | |
| 934 | bool has_next() const { |
| 935 | return _current < _mr.end(); |
| 936 | } |
| 937 | }; |
| 938 | |
| 939 | // Rebuild remembered sets in the part of the region specified by mr and hr. |
| 940 | // Objects between the bottom of the region and the TAMS are checked for liveness |
| 941 | // using the given bitmap. Objects between TAMS and TARS are assumed to be live. |
| 942 | // Returns the number of live words between bottom and TAMS. |
| 943 | size_t rebuild_rem_set_in_region(const G1CMBitMap* const bitmap, |
| 944 | HeapWord* const top_at_mark_start, |
| 945 | HeapWord* const top_at_rebuild_start, |
| 946 | HeapRegion* hr, |
| 947 | MemRegion mr) { |
| 948 | size_t marked_words = 0; |
| 949 | |
| 950 | if (hr->is_humongous()) { |
| 951 | oop const humongous_obj = oop(hr->humongous_start_region()->bottom()); |
| 952 | if (is_humongous_live(humongous_obj, bitmap, top_at_mark_start, top_at_rebuild_start)) { |
| 953 | // We need to scan both [bottom, TAMS) and [TAMS, top_at_rebuild_start); |
| 954 | // however in case of humongous objects it is sufficient to scan the encompassing |
| 955 | // area (top_at_rebuild_start is always larger or equal to TAMS) as one of the |
| 956 | // two areas will be zero sized. I.e. TAMS is either |
| 957 | // the same as bottom or top(_at_rebuild_start). There is no way TAMS has a different |
| 958 | // value: this would mean that TAMS points somewhere into the object. |
| 959 | assert(hr->top() == top_at_mark_start || hr->top() == top_at_rebuild_start, |
| 960 | "More than one object in the humongous region?" ); |
| 961 | humongous_obj->oop_iterate(&_update_cl, mr); |
| 962 | return top_at_mark_start != hr->bottom() ? mr.intersection(MemRegion((HeapWord*)humongous_obj, humongous_obj->size())).byte_size() : 0; |
| 963 | } else { |
| 964 | return 0; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | for (LiveObjIterator it(bitmap, top_at_mark_start, mr, hr->block_start(mr.start())); it.has_next(); it.move_to_next()) { |
| 969 | oop obj = it.next(); |
| 970 | size_t scanned_size = scan_for_references(obj, mr); |
| 971 | if ((HeapWord*)obj < top_at_mark_start) { |
| 972 | marked_words += scanned_size; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | return marked_words * HeapWordSize; |
| 977 | } |
| 978 | public: |
| 979 | G1RebuildRemSetHeapRegionClosure(G1CollectedHeap* g1h, |
| 980 | G1ConcurrentMark* cm, |
| 981 | uint worker_id) : |
| 982 | HeapRegionClosure(), |
| 983 | _cm(cm), |
| 984 | _update_cl(g1h, worker_id) { } |
| 985 | |
| 986 | bool do_heap_region(HeapRegion* hr) { |
| 987 | if (_cm->has_aborted()) { |
| 988 | return true; |
| 989 | } |
| 990 | |
| 991 | uint const region_idx = hr->hrm_index(); |
| 992 | DEBUG_ONLY(HeapWord* const top_at_rebuild_start_check = _cm->top_at_rebuild_start(region_idx);) |
| 993 | assert(top_at_rebuild_start_check == NULL || |
| 994 | top_at_rebuild_start_check > hr->bottom(), |
| 995 | "A TARS (" PTR_FORMAT ") == bottom() (" PTR_FORMAT ") indicates the old region %u is empty (%s)" , |
| 996 | p2i(top_at_rebuild_start_check), p2i(hr->bottom()), region_idx, hr->get_type_str()); |
| 997 | |
| 998 | size_t total_marked_bytes = 0; |
| 999 | size_t const chunk_size_in_words = G1RebuildRemSetChunkSize / HeapWordSize; |
| 1000 | |
| 1001 | HeapWord* const top_at_mark_start = hr->prev_top_at_mark_start(); |
| 1002 | |
| 1003 | HeapWord* cur = hr->bottom(); |
| 1004 | while (cur < hr->end()) { |
| 1005 | // After every iteration (yield point) we need to check whether the region's |
| 1006 | // TARS changed due to e.g. eager reclaim. |
| 1007 | HeapWord* const top_at_rebuild_start = _cm->top_at_rebuild_start(region_idx); |
| 1008 | if (top_at_rebuild_start == NULL) { |
| 1009 | return false; |
| 1010 | } |
| 1011 | |
| 1012 | MemRegion next_chunk = MemRegion(hr->bottom(), top_at_rebuild_start).intersection(MemRegion(cur, chunk_size_in_words)); |
| 1013 | if (next_chunk.is_empty()) { |
| 1014 | break; |
| 1015 | } |
| 1016 | |
| 1017 | const Ticks start = Ticks::now(); |
| 1018 | size_t marked_bytes = rebuild_rem_set_in_region(_cm->prev_mark_bitmap(), |
| 1019 | top_at_mark_start, |
| 1020 | top_at_rebuild_start, |
| 1021 | hr, |
| 1022 | next_chunk); |
| 1023 | Tickspan time = Ticks::now() - start; |
| 1024 | |
| 1025 | log_trace(gc, remset, tracking)("Rebuilt region %u " |
| 1026 | "live " SIZE_FORMAT " " |
| 1027 | "time %.3fms " |
| 1028 | "marked bytes " SIZE_FORMAT " " |
| 1029 | "bot " PTR_FORMAT " " |
| 1030 | "TAMS " PTR_FORMAT " " |
| 1031 | "TARS " PTR_FORMAT, |
| 1032 | region_idx, |
| 1033 | _cm->liveness(region_idx) * HeapWordSize, |
| 1034 | time.seconds() * 1000.0, |
| 1035 | marked_bytes, |
| 1036 | p2i(hr->bottom()), |
| 1037 | p2i(top_at_mark_start), |
| 1038 | p2i(top_at_rebuild_start)); |
| 1039 | |
| 1040 | if (marked_bytes > 0) { |
| 1041 | total_marked_bytes += marked_bytes; |
| 1042 | } |
| 1043 | cur += chunk_size_in_words; |
| 1044 | |
| 1045 | _cm->do_yield_check(); |
| 1046 | if (_cm->has_aborted()) { |
| 1047 | return true; |
| 1048 | } |
| 1049 | } |
| 1050 | // In the final iteration of the loop the region might have been eagerly reclaimed. |
| 1051 | // Simply filter out those regions. We can not just use region type because there |
| 1052 | // might have already been new allocations into these regions. |
| 1053 | DEBUG_ONLY(HeapWord* const top_at_rebuild_start = _cm->top_at_rebuild_start(region_idx);) |
| 1054 | assert(top_at_rebuild_start == NULL || |
| 1055 | total_marked_bytes == hr->marked_bytes(), |
| 1056 | "Marked bytes " SIZE_FORMAT " for region %u (%s) in [bottom, TAMS) do not match calculated marked bytes " SIZE_FORMAT " " |
| 1057 | "(" PTR_FORMAT " " PTR_FORMAT " " PTR_FORMAT ")" , |
| 1058 | total_marked_bytes, hr->hrm_index(), hr->get_type_str(), hr->marked_bytes(), |
| 1059 | p2i(hr->bottom()), p2i(top_at_mark_start), p2i(top_at_rebuild_start)); |
| 1060 | // Abort state may have changed after the yield check. |
| 1061 | return _cm->has_aborted(); |
| 1062 | } |
| 1063 | }; |
| 1064 | |
| 1065 | HeapRegionClaimer _hr_claimer; |
| 1066 | G1ConcurrentMark* _cm; |
| 1067 | |
| 1068 | uint _worker_id_offset; |
| 1069 | public: |
| 1070 | G1RebuildRemSetTask(G1ConcurrentMark* cm, |
| 1071 | uint n_workers, |
| 1072 | uint worker_id_offset) : |
| 1073 | AbstractGangTask("G1 Rebuild Remembered Set" ), |
| 1074 | _hr_claimer(n_workers), |
| 1075 | _cm(cm), |
| 1076 | _worker_id_offset(worker_id_offset) { |
| 1077 | } |
| 1078 | |
| 1079 | void work(uint worker_id) { |
| 1080 | SuspendibleThreadSetJoiner sts_join; |
| 1081 | |
| 1082 | G1CollectedHeap* g1h = G1CollectedHeap::heap(); |
| 1083 | |
| 1084 | G1RebuildRemSetHeapRegionClosure cl(g1h, _cm, _worker_id_offset + worker_id); |
| 1085 | g1h->heap_region_par_iterate_from_worker_offset(&cl, &_hr_claimer, worker_id); |
| 1086 | } |
| 1087 | }; |
| 1088 | |
| 1089 | void G1RemSet::rebuild_rem_set(G1ConcurrentMark* cm, |
| 1090 | WorkGang* workers, |
| 1091 | uint worker_id_offset) { |
| 1092 | uint num_workers = workers->active_workers(); |
| 1093 | |
| 1094 | G1RebuildRemSetTask cl(cm, |
| 1095 | num_workers, |
| 1096 | worker_id_offset); |
| 1097 | workers->run_task(&cl, num_workers); |
| 1098 | } |
| 1099 | |