Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/gc_interface/collectedHeap.inline.hpp
32285 views
/*1* Copyright (c) 2001, 2020, 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_VM_GC_INTERFACE_COLLECTEDHEAP_INLINE_HPP25#define SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_INLINE_HPP2627#include "gc_interface/allocTracer.hpp"28#include "gc_interface/collectedHeap.hpp"29#include "memory/threadLocalAllocBuffer.inline.hpp"30#include "memory/universe.hpp"31#include "oops/arrayOop.hpp"32#include "prims/jvmtiExport.hpp"33#include "runtime/sharedRuntime.hpp"34#include "runtime/thread.inline.hpp"35#include "services/lowMemoryDetector.hpp"36#include "utilities/copy.hpp"3738// Inline allocation implementations.3940void CollectedHeap::post_allocation_setup_common(KlassHandle klass,41HeapWord* obj_ptr) {42post_allocation_setup_no_klass_install(klass, obj_ptr);43oop obj = (oop)obj_ptr;44#if ! INCLUDE_ALL_GCS45obj->set_klass(klass());46#else47// Need a release store to ensure array/class length, mark word, and48// object zeroing are visible before setting the klass non-NULL, for49// concurrent collectors.50obj->release_set_klass(klass());51#endif52}5354void CollectedHeap::post_allocation_setup_no_klass_install(KlassHandle klass,55HeapWord* obj_ptr) {56oop obj = (oop)obj_ptr;5758assert(obj != NULL, "NULL object pointer");59if (UseBiasedLocking && (klass() != NULL)) {60obj->set_mark(klass->prototype_header());61} else {62// May be bootstrapping63obj->set_mark(markOopDesc::prototype());64}65}6667inline void send_jfr_allocation_event(KlassHandle klass, HeapWord* obj, size_t size) {68Thread* t = Thread::current();69ThreadLocalAllocBuffer& tlab = t->tlab();70if (obj == tlab.start()) {71// allocate in new TLAB72size_t new_tlab_size = tlab.hard_size_bytes();73AllocTracer::send_allocation_in_new_tlab_event(klass, obj, new_tlab_size, size * HeapWordSize, t);74} else if (!tlab.in_used(obj)) {75// allocate outside TLAB76AllocTracer::send_allocation_outside_tlab_event(klass, obj, size * HeapWordSize, t);77}78}7980// Support for jvmti, dtrace and jfr81inline void post_allocation_notify(KlassHandle klass, oop obj, int size) {82send_jfr_allocation_event(klass, (HeapWord*)obj, size);83// support low memory notifications (no-op if not enabled)84LowMemoryDetector::detect_low_memory_for_collected_pools();8586// support for JVMTI VMObjectAlloc event (no-op if not enabled)87JvmtiExport::vm_object_alloc_event_collector(obj);8889if (DTraceAllocProbes) {90// support for Dtrace object alloc event (no-op most of the time)91if (klass() != NULL && klass()->name() != NULL) {92SharedRuntime::dtrace_object_alloc(obj, size);93}94}95}9697void CollectedHeap::post_allocation_setup_obj(KlassHandle klass,98HeapWord* obj_ptr,99int size) {100post_allocation_setup_common(klass, obj_ptr);101oop obj = (oop)obj_ptr;102assert(Universe::is_bootstrapping() ||103!obj->is_array(), "must not be an array");104// notify jvmti and dtrace105post_allocation_notify(klass, obj, size);106}107108void CollectedHeap::post_allocation_setup_array(KlassHandle klass,109HeapWord* obj_ptr,110int length) {111// Set array length before setting the _klass field because a112// non-NULL klass field indicates that the object is parsable by113// concurrent GC.114assert(length >= 0, "length should be non-negative");115((arrayOop)obj_ptr)->set_length(length);116post_allocation_setup_common(klass, obj_ptr);117oop new_obj = (oop)obj_ptr;118assert(new_obj->is_array(), "must be an array");119// notify jvmti and dtrace (must be after length is set for dtrace)120post_allocation_notify(klass, new_obj, new_obj->size());121}122123HeapWord* CollectedHeap::common_mem_allocate_noinit(KlassHandle klass, size_t size, TRAPS) {124125// Clear unhandled oops for memory allocation. Memory allocation might126// not take out a lock if from tlab, so clear here.127CHECK_UNHANDLED_OOPS_ONLY(THREAD->clear_unhandled_oops();)128129if (HAS_PENDING_EXCEPTION) {130NOT_PRODUCT(guarantee(false, "Should not allocate with exception pending"));131return NULL; // caller does a CHECK_0 too132}133134HeapWord* result = NULL;135if (UseTLAB) {136result = allocate_from_tlab(klass, THREAD, size);137if (result != NULL) {138assert(!HAS_PENDING_EXCEPTION,139"Unexpected exception, will result in uninitialized storage");140return result;141}142}143bool gc_overhead_limit_was_exceeded = false;144result = Universe::heap()->mem_allocate(size,145&gc_overhead_limit_was_exceeded);146if (result != NULL) {147NOT_PRODUCT(Universe::heap()->148check_for_non_bad_heap_word_value(result, size));149assert(!HAS_PENDING_EXCEPTION,150"Unexpected exception, will result in uninitialized storage");151THREAD->incr_allocated_bytes(size * HeapWordSize);152153return result;154}155156157if (!gc_overhead_limit_was_exceeded) {158// -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support159report_java_out_of_memory("Java heap space");160161if (JvmtiExport::should_post_resource_exhausted()) {162JvmtiExport::post_resource_exhausted(163JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,164"Java heap space");165}166167THROW_OOP_0(Universe::out_of_memory_error_java_heap());168} else {169// -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support170report_java_out_of_memory("GC overhead limit exceeded");171172if (JvmtiExport::should_post_resource_exhausted()) {173JvmtiExport::post_resource_exhausted(174JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,175"GC overhead limit exceeded");176}177178THROW_OOP_0(Universe::out_of_memory_error_gc_overhead_limit());179}180}181182HeapWord* CollectedHeap::common_mem_allocate_init(KlassHandle klass, size_t size, TRAPS) {183HeapWord* obj = common_mem_allocate_noinit(klass, size, CHECK_NULL);184init_obj(obj, size);185return obj;186}187188HeapWord* CollectedHeap::allocate_from_tlab(KlassHandle klass, Thread* thread, size_t size) {189assert(UseTLAB, "should use UseTLAB");190191HeapWord* obj = thread->tlab().allocate(size);192if (obj != NULL) {193return obj;194}195// Otherwise...196return allocate_from_tlab_slow(klass, thread, size);197}198199void CollectedHeap::init_obj(HeapWord* obj, size_t size) {200assert(obj != NULL, "cannot initialize NULL object");201const size_t hs = oopDesc::header_size();202assert(size >= hs, "unexpected object size");203((oop)obj)->set_klass_gap(0);204Copy::fill_to_aligned_words(obj + hs, size - hs);205}206207oop CollectedHeap::obj_allocate(KlassHandle klass, int size, TRAPS) {208debug_only(check_for_valid_allocation_state());209assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");210assert(size >= 0, "int won't convert to size_t");211HeapWord* obj = common_mem_allocate_init(klass, size, CHECK_NULL);212post_allocation_setup_obj(klass, obj, size);213NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));214return (oop)obj;215}216217oop CollectedHeap::array_allocate(KlassHandle klass,218int size,219int length,220TRAPS) {221debug_only(check_for_valid_allocation_state());222assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");223assert(size >= 0, "int won't convert to size_t");224HeapWord* obj = common_mem_allocate_init(klass, size, CHECK_NULL);225post_allocation_setup_array(klass, obj, length);226NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));227return (oop)obj;228}229230oop CollectedHeap::array_allocate_nozero(KlassHandle klass,231int size,232int length,233TRAPS) {234debug_only(check_for_valid_allocation_state());235assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");236assert(size >= 0, "int won't convert to size_t");237HeapWord* obj = common_mem_allocate_noinit(klass, size, CHECK_NULL);238((oop)obj)->set_klass_gap(0);239post_allocation_setup_array(klass, obj, length);240#ifndef PRODUCT241const size_t hs = oopDesc::header_size()+1;242Universe::heap()->check_for_non_bad_heap_word_value(obj+hs, size-hs);243#endif244return (oop)obj;245}246247inline void CollectedHeap::oop_iterate_no_header(OopClosure* cl) {248NoHeaderExtendedOopClosure no_header_cl(cl);249oop_iterate(&no_header_cl);250}251252253inline HeapWord* CollectedHeap::align_allocation_or_fail(HeapWord* addr,254HeapWord* end,255unsigned short alignment_in_bytes) {256if (alignment_in_bytes <= ObjectAlignmentInBytes) {257return addr;258}259260assert(is_ptr_aligned(addr, HeapWordSize),261err_msg("Address " PTR_FORMAT " is not properly aligned.", p2i(addr)));262assert(is_size_aligned(alignment_in_bytes, HeapWordSize),263err_msg("Alignment size %u is incorrect.", alignment_in_bytes));264265HeapWord* new_addr = (HeapWord*) align_pointer_up(addr, alignment_in_bytes);266size_t padding = pointer_delta(new_addr, addr);267268if (padding == 0) {269return addr;270}271272if (padding < CollectedHeap::min_fill_size()) {273padding += alignment_in_bytes / HeapWordSize;274assert(padding >= CollectedHeap::min_fill_size(),275err_msg("alignment_in_bytes %u is expect to be larger "276"than the minimum object size", alignment_in_bytes));277new_addr = addr + padding;278}279280assert(new_addr > addr, err_msg("Unexpected arithmetic overflow "281PTR_FORMAT " not greater than " PTR_FORMAT, p2i(new_addr), p2i(addr)));282if(new_addr < end) {283CollectedHeap::fill_with_object(addr, padding);284return new_addr;285} else {286return NULL;287}288}289290#ifndef PRODUCT291292inline bool293CollectedHeap::promotion_should_fail(volatile size_t* count) {294// Access to count is not atomic; the value does not have to be exact.295if (PromotionFailureALot) {296const size_t gc_num = total_collections();297const size_t elapsed_gcs = gc_num - _promotion_failure_alot_gc_number;298if (elapsed_gcs >= PromotionFailureALotInterval) {299// Test for unsigned arithmetic wrap-around.300if (++*count >= PromotionFailureALotCount) {301*count = 0;302return true;303}304}305}306return false;307}308309inline bool CollectedHeap::promotion_should_fail() {310return promotion_should_fail(&_promotion_failure_alot_count);311}312313inline void CollectedHeap::reset_promotion_should_fail(volatile size_t* count) {314if (PromotionFailureALot) {315_promotion_failure_alot_gc_number = total_collections();316*count = 0;317}318}319320inline void CollectedHeap::reset_promotion_should_fail() {321reset_promotion_should_fail(&_promotion_failure_alot_count);322}323#endif // #ifndef PRODUCT324325#endif // SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_INLINE_HPP326327328