189
190 // Although we never intentionally push references outside of the collection
191 // set, due to (benign) races in the claim mechanism during RSet scanning more
192 // than one thread might claim the same card. So the same card may be
193 // processed multiple times, and so we might get references into old gen here.
194 // So we need to redo this check.
195 const G1HeapRegionAttr region_attr = _g1h->region_attr(obj);
196 // References pushed onto the work stack should never point to a humongous region
197 // as they are not added to the collection set due to above precondition.
198 assert(!region_attr.is_humongous_candidate(),
199 "Obj " PTR_FORMAT " should not refer to humongous region %u from " PTR_FORMAT,
200 p2i(obj), _g1h->addr_to_region(obj), p2i(p));
201
202 if (!region_attr.is_in_cset()) {
203 // In this case somebody else already did all the work.
204 return;
205 }
206
207 markWord m = obj->mark();
208 if (m.is_marked()) {
209 obj = cast_to_oop(m.decode_pointer());
210 } else {
211 obj = do_copy_to_survivor_space(region_attr, obj, m);
212 }
213 RawAccess<IS_NOT_NULL>::oop_store(p, obj);
214
215 write_ref_field_post(p, obj);
216 }
217
218 MAYBE_INLINE_EVACUATION
219 void G1ParScanThreadState::do_partial_array(PartialArrayScanTask task) {
220 oop from_obj = task.to_source_array();
221
222 assert(_g1h->is_in_reserved(from_obj), "must be in heap.");
223 assert(from_obj->is_objArray(), "must be obj array");
224 assert(from_obj->is_forwarded(), "must be forwarded");
225
226 oop to_obj = from_obj->forwardee();
227 assert(from_obj != to_obj, "should not be chunking self-forwarded objects");
228 assert(to_obj->is_objArray(), "must be obj array");
229 objArrayOop to_array = objArrayOop(to_obj);
230
231 PartialArrayTaskStepper::Step step
232 = _partial_array_stepper.next(objArrayOop(from_obj),
233 to_array,
234 _partial_objarray_chunk_size);
235 for (uint i = 0; i < step._ncreate; ++i) {
236 push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
237 }
238
239 G1HeapRegionAttr dest_attr = _g1h->region_attr(to_array);
240 G1SkipCardEnqueueSetter x(&_scanner, dest_attr.is_new_survivor());
241 // Process claimed task. The length of to_array is not correct, but
242 // fortunately the iteration ignores the length field and just relies
243 // on start/end.
244 to_array->oop_iterate_range(&_scanner,
245 step._index,
246 step._index + _partial_objarray_chunk_size);
247 }
248
249 MAYBE_INLINE_EVACUATION
250 void G1ParScanThreadState::start_partial_objarray(G1HeapRegionAttr dest_attr,
251 oop from_obj,
252 oop to_obj) {
253 assert(from_obj->is_objArray(), "precondition");
254 assert(from_obj->is_forwarded(), "precondition");
255 assert(from_obj->forwardee() == to_obj, "precondition");
256 assert(from_obj != to_obj, "should not be scanning self-forwarded objects");
257 assert(to_obj->is_objArray(), "precondition");
258
259 objArrayOop to_array = objArrayOop(to_obj);
260
261 PartialArrayTaskStepper::Step step
262 = _partial_array_stepper.start(objArrayOop(from_obj),
263 to_array,
264 _partial_objarray_chunk_size);
265
266 // Push any needed partial scan tasks. Pushed before processing the
267 // initial chunk to allow other workers to steal while we're processing.
268 for (uint i = 0; i < step._ncreate; ++i) {
269 push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
270 }
271
272 // Skip the card enqueue iff the object (to_array) is in survivor region.
273 // However, HeapRegion::is_survivor() is too expensive here.
360 // no other space to try.
361 return nullptr;
362 }
363 }
364
365 G1HeapRegionAttr G1ParScanThreadState::next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age) {
366 assert(region_attr.is_young() || region_attr.is_old(), "must be either Young or Old");
367
368 if (region_attr.is_young()) {
369 age = !m.has_displaced_mark_helper() ? m.age()
370 : m.displaced_mark_helper().age();
371 if (age < _tenuring_threshold) {
372 return region_attr;
373 }
374 }
375 // young-to-old (promotion) or old-to-old; destination is old in both cases.
376 return G1HeapRegionAttr::Old;
377 }
378
379 void G1ParScanThreadState::report_promotion_event(G1HeapRegionAttr const dest_attr,
380 oop const old, size_t word_sz, uint age,
381 HeapWord * const obj_ptr, uint node_index) const {
382 PLAB* alloc_buf = _plab_allocator->alloc_buffer(dest_attr, node_index);
383 if (alloc_buf->contains(obj_ptr)) {
384 _g1h->gc_tracer_stw()->report_promotion_in_new_plab_event(old->klass(), word_sz * HeapWordSize, age,
385 dest_attr.type() == G1HeapRegionAttr::Old,
386 alloc_buf->word_sz() * HeapWordSize);
387 } else {
388 _g1h->gc_tracer_stw()->report_promotion_outside_plab_event(old->klass(), word_sz * HeapWordSize, age,
389 dest_attr.type() == G1HeapRegionAttr::Old);
390 }
391 }
392
393 NOINLINE
394 HeapWord* G1ParScanThreadState::allocate_copy_slow(G1HeapRegionAttr* dest_attr,
395 oop old,
396 size_t word_sz,
397 uint age,
398 uint node_index) {
399 HeapWord* obj_ptr = nullptr;
400 // Try slow-path allocation unless we're allocating old and old is already full.
401 if (!(dest_attr->is_old() && _old_gen_is_full)) {
402 bool plab_refill_failed = false;
403 obj_ptr = _plab_allocator->allocate_direct_or_new_plab(*dest_attr,
404 word_sz,
405 &plab_refill_failed,
406 node_index);
407 if (obj_ptr == nullptr) {
408 obj_ptr = allocate_in_next_plab(dest_attr,
409 word_sz,
410 plab_refill_failed,
411 node_index);
412 }
413 }
414 if (obj_ptr != nullptr) {
415 update_numa_stats(node_index);
416 if (_g1h->gc_tracer_stw()->should_report_promotion_events()) {
417 // The events are checked individually as part of the actual commit
418 report_promotion_event(*dest_attr, old, word_sz, age, obj_ptr, node_index);
419 }
420 }
421 return obj_ptr;
422 }
423
424 #if EVAC_FAILURE_INJECTOR
425 bool G1ParScanThreadState::inject_evacuation_failure(uint region_idx) {
426 return _g1h->evac_failure_injector()->evacuation_should_fail(_evac_failure_inject_counter, region_idx);
427 }
428 #endif
429
430 NOINLINE
431 void G1ParScanThreadState::undo_allocation(G1HeapRegionAttr dest_attr,
432 HeapWord* obj_ptr,
433 size_t word_sz,
434 uint node_index) {
435 _plab_allocator->undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
436 }
437
438 void G1ParScanThreadState::update_bot_after_copying(oop obj, size_t word_sz) {
439 HeapWord* obj_start = cast_from_oop<HeapWord*>(obj);
440 HeapRegion* region = _g1h->heap_region_containing(obj_start);
441 region->update_bot_for_obj(obj_start, word_sz);
442 }
443
444 // Private inline function, for direct internal use and providing the
445 // implementation of the public not-inline function.
446 MAYBE_INLINE_EVACUATION
447 oop G1ParScanThreadState::do_copy_to_survivor_space(G1HeapRegionAttr const region_attr,
448 oop const old,
449 markWord const old_mark) {
450 assert(region_attr.is_in_cset(),
451 "Unexpected region attr type: %s", region_attr.get_type_str());
452
453 // Get the klass once. We'll need it again later, and this avoids
454 // re-decoding when it's compressed.
455 Klass* klass = old->klass();
456 const size_t word_sz = old->size_given_klass(klass);
457
458 uint age = 0;
459 G1HeapRegionAttr dest_attr = next_region_attr(region_attr, old_mark, age);
460 HeapRegion* const from_region = _g1h->heap_region_containing(old);
461 uint node_index = from_region->node_index();
462
463 HeapWord* obj_ptr = _plab_allocator->plab_allocate(dest_attr, word_sz, node_index);
464
465 // PLAB allocations should succeed most of the time, so we'll
466 // normally check against null once and that's it.
467 if (obj_ptr == nullptr) {
468 obj_ptr = allocate_copy_slow(&dest_attr, old, word_sz, age, node_index);
469 if (obj_ptr == nullptr) {
470 // This will either forward-to-self, or detect that someone else has
471 // installed a forwarding pointer.
472 return handle_evacuation_failure_par(old, old_mark, word_sz);
473 }
474 }
475
476 assert(obj_ptr != nullptr, "when we get here, allocation should have succeeded");
477 assert(_g1h->is_in_reserved(obj_ptr), "Allocated memory should be in the heap");
478
479 // Should this evacuation fail?
480 if (inject_evacuation_failure(from_region->hrm_index())) {
481 // Doing this after all the allocation attempts also tests the
482 // undo_allocation() method too.
483 undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
484 return handle_evacuation_failure_par(old, old_mark, word_sz);
485 }
486
487 // We're going to allocate linearly, so might as well prefetch ahead.
488 Prefetch::write(obj_ptr, PrefetchCopyIntervalInBytes);
603 delete pss;
604 _states[worker_id] = nullptr;
605 }
606 _flushed = true;
607 }
608
609 void G1ParScanThreadStateSet::record_unused_optional_region(HeapRegion* hr) {
610 for (uint worker_index = 0; worker_index < _num_workers; ++worker_index) {
611 G1ParScanThreadState* pss = _states[worker_index];
612 assert(pss != nullptr, "must be initialized");
613
614 size_t used_memory = pss->oops_into_optional_region(hr)->used_memory();
615 _g1h->phase_times()->record_or_add_thread_work_item(G1GCPhaseTimes::OptScanHR, worker_index, used_memory, G1GCPhaseTimes::ScanHRUsedMemory);
616 }
617 }
618
619 NOINLINE
620 oop G1ParScanThreadState::handle_evacuation_failure_par(oop old, markWord m, size_t word_sz) {
621 assert(_g1h->is_in_cset(old), "Object " PTR_FORMAT " should be in the CSet", p2i(old));
622
623 oop forward_ptr = old->forward_to_atomic(old, m, memory_order_relaxed);
624 if (forward_ptr == nullptr) {
625 // Forward-to-self succeeded. We are the "owner" of the object.
626 HeapRegion* r = _g1h->heap_region_containing(old);
627
628 if (_evac_failure_regions->record(r->hrm_index())) {
629 _g1h->hr_printer()->evac_failure(r);
630 }
631
632 // Mark the failing object in the marking bitmap and later use the bitmap to handle
633 // evacuation failure recovery.
634 _g1h->mark_evac_failure_object(_worker_id, old, word_sz);
635
636 _preserved_marks->push_if_necessary(old, m);
637
638 ContinuationGCSupport::transform_stack_chunk(old);
639
640 _evacuation_failed_info.register_copy_failure(word_sz);
641
642 // For iterating objects that failed evacuation currently we can reuse the
643 // existing closure to scan evacuated objects because:
|
189
190 // Although we never intentionally push references outside of the collection
191 // set, due to (benign) races in the claim mechanism during RSet scanning more
192 // than one thread might claim the same card. So the same card may be
193 // processed multiple times, and so we might get references into old gen here.
194 // So we need to redo this check.
195 const G1HeapRegionAttr region_attr = _g1h->region_attr(obj);
196 // References pushed onto the work stack should never point to a humongous region
197 // as they are not added to the collection set due to above precondition.
198 assert(!region_attr.is_humongous_candidate(),
199 "Obj " PTR_FORMAT " should not refer to humongous region %u from " PTR_FORMAT,
200 p2i(obj), _g1h->addr_to_region(obj), p2i(p));
201
202 if (!region_attr.is_in_cset()) {
203 // In this case somebody else already did all the work.
204 return;
205 }
206
207 markWord m = obj->mark();
208 if (m.is_marked()) {
209 obj = obj->forwardee(m);
210 } else {
211 obj = do_copy_to_survivor_space(region_attr, obj, m);
212 }
213 RawAccess<IS_NOT_NULL>::oop_store(p, obj);
214
215 write_ref_field_post(p, obj);
216 }
217
218 MAYBE_INLINE_EVACUATION
219 void G1ParScanThreadState::do_partial_array(PartialArrayScanTask task) {
220 oop from_obj = task.to_source_array();
221
222 assert(_g1h->is_in_reserved(from_obj), "must be in heap.");
223 assert(from_obj->is_forwarded(), "must be forwarded");
224
225 oop to_obj = from_obj->forwardee();
226 assert(from_obj != to_obj, "should not be chunking self-forwarded objects");
227 assert(to_obj->is_objArray(), "must be obj array");
228 objArrayOop to_array = objArrayOop(to_obj);
229
230 PartialArrayTaskStepper::Step step
231 = _partial_array_stepper.next(objArrayOop(from_obj),
232 to_array,
233 _partial_objarray_chunk_size);
234 for (uint i = 0; i < step._ncreate; ++i) {
235 push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
236 }
237
238 G1HeapRegionAttr dest_attr = _g1h->region_attr(to_array);
239 G1SkipCardEnqueueSetter x(&_scanner, dest_attr.is_new_survivor());
240 // Process claimed task. The length of to_array is not correct, but
241 // fortunately the iteration ignores the length field and just relies
242 // on start/end.
243 to_array->oop_iterate_range(&_scanner,
244 step._index,
245 step._index + _partial_objarray_chunk_size);
246 }
247
248 MAYBE_INLINE_EVACUATION
249 void G1ParScanThreadState::start_partial_objarray(G1HeapRegionAttr dest_attr,
250 oop from_obj,
251 oop to_obj) {
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 // initial 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 // Skip the card enqueue iff the object (to_array) is in survivor region.
271 // However, HeapRegion::is_survivor() is too expensive here.
358 // no other space to try.
359 return nullptr;
360 }
361 }
362
363 G1HeapRegionAttr G1ParScanThreadState::next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age) {
364 assert(region_attr.is_young() || region_attr.is_old(), "must be either Young or Old");
365
366 if (region_attr.is_young()) {
367 age = !m.has_displaced_mark_helper() ? m.age()
368 : m.displaced_mark_helper().age();
369 if (age < _tenuring_threshold) {
370 return region_attr;
371 }
372 }
373 // young-to-old (promotion) or old-to-old; destination is old in both cases.
374 return G1HeapRegionAttr::Old;
375 }
376
377 void G1ParScanThreadState::report_promotion_event(G1HeapRegionAttr const dest_attr,
378 oop const old, Klass* klass, size_t word_sz, uint age,
379 HeapWord * const obj_ptr, uint node_index) const {
380 PLAB* alloc_buf = _plab_allocator->alloc_buffer(dest_attr, node_index);
381 if (alloc_buf->contains(obj_ptr)) {
382 _g1h->gc_tracer_stw()->report_promotion_in_new_plab_event(klass, word_sz * HeapWordSize, age,
383 dest_attr.type() == G1HeapRegionAttr::Old,
384 alloc_buf->word_sz() * HeapWordSize);
385 } else {
386 _g1h->gc_tracer_stw()->report_promotion_outside_plab_event(klass, word_sz * HeapWordSize, age,
387 dest_attr.type() == G1HeapRegionAttr::Old);
388 }
389 }
390
391 NOINLINE
392 HeapWord* G1ParScanThreadState::allocate_copy_slow(G1HeapRegionAttr* dest_attr,
393 oop old,
394 Klass* klass,
395 size_t word_sz,
396 uint age,
397 uint node_index) {
398 HeapWord* obj_ptr = nullptr;
399 // Try slow-path allocation unless we're allocating old and old is already full.
400 if (!(dest_attr->is_old() && _old_gen_is_full)) {
401 bool plab_refill_failed = false;
402 obj_ptr = _plab_allocator->allocate_direct_or_new_plab(*dest_attr,
403 word_sz,
404 &plab_refill_failed,
405 node_index);
406 if (obj_ptr == nullptr) {
407 obj_ptr = allocate_in_next_plab(dest_attr,
408 word_sz,
409 plab_refill_failed,
410 node_index);
411 }
412 }
413 if (obj_ptr != nullptr) {
414 update_numa_stats(node_index);
415 if (_g1h->gc_tracer_stw()->should_report_promotion_events()) {
416 // The events are checked individually as part of the actual commit
417 report_promotion_event(*dest_attr, old, klass, word_sz, age, obj_ptr, node_index);
418 }
419 }
420 return obj_ptr;
421 }
422
423 #if EVAC_FAILURE_INJECTOR
424 bool G1ParScanThreadState::inject_evacuation_failure(uint region_idx) {
425 return _g1h->evac_failure_injector()->evacuation_should_fail(_evac_failure_inject_counter, region_idx);
426 }
427 #endif
428
429 NOINLINE
430 void G1ParScanThreadState::undo_allocation(G1HeapRegionAttr dest_attr,
431 HeapWord* obj_ptr,
432 size_t word_sz,
433 uint node_index) {
434 _plab_allocator->undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
435 }
436
437 void G1ParScanThreadState::update_bot_after_copying(oop obj, size_t word_sz) {
438 HeapWord* obj_start = cast_from_oop<HeapWord*>(obj);
439 HeapRegion* region = _g1h->heap_region_containing(obj_start);
440 region->update_bot_for_obj(obj_start, word_sz);
441 }
442
443 // Private inline function, for direct internal use and providing the
444 // implementation of the public not-inline function.
445 MAYBE_INLINE_EVACUATION
446 oop G1ParScanThreadState::do_copy_to_survivor_space(G1HeapRegionAttr const region_attr,
447 oop const old,
448 markWord const old_mark) {
449 assert(region_attr.is_in_cset(),
450 "Unexpected region attr type: %s", region_attr.get_type_str());
451
452 if (old_mark.is_marked()) {
453 // Already forwarded by somebody else, return forwardee.
454 return old->forwardee(old_mark);
455 }
456 // Get the klass once. We'll need it again later, and this avoids
457 // re-decoding when it's compressed.
458 Klass* klass;
459 #ifdef _LP64
460 if (UseCompactObjectHeaders) {
461 klass = old_mark.safe_klass();
462 } else
463 #endif
464 {
465 klass = old->klass();
466 }
467 const size_t word_sz = old->size_given_klass(klass);
468
469 uint age = 0;
470 G1HeapRegionAttr dest_attr = next_region_attr(region_attr, old_mark, age);
471 HeapRegion* const from_region = _g1h->heap_region_containing(old);
472 uint node_index = from_region->node_index();
473
474 HeapWord* obj_ptr = _plab_allocator->plab_allocate(dest_attr, word_sz, node_index);
475
476 // PLAB allocations should succeed most of the time, so we'll
477 // normally check against null once and that's it.
478 if (obj_ptr == nullptr) {
479 obj_ptr = allocate_copy_slow(&dest_attr, old, klass, word_sz, age, node_index);
480 if (obj_ptr == nullptr) {
481 // This will either forward-to-self, or detect that someone else has
482 // installed a forwarding pointer.
483 return handle_evacuation_failure_par(old, old_mark, word_sz);
484 }
485 }
486
487 assert(obj_ptr != nullptr, "when we get here, allocation should have succeeded");
488 assert(_g1h->is_in_reserved(obj_ptr), "Allocated memory should be in the heap");
489
490 // Should this evacuation fail?
491 if (inject_evacuation_failure(from_region->hrm_index())) {
492 // Doing this after all the allocation attempts also tests the
493 // undo_allocation() method too.
494 undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
495 return handle_evacuation_failure_par(old, old_mark, word_sz);
496 }
497
498 // We're going to allocate linearly, so might as well prefetch ahead.
499 Prefetch::write(obj_ptr, PrefetchCopyIntervalInBytes);
614 delete pss;
615 _states[worker_id] = nullptr;
616 }
617 _flushed = true;
618 }
619
620 void G1ParScanThreadStateSet::record_unused_optional_region(HeapRegion* hr) {
621 for (uint worker_index = 0; worker_index < _num_workers; ++worker_index) {
622 G1ParScanThreadState* pss = _states[worker_index];
623 assert(pss != nullptr, "must be initialized");
624
625 size_t used_memory = pss->oops_into_optional_region(hr)->used_memory();
626 _g1h->phase_times()->record_or_add_thread_work_item(G1GCPhaseTimes::OptScanHR, worker_index, used_memory, G1GCPhaseTimes::ScanHRUsedMemory);
627 }
628 }
629
630 NOINLINE
631 oop G1ParScanThreadState::handle_evacuation_failure_par(oop old, markWord m, size_t word_sz) {
632 assert(_g1h->is_in_cset(old), "Object " PTR_FORMAT " should be in the CSet", p2i(old));
633
634 oop forward_ptr = old->forward_to_self_atomic(m, memory_order_relaxed);
635 if (forward_ptr == nullptr) {
636 // Forward-to-self succeeded. We are the "owner" of the object.
637 HeapRegion* r = _g1h->heap_region_containing(old);
638
639 if (_evac_failure_regions->record(r->hrm_index())) {
640 _g1h->hr_printer()->evac_failure(r);
641 }
642
643 // Mark the failing object in the marking bitmap and later use the bitmap to handle
644 // evacuation failure recovery.
645 _g1h->mark_evac_failure_object(_worker_id, old, word_sz);
646
647 _preserved_marks->push_if_necessary(old, m);
648
649 ContinuationGCSupport::transform_stack_chunk(old);
650
651 _evacuation_failed_info.register_copy_failure(word_sz);
652
653 // For iterating objects that failed evacuation currently we can reuse the
654 // existing closure to scan evacuated objects because:
|