Path: blob/master/src/hotspot/share/gc/parallel/psPromotionManager.inline.hpp
66644 views
/*1* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_GC_PARALLEL_PSPROMOTIONMANAGER_INLINE_HPP25#define SHARE_GC_PARALLEL_PSPROMOTIONMANAGER_INLINE_HPP2627#include "gc/parallel/psPromotionManager.hpp"2829#include "gc/parallel/parallelScavengeHeap.hpp"30#include "gc/parallel/parMarkBitMap.inline.hpp"31#include "gc/parallel/psOldGen.hpp"32#include "gc/parallel/psPromotionLAB.inline.hpp"33#include "gc/parallel/psScavenge.inline.hpp"34#include "gc/shared/taskqueue.inline.hpp"35#include "gc/shared/tlab_globals.hpp"36#include "logging/log.hpp"37#include "memory/iterator.inline.hpp"38#include "oops/access.inline.hpp"39#include "oops/oop.inline.hpp"40#include "runtime/orderAccess.hpp"41#include "runtime/prefetch.inline.hpp"4243inline PSPromotionManager* PSPromotionManager::manager_array(uint index) {44assert(_manager_array != NULL, "access of NULL manager_array");45assert(index <= ParallelGCThreads, "out of range manager_array access");46return &_manager_array[index];47}4849inline void PSPromotionManager::push_depth(ScannerTask task) {50claimed_stack_depth()->push(task);51}5253template <class T>54inline void PSPromotionManager::claim_or_forward_depth(T* p) {55assert(should_scavenge(p, true), "revisiting object?");56assert(ParallelScavengeHeap::heap()->is_in(p), "pointer outside heap");57oop obj = RawAccess<IS_NOT_NULL>::oop_load(p);58Prefetch::write(obj->mark_addr(), 0);59push_depth(ScannerTask(p));60}6162inline void PSPromotionManager::promotion_trace_event(oop new_obj, oop old_obj,63size_t obj_size,64uint age, bool tenured,65const PSPromotionLAB* lab) {66// Skip if memory allocation failed67if (new_obj != NULL) {68const ParallelScavengeTracer* gc_tracer = PSScavenge::gc_tracer();6970if (lab != NULL) {71// Promotion of object through newly allocated PLAB72if (gc_tracer->should_report_promotion_in_new_plab_event()) {73size_t obj_bytes = obj_size * HeapWordSize;74size_t lab_size = lab->capacity();75gc_tracer->report_promotion_in_new_plab_event(old_obj->klass(), obj_bytes,76age, tenured, lab_size);77}78} else {79// Promotion of object directly to heap80if (gc_tracer->should_report_promotion_outside_plab_event()) {81size_t obj_bytes = obj_size * HeapWordSize;82gc_tracer->report_promotion_outside_plab_event(old_obj->klass(), obj_bytes,83age, tenured);84}85}86}87}8889class PSPushContentsClosure: public BasicOopIterateClosure {90PSPromotionManager* _pm;91public:92PSPushContentsClosure(PSPromotionManager* pm) : BasicOopIterateClosure(PSScavenge::reference_processor()), _pm(pm) {}9394template <typename T> void do_oop_nv(T* p) {95if (PSScavenge::should_scavenge(p)) {96_pm->claim_or_forward_depth(p);97}98}99100virtual void do_oop(oop* p) { do_oop_nv(p); }101virtual void do_oop(narrowOop* p) { do_oop_nv(p); }102};103104//105// This closure specialization will override the one that is defined in106// instanceRefKlass.inline.cpp. It swaps the order of oop_oop_iterate and107// oop_oop_iterate_ref_processing. Unfortunately G1 and Parallel behaves108// significantly better (especially in the Derby benchmark) using opposite109// order of these function calls.110//111template <>112inline void InstanceRefKlass::oop_oop_iterate_reverse<oop, PSPushContentsClosure>(oop obj, PSPushContentsClosure* closure) {113oop_oop_iterate_ref_processing<oop>(obj, closure);114InstanceKlass::oop_oop_iterate_reverse<oop>(obj, closure);115}116117template <>118inline void InstanceRefKlass::oop_oop_iterate_reverse<narrowOop, PSPushContentsClosure>(oop obj, PSPushContentsClosure* closure) {119oop_oop_iterate_ref_processing<narrowOop>(obj, closure);120InstanceKlass::oop_oop_iterate_reverse<narrowOop>(obj, closure);121}122123inline void PSPromotionManager::push_contents(oop obj) {124if (!obj->klass()->is_typeArray_klass()) {125PSPushContentsClosure pcc(this);126obj->oop_iterate_backwards(&pcc);127}128}129130template<bool promote_immediately>131inline oop PSPromotionManager::copy_to_survivor_space(oop o) {132assert(should_scavenge(&o), "Sanity");133134// NOTE! We must be very careful with any methods that access the mark135// in o. There may be multiple threads racing on it, and it may be forwarded136// at any time.137markWord m = o->mark();138if (!m.is_marked()) {139return copy_unmarked_to_survivor_space<promote_immediately>(o, m);140} else {141// Ensure any loads from the forwardee follow all changes that precede142// the release-cmpxchg that performed the forwarding, possibly in some143// other thread.144OrderAccess::acquire();145// Return the already installed forwardee.146return cast_to_oop(m.decode_pointer());147}148}149150//151// This method is pretty bulky. It would be nice to split it up152// into smaller submethods, but we need to be careful not to hurt153// performance.154//155template<bool promote_immediately>156inline oop PSPromotionManager::copy_unmarked_to_survivor_space(oop o,157markWord test_mark) {158assert(should_scavenge(&o), "Sanity");159160oop new_obj = NULL;161bool new_obj_is_tenured = false;162size_t new_obj_size = o->size();163164// Find the objects age, MT safe.165uint age = (test_mark.has_displaced_mark_helper() /* o->has_displaced_mark() */) ?166test_mark.displaced_mark_helper().age() : test_mark.age();167168if (!promote_immediately) {169// Try allocating obj in to-space (unless too old)170if (age < PSScavenge::tenuring_threshold()) {171new_obj = cast_to_oop(_young_lab.allocate(new_obj_size));172if (new_obj == NULL && !_young_gen_is_full) {173// Do we allocate directly, or flush and refill?174if (new_obj_size > (YoungPLABSize / 2)) {175// Allocate this object directly176new_obj = cast_to_oop(young_space()->cas_allocate(new_obj_size));177promotion_trace_event(new_obj, o, new_obj_size, age, false, NULL);178} else {179// Flush and fill180_young_lab.flush();181182HeapWord* lab_base = young_space()->cas_allocate(YoungPLABSize);183if (lab_base != NULL) {184_young_lab.initialize(MemRegion(lab_base, YoungPLABSize));185// Try the young lab allocation again.186new_obj = cast_to_oop(_young_lab.allocate(new_obj_size));187promotion_trace_event(new_obj, o, new_obj_size, age, false, &_young_lab);188} else {189_young_gen_is_full = true;190}191}192}193}194}195196// Otherwise try allocating obj tenured197if (new_obj == NULL) {198#ifndef PRODUCT199if (ParallelScavengeHeap::heap()->promotion_should_fail()) {200return oop_promotion_failed(o, test_mark);201}202#endif // #ifndef PRODUCT203204new_obj = cast_to_oop(_old_lab.allocate(new_obj_size));205new_obj_is_tenured = true;206207if (new_obj == NULL) {208if (!_old_gen_is_full) {209// Do we allocate directly, or flush and refill?210if (new_obj_size > (OldPLABSize / 2)) {211// Allocate this object directly212new_obj = cast_to_oop(old_gen()->allocate(new_obj_size));213promotion_trace_event(new_obj, o, new_obj_size, age, true, NULL);214} else {215// Flush and fill216_old_lab.flush();217218HeapWord* lab_base = old_gen()->allocate(OldPLABSize);219if(lab_base != NULL) {220#ifdef ASSERT221// Delay the initialization of the promotion lab (plab).222// This exposes uninitialized plabs to card table processing.223if (GCWorkerDelayMillis > 0) {224os::naked_sleep(GCWorkerDelayMillis);225}226#endif227_old_lab.initialize(MemRegion(lab_base, OldPLABSize));228// Try the old lab allocation again.229new_obj = cast_to_oop(_old_lab.allocate(new_obj_size));230promotion_trace_event(new_obj, o, new_obj_size, age, true, &_old_lab);231}232}233}234235// This is the promotion failed test, and code handling.236// The code belongs here for two reasons. It is slightly237// different than the code below, and cannot share the238// CAS testing code. Keeping the code here also minimizes239// the impact on the common case fast path code.240241if (new_obj == NULL) {242_old_gen_is_full = true;243return oop_promotion_failed(o, test_mark);244}245}246}247248assert(new_obj != NULL, "allocation should have succeeded");249250// Copy obj251Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(o), cast_from_oop<HeapWord*>(new_obj), new_obj_size);252253// Now we have to CAS in the header.254// Make copy visible to threads reading the forwardee.255oop forwardee = o->forward_to_atomic(new_obj, test_mark, memory_order_release);256if (forwardee == NULL) { // forwardee is NULL when forwarding is successful257// We won any races, we "own" this object.258assert(new_obj == o->forwardee(), "Sanity");259260// Increment age if obj still in new generation. Now that261// we're dealing with a markWord that cannot change, it is262// okay to use the non mt safe oop methods.263if (!new_obj_is_tenured) {264new_obj->incr_age();265assert(young_space()->contains(new_obj), "Attempt to push non-promoted obj");266}267268log_develop_trace(gc, scavenge)("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (%d)}",269new_obj_is_tenured ? "copying" : "tenuring",270new_obj->klass()->internal_name(),271p2i((void *)o), p2i((void *)new_obj), new_obj->size());272273// Do the size comparison first with new_obj_size, which we274// already have. Hopefully, only a few objects are larger than275// _min_array_size_for_chunking, and most of them will be arrays.276// So, the is->objArray() test would be very infrequent.277if (new_obj_size > _min_array_size_for_chunking &&278new_obj->is_objArray() &&279PSChunkLargeArrays) {280// we'll chunk it281push_depth(ScannerTask(PartialArrayScanTask(o)));282TASKQUEUE_STATS_ONLY(++_arrays_chunked; ++_array_chunk_pushes);283} else {284// we'll just push its contents285push_contents(new_obj);286}287return new_obj;288} else {289// We lost, someone else "owns" this object.290// Ensure loads from the forwardee follow all changes that preceeded the291// release-cmpxchg that performed the forwarding in another thread.292OrderAccess::acquire();293294assert(o->is_forwarded(), "Object must be forwarded if the cas failed.");295assert(o->forwardee() == forwardee, "invariant");296297// Try to deallocate the space. If it was directly allocated we cannot298// deallocate it, so we have to test. If the deallocation fails,299// overwrite with a filler object.300if (new_obj_is_tenured) {301if (!_old_lab.unallocate_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size)) {302CollectedHeap::fill_with_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size);303}304} else if (!_young_lab.unallocate_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size)) {305CollectedHeap::fill_with_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size);306}307return forwardee;308}309}310311// Attempt to "claim" oop at p via CAS, push the new obj if successful312// This version tests the oop* to make sure it is within the heap before313// attempting marking.314template <bool promote_immediately, class T>315inline void PSPromotionManager::copy_and_push_safe_barrier(T* p) {316assert(should_scavenge(p, true), "revisiting object?");317318oop o = RawAccess<IS_NOT_NULL>::oop_load(p);319oop new_obj = copy_to_survivor_space<promote_immediately>(o);320RawAccess<IS_NOT_NULL>::oop_store(p, new_obj);321322// We cannot mark without test, as some code passes us pointers323// that are outside the heap. These pointers are either from roots324// or from metadata.325if ((!PSScavenge::is_obj_in_young((HeapWord*)p)) &&326ParallelScavengeHeap::heap()->is_in_reserved(p)) {327if (PSScavenge::is_obj_in_young(new_obj)) {328PSScavenge::card_table()->inline_write_ref_field_gc(p, new_obj);329}330}331}332333inline void PSPromotionManager::process_popped_location_depth(ScannerTask task) {334if (task.is_partial_array_task()) {335assert(PSChunkLargeArrays, "invariant");336process_array_chunk(task.to_partial_array_task());337} else {338if (task.is_narrow_oop_ptr()) {339assert(UseCompressedOops, "Error");340copy_and_push_safe_barrier</*promote_immediately=*/false>(task.to_narrow_oop_ptr());341} else {342copy_and_push_safe_barrier</*promote_immediately=*/false>(task.to_oop_ptr());343}344}345}346347inline bool PSPromotionManager::steal_depth(int queue_num, ScannerTask& t) {348return stack_array_depth()->steal(queue_num, t);349}350351#if TASKQUEUE_STATS352void PSPromotionManager::record_steal(ScannerTask task) {353if (task.is_partial_array_task()) {354++_array_chunk_steals;355}356}357#endif // TASKQUEUE_STATS358359#endif // SHARE_GC_PARALLEL_PSPROMOTIONMANAGER_INLINE_HPP360361362