0N/A/*
3643N/A * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_INLINE_HPP
1879N/A#define SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_INLINE_HPP
1879N/A
4359N/A#include "gc_interface/allocTracer.hpp"
1879N/A#include "gc_interface/collectedHeap.hpp"
1879N/A#include "memory/threadLocalAllocBuffer.inline.hpp"
1879N/A#include "memory/universe.hpp"
1879N/A#include "oops/arrayOop.hpp"
1879N/A#include "prims/jvmtiExport.hpp"
1879N/A#include "runtime/sharedRuntime.hpp"
1879N/A#include "runtime/thread.hpp"
1879N/A#include "services/lowMemoryDetector.hpp"
1879N/A#include "utilities/copy.hpp"
1879N/A#ifdef TARGET_OS_FAMILY_linux
1879N/A# include "thread_linux.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_solaris
1879N/A# include "thread_solaris.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_windows
1879N/A# include "thread_windows.inline.hpp"
1879N/A#endif
2796N/A#ifdef TARGET_OS_FAMILY_bsd
2796N/A# include "thread_bsd.inline.hpp"
2796N/A#endif
1879N/A
0N/A// Inline allocation implementations.
0N/A
0N/Avoid CollectedHeap::post_allocation_setup_common(KlassHandle klass,
3643N/A HeapWord* obj) {
3643N/A post_allocation_setup_no_klass_install(klass, obj);
3643N/A post_allocation_install_obj_klass(klass, oop(obj));
0N/A}
0N/A
0N/Avoid CollectedHeap::post_allocation_setup_no_klass_install(KlassHandle klass,
3643N/A HeapWord* objPtr) {
0N/A oop obj = (oop)objPtr;
0N/A
0N/A assert(obj != NULL, "NULL object pointer");
0N/A if (UseBiasedLocking && (klass() != NULL)) {
0N/A obj->set_mark(klass->prototype_header());
0N/A } else {
0N/A // May be bootstrapping
0N/A obj->set_mark(markOopDesc::prototype());
0N/A }
0N/A}
0N/A
0N/Avoid CollectedHeap::post_allocation_install_obj_klass(KlassHandle klass,
3643N/A oop obj) {
0N/A // These asserts are kind of complicated because of klassKlass
0N/A // and the beginning of the world.
0N/A assert(klass() != NULL || !Universe::is_fully_initialized(), "NULL klass");
0N/A assert(klass() == NULL || klass()->is_klass(), "not a klass");
0N/A assert(klass() == NULL || klass()->klass_part() != NULL, "not a klass");
0N/A assert(obj != NULL, "NULL object pointer");
0N/A obj->set_klass(klass());
0N/A assert(!Universe::is_fully_initialized() || obj->blueprint() != NULL,
0N/A "missing blueprint");
113N/A}
0N/A
113N/A// Support for jvmti and dtrace
113N/Ainline void post_allocation_notify(KlassHandle klass, oop obj) {
481N/A // support low memory notifications (no-op if not enabled)
481N/A LowMemoryDetector::detect_low_memory_for_collected_pools();
481N/A
0N/A // support for JVMTI VMObjectAlloc event (no-op if not enabled)
0N/A JvmtiExport::vm_object_alloc_event_collector(obj);
0N/A
0N/A if (DTraceAllocProbes) {
0N/A // support for Dtrace object alloc event (no-op most of the time)
0N/A if (klass() != NULL && klass()->klass_part()->name() != NULL) {
0N/A SharedRuntime::dtrace_object_alloc(obj);
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid CollectedHeap::post_allocation_setup_obj(KlassHandle klass,
3643N/A HeapWord* obj) {
3643N/A post_allocation_setup_common(klass, obj);
0N/A assert(Universe::is_bootstrapping() ||
0N/A !((oop)obj)->blueprint()->oop_is_array(), "must not be an array");
113N/A // notify jvmti and dtrace
113N/A post_allocation_notify(klass, (oop)obj);
0N/A}
0N/A
0N/Avoid CollectedHeap::post_allocation_setup_array(KlassHandle klass,
0N/A HeapWord* obj,
0N/A int length) {
167N/A // Set array length before setting the _klass field
167N/A // in post_allocation_setup_common() because the klass field
167N/A // indicates that the object is parsable by concurrent GC.
0N/A assert(length >= 0, "length should be non-negative");
167N/A ((arrayOop)obj)->set_length(length);
3643N/A post_allocation_setup_common(klass, obj);
0N/A assert(((oop)obj)->blueprint()->oop_is_array(), "must be an array");
113N/A // notify jvmti and dtrace (must be after length is set for dtrace)
113N/A post_allocation_notify(klass, (oop)obj);
0N/A}
0N/A
4359N/AHeapWord* CollectedHeap::common_mem_allocate_noinit(KlassHandle klass, size_t size, TRAPS) {
0N/A
0N/A // Clear unhandled oops for memory allocation. Memory allocation might
0N/A // not take out a lock if from tlab, so clear here.
0N/A CHECK_UNHANDLED_OOPS_ONLY(THREAD->clear_unhandled_oops();)
0N/A
0N/A if (HAS_PENDING_EXCEPTION) {
0N/A NOT_PRODUCT(guarantee(false, "Should not allocate with exception pending"));
0N/A return NULL; // caller does a CHECK_0 too
0N/A }
0N/A
0N/A HeapWord* result = NULL;
0N/A if (UseTLAB) {
4359N/A result = allocate_from_tlab(klass, THREAD, size);
0N/A if (result != NULL) {
0N/A assert(!HAS_PENDING_EXCEPTION,
0N/A "Unexpected exception, will result in uninitialized storage");
0N/A return result;
0N/A }
0N/A }
342N/A bool gc_overhead_limit_was_exceeded = false;
0N/A result = Universe::heap()->mem_allocate(size,
0N/A &gc_overhead_limit_was_exceeded);
0N/A if (result != NULL) {
0N/A NOT_PRODUCT(Universe::heap()->
0N/A check_for_non_bad_heap_word_value(result, size));
0N/A assert(!HAS_PENDING_EXCEPTION,
0N/A "Unexpected exception, will result in uninitialized storage");
1988N/A THREAD->incr_allocated_bytes(size * HeapWordSize);
4359N/A
4359N/A AllocTracer::send_allocation_outside_tlab_event(klass, size * HeapWordSize);
4359N/A
0N/A return result;
0N/A }
0N/A
0N/A
0N/A if (!gc_overhead_limit_was_exceeded) {
0N/A // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
0N/A report_java_out_of_memory("Java heap space");
0N/A
0N/A if (JvmtiExport::should_post_resource_exhausted()) {
0N/A JvmtiExport::post_resource_exhausted(
0N/A JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
0N/A "Java heap space");
0N/A }
0N/A
0N/A THROW_OOP_0(Universe::out_of_memory_error_java_heap());
0N/A } else {
0N/A // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
0N/A report_java_out_of_memory("GC overhead limit exceeded");
0N/A
0N/A if (JvmtiExport::should_post_resource_exhausted()) {
0N/A JvmtiExport::post_resource_exhausted(
0N/A JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,
0N/A "GC overhead limit exceeded");
0N/A }
0N/A
0N/A THROW_OOP_0(Universe::out_of_memory_error_gc_overhead_limit());
0N/A }
0N/A}
0N/A
4359N/AHeapWord* CollectedHeap::common_mem_allocate_init(KlassHandle klass, size_t size, TRAPS) {
4359N/A HeapWord* obj = common_mem_allocate_noinit(klass, size, CHECK_NULL);
0N/A init_obj(obj, size);
0N/A return obj;
0N/A}
0N/A
0N/A// Need to investigate, do we really want to throw OOM exception here?
0N/AHeapWord* CollectedHeap::common_permanent_mem_allocate_noinit(size_t size, TRAPS) {
0N/A if (HAS_PENDING_EXCEPTION) {
0N/A NOT_PRODUCT(guarantee(false, "Should not allocate with exception pending"));
0N/A return NULL; // caller does a CHECK_NULL too
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A if (CIFireOOMAt > 0 && THREAD->is_Compiler_thread() &&
0N/A ++_fire_out_of_memory_count >= CIFireOOMAt) {
0N/A // For testing of OOM handling in the CI throw an OOM and see how
0N/A // it does. Historically improper handling of these has resulted
0N/A // in crashes which we really don't want to have in the CI.
0N/A THROW_OOP_0(Universe::out_of_memory_error_perm_gen());
0N/A }
0N/A#endif
0N/A
0N/A HeapWord* result = Universe::heap()->permanent_mem_allocate(size);
0N/A if (result != NULL) {
0N/A NOT_PRODUCT(Universe::heap()->
0N/A check_for_non_bad_heap_word_value(result, size));
0N/A assert(!HAS_PENDING_EXCEPTION,
0N/A "Unexpected exception, will result in uninitialized storage");
0N/A return result;
0N/A }
0N/A // -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError support
0N/A report_java_out_of_memory("PermGen space");
0N/A
0N/A if (JvmtiExport::should_post_resource_exhausted()) {
0N/A JvmtiExport::post_resource_exhausted(
0N/A JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR,
0N/A "PermGen space");
0N/A }
0N/A
0N/A THROW_OOP_0(Universe::out_of_memory_error_perm_gen());
0N/A}
0N/A
0N/AHeapWord* CollectedHeap::common_permanent_mem_allocate_init(size_t size, TRAPS) {
0N/A HeapWord* obj = common_permanent_mem_allocate_noinit(size, CHECK_NULL);
0N/A init_obj(obj, size);
0N/A return obj;
0N/A}
0N/A
4359N/AHeapWord* CollectedHeap::allocate_from_tlab(KlassHandle klass, Thread* thread, size_t size) {
0N/A assert(UseTLAB, "should use UseTLAB");
0N/A
0N/A HeapWord* obj = thread->tlab().allocate(size);
0N/A if (obj != NULL) {
0N/A return obj;
0N/A }
0N/A // Otherwise...
4359N/A return allocate_from_tlab_slow(klass, thread, size);
0N/A}
0N/A
0N/Avoid CollectedHeap::init_obj(HeapWord* obj, size_t size) {
0N/A assert(obj != NULL, "cannot initialize NULL object");
0N/A const size_t hs = oopDesc::header_size();
0N/A assert(size >= hs, "unexpected object size");
167N/A ((oop)obj)->set_klass_gap(0);
0N/A Copy::fill_to_aligned_words(obj + hs, size - hs);
0N/A}
0N/A
0N/Aoop CollectedHeap::obj_allocate(KlassHandle klass, int size, TRAPS) {
0N/A debug_only(check_for_valid_allocation_state());
0N/A assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
0N/A assert(size >= 0, "int won't convert to size_t");
4359N/A HeapWord* obj = common_mem_allocate_init(klass, size, CHECK_NULL);
3643N/A post_allocation_setup_obj(klass, obj);
0N/A NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
0N/A return (oop)obj;
0N/A}
0N/A
0N/Aoop CollectedHeap::array_allocate(KlassHandle klass,
0N/A int size,
0N/A int length,
0N/A TRAPS) {
0N/A debug_only(check_for_valid_allocation_state());
0N/A assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
0N/A assert(size >= 0, "int won't convert to size_t");
4359N/A HeapWord* obj = common_mem_allocate_init(klass, size, CHECK_NULL);
3643N/A post_allocation_setup_array(klass, obj, length);
0N/A NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
0N/A return (oop)obj;
0N/A}
0N/A
2797N/Aoop CollectedHeap::array_allocate_nozero(KlassHandle klass,
2797N/A int size,
2797N/A int length,
2797N/A TRAPS) {
2797N/A debug_only(check_for_valid_allocation_state());
2797N/A assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
2797N/A assert(size >= 0, "int won't convert to size_t");
4359N/A HeapWord* obj = common_mem_allocate_noinit(klass, size, CHECK_NULL);
2797N/A ((oop)obj)->set_klass_gap(0);
3643N/A post_allocation_setup_array(klass, obj, length);
2797N/A#ifndef PRODUCT
2797N/A const size_t hs = oopDesc::header_size()+1;
2797N/A Universe::heap()->check_for_non_bad_heap_word_value(obj+hs, size-hs);
2797N/A#endif
2797N/A return (oop)obj;
2797N/A}
2797N/A
0N/Aoop CollectedHeap::permanent_obj_allocate(KlassHandle klass, int size, TRAPS) {
0N/A oop obj = permanent_obj_allocate_no_klass_install(klass, size, CHECK_NULL);
3643N/A post_allocation_install_obj_klass(klass, obj);
0N/A NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value((HeapWord*) obj,
0N/A size));
0N/A return obj;
0N/A}
0N/A
0N/Aoop CollectedHeap::permanent_obj_allocate_no_klass_install(KlassHandle klass,
0N/A int size,
0N/A TRAPS) {
0N/A debug_only(check_for_valid_allocation_state());
0N/A assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
0N/A assert(size >= 0, "int won't convert to size_t");
0N/A HeapWord* obj = common_permanent_mem_allocate_init(size, CHECK_NULL);
3643N/A post_allocation_setup_no_klass_install(klass, obj);
2726N/A#ifndef PRODUCT
2726N/A const size_t hs = oopDesc::header_size();
2726N/A Universe::heap()->check_for_bad_heap_word_value(obj+hs, size-hs);
2726N/A#endif
0N/A return (oop)obj;
0N/A}
0N/A
0N/Aoop CollectedHeap::permanent_array_allocate(KlassHandle klass,
0N/A int size,
0N/A int length,
0N/A TRAPS) {
0N/A debug_only(check_for_valid_allocation_state());
0N/A assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
0N/A assert(size >= 0, "int won't convert to size_t");
0N/A HeapWord* obj = common_permanent_mem_allocate_init(size, CHECK_NULL);
3643N/A post_allocation_setup_array(klass, obj, length);
0N/A NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
0N/A return (oop)obj;
0N/A}
0N/A
0N/A// Returns "TRUE" if "p" is a method oop in the
0N/A// current heap with high probability. NOTE: The main
0N/A// current consumers of this interface are Forte::
0N/A// and ThreadProfiler::. In these cases, the
0N/A// interpreter frame from which "p" came, may be
0N/A// under construction when sampled asynchronously, so
0N/A// the clients want to check that it represents a
0N/A// valid method before using it. Nonetheless since
0N/A// the clients do not typically lock out GC, the
0N/A// predicate is_valid_method() is not stable, so
0N/A// it is possible that by the time "p" is used, it
0N/A// is no longer valid.
0N/Ainline bool CollectedHeap::is_valid_method(oop p) const {
0N/A return
0N/A p != NULL &&
0N/A
0N/A // Check whether it is aligned at a HeapWord boundary.
0N/A Space::is_aligned(p) &&
0N/A
0N/A // Check whether "method" is in the allocated part of the
0N/A // permanent generation -- this needs to be checked before
0N/A // p->klass() below to avoid a SEGV (but see below
0N/A // for a potential window of vulnerability).
0N/A is_permanent((void*)p) &&
0N/A
0N/A // See if GC is active; however, there is still an
0N/A // apparently unavoidable window after this call
0N/A // and before the client of this interface uses "p".
0N/A // If the client chooses not to lock out GC, then
0N/A // it's a risk the client must accept.
0N/A !is_gc_active() &&
0N/A
0N/A // Check that p is a methodOop.
4141N/A p->unsafe_klass_or_null() == Universe::methodKlassObj();
0N/A}
0N/A
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/Ainline bool
0N/ACollectedHeap::promotion_should_fail(volatile size_t* count) {
0N/A // Access to count is not atomic; the value does not have to be exact.
0N/A if (PromotionFailureALot) {
0N/A const size_t gc_num = total_collections();
0N/A const size_t elapsed_gcs = gc_num - _promotion_failure_alot_gc_number;
0N/A if (elapsed_gcs >= PromotionFailureALotInterval) {
0N/A // Test for unsigned arithmetic wrap-around.
0N/A if (++*count >= PromotionFailureALotCount) {
0N/A *count = 0;
0N/A return true;
0N/A }
0N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
0N/Ainline bool CollectedHeap::promotion_should_fail() {
0N/A return promotion_should_fail(&_promotion_failure_alot_count);
0N/A}
0N/A
0N/Ainline void CollectedHeap::reset_promotion_should_fail(volatile size_t* count) {
0N/A if (PromotionFailureALot) {
0N/A _promotion_failure_alot_gc_number = total_collections();
0N/A *count = 0;
0N/A }
0N/A}
0N/A
0N/Ainline void CollectedHeap::reset_promotion_should_fail() {
0N/A reset_promotion_should_fail(&_promotion_failure_alot_count);
0N/A}
0N/A#endif // #ifndef PRODUCT
1879N/A
1879N/A#endif // SHARE_VM_GC_INTERFACE_COLLECTEDHEAP_INLINE_HPP