< prev index next >

src/hotspot/share/gc/g1/g1ParScanThreadState.cpp

Print this page

190 
191   // Although we never intentionally push references outside of the collection
192   // set, due to (benign) races in the claim mechanism during RSet scanning more
193   // than one thread might claim the same card. So the same card may be
194   // processed multiple times, and so we might get references into old gen here.
195   // So we need to redo this check.
196   const G1HeapRegionAttr region_attr = _g1h->region_attr(obj);
197   // References pushed onto the work stack should never point to a humongous region
198   // as they are not added to the collection set due to above precondition.
199   assert(!region_attr.is_humongous_candidate(),
200          "Obj " PTR_FORMAT " should not refer to humongous region %u from " PTR_FORMAT,
201          p2i(obj), _g1h->addr_to_region(obj), p2i(p));
202 
203   if (!region_attr.is_in_cset()) {
204     // In this case somebody else already did all the work.
205     return;
206   }
207 
208   markWord m = obj->mark();
209   if (m.is_marked()) {
210     obj = cast_to_oop(m.decode_pointer());
211   } else {
212     obj = do_copy_to_survivor_space(region_attr, obj, m);
213   }
214   RawAccess<IS_NOT_NULL>::oop_store(p, obj);
215 
216   write_ref_field_post(p, obj);
217 }
218 
219 MAYBE_INLINE_EVACUATION
220 void G1ParScanThreadState::do_partial_array(PartialArrayScanTask task) {
221   oop from_obj = task.to_source_array();
222 
223   assert(_g1h->is_in_reserved(from_obj), "must be in heap.");
224   assert(from_obj->is_objArray(), "must be obj array");
225   assert(from_obj->is_forwarded(), "must be forwarded");
226 
227   oop to_obj = from_obj->forwardee();
228   assert(from_obj != to_obj, "should not be chunking self-forwarded objects");
229   assert(to_obj->is_objArray(), "must be obj array");
230   objArrayOop to_array = objArrayOop(to_obj);
231 
232   PartialArrayTaskStepper::Step step
233     = _partial_array_stepper.next(objArrayOop(from_obj),
234                                   to_array,
235                                   _partial_objarray_chunk_size);
236   for (uint i = 0; i < step._ncreate; ++i) {
237     push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
238   }
239 
240   G1HeapRegionAttr dest_attr = _g1h->region_attr(to_array);
241   G1SkipCardEnqueueSetter x(&_scanner, dest_attr.is_new_survivor());
242   // Process claimed task.  The length of to_array is not correct, but
243   // fortunately the iteration ignores the length field and just relies
244   // on start/end.
245   to_array->oop_iterate_range(&_scanner,
246                               step._index,
247                               step._index + _partial_objarray_chunk_size);
248 }
249 
250 MAYBE_INLINE_EVACUATION
251 void G1ParScanThreadState::start_partial_objarray(G1HeapRegionAttr dest_attr,
252                                                   oop from_obj,
253                                                   oop to_obj) {
254   assert(from_obj->is_objArray(), "precondition");
255   assert(from_obj->is_forwarded(), "precondition");
256   assert(from_obj->forwardee() == to_obj, "precondition");
257   assert(from_obj != to_obj, "should not be scanning self-forwarded objects");
258   assert(to_obj->is_objArray(), "precondition");
259 
260   objArrayOop to_array = objArrayOop(to_obj);
261 
262   PartialArrayTaskStepper::Step step
263     = _partial_array_stepper.start(objArrayOop(from_obj),
264                                    to_array,
265                                    _partial_objarray_chunk_size);
266 
267   // Push any needed partial scan tasks.  Pushed before processing the
268   // initial chunk to allow other workers to steal while we're processing.
269   for (uint i = 0; i < step._ncreate; ++i) {
270     push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
271   }
272 
273   // Skip the card enqueue iff the object (to_array) is in survivor region.
274   // However, HeapRegion::is_survivor() is too expensive here.

361     // no other space to try.
362     return nullptr;
363   }
364 }
365 
366 G1HeapRegionAttr G1ParScanThreadState::next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age) {
367   assert(region_attr.is_young() || region_attr.is_old(), "must be either Young or Old");
368 
369   if (region_attr.is_young()) {
370     age = !m.has_displaced_mark_helper() ? m.age()
371                                          : m.displaced_mark_helper().age();
372     if (age < _tenuring_threshold) {
373       return region_attr;
374     }
375   }
376   // young-to-old (promotion) or old-to-old; destination is old in both cases.
377   return G1HeapRegionAttr::Old;
378 }
379 
380 void G1ParScanThreadState::report_promotion_event(G1HeapRegionAttr const dest_attr,
381                                                   oop const old, size_t word_sz, uint age,
382                                                   HeapWord * const obj_ptr, uint node_index) const {
383   PLAB* alloc_buf = _plab_allocator->alloc_buffer(dest_attr, node_index);
384   if (alloc_buf->contains(obj_ptr)) {
385     _g1h->gc_tracer_stw()->report_promotion_in_new_plab_event(old->klass(), word_sz * HeapWordSize, age,
386                                                               dest_attr.type() == G1HeapRegionAttr::Old,
387                                                               alloc_buf->word_sz() * HeapWordSize);
388   } else {
389     _g1h->gc_tracer_stw()->report_promotion_outside_plab_event(old->klass(), word_sz * HeapWordSize, age,
390                                                                dest_attr.type() == G1HeapRegionAttr::Old);
391   }
392 }
393 
394 NOINLINE
395 HeapWord* G1ParScanThreadState::allocate_copy_slow(G1HeapRegionAttr* dest_attr,
396                                                    oop old,
397                                                    size_t word_sz,
398                                                    uint age,
399                                                    uint node_index) {
400   HeapWord* obj_ptr = nullptr;
401   // Try slow-path allocation unless we're allocating old and old is already full.
402   if (!(dest_attr->is_old() && _old_gen_is_full)) {
403     bool plab_refill_failed = false;
404     obj_ptr = _plab_allocator->allocate_direct_or_new_plab(*dest_attr,
405                                                            word_sz,
406                                                            &plab_refill_failed,
407                                                            node_index);
408     if (obj_ptr == nullptr) {
409       obj_ptr = allocate_in_next_plab(dest_attr,
410                                       word_sz,
411                                       plab_refill_failed,
412                                       node_index);
413     }
414   }
415   if (obj_ptr != nullptr) {
416     update_numa_stats(node_index);
417     if (_g1h->gc_tracer_stw()->should_report_promotion_events()) {
418       // The events are checked individually as part of the actual commit
419       report_promotion_event(*dest_attr, old, word_sz, age, obj_ptr, node_index);
420     }
421   }
422   return obj_ptr;
423 }
424 
425 #if EVAC_FAILURE_INJECTOR
426 bool G1ParScanThreadState::inject_evacuation_failure(uint region_idx) {
427   return _g1h->evac_failure_injector()->evacuation_should_fail(_evac_failure_inject_counter, region_idx);
428 }
429 #endif
430 
431 NOINLINE
432 void G1ParScanThreadState::undo_allocation(G1HeapRegionAttr dest_attr,
433                                            HeapWord* obj_ptr,
434                                            size_t word_sz,
435                                            uint node_index) {
436   _plab_allocator->undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
437 }
438 
439 void G1ParScanThreadState::update_bot_after_copying(oop obj, size_t word_sz) {
440   HeapWord* obj_start = cast_from_oop<HeapWord*>(obj);
441   HeapRegion* region = _g1h->heap_region_containing(obj_start);
442   region->update_bot_for_obj(obj_start, word_sz);
443 }
444 
445 // Private inline function, for direct internal use and providing the
446 // implementation of the public not-inline function.
447 MAYBE_INLINE_EVACUATION
448 oop G1ParScanThreadState::do_copy_to_survivor_space(G1HeapRegionAttr const region_attr,
449                                                     oop const old,
450                                                     markWord const old_mark) {
451   assert(region_attr.is_in_cset(),
452          "Unexpected region attr type: %s", region_attr.get_type_str());
453 
454   // Get the klass once.  We'll need it again later, and this avoids
455   // re-decoding when it's compressed.
456   Klass* klass = old->klass();






457   const size_t word_sz = old->size_given_klass(klass);
458 
459   uint age = 0;
460   G1HeapRegionAttr dest_attr = next_region_attr(region_attr, old_mark, age);
461   HeapRegion* const from_region = _g1h->heap_region_containing(old);
462   uint node_index = from_region->node_index();
463 
464   HeapWord* obj_ptr = _plab_allocator->plab_allocate(dest_attr, word_sz, node_index);
465 
466   // PLAB allocations should succeed most of the time, so we'll
467   // normally check against null once and that's it.
468   if (obj_ptr == nullptr) {
469     obj_ptr = allocate_copy_slow(&dest_attr, old, word_sz, age, node_index);
470     if (obj_ptr == nullptr) {
471       // This will either forward-to-self, or detect that someone else has
472       // installed a forwarding pointer.
473       return handle_evacuation_failure_par(old, old_mark, word_sz);
474     }
475   }
476 
477   assert(obj_ptr != nullptr, "when we get here, allocation should have succeeded");
478   assert(_g1h->is_in_reserved(obj_ptr), "Allocated memory should be in the heap");
479 
480   // Should this evacuation fail?
481   if (inject_evacuation_failure(from_region->hrm_index())) {
482     // Doing this after all the allocation attempts also tests the
483     // undo_allocation() method too.
484     undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
485     return handle_evacuation_failure_par(old, old_mark, word_sz);
486   }
487 
488   // We're going to allocate linearly, so might as well prefetch ahead.
489   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:

190 
191   // Although we never intentionally push references outside of the collection
192   // set, due to (benign) races in the claim mechanism during RSet scanning more
193   // than one thread might claim the same card. So the same card may be
194   // processed multiple times, and so we might get references into old gen here.
195   // So we need to redo this check.
196   const G1HeapRegionAttr region_attr = _g1h->region_attr(obj);
197   // References pushed onto the work stack should never point to a humongous region
198   // as they are not added to the collection set due to above precondition.
199   assert(!region_attr.is_humongous_candidate(),
200          "Obj " PTR_FORMAT " should not refer to humongous region %u from " PTR_FORMAT,
201          p2i(obj), _g1h->addr_to_region(obj), p2i(p));
202 
203   if (!region_attr.is_in_cset()) {
204     // In this case somebody else already did all the work.
205     return;
206   }
207 
208   markWord m = obj->mark();
209   if (m.is_marked()) {
210     obj = obj->forwardee(m);
211   } else {
212     obj = do_copy_to_survivor_space(region_attr, obj, m);
213   }
214   RawAccess<IS_NOT_NULL>::oop_store(p, obj);
215 
216   write_ref_field_post(p, obj);
217 }
218 
219 MAYBE_INLINE_EVACUATION
220 void G1ParScanThreadState::do_partial_array(PartialArrayScanTask task) {
221   oop from_obj = task.to_source_array();
222 
223   assert(_g1h->is_in_reserved(from_obj), "must be in heap.");
224   assert(from_obj->forward_safe_klass()->is_objArray_klass(), "must be obj array");
225   assert(from_obj->is_forwarded(), "must be forwarded");
226 
227   oop to_obj = from_obj->forwardee();
228   assert(from_obj != to_obj, "should not be chunking self-forwarded objects");
229   assert(to_obj->is_objArray(), "must be obj array");
230   objArrayOop to_array = objArrayOop(to_obj);
231 
232   PartialArrayTaskStepper::Step step
233     = _partial_array_stepper.next(objArrayOop(from_obj),
234                                   to_array,
235                                   _partial_objarray_chunk_size);
236   for (uint i = 0; i < step._ncreate; ++i) {
237     push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
238   }
239 
240   G1HeapRegionAttr dest_attr = _g1h->region_attr(to_array);
241   G1SkipCardEnqueueSetter x(&_scanner, dest_attr.is_new_survivor());
242   // Process claimed task.  The length of to_array is not correct, but
243   // fortunately the iteration ignores the length field and just relies
244   // on start/end.
245   to_array->oop_iterate_range(&_scanner,
246                               step._index,
247                               step._index + _partial_objarray_chunk_size);
248 }
249 
250 MAYBE_INLINE_EVACUATION
251 void G1ParScanThreadState::start_partial_objarray(G1HeapRegionAttr dest_attr,
252                                                   oop from_obj,
253                                                   oop to_obj) {
254   assert(from_obj->forward_safe_klass()->is_objArray_klass(), "precondition");
255   assert(from_obj->is_forwarded(), "precondition");
256   assert(from_obj->forwardee() == to_obj, "precondition");
257   assert(from_obj != to_obj, "should not be scanning self-forwarded objects");
258   assert(to_obj->is_objArray(), "precondition");
259 
260   objArrayOop to_array = objArrayOop(to_obj);
261 
262   PartialArrayTaskStepper::Step step
263     = _partial_array_stepper.start(objArrayOop(from_obj),
264                                    to_array,
265                                    _partial_objarray_chunk_size);
266 
267   // Push any needed partial scan tasks.  Pushed before processing the
268   // initial chunk to allow other workers to steal while we're processing.
269   for (uint i = 0; i < step._ncreate; ++i) {
270     push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
271   }
272 
273   // Skip the card enqueue iff the object (to_array) is in survivor region.
274   // However, HeapRegion::is_survivor() is too expensive here.

361     // no other space to try.
362     return nullptr;
363   }
364 }
365 
366 G1HeapRegionAttr G1ParScanThreadState::next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age) {
367   assert(region_attr.is_young() || region_attr.is_old(), "must be either Young or Old");
368 
369   if (region_attr.is_young()) {
370     age = !m.has_displaced_mark_helper() ? m.age()
371                                          : m.displaced_mark_helper().age();
372     if (age < _tenuring_threshold) {
373       return region_attr;
374     }
375   }
376   // young-to-old (promotion) or old-to-old; destination is old in both cases.
377   return G1HeapRegionAttr::Old;
378 }
379 
380 void G1ParScanThreadState::report_promotion_event(G1HeapRegionAttr const dest_attr,
381                                                   Klass* klass, size_t word_sz, uint age,
382                                                   HeapWord * const obj_ptr, uint node_index) const {
383   PLAB* alloc_buf = _plab_allocator->alloc_buffer(dest_attr, node_index);
384   if (alloc_buf->contains(obj_ptr)) {
385     _g1h->gc_tracer_stw()->report_promotion_in_new_plab_event(klass, word_sz * HeapWordSize, age,
386                                                               dest_attr.type() == G1HeapRegionAttr::Old,
387                                                               alloc_buf->word_sz() * HeapWordSize);
388   } else {
389     _g1h->gc_tracer_stw()->report_promotion_outside_plab_event(klass, word_sz * HeapWordSize, age,
390                                                                dest_attr.type() == G1HeapRegionAttr::Old);
391   }
392 }
393 
394 NOINLINE
395 HeapWord* G1ParScanThreadState::allocate_copy_slow(G1HeapRegionAttr* dest_attr,
396                                                    Klass* klass,
397                                                    size_t word_sz,
398                                                    uint age,
399                                                    uint node_index) {
400   HeapWord* obj_ptr = nullptr;
401   // Try slow-path allocation unless we're allocating old and old is already full.
402   if (!(dest_attr->is_old() && _old_gen_is_full)) {
403     bool plab_refill_failed = false;
404     obj_ptr = _plab_allocator->allocate_direct_or_new_plab(*dest_attr,
405                                                            word_sz,
406                                                            &plab_refill_failed,
407                                                            node_index);
408     if (obj_ptr == nullptr) {
409       obj_ptr = allocate_in_next_plab(dest_attr,
410                                       word_sz,
411                                       plab_refill_failed,
412                                       node_index);
413     }
414   }
415   if (obj_ptr != nullptr) {
416     update_numa_stats(node_index);
417     if (_g1h->gc_tracer_stw()->should_report_promotion_events()) {
418       // The events are checked individually as part of the actual commit
419       report_promotion_event(*dest_attr, klass, word_sz, age, obj_ptr, node_index);
420     }
421   }
422   return obj_ptr;
423 }
424 
425 #if EVAC_FAILURE_INJECTOR
426 bool G1ParScanThreadState::inject_evacuation_failure(uint region_idx) {
427   return _g1h->evac_failure_injector()->evacuation_should_fail(_evac_failure_inject_counter, region_idx);
428 }
429 #endif
430 
431 NOINLINE
432 void G1ParScanThreadState::undo_allocation(G1HeapRegionAttr dest_attr,
433                                            HeapWord* obj_ptr,
434                                            size_t word_sz,
435                                            uint node_index) {
436   _plab_allocator->undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
437 }
438 
439 void G1ParScanThreadState::update_bot_after_copying(oop obj, size_t word_sz) {
440   HeapWord* obj_start = cast_from_oop<HeapWord*>(obj);
441   HeapRegion* region = _g1h->heap_region_containing(obj_start);
442   region->update_bot_for_obj(obj_start, word_sz);
443 }
444 
445 // Private inline function, for direct internal use and providing the
446 // implementation of the public not-inline function.
447 MAYBE_INLINE_EVACUATION
448 oop G1ParScanThreadState::do_copy_to_survivor_space(G1HeapRegionAttr const region_attr,
449                                                     oop const old,
450                                                     markWord const old_mark) {
451   assert(region_attr.is_in_cset(),
452          "Unexpected region attr type: %s", region_attr.get_type_str());
453 
454   // Get the klass once.  We'll need it again later, and this avoids
455   // re-decoding when it's compressed.
456   // NOTE: With compact headers, it is not safe to load the Klass* from o, because
457   // that would access the mark-word, and the mark-word might change at any time by
458   // concurrent promotion. The promoted mark-word would point to the forwardee, which
459   // may not yet have completed copying. Therefore we must load the Klass* from
460   // the mark-word that we have already loaded. This is safe, because we have checked
461   // that this is not yet forwarded in the caller.
462   Klass* klass = old->forward_safe_klass(old_mark);
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 == nullptr) {
475     obj_ptr = allocate_copy_slow(&dest_attr, klass, word_sz, age, node_index);
476     if (obj_ptr == nullptr) {
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 != nullptr, "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);

609     delete pss;
610     _states[worker_id] = nullptr;
611   }
612   _flushed = true;
613 }
614 
615 void G1ParScanThreadStateSet::record_unused_optional_region(HeapRegion* hr) {
616   for (uint worker_index = 0; worker_index < _num_workers; ++worker_index) {
617     G1ParScanThreadState* pss = _states[worker_index];
618     assert(pss != nullptr, "must be initialized");
619 
620     size_t used_memory = pss->oops_into_optional_region(hr)->used_memory();
621     _g1h->phase_times()->record_or_add_thread_work_item(G1GCPhaseTimes::OptScanHR, worker_index, used_memory, G1GCPhaseTimes::ScanHRUsedMemory);
622   }
623 }
624 
625 NOINLINE
626 oop G1ParScanThreadState::handle_evacuation_failure_par(oop old, markWord m, size_t word_sz) {
627   assert(_g1h->is_in_cset(old), "Object " PTR_FORMAT " should be in the CSet", p2i(old));
628 
629   oop forward_ptr = old->forward_to_self_atomic(m, memory_order_relaxed);
630   if (forward_ptr == nullptr) {
631     // Forward-to-self succeeded. We are the "owner" of the object.
632     HeapRegion* r = _g1h->heap_region_containing(old);
633 
634     if (_evac_failure_regions->record(r->hrm_index())) {
635       _g1h->hr_printer()->evac_failure(r);
636     }
637 
638     // Mark the failing object in the marking bitmap and later use the bitmap to handle
639     // evacuation failure recovery.
640     _g1h->mark_evac_failure_object(_worker_id, old, word_sz);
641 
642     _preserved_marks->push_if_necessary(old, m);
643 
644     ContinuationGCSupport::transform_stack_chunk(old);
645 
646     _evacuation_failed_info.register_copy_failure(word_sz);
647 
648     // For iterating objects that failed evacuation currently we can reuse the
649     // existing closure to scan evacuated objects because:
< prev index next >