collectedHeap.cpp revision 2845
0N/A/*
4596N/A * Copyright (c) 2001, 2011, 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
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/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 *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A *
2362N/A */
0N/A
0N/A#include "precompiled.hpp"
0N/A#include "classfile/systemDictionary.hpp"
0N/A#include "gc_implementation/shared/vmGCOperations.hpp"
0N/A#include "gc_interface/collectedHeap.hpp"
0N/A#include "gc_interface/collectedHeap.inline.hpp"
0N/A#include "oops/oop.inline.hpp"
0N/A#include "oops/instanceMirrorKlass.hpp"
0N/A#include "runtime/init.hpp"
0N/A#include "services/heapDumper.hpp"
0N/A#ifdef TARGET_OS_FAMILY_linux
0N/A# include "thread_linux.inline.hpp"
0N/A#endif
0N/A#ifdef TARGET_OS_FAMILY_solaris
0N/A# include "thread_solaris.inline.hpp"
0N/A#endif
0N/A#ifdef TARGET_OS_FAMILY_windows
0N/A# include "thread_windows.inline.hpp"
0N/A#endif
0N/A#ifdef TARGET_OS_FAMILY_bsd
0N/A# include "thread_bsd.inline.hpp"
0N/A#endif
0N/A
0N/A
0N/A#ifdef ASSERT
0N/Aint CollectedHeap::_fire_out_of_memory_count = 0;
0N/A#endif
0N/A
0N/Asize_t CollectedHeap::_filler_array_max_size = 0;
0N/A
0N/A// Memory state functions.
0N/A
0N/A
0N/ACollectedHeap::CollectedHeap() : _n_par_threads(0)
0N/A
0N/A{
0N/A const size_t max_len = size_t(arrayOopDesc::max_array_length(T_INT));
0N/A const size_t elements_per_word = HeapWordSize / sizeof(jint);
1460N/A _filler_array_max_size = align_object_size(filler_array_hdr_size() +
1460N/A max_len * elements_per_word);
1460N/A
1460N/A _barrier_set = NULL;
0N/A _is_gc_active = false;
1460N/A _total_collections = _total_full_collections = 0;
1460N/A _gc_cause = _gc_lastcause = GCCause::_no_gc;
1460N/A NOT_PRODUCT(_promotion_failure_alot_count = 0;)
0N/A NOT_PRODUCT(_promotion_failure_alot_gc_number = 0;)
0N/A
0N/A if (UsePerfData) {
1460N/A EXCEPTION_MARK;
1460N/A
0N/A // create the gc cause jvmstat counters
1460N/A _perf_gc_cause = PerfDataManager::create_string_variable(SUN_GC, "cause",
0N/A 80, GCCause::to_string(_gc_cause), CHECK);
0N/A
0N/A _perf_gc_lastcause =
1460N/A PerfDataManager::create_string_variable(SUN_GC, "lastCause",
1460N/A 80, GCCause::to_string(_gc_lastcause), CHECK);
0N/A }
1460N/A _defer_initial_card_mark = false; // strengthened by subclass in pre_initialize() below.
1460N/A}
0N/A
1460N/Avoid CollectedHeap::pre_initialize() {
0N/A // Used for ReduceInitialCardMarks (when COMPILER2 is used);
0N/A // otherwise remains unused.
0N/A#ifdef COMPILER2
0N/A _defer_initial_card_mark = ReduceInitialCardMarks && can_elide_tlab_store_barriers()
0N/A && (DeferInitialCardMark || card_mark_must_follow_store());
0N/A#else
0N/A assert(_defer_initial_card_mark == false, "Who would set it?");
0N/A#endif
160N/A}
160N/A
160N/A#ifndef PRODUCT
160N/Avoid CollectedHeap::check_for_bad_heap_word_value(HeapWord* addr, size_t size) {
160N/A if (CheckMemoryInitialization && ZapUnusedHeapArea) {
160N/A for (size_t slot = 0; slot < size; slot += 1) {
160N/A assert((*(intptr_t*) (addr + slot)) != ((intptr_t) badHeapWordVal),
160N/A "Found badHeapWordValue in post-allocation check");
160N/A }
160N/A }
160N/A}
160N/A
160N/Avoid CollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr, size_t size) {
160N/A if (CheckMemoryInitialization && ZapUnusedHeapArea) {
160N/A for (size_t slot = 0; slot < size; slot += 1) {
160N/A assert((*(intptr_t*) (addr + slot)) == ((intptr_t) badHeapWordVal),
160N/A "Found non badHeapWordValue in pre-allocation check");
160N/A }
0N/A }
0N/A}
0N/A#endif // PRODUCT
0N/A
0N/A#ifdef ASSERT
0N/Avoid CollectedHeap::check_for_valid_allocation_state() {
0N/A Thread *thread = Thread::current();
0N/A // How to choose between a pending exception and a potential
0N/A // OutOfMemoryError? Don't allow pending exceptions.
0N/A // This is a VM policy failure, so how do we exhaustively test it?
0N/A assert(!thread->has_pending_exception(),
0N/A "shouldn't be allocating with pending exception");
0N/A if (StrictSafepointChecks) {
0N/A assert(thread->allow_allocation(),
0N/A "Allocation done by thread for which allocation is blocked "
0N/A "by No_Allocation_Verifier!");
0N/A // Allocation of an oop can always invoke a safepoint,
0N/A // hence, the true argument
0N/A thread->check_for_valid_safepoint_state(true);
0N/A }
0N/A}
0N/A#endif
0N/A
0N/AHeapWord* CollectedHeap::allocate_from_tlab_slow(Thread* thread, size_t size) {
0N/A
0N/A // Retain tlab and allocate object in shared space if
0N/A // the amount free in the tlab is too large to discard.
0N/A if (thread->tlab().free() > thread->tlab().refill_waste_limit()) {
0N/A thread->tlab().record_slow_allocation(size);
0N/A return NULL;
0N/A }
0N/A
0N/A // Discard tlab and allocate a new one.
0N/A // To minimize fragmentation, the last TLAB may be smaller than the rest.
0N/A size_t new_tlab_size = thread->tlab().compute_size(size);
0N/A
0N/A thread->tlab().clear_before_allocation();
4596N/A
0N/A if (new_tlab_size == 0) {
0N/A return NULL;
0N/A }
0N/A
0N/A // Allocate a new TLAB...
0N/A HeapWord* obj = Universe::heap()->allocate_new_tlab(new_tlab_size);
0N/A if (obj == NULL) {
0N/A return NULL;
0N/A }
0N/A if (ZeroTLAB) {
0N/A // ..and clear it.
0N/A Copy::zero_to_words(obj, new_tlab_size);
0N/A } else {
0N/A // ...and zap just allocated object.
2080N/A#ifdef ASSERT
2080N/A // Skip mangling the space corresponding to the object header to
2080N/A // ensure that the returned space is not considered parsable by
2080N/A // any concurrent GC thread.
2080N/A size_t hdr_size = oopDesc::header_size();
2080N/A Copy::fill_to_words(obj + hdr_size, new_tlab_size - hdr_size, badHeapWordVal);
2080N/A#endif // ASSERT
2080N/A }
2080N/A thread->tlab().fill(obj, obj + size, new_tlab_size);
2080N/A return obj;
0N/A}
0N/A
0N/Avoid CollectedHeap::flush_deferred_store_barrier(JavaThread* thread) {
0N/A MemRegion deferred = thread->deferred_card_mark();
0N/A if (!deferred.is_empty()) {
0N/A assert(_defer_initial_card_mark, "Otherwise should be empty");
0N/A {
0N/A // Verify that the storage points to a parsable object in heap
0N/A DEBUG_ONLY(oop old_obj = oop(deferred.start());)
0N/A assert(is_in(old_obj), "Not in allocated heap");
0N/A assert(!can_elide_initializing_store_barrier(old_obj),
0N/A "Else should have been filtered in new_store_pre_barrier()");
0N/A assert(!is_in_permanent(old_obj), "Sanity: not expected");
2080N/A assert(old_obj->is_oop(true), "Not an oop");
0N/A assert(old_obj->is_parsable(), "Will not be concurrently parsable");
2080N/A assert(deferred.word_size() == (size_t)(old_obj->size()),
0N/A "Mismatch: multiple objects?");
0N/A }
0N/A BarrierSet* bs = barrier_set();
2080N/A assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
2080N/A bs->write_region(deferred);
2080N/A // "Clear" the deferred_card_mark field
2080N/A thread->set_deferred_card_mark(MemRegion());
2080N/A }
160N/A assert(thread->deferred_card_mark().is_empty(), "invariant");
160N/A}
0N/A
0N/A// Helper for ReduceInitialCardMarks. For performance,
0N/A// compiled code may elide card-marks for initializing stores
0N/A// to a newly allocated object along the fast-path. We
0N/A// compensate for such elided card-marks as follows:
0N/A// (a) Generational, non-concurrent collectors, such as
0N/A// GenCollectedHeap(ParNew,DefNew,Tenured) and
0N/A// ParallelScavengeHeap(ParallelGC, ParallelOldGC)
0N/A// need the card-mark if and only if the region is
0N/A// in the old gen, and do not care if the card-mark
0N/A// succeeds or precedes the initializing stores themselves,
0N/A// so long as the card-mark is completed before the next
0N/A// scavenge. For all these cases, we can do a card mark
0N/A// at the point at which we do a slow path allocation
0N/A// in the old gen, i.e. in this call.
0N/A// (b) GenCollectedHeap(ConcurrentMarkSweepGeneration) requires
0N/A// in addition that the card-mark for an old gen allocated
0N/A// object strictly follow any associated initializing stores.
0N/A// In these cases, the memRegion remembered below is
0N/A// used to card-mark the entire region either just before the next
0N/A// slow-path allocation by this thread or just before the next scavenge or
0N/A// CMS-associated safepoint, whichever of these events happens first.
0N/A// (The implicit assumption is that the object has been fully
0N/A// initialized by this point, a fact that we assert when doing the
0N/A// card-mark.)
0N/A// (c) G1CollectedHeap(G1) uses two kinds of write barriers. When a
0N/A// G1 concurrent marking is in progress an SATB (pre-write-)barrier is
0N/A// is used to remember the pre-value of any store. Initializing
459N/A// stores will not need this barrier, so we need not worry about
0N/A// compensating for the missing pre-barrier here. Turning now
0N/A// to the post-barrier, we note that G1 needs a RS update barrier
0N/A// which simply enqueues a (sequence of) dirty cards which may
0N/A// optionally be refined by the concurrent update threads. Note
0N/A// that this barrier need only be applied to a non-young write,
0N/A// but, like in CMS, because of the presence of concurrent refinement
0N/A// (much like CMS' precleaning), must strictly follow the oop-store.
0N/A// Thus, using the same protocol for maintaining the intended
160N/A// invariants turns out, serendepitously, to be the same for both
0N/A// G1 and CMS.
0N/A//
0N/A// For any future collector, this code should be reexamined with
0N/A// that specific collector in mind, and the documentation above suitably
0N/A// extended and updated.
160N/Aoop CollectedHeap::new_store_pre_barrier(JavaThread* thread, oop new_obj) {
0N/A // If a previous card-mark was deferred, flush it now.
160N/A flush_deferred_store_barrier(thread);
160N/A if (can_elide_initializing_store_barrier(new_obj)) {
160N/A // The deferred_card_mark region should be empty
160N/A // following the flush above.
160N/A assert(thread->deferred_card_mark().is_empty(), "Error");
160N/A } else {
160N/A MemRegion mr((HeapWord*)new_obj, new_obj->size());
160N/A assert(!mr.is_empty(), "Error");
160N/A if (_defer_initial_card_mark) {
160N/A // Defer the card mark
160N/A thread->set_deferred_card_mark(mr);
160N/A } else {
0N/A // Do the card mark
0N/A BarrierSet* bs = barrier_set();
0N/A assert(bs->has_write_region_opt(), "No write_region() on BarrierSet");
0N/A bs->write_region(mr);
0N/A }
0N/A }
0N/A return new_obj;
160N/A}
160N/A
160N/Asize_t CollectedHeap::filler_array_hdr_size() {
0N/A return size_t(align_object_offset(arrayOopDesc::header_size(T_INT))); // align to Long
0N/A}
0N/A
0N/Asize_t CollectedHeap::filler_array_min_size() {
0N/A return align_object_size(filler_array_hdr_size()); // align to MinObjAlignment
0N/A}
0N/A
0N/Asize_t CollectedHeap::filler_array_max_size() {
0N/A return _filler_array_max_size;
0N/A}
0N/A
0N/A#ifdef ASSERT
0N/Avoid CollectedHeap::fill_args_check(HeapWord* start, size_t words)
0N/A{
0N/A assert(words >= min_fill_size(), "too small to fill");
0N/A assert(words % MinObjAlignment == 0, "unaligned size");
0N/A assert(Universe::heap()->is_in_reserved(start), "not in heap");
0N/A assert(Universe::heap()->is_in_reserved(start + words - 1), "not in heap");
0N/A}
0N/A
0N/Avoid CollectedHeap::zap_filler_array(HeapWord* start, size_t words, bool zap)
0N/A{
3686N/A if (ZapFillerObjects && zap) {
0N/A Copy::fill_to_words(start + filler_array_hdr_size(),
0N/A words - filler_array_hdr_size(), 0XDEAFBABE);
0N/A }
0N/A}
0N/A#endif // ASSERT
0N/A
0N/Avoid
0N/ACollectedHeap::fill_with_array(HeapWord* start, size_t words, bool zap)
0N/A{
0N/A assert(words >= filler_array_min_size(), "too small for an array");
0N/A assert(words <= filler_array_max_size(), "too big for a single object");
0N/A
0N/A const size_t payload_size = words - filler_array_hdr_size();
0N/A const size_t len = payload_size * HeapWordSize / sizeof(jint);
160N/A
2080N/A // Set the length first for concurrent GC.
0N/A ((arrayOop)start)->set_length((int)len);
0N/A post_allocation_setup_common(Universe::intArrayKlassObj(), start, words);
0N/A DEBUG_ONLY(zap_filler_array(start, words, zap);)
0N/A}
0N/A
void
CollectedHeap::fill_with_object_impl(HeapWord* start, size_t words, bool zap)
{
assert(words <= filler_array_max_size(), "too big for a single object");
if (words >= filler_array_min_size()) {
fill_with_array(start, words, zap);
} else if (words > 0) {
assert(words == min_fill_size(), "unaligned size");
post_allocation_setup_common(SystemDictionary::Object_klass(), start,
words);
}
}
void CollectedHeap::fill_with_object(HeapWord* start, size_t words, bool zap)
{
DEBUG_ONLY(fill_args_check(start, words);)
HandleMark hm; // Free handles before leaving.
fill_with_object_impl(start, words, zap);
}
void CollectedHeap::fill_with_objects(HeapWord* start, size_t words, bool zap)
{
DEBUG_ONLY(fill_args_check(start, words);)
HandleMark hm; // Free handles before leaving.
#ifdef _LP64
// A single array can fill ~8G, so multiple objects are needed only in 64-bit.
// First fill with arrays, ensuring that any remaining space is big enough to
// fill. The remainder is filled with a single object.
const size_t min = min_fill_size();
const size_t max = filler_array_max_size();
while (words > max) {
const size_t cur = words - max >= min ? max : max - min;
fill_with_array(start, cur, zap);
start += cur;
words -= cur;
}
#endif
fill_with_object_impl(start, words, zap);
}
HeapWord* CollectedHeap::allocate_new_tlab(size_t size) {
guarantee(false, "thread-local allocation buffers not supported");
return NULL;
}
void CollectedHeap::ensure_parsability(bool retire_tlabs) {
// The second disjunct in the assertion below makes a concession
// for the start-up verification done while the VM is being
// created. Callers be careful that you know that mutators
// aren't going to interfere -- for instance, this is permissible
// if we are still single-threaded and have either not yet
// started allocating (nothing much to verify) or we have
// started allocating but are now a full-fledged JavaThread
// (and have thus made our TLAB's) available for filling.
assert(SafepointSynchronize::is_at_safepoint() ||
!is_init_completed(),
"Should only be called at a safepoint or at start-up"
" otherwise concurrent mutator activity may make heap "
" unparsable again");
const bool use_tlab = UseTLAB;
const bool deferred = _defer_initial_card_mark;
// The main thread starts allocating via a TLAB even before it
// has added itself to the threads list at vm boot-up.
assert(!use_tlab || Threads::first() != NULL,
"Attempt to fill tlabs before main thread has been added"
" to threads list is doomed to failure!");
for (JavaThread *thread = Threads::first(); thread; thread = thread->next()) {
if (use_tlab) thread->tlab().make_parsable(retire_tlabs);
#ifdef COMPILER2
// The deferred store barriers must all have been flushed to the
// card-table (or other remembered set structure) before GC starts
// processing the card-table (or other remembered set).
if (deferred) flush_deferred_store_barrier(thread);
#else
assert(!deferred, "Should be false");
assert(thread->deferred_card_mark().is_empty(), "Should be empty");
#endif
}
}
void CollectedHeap::accumulate_statistics_all_tlabs() {
if (UseTLAB) {
assert(SafepointSynchronize::is_at_safepoint() ||
!is_init_completed(),
"should only accumulate statistics on tlabs at safepoint");
ThreadLocalAllocBuffer::accumulate_statistics_before_gc();
}
}
void CollectedHeap::resize_all_tlabs() {
if (UseTLAB) {
assert(SafepointSynchronize::is_at_safepoint() ||
!is_init_completed(),
"should only resize tlabs at safepoint");
ThreadLocalAllocBuffer::resize_all_tlabs();
}
}
void CollectedHeap::pre_full_gc_dump() {
if (HeapDumpBeforeFullGC) {
TraceTime tt("Heap Dump (before full gc): ", PrintGCDetails, false, gclog_or_tty);
// We are doing a "major" collection and a heap dump before
// major collection has been requested.
HeapDumper::dump_heap();
}
if (PrintClassHistogramBeforeFullGC) {
TraceTime tt("Class Histogram (before full gc): ", PrintGCDetails, true, gclog_or_tty);
VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */, false /* ! prologue */);
inspector.doit();
}
}
void CollectedHeap::post_full_gc_dump() {
if (HeapDumpAfterFullGC) {
TraceTime tt("Heap Dump (after full gc): ", PrintGCDetails, false, gclog_or_tty);
HeapDumper::dump_heap();
}
if (PrintClassHistogramAfterFullGC) {
TraceTime tt("Class Histogram (after full gc): ", PrintGCDetails, true, gclog_or_tty);
VM_GC_HeapInspection inspector(gclog_or_tty, false /* ! full gc */, false /* ! prologue */);
inspector.doit();
}
}
oop CollectedHeap::Class_obj_allocate(KlassHandle klass, int size, KlassHandle real_klass, TRAPS) {
debug_only(check_for_valid_allocation_state());
assert(!Universe::heap()->is_gc_active(), "Allocation during gc not allowed");
assert(size >= 0, "int won't convert to size_t");
HeapWord* obj;
if (JavaObjectsInPerm) {
obj = common_permanent_mem_allocate_init(size, CHECK_NULL);
} else {
assert(ScavengeRootsInCode > 0, "must be");
obj = common_mem_allocate_init(size, CHECK_NULL);
}
post_allocation_setup_common(klass, obj, size);
assert(Universe::is_bootstrapping() ||
!((oop)obj)->blueprint()->oop_is_array(), "must not be an array");
NOT_PRODUCT(Universe::heap()->check_for_bad_heap_word_value(obj, size));
oop mirror = (oop)obj;
java_lang_Class::set_oop_size(mirror, size);
// Setup indirections
if (!real_klass.is_null()) {
java_lang_Class::set_klass(mirror, real_klass());
real_klass->set_java_mirror(mirror);
}
instanceMirrorKlass* mk = instanceMirrorKlass::cast(mirror->klass());
assert(size == mk->instance_size(real_klass), "should have been set");
// notify jvmti and dtrace
post_allocation_notify(klass, (oop)obj);
return mirror;
}