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 NULL;
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 = NULL;
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 == NULL) {
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 != NULL) {
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 == NULL) {
468 obj_ptr = allocate_copy_slow(&dest_attr, old, word_sz, age, node_index);
469 if (obj_ptr == NULL) {
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 != NULL, "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);
601 delete pss;
602 _states[worker_id] = NULL;
603 }
604 _flushed = true;
605 }
606
607 void G1ParScanThreadStateSet::record_unused_optional_region(HeapRegion* hr) {
608 for (uint worker_index = 0; worker_index < _n_workers; ++worker_index) {
609 G1ParScanThreadState* pss = _states[worker_index];
610 assert(pss != nullptr, "must be initialized");
611
612 size_t used_memory = pss->oops_into_optional_region(hr)->used_memory();
613 _g1h->phase_times()->record_or_add_thread_work_item(G1GCPhaseTimes::OptScanHR, worker_index, used_memory, G1GCPhaseTimes::ScanHRUsedMemory);
614 }
615 }
616
617 NOINLINE
618 oop G1ParScanThreadState::handle_evacuation_failure_par(oop old, markWord m, size_t word_sz) {
619 assert(_g1h->is_in_cset(old), "Object " PTR_FORMAT " should be in the CSet", p2i(old));
620
621 oop forward_ptr = old->forward_to_atomic(old, m, memory_order_relaxed);
622 if (forward_ptr == NULL) {
623 // Forward-to-self succeeded. We are the "owner" of the object.
624 HeapRegion* r = _g1h->heap_region_containing(old);
625
626 if (_evac_failure_regions->record(r->hrm_index())) {
627 _g1h->hr_printer()->evac_failure(r);
628 }
629
630 // Mark the failing object in the marking bitmap and later use the bitmap to handle
631 // evacuation failure recovery.
632 _g1h->mark_evac_failure_object(_worker_id, old, word_sz);
633
634 _preserved_marks->push_if_necessary(old, m);
635
636 ContinuationGCSupport::transform_stack_chunk(old);
637
638 _evacuation_failed_info.register_copy_failure(word_sz);
639
640 // For iterating objects that failed evacuation currently we can reuse the
641 // 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 NULL;
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 = NULL;
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 == NULL) {
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 != NULL) {
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 #ifdef _LP64
459 Klass* klass = old_mark.safe_klass();
460 #else
461 Klass* klass = old->klass();
462 #endif
463 const size_t word_sz = old->size_given_klass(klass);
464
465 uint age = 0;
466 G1HeapRegionAttr dest_attr = next_region_attr(region_attr, old_mark, age);
467 HeapRegion* const from_region = _g1h->heap_region_containing(old);
468 uint node_index = from_region->node_index();
469
470 HeapWord* obj_ptr = _plab_allocator->plab_allocate(dest_attr, word_sz, node_index);
471
472 // PLAB allocations should succeed most of the time, so we'll
473 // normally check against NULL once and that's it.
474 if (obj_ptr == NULL) {
475 obj_ptr = allocate_copy_slow(&dest_attr, old, klass, word_sz, age, node_index);
476 if (obj_ptr == NULL) {
477 // This will either forward-to-self, or detect that someone else has
478 // installed a forwarding pointer.
479 return handle_evacuation_failure_par(old, old_mark, word_sz);
480 }
481 }
482
483 assert(obj_ptr != NULL, "when we get here, allocation should have succeeded");
484 assert(_g1h->is_in_reserved(obj_ptr), "Allocated memory should be in the heap");
485
486 // Should this evacuation fail?
487 if (inject_evacuation_failure(from_region->hrm_index())) {
488 // Doing this after all the allocation attempts also tests the
489 // undo_allocation() method too.
490 undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
491 return handle_evacuation_failure_par(old, old_mark, word_sz);
492 }
493
494 // We're going to allocate linearly, so might as well prefetch ahead.
495 Prefetch::write(obj_ptr, PrefetchCopyIntervalInBytes);
608 delete pss;
609 _states[worker_id] = NULL;
610 }
611 _flushed = true;
612 }
613
614 void G1ParScanThreadStateSet::record_unused_optional_region(HeapRegion* hr) {
615 for (uint worker_index = 0; worker_index < _n_workers; ++worker_index) {
616 G1ParScanThreadState* pss = _states[worker_index];
617 assert(pss != nullptr, "must be initialized");
618
619 size_t used_memory = pss->oops_into_optional_region(hr)->used_memory();
620 _g1h->phase_times()->record_or_add_thread_work_item(G1GCPhaseTimes::OptScanHR, worker_index, used_memory, G1GCPhaseTimes::ScanHRUsedMemory);
621 }
622 }
623
624 NOINLINE
625 oop G1ParScanThreadState::handle_evacuation_failure_par(oop old, markWord m, size_t word_sz) {
626 assert(_g1h->is_in_cset(old), "Object " PTR_FORMAT " should be in the CSet", p2i(old));
627
628 oop forward_ptr = old->forward_to_self_atomic(m, memory_order_relaxed);
629 if (forward_ptr == NULL) {
630 // Forward-to-self succeeded. We are the "owner" of the object.
631 HeapRegion* r = _g1h->heap_region_containing(old);
632
633 if (_evac_failure_regions->record(r->hrm_index())) {
634 _g1h->hr_printer()->evac_failure(r);
635 }
636
637 // Mark the failing object in the marking bitmap and later use the bitmap to handle
638 // evacuation failure recovery.
639 _g1h->mark_evac_failure_object(_worker_id, old, word_sz);
640
641 _preserved_marks->push_if_necessary(old, m);
642
643 ContinuationGCSupport::transform_stack_chunk(old);
644
645 _evacuation_failed_info.register_copy_failure(word_sz);
646
647 // For iterating objects that failed evacuation currently we can reuse the
648 // existing closure to scan evacuated objects because:
|