< prev index next >

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

Print this page

188 
189   // Although we never intentionally push references outside of the collection
190   // set, due to (benign) races in the claim mechanism during RSet scanning more
191   // than one thread might claim the same card. So the same card may be
192   // processed multiple times, and so we might get references into old gen here.
193   // So we need to redo this check.
194   const G1HeapRegionAttr region_attr = _g1h->region_attr(obj);
195   // References pushed onto the work stack should never point to a humongous region
196   // as they are not added to the collection set due to above precondition.
197   assert(!region_attr.is_humongous(),
198          "Obj " PTR_FORMAT " should not refer to humongous region %u from " PTR_FORMAT,
199          p2i(obj), _g1h->addr_to_region(cast_from_oop<HeapWord*>(obj)), p2i(p));
200 
201   if (!region_attr.is_in_cset()) {
202     // In this case somebody else already did all the work.
203     return;
204   }
205 
206   markWord m = obj->mark();
207   if (m.is_marked()) {
208     obj = cast_to_oop(m.decode_pointer());
209   } else {
210     obj = do_copy_to_survivor_space(region_attr, obj, m);
211   }
212   RawAccess<IS_NOT_NULL>::oop_store(p, obj);
213 
214   write_ref_field_post(p, obj);
215 }
216 
217 MAYBE_INLINE_EVACUATION
218 void G1ParScanThreadState::do_partial_array(PartialArrayScanTask task) {
219   oop from_obj = task.to_source_array();
220 
221   assert(_g1h->is_in_reserved(from_obj), "must be in heap.");
222   assert(from_obj->is_objArray(), "must be obj array");
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_objArray(), "precondition");
253   assert(from_obj->is_forwarded(), "precondition");
254   assert(from_obj->forwardee() == to_obj, "precondition");
255   assert(from_obj != to_obj, "should not be scanning self-forwarded objects");
256   assert(to_obj->is_objArray(), "precondition");
257 
258   objArrayOop to_array = objArrayOop(to_obj);
259 
260   PartialArrayTaskStepper::Step step
261     = _partial_array_stepper.start(objArrayOop(from_obj),
262                                    to_array,
263                                    _partial_objarray_chunk_size);
264 
265   // Push any needed partial scan tasks.  Pushed before processing the
266   // intitial chunk to allow other workers to steal while we're processing.
267   for (uint i = 0; i < step._ncreate; ++i) {
268     push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
269   }
270 
271   // Skip the card enqueue iff the object (to_array) is in survivor region.
272   // However, HeapRegion::is_survivor() is too expensive here.

