1 /* 2 * Copyright (c) 2014, 2021, 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/g1Allocator.inline.hpp" 27 #include "gc/g1/g1CollectedHeap.inline.hpp" 28 #include "gc/g1/g1CollectionSet.hpp" 29 #include "gc/g1/g1OopClosures.inline.hpp" 30 #include "gc/g1/g1ParScanThreadState.inline.hpp" 31 #include "gc/g1/g1RootClosures.hpp" 32 #include "gc/g1/g1StringDedup.hpp" 33 #include "gc/g1/g1Trace.hpp" 34 #include "gc/shared/partialArrayTaskStepper.inline.hpp" 35 #include "gc/shared/stringdedup/stringDedup.hpp" 36 #include "gc/shared/taskqueue.inline.hpp" 37 #include "memory/allocation.inline.hpp" 38 #include "oops/access.inline.hpp" 39 #include "oops/oop.inline.hpp" 40 #include "runtime/atomic.hpp" 41 #include "runtime/prefetch.inline.hpp" 42 #include "utilities/globalDefinitions.hpp" 43 #include "utilities/macros.hpp" 44 45 // In fastdebug builds the code size can get out of hand, potentially 46 // tripping over compiler limits (which may be bugs, but nevertheless 47 // need to be taken into consideration). A side benefit of limiting 48 // inlining is that we get more call frames that might aid debugging. 49 // And the fastdebug compile time for this file is much reduced. 50 // Explicit NOINLINE to block ATTRIBUTE_FLATTENing. 51 #define MAYBE_INLINE_EVACUATION NOT_DEBUG(inline) DEBUG_ONLY(NOINLINE) 52 53 G1ParScanThreadState::G1ParScanThreadState(G1CollectedHeap* g1h, 54 G1RedirtyCardsQueueSet* rdcqs, 55 uint worker_id, 56 uint n_workers, 57 size_t young_cset_length, 58 size_t optional_cset_length) 59 : _g1h(g1h), 60 _task_queue(g1h->task_queue(worker_id)), 61 _rdc_local_qset(rdcqs), 62 _ct(g1h->card_table()), 63 _closures(NULL), 64 _plab_allocator(NULL), 65 _age_table(false), 66 _tenuring_threshold(g1h->policy()->tenuring_threshold()), 67 _scanner(g1h, this), 68 _worker_id(worker_id), 69 _last_enqueued_card(SIZE_MAX), 70 _stack_trim_upper_threshold(GCDrainStackTargetSize * 2 + 1), 71 _stack_trim_lower_threshold(GCDrainStackTargetSize), 72 _trim_ticks(), 73 _surviving_young_words_base(NULL), 74 _surviving_young_words(NULL), 75 _surviving_words_length(young_cset_length + 1), 76 _old_gen_is_full(false), 77 _partial_objarray_chunk_size(ParGCArrayScanChunk), 78 _partial_array_stepper(n_workers), 79 _string_dedup_requests(), 80 _num_optional_regions(optional_cset_length), 81 _numa(g1h->numa()), 82 _obj_alloc_stat(NULL) 83 { 84 // We allocate number of young gen regions in the collection set plus one 85 // entries, since entry 0 keeps track of surviving bytes for non-young regions. 86 // We also add a few elements at the beginning and at the end in 87 // an attempt to eliminate cache contention 88 const size_t padding_elem_num = (DEFAULT_CACHE_LINE_SIZE / sizeof(size_t)); 89 size_t array_length = padding_elem_num + _surviving_words_length + padding_elem_num; 90 91 _surviving_young_words_base = NEW_C_HEAP_ARRAY(size_t, array_length, mtGC); 92 _surviving_young_words = _surviving_young_words_base + padding_elem_num; 93 memset(_surviving_young_words, 0, _surviving_words_length * sizeof(size_t)); 94 95 _plab_allocator = new G1PLABAllocator(_g1h->allocator()); 96 97 // The dest for Young is used when the objects are aged enough to 98 // need to be moved to the next space. 99 _dest[G1HeapRegionAttr::Young] = G1HeapRegionAttr::Old; 100 _dest[G1HeapRegionAttr::Old] = G1HeapRegionAttr::Old; 101 102 _closures = G1EvacuationRootClosures::create_root_closures(this, _g1h); 103 104 _oops_into_optional_regions = new G1OopStarChunkedList[_num_optional_regions]; 105 106 initialize_numa_stats(); 107 } 108 109 size_t G1ParScanThreadState::flush(size_t* surviving_young_words) { 110 _rdc_local_qset.flush(); 111 flush_numa_stats(); 112 // Update allocation statistics. 113 _plab_allocator->flush_and_retire_stats(); 114 _g1h->policy()->record_age_table(&_age_table); 115 116 size_t sum = 0; 117 for (uint i = 0; i < _surviving_words_length; i++) { 118 surviving_young_words[i] += _surviving_young_words[i]; 119 sum += _surviving_young_words[i]; 120 } 121 return sum; 122 } 123 124 G1ParScanThreadState::~G1ParScanThreadState() { 125 delete _plab_allocator; 126 delete _closures; 127 FREE_C_HEAP_ARRAY(size_t, _surviving_young_words_base); 128 delete[] _oops_into_optional_regions; 129 FREE_C_HEAP_ARRAY(size_t, _obj_alloc_stat); 130 } 131 132 size_t G1ParScanThreadState::lab_waste_words() const { 133 return _plab_allocator->waste(); 134 } 135 136 size_t G1ParScanThreadState::lab_undo_waste_words() const { 137 return _plab_allocator->undo_waste(); 138 } 139 140 #ifdef ASSERT 141 void G1ParScanThreadState::verify_task(narrowOop* task) const { 142 assert(task != NULL, "invariant"); 143 assert(UseCompressedOops, "sanity"); 144 oop p = RawAccess<>::oop_load(task); 145 assert(_g1h->is_in_reserved(p), 146 "task=" PTR_FORMAT " p=" PTR_FORMAT, p2i(task), p2i(p)); 147 } 148 149 void G1ParScanThreadState::verify_task(oop* task) const { 150 assert(task != NULL, "invariant"); 151 oop p = RawAccess<>::oop_load(task); 152 assert(_g1h->is_in_reserved(p), 153 "task=" PTR_FORMAT " p=" PTR_FORMAT, p2i(task), p2i(p)); 154 } 155 156 void G1ParScanThreadState::verify_task(PartialArrayScanTask task) const { 157 // Must be in the collection set--it's already been copied. 158 oop p = task.to_source_array(); 159 assert(_g1h->is_in_cset(p), "p=" PTR_FORMAT, p2i(p)); 160 } 161 162 void G1ParScanThreadState::verify_task(ScannerTask task) const { 163 if (task.is_narrow_oop_ptr()) { 164 verify_task(task.to_narrow_oop_ptr()); 165 } else if (task.is_oop_ptr()) { 166 verify_task(task.to_oop_ptr()); 167 } else if (task.is_partial_array_task()) { 168 verify_task(task.to_partial_array_task()); 169 } else { 170 ShouldNotReachHere(); 171 } 172 } 173 #endif // ASSERT 174 175 template <class T> 176 MAYBE_INLINE_EVACUATION 177 void G1ParScanThreadState::do_oop_evac(T* p) { 178 // Reference should not be NULL here as such are never pushed to the task queue. 179 oop obj = RawAccess<IS_NOT_NULL>::oop_load(p); 180 181 // Although we never intentionally push references outside of the collection 182 // set, due to (benign) races in the claim mechanism during RSet scanning more 183 // than one thread might claim the same card. So the same card may be 184 // processed multiple times, and so we might get references into old gen here. 185 // So we need to redo this check. 186 const G1HeapRegionAttr region_attr = _g1h->region_attr(obj); 187 // References pushed onto the work stack should never point to a humongous region 188 // as they are not added to the collection set due to above precondition. 189 assert(!region_attr.is_humongous(), 190 "Obj " PTR_FORMAT " should not refer to humongous region %u from " PTR_FORMAT, 191 p2i(obj), _g1h->addr_to_region(cast_from_oop<HeapWord*>(obj)), p2i(p)); 192 193 if (!region_attr.is_in_cset()) { 194 // In this case somebody else already did all the work. 195 return; 196 } 197 198 markWord m = obj->mark(); 199 if (m.is_marked()) { 200 obj = cast_to_oop(m.decode_pointer()); 201 } else { 202 obj = do_copy_to_survivor_space(region_attr, obj, m); 203 } 204 RawAccess<IS_NOT_NULL>::oop_store(p, obj); 205 206 assert(obj != NULL, "Must be"); 207 if (HeapRegion::is_in_same_region(p, obj)) { 208 return; 209 } 210 HeapRegion* from = _g1h->heap_region_containing(p); 211 if (!from->is_young()) { 212 enqueue_card_if_tracked(_g1h->region_attr(obj), p, obj); 213 } 214 } 215 216 MAYBE_INLINE_EVACUATION 217 void G1ParScanThreadState::do_partial_array(PartialArrayScanTask task) { 218 oop from_obj = task.to_source_array(); 219 220 assert(_g1h->is_in_reserved(from_obj), "must be in heap."); 221 assert(from_obj->is_objArray(), "must be obj array"); 222 assert(from_obj->is_forwarded(), "must be forwarded"); 223 224 oop to_obj = from_obj->forwardee(); 225 assert(from_obj != to_obj, "should not be chunking self-forwarded objects"); 226 assert(to_obj->is_objArray(), "must be obj array"); 227 objArrayOop to_array = objArrayOop(to_obj); 228 229 PartialArrayTaskStepper::Step step 230 = _partial_array_stepper.next(objArrayOop(from_obj), 231 to_array, 232 _partial_objarray_chunk_size); 233 for (uint i = 0; i < step._ncreate; ++i) { 234 push_on_queue(ScannerTask(PartialArrayScanTask(from_obj))); 235 } 236 237 HeapRegion* hr = _g1h->heap_region_containing(to_array); 238 G1ScanInYoungSetter x(&_scanner, hr->is_young()); 239 // Process claimed task. The length of to_array is not correct, but 240 // fortunately the iteration ignores the length field and just relies 241 // on start/end. 242 to_array->oop_iterate_range(&_scanner, 243 step._index, 244 step._index + _partial_objarray_chunk_size); 245 } 246 247 MAYBE_INLINE_EVACUATION 248 void G1ParScanThreadState::start_partial_objarray(G1HeapRegionAttr dest_attr, 249 oop from_obj, 250 oop to_obj) { 251 assert(from_obj->is_objArray(), "precondition"); 252 assert(from_obj->is_forwarded(), "precondition"); 253 assert(from_obj->forwardee() == to_obj, "precondition"); 254 assert(from_obj != to_obj, "should not be scanning self-forwarded objects"); 255 assert(to_obj->is_objArray(), "precondition"); 256 257 objArrayOop to_array = objArrayOop(to_obj); 258 259 PartialArrayTaskStepper::Step step 260 = _partial_array_stepper.start(objArrayOop(from_obj), 261 to_array, 262 _partial_objarray_chunk_size); 263 264 // Push any needed partial scan tasks. Pushed before processing the 265 // intitial chunk to allow other workers to steal while we're processing. 266 for (uint i = 0; i < step._ncreate; ++i) { 267 push_on_queue(ScannerTask(PartialArrayScanTask(from_obj))); 268 } 269 270 G1ScanInYoungSetter x(&_scanner, dest_attr.is_young()); 271 // Process the initial chunk. No need to process the type in the 272 // klass, as it will already be handled by processing the built-in 273 // module. The length of to_array is not correct, but fortunately 274 // the iteration ignores that length field and relies on start/end. 275 to_array->oop_iterate_range(&_scanner, 0, step._index); 276 } 277 278 MAYBE_INLINE_EVACUATION 279 void G1ParScanThreadState::dispatch_task(ScannerTask task) { 280 verify_task(task); 281 if (task.is_narrow_oop_ptr()) { 282 do_oop_evac(task.to_narrow_oop_ptr()); 283 } else if (task.is_oop_ptr()) { 284 do_oop_evac(task.to_oop_ptr()); 285 } else { 286 do_partial_array(task.to_partial_array_task()); 287 } 288 } 289 290 // Process tasks until overflow queue is empty and local queue 291 // contains no more than threshold entries. NOINLINE to prevent 292 // inlining into steal_and_trim_queue. 293 ATTRIBUTE_FLATTEN NOINLINE 294 void G1ParScanThreadState::trim_queue_to_threshold(uint threshold) { 295 ScannerTask task; 296 do { 297 while (_task_queue->pop_overflow(task)) { 298 if (!_task_queue->try_push_to_taskqueue(task)) { 299 dispatch_task(task); 300 } 301 } 302 while (_task_queue->pop_local(task, threshold)) { 303 dispatch_task(task); 304 } 305 } while (!_task_queue->overflow_empty()); 306 } 307 308 ATTRIBUTE_FLATTEN 309 void G1ParScanThreadState::steal_and_trim_queue(G1ScannerTasksQueueSet* task_queues) { 310 ScannerTask stolen_task; 311 while (task_queues->steal(_worker_id, stolen_task)) { 312 dispatch_task(stolen_task); 313 // Processing stolen task may have added tasks to our queue. 314 trim_queue(); 315 } 316 } 317 318 HeapWord* G1ParScanThreadState::allocate_in_next_plab(G1HeapRegionAttr* dest, 319 size_t word_sz, 320 bool previous_plab_refill_failed, 321 uint node_index) { 322 323 assert(dest->is_in_cset_or_humongous(), "Unexpected dest: %s region attr", dest->get_type_str()); 324 325 // Right now we only have two types of regions (young / old) so 326 // let's keep the logic here simple. We can generalize it when necessary. 327 if (dest->is_young()) { 328 bool plab_refill_in_old_failed = false; 329 HeapWord* const obj_ptr = _plab_allocator->allocate(G1HeapRegionAttr::Old, 330 word_sz, 331 &plab_refill_in_old_failed, 332 node_index); 333 // Make sure that we won't attempt to copy any other objects out 334 // of a survivor region (given that apparently we cannot allocate 335 // any new ones) to avoid coming into this slow path again and again. 336 // Only consider failed PLAB refill here: failed inline allocations are 337 // typically large, so not indicative of remaining space. 338 if (previous_plab_refill_failed) { 339 _tenuring_threshold = 0; 340 } 341 342 if (obj_ptr != NULL) { 343 dest->set_old(); 344 } else { 345 // We just failed to allocate in old gen. The same idea as explained above 346 // for making survivor gen unavailable for allocation applies for old gen. 347 _old_gen_is_full = plab_refill_in_old_failed; 348 } 349 return obj_ptr; 350 } else { 351 _old_gen_is_full = previous_plab_refill_failed; 352 assert(dest->is_old(), "Unexpected dest region attr: %s", dest->get_type_str()); 353 // no other space to try. 354 return NULL; 355 } 356 } 357 358 G1HeapRegionAttr G1ParScanThreadState::next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age) { 359 if (region_attr.is_young()) { 360 age = !m.has_displaced_mark_helper() ? m.age() 361 : m.displaced_mark_helper().age(); 362 if (age < _tenuring_threshold) { 363 return region_attr; 364 } 365 } 366 return dest(region_attr); 367 } 368 369 void G1ParScanThreadState::report_promotion_event(G1HeapRegionAttr const dest_attr, 370 oop const old, size_t word_sz, uint age, 371 HeapWord * const obj_ptr, uint node_index) const { 372 PLAB* alloc_buf = _plab_allocator->alloc_buffer(dest_attr, node_index); 373 if (alloc_buf->contains(obj_ptr)) { 374 _g1h->_gc_tracer_stw->report_promotion_in_new_plab_event(old->klass(), word_sz * HeapWordSize, age, 375 dest_attr.type() == G1HeapRegionAttr::Old, 376 alloc_buf->word_sz() * HeapWordSize); 377 } else { 378 _g1h->_gc_tracer_stw->report_promotion_outside_plab_event(old->klass(), word_sz * HeapWordSize, age, 379 dest_attr.type() == G1HeapRegionAttr::Old); 380 } 381 } 382 383 NOINLINE 384 HeapWord* G1ParScanThreadState::allocate_copy_slow(G1HeapRegionAttr* dest_attr, 385 oop old, 386 size_t word_sz, 387 uint age, 388 uint node_index) { 389 HeapWord* obj_ptr = NULL; 390 // Try slow-path allocation unless we're allocating old and old is already full. 391 if (!(dest_attr->is_old() && _old_gen_is_full)) { 392 bool plab_refill_failed = false; 393 obj_ptr = _plab_allocator->allocate_direct_or_new_plab(*dest_attr, 394 word_sz, 395 &plab_refill_failed, 396 node_index); 397 if (obj_ptr == NULL) { 398 obj_ptr = allocate_in_next_plab(dest_attr, 399 word_sz, 400 plab_refill_failed, 401 node_index); 402 } 403 } 404 if (obj_ptr != NULL) { 405 update_numa_stats(node_index); 406 if (_g1h->_gc_tracer_stw->should_report_promotion_events()) { 407 // The events are checked individually as part of the actual commit 408 report_promotion_event(*dest_attr, old, word_sz, age, obj_ptr, node_index); 409 } 410 } 411 return obj_ptr; 412 } 413 414 NOINLINE 415 void G1ParScanThreadState::undo_allocation(G1HeapRegionAttr dest_attr, 416 HeapWord* obj_ptr, 417 size_t word_sz, 418 uint node_index) { 419 _plab_allocator->undo_allocation(dest_attr, obj_ptr, word_sz, node_index); 420 } 421 422 // Private inline function, for direct internal use and providing the 423 // implementation of the public not-inline function. 424 MAYBE_INLINE_EVACUATION 425 oop G1ParScanThreadState::do_copy_to_survivor_space(G1HeapRegionAttr const region_attr, 426 oop const old, 427 markWord const old_mark) { 428 assert(region_attr.is_in_cset(), 429 "Unexpected region attr type: %s", region_attr.get_type_str()); 430 431 // Get the klass once. We'll need it again later, and this avoids 432 // re-decoding when it's compressed. 433 Klass* klass = old->klass(); 434 const size_t word_sz = old->size_given_klass(klass); 435 436 uint age = 0; 437 G1HeapRegionAttr dest_attr = next_region_attr(region_attr, old_mark, age); 438 HeapRegion* const from_region = _g1h->heap_region_containing(old); 439 uint node_index = from_region->node_index(); 440 441 HeapWord* obj_ptr = _plab_allocator->plab_allocate(dest_attr, word_sz, node_index); 442 443 // PLAB allocations should succeed most of the time, so we'll 444 // normally check against NULL once and that's it. 445 if (obj_ptr == NULL) { 446 obj_ptr = allocate_copy_slow(&dest_attr, old, word_sz, age, node_index); 447 if (obj_ptr == NULL) { 448 // This will either forward-to-self, or detect that someone else has 449 // installed a forwarding pointer. 450 return handle_evacuation_failure_par(old, old_mark); 451 } 452 } 453 454 assert(obj_ptr != NULL, "when we get here, allocation should have succeeded"); 455 assert(_g1h->is_in_reserved(obj_ptr), "Allocated memory should be in the heap"); 456 457 #ifndef PRODUCT 458 // Should this evacuation fail? 459 if (_g1h->evacuation_should_fail()) { 460 // Doing this after all the allocation attempts also tests the 461 // undo_allocation() method too. 462 undo_allocation(dest_attr, obj_ptr, word_sz, node_index); 463 return handle_evacuation_failure_par(old, old_mark); 464 } 465 #endif // !PRODUCT 466 467 // We're going to allocate linearly, so might as well prefetch ahead. 468 Prefetch::write(obj_ptr, PrefetchCopyIntervalInBytes); 469 470 const oop obj = cast_to_oop(obj_ptr); 471 const oop forward_ptr = old->forward_to_atomic(obj, old_mark, memory_order_relaxed); 472 if (forward_ptr == NULL) { 473 Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(old), obj_ptr, word_sz); 474 475 { 476 const uint young_index = from_region->young_index_in_cset(); 477 assert((from_region->is_young() && young_index > 0) || 478 (!from_region->is_young() && young_index == 0), "invariant" ); 479 _surviving_young_words[young_index] += word_sz; 480 } 481 482 if (dest_attr.is_young()) { 483 if (age < markWord::max_age) { 484 age++; 485 } 486 if (old_mark.has_displaced_mark_helper()) { 487 // In this case, we have to install the old mark word containing the 488 // displacement tag, and update the age in the displaced mark word. 489 markWord new_mark = old_mark.displaced_mark_helper().set_age(age); 490 old_mark.set_displaced_mark_helper(new_mark); 491 obj->set_mark(old_mark); 492 } else { 493 obj->set_mark(old_mark.set_age(age)); 494 } 495 _age_table.add(age, word_sz); 496 } else { 497 obj->set_mark(old_mark); 498 } 499 500 // Most objects are not arrays, so do one array check rather than 501 // checking for each array category for each object. 502 if (klass->is_array_klass()) { 503 if (klass->is_objArray_klass()) { 504 start_partial_objarray(dest_attr, old, obj); 505 } else { 506 // Nothing needs to be done for typeArrays. Body doesn't contain 507 // any oops to scan, and the type in the klass will already be handled 508 // by processing the built-in module. 509 assert(klass->is_typeArray_klass(), "invariant"); 510 } 511 return obj; 512 } 513 514 // Check for deduplicating young Strings. 515 if (G1StringDedup::is_candidate_from_evacuation(klass, 516 region_attr, 517 dest_attr, 518 age)) { 519 // Record old; request adds a new weak reference, which reference 520 // processing expects to refer to a from-space object. 521 _string_dedup_requests.add(old); 522 } 523 524 G1ScanInYoungSetter x(&_scanner, dest_attr.is_young()); 525 obj->oop_iterate_backwards(&_scanner, klass); 526 return obj; 527 528 } else { 529 _plab_allocator->undo_allocation(dest_attr, obj_ptr, word_sz, node_index); 530 return forward_ptr; 531 } 532 } 533 534 // Public not-inline entry point. 535 ATTRIBUTE_FLATTEN 536 oop G1ParScanThreadState::copy_to_survivor_space(G1HeapRegionAttr region_attr, 537 oop old, 538 markWord old_mark) { 539 return do_copy_to_survivor_space(region_attr, old, old_mark); 540 } 541 542 G1ParScanThreadState* G1ParScanThreadStateSet::state_for_worker(uint worker_id) { 543 assert(worker_id < _n_workers, "out of bounds access"); 544 if (_states[worker_id] == NULL) { 545 _states[worker_id] = 546 new G1ParScanThreadState(_g1h, _rdcqs, 547 worker_id, _n_workers, 548 _young_cset_length, _optional_cset_length); 549 } 550 return _states[worker_id]; 551 } 552 553 const size_t* G1ParScanThreadStateSet::surviving_young_words() const { 554 assert(_flushed, "thread local state from the per thread states should have been flushed"); 555 return _surviving_young_words_total; 556 } 557 558 void G1ParScanThreadStateSet::flush() { 559 assert(!_flushed, "thread local state from the per thread states should be flushed once"); 560 561 for (uint worker_id = 0; worker_id < _n_workers; ++worker_id) { 562 G1ParScanThreadState* pss = _states[worker_id]; 563 564 if (pss == NULL) { 565 continue; 566 } 567 568 G1GCPhaseTimes* p = _g1h->phase_times(); 569 570 // Need to get the following two before the call to G1ParThreadScanState::flush() 571 // because it resets the PLAB allocator where we get this info from. 572 size_t lab_waste_bytes = pss->lab_waste_words() * HeapWordSize; 573 size_t lab_undo_waste_bytes = pss->lab_undo_waste_words() * HeapWordSize; 574 size_t copied_bytes = pss->flush(_surviving_young_words_total) * HeapWordSize; 575 576 p->record_or_add_thread_work_item(G1GCPhaseTimes::MergePSS, worker_id, copied_bytes, G1GCPhaseTimes::MergePSSCopiedBytes); 577 p->record_or_add_thread_work_item(G1GCPhaseTimes::MergePSS, worker_id, lab_waste_bytes, G1GCPhaseTimes::MergePSSLABWasteBytes); 578 p->record_or_add_thread_work_item(G1GCPhaseTimes::MergePSS, worker_id, lab_undo_waste_bytes, G1GCPhaseTimes::MergePSSLABUndoWasteBytes); 579 580 delete pss; 581 _states[worker_id] = NULL; 582 } 583 _flushed = true; 584 } 585 586 void G1ParScanThreadStateSet::record_unused_optional_region(HeapRegion* hr) { 587 for (uint worker_index = 0; worker_index < _n_workers; ++worker_index) { 588 G1ParScanThreadState* pss = _states[worker_index]; 589 590 if (pss == NULL) { 591 continue; 592 } 593 594 size_t used_memory = pss->oops_into_optional_region(hr)->used_memory(); 595 _g1h->phase_times()->record_or_add_thread_work_item(G1GCPhaseTimes::OptScanHR, worker_index, used_memory, G1GCPhaseTimes::ScanHRUsedMemory); 596 } 597 } 598 599 NOINLINE 600 oop G1ParScanThreadState::handle_evacuation_failure_par(oop old, markWord m) { 601 assert(_g1h->is_in_cset(old), "Object " PTR_FORMAT " should be in the CSet", p2i(old)); 602 603 oop forward_ptr = old->forward_to_atomic(old, m, memory_order_relaxed); 604 if (forward_ptr == NULL) { 605 // Forward-to-self succeeded. We are the "owner" of the object. 606 HeapRegion* r = _g1h->heap_region_containing(old); 607 608 if (_g1h->notify_region_failed_evacuation(r->hrm_index())) { 609 _g1h->hr_printer()->evac_failure(r); 610 } 611 612 _g1h->preserve_mark_during_evac_failure(_worker_id, old, m); 613 614 G1ScanInYoungSetter x(&_scanner, r->is_young()); 615 old->oop_iterate_backwards(&_scanner); 616 617 return old; 618 } else { 619 // Forward-to-self failed. Either someone else managed to allocate 620 // space for this object (old != forward_ptr) or they beat us in 621 // self-forwarding it (old == forward_ptr). 622 assert(old == forward_ptr || !_g1h->is_in_cset(forward_ptr), 623 "Object " PTR_FORMAT " forwarded to: " PTR_FORMAT " " 624 "should not be in the CSet", 625 p2i(old), p2i(forward_ptr)); 626 return forward_ptr; 627 } 628 } 629 630 void G1ParScanThreadState::initialize_numa_stats() { 631 if (_numa->is_enabled()) { 632 LogTarget(Info, gc, heap, numa) lt; 633 634 if (lt.is_enabled()) { 635 uint num_nodes = _numa->num_active_nodes(); 636 // Record only if there are multiple active nodes. 637 _obj_alloc_stat = NEW_C_HEAP_ARRAY(size_t, num_nodes, mtGC); 638 memset(_obj_alloc_stat, 0, sizeof(size_t) * num_nodes); 639 } 640 } 641 } 642 643 void G1ParScanThreadState::flush_numa_stats() { 644 if (_obj_alloc_stat != NULL) { 645 uint node_index = _numa->index_of_current_thread(); 646 _numa->copy_statistics(G1NUMAStats::LocalObjProcessAtCopyToSurv, node_index, _obj_alloc_stat); 647 } 648 } 649 650 void G1ParScanThreadState::update_numa_stats(uint node_index) { 651 if (_obj_alloc_stat != NULL) { 652 _obj_alloc_stat[node_index]++; 653 } 654 } 655 656 G1ParScanThreadStateSet::G1ParScanThreadStateSet(G1CollectedHeap* g1h, 657 G1RedirtyCardsQueueSet* rdcqs, 658 uint n_workers, 659 size_t young_cset_length, 660 size_t optional_cset_length) : 661 _g1h(g1h), 662 _rdcqs(rdcqs), 663 _states(NEW_C_HEAP_ARRAY(G1ParScanThreadState*, n_workers, mtGC)), 664 _surviving_young_words_total(NEW_C_HEAP_ARRAY(size_t, young_cset_length + 1, mtGC)), 665 _young_cset_length(young_cset_length), 666 _optional_cset_length(optional_cset_length), 667 _n_workers(n_workers), 668 _flushed(false) { 669 for (uint i = 0; i < n_workers; ++i) { 670 _states[i] = NULL; 671 } 672 memset(_surviving_young_words_total, 0, (young_cset_length + 1) * sizeof(size_t)); 673 } 674 675 G1ParScanThreadStateSet::~G1ParScanThreadStateSet() { 676 assert(_flushed, "thread local state from the per thread states should have been flushed"); 677 FREE_C_HEAP_ARRAY(G1ParScanThreadState*, _states); 678 FREE_C_HEAP_ARRAY(size_t, _surviving_young_words_total); 679 }