Path: blob/master/src/hotspot/share/gc/parallel/psPromotionManager.inline.hpp
41149 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/prefetch.inline.hpp"4142inline PSPromotionManager* PSPromotionManager::manager_array(uint index) {43assert(_manager_array != NULL, "access of NULL manager_array");44assert(index <= ParallelGCThreads, "out of range manager_array access");45return &_manager_array[index];46}4748inline void PSPromotionManager::push_depth(ScannerTask task) {49claimed_stack_depth()->push(task);50}5152template <class T>53inline void PSPromotionManager::claim_or_forward_depth(T* p) {54assert(should_scavenge(p, true), "revisiting object?");55assert(ParallelScavengeHeap::heap()->is_in(p), "pointer outside heap");56oop obj = RawAccess<IS_NOT_NULL>::oop_load(p);57Prefetch::write(obj->mark_addr(), 0);58push_depth(ScannerTask(p));59}6061inline void PSPromotionManager::promotion_trace_event(oop new_obj, oop old_obj,62size_t obj_size,63uint age, bool tenured,64const PSPromotionLAB* lab) {65// Skip if memory allocation failed66if (new_obj != NULL) {67const ParallelScavengeTracer* gc_tracer = PSScavenge::gc_tracer();6869if (lab != NULL) {70// Promotion of object through newly allocated PLAB71if (gc_tracer->should_report_promotion_in_new_plab_event()) {72size_t obj_bytes = obj_size * HeapWordSize;73size_t lab_size = lab->capacity();74gc_tracer->report_promotion_in_new_plab_event(old_obj->klass(), obj_bytes,75age, tenured, lab_size);76}77} else {78// Promotion of object directly to heap79if (gc_tracer->should_report_promotion_outside_plab_event()) {80size_t obj_bytes = obj_size * HeapWordSize;81gc_tracer->report_promotion_outside_plab_event(old_obj->klass(), obj_bytes,82age, tenured);83}84}85}86}8788class PSPushContentsClosure: public BasicOopIterateClosure {89PSPromotionManager* _pm;90public:91PSPushContentsClosure(PSPromotionManager* pm) : BasicOopIterateClosure(PSScavenge::reference_processor()), _pm(pm) {}9293template <typename T> void do_oop_nv(T* p) {94if (PSScavenge::should_scavenge(p)) {95_pm->claim_or_forward_depth(p);96}97}9899virtual void do_oop(oop* p) { do_oop_nv(p); }100virtual void do_oop(narrowOop* p) { do_oop_nv(p); }101};102103//104// This closure specialization will override the one that is defined in105// instanceRefKlass.inline.cpp. It swaps the order of oop_oop_iterate and106// oop_oop_iterate_ref_processing. Unfortunately G1 and Parallel behaves107// significantly better (especially in the Derby benchmark) using opposite108// order of these function calls.109//110template <>111inline void InstanceRefKlass::oop_oop_iterate_reverse<oop, PSPushContentsClosure>(oop obj, PSPushContentsClosure* closure) {112oop_oop_iterate_ref_processing<oop>(obj, closure);113InstanceKlass::oop_oop_iterate_reverse<oop>(obj, closure);114}115116template <>117inline void InstanceRefKlass::oop_oop_iterate_reverse<narrowOop, PSPushContentsClosure>(oop obj, PSPushContentsClosure* closure) {118oop_oop_iterate_ref_processing<narrowOop>(obj, closure);119InstanceKlass::oop_oop_iterate_reverse<narrowOop>(obj, closure);120}121122inline void PSPromotionManager::push_contents(oop obj) {123if (!obj->klass()->is_typeArray_klass()) {124PSPushContentsClosure pcc(this);125obj->oop_iterate_backwards(&pcc);126}127}128//129// This method is pretty bulky. It would be nice to split it up130// into smaller submethods, but we need to be careful not to hurt131// performance.132//133template<bool promote_immediately>134inline oop PSPromotionManager::copy_to_survivor_space(oop o) {135assert(should_scavenge(&o), "Sanity");136137oop new_obj = NULL;138139// NOTE! We must be very careful with any methods that access the mark140// in o. There may be multiple threads racing on it, and it may be forwarded141// at any time. Do not use oop methods for accessing the mark!142markWord test_mark = o->mark();143144// The same test as "o->is_forwarded()"145if (!test_mark.is_marked()) {146bool new_obj_is_tenured = false;147size_t new_obj_size = o->size();148149// Find the objects age, MT safe.150uint age = (test_mark.has_displaced_mark_helper() /* o->has_displaced_mark() */) ?151test_mark.displaced_mark_helper().age() : test_mark.age();152153if (!promote_immediately) {154// Try allocating obj in to-space (unless too old)155if (age < PSScavenge::tenuring_threshold()) {156new_obj = cast_to_oop(_young_lab.allocate(new_obj_size));157if (new_obj == NULL && !_young_gen_is_full) {158// Do we allocate directly, or flush and refill?159if (new_obj_size > (YoungPLABSize / 2)) {160// Allocate this object directly161new_obj = cast_to_oop(young_space()->cas_allocate(new_obj_size));162promotion_trace_event(new_obj, o, new_obj_size, age, false, NULL);163} else {164// Flush and fill165_young_lab.flush();166167HeapWord* lab_base = young_space()->cas_allocate(YoungPLABSize);168if (lab_base != NULL) {169_young_lab.initialize(MemRegion(lab_base, YoungPLABSize));170// Try the young lab allocation again.171new_obj = cast_to_oop(_young_lab.allocate(new_obj_size));172promotion_trace_event(new_obj, o, new_obj_size, age, false, &_young_lab);173} else {174_young_gen_is_full = true;175}176}177}178}179}180181// Otherwise try allocating obj tenured182if (new_obj == NULL) {183#ifndef PRODUCT184if (ParallelScavengeHeap::heap()->promotion_should_fail()) {185return oop_promotion_failed(o, test_mark);186}187#endif // #ifndef PRODUCT188189new_obj = cast_to_oop(_old_lab.allocate(new_obj_size));190new_obj_is_tenured = true;191192if (new_obj == NULL) {193if (!_old_gen_is_full) {194// Do we allocate directly, or flush and refill?195if (new_obj_size > (OldPLABSize / 2)) {196// Allocate this object directly197new_obj = cast_to_oop(old_gen()->allocate(new_obj_size));198promotion_trace_event(new_obj, o, new_obj_size, age, true, NULL);199} else {200// Flush and fill201_old_lab.flush();202203HeapWord* lab_base = old_gen()->allocate(OldPLABSize);204if(lab_base != NULL) {205#ifdef ASSERT206// Delay the initialization of the promotion lab (plab).207// This exposes uninitialized plabs to card table processing.208if (GCWorkerDelayMillis > 0) {209os::naked_sleep(GCWorkerDelayMillis);210}211#endif212_old_lab.initialize(MemRegion(lab_base, OldPLABSize));213// Try the old lab allocation again.214new_obj = cast_to_oop(_old_lab.allocate(new_obj_size));215promotion_trace_event(new_obj, o, new_obj_size, age, true, &_old_lab);216}217}218}219220// This is the promotion failed test, and code handling.221// The code belongs here for two reasons. It is slightly222// different than the code below, and cannot share the223// CAS testing code. Keeping the code here also minimizes224// the impact on the common case fast path code.225226if (new_obj == NULL) {227_old_gen_is_full = true;228return oop_promotion_failed(o, test_mark);229}230}231}232233assert(new_obj != NULL, "allocation should have succeeded");234235// Copy obj236Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(o), cast_from_oop<HeapWord*>(new_obj), new_obj_size);237238// Now we have to CAS in the header.239// Make copy visible to threads reading the forwardee.240if (o->cas_forward_to(new_obj, test_mark, memory_order_release)) {241// We won any races, we "own" this object.242assert(new_obj == o->forwardee(), "Sanity");243244// Increment age if obj still in new generation. Now that245// we're dealing with a markWord that cannot change, it is246// okay to use the non mt safe oop methods.247if (!new_obj_is_tenured) {248new_obj->incr_age();249assert(young_space()->contains(new_obj), "Attempt to push non-promoted obj");250}251252// Do the size comparison first with new_obj_size, which we253// already have. Hopefully, only a few objects are larger than254// _min_array_size_for_chunking, and most of them will be arrays.255// So, the is->objArray() test would be very infrequent.256if (new_obj_size > _min_array_size_for_chunking &&257new_obj->is_objArray() &&258PSChunkLargeArrays) {259// we'll chunk it260push_depth(ScannerTask(PartialArrayScanTask(o)));261TASKQUEUE_STATS_ONLY(++_arrays_chunked; ++_array_chunk_pushes);262} else {263// we'll just push its contents264push_contents(new_obj);265}266} else {267// We lost, someone else "owns" this object268guarantee(o->is_forwarded(), "Object must be forwarded if the cas failed.");269270// Try to deallocate the space. If it was directly allocated we cannot271// deallocate it, so we have to test. If the deallocation fails,272// overwrite with a filler object.273if (new_obj_is_tenured) {274if (!_old_lab.unallocate_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size)) {275CollectedHeap::fill_with_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size);276}277} else if (!_young_lab.unallocate_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size)) {278CollectedHeap::fill_with_object(cast_from_oop<HeapWord*>(new_obj), new_obj_size);279}280281// don't update this before the unallocation!282// Using acquire though consume would be accurate for accessing new_obj.283new_obj = o->forwardee_acquire();284}285} else {286assert(o->is_forwarded(), "Sanity");287new_obj = o->forwardee_acquire();288}289290// This code must come after the CAS test, or it will print incorrect291// information.292log_develop_trace(gc, scavenge)("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (%d)}",293should_scavenge(&new_obj) ? "copying" : "tenuring",294new_obj->klass()->internal_name(), p2i((void *)o), p2i((void *)new_obj), new_obj->size());295296return new_obj;297}298299// Attempt to "claim" oop at p via CAS, push the new obj if successful300// This version tests the oop* to make sure it is within the heap before301// attempting marking.302template <bool promote_immediately, class T>303inline void PSPromotionManager::copy_and_push_safe_barrier(T* p) {304assert(should_scavenge(p, true), "revisiting object?");305306oop o = RawAccess<IS_NOT_NULL>::oop_load(p);307oop new_obj = o->is_forwarded()308? o->forwardee()309: copy_to_survivor_space<promote_immediately>(o);310311// This code must come after the CAS test, or it will print incorrect312// information.313if (log_develop_is_enabled(Trace, gc, scavenge) && o->is_forwarded()) {314log_develop_trace(gc, scavenge)("{%s %s " PTR_FORMAT " -> " PTR_FORMAT " (%d)}",315"forwarding",316new_obj->klass()->internal_name(), p2i((void *)o), p2i((void *)new_obj), new_obj->size());317}318319RawAccess<IS_NOT_NULL>::oop_store(p, new_obj);320321// We cannot mark without test, as some code passes us pointers322// that are outside the heap. These pointers are either from roots323// or from metadata.324if ((!PSScavenge::is_obj_in_young((HeapWord*)p)) &&325ParallelScavengeHeap::heap()->is_in_reserved(p)) {326if (PSScavenge::is_obj_in_young(new_obj)) {327PSScavenge::card_table()->inline_write_ref_field_gc(p, new_obj);328}329}330}331332inline void PSPromotionManager::process_popped_location_depth(ScannerTask task) {333if (task.is_partial_array_task()) {334assert(PSChunkLargeArrays, "invariant");335process_array_chunk(task.to_partial_array_task());336} else {337if (task.is_narrow_oop_ptr()) {338assert(UseCompressedOops, "Error");339copy_and_push_safe_barrier</*promote_immediately=*/false>(task.to_narrow_oop_ptr());340} else {341copy_and_push_safe_barrier</*promote_immediately=*/false>(task.to_oop_ptr());342}343}344}345346inline bool PSPromotionManager::steal_depth(int queue_num, ScannerTask& t) {347return stack_array_depth()->steal(queue_num, t);348}349350#if TASKQUEUE_STATS351void PSPromotionManager::record_steal(ScannerTask task) {352if (task.is_partial_array_task()) {353++_array_chunk_steals;354}355}356#endif // TASKQUEUE_STATS357358#endif // SHARE_GC_PARALLEL_PSPROMOTIONMANAGER_INLINE_HPP359360361