359     // no other space to try.
360     return NULL;
361   }
362 }
363 
364 G1HeapRegionAttr G1ParScanThreadState::next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age) {
365   assert(region_attr.is_young() || region_attr.is_old(), "must be either Young or Old");
366 
367   if (region_attr.is_young()) {
368     age = !m.has_displaced_mark_helper() ? m.age()
369                                          : m.displaced_mark_helper().age();
370     if (age < _tenuring_threshold) {
371       return region_attr;
372     }
373   }
374   // young-to-old (promotion) or old-to-old; destination is old in both cases.
375   return G1HeapRegionAttr::Old;
376 }
377 
378 void G1ParScanThreadState::report_promotion_event(G1HeapRegionAttr const dest_attr,
379                                                   oop const old, size_t word_sz, uint age,
380                                                   HeapWord * const obj_ptr, uint node_index) const {
381   PLAB* alloc_buf = _plab_allocator->alloc_buffer(dest_attr, node_index);
382   if (alloc_buf->contains(obj_ptr)) {
383     _g1h->gc_tracer_stw()->report_promotion_in_new_plab_event(old->klass(), word_sz * HeapWordSize, age,
384                                                               dest_attr.type() == G1HeapRegionAttr::Old,
385                                                               alloc_buf->word_sz() * HeapWordSize);
386   } else {
387     _g1h->gc_tracer_stw()->report_promotion_outside_plab_event(old->klass(), word_sz * HeapWordSize, age,
388                                                                dest_attr.type() == G1HeapRegionAttr::Old);
389   }
390 }
391 
392 NOINLINE
393 HeapWord* G1ParScanThreadState::allocate_copy_slow(G1HeapRegionAttr* dest_attr,
394                                                    oop old,

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, 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 // Private inline function, for direct internal use and providing the
438 // implementation of the public not-inline function.
439 MAYBE_INLINE_EVACUATION
440 oop G1ParScanThreadState::do_copy_to_survivor_space(G1HeapRegionAttr const region_attr,
441                                                     oop const old,
442                                                     markWord const old_mark) {
443   assert(region_attr.is_in_cset(),
444          "Unexpected region attr type: %s", region_attr.get_type_str());
445 




446   // Get the klass once.  We'll need it again later, and this avoids
447   // re-decoding when it's compressed.



448   Klass* klass = old->klass();

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

595     delete pss;
596     _states[worker_id] = NULL;
597   }
598   _flushed = true;
599 }
600 
601 void G1ParScanThreadStateSet::record_unused_optional_region(HeapRegion* hr) {
602   for (uint worker_index = 0; worker_index < _n_workers; ++worker_index) {
603     G1ParScanThreadState* pss = _states[worker_index];
604     assert(pss != nullptr, "must be initialized");
605 
606     size_t used_memory = pss->oops_into_optional_region(hr)->used_memory();
607     _g1h->phase_times()->record_or_add_thread_work_item(G1GCPhaseTimes::OptScanHR, worker_index, used_memory, G1GCPhaseTimes::ScanHRUsedMemory);
608   }
609 }
610 
611 NOINLINE
612 oop G1ParScanThreadState::handle_evacuation_failure_par(oop old, markWord m, size_t word_sz) {
613   assert(_g1h->is_in_cset(old), "Object " PTR_FORMAT " should be in the CSet", p2i(old));
614 
615   oop forward_ptr = old->forward_to_atomic(old, m, memory_order_relaxed);
616   if (forward_ptr == NULL) {
617     // Forward-to-self succeeded. We are the "owner" of the object.
618     HeapRegion* r = _g1h->heap_region_containing(old);
619     // Records evac failure objs, this will help speed up iteration
620     // of these objs later in *remove self forward* phase of post evacuation.
621     r->record_evac_failure_obj(old);
622 
623     if (_evac_failure_regions->record(r->hrm_index())) {
624       _g1h->hr_printer()->evac_failure(r);
625     }
626 
627     _preserved_marks->push_if_necessary(old, m);
628     _evacuation_failed_info.register_copy_failure(word_sz);
629 
630     // For iterating objects that failed evacuation currently we can reuse the
631     // existing closure to scan evacuated objects because:
632     // - for objects referring into the collection set we do not need to gather
633     // cards at this time. The regions they are in will be unconditionally turned
634     // to old regions without remembered sets.
635     // - since we are iterating from a collection set region (i.e. never a Survivor

188 
189   // Although we never intentionally push references outside of the collection
190   // set, due to (benign) races in the claim mechanism during RSet scanning more
191   // than one thread might claim the same card. So the same card may be
192   // processed multiple times, and so we might get references into old gen here.
193   // So we need to redo this check.
194   const G1HeapRegionAttr region_attr = _g1h->region_attr(obj);
195   // References pushed onto the work stack should never point to a humongous region
196   // as they are not added to the collection set due to above precondition.
197   assert(!region_attr.is_humongous(),
198          "Obj " PTR_FORMAT " should not refer to humongous region %u from " PTR_FORMAT,
199          p2i(obj), _g1h->addr_to_region(cast_from_oop<HeapWord*>(obj)), p2i(p));
200 
201   if (!region_attr.is_in_cset()) {
202     // In this case somebody else already did all the work.
203     return;
204   }
205 
206   markWord m = obj->mark();
207   if (m.is_marked()) {
208     obj = obj->forwardee(m);
209   } else {
210     obj = do_copy_to_survivor_space(region_attr, obj, m);
211   }
212   RawAccess<IS_NOT_NULL>::oop_store(p, obj);
213 
214   write_ref_field_post(p, obj);
215 }
216 
217 MAYBE_INLINE_EVACUATION
218 void G1ParScanThreadState::do_partial_array(PartialArrayScanTask task) {
219   oop from_obj = task.to_source_array();
220 
221   assert(_g1h->is_in_reserved(from_obj), "must be in heap.");

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   G1HeapRegionAttr dest_attr = _g1h->region_attr(to_array);
238   G1SkipCardEnqueueSetter x(&_scanner, dest_attr.is_new_survivor());
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_forwarded(), "precondition");
252   assert(from_obj->forwardee() == to_obj, "precondition");
253   assert(from_obj != to_obj, "should not be scanning self-forwarded objects");
254   assert(to_obj->is_objArray(), "precondition");
255 
256   objArrayOop to_array = objArrayOop(to_obj);
257 
258   PartialArrayTaskStepper::Step step
259     = _partial_array_stepper.start(objArrayOop(from_obj),
260                                    to_array,
261                                    _partial_objarray_chunk_size);
262 
263   // Push any needed partial scan tasks.  Pushed before processing the
264   // intitial chunk to allow other workers to steal while we're processing.
265   for (uint i = 0; i < step._ncreate; ++i) {
266     push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
267   }
268 
269   // Skip the card enqueue iff the object (to_array) is in survivor region.
270   // However, HeapRegion::is_survivor() is too expensive here.

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

602     delete pss;
603     _states[worker_id] = NULL;
604   }
605   _flushed = true;
606 }
607 
608 void G1ParScanThreadStateSet::record_unused_optional_region(HeapRegion* hr) {
609   for (uint worker_index = 0; worker_index < _n_workers; ++worker_index) {
610     G1ParScanThreadState* pss = _states[worker_index];
611     assert(pss != nullptr, "must be initialized");
612 
613     size_t used_memory = pss->oops_into_optional_region(hr)->used_memory();
614     _g1h->phase_times()->record_or_add_thread_work_item(G1GCPhaseTimes::OptScanHR, worker_index, used_memory, G1GCPhaseTimes::ScanHRUsedMemory);
615   }
616 }
617 
618 NOINLINE
619 oop G1ParScanThreadState::handle_evacuation_failure_par(oop old, markWord m, size_t word_sz) {
620   assert(_g1h->is_in_cset(old), "Object " PTR_FORMAT " should be in the CSet", p2i(old));
621 
622   oop forward_ptr = old->forward_to_self_atomic(m, memory_order_relaxed);
623   if (forward_ptr == NULL) {
624     // Forward-to-self succeeded. We are the "owner" of the object.
625     HeapRegion* r = _g1h->heap_region_containing(old);
626     // Records evac failure objs, this will help speed up iteration
627     // of these objs later in *remove self forward* phase of post evacuation.
628     r->record_evac_failure_obj(old);
629 
630     if (_evac_failure_regions->record(r->hrm_index())) {
631       _g1h->hr_printer()->evac_failure(r);
632     }
633 
634     _preserved_marks->push_if_necessary(old, m);
635     _evacuation_failed_info.register_copy_failure(word_sz);
636 
637     // For iterating objects that failed evacuation currently we can reuse the
638     // existing closure to scan evacuated objects because:
639     // - for objects referring into the collection set we do not need to gather
640     // cards at this time. The regions they are in will be unconditionally turned
641     // to old regions without remembered sets.
642     // - since we are iterating from a collection set region (i.e. never a Survivor
< prev index next >