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 write_ref_field_post(p, obj);
207 }
208
209 MAYBE_INLINE_EVACUATION
210 void G1ParScanThreadState::do_partial_array(PartialArrayScanTask task) {
211 oop from_obj = task.to_source_array();
212
213 assert(_g1h->is_in_reserved(from_obj), "must be in heap.");
214 assert(from_obj->is_objArray(), "must be obj array");
215 assert(from_obj->is_forwarded(), "must be forwarded");
216
217 oop to_obj = from_obj->forwardee();
218 assert(from_obj != to_obj, "should not be chunking self-forwarded objects");
219 assert(to_obj->is_objArray(), "must be obj array");
220 objArrayOop to_array = objArrayOop(to_obj);
221
222 PartialArrayTaskStepper::Step step
223 = _partial_array_stepper.next(objArrayOop(from_obj),
224 to_array,
225 _partial_objarray_chunk_size);
226 for (uint i = 0; i < step._ncreate; ++i) {
227 push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
228 }
229
230 HeapRegion* hr = _g1h->heap_region_containing(to_array);
231 G1ScanInYoungSetter x(&_scanner, hr->is_young());
232 // Process claimed task. The length of to_array is not correct, but
233 // fortunately the iteration ignores the length field and just relies
234 // on start/end.
235 to_array->oop_iterate_range(&_scanner,
236 step._index,
237 step._index + _partial_objarray_chunk_size);
238 }
239
240 MAYBE_INLINE_EVACUATION
241 void G1ParScanThreadState::start_partial_objarray(G1HeapRegionAttr dest_attr,
242 oop from_obj,
243 oop to_obj) {
244 assert(from_obj->is_objArray(), "precondition");
245 assert(from_obj->is_forwarded(), "precondition");
246 assert(from_obj->forwardee() == to_obj, "precondition");
247 assert(from_obj != to_obj, "should not be scanning self-forwarded objects");
248 assert(to_obj->is_objArray(), "precondition");
249
250 objArrayOop to_array = objArrayOop(to_obj);
251
252 PartialArrayTaskStepper::Step step
253 = _partial_array_stepper.start(objArrayOop(from_obj),
254 to_array,
255 _partial_objarray_chunk_size);
256
257 // Push any needed partial scan tasks. Pushed before processing the
258 // intitial chunk to allow other workers to steal while we're processing.
259 for (uint i = 0; i < step._ncreate; ++i) {
260 push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
261 }
262
263 G1ScanInYoungSetter x(&_scanner, dest_attr.is_young());
264 // Process the initial chunk. No need to process the type in the
343 } else {
344 _old_gen_is_full = previous_plab_refill_failed;
345 assert(dest->is_old(), "Unexpected dest region attr: %s", dest->get_type_str());
346 // no other space to try.
347 return NULL;
348 }
349 }
350
351 G1HeapRegionAttr G1ParScanThreadState::next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age) {
352 if (region_attr.is_young()) {
353 age = !m.has_displaced_mark_helper() ? m.age()
354 : m.displaced_mark_helper().age();
355 if (age < _tenuring_threshold) {
356 return region_attr;
357 }
358 }
359 return dest(region_attr);
360 }
361
362 void G1ParScanThreadState::report_promotion_event(G1HeapRegionAttr const dest_attr,
363 oop const old, size_t word_sz, uint age,
364 HeapWord * const obj_ptr, uint node_index) const {
365 PLAB* alloc_buf = _plab_allocator->alloc_buffer(dest_attr, node_index);
366 if (alloc_buf->contains(obj_ptr)) {
367 _g1h->_gc_tracer_stw->report_promotion_in_new_plab_event(old->klass(), word_sz * HeapWordSize, age,
368 dest_attr.type() == G1HeapRegionAttr::Old,
369 alloc_buf->word_sz() * HeapWordSize);
370 } else {
371 _g1h->_gc_tracer_stw->report_promotion_outside_plab_event(old->klass(), word_sz * HeapWordSize, age,
372 dest_attr.type() == G1HeapRegionAttr::Old);
373 }
374 }
375
376 NOINLINE
377 HeapWord* G1ParScanThreadState::allocate_copy_slow(G1HeapRegionAttr* dest_attr,
378 oop old,
379 size_t word_sz,
380 uint age,
381 uint node_index) {
382 HeapWord* obj_ptr = NULL;
383 // Try slow-path allocation unless we're allocating old and old is already full.
384 if (!(dest_attr->is_old() && _old_gen_is_full)) {
385 bool plab_refill_failed = false;
386 obj_ptr = _plab_allocator->allocate_direct_or_new_plab(*dest_attr,
387 word_sz,
388 &plab_refill_failed,
389 node_index);
390 if (obj_ptr == NULL) {
391 obj_ptr = allocate_in_next_plab(dest_attr,
392 word_sz,
393 plab_refill_failed,
394 node_index);
395 }
396 }
397 if (obj_ptr != NULL) {
398 update_numa_stats(node_index);
399 if (_g1h->_gc_tracer_stw->should_report_promotion_events()) {
400 // The events are checked individually as part of the actual commit
401 report_promotion_event(*dest_attr, old, word_sz, age, obj_ptr, node_index);
402 }
403 }
404 return obj_ptr;
405 }
406
407 NOINLINE
408 void G1ParScanThreadState::undo_allocation(G1HeapRegionAttr dest_attr,
409 HeapWord* obj_ptr,
410 size_t word_sz,
411 uint node_index) {
412 _plab_allocator->undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
413 }
414
415 // Private inline function, for direct internal use and providing the
416 // implementation of the public not-inline function.
417 MAYBE_INLINE_EVACUATION
418 oop G1ParScanThreadState::do_copy_to_survivor_space(G1HeapRegionAttr const region_attr,
419 oop const old,
420 markWord const old_mark) {
421 assert(region_attr.is_in_cset(),
422 "Unexpected region attr type: %s", region_attr.get_type_str());
423
424 // Get the klass once. We'll need it again later, and this avoids
425 // re-decoding when it's compressed.
426 Klass* klass = old->klass();
427 const size_t word_sz = old->size_given_klass(klass);
428
429 uint age = 0;
430 G1HeapRegionAttr dest_attr = next_region_attr(region_attr, old_mark, age);
431 HeapRegion* const from_region = _g1h->heap_region_containing(old);
432 uint node_index = from_region->node_index();
433
434 HeapWord* obj_ptr = _plab_allocator->plab_allocate(dest_attr, word_sz, node_index);
435
436 // PLAB allocations should succeed most of the time, so we'll
437 // normally check against NULL once and that's it.
438 if (obj_ptr == NULL) {
439 obj_ptr = allocate_copy_slow(&dest_attr, old, word_sz, age, node_index);
440 if (obj_ptr == NULL) {
441 // This will either forward-to-self, or detect that someone else has
442 // installed a forwarding pointer.
443 return handle_evacuation_failure_par(old, old_mark);
444 }
445 }
446
447 assert(obj_ptr != NULL, "when we get here, allocation should have succeeded");
448 assert(_g1h->is_in_reserved(obj_ptr), "Allocated memory should be in the heap");
449
450 #ifndef PRODUCT
451 // Should this evacuation fail?
452 if (_g1h->evacuation_should_fail()) {
453 // Doing this after all the allocation attempts also tests the
454 // undo_allocation() method too.
455 undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
456 return handle_evacuation_failure_par(old, old_mark);
457 }
458 #endif // !PRODUCT
459
576 _flushed = true;
577 }
578
579 void G1ParScanThreadStateSet::record_unused_optional_region(HeapRegion* hr) {
580 for (uint worker_index = 0; worker_index < _n_workers; ++worker_index) {
581 G1ParScanThreadState* pss = _states[worker_index];
582
583 if (pss == NULL) {
584 continue;
585 }
586
587 size_t used_memory = pss->oops_into_optional_region(hr)->used_memory();
588 _g1h->phase_times()->record_or_add_thread_work_item(G1GCPhaseTimes::OptScanHR, worker_index, used_memory, G1GCPhaseTimes::ScanHRUsedMemory);
589 }
590 }
591
592 NOINLINE
593 oop G1ParScanThreadState::handle_evacuation_failure_par(oop old, markWord m) {
594 assert(_g1h->is_in_cset(old), "Object " PTR_FORMAT " should be in the CSet", p2i(old));
595
596 oop forward_ptr = old->forward_to_atomic(old, m, memory_order_relaxed);
597 if (forward_ptr == NULL) {
598 // Forward-to-self succeeded. We are the "owner" of the object.
599 HeapRegion* r = _g1h->heap_region_containing(old);
600
601 if (_g1h->notify_region_failed_evacuation(r->hrm_index())) {
602 _g1h->hr_printer()->evac_failure(r);
603 }
604
605 _g1h->preserve_mark_during_evac_failure(_worker_id, old, m);
606
607 G1ScanInYoungSetter x(&_scanner, r->is_young());
608 old->oop_iterate_backwards(&_scanner);
609
610 return old;
611 } else {
612 // Forward-to-self failed. Either someone else managed to allocate
613 // space for this object (old != forward_ptr) or they beat us in
614 // self-forwarding it (old == forward_ptr).
615 assert(old == forward_ptr || !_g1h->is_in_cset(forward_ptr),
616 "Object " PTR_FORMAT " forwarded to: " PTR_FORMAT " "
|
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 = obj->forwardee(m);
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 write_ref_field_post(p, obj);
207 }
208
209 MAYBE_INLINE_EVACUATION
210 void G1ParScanThreadState::do_partial_array(PartialArrayScanTask task) {
211 oop from_obj = task.to_source_array();
212
213 assert(_g1h->is_in_reserved(from_obj), "must be in heap.");
214 assert(UseCompactObjectHeaders || from_obj->is_objArray(), "must be obj array");
215 assert(from_obj->is_forwarded(), "must be forwarded");
216
217 oop to_obj = from_obj->forwardee();
218 assert(from_obj != to_obj, "should not be chunking self-forwarded objects");
219 assert(to_obj->is_objArray(), "must be obj array");
220 objArrayOop to_array = objArrayOop(to_obj);
221
222 PartialArrayTaskStepper::Step step
223 = _partial_array_stepper.next(objArrayOop(from_obj),
224 to_array,
225 _partial_objarray_chunk_size);
226 for (uint i = 0; i < step._ncreate; ++i) {
227 push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
228 }
229
230 HeapRegion* hr = _g1h->heap_region_containing(to_array);
231 G1ScanInYoungSetter x(&_scanner, hr->is_young());
232 // Process claimed task. The length of to_array is not correct, but
233 // fortunately the iteration ignores the length field and just relies
234 // on start/end.
235 to_array->oop_iterate_range(&_scanner,
236 step._index,
237 step._index + _partial_objarray_chunk_size);
238 }
239
240 MAYBE_INLINE_EVACUATION
241 void G1ParScanThreadState::start_partial_objarray(G1HeapRegionAttr dest_attr,
242 oop from_obj,
243 oop to_obj) {
244 assert(UseCompactObjectHeaders || from_obj->is_objArray(), "precondition");
245 assert(from_obj->is_forwarded(), "precondition");
246 assert(from_obj->forwardee() == to_obj, "precondition");
247 assert(from_obj != to_obj, "should not be scanning self-forwarded objects");
248 assert(to_obj->is_objArray(), "precondition");
249
250 objArrayOop to_array = objArrayOop(to_obj);
251
252 PartialArrayTaskStepper::Step step
253 = _partial_array_stepper.start(objArrayOop(from_obj),
254 to_array,
255 _partial_objarray_chunk_size);
256
257 // Push any needed partial scan tasks. Pushed before processing the
258 // intitial chunk to allow other workers to steal while we're processing.
259 for (uint i = 0; i < step._ncreate; ++i) {
260 push_on_queue(ScannerTask(PartialArrayScanTask(from_obj)));
261 }
262
263 G1ScanInYoungSetter x(&_scanner, dest_attr.is_young());
264 // Process the initial chunk. No need to process the type in the
343 } else {
344 _old_gen_is_full = previous_plab_refill_failed;
345 assert(dest->is_old(), "Unexpected dest region attr: %s", dest->get_type_str());
346 // no other space to try.
347 return NULL;
348 }
349 }
350
351 G1HeapRegionAttr G1ParScanThreadState::next_region_attr(G1HeapRegionAttr const region_attr, markWord const m, uint& age) {
352 if (region_attr.is_young()) {
353 age = !m.has_displaced_mark_helper() ? m.age()
354 : m.displaced_mark_helper().age();
355 if (age < _tenuring_threshold) {
356 return region_attr;
357 }
358 }
359 return dest(region_attr);
360 }
361
362 void G1ParScanThreadState::report_promotion_event(G1HeapRegionAttr const dest_attr,
363 Klass* klass, size_t word_sz, uint age,
364 HeapWord * const obj_ptr, uint node_index) const {
365 PLAB* alloc_buf = _plab_allocator->alloc_buffer(dest_attr, node_index);
366 if (alloc_buf->contains(obj_ptr)) {
367 _g1h->_gc_tracer_stw->report_promotion_in_new_plab_event(klass, word_sz * HeapWordSize, age,
368 dest_attr.type() == G1HeapRegionAttr::Old,
369 alloc_buf->word_sz() * HeapWordSize);
370 } else {
371 _g1h->_gc_tracer_stw->report_promotion_outside_plab_event(klass, word_sz * HeapWordSize, age,
372 dest_attr.type() == G1HeapRegionAttr::Old);
373 }
374 }
375
376 NOINLINE
377 HeapWord* G1ParScanThreadState::allocate_copy_slow(G1HeapRegionAttr* dest_attr,
378 Klass* klass,
379 size_t word_sz,
380 uint age,
381 uint node_index) {
382 HeapWord* obj_ptr = NULL;
383 // Try slow-path allocation unless we're allocating old and old is already full.
384 if (!(dest_attr->is_old() && _old_gen_is_full)) {
385 bool plab_refill_failed = false;
386 obj_ptr = _plab_allocator->allocate_direct_or_new_plab(*dest_attr,
387 word_sz,
388 &plab_refill_failed,
389 node_index);
390 if (obj_ptr == NULL) {
391 obj_ptr = allocate_in_next_plab(dest_attr,
392 word_sz,
393 plab_refill_failed,
394 node_index);
395 }
396 }
397 if (obj_ptr != NULL) {
398 update_numa_stats(node_index);
399 if (_g1h->_gc_tracer_stw->should_report_promotion_events()) {
400 // The events are checked individually as part of the actual commit
401 report_promotion_event(*dest_attr, klass, word_sz, age, obj_ptr, node_index);
402 }
403 }
404 return obj_ptr;
405 }
406
407 NOINLINE
408 void G1ParScanThreadState::undo_allocation(G1HeapRegionAttr dest_attr,
409 HeapWord* obj_ptr,
410 size_t word_sz,
411 uint node_index) {
412 _plab_allocator->undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
413 }
414
415 // Private inline function, for direct internal use and providing the
416 // implementation of the public not-inline function.
417 MAYBE_INLINE_EVACUATION
418 oop G1ParScanThreadState::do_copy_to_survivor_space(G1HeapRegionAttr const region_attr,
419 oop const old,
420 markWord const old_mark) {
421 assert(region_attr.is_in_cset(),
422 "Unexpected region attr type: %s", region_attr.get_type_str());
423
424 // Get the klass once. We'll need it again later, and this avoids
425 // re-decoding when it's compressed.
426 // NOTE: With compact headers, it is not safe to load the Klass* from o, because
427 // that would access the mark-word, and the mark-word might change at any time by
428 // concurrent promotion. The promoted mark-word would point to the forwardee, which
429 // may not yet have completed copying. Therefore we must load the Klass* from
430 // the mark-word that we have already loaded. This is safe, because we have checked
431 // that this is not yet forwarded in the caller.
432 Klass* klass = old->forward_safe_klass(old_mark);
433 const size_t word_sz = old->size_given_klass(klass);
434
435 uint age = 0;
436 G1HeapRegionAttr dest_attr = next_region_attr(region_attr, old_mark, age);
437 HeapRegion* const from_region = _g1h->heap_region_containing(old);
438 uint node_index = from_region->node_index();
439
440 HeapWord* obj_ptr = _plab_allocator->plab_allocate(dest_attr, word_sz, node_index);
441
442 // PLAB allocations should succeed most of the time, so we'll
443 // normally check against NULL once and that's it.
444 if (obj_ptr == NULL) {
445 obj_ptr = allocate_copy_slow(&dest_attr, klass, word_sz, age, node_index);
446 if (obj_ptr == NULL) {
447 // This will either forward-to-self, or detect that someone else has
448 // installed a forwarding pointer.
449 return handle_evacuation_failure_par(old, old_mark);
450 }
451 }
452
453 assert(obj_ptr != NULL, "when we get here, allocation should have succeeded");
454 assert(_g1h->is_in_reserved(obj_ptr), "Allocated memory should be in the heap");
455
456 #ifndef PRODUCT
457 // Should this evacuation fail?
458 if (_g1h->evacuation_should_fail()) {
459 // Doing this after all the allocation attempts also tests the
460 // undo_allocation() method too.
461 undo_allocation(dest_attr, obj_ptr, word_sz, node_index);
462 return handle_evacuation_failure_par(old, old_mark);
463 }
464 #endif // !PRODUCT
465
582 _flushed = true;
583 }
584
585 void G1ParScanThreadStateSet::record_unused_optional_region(HeapRegion* hr) {
586 for (uint worker_index = 0; worker_index < _n_workers; ++worker_index) {
587 G1ParScanThreadState* pss = _states[worker_index];
588
589 if (pss == NULL) {
590 continue;
591 }
592
593 size_t used_memory = pss->oops_into_optional_region(hr)->used_memory();
594 _g1h->phase_times()->record_or_add_thread_work_item(G1GCPhaseTimes::OptScanHR, worker_index, used_memory, G1GCPhaseTimes::ScanHRUsedMemory);
595 }
596 }
597
598 NOINLINE
599 oop G1ParScanThreadState::handle_evacuation_failure_par(oop old, markWord m) {
600 assert(_g1h->is_in_cset(old), "Object " PTR_FORMAT " should be in the CSet", p2i(old));
601
602 oop forward_ptr = old->forward_to_self_atomic(m, memory_order_relaxed);
603 if (forward_ptr == NULL) {
604 // Forward-to-self succeeded. We are the "owner" of the object.
605 HeapRegion* r = _g1h->heap_region_containing(old);
606
607 if (_g1h->notify_region_failed_evacuation(r->hrm_index())) {
608 _g1h->hr_printer()->evac_failure(r);
609 }
610
611 _g1h->preserve_mark_during_evac_failure(_worker_id, old, m);
612
613 G1ScanInYoungSetter x(&_scanner, r->is_young());
614 old->oop_iterate_backwards(&_scanner);
615
616 return old;
617 } else {
618 // Forward-to-self failed. Either someone else managed to allocate
619 // space for this object (old != forward_ptr) or they beat us in
620 // self-forwarding it (old == forward_ptr).
621 assert(old == forward_ptr || !_g1h->is_in_cset(forward_ptr),
622 "Object " PTR_FORMAT " forwarded to: " PTR_FORMAT " "
|